PluginProbe
Customify / 1.2.6
Customify v1.2.6
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 / trunk / class-pixcustomify.php

class-pixcustomify.php in Customify 1.2.6, at trunk/class-pixcustomify.php

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