conditional-logic.php
1 year ago
field.php
2 months ago
fields.php
2 months ago
index.php
1 year ago
list-empty.php
8 months ago
location-group.php
1 year ago
location-rule.php
8 months ago
locations.php
8 months ago
options.php
8 months ago
conditional-logic.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | // vars |
| 4 | $disabled = false; |
| 5 | |
| 6 | // empty |
| 7 | if ( empty( $field['conditional_logic'] ) ) { |
| 8 | $disabled = true; |
| 9 | $field['conditional_logic'] = array( |
| 10 | |
| 11 | // group 0 |
| 12 | array( |
| 13 | |
| 14 | // rule 0 |
| 15 | array(), |
| 16 | ), |
| 17 | ); |
| 18 | } |
| 19 | |
| 20 | ?> |
| 21 | <div class="acf-field acf-field-true-false acf-field-setting-conditional_logic" data-type="true_false" data-name="conditional_logic"> |
| 22 | <div class="acf-conditional-toggle"> |
| 23 | <div class="acf-label"> |
| 24 | <?php $acf_label_for = acf_idify( $field['prefix'] . '[conditional_logic]' ); ?> |
| 25 | <label for="<?php echo esc_attr( $acf_label_for ); ?>"><?php esc_html_e( 'Conditional Logic', 'secure-custom-fields' ); ?></label> |
| 26 | </div> |
| 27 | <div class="acf-input"> |
| 28 | <?php |
| 29 | |
| 30 | acf_render_field( |
| 31 | array( |
| 32 | 'type' => 'true_false', |
| 33 | 'name' => 'conditional_logic', |
| 34 | 'prefix' => $field['prefix'], |
| 35 | 'value' => $disabled ? 0 : 1, |
| 36 | 'ui' => 1, |
| 37 | 'class' => 'conditions-toggle', |
| 38 | ) |
| 39 | ); |
| 40 | |
| 41 | ?> |
| 42 | |
| 43 | </div> |
| 44 | </div> |
| 45 | <div class="rule-groups" |
| 46 | <?php |
| 47 | if ( $disabled ) { |
| 48 | echo ' style="display:none"'; |
| 49 | } |
| 50 | ?> |
| 51 | > |
| 52 | <?php |
| 53 | foreach ( $field['conditional_logic'] as $group_id => $group ) : |
| 54 | |
| 55 | // validate |
| 56 | if ( empty( $group ) ) { |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | // vars |
| 62 | // $group_id must be completely different to $rule_id to avoid JS issues |
| 63 | $group_id = "group_{$group_id}"; |
| 64 | $h4 = ( $group_id == 'group_0' ) ? __( 'Show this field if', 'secure-custom-fields' ) : __( 'or', 'secure-custom-fields' ); |
| 65 | |
| 66 | ?> |
| 67 | <div class="rule-group" data-id="<?php echo esc_attr( $group_id ); ?>"> |
| 68 | |
| 69 | <h4><?php echo esc_html( $h4 ); ?></h4> |
| 70 | |
| 71 | <table class="acf-table -clear"> |
| 72 | <tbody> |
| 73 | <?php |
| 74 | foreach ( $group as $rule_id => $rule ) : |
| 75 | |
| 76 | // valid rule |
| 77 | $rule = wp_parse_args( |
| 78 | $rule, |
| 79 | array( |
| 80 | 'field' => '', |
| 81 | 'operator' => '', |
| 82 | 'value' => '', |
| 83 | ) |
| 84 | ); |
| 85 | |
| 86 | |
| 87 | // vars |
| 88 | // $group_id must be completely different to $rule_id to avoid JS issues |
| 89 | $rule_id = "rule_{$rule_id}"; |
| 90 | $prefix = "{$field['prefix']}[conditional_logic][{$group_id}][{$rule_id}]"; |
| 91 | |
| 92 | // data attributes |
| 93 | $attributes = array( |
| 94 | 'data-id' => $rule_id, |
| 95 | 'data-field' => $rule['field'], |
| 96 | 'data-operator' => $rule['operator'], |
| 97 | 'data-value' => $rule['value'], |
| 98 | ); |
| 99 | |
| 100 | ?> |
| 101 | <tr class="rule" <?php echo acf_esc_attrs( $attributes ); ?>> |
| 102 | <td class="param"> |
| 103 | <?php |
| 104 | |
| 105 | acf_render_field( |
| 106 | array( |
| 107 | 'type' => 'select', |
| 108 | 'prefix' => $prefix, |
| 109 | 'name' => 'field', |
| 110 | 'class' => 'condition-rule-field', |
| 111 | 'disabled' => $disabled, |
| 112 | 'value' => $rule['field'], |
| 113 | 'choices' => array( |
| 114 | $rule['field'] => $rule['field'], |
| 115 | ), |
| 116 | ) |
| 117 | ); |
| 118 | |
| 119 | ?> |
| 120 | </td> |
| 121 | <td class="operator"> |
| 122 | <?php |
| 123 | |
| 124 | acf_render_field( |
| 125 | array( |
| 126 | 'type' => 'select', |
| 127 | 'prefix' => $prefix, |
| 128 | 'name' => 'operator', |
| 129 | 'class' => 'condition-rule-operator', |
| 130 | 'disabled' => $disabled, |
| 131 | 'value' => $rule['operator'], |
| 132 | 'choices' => array( |
| 133 | $rule['operator'] => $rule['operator'], |
| 134 | ), |
| 135 | ) |
| 136 | ); |
| 137 | |
| 138 | ?> |
| 139 | </td> |
| 140 | <td class="value"> |
| 141 | <?php |
| 142 | $conditional_field = get_field_object( $rule['field'] ); |
| 143 | |
| 144 | /** |
| 145 | * Filters the choices available for a conditional logic rule. |
| 146 | * |
| 147 | * @since ACF 6.3.0 |
| 148 | * |
| 149 | * @param array $choices The available choices. |
| 150 | * @param array $conditional_field The field object for the conditional field. |
| 151 | * @param mixed $value The value of the rule. |
| 152 | */ |
| 153 | $choices = apply_filters( 'acf/conditional_logic/choices', array( $rule['value'] => $rule['value'] ), $conditional_field, $rule['value'] ); |
| 154 | |
| 155 | acf_render_field( |
| 156 | array( |
| 157 | 'type' => 'select', |
| 158 | 'prefix' => $prefix, |
| 159 | 'name' => 'value', |
| 160 | 'class' => 'condition-rule-value', |
| 161 | 'disabled' => $disabled, |
| 162 | 'value' => $rule['value'], |
| 163 | 'choices' => $choices, |
| 164 | ) |
| 165 | ); |
| 166 | ?> |
| 167 | </td> |
| 168 | <td class="add"> |
| 169 | <a href="#" class="button add-conditional-rule"><?php esc_html_e( 'and', 'secure-custom-fields' ); ?></a> |
| 170 | </td> |
| 171 | <td class="remove"> |
| 172 | <a href="#" class="acf-icon -minus remove-conditional-rule"></a> |
| 173 | </td> |
| 174 | </tr> |
| 175 | <?php endforeach; ?> |
| 176 | </tbody> |
| 177 | </table> |
| 178 | |
| 179 | </div> |
| 180 | <?php endforeach; ?> |
| 181 | |
| 182 | <h4><?php esc_html_e( 'or', 'secure-custom-fields' ); ?></h4> |
| 183 | |
| 184 | <a href="#" class="button add-conditional-group"><?php esc_html_e( 'Add rule group', 'secure-custom-fields' ); ?></a> |
| 185 | </div> |
| 186 | </div> |
| 187 |