Completed
Push — develop ( 2d809e...4b580b )
by Aristeides
03:12
created

Kirki_Modules_Search::get_instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
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) 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 script for Search.
20
 */
21
class Kirki_Modules_Search {
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
	 * An array containing field identifieds and their labels/descriptions.
35
	 *
36
	 * @access private
37
	 * @since 3.0.0
38
	 * @var array
39
	 */
40
	private $search_content = array();
41
42
	/**
43
	 * The class constructor
44
	 *
45
	 * @access protected
46
	 * @since 3.0.0
47
	 */
48
	protected function __construct() {
49
		// Add the custom section.
50
		add_action( 'customize_register', array( $this, 'customize_register' ) );
51
		// Enqueue styles and scripts.
52
		add_action( 'customize_controls_print_footer_scripts', array( $this, 'customize_controls_print_footer_scripts' ) );
53
	}
54
55
	/**
56
	 * Gets an instance of this object.
57
	 * Prevents duplicate instances which avoid artefacts and improves performance.
58
	 *
59
	 * @static
60
	 * @access public
61
	 * @since 3.0.0
62
	 * @return object
63
	 */
64
	public static function get_instance() {
65
		if ( ! self::$instance ) {
66
			self::$instance = new self();
67
		}
68
		return self::$instance;
69
	}
70
71
	/**
72
	 * Parses fields and adds their labels and descriptions to the
73
	 * object's $search_content property.
74
	 *
75
	 * @access private
76
	 * @since 3.0.0
77
	 */
78
	private function parse_fields() {
79
80
		$fields = Kirki::$fields;
81
		foreach ( $fields as $field ) {
82
			$id = str_replace( '[', '-', str_replace( ']', '', $field['settings'] ) );
83
			$this->search_content[] = array(
84
				'id'          => $id,
85
				'label'       => ( isset( $field['label'] ) && ! empty( $field['label'] ) ) ? esc_html( $field['label'] ) : '',
86
				'description' => ( isset( $field['description'] ) && ! empty( $field['description'] ) ) ? esc_html( $field['description'] ) : '',
87
			);
88
		}
89
	}
90
91
	/**
92
	 * Enqueue scripts.
93
	 *
94
	 * @access public
95
	 * @since 3.0.0
96
	 */
97
	public function customize_controls_print_footer_scripts() {
98
99
		$this->parse_fields();
100
101
		$vars = array(
102
			'fields' => $this->search_content,
103
			'button' => '<a class="kirki-customize-controls-search" href="#"><span class="screen-reader-text">' . esc_attr__( 'Search', 'kirki' ) . '</span></a>',
104
			'form'   => '<div class="kirki-search-form-wrapper hidden"><input type="text" id="kirki-search"></div><div class="kirki-search-results"></div>',
105
		);
106
107
		wp_enqueue_script( 'fuse', trailingslashit( Kirki::$url ) . 'modules/search/fuse.min.js', array( 'jquery' ) );
108
		wp_enqueue_script( 'kirki-search', trailingslashit( Kirki::$url ) . 'modules/search/search.js', array( 'jquery', 'fuse' ) );
109
		wp_localize_script( 'kirki-search', 'kirkiFieldsSearch', $vars );
110
		wp_enqueue_style( 'kirki-search', trailingslashit( Kirki::$url ) . 'modules/search/search.css', null );
111
112
	}
113
114
	/**
115
	 * Adds the section to the customizer.
116
	 *
117
	 * @access public
118
	 * @since 3.0.0
119
	 * @param object $wp_customize The customizer object.
120
	 */
121
	public function customize_register( $wp_customize ) {
122
123
		// Include the custom search section.
124
		if ( ! class_exists( 'Kirki_Modules_Search_Section' ) ) {
125
			include_once 'class-kirki-modules-search-section.php';
126
		}
127
128
		// Add section.
129
		$wp_customize->add_section( new Kirki_Modules_Search_Section( $wp_customize, 'kirki_search_module', array(
130
			'title'       => esc_attr__( 'Search', 'kirki' ),
131
			'priority'    => 999,
132
		) ) );
133
134
		// Add setting & control.
135
		// THose these are not actually necessary,
136
		// the section is not displayed without them.
137
		$wp_customize->add_setting( 'kirki_search', array(
138
			'type'              => 'theme_mod',
139
			'capability'        => 'edit_theme_options',
140
			'default'           => '',
141
			'transport'         => 'postMessage',
142
			'sanitize_callback' => '__return_empty_string',
143
		) );
144
		$wp_customize->add_control( 'kirki_search', array(
145
			'label'   => esc_attr__( 'Search Controls', 'kirki' ),
146
			'type'    => 'text',
147
			'section' => 'kirki_search_module',
148
		) );
149
	}
150
}
151