field-group-field-conditional-logic.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // vars
  3. $disabled = false;
  4. // empty
  5. if ( empty( $field['conditional_logic'] ) ) {
  6. $disabled = true;
  7. $field['conditional_logic'] = array(
  8. // group 0
  9. array(
  10. // rule 0
  11. array(),
  12. ),
  13. );
  14. }
  15. ?>
  16. <div class="acf-field acf-field-true-false acf-field-setting-conditional_logic" data-type="true_false" data-name="conditional_logic">
  17. <div class="acf-conditional-toggle">
  18. <div class="acf-label">
  19. <?php $acf_label_for = acf_idify( $field['prefix'] . '[conditional_logic]' ); ?>
  20. <label for="<?php echo esc_attr( $acf_label_for ); ?>"><?php _e( 'Conditional Logic', 'acf' ); ?></label>
  21. </div>
  22. <div class="acf-input">
  23. <?php
  24. acf_render_field(
  25. array(
  26. 'type' => 'true_false',
  27. 'name' => 'conditional_logic',
  28. 'prefix' => $field['prefix'],
  29. 'value' => $disabled ? 0 : 1,
  30. 'ui' => 1,
  31. 'class' => 'conditions-toggle',
  32. )
  33. );
  34. ?>
  35. </div>
  36. </div>
  37. <div class="rule-groups" <?php if ( $disabled ) echo ' style="display:none"'; ?>>
  38. <?php
  39. foreach ( $field['conditional_logic'] as $group_id => $group ) :
  40. // validate
  41. if ( empty( $group ) ) {
  42. continue;
  43. }
  44. // vars
  45. // $group_id must be completely different to $rule_id to avoid JS issues
  46. $group_id = "group_{$group_id}";
  47. $h4 = ( $group_id == 'group_0' ) ? __( 'Show this field if', 'acf' ) : __( 'or', 'acf' );
  48. ?>
  49. <div class="rule-group" data-id="<?php echo $group_id; ?>">
  50. <h4><?php echo $h4; ?></h4>
  51. <table class="acf-table -clear">
  52. <tbody>
  53. <?php
  54. foreach ( $group as $rule_id => $rule ) :
  55. // valid rule
  56. $rule = wp_parse_args(
  57. $rule,
  58. array(
  59. 'field' => '',
  60. 'operator' => '',
  61. 'value' => '',
  62. )
  63. );
  64. // vars
  65. // $group_id must be completely different to $rule_id to avoid JS issues
  66. $rule_id = "rule_{$rule_id}";
  67. $prefix = "{$field['prefix']}[conditional_logic][{$group_id}][{$rule_id}]";
  68. // data attributes
  69. $attributes = array(
  70. 'data-id' => $rule_id,
  71. 'data-field' => $rule['field'],
  72. 'data-operator' => $rule['operator'],
  73. 'data-value' => $rule['value'],
  74. );
  75. ?>
  76. <tr class="rule" <?php echo acf_esc_attrs( $attributes ); ?>>
  77. <td class="param">
  78. <?php
  79. acf_render_field(
  80. array(
  81. 'type' => 'select',
  82. 'prefix' => $prefix,
  83. 'name' => 'field',
  84. 'class' => 'condition-rule-field',
  85. 'disabled' => $disabled,
  86. 'value' => $rule['field'],
  87. 'choices' => array(
  88. $rule['field'] => $rule['field'],
  89. ),
  90. )
  91. );
  92. ?>
  93. </td>
  94. <td class="operator">
  95. <?php
  96. acf_render_field(
  97. array(
  98. 'type' => 'select',
  99. 'prefix' => $prefix,
  100. 'name' => 'operator',
  101. 'class' => 'condition-rule-operator',
  102. 'disabled' => $disabled,
  103. 'value' => $rule['operator'],
  104. 'choices' => array(
  105. $rule['operator'] => $rule['operator'],
  106. ),
  107. )
  108. );
  109. ?>
  110. </td>
  111. <td class="value">
  112. <?php
  113. // create field
  114. acf_render_field(
  115. array(
  116. 'type' => 'select',
  117. 'prefix' => $prefix,
  118. 'name' => 'value',
  119. 'class' => 'condition-rule-value',
  120. 'disabled' => $disabled,
  121. 'value' => $rule['value'],
  122. 'choices' => array(
  123. $rule['value'] => $rule['value'],
  124. ),
  125. )
  126. );
  127. ?>
  128. </td>
  129. <td class="add">
  130. <a href="#" class="button add-conditional-rule"><?php _e( 'and', 'acf' ); ?></a>
  131. </td>
  132. <td class="remove">
  133. <a href="#" class="acf-icon -minus remove-conditional-rule"></a>
  134. </td>
  135. </tr>
  136. <?php endforeach; ?>
  137. </tbody>
  138. </table>
  139. </div>
  140. <?php endforeach; ?>
  141. <h4><?php _e( 'or', 'acf' ); ?></h4>
  142. <a href="#" class="button add-conditional-group"><?php _e( 'Add rule group', 'acf' ); ?></a>
  143. </div>
  144. </div>