PluginProbe
Customify / 1.4.1
Customify v1.4.1
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / class-pixcustomify.php

class-pixcustomify.php in Customify 1.4.1, at class-pixcustomify.php

1,865 lines 54.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PixCustomify.
4 * @package PixCustomify
5 * @author Pixelgrade <contact@pixelgrade.com>
6 * @license GPL-2.0+
7 * @link http://pixelgrade.com
8 * @copyright 2014 Pixelgrade
9 */
10
11 /**
12 * Plugin class.
13 * @package PixCustomify
14 * @author Pixelgrade <contact@pixelgrade.com>
15 */
16 class PixCustomifyPlugin {
17
18 /**
19 * Plugin version, used for cache-busting of style and script file references.
20 * @since 1.0.0
21 * @const string
22 */
23 protected $version = '1.4.1';
24 /**
25 * Unique identifier for your plugin.
26 * Use this value (not the variable name) as the text domain when internationalizing strings of text. It should
27 * match the Text Domain file header in the main plugin file.
28 * @since 1.0.0
29 * @var string
30 */
31 protected $plugin_slug = 'customify';
32
33 /**
34 * Instance of this class.
35 * @since 1.0.0
36 * @var object
37 */
38 protected static $instance = null;
39
40 /**
41 * Slug of the plugin screen.
42 * @since 1.0.0
43 * @var string
44 */
45 protected $plugin_screen_hook_suffix = null;
46
47 /**
48 * Path to the plugin.
49 * @since 1.0.0
50 * @var string
51 */
52 protected $plugin_basepath = null;
53
54 public $display_admin_menu = false;
55
56 private static $config;
57
58 private static $customizer_config;
59
60 public static $plugin_settings;
61
62 protected static $localized = array();
63
64 protected static $current_values = array();
65
66 protected static $options_list = array();
67
68 protected static $media_queries = array();
69
70 protected static $opt_name;
71
72 protected static $typo_settings;
73
74 protected static $google_fonts = null;
75
76 protected static $theme_fonts = null;
77
78 // these properties will get 'px' as a default unit
79 protected static $pixel_dependent_css_properties = array(
80 'width',
81 'max-width',
82 'min-width',
83
84 'height',
85 'max-height',
86 'min-height',
87
88 'padding',
89 'padding-left',
90 'padding-right',
91 'padding-top',
92 'padding-bottom',
93
94 'margin',
95 'margin-right',
96 'margin-left',
97 'margin-top',
98 'margin-bottom',
99
100 'right',
101 'left',
102 'top',
103 'bottom',
104
105 'font-size',
106 'letter-spacing',
107
108 'border-size',
109 'border-width',
110 'border-bottom-width',
111 'border-left-width',
112 'border-right-width',
113 'border-top-width'
114 );
115
116 protected static $jetpack_default_modules = array();
117 protected static $jetpack_blocked_modules = array();
118 protected static $jetpack_sharing_default_options = array();
119
120 /**
121 * Initialize the plugin by setting localization, filters, and administration functions.
122 * @since 1.0.0
123 */
124 protected function __construct() {
125
126 $this->plugin_basepath = plugin_dir_path( __FILE__ );
127 self::$config = self::get_config();
128 self::$plugin_settings = get_option( 'pixcustomify_settings' );
129
130 // load custom modules
131 include_once( self::get_base_path() . '/features/class-CSS_Editor.php' );
132 include_once( self::get_base_path() . 'features/class-Font_Selector.php' );
133
134 // Load plugin text domain
135 add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
136
137 add_action( 'wp_loaded', array( $this, 'init_plugin_configs' ), 5 );
138
139 add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) );
140
141 // Add an action link pointing to the options page.
142 $plugin_basename = plugin_basename( plugin_dir_path( __FILE__ ) . 'pixcustomify.php' );
143 add_filter( 'plugin_action_links_' . $plugin_basename, array( $this, 'add_action_links' ) );
144
145 // Load admin style sheet and JavaScript.
146 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
147 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
148
149 // add_action( 'plugins_loaded', array( $this, 'register_metaboxes' ), 14 );
150 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_admin_customizer_styles' ), 10 );
151 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_admin_customizer_scripts' ), 10 );
152 add_action( 'customize_preview_init', array( $this, 'customizer_live_preview_enqueue_scripts' ), 99999 );
153
154 $load_location = self::get_plugin_option( 'style_resources_location', 'wp_head' );
155
156 add_action( $load_location, array( $this, 'output_dynamic_style' ), 99999 );
157 add_action( 'wp_head', array( $this, 'output_typography_dynamic_style' ), 10 );
158
159 add_action( 'customize_register', array( $this, 'remove_default_sections' ), 11 );
160 add_action( 'customize_register', array( $this, 'register_customizer' ), 12 );
161
162 if ( self::get_plugin_option( 'enable_editor_style', true ) ) {
163 add_action( 'admin_head', array( $this, 'add_customizer_settings_into_wp_editor' ) );
164 }
165
166 // Jetpack Related
167 add_action( 'init', array( $this, 'set_jetpack_modules_config') );
168 add_filter( 'default_option_jetpack_active_modules', array( $this, 'default_jetpack_active_modules' ), 10, 1 );
169 add_filter( 'jetpack_get_available_modules', array( $this, 'jetpack_hide_blocked_modules'), 10, 1 );
170 add_filter( 'default_option_sharing-options', array( $this, 'default_jetpack_sharing_options' ), 10, 1 );
171 }
172
173 /**
174 * Return an instance of this class.
175 * @since 1.0.0
176 * @return object A single instance of this class.
177 */
178 public static function get_instance() {
179
180 // If the single instance hasn't been set, set it now.
181 if ( null == self::$instance ) {
182 self::$instance = new self;
183 }
184
185 return self::$instance;
186 }
187
188 /**
189 * Configs, Options and Values methods
190 */
191
192 function init_plugin_configs() {
193 self::$customizer_config = get_option( 'pixcustomify_config' );
194
195 // no option so go for default
196 if ( empty( self::$customizer_config ) ) {
197 self::$customizer_config = self::get_config_option( 'default_options' );
198 }
199
200 if ( empty( self::$customizer_config ) ) {
201 self::$customizer_config = array();
202 }
203
204 // allow themes or other plugins to filter the config
205 self::$customizer_config = apply_filters( 'customify_filter_fields', self::$customizer_config );
206
207 self::$opt_name = self::$localized['options_name'] = self::$customizer_config['opt-name'];
208
209 self::get_current_values();
210 self::$options_list = $this->get_options();
211
212 if ( $this->import_button_exists() ) {
213 self::$opt_name = self::$localized['import_rest_url'] = get_rest_url( '/customify/1.0/' );
214 self::$opt_name = self::$localized['import_rest_nonce'] = wp_create_nonce( 'wp_rest' );
215
216 $this->register_import_api();
217 }
218
219 $font_selector = new Customify_Font_Selector( $this );
220 self::$localized['theme_fonts'] = self::$theme_fonts = Customify_Font_Selector::$theme_fonts;
221 }
222
223 function set_jetpack_modules_config() {
224 // We expect an array of string module names like array( 'infinite-scroll', 'widgets' )
225 // See jetpack/modules/modules-heading.php for module names
226 self::$jetpack_default_modules = apply_filters ( 'customify_filter_jetpack_default_modules', array(
227 'shortcodes',
228 'widget-visibility',
229 'widgets',
230 ) );
231
232 // We expect an array of string module names like array( 'infinite-scroll', 'widgets' )
233 // See jetpack/modules/modules-heading.php for module names
234 self::$jetpack_blocked_modules = apply_filters ( 'customify_filter_jetpack_blocked_modules', array() );
235
236 self::$jetpack_sharing_default_options = apply_filters ( 'customify_filter_jetpack_sharing_default_options', array() );
237 }
238
239 /**
240 * Control the default modules that are activated in Jetpack.
241 * Use the `customify_filter_jetpack_default_modules` to set your's.
242 *
243 * @param array $default The default value to return if the option does not exist
244 * in the database.
245 * @return array
246 */
247 function default_jetpack_active_modules( $default ) {
248 if ( ! is_array( $default ) ) {
249 $default = array();
250 }
251
252 return array_merge( $default, self::$jetpack_default_modules );
253 }
254
255 /**
256 * Control the default Jetpack Sharing options.
257 * Use the `customify_filter_jetpack_sharing_default_options` to set your's.
258 *
259 * @param array $default The default value to return if the option does not exist
260 * in the database.
261 *
262 * @return array
263 */
264 function default_jetpack_sharing_options( $default ) {
265 if ( ! is_array( $default ) ) {
266 $default = array();
267 }
268
269 return array_merge( $default, self::$jetpack_sharing_default_options );
270 }
271
272 /**
273 * Control the modules that are available in Jetpack (hide some of them).
274 * Use the `customify_filter_jetpack_blocked_modules` filter to set your's.
275 *
276 * @param array $modules
277 *
278 * @return array
279 */
280 function jetpack_hide_blocked_modules( $modules ) {
281 return array_diff_key( $modules, array_flip( self::$jetpack_blocked_modules ) );
282 }
283
284 /**
285 * Fired when the plugin is activated.
286 * @since 1.0.0
287 *
288 * @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
289 */
290 public static function activate( $network_wide ) {
291
292 }
293
294 /**
295 * Fired when the plugin is deactivated.
296 * @since 1.0.0
297 *
298 * @param boolean $network_wide True if WPMU superadmin uses "Network Deactivate" action, false if WPMU is disabled or plugin is deactivated on an individual blog.
299 */
300 static function deactivate( $network_wide ) {
301 // TODO: Define deactivation functionality here
302 }
303
304 /**
305 * Load the plugin text domain for translation.
306 * @since 1.0.0
307 */
308 function load_plugin_textdomain() {
309 $domain = $this->plugin_slug;
310 load_plugin_textdomain( $domain, false, basename( dirname( __FILE__ ) ) . '/languages/' );
311 }
312
313 /** === RESOURCES === **/
314
315 /**
316 * Customizer admin styles
317 */
318 function enqueue_admin_customizer_styles() {
319 wp_enqueue_style( 'select2', plugins_url( 'js/select2/css/select2.css', __FILE__ ), array(), $this->version );
320 wp_enqueue_style( 'customify_style', plugins_url( 'css/customizer.css', __FILE__ ), array(), $this->version );
321 }
322
323 /**
324 * Customizer admin scripts
325 */
326 function enqueue_admin_customizer_scripts() {
327
328 wp_enqueue_script( 'select2', plugins_url( 'js/select2/js/select2.js', __FILE__ ), array( 'jquery' ), $this->version );
329 wp_enqueue_script( 'jquery-react', plugins_url( 'js/jquery-react.js', __FILE__ ), array( 'jquery' ), $this->version );
330 wp_enqueue_script( $this->plugin_slug . '-customizer-scripts', plugins_url( 'js/customizer.js', __FILE__ ), array(
331 'jquery',
332 'select2',
333 'underscore',
334 'customize-controls'
335 ), $this->version );
336
337 wp_localize_script( $this->plugin_slug . '-customizer-scripts', 'customify_settings', self::$localized );
338 }
339
340 /** Customizer scripts loaded only on previewer page */
341 function customizer_live_preview_enqueue_scripts() {
342
343 wp_register_script( $this->plugin_slug . 'CSSOM', plugins_url( 'js/CSSOM.js', __FILE__ ), array( 'jquery' ), $this->version, true );
344 wp_register_script( $this->plugin_slug . 'cssUpdate', plugins_url( 'js/jquery.cssUpdate.js', __FILE__ ), array(), $this->version, true );
345 wp_enqueue_script( $this->plugin_slug . '-previewer-scripts', plugins_url( 'js/customizer_preview.js', __FILE__ ), array(
346 'jquery',
347 $this->plugin_slug . 'CSSOM',
348 $this->plugin_slug . 'cssUpdate'
349 ), $this->version, true );
350
351 // when a live preview field is in action we need to know which props need 'px' as defaults
352 self::$localized['px_dependent_css_props'] = self::$pixel_dependent_css_properties;
353
354 wp_localize_script( $this->plugin_slug . '-previewer-scripts', 'customify_settings', self::$localized );
355 }
356
357 /**
358 * Add dynamic style only on the previewer page
359 */
360
361 /**
362 * Settings page styles
363 */
364 function enqueue_admin_styles() {
365
366 if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
367 return;
368 }
369
370 $screen = get_current_screen();
371 if ( $screen->id == $this->plugin_screen_hook_suffix ) {
372 wp_enqueue_style( $this->plugin_slug . '-admin-styles', plugins_url( 'css/admin.css', __FILE__ ), array(), $this->version );
373 }
374 }
375
376 /**
377 * Settings page scripts
378 */
379 function enqueue_admin_scripts() {
380
381 if ( ! isset( $this->plugin_screen_hook_suffix ) ) {
382 return;
383 }
384
385 $screen = get_current_screen();
386 if ( $screen->id == $this->plugin_screen_hook_suffix ) {
387 wp_enqueue_script( $this->plugin_slug . '-admin-script', plugins_url( 'js/admin.js', __FILE__ ), array( 'jquery' ), $this->version );
388 wp_localize_script( $this->plugin_slug . '-admin-script', 'locals', array(
389 'ajax_url' => admin_url( 'admin-ajax.php' )
390 ) );
391 }
392
393
394 wp_localize_script( $this->plugin_slug . '-customizer-scripts', 'WP_API_Settings', array( 'root' => esc_url_raw( rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ) ) );
395 }
396
397 /**
398 * Public style generated by customizer
399 */
400 function output_dynamic_style() {
401 $custom_css = "\n";
402
403 foreach ( self::$options_list as $option_id => $option ) {
404
405 if ( isset( $option['css'] ) && ! empty( $option['css'] ) ) {
406 // now process each
407 $custom_css .= $this->convert_setting_to_css( $option_id, $option['css'] );
408 }
409
410 if ( $option['type'] === 'custom_background' ) {
411 $custom_css .= $this->process_custom_background_field_output( $option_id, $option );
412 }
413 }
414
415 if ( ! empty( self::$media_queries ) ) {
416
417 foreach ( self::$media_queries as $media_query => $properties ) {
418
419 if ( empty( $properties ) ) {
420 continue;
421 }
422
423 $custom_css .= '@media ' . $media_query . " {\n";
424
425 foreach ( $properties as $key => $property ) {
426 $property_settings = $property['property'];
427 $property_value = $property['value'];
428 $custom_css .= "\t" . self::proccess_css_property( $property_settings, $property_value );
429 }
430
431 $custom_css .= "\n}\n";
432
433 }
434 }
435
436 $custom_css .= "\n";
437
438 //@todo maybe add a filter to this output ?>
439 <style id="customify_output_style">
440 <?php echo( $custom_css ); ?>
441 </style>
442 <?php
443
444
445 /**
446 * from now on we output only style tags only for the preview purpose
447 * so don't cry if you see 30+ style tags for each section
448 */
449 if ( ! isset( $GLOBALS['wp_customize'] ) ) {
450 return;
451 }
452
453 foreach ( self::$options_list as $option_id => $options ) {
454
455 if ( $options['type'] === 'custom_background' ) {
456 $options['value'] = self::get_option( $option_id );
457 $custom_background_output = $this->process_custom_background_field_output( $option_id, $options ); ?>
458
459 <style id="custom_backgorund_output_for_<?php echo $option_id; ?>">
460 <?php
461 if ( isset( $custom_background_output ) && ! empty( $custom_background_output )) {
462 echo $custom_background_output;
463 } ?>
464 </style>
465 <?php }
466
467 if ( ! isset( $options['live'] ) || $options['live'] !== true ) {
468 continue;
469 }
470
471 $this_value = self::get_option( $option_id );
472 foreach ( $options['css'] as $key => $properties_set ) { ?>
473 <style id="dynamic_setting_<?php echo $option_id . '_property_' . str_replace( '-', '_', $properties_set['property'] ); ?>" type="text/css"><?php
474
475 if ( isset( $properties_set['media'] ) && ! empty( $properties_set['media'] ) ) {
476 echo '@media '. $properties_set['media'] . " {\n";
477 }
478
479 if ( isset( $properties_set['selector'] ) && isset( $properties_set['property'] ) ) {
480 echo self::proccess_css_property($properties_set, $this_value);
481 }
482
483 if ( isset( $properties_set['media'] ) && ! empty( $properties_set['media'] ) ) {
484 echo "}\n";
485 } ?>
486 </style>
487 <?php }
488 }
489
490 if ( ! empty( self::$media_queries ) ) {
491
492 foreach ( self::$media_queries as $media_query => $properties ) {
493
494 if ( empty( $properties ) ) {
495 continue;
496 }
497
498 $display = false;
499 $media_q = '@media ' . $media_query . " {\n";
500
501 foreach ( $properties as $key => $property ) {
502
503 if ( ! isset( $options['live'] ) || $options['live'] !== true ) {
504 continue;
505 }
506
507 $display = true; ?>
508 <style id="dynamic_setting_<?php echo $key; ?>" type="text/css"><?php
509 $property_settings = $property['property'];
510 $property_value = $property['value'];
511 $media_q .= "\t" . self::proccess_css_property( $property_settings, $property_value );?>
512 </style>
513 <?php }
514
515 $media_q .= "\n}\n";
516
517 if ( $display ) {
518 $custom_css .= $media_q;
519 }
520 }
521 }
522 }
523
524 protected function load_google_fonts() {
525
526 $fonts_path = plugin_dir_path( __FILE__ ) . 'features/customizer/controls/resources/google.fonts.php';
527
528 if ( file_exists( $fonts_path ) ) {
529 self::$google_fonts = require( $fonts_path );
530 }
531
532 if ( ! empty( self::$google_fonts ) ) {
533 return self::$google_fonts;
534 }
535
536 return false;
537 }
538
539 function output_typography_dynamic_style() {
540
541 self::get_typography_fields( self::$options_list, 'type', 'typography', self::$typo_settings );
542
543 if ( empty( self::$typo_settings ) ) {
544 return;
545 }
546
547 $families = '';
548
549 foreach ( self::$typo_settings as $id => $font ) {
550 if ( isset ( $font['value'] ) ) {
551
552 $load_all_weights = false;
553 if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) {
554 $load_all_weights = true;
555 }
556
557 // shim the time when this was an array
558 if ( is_array( $font['value'] ) ) {
559 $font['value'] = stripslashes_deep( $font['value'] );
560 $font['value'] = json_encode( $font['value'] );
561 }
562
563 $value = json_decode( wp_unslash( PixCustomifyPlugin::decodeURIComponent( $font['value'] ) ), true );
564
565 // in case the value is still null, try default value(mostly for google fonts)
566 if ( ! is_array( $value ) || $value === null ) {
567 $value = $this->get_font_defaults_value( str_replace( '"', '', $font['value'] ) );
568 }
569
570 //bail if by this time we don't have a value of some sort
571 if ( empty( $value ) ) {
572 continue;
573 }
574
575 //Handle special logic for when the $value array is not an associative array
576 if ( ! self::is_assoc( $value ) ) {
577 $value = $this->process_a_not_associative_font_default( $value );
578 }
579
580 if ( isset( $value['font_family'] ) && isset( $value['type'] ) && $value['type'] == 'google' ) {
581 $families .= "'" . $value['font_family'];
582
583 if ( $load_all_weights && is_array( $value['variants'] ) ) {
584 $families .= ":" . implode( ',', $value['variants'] );
585 } elseif ( isset( $value['selected_variants'] ) && ! empty( $value['selected_variants'] ) ) {
586 if ( is_array( $value['selected_variants'] ) ) {
587 $families .= ":" . implode( ',', $value['selected_variants'] );
588 } elseif ( is_string( $value['selected_variants'] ) || is_numeric( $value['selected_variants'] ) ) {
589 $families .= ":" . $value['selected_variants'];
590 }
591 } elseif ( isset( $value['variants'] ) && ! empty( $value['variants'] ) ) {
592 if ( is_array( $value['variants'] ) ) {
593 $families .= ":" . implode( ',', $value['variants'] );
594 } else {
595 $families .= ":" . $value['variants'];
596 }
597 }
598
599 if ( isset( $value['selected_subsets'] ) && ! empty( $value['selected_subsets'] ) ) {
600 if ( is_array( $value['selected_subsets'] ) ) {
601 $families .= ":" . implode( ',', $value['selected_subsets'] );
602 } else {
603 $families .= ":" . $value['selected_subsets'];
604 }
605 } elseif ( isset( $value['subsets'] ) && ! empty( $value['subsets'] ) ) {
606 if ( is_array( $value['subsets'] ) ) {
607 $families .= ":" . implode( ',', $value['subsets'] );
608 } else {
609 $families .= ":" . $value['subsets'];
610 }
611 }
612
613 $families .= '\',';
614 }
615 }
616 }
617
618 if ( ! empty ( $families ) && self::get_plugin_option( 'typography', '1' ) && self::get_plugin_option( 'typography_google_fonts', 1 ) ) { ?>
619 <script type="text/javascript">
620 if ( typeof WebFont !== 'undefined' ) {<?php // if there is a WebFont object, use it ?>
621 WebFont.load( {
622 google: {families: [<?php echo( rtrim( $families, ',' ) ); ?>]},
623 classes: false,
624 events: false
625 } );
626 } else {<?php // basically when we don't have the WebFont object we create the google script dynamically ?>
627
628 var tk = document.createElement( 'script' );
629 tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
630 tk.type = 'text/javascript';
631
632 tk.onload = tk.onreadystatechange = function() {
633 WebFont.load( {
634 google: {families: [<?php echo( rtrim( $families, ',' ) ); ?>]},
635 classes: false,
636 events: false
637 } );
638 };
639
640 var s = document.getElementsByTagName( 'script' )[0];
641 s.parentNode.insertBefore( tk, s );
642 }
643 </script>
644 <?php } ?>
645 <style id="customify_typography_output_style">
646 <?php
647 foreach ( self::$typo_settings as $key => $font ) {
648 $load_all_weights = false;
649 if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) {
650 $load_all_weights = true;
651 }
652
653 if ( isset( $font['selector'] ) && isset( $font['value'] ) && ! empty( $font['value'] ) ) {
654
655 $value = json_decode( PixCustomifyPlugin::decodeURIComponent( $font['value'] ), true );
656 // in case the value is still null, try default value(mostly for google fonts)
657 if ( $value === null ) {
658 $value = $this->get_font_defaults_value( $font['value'] );
659 }
660
661 // shim the old case when the default was only the font name
662 if ( is_string( $value ) && ! empty( $value ) ) {
663 $value = array( 'font_family' => $value );
664 }
665
666 //Handle special logic for when the $value array is not an associative array
667 if ( ! self::is_assoc( $value ) ) {
668 $value = $this->process_a_not_associative_font_default( $value );
669 }
670
671 $selected_variant = '';
672 if ( ! empty( $value['selected_variants'] ) ) {
673 if ( is_array( $value['selected_variants'] ) ) {
674 $selected_variant = $value['selected_variants'][0];
675 } else {
676 $selected_variant = $value['selected_variants'];
677 }
678 }
679
680 // First handle the case where we have the font-family in the selected variant (usually this means a custom font from our Fonto plugin)
681 if ( ! empty( $selected_variant ) && is_array( $selected_variant ) && ! empty( $selected_variant['font-family'] ) ) {
682 //the variant's font-family
683 echo $font['selector'] . " {\nfont-family: " . $selected_variant['font-family'] . ";\n";
684
685 if ( ! $load_all_weights ) {
686 // if this is a custom font (like from our plugin Fonto) with individual styles & weights - i.e. the font-family says it all
687 // we need to "force" the font-weight and font-style
688 if ( ! empty( $value['type'] ) && 'custom_individual' == $value['type'] ) {
689 $selected_variant['font-weight'] = '400 !important';
690 $selected_variant['font-style'] = 'normal !important';
691 }
692
693 // output the font weight, if available
694 if ( ! empty( $selected_variant['font-weight'] ) ) {
695 echo "font-weight: " . $selected_variant['font-weight'] . ";\n";
696 }
697
698 // output the font style, if available
699 if ( ! empty( $selected_variant['font-style'] ) ) {
700 echo "font-style: " . $selected_variant['font-style'] . ";\n";
701 }
702 }
703
704 echo "}\n";
705 } elseif ( isset( $value['font_family'] ) ) {
706 // the selected font family
707 echo $font['selector'] . " {\n font-family: " . $value['font_family'] . ";\n";
708
709 if ( ! empty( $selected_variant ) && ! $load_all_weights ) {
710 $weight_and_style = strtolower( $selected_variant );
711
712 $italic_font = false;
713
714 //determine if this is an italic font (the $weight_and_style is usually like '400' or '400italic' )
715 if ( strpos( $weight_and_style, 'italic' ) !== false ) {
716 $weight_and_style = str_replace( 'italic', '', $weight_and_style);
717 $italic_font = true;
718 }
719
720 if ( ! empty( $weight_and_style ) ) {
721 //a little bit of sanity check - in case it's not a number
722 if( $weight_and_style === 'regular' ) {
723 $weight_and_style = 'normal';
724 }
725 echo "font-weight: " . $weight_and_style . ";\n";
726 }
727
728 if ( $italic_font ) {
729 echo "font-style: italic;\n";
730 }
731 }
732
733 echo "}\n";
734 }
735 }
736 } ?>
737 </style>
738 <?php }
739
740 /**
741 * Handle special logic for when the $value array is not an associative array
742 * Return a new associative array with proper keys
743 */
744 protected function process_a_not_associative_font_default( $value ) {
745
746 $new_value = array();
747
748 //Let's determine some type of font
749 if ( ! isset( $value[2] ) || ( isset( $value[2] ) && 'google' == $value[2] ) ) {
750 $new_value = $this->get_font_defaults_value( $value[0] );
751 } else {
752 $new_value['type'] = $value[2];
753 }
754
755 if ( null == $new_value ) {
756 $new_value = array();
757 }
758
759 //The first entry is the font-family
760 if ( isset( $value[0] ) ) {
761 $new_value['font_family'] = $value[0];
762 }
763
764 //In case we don't have an associative array
765 //The second entry is the variants
766 if ( isset( $value[1] ) ) {
767 $new_value['selected_variants'] = $value[1];
768 }
769
770 return $new_value;
771 }
772
773 /**
774 *
775 * @param $font_name
776 *
777 * @return null
778 */
779 protected function get_font_defaults_value( $font_name ) {
780
781 if ( empty( self::$google_fonts ) ) {
782 $this->load_google_fonts();
783 }
784
785 if ( isset( self::$google_fonts[ $font_name ] ) ) {
786 $value = self::$google_fonts[ $font_name ];
787 $value['font_family'] = $font_name;
788 $value['type'] = 'google';
789 return $value;
790 } elseif ( isset( self::$theme_fonts[ $font_name ] ) ) {
791 $value['type'] = 'theme_font';
792 $value['src'] = self::$theme_fonts[ $font_name ]['src'];
793 $value['variants'] = self::$theme_fonts[ $font_name ]['variants'];
794 $value['font_family'] = self::$theme_fonts[ $font_name ]['family'];
795 return $value;
796 }
797
798 return null;
799 }
800
801 /**
802 * Turn css options into a valid CSS output
803 *
804 * @param $option_id
805 * @param array $css_config
806 *
807 * @return string
808 */
809 protected function convert_setting_to_css( $option_id, $css_config = array() ) {
810 $output = '';
811
812 $this_value = self::get_option( $option_id );
813
814 if ( empty( $css_config ) ) {
815 return $output;
816 }
817
818 foreach ( $css_config as $css_property ) {
819
820 if ( isset( $css_property['media'] ) && ! empty( $css_property['media'] ) ) {
821 self::$media_queries[ $css_property['media'] ][ $option_id ] = array(
822 'property' => $css_property,
823 'value' => $this_value
824 );
825 continue;
826 }
827
828 if ( ! isset( $css_property['selector'] ) || isset( $css_property['property'] ) ) {
829 $output .= self::proccess_css_property( $css_property, $this_value );
830 }
831 }
832
833 return $output;
834 }
835
836 protected function proccess_css_property( $css_property, $this_value ) {
837 $unit = '';
838
839 if ( isset( $css_property['unit'] ) ) {
840 $unit = $css_property['unit'];
841 }
842
843 // if the unit isn't specified but the property should have a unit force 'px' as it
844 if ( empty( $unit ) && in_array( $css_property['property'], self::$pixel_dependent_css_properties ) ) {
845 $unit = 'px';
846 }
847
848 $this_property_output = $css_property['selector'] . ' { ' . $css_property['property'] . ': ' . $this_value . $unit . "; } \n";
849
850 if ( isset( $css_property['callback_filter'] ) && function_exists( $css_property['callback_filter'] ) ) {
851 $this_property_output = call_user_func( $css_property['callback_filter'], $this_value, $css_property['selector'], $css_property['property'], $unit );
852 }
853
854 return $this_property_output;
855 }
856
857 protected function process_custom_background_field_output( $option_id, $options ) {
858 $selector = '';
859
860 if ( ! isset( $options['value'] ) ) {
861 return false;
862 }
863 $value = $options['value'];
864
865 if ( ! isset( $options['output'] ) ) {
866 return $selector;
867 } elseif ( is_string( $options['output'] ) ) {
868 $selector = $options['output'];
869 } elseif ( is_array( $options['output'] ) ) {
870 $selector = implode( ' ', $options['output'] );
871 }
872 ob_start();
873
874 echo "\n" . $selector . " { \n";
875 if ( isset( $value['background-image'] ) && ! empty( $value['background-image'] ) ) {
876 echo "background-image: url( " . $value['background-image'] . ");\n";
877 } else {
878 echo "background-image: none;\n";
879 }
880
881 if ( isset( $value['background-repeat'] ) && ! empty( $value['background-repeat'] ) ) {
882 echo "background-repeat:" . $value['background-repeat'] . ";\n";
883 }
884
885 if ( isset( $value['background-position'] ) && ! empty( $value['background-position'] ) ) {
886 echo "background-position:" . $value['background-position'] . ";\n";
887 }
888
889 if ( isset( $value['background-size'] ) && ! empty( $value['background-size'] ) ) {
890 echo "background-size:" . $value['background-size'] . ";\n";
891 }
892
893 if ( isset( $value['background-attachment'] ) && ! empty( $value['background-attachment'] ) ) {
894 echo "background-attachment:" . $value['background-attachment'] . ";\n";
895 }
896 echo "\n}\n";
897
898 return ob_get_clean();
899 }
900
901 /**
902 * add our customizer styling edits into the wp_editor
903 */
904 function add_customizer_settings_into_wp_editor() {
905
906 ob_start();
907 $this->output_typography_dynamic_style();
908 $this->output_dynamic_style();
909
910 $custom_css = ob_get_clean(); ?>
911 <script type="text/javascript">
912 /* <![CDATA[ */
913 (function( $ ) {
914 $( window ).load( function() {
915 /**
916 * @param iframe_id the id of the frame you whant to append the style
917 * @param style_element the style element you want to append
918 */
919 var append_script_to_iframe = function( ifrm_id, scriptEl ) {
920 var myIframe = document.getElementById( ifrm_id );
921
922 var script = myIframe.contentWindow.document.createElement( "script" );
923 script.type = "text/javascript";
924 script.innerHTML = scriptEl.innerHTML;
925
926 myIframe.contentWindow.document.head.appendChild( script );
927 };
928
929 var append_style_to_iframe = function( ifrm_id, styleElment ) {
930 var ifrm = window.frames[ifrm_id];
931 ifrm = ( ifrm.contentDocument || ifrm.contentDocument || ifrm.document );
932 var head = ifrm.getElementsByTagName( 'head' )[0];
933
934 if ( typeof styleElment !== "undefined" ) {
935 head.appendChild( styleElment );
936 }
937 };
938
939 var xmlString = <?php echo json_encode( str_replace( "\n", "", $custom_css ) ); ?>,
940 parser = new DOMParser(),
941 doc = parser.parseFromString( xmlString, "text/html" );
942
943 if ( typeof window.frames['content_ifr'] !== 'undefined' ) {
944
945 $.each( doc.head.childNodes, function( key, el ) {
946
947 if ( typeof el !== "undefined" && typeof el.tagName !== "undefined" ) {
948
949 switch ( el.tagName ) {
950
951 case 'STYLE' :
952 append_style_to_iframe( 'content_ifr', el );
953 break;
954
955 case 'SCRIPT' :
956 append_script_to_iframe( 'content_ifr', el );
957 break;
958 default:
959 break;
960 }
961 }
962 } );
963 }
964 } );
965 })( jQuery );
966 /* ]]> */
967 </script>
968 <?php }
969
970 /**
971 * Register the administration menu for this plugin into the WordPress Dashboard menu.
972 */
973 function add_plugin_admin_menu() {
974 $this->plugin_screen_hook_suffix = add_options_page( __( 'Customify', $this->plugin_slug ), __( 'Customify', $this->plugin_slug ), 'edit_plugins', $this->plugin_slug, array(
975 $this,
976 'display_plugin_admin_page'
977 ) );
978 }
979
980 /**
981 * Render the settings page for this plugin.
982 */
983 function display_plugin_admin_page() {
984 include_once( 'views/admin.php' );
985 }
986
987 /**
988 * Add settings action link to the plugins page.
989 */
990 function add_action_links( $links ) {
991 return array_merge( array( 'settings' => '<a href="' . admin_url( 'options-general.php?page=pixcustomify' ) . '">' . __( 'Settings', $this->plugin_slug ) . '</a>' ), $links );
992 }
993
994 protected function register_customizer_controls() {
995
996 // first get the base customizer extend class
997 require_once( self::get_base_path() . '/features/customizer/class-Pix_Customize_Control.php' );
998
999 // now get all the controls
1000 $path = self::get_base_path() . '/features/customizer/controls/';
1001 pixcustomify::require_all( $path );
1002 }
1003
1004 function register_customizer( $wp_customize ) {
1005
1006 $this->register_customizer_controls();
1007
1008 $customizer_settings = self::$customizer_config;
1009
1010 if ( ! empty ( $customizer_settings ) ) {
1011
1012 // first check the very needed options name
1013 if ( ! isset ( $customizer_settings['opt-name'] ) || empty( $customizer_settings['opt-name'] ) ) {
1014 return;
1015 }
1016 $options_name = $customizer_settings['opt-name'];
1017 $wp_customize->options_key = $options_name;
1018
1019 // let's check if we have sections or panels
1020 if ( isset( $customizer_settings['panels'] ) && ! empty( $customizer_settings['panels'] ) ) {
1021
1022 foreach ( $customizer_settings['panels'] as $panel_id => $panel_settings ) {
1023
1024 if ( ! empty( $panel_id ) && isset( $panel_settings['sections'] ) && ! empty( $panel_settings['sections'] ) ) {
1025
1026 $panel_id = $options_name . '[' . $panel_id . ']';
1027 $panel_args = array(
1028 'priority' => 10,
1029 'capability' => 'edit_theme_options',
1030 'title' => __( 'Panel title is required', 'pixcustomify' ),
1031 'description' => __( 'Description of what this panel does.', 'pixcustomify' ),
1032 );
1033
1034 if ( isset( $panel_settings['priority'] ) && ! empty( $panel_settings['priority'] ) ) {
1035 $panel_args['priority'] = $panel_settings['priority'];
1036 }
1037
1038 if ( isset( $panel_settings['title'] ) && ! empty( $panel_settings['title'] ) ) {
1039 $panel_args['title'] = $panel_settings['title'];
1040 }
1041
1042 if ( isset( $panel_settings['description'] ) && ! empty( $panel_settings['description'] ) ) {
1043 $panel_args[10] = $panel_settings['description'];
1044 }
1045
1046 $wp_customize->add_panel( $panel_id, $panel_args );
1047
1048 foreach ( $panel_settings['sections'] as $section_id => $section_settings ) {
1049 if ( ! empty( $section_id ) && isset( $section_settings['options'] ) && ! empty( $section_settings['options'] ) ) {
1050 $this->register_section( $panel_id, $section_id, $options_name, $section_settings, $wp_customize );
1051 }
1052 }
1053 }
1054 }
1055 }
1056
1057 if ( isset( $customizer_settings['sections'] ) && ! empty( $customizer_settings['sections'] ) ) {
1058
1059 foreach ( $customizer_settings['sections'] as $section_id => $section_settings ) {
1060 if ( ! empty( $section_id ) && isset( $section_settings['options'] ) && ! empty( $section_settings['options'] ) ) {
1061 $this->register_section( $panel_id = false, $section_id, $options_name, $section_settings, $wp_customize );
1062 }
1063 }
1064 }
1065
1066 if ( self::$plugin_settings['enable_reset_buttons'] ) {
1067 // create a toolbar section which will be present all the time
1068 $reset_section_settings = array(
1069 'title' => 'Customify toolbar',
1070 'options' => array(
1071 'reset_all_button' => array(
1072 'type' => 'button',
1073 'label' => 'Reset Customify',
1074 'action' => 'reset_customify',
1075 'value' => 'Reset'
1076 ),
1077 )
1078 );
1079
1080 $wp_customize->add_section(
1081 'customify_toolbar',
1082 array(
1083 'title' => '',
1084 'priority' => 999999999
1085 )
1086 );
1087
1088 $wp_customize->add_setting(
1089 'reset_customify',
1090 array()
1091 );
1092 $wp_customize->add_control( new Pix_Customize_Button_Control(
1093 $wp_customize,
1094 'reset_customify',
1095 array(
1096 'label' => __( 'Reset Customify to Defaults', 'customify' ),
1097 'section' => 'customify_toolbar',
1098 'settings' => 'reset_customify',
1099 'action' => 'reset_customify',
1100 )
1101 ) );
1102 }
1103
1104 // register typekit options
1105 if ( isset( $customizer_settings['typekit_options'] ) ) {
1106
1107 // create a toolbar section which will be present all the time
1108 $reset_section_settings = array(
1109 'title' => 'Customify Typekit Options',
1110 'capability' => 'manage_options',
1111 'options' => array(
1112 'typkit_user' => array(
1113 'type' => 'text',
1114 'label' => 'Typekit Username',
1115 ),
1116 'typkit_password' => array(
1117 'type' => 'text',
1118 'label' => 'Typekit Username',
1119 ),
1120 )
1121 );
1122 }
1123 }
1124
1125 do_action( 'customify_create_custom_control', $wp_customize );
1126 }
1127
1128 protected function register_section( $panel_id, $section_key, $options_name, $section_settings, $wp_customize ) {
1129
1130 if ( isset( self::$plugin_settings['disable_customify_sections'] ) && isset( self::$plugin_settings['disable_customify_sections'][ $section_key ] ) ) {
1131 return;
1132 }
1133
1134 $section_args = array(
1135 'priority' => 10,
1136 'capability' => 'edit_theme_options',
1137 'title' => __( 'Title Section is required', '' ),
1138 'panel' => $panel_id,
1139 );
1140 $section_id = $options_name . '[' . $section_key . ']';
1141
1142 if ( isset( $section_settings['priority'] ) && ! empty( $section_settings['priority'] ) ) {
1143 $section_args['priority'] = $section_settings['priority'];
1144 }
1145
1146 if ( isset( $section_settings['title'] ) && ! empty( $section_settings['title'] ) ) {
1147 $section_args['title'] = $section_settings['title'];
1148 }
1149
1150 if ( isset( $section_settings['theme_supports'] ) && ! empty( $section_settings['theme_supports'] ) ) {
1151 $section_args['theme_supports'] = $section_settings['theme_supports'];
1152 }
1153
1154 if ( isset( $section_settings['description'] ) && ! empty( $section_settings['description'] ) ) {
1155 $section_args['description'] = $section_settings['description'];
1156 }
1157
1158 $wp_customize->add_section( $section_id, $section_args );
1159
1160 foreach ( $section_settings['options'] as $option_id => $option_config ) {
1161
1162 if ( empty( $option_id ) || ! isset( $option_config['type'] ) ) {
1163 continue;
1164 }
1165
1166 $option_id = $options_name . '[' . $option_id . ']';
1167
1168 $this->register_field( $section_id, $option_id, $option_config, $wp_customize );
1169 }
1170
1171 }
1172
1173 protected function register_field( $section_id, $setting_id, $setting_config, $wp_customize ) {
1174
1175 $add_control = true;
1176 // defaults
1177 $setting_args = array(
1178 'default' => '',
1179 'capability' => 'edit_theme_options',
1180 'transport' => 'refresh',
1181 );
1182 $control_args = array(
1183 'label' => '',
1184 'section' => $section_id,
1185 'settings' => $setting_id,
1186 );
1187
1188 self::$localized['settings'][ $setting_id ] = $setting_config;
1189
1190 // sanitize settings
1191 if ( ( isset( $setting_config['live'] ) && $setting_config['live'] ) || $setting_config['type'] === 'font' ) {
1192 $setting_args['transport'] = 'postMessage';
1193 }
1194
1195 if ( isset( $setting_config['default'] ) ) {
1196 $setting_args['default'] = $setting_config['default'];
1197 }
1198
1199 if ( isset( $setting_config['capability'] ) && ! empty( $setting_config['capability'] ) ) {
1200 $setting_args['capability'] = $setting_config['capability'];
1201 }
1202
1203 if ( self::$plugin_settings['values_store_mod'] == 'option' ) {
1204 $setting_args['type'] = 'option';
1205 }
1206
1207 // if we arrive here this means we have a custom field control
1208 switch ( $setting_config['type'] ) {
1209
1210 case 'checkbox':
1211
1212 $setting_args['sanitize_callback'] = array( $this, 'setting_sanitize_checkbox' );
1213 break;
1214
1215 default:
1216 break;
1217 }
1218
1219 if ( isset( $setting_config['sanitize_callback'] ) && ! empty( $setting_config['sanitize_callback'] ) && function_exists( $setting_config['sanitize_callback'] ) ) {
1220 $setting_args['sanitize_callback'] = $setting_config['sanitize_callback'];
1221 }
1222
1223 // and add it
1224 $wp_customize->add_setting( $setting_id, $setting_args );
1225
1226 // now sanitize the control
1227 if ( isset( $setting_config['label'] ) && ! empty( $setting_config['label'] ) ) {
1228 $control_args['label'] = $setting_config['label'];
1229 }
1230
1231 if ( isset( $setting_config['priority'] ) && ! empty( $setting_config['priority'] ) ) {
1232 $control_args['priority'] = $setting_config['priority'];
1233 }
1234
1235 if ( isset( $setting_config['desc'] ) && ! empty( $setting_config['desc'] ) ) {
1236 $control_args['description'] = $setting_config['desc'];
1237 }
1238
1239
1240 $control_args['type'] = $setting_config['type'];
1241
1242 // select the control type
1243 // but first init a default
1244 $control_class_name = 'Pix_Customize_Text_Control';
1245
1246 // if is a standard wp field type call it here and skip the rest
1247 if ( in_array( $setting_config['type'], array(
1248 'checkbox',
1249 'dropdown-pages',
1250 'url',
1251 'date',
1252 'time',
1253 'datetime',
1254 'week',
1255 'search'
1256 ) ) ) {
1257 $wp_customize->add_control( $setting_id . '_control', $control_args );
1258
1259 return;
1260 } elseif ( in_array( $setting_config['type'], array(
1261 'radio',
1262 'select'
1263 ) ) && isset( $setting_config['choices'] ) && ! empty( $setting_config['choices'] )
1264 ) {
1265 $control_args['choices'] = $setting_config['choices'];
1266 $wp_customize->add_control( $setting_id . '_control', $control_args );
1267
1268 return;
1269 } elseif ( in_array( $setting_config['type'], array( 'range' ) ) && isset( $setting_config['input_attrs'] ) && ! empty( $setting_config['input_attrs'] ) ) {
1270
1271 $control_args['input_attrs'] = $setting_config['input_attrs'];
1272
1273 $wp_customize->add_control( $setting_id . '_control', $control_args );
1274 }
1275
1276 // if we arrive here this means we have a custom field control
1277 switch ( $setting_config['type'] ) {
1278
1279 case 'text':
1280
1281 if ( isset( $setting_config['live'] ) ) {
1282 $control_args['live'] = $setting_config['live'];
1283 }
1284
1285 $control_class_name = 'Pix_Customize_Text_Control';
1286 break;
1287
1288 case 'textarea':
1289
1290 if ( isset( $setting_config['live'] ) ) {
1291 $control_args['live'] = $setting_config['live'];
1292 }
1293
1294 $control_class_name = 'Pix_Customize_Textarea_Control';
1295 break;
1296
1297 case 'color':
1298
1299 $control_class_name = 'WP_Customize_Color_Control';
1300 break;
1301
1302 case 'color_drop':
1303
1304 $control_class_name = 'Pix_Customize_Color_Drop_Control';
1305 break;
1306
1307 case 'ace_editor':
1308
1309 if ( isset( $setting_config['live'] ) ) {
1310 $control_args['live'] = $setting_config['live'];
1311 }
1312
1313 if ( isset( $setting_config['editor_type'] ) ) {
1314 $control_args['editor_type'] = $setting_config['editor_type'];
1315 }
1316
1317 $control_class_name = 'Pix_Customize_Ace_Editor_Control';
1318 break;
1319
1320 case 'upload':
1321
1322 $control_class_name = 'WP_Customize_Upload_Control';
1323 break;
1324
1325 case 'image':
1326
1327 $control_class_name = 'WP_Customize_Image_Control';
1328 break;
1329
1330 case 'media':
1331
1332 $control_class_name = 'WP_Customize_Media_Control';
1333 break;
1334
1335 case 'custom_background':
1336 if ( isset( $setting_config['field'] ) ) {
1337 $control_args['field'] = $setting_config['field'];
1338 }
1339
1340 $control_class_name = 'Pix_Customize_Background_Control';
1341 break;
1342
1343 case 'cropped_media':
1344
1345 if ( isset( $setting_config['width'] ) ) {
1346 $control_args['width'] = $setting_config['width'];
1347 }
1348
1349 if ( isset( $setting_config['height'] ) ) {
1350 $control_args['height'] = $setting_config['height'];
1351 }
1352
1353 if ( isset( $setting_config['flex_width'] ) ) {
1354 $control_args['flex_width'] = $setting_config['flex_width'];
1355 }
1356
1357 if ( isset( $setting_config['flex_height'] ) ) {
1358 $control_args['flex_height'] = $setting_config['flex_height'];
1359 }
1360
1361 $control_class_name = 'WP_Customize_Cropped_Image_Control';
1362 break;
1363
1364 // Custom types
1365 case 'typography' :
1366
1367 $use_typography = self::get_plugin_option( 'typography', '1' );
1368
1369 if ( $use_typography === false ) {
1370 $add_control = false;
1371 continue;
1372 }
1373
1374 $control_class_name = 'Pix_Customize_Typography_Control';
1375
1376 if ( isset( $setting_config['backup'] ) ) {
1377 $control_args['backup'] = $setting_config['backup'];
1378 }
1379
1380 if ( isset( $setting_config['font_weight'] ) ) {
1381 $control_args['font_weight'] = $setting_config['font_weight'];
1382 }
1383
1384 if ( isset( $setting_config['subsets'] ) ) {
1385 $control_args['subsets'] = $setting_config['subsets'];
1386 }
1387
1388 if ( isset( $setting_config['recommended'] ) ) {
1389 $control_args['recommended'] = array_flip( $setting_config['recommended'] );
1390 }
1391
1392 if ( isset( $setting_config['load_all_weights'] ) ) {
1393 $control_args['load_all_weights'] = $setting_config['load_all_weights'];
1394 }
1395
1396 if ( isset( $setting_config['default'] ) ) {
1397 $control_args['default'] = $setting_config['default'];
1398 }
1399
1400 break;
1401
1402 // Custom types
1403 case 'font' :
1404
1405 $use_typography = self::get_plugin_option( 'typography', '1' );
1406
1407 if ( $use_typography === false ) {
1408 $add_control = false;
1409 continue;
1410 }
1411
1412 $control_class_name = 'Pix_Customize_Font_Control';
1413
1414 if ( isset( $setting_config['backup'] ) ) {
1415 $control_args['backup'] = $setting_config['backup'];
1416 }
1417
1418 if ( isset( $setting_config['font_weight'] ) ) {
1419 $control_args['font_weight'] = $setting_config['font_weight'];
1420 }
1421
1422 if ( isset( $setting_config['subsets'] ) ) {
1423 $control_args['subsets'] = $setting_config['subsets'];
1424 }
1425
1426 if ( isset( $setting_config['recommended'] ) ) {
1427 $control_args['recommended'] = array_flip( $setting_config['recommended'] );
1428 }
1429
1430 if ( isset( $setting_config['load_all_weights'] ) ) {
1431 $control_args['load_all_weights'] = $setting_config['load_all_weights'];
1432 }
1433
1434 if ( isset( $setting_config['default'] ) ) {
1435 $control_args['default'] = $setting_config['default'];
1436 }
1437
1438 if ( isset( $setting_config['fields'] ) ) {
1439 $control_args['fields'] = $setting_config['fields'];
1440 }
1441 $control_args['live'] = true;
1442
1443 break;
1444
1445 case 'select2' :
1446
1447 if ( ! isset( $setting_config['choices'] ) || empty( $setting_config['choices'] ) ) {
1448 return;
1449 }
1450
1451 $control_args['choices'] = $setting_config['choices'];
1452
1453 $control_class_name = 'Pix_Customize_Select2_Control';
1454 break;
1455
1456 case 'preset' :
1457
1458 if ( ! isset( $setting_config['choices'] ) || empty( $setting_config['choices'] ) ) {
1459 return;
1460 }
1461
1462 $control_args['choices'] = $setting_config['choices'];
1463
1464 if ( isset( $setting_config['choices_type'] ) || ! empty( $setting_config['choices_type'] ) ) {
1465 $control_args['choices_type'] = $setting_config['choices_type'];
1466 }
1467
1468 if ( isset( $setting_config['desc'] ) || ! empty( $setting_config['desc'] ) ) {
1469 $control_args['description'] = $setting_config['desc'];
1470 }
1471
1472
1473 $control_class_name = 'Pix_Customize_Preset_Control';
1474 break;
1475
1476 case 'radio_image' :
1477
1478 if ( ! isset( $setting_config['choices'] ) || empty( $setting_config['choices'] ) ) {
1479 return;
1480 }
1481
1482 $control_args['choices'] = $setting_config['choices'];
1483
1484 if ( isset( $setting_config['choices_type'] ) || ! empty( $setting_config['choices_type'] ) ) {
1485 $control_args['choices_type'] = $setting_config['choices_type'];
1486 }
1487
1488 if ( isset( $setting_config['desc'] ) || ! empty( $setting_config['desc'] ) ) {
1489 $control_args['description'] = $setting_config['desc'];
1490 }
1491
1492
1493 $control_class_name = 'Pix_Customize_Radio_Image_Control';
1494 break;
1495
1496 case 'button' :
1497
1498 if ( ! isset( $setting_config['action'] ) || empty( $setting_config['action'] ) ) {
1499 return;
1500 }
1501
1502 $control_args['action'] = $setting_config['action'];
1503
1504 $control_class_name = 'Pix_Customize_Button_Control';
1505
1506 break;
1507
1508 case 'html' :
1509
1510 if ( isset( $setting_config['html'] ) || ! empty( $setting_config['html'] ) ) {
1511 $control_args['html'] = $setting_config['html'];
1512 }
1513
1514 $control_class_name = 'Pix_Customize_HTML_Control';
1515 break;
1516
1517 case 'import_demo_data' :
1518
1519 if ( isset( $setting_config['html'] ) || ! empty( $setting_config['html'] ) ) {
1520 $control_args['html'] = $setting_config['html'];
1521 }
1522
1523 if ( ! isset( $setting_config['label'] ) || empty( $setting_config['label'] ) ) {
1524 $control_args['label'] = esc_html__( 'Import', 'customify' );
1525 } else {
1526 $control_args['label'] = $setting_config['label'];
1527 }
1528
1529 if ( isset( $setting_config['notices'] ) && ! empty( $setting_config['notices'] ) ) {
1530 $control_args['notices'] = $setting_config['notices'];
1531 }
1532
1533 $control_class_name = 'Pix_Customize_Import_Demo_Data_Control';
1534 break;
1535
1536 default:
1537 // if we don't have a real control just quit, it doesn't even matter
1538 return;
1539 break;
1540 }
1541
1542 $this_control = new $control_class_name(
1543 $wp_customize,
1544 $setting_id . '_control',
1545 $control_args
1546 );
1547
1548 if ( $add_control ) {
1549 $wp_customize->add_control( $this_control );
1550 }
1551 }
1552
1553 /**
1554 * Remove the sections selected by user
1555 *
1556 * @param $wp_customize
1557 */
1558 function remove_default_sections( $wp_customize ) {
1559 global $wp_registered_sidebars;
1560
1561 $to_remove = self::get_plugin_option( 'disable_default_sections' );
1562
1563 if ( ! empty( $to_remove ) ) {
1564 foreach ( $to_remove as $section => $nothing ) {
1565
1566 if ( $section === 'widgets' ) {
1567 foreach ( $wp_registered_sidebars as $widget => $settings ) {
1568 $wp_customize->remove_section( 'sidebar-widgets-' . $widget );
1569 }
1570 continue;
1571 }
1572
1573 $wp_customize->remove_section( $section );
1574 }
1575 }
1576 }
1577
1578 static function get_base_path() {
1579 return plugin_dir_path( __FILE__ );
1580 }
1581
1582 protected static function get_typography_fields( $array, $key, $value, &$results, $input_key = 0 ) {
1583 if ( ! is_array( $array ) ) {
1584 return;
1585 }
1586
1587 if ( isset( $array[ $key ] ) && $array[ $key ] == $value ) {
1588 $results[ $input_key ] = $array;
1589
1590 $default = null;
1591
1592 if ( isset( $array['default'] ) && is_array( $array['default'] ) ) {
1593 $default = json_encode( $array['default'] );
1594 }
1595
1596 $results[ $input_key ]['value'] = self::get_option( $input_key, $default );
1597 }
1598
1599 foreach ( $array as $i => $subarray ) {
1600 self::get_typography_fields( $subarray, $key, $value, $results, $i );
1601 }
1602 }
1603
1604 function get_options_key() {
1605 if ( ! empty( self::$opt_name ) ) {
1606 return self::$opt_name;
1607 }
1608
1609 return false;
1610 }
1611
1612 /**
1613 * Use this function when you need to know if an import button is used
1614 * @return bool
1615 */
1616 function import_button_exists() {
1617
1618 if ( empty( self::$options_list ) ) {
1619 self::$options_list = $this->get_options();
1620 }
1621
1622 foreach ( self::$options_list as $option ) {
1623 if ( isset( $option['type'] ) && 'import_demo_data' === $option['type'] ) {
1624 return true;
1625 break;
1626 }
1627 }
1628
1629 return false;
1630 }
1631
1632 /** == Helpers == */
1633
1634 protected static function get_current_values() {
1635 $store_type = self::get_plugin_option( 'values_store_mod', 'option' );
1636 if ( $store_type === 'option' ) {
1637 self::$current_values = get_option( self::$opt_name );
1638 } elseif ( $store_type === 'theme_mod' ) {
1639 self::$current_values = get_theme_mod( self::$opt_name );
1640 }
1641 }
1642
1643 public function get_options() {
1644
1645 $settings = array();
1646
1647 if ( isset ( self::$customizer_config['panels'] ) ) {
1648
1649 foreach ( self::$customizer_config['panels'] as $pane_id => $panel_settings ) {
1650
1651 if ( isset( $panel_settings['sections'] ) ) {
1652 foreach ( $panel_settings['sections'] as $section_id => $section_settings ) {
1653 if ( isset( $section_settings['options'] ) ) {
1654 foreach ( $section_settings['options'] as $option_id => $option ) {
1655 $settings[ $option_id ] = $option;
1656 if ( isset( self::$current_values[ $option_id ] ) ) {
1657 $settings[ $option_id ]['value'] = self::$current_values[ $option_id ];
1658 }
1659 }
1660 }
1661 }
1662 }
1663 }
1664 }
1665
1666 if ( isset ( self::$customizer_config['sections'] ) ) {
1667 foreach ( self::$customizer_config['sections'] as $section_id => $section_settings ) {
1668 if ( isset( $section_settings['options'] ) ) {
1669 foreach ( $section_settings['options'] as $option_id => $option ) {
1670 $settings[ $option_id ] = $option;
1671 if ( isset( self::$current_values[ $option_id ] ) ) {
1672 $settings[ $option_id ]['value'] = self::$current_values[ $option_id ];
1673 }
1674 }
1675 }
1676 }
1677 }
1678
1679 return $settings;
1680 }
1681
1682 protected static function get_value( $option ) {
1683 global $wp_customize;
1684
1685 if ( ! empty( $wp_customize ) && method_exists( $wp_customize, 'get_setting') ) {
1686
1687 $option_key = self::$opt_name . '[' . $option . ']';
1688 $setting = $wp_customize->get_setting( $option_key );
1689 if ( ! empty( $setting ) ) {
1690 $value = $setting->value();
1691 return $value;
1692 }
1693 }
1694
1695 // shim
1696 if ( strpos( $option, self::$opt_name . '[' ) !== false ) {
1697 // get only the setting id
1698 $option = explode( '[', $option );
1699 $option = rtrim( $option[1], ']' );
1700 }
1701
1702 if ( isset( self::$current_values[ $option ] ) ) {
1703 return self::$current_values[ $option ];
1704 }
1705
1706 return null;
1707 }
1708
1709 /**
1710 * A public function to retreat an option's value
1711 * If there is a value and return it
1712 * Otherwise try to get the default parameter or the default from config
1713 *
1714 * @param $option
1715 * @param null $default
1716 *
1717 * @return bool|null|string
1718 */
1719 static function get_option( $option, $default = null ) {
1720
1721 $return = self::get_value( $option );
1722
1723 if ( $return !== null ) {
1724 return $return;
1725 } elseif ( $default !== null ) {
1726 return $default;
1727 } elseif ( isset( self::$options_list[ $option ] ) && isset( self::$options_list[ $option ]['default'] ) ) {
1728 return self::$options_list[ $option ]['default'];
1729 }
1730
1731 return null;
1732 }
1733
1734 static function has_option( $option ) {
1735
1736 if ( isset( self::$options_list[ $option ] ) ) {
1737 return true;
1738 }
1739
1740 return false;
1741 }
1742
1743 protected static function get_config() {
1744 // @TODO maybe check this
1745 return include 'plugin-config.php';
1746 }
1747
1748 /**
1749 * Get an option's value from the config file
1750 *
1751 * @param $option
1752 * @param null $default
1753 *
1754 * @return bool|null
1755 */
1756 public static function get_config_option( $option, $default = null ) {
1757
1758 if ( isset( self::$config[ $option ] ) ) {
1759 return self::$config[ $option ];
1760 } elseif ( $default !== null ) {
1761 return $default;
1762 }
1763
1764 return false;
1765 }
1766
1767 static function get_plugin_option( $option, $default = null ) {
1768
1769 if ( isset( self::$plugin_settings[ $option ] ) ) {
1770 return self::$plugin_settings[ $option ];
1771 } elseif ( $default !== null ) {
1772 return $default;
1773 }
1774
1775 return false;
1776 }
1777
1778 /**
1779 * Sanitize functions
1780 */
1781
1782 /**
1783 * Sanitize the checkbox.
1784 *
1785 * @param boolean $input .
1786 *
1787 * @return boolean true if is 1 or '1', false if anything else
1788 */
1789 function setting_sanitize_checkbox( $input ) {
1790 if ( 1 == $input ) {
1791 return true;
1792 } else {
1793 return false;
1794 }
1795 }
1796
1797 /**
1798 * Checks whether an array is associative or not
1799 *
1800 * @param array $array
1801 *
1802 * @return bool
1803 */
1804 public static function is_assoc( $array ) {
1805
1806 if ( ! is_array( $array ) ) {
1807 return false;
1808 }
1809
1810 // Keys of the array
1811 $keys = array_keys( $array );
1812
1813 // If the array keys of the keys match the keys, then the array must
1814 // not be associative (e.g. the keys array looked like {0:0, 1:1...}).
1815 return array_keys( $keys ) !== $keys;
1816 }
1817
1818 function register_import_api(){
1819
1820 include_once( self::get_base_path() . '/features/class-Customify_Importer.php' );
1821 $controller = new Customify_Importer_Controller();
1822 $controller->init();
1823 }
1824
1825 function get_options_configs () {
1826 return self::$options_list;
1827 }
1828
1829 /**
1830 * Does the same thing the JS encodeURIComponent() does
1831 *
1832 * @param string $str
1833 *
1834 * @return string
1835 */
1836 public static function encodeURIComponent( $str ) {
1837 //if we get an array we just let it be
1838 if ( is_string( $str ) ) {
1839 $revert = array( '%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')' );
1840
1841 $str = strtr( rawurlencode( $str ), $revert );
1842 } else {
1843 var_dump('boooom');die;
1844 }
1845
1846 return $str;
1847 }
1848
1849 /**
1850 * Does the same thing the JS decodeURIComponent() does
1851 *
1852 * @param string $str
1853 *
1854 * @return string
1855 */
1856 public static function decodeURIComponent( $str ) {
1857 //if we get an array we just let it be
1858 if ( is_string( $str ) ) {
1859 $revert = array( '!' => '%21', '*' => '%2A', "'" => '%27', '(' => '%28', ')' => '%29' );
1860 $str = rawurldecode( strtr( $str, $revert ) );
1861 }
1862
1863 return $str;
1864 }
1865 }