| 1 |
<?php |
| 2 |
/** |
| 3 |
* This is the class that handles the logic for Customizer controls search. |
| 4 |
* |
| 5 |
* @see https://pixelgrade.com |
| 6 |
* @author Pixelgrade |
| 7 |
* @since 2.9.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly |
| 12 |
} |
| 13 |
|
| 14 |
if ( ! class_exists( 'Customify_Customizer_Search' ) ) : |
| 15 |
|
| 16 |
class Customify_Customizer_Search { |
| 17 |
|
| 18 |
/** |
| 19 |
* Holds the only instance of this class. |
| 20 |
* @var null|Customify_Customizer_Search |
| 21 |
* @access protected |
| 22 |
* @since 2.9.0 |
| 23 |
*/ |
| 24 |
protected static $_instance = null; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @since 2.9.0 |
| 30 |
*/ |
| 31 |
protected function __construct() { |
| 32 |
$this->init(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Initialize this module. |
| 37 |
* |
| 38 |
* @since 2.9.0 |
| 39 |
*/ |
| 40 |
public function init() { |
| 41 |
// Hook up. |
| 42 |
$this->add_hooks(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Initiate our hooks |
| 47 |
* |
| 48 |
* @since 2.9.0 |
| 49 |
*/ |
| 50 |
public function add_hooks() { |
| 51 |
|
| 52 |
/* |
| 53 |
* Enqueue the needed scripts and styles. |
| 54 |
*/ |
| 55 |
add_action( 'customize_controls_init', array( $this, 'register_admin_customizer_scripts' ), 10 ); |
| 56 |
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_admin_customizer_scripts' ), 10 ); |
| 57 |
|
| 58 |
/* |
| 59 |
* Print the needed JavaScript templates. |
| 60 |
*/ |
| 61 |
add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_js_template' ) ); |
| 62 |
|
| 63 |
// Add configuration data to be passed to JS. |
| 64 |
add_filter( 'customify_localized_js_settings', array( $this, 'add_to_localized_data' ), 10, 1 ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Register Customizer admin scripts. |
| 69 |
* |
| 70 |
* @since 2.9.0 |
| 71 |
*/ |
| 72 |
public function register_admin_customizer_scripts() { |
| 73 |
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
| 74 |
|
| 75 |
wp_register_script( PixCustomifyPlugin()->get_slug() . '-fuse', |
| 76 |
plugins_url( 'js/vendor/fuse-6.0.0/fuse.basic' . $suffix . '.js', PixCustomifyPlugin()->get_file() ), |
| 77 |
[], null ); |
| 78 |
|
| 79 |
wp_register_script( PixCustomifyPlugin()->get_slug() . '-customizer-search', |
| 80 |
plugins_url( 'js/customizer/search' . $suffix . '.js', PixCustomifyPlugin()->get_file() ), |
| 81 |
[ 'jquery', PixCustomifyPlugin()->get_slug() . '-fuse', ], PixCustomifyPlugin()->get_version() ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Enqueue Customizer admin scripts. |
| 86 |
* |
| 87 |
* @since 2.9.0 |
| 88 |
*/ |
| 89 |
public function enqueue_admin_customizer_scripts() { |
| 90 |
// If there is no customizer search support, bail early. |
| 91 |
if ( ! $this->is_supported() ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
wp_enqueue_script( PixCustomifyPlugin()->get_slug() . '-customizer-search' ); |
| 96 |
} |
| 97 |
|
| 98 |
public function print_js_template() { |
| 99 |
// If there is no customizer search support, bail early. |
| 100 |
if ( ! $this->is_supported() ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
?> |
| 104 |
<script type="text/html" id="tmpl-customify-search-button"> |
| 105 |
<button type="button" class="customize-search-toggle dashicons dashicons-search" aria-expanded="false"><span class="screen-reader-text"><?php esc_html_e( 'Search', 'customify' ); ?></span></button> |
| 106 |
</script> |
| 107 |
|
| 108 |
<script type="text/html" id="tmpl-customify-search-form"> |
| 109 |
<div id="accordion-section-customify-customizer-search" style="display: none;"> |
| 110 |
<h4 class="customify-customizer-search-section accordion-section-title"> |
| 111 |
<span class="search-input-label"><?php esc_html_e( 'Search through all controls, menus, and widgets.', 'customify' ); ?></span> |
| 112 |
<span class="search-field-wrapper"> |
| 113 |
<input type="text" placeholder="<?php esc_html_e( 'Start typing...', 'customify' ); ?>" name="customify-customizer-search-input" autofocus="autofocus" id="customify-customizer-search-input" class="customizer-search-input" /> |
| 114 |
<span class="search-field-button-wrapper"> |
| 115 |
<button type="button" class="clear-search button button-primary has-next-sibling" tabindex="0" aria-label="<?php esc_html_e( 'Clear current search', 'customify' ); ?>"><?php esc_html_e( 'Clear', 'customify' ); ?></button> |
| 116 |
<button type="button" class="close-search button-primary button dashicons dashicons-no" aria-label="<?php esc_html_e( 'Close search', 'customify' ); ?>"></button> |
| 117 |
</span> |
| 118 |
</span> |
| 119 |
</h4> |
| 120 |
</div> |
| 121 |
</script> |
| 122 |
<?php } |
| 123 |
|
| 124 |
/** |
| 125 |
* Add data to be available in JS. |
| 126 |
* |
| 127 |
* @since 2.9.0 |
| 128 |
* |
| 129 |
* @param $localized |
| 130 |
* |
| 131 |
* @return mixed |
| 132 |
*/ |
| 133 |
public function add_to_localized_data( $localized ) { |
| 134 |
if ( empty( $localized['search'] ) ) { |
| 135 |
$localized['search'] = []; |
| 136 |
} |
| 137 |
|
| 138 |
$localized['search']['excludedControls'] = [ |
| 139 |
// Color Palettes Controls |
| 140 |
'sm_dark_color_master_slider', |
| 141 |
'sm_dark_color_primary_slider', |
| 142 |
'sm_dark_color_secondary_slider', |
| 143 |
'sm_dark_color_tertiary_slider', |
| 144 |
'sm_colors_dispersion', |
| 145 |
'sm_colors_focus_point', |
| 146 |
'sm_color_palette', |
| 147 |
'sm_color_palette_variation', |
| 148 |
'sm_color_primary', |
| 149 |
'sm_color_primary_final', |
| 150 |
'sm_color_secondary', |
| 151 |
'sm_color_secondary_final', |
| 152 |
'sm_color_tertiary', |
| 153 |
'sm_color_tertiary_final', |
| 154 |
'sm_dark_primary', |
| 155 |
'sm_dark_primary_final', |
| 156 |
'sm_dark_secondary', |
| 157 |
'sm_dark_secondary_final', |
| 158 |
'sm_dark_tertiary', |
| 159 |
'sm_dark_tertiary_final', |
| 160 |
'sm_light_primary', |
| 161 |
'sm_light_primary_final', |
| 162 |
'sm_light_secondary', |
| 163 |
'sm_light_secondary_final', |
| 164 |
'sm_light_tertiary', |
| 165 |
'sm_light_tertiary_final', |
| 166 |
'sm_swap_colors', |
| 167 |
'sm_swap_dark_light', |
| 168 |
'sm_swap_colors_dark', |
| 169 |
'sm_swap_secondary_colors_dark', |
| 170 |
'sm_advanced_toggle', |
| 171 |
'sm_spacing_bottom', |
| 172 |
// Font Palettes Controls |
| 173 |
'sm_font_palette', |
| 174 |
'sm_font_palette_variation', |
| 175 |
'sm_font_primary', |
| 176 |
'sm_font_secondary', |
| 177 |
'sm_font_body', |
| 178 |
'sm_font_accent', |
| 179 |
'sm_swap_fonts', |
| 180 |
'sm_swap_primary_secondary_fonts', |
| 181 |
]; |
| 182 |
|
| 183 |
if ( empty( $localized['l10n'] ) ) { |
| 184 |
$localized['l10n'] = []; |
| 185 |
} |
| 186 |
$localized['l10n']['search'] = [ |
| 187 |
'resultsSectionScreenReaderText' => esc_html__( 'Press return or enter to open this section', 'customify' ), |
| 188 |
]; |
| 189 |
|
| 190 |
return $localized; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Determine if the Customizer search is supported. |
| 195 |
* |
| 196 |
* @since 2.9.0 |
| 197 |
* |
| 198 |
* @return bool |
| 199 |
*/ |
| 200 |
public function is_supported() { |
| 201 |
// Determine if the controls search functionality is supported. |
| 202 |
return apply_filters( 'customify_customizer_search_is_supported', true ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Main Customify_Customizer_Search Instance |
| 207 |
* |
| 208 |
* Ensures only one instance of Customify_Customizer_Search is loaded or can be loaded. |
| 209 |
* |
| 210 |
* @since 2.9.0 |
| 211 |
* @static |
| 212 |
* |
| 213 |
* @return Customify_Customizer_Search Main Customify_Customizer_Search instance |
| 214 |
*/ |
| 215 |
public static function instance() { |
| 216 |
|
| 217 |
if ( is_null( self::$_instance ) ) { |
| 218 |
self::$_instance = new self(); |
| 219 |
} |
| 220 |
return self::$_instance; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Cloning is forbidden. |
| 225 |
* |
| 226 |
* @since 2.9.0 |
| 227 |
*/ |
| 228 |
public function __clone() { |
| 229 |
|
| 230 |
_doing_it_wrong( __FUNCTION__,esc_html__( 'You should not do that!', 'customify' ), null ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Unserializing instances of this class is forbidden. |
| 235 |
* |
| 236 |
* @since 2.9.0 |
| 237 |
*/ |
| 238 |
public function __wakeup() { |
| 239 |
|
| 240 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), null ); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
endif; |
| 245 |
|