|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Automatic field-dependencies scripts calculation for Kirki controls. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Kirki |
|
6
|
|
|
* @category Modules |
|
7
|
|
|
* @author Aristeides Stathopoulos |
|
8
|
|
|
* @copyright Copyright (c) 2017, Aristeides Stathopoulos |
|
9
|
|
|
* @license http://opensource.org/licenses/https://opensource.org/licenses/MIT |
|
10
|
|
|
* @since 3.0.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
// Exit if accessed directly. |
|
14
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
15
|
|
|
exit; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Adds styles to the customizer. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Kirki_Modules_Field_Dependencies { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The object instance. |
|
25
|
|
|
* |
|
26
|
|
|
* @static |
|
27
|
|
|
* @access private |
|
28
|
|
|
* @since 3.0.0 |
|
29
|
|
|
* @var object |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $instance; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @access protected |
|
37
|
|
|
* @since 3.0.0 |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function __construct() { |
|
40
|
|
|
add_action( 'customize_controls_enqueue_scripts', array( $this, 'field_dependencies' ) ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Gets an instance of this object. |
|
45
|
|
|
* Prevents duplicate instances which avoid artefacts and improves performance. |
|
46
|
|
|
* |
|
47
|
|
|
* @static |
|
48
|
|
|
* @access public |
|
49
|
|
|
* @since 3.0.0 |
|
50
|
|
|
* @return object |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function get_instance() { |
|
53
|
|
|
if ( ! self::$instance ) { |
|
54
|
|
|
self::$instance = new self(); |
|
55
|
|
|
} |
|
56
|
|
|
return self::$instance; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Enqueues the field-dependencies script |
|
61
|
|
|
* and adds variables to it using the wp_localize_script function. |
|
62
|
|
|
* The rest is handled via JS. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function field_dependencies() { |
|
65
|
|
|
|
|
66
|
|
|
wp_enqueue_script( 'kirki_field_dependencies', trailingslashit( Kirki::$url ) . 'modules/field-dependencies/field-dependencies.js', array( 'jquery', 'customize-base', 'customize-controls' ), false, true ); |
|
67
|
|
|
$field_dependencies = array(); |
|
68
|
|
|
$fields = Kirki::$fields; |
|
69
|
|
|
foreach ( $fields as $field ) { |
|
70
|
|
|
if ( isset( $field['required'] ) && is_array( $field['required'] ) && ! empty( $field['required'] ) ) { |
|
71
|
|
|
$field_dependencies[ $field['id'] ] = $field['required']; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
wp_localize_script( 'kirki_field_dependencies', 'fieldDependencies', $field_dependencies ); |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|