Field_Dependencies.php
173 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Automatic field-dependencies scripts calculation for Kirki controls. |
| 4 | * |
| 5 | * @package kirki-framework/module-field-dependencies |
| 6 | * @author Themeum |
| 7 | * @copyright Copyright (c) 2023, Themeum |
| 8 | * @license https://opensource.org/licenses/MIT |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Kirki\Module; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | use Kirki\URL; |
| 19 | |
| 20 | /** |
| 21 | * Field dependencies. |
| 22 | */ |
| 23 | class Field_Dependencies { |
| 24 | |
| 25 | /** |
| 26 | * The class instance. |
| 27 | * |
| 28 | * @static |
| 29 | * @access private |
| 30 | * @since 1.0.0 |
| 31 | * @var object |
| 32 | */ |
| 33 | private static $instance; |
| 34 | |
| 35 | /** |
| 36 | * An array of field dependencies. |
| 37 | * |
| 38 | * @access private |
| 39 | * @since 1.0.0 |
| 40 | * @var array |
| 41 | */ |
| 42 | private $dependencies = []; |
| 43 | |
| 44 | /** |
| 45 | * An array of all repeater controls available. |
| 46 | * Regardless if it has [active_callback] or not. |
| 47 | * |
| 48 | * @access private |
| 49 | * @since 4.1.1 |
| 50 | * @var array |
| 51 | */ |
| 52 | private $repeater_controls = []; |
| 53 | |
| 54 | /** |
| 55 | * Get the one, true instance of this class. |
| 56 | * |
| 57 | * @static |
| 58 | * @access public |
| 59 | * @since 1.0.0 |
| 60 | * @return object |
| 61 | */ |
| 62 | public static function get_instance() { |
| 63 | if ( null === self::$instance ) { |
| 64 | self::$instance = new self(); |
| 65 | } |
| 66 | return self::$instance; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Constructor. |
| 71 | * |
| 72 | * @access public |
| 73 | * @since 1.0.0 |
| 74 | */ |
| 75 | public function __construct() { |
| 76 | |
| 77 | add_action( 'customize_controls_enqueue_scripts', [ $this, 'field_dependencies' ] ); |
| 78 | add_filter( 'kirki_field_add_control_args', [ $this, 'field_add_control_args' ] ); |
| 79 | |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Collect all Repeater Controls in a new dependencies array |
| 84 | * other than [kirkiControlDependencies] |
| 85 | * to fix the issue that we can't find the repeater control |
| 86 | * in the array [kirkiControlDependencies] because |
| 87 | * it doesn't have [active_callback] array |
| 88 | * |
| 89 | * Now, We can use [active_callback] array in the repeater's childern |
| 90 | * |
| 91 | * @access private |
| 92 | * @since 4.1.1 |
| 93 | * @param array $args The field arguments. |
| 94 | * @return void |
| 95 | */ |
| 96 | private function field_add_repeater_controls( $args ) { |
| 97 | $type = isset( $args['type'] ) ? $args['type'] : ''; |
| 98 | if ( in_array( $type, array( 'repeater', 'kirki-repeater' ), true ) ) { |
| 99 | $this->repeater_controls[$args['settings']] = '__return_true'; |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Filter control arguments. |
| 106 | * |
| 107 | * @access public |
| 108 | * @since 1.0.0 |
| 109 | * @param array $args The field arguments. |
| 110 | * @return array |
| 111 | */ |
| 112 | public function field_add_control_args( $args ) { |
| 113 | |
| 114 | // Collect a list of all Repeater Controls available |
| 115 | $this->field_add_repeater_controls( $args ); |
| 116 | |
| 117 | if ( isset( $args['active_callback'] ) ) { |
| 118 | if ( is_array( $args['active_callback'] ) ) { |
| 119 | if ( ! is_callable( $args['active_callback'] ) ) { |
| 120 | |
| 121 | // Bugfix for https://github.com/aristath/kirki/issues/1961. |
| 122 | foreach ( $args['active_callback'] as $key => $val ) { |
| 123 | if ( is_callable( $val ) ) { |
| 124 | unset( $args['active_callback'][ $key ] ); |
| 125 | } |
| 126 | } |
| 127 | if ( isset( $args['active_callback'][0] ) ) { |
| 128 | $args['required'] = $args['active_callback']; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if ( ! empty( $args['required'] ) ) { |
| 134 | $this->dependencies[ $args['settings'] ] = $args['required']; |
| 135 | $args['active_callback'] = '__return_true'; |
| 136 | return $args; |
| 137 | } |
| 138 | |
| 139 | // No need to proceed any further if we're using the default value. |
| 140 | if ( '__return_true' === $args['active_callback'] ) { |
| 141 | return $args; |
| 142 | } |
| 143 | |
| 144 | // Make sure the function is callable, otherwise fallback to __return_true. |
| 145 | if ( ! is_callable( $args['active_callback'] ) ) { |
| 146 | $args['active_callback'] = '__return_true'; |
| 147 | } |
| 148 | } else { |
| 149 | // The ReactSelect field triggered from Background field doesn't have $args['active_callback'] argument. |
| 150 | if ( ! empty( $args['required'] ) ) { |
| 151 | $this->dependencies[ $args['settings'] ] = $args['required']; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return $args; |
| 156 | |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Enqueues the field-dependencies script |
| 161 | * and adds variables to it using the wp_localize_script function. |
| 162 | * The rest is handled via JS. |
| 163 | * |
| 164 | * @access public |
| 165 | * @return void |
| 166 | */ |
| 167 | public function field_dependencies() { |
| 168 | |
| 169 | wp_localize_script( 'kirki-customizer', 'kirkiControlDependencies', $this->dependencies ); |
| 170 | wp_localize_script( 'kirki-customizer', 'kirkiRepeaterControlsAvailable', $this->repeater_controls ); |
| 171 | |
| 172 | } |
| 173 | } |