Skip to content

Commit

Permalink
feat: add new condition options and improve UX & UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Sep 23, 2024
1 parent 4ccef29 commit dd15e55
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 127 deletions.
74 changes: 72 additions & 2 deletions classes/fields.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,18 @@ function render_all_input_types( $name, $data, $fields_type, $field_index, $valu
$html_input .= '<div class="row ppom-condition-clone-js">';
foreach ( $condition_rules as $rule_index => $condition ) {

$element_values = isset( $condition['element_values'] ) ? stripslashes( $condition['element_values'] ) : '';
$element = isset( $condition['elements'] ) ? stripslashes( $condition['elements'] ) : '';
$element_values = isset( $condition['element_values'] ) ? stripslashes( $condition['element_values'] ) : '';
$element = isset( $condition['elements'] ) ? stripslashes( $condition['elements'] ) : '';
$element_constant_value = isset( $condition['element_constant'] ) ? stripslashes( $condition['element_constant'] ) : '';

$element_between_value_to = '';
$element_between_value_from = '';

if ( isset( $condition['cond-between-interval'] ) && is_array( $condition['cond-between-interval'] ) ) {
$element_between_value_to = isset( $condition['cond-between-interval']['to'] ) ? $condition['cond-between-interval']['to'] : '';
$element_between_value_from = isset( $condition['cond-between-interval']['from'] ) ? $condition['cond-between-interval']['from'] : '';
}

$operator_is = ( $condition['operators'] == 'is' ) ? 'selected="selected"' : '';
$operator_not = ( $condition['operators'] == 'not' ) ? 'selected="selected"' : '';
$operator_greater = ( $condition['operators'] == 'greater than' ) ? 'selected="selected"' : '';
Expand All @@ -826,12 +836,39 @@ function render_all_input_types( $name, $data, $fields_type, $field_index, $valu
$html_input .= '</div>';

// is value meta
$pro_enabled = ppom_pro_is_installed() && 'valid' === apply_filters( 'product_ppom_license_status', '' );

$html_input .= '<div class="col-md-2 col-sm-2">';
$html_input .= '<select name="ppom[' . esc_attr( $field_index ) . '][conditions][rules][' . esc_attr( $rule_index ) . '][operators]" class="form-control ppom-conditional-keys" data-metatype="operators">';


$html_input .= '<optgroup label="' . __( 'Any type field', 'woocommerce-product-addon' ) . '">';
$html_input .= '<option ' . selected( 'any', $condition['operators'], false ) . ' value="any">' . __( 'has any value', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '<option ' . selected( 'empty', $condition['operators'], false ) . ' value="empty">' . __( 'is empty', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '</optgroup>';

$html_input .= '<optgroup label="' . __( 'Select type field', 'woocommerce-product-addon' ) . '">';
$html_input .= '<option ' . $operator_is . ' value="is">' . __( 'is', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '<option ' . $operator_not . ' value="not">' . __( 'not', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '<option ' . $operator_greater . ' value="greater than">' . __( 'greater than', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '<option ' . $operator_less . ' value="less than">' . __( 'less than', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '</optgroup>';


// PRO Options
$html_input .= '<optgroup label="' . __( 'Text type field', 'woocommerce-product-addon' ) . '">';
$html_input .= '<option ' . selected( 'contains', $condition['operators'], false ) . ' value="' . ( $pro_enabled ? 'contains' : '' ) . '"' . disabled( false, $pro_enabled, false ) . '>' . __( 'contains', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '<option ' . selected( 'not-contains', $condition['operators'], false ) . ' value="' . ( $pro_enabled ? 'not-contains' : '' ) . '"' . disabled( false, $pro_enabled, false ) . '>' . __( 'does not contains', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '<option ' . selected( 'regex', $condition['operators'], false ) . ' value="' . ( $pro_enabled ? 'regex' : '' ) . '"' . disabled( false, $pro_enabled, false ) . '>' . __( 'matches regex', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '</optgroup>';

$html_input .= '<optgroup label="' . __( 'Numeric type field', 'woocommerce-product-addon' ) . '">';
$html_input .= '<option ' . selected( 'between', $condition['operators'], false ) . ' value="' . ( $pro_enabled ? 'between' : '' ) . '">' . __( 'between', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '<option ' . selected( 'number-multiplier', $condition['operators'], false ) . ' value="' . ( $pro_enabled ? 'number-multiplier' : '' ) . '">' . __( 'is multiple of number', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '<option ' . selected( 'even-number', $condition['operators'], false ) . ' value="' . ( $pro_enabled ? 'even-number' : '' ) . '">' . __( 'is even', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '<option ' . selected( 'odd-number', $condition['operators'], false ) . ' value="' . ( $pro_enabled ? 'odd-number' : '' ) . '">' . __( 'is odd', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '</optgroup>';

$html_input .= '</select> ';
$html_input .= '</div>';

Expand All @@ -842,6 +879,13 @@ function render_all_input_types( $name, $data, $fields_type, $field_index, $valu
data-existingvalue="' . esc_attr( $element_values ) . '" >';
$html_input .= '<option>' . $element_values . '</option>';
$html_input .= '</select>';
$html_input .= '<input name="ppom[' . esc_attr( $field_index ) . '][conditions][rules][' . esc_attr( $rule_index ) . '][element_constant]" class="form-control ppom-conditional-keys ppom-hide-element" data-metatype="element_constant" value="' . esc_attr( $element_constant_value ) . '" >';

$html_input .= '<div class="ppom-between-input-container"> ';
$html_input .= '<input type="number" name="ppom[' . esc_attr( $field_index ) . '][conditions][rules][' . esc_attr( $rule_index ) . '][cond-between-interval][from]" class="form-control ppom-conditional-keys" data-metatype="between-input-from" value="' . esc_attr( $element_between_value_from ) . '" pattern="^\\d+(\\.\\d{1,4})?$">';
$html_input .= '<span>' . __( 'and', 'woocommerce-product-addon' ) . '</span>';
$html_input .= '<input type="number" name="ppom[' . esc_attr( $field_index ) . '][conditions][rules][' . esc_attr( $rule_index ) . '][cond-between-interval][to]" class="form-control ppom-conditional-keys" data-metatype="between-input-to" value="' . esc_attr( $element_between_value_to ) . '" pattern="^\\d+(\\.\\d{1,4})?$">';
$html_input .= '</div>';

// $html_input .= '<input type="text" name="ppom['.esc_attr($field_index).'][conditions][rules]['.esc_attr($rule_index).'][element_values]" class="form-control ppom-conditional-keys" value="'.esc_attr($element_values).'" placeholder="Enter Option" data-metatype="element_values">';
$html_input .= '</div>';
Expand Down Expand Up @@ -891,12 +935,38 @@ function render_all_input_types( $name, $data, $fields_type, $field_index, $valu
$html_input .= '</div>';

// is
$pro_enabled = ppom_pro_is_installed() && 'valid' === apply_filters( 'product_ppom_license_status', '' );

$html_input .= '<div class="col-md-2 col-sm-2">';
$html_input .= '<select data-metatype="operators" class="ppom-conditional-keys form-control">';

$html_input .= '<optgroup label="' . __( 'Any type field', 'woocommerce-product-addon' ) . '">';
$html_input .= '<option value="any">' . __( 'has any value', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '<option value="empty">' . __( 'is empty', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '</optgroup>';

$html_input .= '<optgroup label="' . __( 'Select type field', 'woocommerce-product-addon' ) . '">';
$html_input .= '<option value="is">' . __( 'is', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '<option value="not">' . __( 'not', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '<option value="greater than">' . __( 'greater than', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '<option value="less than">' . __( 'less than', 'woocommerce-product-addon' ) . '</option>';
$html_input .= '</optgroup>';


// PRO Options
$html_input .= '<optgroup label="' . __( 'Text type field', 'woocommerce-product-addon' ) . '">';
$html_input .= '<option value="' . ( $pro_enabled ? 'contains' : '' ) . '"' . disabled( false, $pro_enabled, false ) . '>' . __( 'contains', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '<option value="' . ( $pro_enabled ? 'not-contains' : '' ) . '"' . disabled( false, $pro_enabled, false ) . '>' . __( 'does not contains', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '<option value="' . ( $pro_enabled ? 'regex' : '' ) . '"' . disabled( false, $pro_enabled, false ) . '>' . __( 'matchs regex', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '</optgroup>';

$html_input .= '<optgroup label="' . __( 'Numeric type field', 'woocommerce-product-addon' ) . '">';
$html_input .= '<option value="' . ( $pro_enabled ? 'even-number' : '' ) . '"' . disabled( false, $pro_enabled, false ) . '>' . __( 'is even', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '<option value="' . ( $pro_enabled ? 'odd-number' : '' ) . '"' . disabled( false, $pro_enabled, false ) . '>' . __( 'is odd', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '<option value="' . ( $pro_enabled ? 'between' : '' ) . '"' . disabled( false, $pro_enabled, false ) . '>' . __( 'between', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '<option value="' . ( $pro_enabled ? 'number-multiplier' : '' ) . '"' . disabled( false, $pro_enabled, false ) . '>' . __( 'is multiple of number', 'woocommerce-product-addon' ) . ( ! $pro_enabled ? ' (PRO)' : '' ) . '</option>';
$html_input .= '</optgroup>';

$html_input .= '</select> ';
$html_input .= '</div>';

Expand Down
19 changes: 19 additions & 0 deletions css/ppom-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -1370,3 +1370,22 @@ a.ppom-repeater-learn-more:focus {
.ppom-admin-tabs-css .ppom-admin-tab-content .notice-info p {
margin: 0;
}
.ppom-hide-element {
display: none !important;
}

.ppom-invisible-element {
visibility: hidden;
}

.ppom-between-input-container {
display: flex;
flex-direction: row;
align-items: center;

gap: 10px;
}

.ppom-between-input-container span {
font-size: 16px;
}
53 changes: 41 additions & 12 deletions inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2347,10 +2347,26 @@ function ppom_get_conditional_data_attributes( $meta ) {

$bound = isset( $conditions['bound'] ) ? ppom_wpml_translate( $conditions['bound'], 'PPOM' ) : '';
$visibility = isset( $conditions['visibility'] ) ? ppom_wpml_translate( $conditions['visibility'], 'PPOM' ) : '';

$conditions['rules'] = array_filter(
$conditions['rules'],
function( $rule ) {
if ( in_array( $rule['operators'], array( 'any', 'empty', 'even-number', 'odd-number', 'regex') ) ) {
return true;
}

if ( in_array( $rule['operators'], array( 'number-multiplier', 'regex', 'contains', 'not-contains') ) ) {
return ! empty( $rule['element_constant'] );
}

if ( 'between' === $rule['operators'] ) {
return (
! empty( $rule['cond-between-interval']) && is_array( $rule['cond-between-interval'] )
&& isset( $rule['cond-between-interval']['to'] )
&& isset( $rule['cond-between-interval']['from'] )
);
}

return ! empty( $rule['element_values'] );
}
);
Expand All @@ -2360,19 +2376,32 @@ function( $rule ) {
$attr_html .= ' data-cond-bind="' . esc_attr( $bound ) . '"';
$attr_html .= ' data-cond-visibility="' . esc_attr( strtolower( $visibility ) ) . '"';

$index = 0;
$index = 0;
$pro_enabled = ppom_pro_is_installed() && 'valid' === apply_filters( 'product_ppom_license_status', '' );

foreach ( $conditions['rules'] as $rule ) {

$counter = ++ $index;
$input = 'input' . $counter;
$value = 'val' . $counter;
$opr = 'operator' . $counter;
$element = isset( $rule['elements'] ) ? ppom_wpml_translate( $rule['elements'], 'PPOM' ) : '';
$element_val = isset( $rule['element_values'] ) ? ppom_wpml_translate( $rule['element_values'], 'PPOM' ) : '';
$operator = isset( $rule['operators'] ) ? ppom_wpml_translate( $rule['operators'], 'PPOM' ) : '';
$attr_html .= ' data-cond-' . $input . '="' . esc_attr( $element ) . '"';
$attr_html .= ' data-cond-' . $value . '="' . esc_attr( $element_val ) . '"';
$attr_html .= ' data-cond-' . $opr . '="' . esc_attr( $operator ) . '"';
$counter = ++ $index;
$input = 'input' . $counter;
$value = 'val' . $counter;
$opr = 'operator' . $counter;
$element = isset( $rule['elements'] ) ? ppom_wpml_translate( $rule['elements'], 'PPOM' ) : '';
$element_val = isset( $rule['element_values'] ) ? ppom_wpml_translate( $rule['element_values'], 'PPOM' ) : '';
$constant_val = isset( $rule['element_constant'] ) ? ppom_wpml_translate( $rule['element_constant'], 'PPOM' ) : '';
$operator = isset( $rule['operators'] ) ? ppom_wpml_translate( $rule['operators'], 'PPOM' ) : '';

$attr_html .= ' data-cond-' . $input . '="' . esc_attr( $element ) . '"';
$attr_html .= ' data-cond-' . $value . '="' . esc_attr( $element_val ) . '"';
$attr_html .= ' data-cond-' . $opr . '="' . esc_attr( $operator ) . '"';

if ( $pro_enabled && ! empty( $constant_val ) ) {
$attr_html .= ' data-cond-constant-val-' . $counter . '="' . esc_attr( $constant_val ) . '"';
}

if ( $pro_enabled && 'between' === $operator && isset( $rule['cond-between-interval'] ) ) {
$attr_html .= ' data-cond-between-from-' . $counter . '="' . esc_attr( $rule['cond-between-interval']['from'] ) . '"';
$attr_html .= ' data-cond-between-to-' . $counter . '="' . esc_attr( $rule['cond-between-interval']['to'] ) . '"';
}
}
}

Expand Down
Loading

0 comments on commit dd15e55

Please sign in to comment.