Completed
Push — develop ( 464947...308109 )
by Aristeides
02:38
created

Kirki_Modules_Field_Dependencies::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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) 2016, Aristeides Stathopoulos
9
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
10
 * @since       2.4.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
	 * Constructor.
25
	 *
26
	 * @access public
27
	 * @since 2.4.0
28
	 */
29
	public function __construct() {
30
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'field_dependencies' ) );
31
	}
32
33
	/**
34
	 * Enqueues the field-dependencies script
35
	 * and adds variables to it using the wp_localize_script function.
36
	 * The rest is handled via JS.
37
	 */
38
	public function field_dependencies() {
39
40
		wp_enqueue_script( 'kirki_field_dependencies', trailingslashit( Kirki::$url ) . 'modules/field-dependencies/field-dependencies.js', array( 'jquery', 'customize-base', 'customize-controls' ), false, true );
41
		$field_dependencies = array();
42
		$fields = Kirki::$fields;
43
		foreach ( $fields as $field ) {
44
			$process_field = false;
45
			if ( isset( $field['active_callback'] ) && is_array( $field['active_callback'] ) ) {
46
				if ( array( 'Kirki_Active_Callback', 'evaluate' ) === $field['active_callback'] ) {
47
					$process_field = true;
48
				}
49
			}
50
			if ( $process_field && isset( $field['required'] ) && ! empty( $field['required'] ) ) {
51
				$field_dependencies[ $field['id'] ] = $field['required'];
52
			}
53
		}
54
		wp_localize_script( 'kirki_field_dependencies', 'fieldDependencies', $field_dependencies );
55
56
	}
57
}
58