PluginProbe
Customify / 1.2.7.1
Customify v1.2.7.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.2.7.1, at class-pixcustomify.php

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