Completed
Pull Request — develop (#1311)
by Aristeides
03:09
created

customize_controls_print_footer_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Adds search functionality to easier locate fields.
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       3.0.0
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
/**
19
 * Adds script for Search.
20
 */
21
class Kirki_Modules_Search {
22
23
	/**
24
	 * An array containing field identifieds and their labels/descriptions.
25
	 *
26
	 * @access private
27
	 * @since 3.0.0
28
	 * @var array
29
	 */
30
	private $search_content = array();
31
32
	/**
33
	 * The class constructor
34
	 *
35
	 * @access public
36
	 * @since 3.0.0
37
	 */
38
	public function __construct() {
39
40
		add_action( 'customize_controls_print_footer_scripts', array( $this, 'customize_controls_print_footer_scripts' ) );
41
42
	}
43
44
	/**
45
	 * Parses fields and adds their labels and descriptions to the
46
	 * object's $search_content property.
47
	 *
48
	 * @access private
49
	 * @since 3.0.0
50
	 */
51
	private function parse_fields() {
52
53
		$fields = Kirki::$fields;
54
		foreach ( $fields as $field ) {
55
			$id = str_replace( '[', '-', str_replace( ']', '', $field['settings'] ) );
56
			$this->search_content[] = array(
57
				'id'          => $id,
58
				'label'       => ( isset( $field['label'] ) && ! empty( $field['label'] ) ) ? esc_html( $field['label'] ) : '',
59
				'description' => ( isset( $field['description'] ) && ! empty( $field['description'] ) ) ? esc_html( $field['description'] ) : '',
60
			);
61
		}
62
	}
63
64
	/**
65
	 * Enqueue scripts.
66
	 *
67
	 * @access public
68
	 * @since 3.0.0
69
	 */
70
	public function customize_controls_print_footer_scripts() {
71
72
		$this->parse_fields();
73
74
		$vars = array(
75
			'fields' => $this->search_content,
76
			'button' => '<a class="kirki-customize-controls-search" href="#"><span class="screen-reader-text">' . esc_attr__( 'Search', 'kirki' ) . '</span></a>',
77
			'form'   => '<div class="kirki-search-form-wrapper hidden"><input type="text" id="kirki-search"></div><div class="kirki-search-results"></div>',
78
		);
79
80
		wp_enqueue_script( 'fuse', trailingslashit( Kirki::$url ) . 'modules/search/fuse.min.js', array( 'jquery' ) );
81
		wp_enqueue_script( 'kirki-search', trailingslashit( Kirki::$url ) . 'modules/search/search.js', array( 'jquery', 'fuse' ) );
82
		wp_localize_script( 'kirki-search', 'kirkiFieldsSearch', $vars );
83
		wp_enqueue_style( 'kirki-search', trailingslashit( Kirki::$url ) . 'modules/search/search.css', null );
84
85
	}
86
}
87