| @@ -3,10 +3,10 @@ | ||
| 3 | 3 | * PixCustomify. |
| 4 | 4 | * @package PixCustomify |
| 5 | 5 | * @author Pixelgrade <contact@pixelgrade.com> |
| 6 | 6 | * @license GPL-2.0+ |
| 7 | - * @link http://pixelgrade.com | |
| 8 | - * @copyright 2014 Pixelgrade | |
| 7 | + * @link https://pixelgrade.com | |
| 8 | + * @copyright 2014-2017 Pixelgrade | |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * Plugin class. |
| @@ -16,12 +16,12 @@ | ||
| 16 | 16 | class PixCustomifyPlugin { |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Plugin version, used for cache-busting of style and script file references. |
| 20 | - * @since 1.0.0 | |
| 20 | + * @since 1.5.0 | |
| 21 | 21 | * @const string |
| 22 | 22 | */ |
| 23 | - protected $version = '1.4.1'; | |
| 23 | + protected $_version; | |
| 24 | 24 | /** |
| 25 | 25 | * Unique identifier for your plugin. |
| 26 | 26 | * Use this value (not the variable name) as the text domain when internationalizing strings of text. It should |
| 27 | 27 | * match the Text Domain file header in the main plugin file. |
| @@ -31,12 +31,12 @@ | ||
| 31 | 31 | protected $plugin_slug = 'customify'; |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * Instance of this class. |
| 35 | - * @since 1.0.0 | |
| 35 | + * @since 1.5.0 | |
| 36 | 36 | * @var object |
| 37 | 37 | */ |
| 38 | - protected static $instance = null; | |
| 38 | + protected static $_instance = null; | |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * Slug of the plugin screen. |
| 42 | 42 | * @since 1.0.0 |
| @@ -43,38 +43,31 @@ | ||
| 43 | 43 | * @var string |
| 44 | 44 | */ |
| 45 | 45 | protected $plugin_screen_hook_suffix = null; |
| 46 | 46 | |
| 47 | - /** | |
| 48 | - * Path to the plugin. | |
| 49 | - * @since 1.0.0 | |
| 50 | - * @var string | |
| 51 | - */ | |
| 52 | - protected $plugin_basepath = null; | |
| 53 | - | |
| 54 | 47 | public $display_admin_menu = false; |
| 55 | 48 | |
| 56 | - private static $config; | |
| 49 | + private $config; | |
| 57 | 50 | |
| 58 | - private static $customizer_config; | |
| 51 | + private $customizer_config; | |
| 59 | 52 | |
| 60 | - public static $plugin_settings; | |
| 53 | + public $plugin_settings; | |
| 61 | 54 | |
| 62 | - protected static $localized = array(); | |
| 55 | + protected $localized = array(); | |
| 63 | 56 | |
| 64 | - protected static $current_values = array(); | |
| 57 | + protected $current_values = array(); | |
| 65 | 58 | |
| 66 | - protected static $options_list = array(); | |
| 59 | + protected $options_list = array(); | |
| 67 | 60 | |
| 68 | - protected static $media_queries = array(); | |
| 61 | + protected $media_queries = array(); | |
| 69 | 62 | |
| 70 | - protected static $opt_name; | |
| 63 | + protected $opt_name; | |
| 71 | 64 | |
| 72 | - protected static $typo_settings; | |
| 65 | + protected $typo_settings; | |
| 73 | 66 | |
| 74 | - protected static $google_fonts = null; | |
| 67 | + protected $google_fonts = null; | |
| 75 | 68 | |
| 76 | - protected static $theme_fonts = null; | |
| 69 | + protected $theme_fonts = null; | |
| 77 | 70 | |
| 78 | 71 | // these properties will get 'px' as a default unit |
| 79 | 72 | protected static $pixel_dependent_css_properties = array( |
| 80 | 73 | 'width', |
| @@ -112,35 +105,106 @@ | ||
| 112 | 105 | 'border-right-width', |
| 113 | 106 | 'border-top-width' |
| 114 | 107 | ); |
| 115 | 108 | |
| 116 | - protected static $jetpack_default_modules = array(); | |
| 117 | - protected static $jetpack_blocked_modules = array(); | |
| 118 | - protected static $jetpack_sharing_default_options = array(); | |
| 109 | + protected $jetpack_default_modules = array(); | |
| 110 | + protected $jetpack_blocked_modules = array(); | |
| 111 | + protected $jetpack_sharing_default_options = array(); | |
| 119 | 112 | |
| 120 | 113 | /** |
| 121 | - * Initialize the plugin by setting localization, filters, and administration functions. | |
| 122 | - * @since 1.0.0 | |
| 114 | + * The main plugin file. | |
| 115 | + * @var string | |
| 116 | + * @access public | |
| 117 | + * @since 1.5.0 | |
| 123 | 118 | */ |
| 124 | - protected function __construct() { | |
| 119 | + public $file; | |
| 125 | 120 | |
| 126 | - $this->plugin_basepath = plugin_dir_path( __FILE__ ); | |
| 127 | - self::$config = self::get_config(); | |
| 128 | - self::$plugin_settings = get_option( 'pixcustomify_settings' ); | |
| 121 | + /** | |
| 122 | + * Style Manager class object. | |
| 123 | + * @var Customify_Style_Manager | |
| 124 | + * @access public | |
| 125 | + * @since 1.0.0 | |
| 126 | + */ | |
| 127 | + public $style_manager = null; | |
| 129 | 128 | |
| 130 | - // load custom modules | |
| 131 | - include_once( self::get_base_path() . '/features/class-CSS_Editor.php' ); | |
| 132 | - include_once( self::get_base_path() . 'features/class-Font_Selector.php' ); | |
| 129 | + /** | |
| 130 | + * Minimal Required PHP Version | |
| 131 | + * @var string | |
| 132 | + * @access private | |
| 133 | + * @since 1.5.0 | |
| 134 | + */ | |
| 135 | + private $minimalRequiredPhpVersion = 5.2; | |
| 133 | 136 | |
| 134 | - // Load plugin text domain | |
| 137 | + protected function __construct( $file, $version = '1.0.0' ) { | |
| 138 | + //the main plugin file (the one that loads all this) | |
| 139 | + $this->file = $file; | |
| 140 | + //the current plugin version | |
| 141 | + $this->_version = $version; | |
| 142 | + | |
| 143 | + if ( $this->php_version_check() ) { | |
| 144 | + // Only load and run the init function if we know PHP version can parse it. | |
| 145 | + $this->init(); | |
| 146 | + } | |
| 147 | + } | |
| 148 | + | |
| 149 | + /** | |
| 150 | + * Initialize plugin | |
| 151 | + */ | |
| 152 | + private function init() { | |
| 153 | + // Load the config file | |
| 154 | + $this->config = $this->get_config(); | |
| 155 | + // Load the plugin's settings from the DB | |
| 156 | + $this->plugin_settings = get_option( $this->config['settings-key'] ); | |
| 157 | + | |
| 158 | + /* Initialize the Style Manager logic. */ | |
| 159 | + require_once( $this->get_base_path() . 'includes/class-customify-style-manager.php' ); | |
| 160 | + if ( is_null( $this->style_manager ) ) { | |
| 161 | + $this->style_manager = Customify_Style_Manager::instance( $this ); | |
| 162 | + } | |
| 163 | + | |
| 164 | + // Register all the needed hooks | |
| 165 | + $this->register_hooks(); | |
| 166 | + } | |
| 167 | + | |
| 168 | + /** | |
| 169 | + * Register our actions and filters | |
| 170 | + */ | |
| 171 | + function register_hooks() { | |
| 172 | + /* | |
| 173 | + * Register hooks that are fired when the plugin is activated, deactivated, and uninstalled, respectively. | |
| 174 | + */ | |
| 175 | + register_activation_hook( $this->file, array( $this, 'activate' ) ); | |
| 176 | + register_deactivation_hook( $this->file, array( $this, 'deactivate' ) ); | |
| 177 | + | |
| 178 | + /* | |
| 179 | + * Load plugin text domain | |
| 180 | + */ | |
| 135 | 181 | add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); |
| 136 | 182 | |
| 137 | - add_action( 'wp_loaded', array( $this, 'init_plugin_configs' ), 5 ); | |
| 183 | + /* | |
| 184 | + * Load the upgrade logic. | |
| 185 | + */ | |
| 186 | + add_action( 'admin_init', array( $this, 'upgrade' ) ); | |
| 138 | 187 | |
| 188 | + /* | |
| 189 | + * Prepare and load the configuration | |
| 190 | + */ | |
| 191 | + $this->init_plugin_configs(); | |
| 192 | + | |
| 193 | + // We need to load the configuration as late as possible so we allow all that want to influence it | |
| 194 | + // We need the init hook and not after_setup_theme because there a number of plugins that fire up on init (like certain modules from Jetpack) | |
| 195 | + // We need to be able to load things like components configs depending on those firing up or not | |
| 196 | + // DO NOT TRY to use the Customify values before this! | |
| 197 | + add_action( 'init', array( $this, 'load_plugin_configs' ), 15 ); | |
| 198 | + | |
| 199 | + /* | |
| 200 | + * Now setup the admin side of things | |
| 201 | + */ | |
| 202 | + // Starting with the menu item for this plugin | |
| 139 | 203 | add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) ); |
| 140 | 204 | |
| 141 | 205 | // Add an action link pointing to the options page. |
| 142 | - $plugin_basename = plugin_basename( plugin_dir_path( __FILE__ ) . 'pixcustomify.php' ); | |
| 206 | + $plugin_basename = plugin_basename( plugin_dir_path( $this->file ) . 'pixcustomify.php' ); | |
| 143 | 207 | add_filter( 'plugin_action_links_' . $plugin_basename, array( $this, 'add_action_links' ) ); |
| 144 | 208 | |
| 145 | 209 | // Load admin style sheet and JavaScript. |
| 146 | 210 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); |
| @@ -145,144 +209,128 @@ | ||
| 145 | 209 | // Load admin style sheet and JavaScript. |
| 146 | 210 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); |
| 147 | 211 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); |
| 148 | 212 | |
| 149 | -// add_action( 'plugins_loaded', array( $this, 'register_metaboxes' ), 14 ); | |
| 213 | + /* | |
| 214 | + * Now it's time for the Customizer logic to kick in | |
| 215 | + */ | |
| 216 | + // Styles for the Customizer | |
| 217 | + add_action( 'customize_controls_init', array( $this, 'register_admin_customizer_styles' ), 10 ); | |
| 150 | 218 | add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_admin_customizer_styles' ), 10 ); |
| 151 | - add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_admin_customizer_scripts' ), 10 ); | |
| 219 | + // Scripts enqueued in the Customizer | |
| 220 | + add_action( 'customize_controls_init', array( $this, 'register_admin_customizer_scripts' ), 15 ); | |
| 221 | + add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_admin_customizer_scripts' ), 15 ); | |
| 222 | + | |
| 223 | + // Scripts enqueued only in the theme preview | |
| 224 | + add_action( 'customize_preview_init', array( $this, 'customizer_live_preview_register_scripts' ), 10 ); | |
| 152 | 225 | add_action( 'customize_preview_init', array( $this, 'customizer_live_preview_enqueue_scripts' ), 99999 ); |
| 153 | 226 | |
| 154 | - $load_location = self::get_plugin_option( 'style_resources_location', 'wp_head' ); | |
| 227 | + // Add extra settings data to _wpCustomizeSettings.settings of the parent window. | |
| 228 | + add_action( 'customize_controls_print_footer_scripts', array( $this, 'customize_pane_settings_additional_data' ), 10000 ); | |
| 155 | 229 | |
| 156 | - add_action( $load_location, array( $this, 'output_dynamic_style' ), 99999 ); | |
| 230 | + // The frontend effects of the Customizer controls | |
| 231 | + $load_location = $this->get_plugin_setting( 'style_resources_location', 'wp_head' ); | |
| 232 | + | |
| 233 | + add_action( $load_location, array( $this, 'output_dynamic_style' ), 99 ); | |
| 157 | 234 | add_action( 'wp_head', array( $this, 'output_typography_dynamic_style' ), 10 ); |
| 158 | 235 | |
| 159 | 236 | add_action( 'customize_register', array( $this, 'remove_default_sections' ), 11 ); |
| 160 | 237 | add_action( 'customize_register', array( $this, 'register_customizer' ), 12 ); |
| 238 | + // Maybe the theme has instructed us to do things like removing sections or controls. | |
| 239 | + add_action( 'customize_register', array( $this, 'maybe_process_config_extras' ), 13 ); | |
| 161 | 240 | |
| 162 | - if ( self::get_plugin_option( 'enable_editor_style', true ) ) { | |
| 241 | + if ( $this->get_plugin_setting( 'enable_editor_style', true ) ) { | |
| 163 | 242 | add_action( 'admin_head', array( $this, 'add_customizer_settings_into_wp_editor' ) ); |
| 164 | 243 | } |
| 165 | 244 | |
| 166 | - // Jetpack Related | |
| 167 | - add_action( 'init', array( $this, 'set_jetpack_modules_config') ); | |
| 168 | - add_filter( 'default_option_jetpack_active_modules', array( $this, 'default_jetpack_active_modules' ), 10, 1 ); | |
| 169 | - add_filter( 'jetpack_get_available_modules', array( $this, 'jetpack_hide_blocked_modules'), 10, 1 ); | |
| 170 | - add_filter( 'default_option_sharing-options', array( $this, 'default_jetpack_sharing_options' ), 10, 1 ); | |
| 245 | + add_action( 'rest_api_init', array( $this, 'add_rest_routes_api' ) ); | |
| 246 | + | |
| 247 | + /* | |
| 248 | + * Development related | |
| 249 | + */ | |
| 250 | + if ( defined( 'CUSTOMIFY_DEV_FORCE_DEFAULTS' ) && true === CUSTOMIFY_DEV_FORCE_DEFAULTS ) { | |
| 251 | + // If the development constant CUSTOMIFY_DEV_FORCE_DEFAULTS has been defined we will not save anything in the database | |
| 252 | + // Always go with the default | |
| 253 | + add_filter( 'customize_changeset_save_data', array( $this, 'prevent_changeset_save_in_devmode' ), 50, 2 ); | |
| 254 | + // Add a JS to display a notification | |
| 255 | + add_action( 'customize_controls_print_footer_scripts', array( $this, 'prevent_changeset_save_in_devmode_notification' ), 100 ); | |
| 256 | + } | |
| 171 | 257 | } |
| 172 | 258 | |
| 173 | 259 | /** |
| 174 | - * Return an instance of this class. | |
| 175 | - * @since 1.0.0 | |
| 176 | - * @return object A single instance of this class. | |
| 260 | + * Handle the logic to upgrade between versions. It will run only one per version change. | |
| 177 | 261 | */ |
| 178 | - public static function get_instance() { | |
| 262 | + public function upgrade() { | |
| 263 | + $customify_dbversion = get_option( 'customify_dbversion', '0.0.1' ); | |
| 264 | + if ( $this->get_version() === $customify_dbversion ) { | |
| 265 | + return; | |
| 266 | + } | |
| 179 | 267 | |
| 180 | - // If the single instance hasn't been set, set it now. | |
| 181 | - if ( null == self::$instance ) { | |
| 182 | - self::$instance = new self; | |
| 268 | + // For versions, previous of version 2.0.0 (the Color Palettes v2.0 release). | |
| 269 | + if ( version_compare( $customify_dbversion, '2.0.0', '<' ) ) { | |
| 270 | + // Delete the option holding the fact that the user offered feedback. | |
| 271 | + delete_option( 'style_manager_user_feedback_provided' ); | |
| 183 | 272 | } |
| 184 | 273 | |
| 185 | - return self::$instance; | |
| 274 | + // Put the current version in the database. | |
| 275 | + update_option( 'customify_dbversion', $this->get_version(), true ); | |
| 186 | 276 | } |
| 187 | 277 | |
| 188 | 278 | /** |
| 189 | - * Configs, Options and Values methods | |
| 279 | + * Initialize Configs, Options and Values methods. | |
| 190 | 280 | */ |
| 191 | - | |
| 192 | 281 | function init_plugin_configs() { |
| 193 | - self::$customizer_config = get_option( 'pixcustomify_config' ); | |
| 282 | + $this->customizer_config = get_option( 'pixcustomify_config' ); | |
| 194 | 283 | |
| 195 | - // no option so go for default | |
| 196 | - if ( empty( self::$customizer_config ) ) { | |
| 197 | - self::$customizer_config = self::get_config_option( 'default_options' ); | |
| 284 | + // no option so go for default. | |
| 285 | + if ( empty( $this->customizer_config ) ) { | |
| 286 | + $this->customizer_config = $this->get_config_option( 'default_options' ); | |
| 198 | 287 | } |
| 199 | 288 | |
| 200 | - if ( empty( self::$customizer_config ) ) { | |
| 201 | - self::$customizer_config = array(); | |
| 289 | + if ( empty( $this->customizer_config ) ) { | |
| 290 | + $this->customizer_config = array(); | |
| 202 | 291 | } |
| 292 | + } | |
| 203 | 293 | |
| 204 | - // allow themes or other plugins to filter the config | |
| 205 | - self::$customizer_config = apply_filters( 'customify_filter_fields', self::$customizer_config ); | |
| 294 | + /** | |
| 295 | + * Load the plugin configuration and options. | |
| 296 | + */ | |
| 297 | + function load_plugin_configs() { | |
| 206 | 298 | |
| 207 | - self::$opt_name = self::$localized['options_name'] = self::$customizer_config['opt-name']; | |
| 299 | + // Allow themes or other plugins to filter the config. | |
| 300 | + $this->customizer_config = apply_filters( 'customify_filter_fields', $this->customizer_config ); | |
| 301 | + // We apply a second filter for those that wish to work with the final config and not rely on a a huge priority number. | |
| 302 | + $this->customizer_config = apply_filters( 'customify_final_config', $this->customizer_config ); | |
| 208 | 303 | |
| 209 | - self::get_current_values(); | |
| 210 | - self::$options_list = $this->get_options(); | |
| 304 | + $this->opt_name = $this->localized['options_name'] = $this->customizer_config['opt-name']; | |
| 305 | + $this->options_list = $this->get_options(); | |
| 211 | 306 | |
| 307 | + // Load the current options values. | |
| 308 | + $this->current_values = $this->get_current_values(); | |
| 309 | + | |
| 212 | 310 | if ( $this->import_button_exists() ) { |
| 213 | - self::$opt_name = self::$localized['import_rest_url'] = get_rest_url( '/customify/1.0/' ); | |
| 214 | - self::$opt_name = self::$localized['import_rest_nonce'] = wp_create_nonce( 'wp_rest' ); | |
| 311 | + $this->localized['import_rest_url'] = get_rest_url( '/customify/1.0/' ); | |
| 312 | + $this->localized['import_rest_nonce'] = wp_create_nonce( 'wp_rest' ); | |
| 215 | 313 | |
| 216 | 314 | $this->register_import_api(); |
| 217 | 315 | } |
| 218 | 316 | |
| 219 | - $font_selector = new Customify_Font_Selector( $this ); | |
| 220 | - self::$localized['theme_fonts'] = self::$theme_fonts = Customify_Font_Selector::$theme_fonts; | |
| 221 | - } | |
| 317 | + $this->localized['theme_fonts'] = $this->theme_fonts = Customify_Font_Selector::instance()->get_theme_fonts(); | |
| 222 | 318 | |
| 223 | - function set_jetpack_modules_config() { | |
| 224 | - // We expect an array of string module names like array( 'infinite-scroll', 'widgets' ) | |
| 225 | - // See jetpack/modules/modules-heading.php for module names | |
| 226 | - self::$jetpack_default_modules = apply_filters ( 'customify_filter_jetpack_default_modules', array( | |
| 227 | - 'shortcodes', | |
| 228 | - 'widget-visibility', | |
| 229 | - 'widgets', | |
| 230 | - ) ); | |
| 231 | - | |
| 232 | - // We expect an array of string module names like array( 'infinite-scroll', 'widgets' ) | |
| 233 | - // See jetpack/modules/modules-heading.php for module names | |
| 234 | - self::$jetpack_blocked_modules = apply_filters ( 'customify_filter_jetpack_blocked_modules', array() ); | |
| 235 | - | |
| 236 | - self::$jetpack_sharing_default_options = apply_filters ( 'customify_filter_jetpack_sharing_default_options', array() ); | |
| 319 | + $this->localized['ajax_url'] = admin_url( 'admin-ajax.php' ); | |
| 320 | + $this->localized['style_manager_user_feedback_nonce'] = wp_create_nonce( 'customify_style_manager_user_feedback' ); | |
| 321 | + $this->localized['style_manager_user_feedback_provided'] = get_option( 'style_manager_user_feedback_provided', false ); | |
| 237 | 322 | } |
| 238 | 323 | |
| 239 | - /** | |
| 240 | - * Control the default modules that are activated in Jetpack. | |
| 241 | - * Use the `customify_filter_jetpack_default_modules` to set your's. | |
| 242 | - * | |
| 243 | - * @param array $default The default value to return if the option does not exist | |
| 244 | - * in the database. | |
| 245 | - * @return array | |
| 246 | - */ | |
| 247 | - function default_jetpack_active_modules( $default ) { | |
| 248 | - if ( ! is_array( $default ) ) { | |
| 249 | - $default = array(); | |
| 250 | - } | |
| 251 | - | |
| 252 | - return array_merge( $default, self::$jetpack_default_modules ); | |
| 324 | + public function get_version() { | |
| 325 | + return $this->_version; | |
| 253 | 326 | } |
| 254 | 327 | |
| 255 | - /** | |
| 256 | - * Control the default Jetpack Sharing options. | |
| 257 | - * Use the `customify_filter_jetpack_sharing_default_options` to set your's. | |
| 258 | - * | |
| 259 | - * @param array $default The default value to return if the option does not exist | |
| 260 | - * in the database. | |
| 261 | - * | |
| 262 | - * @return array | |
| 263 | - */ | |
| 264 | - function default_jetpack_sharing_options( $default ) { | |
| 265 | - if ( ! is_array( $default ) ) { | |
| 266 | - $default = array(); | |
| 267 | - } | |
| 268 | - | |
| 269 | - return array_merge( $default, self::$jetpack_sharing_default_options ); | |
| 328 | + public function get_slug() { | |
| 329 | + return $this->plugin_slug; | |
| 270 | 330 | } |
| 271 | 331 | |
| 272 | 332 | /** |
| 273 | - * Control the modules that are available in Jetpack (hide some of them). | |
| 274 | - * Use the `customify_filter_jetpack_blocked_modules` filter to set your's. | |
| 275 | - * | |
| 276 | - * @param array $modules | |
| 277 | - * | |
| 278 | - * @return array | |
| 279 | - */ | |
| 280 | - function jetpack_hide_blocked_modules( $modules ) { | |
| 281 | - return array_diff_key( $modules, array_flip( self::$jetpack_blocked_modules ) ); | |
| 282 | - } | |
| 283 | - | |
| 284 | - /** | |
| 285 | 333 | * Fired when the plugin is activated. |
| 286 | 334 | * @since 1.0.0 |
| 287 | 335 | * |
| 288 | 336 | * @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. |
| @@ -287,9 +335,9 @@ | ||
| 287 | 335 | * |
| 288 | 336 | * @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog. |
| 289 | 337 | */ |
| 290 | 338 | public static function activate( $network_wide ) { |
| 291 | - | |
| 339 | + //@todo Define activation functionality here | |
| 292 | 340 | } |
| 293 | 341 | |
| 294 | 342 | /** |
| 295 | 343 | * Fired when the plugin is deactivated. |
| @@ -297,9 +345,9 @@ | ||
| 297 | 345 | * |
| 298 | 346 | * @param boolean $network_wide True if WPMU superadmin uses "Network Deactivate" action, false if WPMU is disabled or plugin is deactivated on an individual blog. |
| 299 | 347 | */ |
| 300 | 348 | static function deactivate( $network_wide ) { |
| 301 | - // TODO: Define deactivation functionality here | |
| 349 | + //@todo Define deactivation functionality here | |
| 302 | 350 | } |
| 303 | 351 | |
| 304 | 352 | /** |
| 305 | 353 | * Load the plugin text domain for translation. |
| @@ -306,53 +354,80 @@ | ||
| 306 | 354 | * @since 1.0.0 |
| 307 | 355 | */ |
| 308 | 356 | function load_plugin_textdomain() { |
| 309 | 357 | $domain = $this->plugin_slug; |
| 310 | - load_plugin_textdomain( $domain, false, basename( dirname( __FILE__ ) ) . '/languages/' ); | |
| 358 | + load_plugin_textdomain( $domain, false, basename( dirname( $this->file ) ) . '/languages/' ); | |
| 311 | 359 | } |
| 312 | 360 | |
| 313 | 361 | /** === RESOURCES === **/ |
| 314 | 362 | |
| 315 | 363 | /** |
| 316 | - * Customizer admin styles | |
| 364 | + * Register Customizer admin styles | |
| 317 | 365 | */ |
| 366 | + function register_admin_customizer_styles() { | |
| 367 | + wp_register_style( 'customify_select2', plugins_url( 'js/select2/css/select2.css', $this->file ), array(), $this->_version ); | |
| 368 | + wp_register_style( 'customify_style', plugins_url( 'css/customizer.css', $this->file ), array( 'customify_select2' ), $this->_version ); | |
| 369 | + } | |
| 370 | + | |
| 371 | + /** | |
| 372 | + * Enqueue Customizer admin styles | |
| 373 | + */ | |
| 318 | 374 | function enqueue_admin_customizer_styles() { |
| 319 | - wp_enqueue_style( 'select2', plugins_url( 'js/select2/css/select2.css', __FILE__ ), array(), $this->version ); | |
| 320 | - wp_enqueue_style( 'customify_style', plugins_url( 'css/customizer.css', __FILE__ ), array(), $this->version ); | |
| 375 | + wp_enqueue_style( 'customify_style' ); | |
| 321 | 376 | } |
| 322 | 377 | |
| 323 | 378 | /** |
| 324 | - * Customizer admin scripts | |
| 379 | + * Register Customizer admin scripts | |
| 325 | 380 | */ |
| 326 | - function enqueue_admin_customizer_scripts() { | |
| 381 | + function register_admin_customizer_scripts() { | |
| 327 | 382 | |
| 328 | - wp_enqueue_script( 'select2', plugins_url( 'js/select2/js/select2.js', __FILE__ ), array( 'jquery' ), $this->version ); | |
| 329 | - wp_enqueue_script( 'jquery-react', plugins_url( 'js/jquery-react.js', __FILE__ ), array( 'jquery' ), $this->version ); | |
| 330 | - wp_enqueue_script( $this->plugin_slug . '-customizer-scripts', plugins_url( 'js/customizer.js', __FILE__ ), array( | |
| 383 | + wp_register_script( 'customify_select2', plugins_url( 'js/select2/js/select2.js', $this->file ), array( 'jquery' ), $this->_version ); | |
| 384 | + wp_register_script( 'jquery-react', plugins_url( 'js/jquery-react.js', $this->file ), array( 'jquery' ), $this->_version ); | |
| 385 | + | |
| 386 | + wp_register_script( 'customify-scale', plugins_url( 'js/customizer/scale-iframe.js', $this->file ), array( 'jquery' ), $this->_version ); | |
| 387 | + wp_register_script( 'customify-fontselectfields', plugins_url( 'js/customizer/font-select-fields.js', $this->file ), array( 'jquery' ), $this->_version ); | |
| 388 | + | |
| 389 | + wp_register_script( $this->plugin_slug . '-customizer-scripts', plugins_url( 'js/customizer.js', $this->file ), array( | |
| 331 | 390 | 'jquery', |
| 332 | - 'select2', | |
| 391 | + 'customify_select2', | |
| 333 | 392 | 'underscore', |
| 334 | - 'customize-controls' | |
| 335 | - ), $this->version ); | |
| 393 | + 'customize-controls', | |
| 394 | + 'customify-fontselectfields', | |
| 336 | 395 | |
| 337 | - wp_localize_script( $this->plugin_slug . '-customizer-scripts', 'customify_settings', self::$localized ); | |
| 396 | + 'customify-scale', | |
| 397 | + ), $this->_version ); | |
| 338 | 398 | } |
| 339 | 399 | |
| 340 | - /** Customizer scripts loaded only on previewer page */ | |
| 341 | - function customizer_live_preview_enqueue_scripts() { | |
| 400 | + /** | |
| 401 | + * Enqueue Customizer admin scripts | |
| 402 | + */ | |
| 403 | + function enqueue_admin_customizer_scripts() { | |
| 404 | + wp_enqueue_script( 'jquery-react' ); | |
| 405 | + wp_enqueue_script( $this->plugin_slug . '-customizer-scripts' ); | |
| 342 | 406 | |
| 343 | - wp_register_script( $this->plugin_slug . 'CSSOM', plugins_url( 'js/CSSOM.js', __FILE__ ), array( 'jquery' ), $this->version, true ); | |
| 344 | - wp_register_script( $this->plugin_slug . 'cssUpdate', plugins_url( 'js/jquery.cssUpdate.js', __FILE__ ), array(), $this->version, true ); | |
| 345 | - wp_enqueue_script( $this->plugin_slug . '-previewer-scripts', plugins_url( 'js/customizer_preview.js', __FILE__ ), array( | |
| 407 | + wp_localize_script( $this->plugin_slug . '-customizer-scripts', 'customify_settings', apply_filters( 'customify_localized_js_settings', $this->localized ) ); | |
| 408 | + } | |
| 409 | + | |
| 410 | + /** Register Customizer scripts loaded only on previewer page */ | |
| 411 | + function customizer_live_preview_register_scripts() { | |
| 412 | + wp_register_script( $this->plugin_slug . 'CSSOM', plugins_url( 'js/CSSOM.js', $this->file ), array( 'jquery' ), $this->_version, true ); | |
| 413 | + wp_register_script( $this->plugin_slug . 'cssUpdate', plugins_url( 'js/jquery.cssUpdate.js', $this->file ), array(), $this->_version, true ); | |
| 414 | + wp_register_script( $this->plugin_slug . '-previewer-scripts', plugins_url( 'js/customizer_preview.js', $this->file ), array( | |
| 346 | 415 | 'jquery', |
| 416 | + 'customize-preview', | |
| 347 | 417 | $this->plugin_slug . 'CSSOM', |
| 348 | 418 | $this->plugin_slug . 'cssUpdate' |
| 349 | - ), $this->version, true ); | |
| 419 | + ), $this->_version, true ); | |
| 420 | + } | |
| 350 | 421 | |
| 422 | + /** Enqueue Customizer scripts loaded only on previewer page */ | |
| 423 | + function customizer_live_preview_enqueue_scripts() { | |
| 424 | + wp_enqueue_script( $this->plugin_slug . '-previewer-scripts' ); | |
| 425 | + | |
| 351 | 426 | // when a live preview field is in action we need to know which props need 'px' as defaults |
| 352 | - self::$localized['px_dependent_css_props'] = self::$pixel_dependent_css_properties; | |
| 427 | + $this->localized['px_dependent_css_props'] = self::$pixel_dependent_css_properties; | |
| 353 | 428 | |
| 354 | - wp_localize_script( $this->plugin_slug . '-previewer-scripts', 'customify_settings', self::$localized ); | |
| 429 | + wp_localize_script( $this->plugin_slug . '-previewer-scripts', 'customify_settings', $this->localized ); | |
| 355 | 430 | } |
| 356 | 431 | |
| 357 | 432 | /** |
| 358 | 433 | * Add dynamic style only on the previewer page |
| @@ -368,9 +443,9 @@ | ||
| 368 | 443 | } |
| 369 | 444 | |
| 370 | 445 | $screen = get_current_screen(); |
| 371 | 446 | if ( $screen->id == $this->plugin_screen_hook_suffix ) { |
| 372 | - wp_enqueue_style( $this->plugin_slug . '-admin-styles', plugins_url( 'css/admin.css', __FILE__ ), array(), $this->version ); | |
| 447 | + wp_enqueue_style( $this->plugin_slug . '-admin-styles', plugins_url( 'css/admin.css', $this->file ), array(), $this->_version ); | |
| 373 | 448 | } |
| 374 | 449 | } |
| 375 | 450 | |
| 376 | 451 | /** |
| @@ -383,25 +458,74 @@ | ||
| 383 | 458 | } |
| 384 | 459 | |
| 385 | 460 | $screen = get_current_screen(); |
| 386 | 461 | if ( $screen->id == $this->plugin_screen_hook_suffix ) { |
| 387 | - wp_enqueue_script( $this->plugin_slug . '-admin-script', plugins_url( 'js/admin.js', __FILE__ ), array( 'jquery' ), $this->version ); | |
| 388 | - wp_localize_script( $this->plugin_slug . '-admin-script', 'locals', array( | |
| 389 | - 'ajax_url' => admin_url( 'admin-ajax.php' ) | |
| 462 | + wp_enqueue_script( $this->plugin_slug . '-admin-script', plugins_url( 'js/admin.js', $this->file ), array( 'jquery' ), $this->_version ); | |
| 463 | + wp_localize_script( $this->plugin_slug . '-admin-script', 'customify_settings', array( | |
| 464 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 465 | + 'wp_rest' => array( | |
| 466 | + 'root' => esc_url_raw( rest_url() ), | |
| 467 | + 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 468 | + 'customify_settings_nonce' => wp_create_nonce( 'customify_settings_nonce' ) | |
| 469 | + ), | |
| 390 | 470 | ) ); |
| 391 | 471 | } |
| 392 | 472 | |
| 473 | + wp_localize_script( $this->plugin_slug . '-customizer-scripts', 'WP_API_Settings', array( | |
| 474 | + 'root' => esc_url_raw( rest_url() ), | |
| 475 | + 'nonce' => wp_create_nonce( 'wp_rest' ) | |
| 476 | + ) ); | |
| 477 | + } | |
| 393 | 478 | |
| 394 | - wp_localize_script( $this->plugin_slug . '-customizer-scripts', 'WP_API_Settings', array( 'root' => esc_url_raw( rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ) ) ); | |
| 479 | + function add_rest_routes_api(){ | |
| 480 | + register_rest_route( 'customify/v1', '/delete_theme_mod', array( | |
| 481 | + 'methods' => 'POST', | |
| 482 | + 'callback' => array( $this, 'delete_theme_mod' ), | |
| 483 | + 'permission_callback' => array( $this, 'permission_nonce_callback' ), | |
| 484 | + ) ); | |
| 395 | 485 | } |
| 396 | 486 | |
| 487 | + function delete_theme_mod(){ | |
| 488 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 489 | + wp_send_json_error('You don\'t have admin privileges.'); | |
| 490 | + } | |
| 491 | + | |
| 492 | + $config = apply_filters('customify_filter_fields', array() ); | |
| 493 | + | |
| 494 | + if ( empty( $config['opt-name'] ) ) { | |
| 495 | + wp_send_json_error('no option key'); | |
| 496 | + } | |
| 497 | + | |
| 498 | + $key = $config['opt-name']; | |
| 499 | + | |
| 500 | + remove_theme_mod( $key ); | |
| 501 | + | |
| 502 | + wp_send_json_success('Deleted ' . $key . ' theme mod!'); | |
| 503 | + } | |
| 504 | + | |
| 505 | + function permission_nonce_callback() { | |
| 506 | + return wp_verify_nonce( $this->get_nonce(), 'customify_settings_nonce' ); | |
| 507 | + } | |
| 508 | + | |
| 509 | + private function get_nonce() { | |
| 510 | + $nonce = null; | |
| 511 | + | |
| 512 | + if ( isset( $_REQUEST['customify_settings_nonce'] ) ) { | |
| 513 | + $nonce = wp_unslash( $_REQUEST['customify_settings_nonce'] ); | |
| 514 | + } elseif ( isset( $_POST['customify_settings_nonce'] ) ) { | |
| 515 | + $nonce = wp_unslash( $_POST['customify_settings_nonce'] ); | |
| 516 | + } | |
| 517 | + | |
| 518 | + return $nonce; | |
| 519 | + } | |
| 520 | + | |
| 397 | 521 | /** |
| 398 | 522 | * Public style generated by customizer |
| 399 | 523 | */ |
| 400 | 524 | function output_dynamic_style() { |
| 401 | - $custom_css = "\n"; | |
| 525 | + $custom_css = ''; | |
| 402 | 526 | |
| 403 | - foreach ( self::$options_list as $option_id => $option ) { | |
| 527 | + foreach ( $this->options_list as $option_id => $option ) { | |
| 404 | 528 | |
| 405 | 529 | if ( isset( $option['css'] ) && ! empty( $option['css'] ) ) { |
| 406 | 530 | // now process each |
| 407 | 531 | $custom_css .= $this->convert_setting_to_css( $option_id, $option['css'] ); |
| @@ -406,43 +530,39 @@ | ||
| 406 | 530 | // now process each |
| 407 | 531 | $custom_css .= $this->convert_setting_to_css( $option_id, $option['css'] ); |
| 408 | 532 | } |
| 409 | 533 | |
| 410 | - if ( $option['type'] === 'custom_background' ) { | |
| 411 | - $custom_css .= $this->process_custom_background_field_output( $option_id, $option ); | |
| 534 | + if ( isset( $option['type'] ) && $option['type'] === 'custom_background' ) { | |
| 535 | + $option['value'] = $this->get_option( $option_id ); | |
| 536 | + $custom_css .= $this->process_custom_background_field_output( $option_id, $option ) . PHP_EOL; | |
| 412 | 537 | } |
| 413 | 538 | } |
| 414 | 539 | |
| 415 | - if ( ! empty( self::$media_queries ) ) { | |
| 540 | + if ( ! empty( $this->media_queries ) ) { | |
| 416 | 541 | |
| 417 | - foreach ( self::$media_queries as $media_query => $properties ) { | |
| 542 | + foreach ( $this->media_queries as $media_query => $properties ) { | |
| 418 | 543 | |
| 419 | 544 | if ( empty( $properties ) ) { |
| 420 | 545 | continue; |
| 421 | 546 | } |
| 422 | 547 | |
| 423 | - $custom_css .= '@media ' . $media_query . " {\n"; | |
| 548 | + $custom_css .= PHP_EOL . '@media ' . $media_query . " { " . PHP_EOL . PHP_EOL; | |
| 424 | 549 | |
| 425 | 550 | foreach ( $properties as $key => $property ) { |
| 426 | 551 | $property_settings = $property['property']; |
| 427 | 552 | $property_value = $property['value']; |
| 428 | - $custom_css .= "\t" . self::proccess_css_property( $property_settings, $property_value ); | |
| 553 | + $custom_css .= "\t" . $this->proccess_css_property( $property_settings, $property_value ) . PHP_EOL; | |
| 429 | 554 | } |
| 430 | 555 | |
| 431 | - $custom_css .= "\n}\n"; | |
| 556 | + $custom_css .= "}" . PHP_EOL; | |
| 432 | 557 | |
| 433 | 558 | } |
| 434 | 559 | } |
| 435 | - | |
| 436 | - $custom_css .= "\n"; | |
| 437 | - | |
| 438 | - //@todo maybe add a filter to this output ?> | |
| 560 | + ?> | |
| 439 | 561 | <style id="customify_output_style"> |
| 440 | - <?php echo( $custom_css ); ?> | |
| 441 | - </style> | |
| 442 | - <?php | |
| 562 | + <?php echo apply_filters( 'customify_dynamic_style', $custom_css ); ?> | |
| 563 | + </style><?php | |
| 443 | 564 | |
| 444 | - | |
| 445 | 565 | /** |
| 446 | 566 | * from now on we output only style tags only for the preview purpose |
| 447 | 567 | * so don't cry if you see 30+ style tags for each section |
| 448 | 568 | */ |
| @@ -449,15 +569,15 @@ | ||
| 449 | 569 | if ( ! isset( $GLOBALS['wp_customize'] ) ) { |
| 450 | 570 | return; |
| 451 | 571 | } |
| 452 | 572 | |
| 453 | - foreach ( self::$options_list as $option_id => $options ) { | |
| 573 | + foreach ( $this->options_list as $option_id => $options ) { | |
| 454 | 574 | |
| 455 | - if ( $options['type'] === 'custom_background' ) { | |
| 456 | - $options['value'] = self::get_option( $option_id ); | |
| 575 | + if ( isset( $options['type'] ) && $options['type'] === 'custom_background' ) { | |
| 576 | + $options['value'] = $this->get_option( $option_id ); | |
| 457 | 577 | $custom_background_output = $this->process_custom_background_field_output( $option_id, $options ); ?> |
| 458 | 578 | |
| 459 | - <style id="custom_backgorund_output_for_<?php echo $option_id; ?>"> | |
| 579 | + <style id="custom_background_output_for_<?php echo sanitize_html_class( $option_id ); ?>"> | |
| 460 | 580 | <?php |
| 461 | 581 | if ( isset( $custom_background_output ) && ! empty( $custom_background_output )) { |
| 462 | 582 | echo $custom_background_output; |
| 463 | 583 | } ?> |
| @@ -467,71 +587,43 @@ | ||
| 467 | 587 | if ( ! isset( $options['live'] ) || $options['live'] !== true ) { |
| 468 | 588 | continue; |
| 469 | 589 | } |
| 470 | 590 | |
| 471 | - $this_value = self::get_option( $option_id ); | |
| 472 | - foreach ( $options['css'] as $key => $properties_set ) { ?> | |
| 473 | - <style id="dynamic_setting_<?php echo $option_id . '_property_' . str_replace( '-', '_', $properties_set['property'] ); ?>" type="text/css"><?php | |
| 591 | + $this_value = $this->get_option( $option_id ); | |
| 592 | + if ( ! empty( $options['css'] ) ) { | |
| 593 | + foreach ( $options['css'] as $key => $properties_set ) { | |
| 594 | + // We need to use a class because we may have multiple <style>s with the same "ID" for example | |
| 595 | + // when targeting the same property but with different selectors. | |
| 596 | + ?> | |
| 597 | + <style class="dynamic_setting_<?php echo sanitize_html_class( $option_id ) . '_property_' . str_replace( '-', '_', $properties_set['property'] ) . '_' . $key; ?>" | |
| 598 | + type="text/css"><?php | |
| 474 | 599 | |
| 475 | 600 | if ( isset( $properties_set['media'] ) && ! empty( $properties_set['media'] ) ) { |
| 476 | - echo '@media '. $properties_set['media'] . " {\n"; | |
| 601 | + echo '@media '. $properties_set['media'] . " {" . PHP_EOL; | |
| 477 | 602 | } |
| 478 | 603 | |
| 479 | 604 | if ( isset( $properties_set['selector'] ) && isset( $properties_set['property'] ) ) { |
| 480 | - echo self::proccess_css_property($properties_set, $this_value); | |
| 605 | + echo $this->proccess_css_property($properties_set, $this_value) . PHP_EOL; | |
| 481 | 606 | } |
| 482 | 607 | |
| 483 | 608 | if ( isset( $properties_set['media'] ) && ! empty( $properties_set['media'] ) ) { |
| 484 | - echo "}\n"; | |
| 609 | + echo "}" . PHP_EOL; | |
| 485 | 610 | } ?> |
| 486 | - </style> | |
| 487 | - <?php } | |
| 488 | - } | |
| 489 | - | |
| 490 | - if ( ! empty( self::$media_queries ) ) { | |
| 491 | - | |
| 492 | - foreach ( self::$media_queries as $media_query => $properties ) { | |
| 493 | - | |
| 494 | - if ( empty( $properties ) ) { | |
| 495 | - continue; | |
| 496 | - } | |
| 497 | - | |
| 498 | - $display = false; | |
| 499 | - $media_q = '@media ' . $media_query . " {\n"; | |
| 500 | - | |
| 501 | - foreach ( $properties as $key => $property ) { | |
| 502 | - | |
| 503 | - if ( ! isset( $options['live'] ) || $options['live'] !== true ) { | |
| 504 | - continue; | |
| 505 | - } | |
| 506 | - | |
| 507 | - $display = true; ?> | |
| 508 | - <style id="dynamic_setting_<?php echo $key; ?>" type="text/css"><?php | |
| 509 | - $property_settings = $property['property']; | |
| 510 | - $property_value = $property['value']; | |
| 511 | - $media_q .= "\t" . self::proccess_css_property( $property_settings, $property_value );?> | |
| 512 | 611 | </style> |
| 513 | 612 | <?php } |
| 514 | - | |
| 515 | - $media_q .= "\n}\n"; | |
| 516 | - | |
| 517 | - if ( $display ) { | |
| 518 | - $custom_css .= $media_q; | |
| 519 | - } | |
| 520 | 613 | } |
| 521 | 614 | } |
| 522 | 615 | } |
| 523 | 616 | |
| 524 | 617 | protected function load_google_fonts() { |
| 618 | + $fonts_path = plugin_dir_path( $this->file ) . 'features/customizer/controls/resources/google.fonts.php'; | |
| 525 | 619 | |
| 526 | - $fonts_path = plugin_dir_path( __FILE__ ) . 'features/customizer/controls/resources/google.fonts.php'; | |
| 527 | - | |
| 528 | 620 | if ( file_exists( $fonts_path ) ) { |
| 529 | - self::$google_fonts = require( $fonts_path ); | |
| 621 | + $this->google_fonts = require( $fonts_path ); | |
| 530 | 622 | } |
| 531 | 623 | |
| 532 | - if ( ! empty( self::$google_fonts ) ) { | |
| 533 | - return self::$google_fonts; | |
| 624 | + if ( ! empty( $this->google_fonts ) ) { | |
| 625 | + return $this->google_fonts; | |
| 534 | 626 | } |
| 535 | 627 | |
| 536 | 628 | return false; |
| 537 | 629 | } |
| @@ -536,18 +628,17 @@ | ||
| 536 | 628 | return false; |
| 537 | 629 | } |
| 538 | 630 | |
| 539 | 631 | function output_typography_dynamic_style() { |
| 632 | + $this->get_typography_fields( $this->options_list, 'type', 'typography', $this->typo_settings ); | |
| 540 | 633 | |
| 541 | - self::get_typography_fields( self::$options_list, 'type', 'typography', self::$typo_settings ); | |
| 542 | - | |
| 543 | - if ( empty( self::$typo_settings ) ) { | |
| 634 | + if ( empty( $this->typo_settings ) ) { | |
| 544 | 635 | return; |
| 545 | 636 | } |
| 546 | 637 | |
| 547 | 638 | $families = ''; |
| 548 | 639 | |
| 549 | - foreach ( self::$typo_settings as $id => $font ) { | |
| 640 | + foreach ( $this->typo_settings as $id => $font ) { | |
| 550 | 641 | if ( isset ( $font['value'] ) ) { |
| 551 | 642 | |
| 552 | 643 | $load_all_weights = false; |
| 553 | 644 | if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) { |
| @@ -554,30 +645,39 @@ | ||
| 554 | 645 | $load_all_weights = true; |
| 555 | 646 | } |
| 556 | 647 | |
| 557 | 648 | // shim the time when this was an array |
| 649 | + // @todo Is this really needed? Or does it make sense? | |
| 558 | 650 | if ( is_array( $font['value'] ) ) { |
| 559 | 651 | $font['value'] = stripslashes_deep( $font['value'] ); |
| 560 | 652 | $font['value'] = json_encode( $font['value'] ); |
| 561 | 653 | } |
| 562 | 654 | |
| 563 | - $value = json_decode( wp_unslash( PixCustomifyPlugin::decodeURIComponent( $font['value'] ) ), true ); | |
| 655 | + $value = wp_unslash( PixCustomifyPlugin::decodeURIComponent( $font['value'] ) ); | |
| 656 | + if ( is_string( $value ) ) { | |
| 657 | + $value = json_decode( $value, true ); | |
| 658 | + } | |
| 564 | 659 | |
| 565 | - // in case the value is still null, try default value(mostly for google fonts) | |
| 566 | - if ( ! is_array( $value ) || $value === null ) { | |
| 660 | + // In case the value is still null, try default value (mostly for google fonts) | |
| 661 | + if ( $value === null || ! is_array( $value ) ) { | |
| 567 | 662 | $value = $this->get_font_defaults_value( str_replace( '"', '', $font['value'] ) ); |
| 568 | 663 | } |
| 569 | 664 | |
| 570 | - //bail if by this time we don't have a value of some sort | |
| 665 | + // Bail if by this time we don't have a value of some sort | |
| 571 | 666 | if ( empty( $value ) ) { |
| 572 | 667 | continue; |
| 573 | 668 | } |
| 574 | 669 | |
| 575 | - //Handle special logic for when the $value array is not an associative array | |
| 576 | - if ( ! self::is_assoc( $value ) ) { | |
| 577 | - $value = $this->process_a_not_associative_font_default( $value ); | |
| 670 | + // Handle special logic for when the $value array is not an associative array | |
| 671 | + if ( ! $this->is_assoc( $value ) ) { | |
| 672 | + $value = $this->standardize_non_associative_font_default( $value ); | |
| 578 | 673 | } |
| 579 | 674 | |
| 675 | + // Bail if empty or we don't have an array | |
| 676 | + if ( empty( $value ) || ! is_array( $value ) ) { | |
| 677 | + continue; | |
| 678 | + } | |
| 679 | + | |
| 580 | 680 | if ( isset( $value['font_family'] ) && isset( $value['type'] ) && $value['type'] == 'google' ) { |
| 581 | 681 | $families .= "'" . $value['font_family']; |
| 582 | 682 | |
| 583 | 683 | if ( $load_all_weights && is_array( $value['variants'] ) ) { |
| @@ -614,38 +714,38 @@ | ||
| 614 | 714 | } |
| 615 | 715 | } |
| 616 | 716 | } |
| 617 | 717 | |
| 618 | - if ( ! empty ( $families ) && self::get_plugin_option( 'typography', '1' ) && self::get_plugin_option( 'typography_google_fonts', 1 ) ) { ?> | |
| 718 | + if ( ! empty ( $families ) && $this->get_plugin_setting( 'typography', '1' ) && $this->get_plugin_setting( 'typography_google_fonts', 1 ) ) { ?> | |
| 619 | 719 | <script type="text/javascript"> |
| 620 | - if ( typeof WebFont !== 'undefined' ) {<?php // if there is a WebFont object, use it ?> | |
| 621 | - WebFont.load( { | |
| 622 | - google: {families: [<?php echo( rtrim( $families, ',' ) ); ?>]}, | |
| 623 | - classes: false, | |
| 624 | - events: false | |
| 625 | - } ); | |
| 626 | - } else {<?php // basically when we don't have the WebFont object we create the google script dynamically ?> | |
| 720 | + if (typeof WebFont !== 'undefined') {<?php // if there is a WebFont object, use it ?> | |
| 721 | + WebFont.load({ | |
| 722 | + google: {families: [<?php echo( rtrim( $families, ',' ) ); ?>]}, | |
| 723 | + classes: false, | |
| 724 | + events: false | |
| 725 | + }); | |
| 726 | + } else {<?php // basically when we don't have the WebFont object we create the google script dynamically ?> | |
| 627 | 727 | |
| 628 | - var tk = document.createElement( 'script' ); | |
| 629 | - tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'; | |
| 630 | - tk.type = 'text/javascript'; | |
| 728 | + var tk = document.createElement('script'); | |
| 729 | + tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'; | |
| 730 | + tk.type = 'text/javascript'; | |
| 631 | 731 | |
| 632 | - tk.onload = tk.onreadystatechange = function() { | |
| 633 | - WebFont.load( { | |
| 634 | - google: {families: [<?php echo( rtrim( $families, ',' ) ); ?>]}, | |
| 635 | - classes: false, | |
| 636 | - events: false | |
| 637 | - } ); | |
| 638 | - }; | |
| 732 | + tk.onload = tk.onreadystatechange = function () { | |
| 733 | + WebFont.load({ | |
| 734 | + google: {families: [<?php echo( rtrim( $families, ',' ) ); ?>]}, | |
| 735 | + classes: false, | |
| 736 | + events: false | |
| 737 | + }); | |
| 738 | + }; | |
| 639 | 739 | |
| 640 | - var s = document.getElementsByTagName( 'script' )[0]; | |
| 641 | - s.parentNode.insertBefore( tk, s ); | |
| 642 | - } | |
| 740 | + var s = document.getElementsByTagName('script')[0]; | |
| 741 | + s.parentNode.insertBefore(tk, s); | |
| 742 | + } | |
| 643 | 743 | </script> |
| 644 | 744 | <?php } ?> |
| 645 | 745 | <style id="customify_typography_output_style"> |
| 646 | 746 | <?php |
| 647 | - foreach ( self::$typo_settings as $key => $font ) { | |
| 747 | + foreach ( $this->typo_settings as $key => $font ) { | |
| 648 | 748 | $load_all_weights = false; |
| 649 | 749 | if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) { |
| 650 | 750 | $load_all_weights = true; |
| 651 | 751 | } |
| @@ -650,25 +750,35 @@ | ||
| 650 | 750 | $load_all_weights = true; |
| 651 | 751 | } |
| 652 | 752 | |
| 653 | 753 | if ( isset( $font['selector'] ) && isset( $font['value'] ) && ! empty( $font['value'] ) ) { |
| 754 | + // Make sure that the value is in the proper format | |
| 755 | + $value = PixCustomifyPlugin::decodeURIComponent( $font['value'] ); | |
| 756 | + if ( is_string( $value ) ) { | |
| 757 | + $value = json_decode( $value, true ); | |
| 758 | + } | |
| 654 | 759 | |
| 655 | - $value = json_decode( PixCustomifyPlugin::decodeURIComponent( $font['value'] ), true ); | |
| 656 | - // in case the value is still null, try default value(mostly for google fonts) | |
| 760 | + // In case the value is null (most probably because the json_decode failed), | |
| 761 | + // try the default value (mostly for google fonts) | |
| 657 | 762 | if ( $value === null ) { |
| 658 | 763 | $value = $this->get_font_defaults_value( $font['value'] ); |
| 659 | 764 | } |
| 660 | 765 | |
| 661 | - // shim the old case when the default was only the font name | |
| 662 | - if ( is_string( $value ) && ! empty( $value ) ) { | |
| 766 | + // Shim the old case when the default was only the font name | |
| 767 | + if ( ! empty( $value ) && is_string( $value ) ) { | |
| 663 | 768 | $value = array( 'font_family' => $value ); |
| 664 | 769 | } |
| 665 | 770 | |
| 666 | - //Handle special logic for when the $value array is not an associative array | |
| 667 | - if ( ! self::is_assoc( $value ) ) { | |
| 668 | - $value = $this->process_a_not_associative_font_default( $value ); | |
| 771 | + // Handle special logic for when the $value array is not an associative array | |
| 772 | + if ( ! $this->is_assoc( $value ) ) { | |
| 773 | + $value = $this->standardize_non_associative_font_default( $value ); | |
| 669 | 774 | } |
| 670 | 775 | |
| 776 | + // Bail if empty or we don't have an array | |
| 777 | + if ( empty( $value ) || ! is_array( $value ) ) { | |
| 778 | + continue; | |
| 779 | + } | |
| 780 | + | |
| 671 | 781 | $selected_variant = ''; |
| 672 | 782 | if ( ! empty( $value['selected_variants'] ) ) { |
| 673 | 783 | if ( is_array( $value['selected_variants'] ) ) { |
| 674 | 784 | $selected_variant = $value['selected_variants'][0]; |
| @@ -678,13 +788,13 @@ | ||
| 678 | 788 | } |
| 679 | 789 | |
| 680 | 790 | // First handle the case where we have the font-family in the selected variant (usually this means a custom font from our Fonto plugin) |
| 681 | 791 | if ( ! empty( $selected_variant ) && is_array( $selected_variant ) && ! empty( $selected_variant['font-family'] ) ) { |
| 682 | - //the variant's font-family | |
| 792 | + // The variant's font-family | |
| 683 | 793 | echo $font['selector'] . " {\nfont-family: " . $selected_variant['font-family'] . ";\n"; |
| 684 | 794 | |
| 685 | 795 | if ( ! $load_all_weights ) { |
| 686 | - // if this is a custom font (like from our plugin Fonto) with individual styles & weights - i.e. the font-family says it all | |
| 796 | + // If this is a custom font (like from our plugin Fonto) with individual styles & weights - i.e. the font-family says it all | |
| 687 | 797 | // we need to "force" the font-weight and font-style |
| 688 | 798 | if ( ! empty( $value['type'] ) && 'custom_individual' == $value['type'] ) { |
| 689 | 799 | $selected_variant['font-weight'] = '400 !important'; |
| 690 | 800 | $selected_variant['font-style'] = 'normal !important'; |
| @@ -689,14 +799,14 @@ | ||
| 689 | 799 | $selected_variant['font-weight'] = '400 !important'; |
| 690 | 800 | $selected_variant['font-style'] = 'normal !important'; |
| 691 | 801 | } |
| 692 | 802 | |
| 693 | - // output the font weight, if available | |
| 803 | + // Output the font weight, if available | |
| 694 | 804 | if ( ! empty( $selected_variant['font-weight'] ) ) { |
| 695 | 805 | echo "font-weight: " . $selected_variant['font-weight'] . ";\n"; |
| 696 | 806 | } |
| 697 | 807 | |
| 698 | - // output the font style, if available | |
| 808 | + // Output the font style, if available | |
| 699 | 809 | if ( ! empty( $selected_variant['font-style'] ) ) { |
| 700 | 810 | echo "font-style: " . $selected_variant['font-style'] . ";\n"; |
| 701 | 811 | } |
| 702 | 812 | } |
| @@ -702,9 +812,9 @@ | ||
| 702 | 812 | } |
| 703 | 813 | |
| 704 | 814 | echo "}\n"; |
| 705 | 815 | } elseif ( isset( $value['font_family'] ) ) { |
| 706 | - // the selected font family | |
| 816 | + // The selected font family | |
| 707 | 817 | echo $font['selector'] . " {\n font-family: " . $value['font_family'] . ";\n"; |
| 708 | 818 | |
| 709 | 819 | if ( ! empty( $selected_variant ) && ! $load_all_weights ) { |
| 710 | 820 | $weight_and_style = strtolower( $selected_variant ); |
| @@ -740,14 +850,18 @@ | ||
| 740 | 850 | /** |
| 741 | 851 | * Handle special logic for when the $value array is not an associative array |
| 742 | 852 | * Return a new associative array with proper keys |
| 743 | 853 | */ |
| 744 | - protected function process_a_not_associative_font_default( $value ) { | |
| 854 | + public function standardize_non_associative_font_default( $value ) { | |
| 855 | + // If the value provided is not array, simply return it | |
| 856 | + if ( ! is_array( $value ) ) { | |
| 857 | + return $value; | |
| 858 | + } | |
| 745 | 859 | |
| 746 | 860 | $new_value = array(); |
| 747 | 861 | |
| 748 | - //Let's determine some type of font | |
| 749 | - if ( ! isset( $value[2] ) || ( isset( $value[2] ) && 'google' == $value[2] ) ) { | |
| 862 | + // Let's determine some type of font | |
| 863 | + if ( ! isset( $value[2] ) || 'google' == $value[2] ) { | |
| 750 | 864 | $new_value = $this->get_font_defaults_value( $value[0] ); |
| 751 | 865 | } else { |
| 752 | 866 | $new_value['type'] = $value[2]; |
| 753 | 867 | } |
| @@ -755,15 +869,15 @@ | ||
| 755 | 869 | if ( null == $new_value ) { |
| 756 | 870 | $new_value = array(); |
| 757 | 871 | } |
| 758 | 872 | |
| 759 | - //The first entry is the font-family | |
| 873 | + // The first entry is the font-family | |
| 760 | 874 | if ( isset( $value[0] ) ) { |
| 761 | 875 | $new_value['font_family'] = $value[0]; |
| 762 | 876 | } |
| 763 | 877 | |
| 764 | - //In case we don't have an associative array | |
| 765 | - //The second entry is the variants | |
| 878 | + // In case we don't have an associative array | |
| 879 | + // The second entry is the variants | |
| 766 | 880 | if ( isset( $value[1] ) ) { |
| 767 | 881 | $new_value['selected_variants'] = $value[1]; |
| 768 | 882 | } |
| 769 | 883 | |
| @@ -775,24 +889,26 @@ | ||
| 775 | 889 | * @param $font_name |
| 776 | 890 | * |
| 777 | 891 | * @return null |
| 778 | 892 | */ |
| 779 | - protected function get_font_defaults_value( $font_name ) { | |
| 893 | + public function get_font_defaults_value( $font_name ) { | |
| 780 | 894 | |
| 781 | - if ( empty( self::$google_fonts ) ) { | |
| 895 | + if ( empty( $this->google_fonts ) ) { | |
| 782 | 896 | $this->load_google_fonts(); |
| 783 | 897 | } |
| 784 | 898 | |
| 785 | - if ( isset( self::$google_fonts[ $font_name ] ) ) { | |
| 786 | - $value = self::$google_fonts[ $font_name ]; | |
| 899 | + if ( isset( $this->google_fonts[ $font_name ] ) ) { | |
| 900 | + $value = $this->google_fonts[ $font_name ]; | |
| 787 | 901 | $value['font_family'] = $font_name; |
| 788 | 902 | $value['type'] = 'google'; |
| 903 | + | |
| 789 | 904 | return $value; |
| 790 | - } elseif ( isset( self::$theme_fonts[ $font_name ] ) ) { | |
| 791 | - $value['type'] = 'theme_font'; | |
| 792 | - $value['src'] = self::$theme_fonts[ $font_name ]['src']; | |
| 793 | - $value['variants'] = self::$theme_fonts[ $font_name ]['variants']; | |
| 794 | - $value['font_family'] = self::$theme_fonts[ $font_name ]['family']; | |
| 905 | + } elseif ( isset( $this->theme_fonts[ $font_name ] ) ) { | |
| 906 | + $value['type'] = 'theme_font'; | |
| 907 | + $value['src'] = $this->theme_fonts[ $font_name ]['src']; | |
| 908 | + $value['variants'] = $this->theme_fonts[ $font_name ]['variants']; | |
| 909 | + $value['font_family'] = $this->theme_fonts[ $font_name ]['family']; | |
| 910 | + | |
| 795 | 911 | return $value; |
| 796 | 912 | } |
| 797 | 913 | |
| 798 | 914 | return null; |
| @@ -808,9 +924,9 @@ | ||
| 808 | 924 | */ |
| 809 | 925 | protected function convert_setting_to_css( $option_id, $css_config = array() ) { |
| 810 | 926 | $output = ''; |
| 811 | 927 | |
| 812 | - $this_value = self::get_option( $option_id ); | |
| 928 | + $this_value = $this->get_option( $option_id ); | |
| 813 | 929 | |
| 814 | 930 | if ( empty( $css_config ) ) { |
| 815 | 931 | return $output; |
| 816 | 932 | } |
| @@ -817,9 +933,9 @@ | ||
| 817 | 933 | |
| 818 | 934 | foreach ( $css_config as $css_property ) { |
| 819 | 935 | |
| 820 | 936 | if ( isset( $css_property['media'] ) && ! empty( $css_property['media'] ) ) { |
| 821 | - self::$media_queries[ $css_property['media'] ][ $option_id ] = array( | |
| 937 | + $this->media_queries[ $css_property['media'] ][ $option_id ] = array( | |
| 822 | 938 | 'property' => $css_property, |
| 823 | 939 | 'value' => $this_value |
| 824 | 940 | ); |
| 825 | 941 | continue; |
| @@ -824,10 +940,10 @@ | ||
| 824 | 940 | ); |
| 825 | 941 | continue; |
| 826 | 942 | } |
| 827 | 943 | |
| 828 | - if ( ! isset( $css_property['selector'] ) || isset( $css_property['property'] ) ) { | |
| 829 | - $output .= self::proccess_css_property( $css_property, $this_value ); | |
| 944 | + if ( isset( $css_property['selector'] ) && isset( $css_property['property'] ) ) { | |
| 945 | + $output .= $this->proccess_css_property( $css_property, $this_value ) . PHP_EOL; | |
| 830 | 946 | } |
| 831 | 947 | } |
| 832 | 948 | |
| 833 | 949 | return $output; |
| @@ -843,12 +959,20 @@ | ||
| 843 | 959 | // if the unit isn't specified but the property should have a unit force 'px' as it |
| 844 | 960 | if ( empty( $unit ) && in_array( $css_property['property'], self::$pixel_dependent_css_properties ) ) { |
| 845 | 961 | $unit = 'px'; |
| 846 | 962 | } |
| 963 | + // lose the tons of tabs | |
| 964 | + $css_property['selector'] = trim( preg_replace( '/\t+/', '', $css_property['selector'] ) ); | |
| 847 | 965 | |
| 848 | - $this_property_output = $css_property['selector'] . ' { ' . $css_property['property'] . ': ' . $this_value . $unit . "; } \n"; | |
| 966 | + $this_property_output = $css_property['selector'] . ' { ' . $css_property['property'] . ': ' . $this_value . $unit . "; }" . PHP_EOL; | |
| 849 | 967 | |
| 850 | - if ( isset( $css_property['callback_filter'] ) && function_exists( $css_property['callback_filter'] ) ) { | |
| 968 | + // Handle the value filter callback. | |
| 969 | + if ( isset( $css_property['filter_value_cb'] ) ) { | |
| 970 | + $this_value = $this->maybe_apply_filter( $css_property['filter_value_cb'], $this_value ); | |
| 971 | + } | |
| 972 | + | |
| 973 | + // Handle output callback. | |
| 974 | + if ( isset( $css_property['callback_filter'] ) && is_callable( $css_property['callback_filter'] ) ) { | |
| 851 | 975 | $this_property_output = call_user_func( $css_property['callback_filter'], $this_value, $css_property['selector'], $css_property['property'], $unit ); |
| 852 | 976 | } |
| 853 | 977 | |
| 854 | 978 | return $this_property_output; |
| @@ -853,15 +977,60 @@ | ||
| 853 | 977 | |
| 854 | 978 | return $this_property_output; |
| 855 | 979 | } |
| 856 | 980 | |
| 981 | + /** | |
| 982 | + * Apply a filter (config) to a value. | |
| 983 | + * | |
| 984 | + * We currently handle filters like these: | |
| 985 | + * // Elaborate filter config | |
| 986 | + * array( | |
| 987 | + * 'callback' => 'is_post_type_archive', | |
| 988 | + * // The arguments we should pass to the check function. | |
| 989 | + * // Think post types, taxonomies, or nothing if that is the case. | |
| 990 | + * // It can be an array of values or a single value. | |
| 991 | + * 'args' => array( | |
| 992 | + * 'jetpack-portfolio', | |
| 993 | + * ), | |
| 994 | + * ), | |
| 995 | + * // Simple filter - just the function name | |
| 996 | + * 'is_404', | |
| 997 | + * | |
| 998 | + * @param array|string $filter | |
| 999 | + * @param mixed $value The value to apply the filter to. | |
| 1000 | + * | |
| 1001 | + * @return mixed The filtered value. | |
| 1002 | + */ | |
| 1003 | + public function maybe_apply_filter( $filter, $value ) { | |
| 1004 | + // Let's get some obvious things off the table. | |
| 1005 | + // On invalid data, we just return what we've received. | |
| 1006 | + if ( empty( $filter ) ) { | |
| 1007 | + return $value; | |
| 1008 | + } | |
| 1009 | + | |
| 1010 | + // First, we handle the shorthand version: just a function name | |
| 1011 | + if ( is_string( $filter ) && is_callable( $filter ) ) { | |
| 1012 | + $value = call_user_func( $filter ); | |
| 1013 | + } elseif ( is_array( $filter ) && ! empty( $filter['callback'] ) && is_callable( $filter['callback'] ) ) { | |
| 1014 | + if ( empty( $filter['args'] ) ) { | |
| 1015 | + $filter['args'] = array(); | |
| 1016 | + } | |
| 1017 | + // The value is always the first argument. | |
| 1018 | + $filter['args'] = array( $value ) + $filter['args']; | |
| 1019 | + | |
| 1020 | + $value = call_user_func_array( $filter['callback'], $filter['args'] ); | |
| 1021 | + } | |
| 1022 | + | |
| 1023 | + return $value; | |
| 1024 | + } | |
| 1025 | + | |
| 857 | 1026 | protected function process_custom_background_field_output( $option_id, $options ) { |
| 858 | - $selector = ''; | |
| 1027 | + $selector = $output = ''; | |
| 859 | 1028 | |
| 860 | 1029 | if ( ! isset( $options['value'] ) ) { |
| 861 | 1030 | return false; |
| 862 | 1031 | } |
| 863 | - $value = $options['value']; | |
| 1032 | + $value = $options['value']; | |
| 864 | 1033 | |
| 865 | 1034 | if ( ! isset( $options['output'] ) ) { |
| 866 | 1035 | return $selector; |
| 867 | 1036 | } elseif ( is_string( $options['output'] ) ) { |
| @@ -868,35 +1037,35 @@ | ||
| 868 | 1037 | $selector = $options['output']; |
| 869 | 1038 | } elseif ( is_array( $options['output'] ) ) { |
| 870 | 1039 | $selector = implode( ' ', $options['output'] ); |
| 871 | 1040 | } |
| 872 | - ob_start(); | |
| 873 | 1041 | |
| 874 | - echo "\n" . $selector . " { \n"; | |
| 1042 | + | |
| 1043 | + $output .= $selector . " {"; | |
| 875 | 1044 | if ( isset( $value['background-image'] ) && ! empty( $value['background-image'] ) ) { |
| 876 | - echo "background-image: url( " . $value['background-image'] . ");\n"; | |
| 1045 | + $output .= "background-image: url( " . $value['background-image'] . ");"; | |
| 877 | 1046 | } else { |
| 878 | - echo "background-image: none;\n"; | |
| 1047 | + $output .= "background-image: none;"; | |
| 879 | 1048 | } |
| 880 | 1049 | |
| 881 | 1050 | if ( isset( $value['background-repeat'] ) && ! empty( $value['background-repeat'] ) ) { |
| 882 | - echo "background-repeat:" . $value['background-repeat'] . ";\n"; | |
| 1051 | + $output .= "background-repeat:" . $value['background-repeat'] . ";"; | |
| 883 | 1052 | } |
| 884 | 1053 | |
| 885 | 1054 | if ( isset( $value['background-position'] ) && ! empty( $value['background-position'] ) ) { |
| 886 | - echo "background-position:" . $value['background-position'] . ";\n"; | |
| 1055 | + $output .= "background-position:" . $value['background-position'] . ";"; | |
| 887 | 1056 | } |
| 888 | 1057 | |
| 889 | 1058 | if ( isset( $value['background-size'] ) && ! empty( $value['background-size'] ) ) { |
| 890 | - echo "background-size:" . $value['background-size'] . ";\n"; | |
| 1059 | + $output .= "background-size:" . $value['background-size'] . ";"; | |
| 891 | 1060 | } |
| 892 | 1061 | |
| 893 | 1062 | if ( isset( $value['background-attachment'] ) && ! empty( $value['background-attachment'] ) ) { |
| 894 | - echo "background-attachment:" . $value['background-attachment'] . ";\n"; | |
| 1063 | + $output .= "background-attachment:" . $value['background-attachment'] . ";"; | |
| 895 | 1064 | } |
| 896 | - echo "\n}\n"; | |
| 1065 | + $output .= "}\n"; | |
| 897 | 1066 | |
| 898 | - return ob_get_clean(); | |
| 1067 | + return $output; | |
| 899 | 1068 | } |
| 900 | 1069 | |
| 901 | 1070 | /** |
| 902 | 1071 | * add our customizer styling edits into the wp_editor |
| @@ -909,61 +1078,61 @@ | ||
| 909 | 1078 | |
| 910 | 1079 | $custom_css = ob_get_clean(); ?> |
| 911 | 1080 | <script type="text/javascript"> |
| 912 | 1081 | /* <![CDATA[ */ |
| 913 | - (function( $ ) { | |
| 914 | - $( window ).load( function() { | |
| 915 | - /** | |
| 916 | - * @param iframe_id the id of the frame you whant to append the style | |
| 917 | - * @param style_element the style element you want to append | |
| 918 | - */ | |
| 919 | - var append_script_to_iframe = function( ifrm_id, scriptEl ) { | |
| 920 | - var myIframe = document.getElementById( ifrm_id ); | |
| 1082 | + (function ($) { | |
| 1083 | + $(window).load(function () { | |
| 1084 | + /** | |
| 1085 | + * @param iframe_id the id of the frame you want to append the style | |
| 1086 | + * @param style_element the style element you want to append | |
| 1087 | + */ | |
| 1088 | + var append_script_to_iframe = function (ifrm_id, scriptEl) { | |
| 1089 | + var myIframe = document.getElementById(ifrm_id); | |
| 921 | 1090 | |
| 922 | - var script = myIframe.contentWindow.document.createElement( "script" ); | |
| 923 | - script.type = "text/javascript"; | |
| 924 | - script.innerHTML = scriptEl.innerHTML; | |
| 1091 | + var script = myIframe.contentWindow.document.createElement("script"); | |
| 1092 | + script.type = "text/javascript"; | |
| 1093 | + script.innerHTML = scriptEl.innerHTML; | |
| 925 | 1094 | |
| 926 | - myIframe.contentWindow.document.head.appendChild( script ); | |
| 927 | - }; | |
| 1095 | + myIframe.contentWindow.document.head.appendChild(script); | |
| 1096 | + }; | |
| 928 | 1097 | |
| 929 | - var append_style_to_iframe = function( ifrm_id, styleElment ) { | |
| 930 | - var ifrm = window.frames[ifrm_id]; | |
| 931 | - ifrm = ( ifrm.contentDocument || ifrm.contentDocument || ifrm.document ); | |
| 932 | - var head = ifrm.getElementsByTagName( 'head' )[0]; | |
| 933 | - | |
| 934 | - if ( typeof styleElment !== "undefined" ) { | |
| 935 | - head.appendChild( styleElment ); | |
| 1098 | + var append_style_to_iframe = function (ifrm_id, styleElment) { | |
| 1099 | + var ifrm = window.frames[ifrm_id]; | |
| 1100 | + if ( typeof ifrm === "undefined" ) { | |
| 1101 | + return; | |
| 936 | 1102 | } |
| 937 | - }; | |
| 1103 | + ifrm = ( ifrm.contentDocument || ifrm.contentDocument || ifrm.document ); | |
| 1104 | + var head = ifrm.getElementsByTagName('head')[0]; | |
| 938 | 1105 | |
| 939 | - var xmlString = <?php echo json_encode( str_replace( "\n", "", $custom_css ) ); ?>, | |
| 940 | - parser = new DOMParser(), | |
| 941 | - doc = parser.parseFromString( xmlString, "text/html" ); | |
| 1106 | + if (typeof styleElment !== "undefined") { | |
| 1107 | + head.appendChild(styleElment); | |
| 1108 | + } | |
| 1109 | + }; | |
| 942 | 1110 | |
| 943 | - if ( typeof window.frames['content_ifr'] !== 'undefined' ) { | |
| 1111 | + var xmlString = <?php echo json_encode( str_replace( "\n", "", $custom_css ) ); ?>, | |
| 1112 | + parser = new DOMParser(), | |
| 1113 | + doc = parser.parseFromString(xmlString, "text/html"); | |
| 944 | 1114 | |
| 945 | - $.each( doc.head.childNodes, function( key, el ) { | |
| 1115 | + if (typeof window.frames['content_ifr'] !== 'undefined') { | |
| 946 | 1116 | |
| 947 | - if ( typeof el !== "undefined" && typeof el.tagName !== "undefined" ) { | |
| 1117 | + $.each(doc.head.childNodes, function (key, el) { | |
| 1118 | + if (typeof el !== "undefined" && typeof el.tagName !== "undefined") { | |
| 948 | 1119 | |
| 949 | - switch ( el.tagName ) { | |
| 950 | - | |
| 951 | - case 'STYLE' : | |
| 952 | - append_style_to_iframe( 'content_ifr', el ); | |
| 953 | - break; | |
| 954 | - | |
| 955 | - case 'SCRIPT' : | |
| 956 | - append_script_to_iframe( 'content_ifr', el ); | |
| 957 | - break; | |
| 958 | - default: | |
| 959 | - break; | |
| 960 | - } | |
| 961 | - } | |
| 962 | - } ); | |
| 963 | - } | |
| 964 | - } ); | |
| 965 | - })( jQuery ); | |
| 1120 | + switch (el.tagName) { | |
| 1121 | + case 'STYLE' : | |
| 1122 | + append_style_to_iframe('content_ifr', el); | |
| 1123 | + break; | |
| 1124 | + case 'SCRIPT' : | |
| 1125 | + append_script_to_iframe('content_ifr', el); | |
| 1126 | + break; | |
| 1127 | + default: | |
| 1128 | + break; | |
| 1129 | + } | |
| 1130 | + } | |
| 1131 | + }); | |
| 1132 | + } | |
| 1133 | + }); | |
| 1134 | + })(jQuery); | |
| 966 | 1135 | /* ]]> */ |
| 967 | 1136 | </script> |
| 968 | 1137 | <?php } |
| 969 | 1138 | |
| @@ -993,25 +1162,198 @@ | ||
| 993 | 1162 | |
| 994 | 1163 | protected function register_customizer_controls() { |
| 995 | 1164 | |
| 996 | 1165 | // first get the base customizer extend class |
| 997 | - require_once( self::get_base_path() . '/features/customizer/class-Pix_Customize_Control.php' ); | |
| 1166 | + require_once( $this->get_base_path() . '/features/customizer/class-Pix_Customize_Control.php' ); | |
| 998 | 1167 | |
| 999 | 1168 | // now get all the controls |
| 1000 | - $path = self::get_base_path() . '/features/customizer/controls/'; | |
| 1169 | + $path = $this->get_base_path() . '/features/customizer/controls/'; | |
| 1001 | 1170 | pixcustomify::require_all( $path ); |
| 1002 | 1171 | } |
| 1003 | 1172 | |
| 1173 | + /** | |
| 1174 | + * Maybe process certain "commands" from the config. | |
| 1175 | + * | |
| 1176 | + * Mainly things like removing sections, controls, etc. | |
| 1177 | + * | |
| 1178 | + * @since 1.9.0 | |
| 1179 | + * | |
| 1180 | + * @param WP_Customize_Manager $wp_customize | |
| 1181 | + */ | |
| 1182 | + public function maybe_process_config_extras( $wp_customize ) { | |
| 1183 | + // Bail if we have no external theme config data. | |
| 1184 | + if ( empty( $this->customizer_config ) || ! is_array( $this->customizer_config ) ) { | |
| 1185 | + return; | |
| 1186 | + } | |
| 1187 | + | |
| 1188 | + // Maybe remove panels | |
| 1189 | + if ( ! empty( $this->customizer_config['remove_panels'] ) ) { | |
| 1190 | + // Standardize it. | |
| 1191 | + if ( is_string( $this->customizer_config['remove_panels'] ) ) { | |
| 1192 | + $this->customizer_config['remove_panels'] = array( $this->customizer_config['remove_panels'] ); | |
| 1193 | + } | |
| 1194 | + | |
| 1195 | + foreach ( $this->customizer_config['remove_panels'] as $panel_id ) { | |
| 1196 | + $wp_customize->remove_panel( $panel_id ); | |
| 1197 | + } | |
| 1198 | + } | |
| 1199 | + | |
| 1200 | + // Maybe change panel props. | |
| 1201 | + if ( ! empty( $this->customizer_config['change_panel_props'] ) ) { | |
| 1202 | + foreach ( $this->customizer_config['change_panel_props'] as $panel_id => $panel_props ) { | |
| 1203 | + if ( ! is_array( $panel_props ) ) { | |
| 1204 | + continue; | |
| 1205 | + } | |
| 1206 | + | |
| 1207 | + $panel = $wp_customize->get_panel( $panel_id ); | |
| 1208 | + if ( empty( $panel ) || ! $panel instanceof WP_Customize_Panel ) { | |
| 1209 | + continue; | |
| 1210 | + } | |
| 1211 | + | |
| 1212 | + $public_props = get_class_vars( get_class( $panel ) ); | |
| 1213 | + foreach ( $panel_props as $prop_name => $prop_value ) { | |
| 1214 | + | |
| 1215 | + if ( ! in_array( $prop_name, array_keys( $public_props ) ) ) { | |
| 1216 | + continue; | |
| 1217 | + } | |
| 1218 | + | |
| 1219 | + $panel->$prop_name = $prop_value; | |
| 1220 | + } | |
| 1221 | + } | |
| 1222 | + } | |
| 1223 | + | |
| 1224 | + // Maybe remove sections | |
| 1225 | + if ( ! empty( $this->customizer_config['remove_sections'] ) ) { | |
| 1226 | + // Standardize it. | |
| 1227 | + if ( is_string( $this->customizer_config['remove_sections'] ) ) { | |
| 1228 | + $this->customizer_config['remove_sections'] = array( $this->customizer_config['remove_sections'] ); | |
| 1229 | + } | |
| 1230 | + | |
| 1231 | + foreach ( $this->customizer_config['remove_sections'] as $section_id ) { | |
| 1232 | + | |
| 1233 | + if ( 'widgets' === $section_id ) { | |
| 1234 | + global $wp_registered_sidebars; | |
| 1235 | + | |
| 1236 | + foreach ( $wp_registered_sidebars as $widget => $settings ) { | |
| 1237 | + $wp_customize->remove_section( 'sidebar-widgets-' . $widget ); | |
| 1238 | + } | |
| 1239 | + continue; | |
| 1240 | + } | |
| 1241 | + | |
| 1242 | + $wp_customize->remove_section( $section_id ); | |
| 1243 | + } | |
| 1244 | + } | |
| 1245 | + | |
| 1246 | + // Maybe change section props. | |
| 1247 | + if ( ! empty( $this->customizer_config['change_section_props'] ) ) { | |
| 1248 | + foreach ( $this->customizer_config['change_section_props'] as $section_id => $section_props ) { | |
| 1249 | + if ( ! is_array( $section_props ) ) { | |
| 1250 | + continue; | |
| 1251 | + } | |
| 1252 | + | |
| 1253 | + $section = $wp_customize->get_section( $section_id ); | |
| 1254 | + if ( empty( $section ) || ! $section instanceof WP_Customize_Section ) { | |
| 1255 | + continue; | |
| 1256 | + } | |
| 1257 | + | |
| 1258 | + $public_props = get_class_vars( get_class( $section ) ); | |
| 1259 | + foreach ( $section_props as $prop_name => $prop_value ) { | |
| 1260 | + | |
| 1261 | + if ( ! in_array( $prop_name, array_keys( $public_props ) ) ) { | |
| 1262 | + continue; | |
| 1263 | + } | |
| 1264 | + | |
| 1265 | + $section->$prop_name = $prop_value; | |
| 1266 | + } | |
| 1267 | + } | |
| 1268 | + } | |
| 1269 | + | |
| 1270 | + // Maybe remove settings | |
| 1271 | + if ( ! empty( $this->customizer_config['remove_settings'] ) ) { | |
| 1272 | + // Standardize it. | |
| 1273 | + if ( is_string( $this->customizer_config['remove_settings'] ) ) { | |
| 1274 | + $this->customizer_config['remove_settings'] = array( $this->customizer_config['remove_settings'] ); | |
| 1275 | + } | |
| 1276 | + | |
| 1277 | + foreach ( $this->customizer_config['remove_settings'] as $setting_id ) { | |
| 1278 | + $wp_customize->remove_setting( $setting_id ); | |
| 1279 | + } | |
| 1280 | + } | |
| 1281 | + | |
| 1282 | + // Maybe change setting props. | |
| 1283 | + if ( ! empty( $this->customizer_config['change_setting_props'] ) ) { | |
| 1284 | + foreach ( $this->customizer_config['change_setting_props'] as $setting_id => $setting_props ) { | |
| 1285 | + if ( ! is_array( $setting_props ) ) { | |
| 1286 | + continue; | |
| 1287 | + } | |
| 1288 | + | |
| 1289 | + $setting = $wp_customize->get_setting( $setting_id ); | |
| 1290 | + if ( empty( $setting ) || ! $setting instanceof WP_Customize_Setting ) { | |
| 1291 | + continue; | |
| 1292 | + } | |
| 1293 | + | |
| 1294 | + $public_props = get_class_vars( get_class( $setting ) ); | |
| 1295 | + foreach ( $setting_props as $prop_name => $prop_value ) { | |
| 1296 | + | |
| 1297 | + if ( ! in_array( $prop_name, array_keys( $public_props ) ) ) { | |
| 1298 | + continue; | |
| 1299 | + } | |
| 1300 | + | |
| 1301 | + $setting->$prop_name = $prop_value; | |
| 1302 | + } | |
| 1303 | + } | |
| 1304 | + } | |
| 1305 | + | |
| 1306 | + // Maybe remove controls | |
| 1307 | + if ( ! empty( $this->customizer_config['remove_controls'] ) ) { | |
| 1308 | + // Standardize it. | |
| 1309 | + if ( is_string( $this->customizer_config['remove_controls'] ) ) { | |
| 1310 | + $this->customizer_config['remove_controls'] = array( $this->customizer_config['remove_controls'] ); | |
| 1311 | + } | |
| 1312 | + | |
| 1313 | + foreach ( $this->customizer_config['remove_controls'] as $control_id ) { | |
| 1314 | + $wp_customize->remove_control( $control_id ); | |
| 1315 | + } | |
| 1316 | + } | |
| 1317 | + | |
| 1318 | + // Maybe change control props. | |
| 1319 | + if ( ! empty( $this->customizer_config['change_control_props'] ) ) { | |
| 1320 | + foreach ( $this->customizer_config['change_control_props'] as $control_id => $control_props ) { | |
| 1321 | + if ( ! is_array( $control_props ) ) { | |
| 1322 | + continue; | |
| 1323 | + } | |
| 1324 | + | |
| 1325 | + $control = $wp_customize->get_control( $control_id ); | |
| 1326 | + if ( empty( $control ) || ! $control instanceof WP_Customize_Control ) { | |
| 1327 | + continue; | |
| 1328 | + } | |
| 1329 | + | |
| 1330 | + $public_props = get_class_vars( get_class( $control ) ); | |
| 1331 | + foreach ( $control_props as $prop_name => $prop_value ) { | |
| 1332 | + | |
| 1333 | + if ( ! in_array( $prop_name, array_keys( $public_props ) ) ) { | |
| 1334 | + continue; | |
| 1335 | + } | |
| 1336 | + | |
| 1337 | + $control->$prop_name = $prop_value; | |
| 1338 | + } | |
| 1339 | + } | |
| 1340 | + } | |
| 1341 | + } | |
| 1342 | + | |
| 1343 | + /** | |
| 1344 | + * @param WP_Customize_Manager $wp_customize | |
| 1345 | + */ | |
| 1004 | 1346 | function register_customizer( $wp_customize ) { |
| 1005 | 1347 | |
| 1006 | 1348 | $this->register_customizer_controls(); |
| 1007 | 1349 | |
| 1008 | - $customizer_settings = self::$customizer_config; | |
| 1350 | + $customizer_settings = $this->customizer_config; | |
| 1009 | 1351 | |
| 1010 | 1352 | if ( ! empty ( $customizer_settings ) ) { |
| 1011 | 1353 | |
| 1012 | 1354 | // first check the very needed options name |
| 1013 | - if ( ! isset ( $customizer_settings['opt-name'] ) || empty( $customizer_settings['opt-name'] ) ) { | |
| 1355 | + if ( empty( $customizer_settings['opt-name'] ) ) { | |
| 1014 | 1356 | return; |
| 1015 | 1357 | } |
| 1016 | 1358 | $options_name = $customizer_settings['opt-name']; |
| 1017 | 1359 | $wp_customize->options_key = $options_name; |
| @@ -1018,37 +1360,49 @@ | ||
| 1018 | 1360 | |
| 1019 | 1361 | // let's check if we have sections or panels |
| 1020 | 1362 | if ( isset( $customizer_settings['panels'] ) && ! empty( $customizer_settings['panels'] ) ) { |
| 1021 | 1363 | |
| 1022 | - foreach ( $customizer_settings['panels'] as $panel_id => $panel_settings ) { | |
| 1364 | + foreach ( $customizer_settings['panels'] as $panel_id => $panel_config ) { | |
| 1023 | 1365 | |
| 1024 | - if ( ! empty( $panel_id ) && isset( $panel_settings['sections'] ) && ! empty( $panel_settings['sections'] ) ) { | |
| 1366 | + if ( ! empty( $panel_id ) && isset( $panel_config['sections'] ) && ! empty( $panel_config['sections'] ) ) { | |
| 1025 | 1367 | |
| 1026 | - $panel_id = $options_name . '[' . $panel_id . ']'; | |
| 1368 | + // If we have been explicitly given a panel ID we will use that | |
| 1369 | + if ( ! empty( $panel_config['panel_id'] ) ) { | |
| 1370 | + $panel_id = $panel_config['panel_id']; | |
| 1371 | + } else { | |
| 1372 | + $panel_id = $options_name . '[' . $panel_id . ']'; | |
| 1373 | + } | |
| 1374 | + | |
| 1027 | 1375 | $panel_args = array( |
| 1028 | - 'priority' => 10, | |
| 1029 | - 'capability' => 'edit_theme_options', | |
| 1030 | - 'title' => __( 'Panel title is required', 'pixcustomify' ), | |
| 1031 | - 'description' => __( 'Description of what this panel does.', 'pixcustomify' ), | |
| 1376 | + 'priority' => 10, | |
| 1377 | + 'capability' => 'edit_theme_options', | |
| 1378 | + 'title' => __( 'Panel title is required', 'customify' ), | |
| 1379 | + 'description' => __( 'Description of what this panel does.', 'customify' ), | |
| 1380 | + 'auto_expand_sole_section' => false, | |
| 1032 | 1381 | ); |
| 1033 | 1382 | |
| 1034 | - if ( isset( $panel_settings['priority'] ) && ! empty( $panel_settings['priority'] ) ) { | |
| 1035 | - $panel_args['priority'] = $panel_settings['priority']; | |
| 1383 | + if ( isset( $panel_config['priority'] ) && ! empty( $panel_config['priority'] ) ) { | |
| 1384 | + $panel_args['priority'] = $panel_config['priority']; | |
| 1036 | 1385 | } |
| 1037 | 1386 | |
| 1038 | - if ( isset( $panel_settings['title'] ) && ! empty( $panel_settings['title'] ) ) { | |
| 1039 | - $panel_args['title'] = $panel_settings['title']; | |
| 1387 | + if ( isset( $panel_config['title'] ) && ! empty( $panel_config['title'] ) ) { | |
| 1388 | + $panel_args['title'] = $panel_config['title']; | |
| 1040 | 1389 | } |
| 1041 | 1390 | |
| 1042 | - if ( isset( $panel_settings['description'] ) && ! empty( $panel_settings['description'] ) ) { | |
| 1043 | - $panel_args[10] = $panel_settings['description']; | |
| 1391 | + if ( isset( $panel_config['description'] ) && ! empty( $panel_config['description'] ) ) { | |
| 1392 | + $panel_args['description'] = $panel_config['description']; | |
| 1044 | 1393 | } |
| 1045 | 1394 | |
| 1395 | + if ( isset( $panel_config['auto_expand_sole_section'] ) ) { | |
| 1396 | + $panel_args['auto_expand_sole_section'] = $panel_config['auto_expand_sole_section']; | |
| 1397 | + } | |
| 1398 | + | |
| 1399 | + | |
| 1046 | 1400 | $wp_customize->add_panel( $panel_id, $panel_args ); |
| 1047 | 1401 | |
| 1048 | - foreach ( $panel_settings['sections'] as $section_id => $section_settings ) { | |
| 1049 | - if ( ! empty( $section_id ) && isset( $section_settings['options'] ) && ! empty( $section_settings['options'] ) ) { | |
| 1050 | - $this->register_section( $panel_id, $section_id, $options_name, $section_settings, $wp_customize ); | |
| 1402 | + foreach ( $panel_config['sections'] as $section_id => $section_config ) { | |
| 1403 | + if ( ! empty( $section_id ) && isset( $section_config['options'] ) && ! empty( $section_config['options'] ) ) { | |
| 1404 | + $this->register_section( $panel_id, $section_id, $options_name, $section_config, $wp_customize ); | |
| 1051 | 1405 | } |
| 1052 | 1406 | } |
| 1053 | 1407 | } |
| 1054 | 1408 | } |
| @@ -1055,19 +1409,21 @@ | ||
| 1055 | 1409 | } |
| 1056 | 1410 | |
| 1057 | 1411 | if ( isset( $customizer_settings['sections'] ) && ! empty( $customizer_settings['sections'] ) ) { |
| 1058 | 1412 | |
| 1059 | - foreach ( $customizer_settings['sections'] as $section_id => $section_settings ) { | |
| 1060 | - if ( ! empty( $section_id ) && isset( $section_settings['options'] ) && ! empty( $section_settings['options'] ) ) { | |
| 1061 | - $this->register_section( $panel_id = false, $section_id, $options_name, $section_settings, $wp_customize ); | |
| 1413 | + foreach ( $customizer_settings['sections'] as $section_id => $section_config ) { | |
| 1414 | + if ( ! empty( $section_id ) && isset( $section_config['options'] ) && ! empty( $section_config['options'] ) ) { | |
| 1415 | + $this->register_section( $panel_id = false, $section_id, $options_name, $section_config, $wp_customize ); | |
| 1062 | 1416 | } |
| 1063 | 1417 | } |
| 1064 | 1418 | } |
| 1065 | 1419 | |
| 1066 | - if ( self::$plugin_settings['enable_reset_buttons'] ) { | |
| 1420 | + if ( $this->plugin_settings['enable_reset_buttons'] ) { | |
| 1067 | 1421 | // create a toolbar section which will be present all the time |
| 1068 | 1422 | $reset_section_settings = array( |
| 1069 | - 'title' => 'Customify toolbar', | |
| 1423 | + 'title' => 'Customify Toolbox', | |
| 1424 | + 'capability' => 'manage_options', | |
| 1425 | + 'priority' => 999999999, | |
| 1070 | 1426 | 'options' => array( |
| 1071 | 1427 | 'reset_all_button' => array( |
| 1072 | 1428 | 'type' => 'button', |
| 1073 | 1429 | 'label' => 'Reset Customify', |
| @@ -1078,12 +1434,9 @@ | ||
| 1078 | 1434 | ); |
| 1079 | 1435 | |
| 1080 | 1436 | $wp_customize->add_section( |
| 1081 | 1437 | 'customify_toolbar', |
| 1082 | - array( | |
| 1083 | - 'title' => '', | |
| 1084 | - 'priority' => 999999999 | |
| 1085 | - ) | |
| 1438 | + $reset_section_settings | |
| 1086 | 1439 | ); |
| 1087 | 1440 | |
| 1088 | 1441 | $wp_customize->add_setting( |
| 1089 | 1442 | 'reset_customify', |
| @@ -1092,9 +1445,9 @@ | ||
| 1092 | 1445 | $wp_customize->add_control( new Pix_Customize_Button_Control( |
| 1093 | 1446 | $wp_customize, |
| 1094 | 1447 | 'reset_customify', |
| 1095 | 1448 | array( |
| 1096 | - 'label' => __( 'Reset Customify to Defaults', 'customify' ), | |
| 1449 | + 'label' => __( 'Reset All Customify Options to Default', 'customify' ), | |
| 1097 | 1450 | 'section' => 'customify_toolbar', |
| 1098 | 1451 | 'settings' => 'reset_customify', |
| 1099 | 1452 | 'action' => 'reset_customify', |
| 1100 | 1453 | ) |
| @@ -1124,54 +1477,83 @@ | ||
| 1124 | 1477 | |
| 1125 | 1478 | do_action( 'customify_create_custom_control', $wp_customize ); |
| 1126 | 1479 | } |
| 1127 | 1480 | |
| 1128 | - protected function register_section( $panel_id, $section_key, $options_name, $section_settings, $wp_customize ) { | |
| 1481 | + /** | |
| 1482 | + * @param string $panel_id | |
| 1483 | + * @param string $section_key | |
| 1484 | + * @param string $options_name | |
| 1485 | + * @param array $section_config | |
| 1486 | + * @param WP_Customize_Manager $wp_customize | |
| 1487 | + */ | |
| 1488 | + protected function register_section( $panel_id, $section_key, $options_name, $section_config, $wp_customize ) { | |
| 1129 | 1489 | |
| 1130 | - if ( isset( self::$plugin_settings['disable_customify_sections'] ) && isset( self::$plugin_settings['disable_customify_sections'][ $section_key ] ) ) { | |
| 1490 | + if ( isset( $this->plugin_settings['disable_customify_sections'] ) && isset( $this->plugin_settings['disable_customify_sections'][ $section_key ] ) ) { | |
| 1131 | 1491 | return; |
| 1132 | 1492 | } |
| 1133 | 1493 | |
| 1134 | - $section_args = array( | |
| 1135 | - 'priority' => 10, | |
| 1136 | - 'capability' => 'edit_theme_options', | |
| 1137 | - 'title' => __( 'Title Section is required', '' ), | |
| 1138 | - 'panel' => $panel_id, | |
| 1139 | - ); | |
| 1140 | - $section_id = $options_name . '[' . $section_key . ']'; | |
| 1141 | - | |
| 1142 | - if ( isset( $section_settings['priority'] ) && ! empty( $section_settings['priority'] ) ) { | |
| 1143 | - $section_args['priority'] = $section_settings['priority']; | |
| 1494 | + // If we have been explicitly given a section ID we will use that | |
| 1495 | + if ( ! empty( $section_config['section_id'] ) ) { | |
| 1496 | + $section_id = $section_config['section_id']; | |
| 1497 | + } else { | |
| 1498 | + $section_id = $options_name . '[' . $section_key . ']'; | |
| 1144 | 1499 | } |
| 1145 | 1500 | |
| 1146 | - if ( isset( $section_settings['title'] ) && ! empty( $section_settings['title'] ) ) { | |
| 1147 | - $section_args['title'] = $section_settings['title']; | |
| 1148 | - } | |
| 1501 | + // Add the new section to the Customizer, but only if it is not already added. | |
| 1502 | + if ( ! $wp_customize->get_section( $section_id ) ) { | |
| 1503 | + // Merge the section settings with the defaults | |
| 1504 | + $section_args = wp_parse_args( $section_config, array( | |
| 1505 | + 'priority' => 10, | |
| 1506 | + 'panel' => $panel_id, | |
| 1507 | + 'capability' => 'edit_theme_options', | |
| 1508 | + 'theme_supports' => '', | |
| 1509 | + 'title' => esc_html__( 'Title Section is required', 'customify' ), | |
| 1510 | + 'description' => '', | |
| 1511 | + 'type' => 'default', | |
| 1512 | + 'description_hidden' => false, | |
| 1513 | + ) ); | |
| 1149 | 1514 | |
| 1150 | - if ( isset( $section_settings['theme_supports'] ) && ! empty( $section_settings['theme_supports'] ) ) { | |
| 1151 | - $section_args['theme_supports'] = $section_settings['theme_supports']; | |
| 1515 | + $wp_customize->add_section( $section_id, $section_args ); | |
| 1152 | 1516 | } |
| 1153 | 1517 | |
| 1154 | - if ( isset( $section_settings['description'] ) && ! empty( $section_settings['description'] ) ) { | |
| 1155 | - $section_args['description'] = $section_settings['description']; | |
| 1156 | - } | |
| 1518 | + // Now go through each section option and add the fields | |
| 1519 | + foreach ( $section_config['options'] as $option_id => $option_config ) { | |
| 1157 | 1520 | |
| 1158 | - $wp_customize->add_section( $section_id, $section_args ); | |
| 1159 | - | |
| 1160 | - foreach ( $section_settings['options'] as $option_id => $option_config ) { | |
| 1161 | - | |
| 1162 | 1521 | if ( empty( $option_id ) || ! isset( $option_config['type'] ) ) { |
| 1163 | 1522 | continue; |
| 1164 | 1523 | } |
| 1165 | 1524 | |
| 1166 | - $option_id = $options_name . '[' . $option_id . ']'; | |
| 1525 | + // If we have been explicitly given a setting ID we will use that | |
| 1526 | + if ( ! empty( $option_config['setting_id'] ) ) { | |
| 1527 | + $setting_id = $option_config['setting_id']; | |
| 1528 | + } else { | |
| 1529 | + $setting_id = $options_name . '[' . $option_id . ']'; | |
| 1530 | + } | |
| 1167 | 1531 | |
| 1168 | - $this->register_field( $section_id, $option_id, $option_config, $wp_customize ); | |
| 1532 | + // Add the option config to the localized array so we can pass the info to JS. | |
| 1533 | + // @todo Maybe we should ensure that the connected_fields configs passed here follow the same format and logic as the ones in ::customize_pane_settings_additional_data() thus maybe having the data in the same place. | |
| 1534 | + $this->localized['settings'][ $setting_id ] = $option_config; | |
| 1535 | + | |
| 1536 | + // Generate a safe option ID (not the final setting ID) to us in HTML attributes like ID or class | |
| 1537 | + $this->localized['settings'][ $setting_id ]['html_safe_option_id'] = sanitize_html_class( $option_id ); | |
| 1538 | + | |
| 1539 | + $this->register_field( $section_id, $setting_id, $option_config, $wp_customize ); | |
| 1169 | 1540 | } |
| 1170 | 1541 | |
| 1171 | 1542 | } |
| 1172 | 1543 | |
| 1173 | - protected function register_field( $section_id, $setting_id, $setting_config, $wp_customize ) { | |
| 1544 | + /** | |
| 1545 | + * Register a Customizer field (setting and control). | |
| 1546 | + * | |
| 1547 | + * @see WP_Customize_Setting | |
| 1548 | + * @see WP_Customize_Control | |
| 1549 | + * | |
| 1550 | + * @param string $section_id | |
| 1551 | + * @param string $setting_id | |
| 1552 | + * @param array $field_config | |
| 1553 | + * @param WP_Customize_Manager $wp_customize | |
| 1554 | + */ | |
| 1555 | + protected function register_field( $section_id, $setting_id, $field_config, $wp_customize ) { | |
| 1174 | 1556 | |
| 1175 | 1557 | $add_control = true; |
| 1176 | 1558 | // defaults |
| 1177 | 1559 | $setting_args = array( |
| @@ -1179,34 +1561,38 @@ | ||
| 1179 | 1561 | 'capability' => 'edit_theme_options', |
| 1180 | 1562 | 'transport' => 'refresh', |
| 1181 | 1563 | ); |
| 1182 | 1564 | $control_args = array( |
| 1565 | + 'priority' => 10, | |
| 1183 | 1566 | 'label' => '', |
| 1184 | 1567 | 'section' => $section_id, |
| 1185 | 1568 | 'settings' => $setting_id, |
| 1186 | 1569 | ); |
| 1187 | 1570 | |
| 1188 | - self::$localized['settings'][ $setting_id ] = $setting_config; | |
| 1189 | - | |
| 1190 | 1571 | // sanitize settings |
| 1191 | - if ( ( isset( $setting_config['live'] ) && $setting_config['live'] ) || $setting_config['type'] === 'font' ) { | |
| 1572 | + if ( ! empty( $field_config['live'] ) || $field_config['type'] === 'font' ) { | |
| 1192 | 1573 | $setting_args['transport'] = 'postMessage'; |
| 1193 | 1574 | } |
| 1194 | 1575 | |
| 1195 | - if ( isset( $setting_config['default'] ) ) { | |
| 1196 | - $setting_args['default'] = $setting_config['default']; | |
| 1576 | + if ( isset( $field_config['default'] ) ) { | |
| 1577 | + $setting_args['default'] = $field_config['default']; | |
| 1197 | 1578 | } |
| 1198 | 1579 | |
| 1199 | - if ( isset( $setting_config['capability'] ) && ! empty( $setting_config['capability'] ) ) { | |
| 1200 | - $setting_args['capability'] = $setting_config['capability']; | |
| 1580 | + if ( ! empty( $field_config['capability'] ) ) { | |
| 1581 | + $setting_args['capability'] = $field_config['capability']; | |
| 1201 | 1582 | } |
| 1202 | 1583 | |
| 1203 | - if ( self::$plugin_settings['values_store_mod'] == 'option' ) { | |
| 1584 | + // If the setting defines it's own type we will respect that, otherwise we will follow the global plugin setting. | |
| 1585 | + if ( ! empty( $field_config['setting_type'] ) ) { | |
| 1586 | + if ( 'option' === $field_config['setting_type'] ) { | |
| 1587 | + $setting_args['type'] = 'option'; | |
| 1588 | + } | |
| 1589 | + } elseif ( $this->plugin_settings['values_store_mod'] === 'option' ) { | |
| 1204 | 1590 | $setting_args['type'] = 'option'; |
| 1205 | 1591 | } |
| 1206 | 1592 | |
| 1207 | 1593 | // if we arrive here this means we have a custom field control |
| 1208 | - switch ( $setting_config['type'] ) { | |
| 1594 | + switch ( $field_config['type'] ) { | |
| 1209 | 1595 | |
| 1210 | 1596 | case 'checkbox': |
| 1211 | 1597 | |
| 1212 | 1598 | $setting_args['sanitize_callback'] = array( $this, 'setting_sanitize_checkbox' ); |
| @@ -1215,37 +1601,41 @@ | ||
| 1215 | 1601 | default: |
| 1216 | 1602 | break; |
| 1217 | 1603 | } |
| 1218 | 1604 | |
| 1219 | - if ( isset( $setting_config['sanitize_callback'] ) && ! empty( $setting_config['sanitize_callback'] ) && function_exists( $setting_config['sanitize_callback'] ) ) { | |
| 1220 | - $setting_args['sanitize_callback'] = $setting_config['sanitize_callback']; | |
| 1605 | + if ( ! empty( $field_config['sanitize_callback'] ) && is_callable( $field_config['sanitize_callback'] ) ) { | |
| 1606 | + $setting_args['sanitize_callback'] = $field_config['sanitize_callback']; | |
| 1221 | 1607 | } |
| 1222 | 1608 | |
| 1223 | - // and add it | |
| 1609 | + // Add the setting | |
| 1224 | 1610 | $wp_customize->add_setting( $setting_id, $setting_args ); |
| 1225 | 1611 | |
| 1226 | 1612 | // now sanitize the control |
| 1227 | - if ( isset( $setting_config['label'] ) && ! empty( $setting_config['label'] ) ) { | |
| 1228 | - $control_args['label'] = $setting_config['label']; | |
| 1613 | + if ( ! empty( $field_config['label'] ) ) { | |
| 1614 | + $control_args['label'] = $field_config['label']; | |
| 1229 | 1615 | } |
| 1230 | 1616 | |
| 1231 | - if ( isset( $setting_config['priority'] ) && ! empty( $setting_config['priority'] ) ) { | |
| 1232 | - $control_args['priority'] = $setting_config['priority']; | |
| 1617 | + if ( ! empty( $field_config['priority'] ) ) { | |
| 1618 | + $control_args['priority'] = $field_config['priority']; | |
| 1233 | 1619 | } |
| 1234 | 1620 | |
| 1235 | - if ( isset( $setting_config['desc'] ) && ! empty( $setting_config['desc'] ) ) { | |
| 1236 | - $control_args['description'] = $setting_config['desc']; | |
| 1621 | + if ( ! empty( $field_config['desc'] ) ) { | |
| 1622 | + $control_args['description'] = $field_config['desc']; | |
| 1237 | 1623 | } |
| 1238 | 1624 | |
| 1625 | + if ( ! empty( $field_config['active_callback'] ) ) { | |
| 1626 | + $control_args['active_callback'] = $field_config['active_callback']; | |
| 1627 | + } | |
| 1239 | 1628 | |
| 1240 | - $control_args['type'] = $setting_config['type']; | |
| 1241 | 1629 | |
| 1630 | + $control_args['type'] = $field_config['type']; | |
| 1631 | + | |
| 1242 | 1632 | // select the control type |
| 1243 | 1633 | // but first init a default |
| 1244 | 1634 | $control_class_name = 'Pix_Customize_Text_Control'; |
| 1245 | 1635 | |
| 1246 | - // if is a standard wp field type call it here and skip the rest | |
| 1247 | - if ( in_array( $setting_config['type'], array( | |
| 1636 | + // If is a standard wp field type call it here and skip the rest. | |
| 1637 | + if ( in_array( $field_config['type'], array( | |
| 1248 | 1638 | 'checkbox', |
| 1249 | 1639 | 'dropdown-pages', |
| 1250 | 1640 | 'url', |
| 1251 | 1641 | 'date', |
| @@ -1256,31 +1646,30 @@ | ||
| 1256 | 1646 | ) ) ) { |
| 1257 | 1647 | $wp_customize->add_control( $setting_id . '_control', $control_args ); |
| 1258 | 1648 | |
| 1259 | 1649 | return; |
| 1260 | - } elseif ( in_array( $setting_config['type'], array( | |
| 1650 | + } elseif ( in_array( $field_config['type'], array( | |
| 1261 | 1651 | 'radio', |
| 1262 | 1652 | 'select' |
| 1263 | - ) ) && isset( $setting_config['choices'] ) && ! empty( $setting_config['choices'] ) | |
| 1653 | + ) ) && ! empty( $field_config['choices'] ) | |
| 1264 | 1654 | ) { |
| 1265 | - $control_args['choices'] = $setting_config['choices']; | |
| 1655 | + $control_args['choices'] = $field_config['choices']; | |
| 1266 | 1656 | $wp_customize->add_control( $setting_id . '_control', $control_args ); |
| 1267 | 1657 | |
| 1268 | 1658 | return; |
| 1269 | - } elseif ( in_array( $setting_config['type'], array( 'range' ) ) && isset( $setting_config['input_attrs'] ) && ! empty( $setting_config['input_attrs'] ) ) { | |
| 1659 | + } elseif ( in_array( $field_config['type'], array( 'range' ) ) && ! empty( $field_config['input_attrs'] ) ) { | |
| 1270 | 1660 | |
| 1271 | - $control_args['input_attrs'] = $setting_config['input_attrs']; | |
| 1661 | + $control_args['input_attrs'] = $field_config['input_attrs']; | |
| 1272 | 1662 | |
| 1273 | 1663 | $wp_customize->add_control( $setting_id . '_control', $control_args ); |
| 1274 | 1664 | } |
| 1275 | 1665 | |
| 1276 | - // if we arrive here this means we have a custom field control | |
| 1277 | - switch ( $setting_config['type'] ) { | |
| 1666 | + // If we arrive here this means we have a custom field control. | |
| 1667 | + switch ( $field_config['type'] ) { | |
| 1278 | 1668 | |
| 1279 | 1669 | case 'text': |
| 1280 | - | |
| 1281 | - if ( isset( $setting_config['live'] ) ) { | |
| 1282 | - $control_args['live'] = $setting_config['live']; | |
| 1670 | + if ( isset( $field_config['live'] ) ) { | |
| 1671 | + $control_args['live'] = $field_config['live']; | |
| 1283 | 1672 | } |
| 1284 | 1673 | |
| 1285 | 1674 | $control_class_name = 'Pix_Customize_Text_Control'; |
| 1286 | 1675 | break; |
| @@ -1285,11 +1674,10 @@ | ||
| 1285 | 1674 | $control_class_name = 'Pix_Customize_Text_Control'; |
| 1286 | 1675 | break; |
| 1287 | 1676 | |
| 1288 | 1677 | case 'textarea': |
| 1289 | - | |
| 1290 | - if ( isset( $setting_config['live'] ) ) { | |
| 1291 | - $control_args['live'] = $setting_config['live']; | |
| 1678 | + if ( isset( $field_config['live'] ) ) { | |
| 1679 | + $control_args['live'] = $field_config['live']; | |
| 1292 | 1680 | } |
| 1293 | 1681 | |
| 1294 | 1682 | $control_class_name = 'Pix_Customize_Textarea_Control'; |
| 1295 | 1683 | break; |
| @@ -1294,25 +1682,22 @@ | ||
| 1294 | 1682 | $control_class_name = 'Pix_Customize_Textarea_Control'; |
| 1295 | 1683 | break; |
| 1296 | 1684 | |
| 1297 | 1685 | case 'color': |
| 1298 | - | |
| 1299 | 1686 | $control_class_name = 'WP_Customize_Color_Control'; |
| 1300 | 1687 | break; |
| 1301 | 1688 | |
| 1302 | 1689 | case 'color_drop': |
| 1303 | - | |
| 1304 | 1690 | $control_class_name = 'Pix_Customize_Color_Drop_Control'; |
| 1305 | 1691 | break; |
| 1306 | 1692 | |
| 1307 | 1693 | case 'ace_editor': |
| 1308 | - | |
| 1309 | - if ( isset( $setting_config['live'] ) ) { | |
| 1310 | - $control_args['live'] = $setting_config['live']; | |
| 1694 | + if ( isset( $field_config['live'] ) ) { | |
| 1695 | + $control_args['live'] = $field_config['live']; | |
| 1311 | 1696 | } |
| 1312 | 1697 | |
| 1313 | - if ( isset( $setting_config['editor_type'] ) ) { | |
| 1314 | - $control_args['editor_type'] = $setting_config['editor_type']; | |
| 1698 | + if ( isset( $field_config['editor_type'] ) ) { | |
| 1699 | + $control_args['editor_type'] = $field_config['editor_type']; | |
| 1315 | 1700 | } |
| 1316 | 1701 | |
| 1317 | 1702 | $control_class_name = 'Pix_Customize_Ace_Editor_Control'; |
| 1318 | 1703 | break; |
| @@ -1317,46 +1702,47 @@ | ||
| 1317 | 1702 | $control_class_name = 'Pix_Customize_Ace_Editor_Control'; |
| 1318 | 1703 | break; |
| 1319 | 1704 | |
| 1320 | 1705 | case 'upload': |
| 1321 | - | |
| 1322 | 1706 | $control_class_name = 'WP_Customize_Upload_Control'; |
| 1323 | 1707 | break; |
| 1324 | 1708 | |
| 1325 | 1709 | case 'image': |
| 1326 | - | |
| 1327 | 1710 | $control_class_name = 'WP_Customize_Image_Control'; |
| 1328 | 1711 | break; |
| 1329 | 1712 | |
| 1330 | 1713 | case 'media': |
| 1331 | - | |
| 1332 | 1714 | $control_class_name = 'WP_Customize_Media_Control'; |
| 1333 | 1715 | break; |
| 1334 | 1716 | |
| 1335 | 1717 | case 'custom_background': |
| 1336 | - if ( isset( $setting_config['field'] ) ) { | |
| 1337 | - $control_args['field'] = $setting_config['field']; | |
| 1718 | + if ( isset( $field_config['field'] ) ) { | |
| 1719 | + $control_args['field'] = $field_config['field']; | |
| 1338 | 1720 | } |
| 1339 | 1721 | |
| 1340 | 1722 | $control_class_name = 'Pix_Customize_Background_Control'; |
| 1341 | 1723 | break; |
| 1342 | 1724 | |
| 1343 | - case 'cropped_media': | |
| 1725 | + case 'cropped_image': | |
| 1726 | + case 'cropped_media': // 'cropped_media' no longer works | |
| 1727 | + if ( isset( $field_config['width'] ) ) { | |
| 1728 | + $control_args['width'] = $field_config['width']; | |
| 1729 | + } | |
| 1344 | 1730 | |
| 1345 | - if ( isset( $setting_config['width'] ) ) { | |
| 1346 | - $control_args['width'] = $setting_config['width']; | |
| 1731 | + if ( isset( $field_config['height'] ) ) { | |
| 1732 | + $control_args['height'] = $field_config['height']; | |
| 1347 | 1733 | } |
| 1348 | 1734 | |
| 1349 | - if ( isset( $setting_config['height'] ) ) { | |
| 1350 | - $control_args['height'] = $setting_config['height']; | |
| 1735 | + if ( isset( $field_config['flex_width'] ) ) { | |
| 1736 | + $control_args['flex_width'] = $field_config['flex_width']; | |
| 1351 | 1737 | } |
| 1352 | 1738 | |
| 1353 | - if ( isset( $setting_config['flex_width'] ) ) { | |
| 1354 | - $control_args['flex_width'] = $setting_config['flex_width']; | |
| 1739 | + if ( isset( $field_config['flex_height'] ) ) { | |
| 1740 | + $control_args['flex_height'] = $field_config['flex_height']; | |
| 1355 | 1741 | } |
| 1356 | 1742 | |
| 1357 | - if ( isset( $setting_config['flex_height'] ) ) { | |
| 1358 | - $control_args['flex_height'] = $setting_config['flex_height']; | |
| 1743 | + if ( isset( $field_config['button_labels'] ) ) { | |
| 1744 | + $control_args['button_labels'] = $field_config['button_labels']; | |
| 1359 | 1745 | } |
| 1360 | 1746 | |
| 1361 | 1747 | $control_class_name = 'WP_Customize_Cropped_Image_Control'; |
| 1362 | 1748 | break; |
| @@ -1362,11 +1748,10 @@ | ||
| 1362 | 1748 | break; |
| 1363 | 1749 | |
| 1364 | 1750 | // Custom types |
| 1365 | 1751 | case 'typography' : |
| 1752 | + $use_typography = $this->get_plugin_setting( 'typography', '1' ); | |
| 1366 | 1753 | |
| 1367 | - $use_typography = self::get_plugin_option( 'typography', '1' ); | |
| 1368 | - | |
| 1369 | 1754 | if ( $use_typography === false ) { |
| 1370 | 1755 | $add_control = false; |
| 1371 | 1756 | continue; |
| 1372 | 1757 | } |
| @@ -1372,39 +1757,37 @@ | ||
| 1372 | 1757 | } |
| 1373 | 1758 | |
| 1374 | 1759 | $control_class_name = 'Pix_Customize_Typography_Control'; |
| 1375 | 1760 | |
| 1376 | - if ( isset( $setting_config['backup'] ) ) { | |
| 1377 | - $control_args['backup'] = $setting_config['backup']; | |
| 1761 | + if ( isset( $field_config['backup'] ) ) { | |
| 1762 | + $control_args['backup'] = $field_config['backup']; | |
| 1378 | 1763 | } |
| 1379 | 1764 | |
| 1380 | - if ( isset( $setting_config['font_weight'] ) ) { | |
| 1381 | - $control_args['font_weight'] = $setting_config['font_weight']; | |
| 1765 | + if ( isset( $field_config['font_weight'] ) ) { | |
| 1766 | + $control_args['font_weight'] = $field_config['font_weight']; | |
| 1382 | 1767 | } |
| 1383 | 1768 | |
| 1384 | - if ( isset( $setting_config['subsets'] ) ) { | |
| 1385 | - $control_args['subsets'] = $setting_config['subsets']; | |
| 1769 | + if ( isset( $field_config['subsets'] ) ) { | |
| 1770 | + $control_args['subsets'] = $field_config['subsets']; | |
| 1386 | 1771 | } |
| 1387 | 1772 | |
| 1388 | - if ( isset( $setting_config['recommended'] ) ) { | |
| 1389 | - $control_args['recommended'] = array_flip( $setting_config['recommended'] ); | |
| 1773 | + if ( isset( $field_config['recommended'] ) ) { | |
| 1774 | + $control_args['recommended'] = array_flip( $field_config['recommended'] ); | |
| 1390 | 1775 | } |
| 1391 | 1776 | |
| 1392 | - if ( isset( $setting_config['load_all_weights'] ) ) { | |
| 1393 | - $control_args['load_all_weights'] = $setting_config['load_all_weights']; | |
| 1777 | + if ( isset( $field_config['load_all_weights'] ) ) { | |
| 1778 | + $control_args['load_all_weights'] = $field_config['load_all_weights']; | |
| 1394 | 1779 | } |
| 1395 | 1780 | |
| 1396 | - if ( isset( $setting_config['default'] ) ) { | |
| 1397 | - $control_args['default'] = $setting_config['default']; | |
| 1781 | + if ( isset( $field_config['default'] ) ) { | |
| 1782 | + $control_args['default'] = $field_config['default']; | |
| 1398 | 1783 | } |
| 1399 | 1784 | |
| 1400 | 1785 | break; |
| 1401 | 1786 | |
| 1402 | - // Custom types | |
| 1403 | 1787 | case 'font' : |
| 1788 | + $use_typography = $this->get_plugin_setting( 'typography', '1' ); | |
| 1404 | 1789 | |
| 1405 | - $use_typography = self::get_plugin_option( 'typography', '1' ); | |
| 1406 | - | |
| 1407 | 1790 | if ( $use_typography === false ) { |
| 1408 | 1791 | $add_control = false; |
| 1409 | 1792 | continue; |
| 1410 | 1793 | } |
| @@ -1410,34 +1793,34 @@ | ||
| 1410 | 1793 | } |
| 1411 | 1794 | |
| 1412 | 1795 | $control_class_name = 'Pix_Customize_Font_Control'; |
| 1413 | 1796 | |
| 1414 | - if ( isset( $setting_config['backup'] ) ) { | |
| 1415 | - $control_args['backup'] = $setting_config['backup']; | |
| 1797 | + if ( isset( $field_config['backup'] ) ) { | |
| 1798 | + $control_args['backup'] = $field_config['backup']; | |
| 1416 | 1799 | } |
| 1417 | 1800 | |
| 1418 | - if ( isset( $setting_config['font_weight'] ) ) { | |
| 1419 | - $control_args['font_weight'] = $setting_config['font_weight']; | |
| 1801 | + if ( isset( $field_config['font_weight'] ) ) { | |
| 1802 | + $control_args['font_weight'] = $field_config['font_weight']; | |
| 1420 | 1803 | } |
| 1421 | 1804 | |
| 1422 | - if ( isset( $setting_config['subsets'] ) ) { | |
| 1423 | - $control_args['subsets'] = $setting_config['subsets']; | |
| 1805 | + if ( isset( $field_config['subsets'] ) ) { | |
| 1806 | + $control_args['subsets'] = $field_config['subsets']; | |
| 1424 | 1807 | } |
| 1425 | 1808 | |
| 1426 | - if ( isset( $setting_config['recommended'] ) ) { | |
| 1427 | - $control_args['recommended'] = array_flip( $setting_config['recommended'] ); | |
| 1809 | + if ( isset( $field_config['recommended'] ) ) { | |
| 1810 | + $control_args['recommended'] = array_flip( $field_config['recommended'] ); | |
| 1428 | 1811 | } |
| 1429 | 1812 | |
| 1430 | - if ( isset( $setting_config['load_all_weights'] ) ) { | |
| 1431 | - $control_args['load_all_weights'] = $setting_config['load_all_weights']; | |
| 1813 | + if ( isset( $field_config['load_all_weights'] ) ) { | |
| 1814 | + $control_args['load_all_weights'] = $field_config['load_all_weights']; | |
| 1432 | 1815 | } |
| 1433 | 1816 | |
| 1434 | - if ( isset( $setting_config['default'] ) ) { | |
| 1435 | - $control_args['default'] = $setting_config['default']; | |
| 1817 | + if ( isset( $field_config['default'] ) ) { | |
| 1818 | + $control_args['default'] = $field_config['default']; | |
| 1436 | 1819 | } |
| 1437 | 1820 | |
| 1438 | - if ( isset( $setting_config['fields'] ) ) { | |
| 1439 | - $control_args['fields'] = $setting_config['fields']; | |
| 1821 | + if ( isset( $field_config['fields'] ) ) { | |
| 1822 | + $control_args['fields'] = $field_config['fields']; | |
| 1440 | 1823 | } |
| 1441 | 1824 | $control_args['live'] = true; |
| 1442 | 1825 | |
| 1443 | 1826 | break; |
| @@ -1442,32 +1825,60 @@ | ||
| 1442 | 1825 | |
| 1443 | 1826 | break; |
| 1444 | 1827 | |
| 1445 | 1828 | case 'select2' : |
| 1829 | + if ( ! isset( $field_config['choices'] ) || empty( $field_config['choices'] ) ) { | |
| 1830 | + return; | |
| 1831 | + } | |
| 1446 | 1832 | |
| 1447 | - if ( ! isset( $setting_config['choices'] ) || empty( $setting_config['choices'] ) ) { | |
| 1833 | + $control_args['choices'] = $field_config['choices']; | |
| 1834 | + | |
| 1835 | + $control_class_name = 'Pix_Customize_Select2_Control'; | |
| 1836 | + break; | |
| 1837 | + | |
| 1838 | + case 'sm_radio' : | |
| 1839 | + if ( ! isset( $field_config['choices'] ) || empty( $field_config['choices'] ) ) { | |
| 1448 | 1840 | return; |
| 1449 | 1841 | } |
| 1450 | 1842 | |
| 1451 | - $control_args['choices'] = $setting_config['choices']; | |
| 1843 | + $control_args['choices'] = $field_config['choices']; | |
| 1452 | 1844 | |
| 1453 | - $control_class_name = 'Pix_Customize_Select2_Control'; | |
| 1845 | + $control_class_name = 'Pix_Customize_SM_radio_Control'; | |
| 1454 | 1846 | break; |
| 1455 | 1847 | |
| 1848 | + case 'sm_palette_filter' : | |
| 1849 | + if ( ! isset( $field_config['choices'] ) || empty( $field_config['choices'] ) ) { | |
| 1850 | + return; | |
| 1851 | + } | |
| 1852 | + | |
| 1853 | + $control_args['choices'] = $field_config['choices']; | |
| 1854 | + | |
| 1855 | + $control_class_name = 'Pix_Customize_SM_palette_filter_Control'; | |
| 1856 | + break; | |
| 1857 | + | |
| 1858 | + case 'sm_switch' : | |
| 1859 | + if ( ! isset( $field_config['choices'] ) || empty( $field_config['choices'] ) ) { | |
| 1860 | + return; | |
| 1861 | + } | |
| 1862 | + | |
| 1863 | + $control_args['choices'] = $field_config['choices']; | |
| 1864 | + | |
| 1865 | + $control_class_name = 'Pix_Customize_SM_switch_Control'; | |
| 1866 | + break; | |
| 1867 | + | |
| 1456 | 1868 | case 'preset' : |
| 1457 | - | |
| 1458 | - if ( ! isset( $setting_config['choices'] ) || empty( $setting_config['choices'] ) ) { | |
| 1869 | + if ( ! isset( $field_config['choices'] ) || empty( $field_config['choices'] ) ) { | |
| 1459 | 1870 | return; |
| 1460 | 1871 | } |
| 1461 | 1872 | |
| 1462 | - $control_args['choices'] = $setting_config['choices']; | |
| 1873 | + $control_args['choices'] = $field_config['choices']; | |
| 1463 | 1874 | |
| 1464 | - if ( isset( $setting_config['choices_type'] ) || ! empty( $setting_config['choices_type'] ) ) { | |
| 1465 | - $control_args['choices_type'] = $setting_config['choices_type']; | |
| 1875 | + if ( isset( $field_config['choices_type'] ) || ! empty( $field_config['choices_type'] ) ) { | |
| 1876 | + $control_args['choices_type'] = $field_config['choices_type']; | |
| 1466 | 1877 | } |
| 1467 | 1878 | |
| 1468 | - if ( isset( $setting_config['desc'] ) || ! empty( $setting_config['desc'] ) ) { | |
| 1469 | - $control_args['description'] = $setting_config['desc']; | |
| 1879 | + if ( isset( $field_config['desc'] ) || ! empty( $field_config['desc'] ) ) { | |
| 1880 | + $control_args['description'] = $field_config['desc']; | |
| 1470 | 1881 | } |
| 1471 | 1882 | |
| 1472 | 1883 | |
| 1473 | 1884 | $control_class_name = 'Pix_Customize_Preset_Control'; |
| @@ -1473,21 +1884,20 @@ | ||
| 1473 | 1884 | $control_class_name = 'Pix_Customize_Preset_Control'; |
| 1474 | 1885 | break; |
| 1475 | 1886 | |
| 1476 | 1887 | case 'radio_image' : |
| 1477 | - | |
| 1478 | - if ( ! isset( $setting_config['choices'] ) || empty( $setting_config['choices'] ) ) { | |
| 1888 | + if ( ! isset( $field_config['choices'] ) || empty( $field_config['choices'] ) ) { | |
| 1479 | 1889 | return; |
| 1480 | 1890 | } |
| 1481 | 1891 | |
| 1482 | - $control_args['choices'] = $setting_config['choices']; | |
| 1892 | + $control_args['choices'] = $field_config['choices']; | |
| 1483 | 1893 | |
| 1484 | - if ( isset( $setting_config['choices_type'] ) || ! empty( $setting_config['choices_type'] ) ) { | |
| 1485 | - $control_args['choices_type'] = $setting_config['choices_type']; | |
| 1894 | + if ( isset( $field_config['choices_type'] ) || ! empty( $field_config['choices_type'] ) ) { | |
| 1895 | + $control_args['choices_type'] = $field_config['choices_type']; | |
| 1486 | 1896 | } |
| 1487 | 1897 | |
| 1488 | - if ( isset( $setting_config['desc'] ) || ! empty( $setting_config['desc'] ) ) { | |
| 1489 | - $control_args['description'] = $setting_config['desc']; | |
| 1898 | + if ( isset( $field_config['desc'] ) || ! empty( $field_config['desc'] ) ) { | |
| 1899 | + $control_args['description'] = $field_config['desc']; | |
| 1490 | 1900 | } |
| 1491 | 1901 | |
| 1492 | 1902 | |
| 1493 | 1903 | $control_class_name = 'Pix_Customize_Radio_Image_Control'; |
| @@ -1493,14 +1903,13 @@ | ||
| 1493 | 1903 | $control_class_name = 'Pix_Customize_Radio_Image_Control'; |
| 1494 | 1904 | break; |
| 1495 | 1905 | |
| 1496 | 1906 | case 'button' : |
| 1497 | - | |
| 1498 | - if ( ! isset( $setting_config['action'] ) || empty( $setting_config['action'] ) ) { | |
| 1907 | + if ( ! isset( $field_config['action'] ) || empty( $field_config['action'] ) ) { | |
| 1499 | 1908 | return; |
| 1500 | 1909 | } |
| 1501 | 1910 | |
| 1502 | - $control_args['action'] = $setting_config['action']; | |
| 1911 | + $control_args['action'] = $field_config['action']; | |
| 1503 | 1912 | |
| 1504 | 1913 | $control_class_name = 'Pix_Customize_Button_Control'; |
| 1505 | 1914 | |
| 1506 | 1915 | break; |
| @@ -1505,11 +1914,10 @@ | ||
| 1505 | 1914 | |
| 1506 | 1915 | break; |
| 1507 | 1916 | |
| 1508 | 1917 | case 'html' : |
| 1509 | - | |
| 1510 | - if ( isset( $setting_config['html'] ) || ! empty( $setting_config['html'] ) ) { | |
| 1511 | - $control_args['html'] = $setting_config['html']; | |
| 1918 | + if ( isset( $field_config['html'] ) || ! empty( $field_config['html'] ) ) { | |
| 1919 | + $control_args['html'] = $field_config['html']; | |
| 1512 | 1920 | } |
| 1513 | 1921 | |
| 1514 | 1922 | $control_class_name = 'Pix_Customize_HTML_Control'; |
| 1515 | 1923 | break; |
| @@ -1514,21 +1922,20 @@ | ||
| 1514 | 1922 | $control_class_name = 'Pix_Customize_HTML_Control'; |
| 1515 | 1923 | break; |
| 1516 | 1924 | |
| 1517 | 1925 | case 'import_demo_data' : |
| 1518 | - | |
| 1519 | - if ( isset( $setting_config['html'] ) || ! empty( $setting_config['html'] ) ) { | |
| 1520 | - $control_args['html'] = $setting_config['html']; | |
| 1926 | + if ( isset( $field_config['html'] ) || ! empty( $field_config['html'] ) ) { | |
| 1927 | + $control_args['html'] = $field_config['html']; | |
| 1521 | 1928 | } |
| 1522 | 1929 | |
| 1523 | - if ( ! isset( $setting_config['label'] ) || empty( $setting_config['label'] ) ) { | |
| 1930 | + if ( ! isset( $field_config['label'] ) || empty( $field_config['label'] ) ) { | |
| 1524 | 1931 | $control_args['label'] = esc_html__( 'Import', 'customify' ); |
| 1525 | 1932 | } else { |
| 1526 | - $control_args['label'] = $setting_config['label']; | |
| 1933 | + $control_args['label'] = $field_config['label']; | |
| 1527 | 1934 | } |
| 1528 | 1935 | |
| 1529 | - if ( isset( $setting_config['notices'] ) && ! empty( $setting_config['notices'] ) ) { | |
| 1530 | - $control_args['notices'] = $setting_config['notices']; | |
| 1936 | + if ( isset( $field_config['notices'] ) && ! empty( $field_config['notices'] ) ) { | |
| 1937 | + $control_args['notices'] = $field_config['notices']; | |
| 1531 | 1938 | } |
| 1532 | 1939 | |
| 1533 | 1940 | $control_class_name = 'Pix_Customize_Import_Demo_Data_Control'; |
| 1534 | 1941 | break; |
| @@ -1552,14 +1959,14 @@ | ||
| 1552 | 1959 | |
| 1553 | 1960 | /** |
| 1554 | 1961 | * Remove the sections selected by user |
| 1555 | 1962 | * |
| 1556 | - * @param $wp_customize | |
| 1963 | + * @param WP_Customize_Manager $wp_customize | |
| 1557 | 1964 | */ |
| 1558 | 1965 | function remove_default_sections( $wp_customize ) { |
| 1559 | 1966 | global $wp_registered_sidebars; |
| 1560 | 1967 | |
| 1561 | - $to_remove = self::get_plugin_option( 'disable_default_sections' ); | |
| 1968 | + $to_remove = $this->get_plugin_setting( 'disable_default_sections' ); | |
| 1562 | 1969 | |
| 1563 | 1970 | if ( ! empty( $to_remove ) ) { |
| 1564 | 1971 | foreach ( $to_remove as $section => $nothing ) { |
| 1565 | 1972 | |
| @@ -1574,13 +1981,104 @@ | ||
| 1574 | 1981 | } |
| 1575 | 1982 | } |
| 1576 | 1983 | } |
| 1577 | 1984 | |
| 1578 | - static function get_base_path() { | |
| 1579 | - return plugin_dir_path( __FILE__ ); | |
| 1985 | + /** | |
| 1986 | + * Print JavaScript for adding additional data to _wpCustomizeSettings.settings object of the main window (not the preview window). | |
| 1987 | + */ | |
| 1988 | + public function customize_pane_settings_additional_data() { | |
| 1989 | + /** | |
| 1990 | + * @global WP_Customize_Manager $wp_customize | |
| 1991 | + */ | |
| 1992 | + global $wp_customize; | |
| 1993 | + | |
| 1994 | + // Without an options name we can't do much. | |
| 1995 | + if ( empty( $this->customizer_config['opt-name'] ) ) { | |
| 1996 | + return; | |
| 1997 | + } | |
| 1998 | + | |
| 1999 | + $options_name = $this->customizer_config['opt-name']; | |
| 2000 | + $customizer_settings = $wp_customize->settings(); | |
| 2001 | + ?> | |
| 2002 | + <script type="text/javascript"> | |
| 2003 | + if ( 'undefined' === typeof _wpCustomizeSettings.settings ) { | |
| 2004 | + _wpCustomizeSettings.settings = {}; | |
| 2005 | + } | |
| 2006 | + | |
| 2007 | + <?php | |
| 2008 | + echo "(function ( sAdditional ){\n"; | |
| 2009 | + | |
| 2010 | + $options = $this->get_options(); | |
| 2011 | + foreach ( $options as $option_id => $option_config ) { | |
| 2012 | + // If we have been explicitly given a setting ID we will use that | |
| 2013 | + if ( ! empty( $option_config['setting_id'] ) ) { | |
| 2014 | + $setting_id = $option_config['setting_id']; | |
| 2015 | + } else { | |
| 2016 | + $setting_id = $options_name . '[' . $option_id . ']'; | |
| 2017 | + } | |
| 2018 | + // @todo Right now we only handle the connected_fields key - make this more dynamic by adding the keys that are not returned by WP_Customize_Setting->json() | |
| 2019 | + if ( ! empty( $customizer_settings[ $setting_id ] ) && ! empty( $option_config['connected_fields'] ) ) { | |
| 2020 | + // Pass through all the connected fields and make sure the id is in the final format | |
| 2021 | + $connected_fields = array(); | |
| 2022 | + foreach ( $option_config['connected_fields'] as $key => $connected_field_config ) { | |
| 2023 | + $connected_field_data = array(); | |
| 2024 | + | |
| 2025 | + if ( is_string( $connected_field_config ) ) { | |
| 2026 | + $connected_field_id = $connected_field_config; | |
| 2027 | + } elseif ( is_array( $connected_field_config ) ) { | |
| 2028 | + // We have a full blown connected field config | |
| 2029 | + if ( is_string( $key ) ) { | |
| 2030 | + $connected_field_id = $key; | |
| 2031 | + } else { | |
| 2032 | + continue; | |
| 2033 | + } | |
| 2034 | + | |
| 2035 | + // We will pass to JS all the configured connected field details. | |
| 2036 | + $connected_field_data = $connected_field_config; | |
| 2037 | + } | |
| 2038 | + | |
| 2039 | + // Continue if we don't have a connected field ID to work with. | |
| 2040 | + if ( empty( $connected_field_id ) ) { | |
| 2041 | + continue; | |
| 2042 | + } | |
| 2043 | + | |
| 2044 | + // If the connected setting is not one of our's, we will use it's ID as it is. | |
| 2045 | + if ( ! array_key_exists( $connected_field_id, $options ) ) { | |
| 2046 | + $connected_field_data['setting_id'] = $connected_field_id; | |
| 2047 | + } | |
| 2048 | + // If the connected setting specifies a setting ID, we will not prefix it and use it as it is. | |
| 2049 | + elseif ( ! empty( $options[ $connected_field_id ] ) && ! empty( $options[ $connected_field_id ]['setting_id'] ) ) { | |
| 2050 | + $connected_field_data['setting_id'] = $options[ $connected_field_id ]['setting_id']; | |
| 2051 | + } else { | |
| 2052 | + $connected_field_data['setting_id'] = $options_name . '[' . $connected_field_id . ']'; | |
| 2053 | + } | |
| 2054 | + | |
| 2055 | + $connected_fields[] = $connected_field_data; | |
| 2056 | + } | |
| 2057 | + | |
| 2058 | + printf( | |
| 2059 | + "sAdditional[%s].%s = %s;\n", | |
| 2060 | + wp_json_encode( $setting_id ), | |
| 2061 | + 'connected_fields', | |
| 2062 | + wp_json_encode( $connected_fields, JSON_FORCE_OBJECT ) | |
| 2063 | + ); | |
| 2064 | + } | |
| 2065 | + } | |
| 2066 | + echo "})( _wpCustomizeSettings.settings );\n"; | |
| 2067 | + ?> | |
| 2068 | + </script> | |
| 2069 | + <?php | |
| 1580 | 2070 | } |
| 1581 | 2071 | |
| 1582 | - protected static function get_typography_fields( $array, $key, $value, &$results, $input_key = 0 ) { | |
| 2072 | + public function get_file() { | |
| 2073 | + return $this->file; | |
| 2074 | + } | |
| 2075 | + | |
| 2076 | + public function get_base_path() { | |
| 2077 | + return plugin_dir_path( $this->file ); | |
| 2078 | + } | |
| 2079 | + | |
| 2080 | + public function get_typography_fields( $array, $key, $value, &$results, $input_key = 0 ) { | |
| 1583 | 2081 | if ( ! is_array( $array ) ) { |
| 1584 | 2082 | return; |
| 1585 | 2083 | } |
| 1586 | 2084 | |
| @@ -1592,19 +2090,19 @@ | ||
| 1592 | 2090 | if ( isset( $array['default'] ) && is_array( $array['default'] ) ) { |
| 1593 | 2091 | $default = json_encode( $array['default'] ); |
| 1594 | 2092 | } |
| 1595 | 2093 | |
| 1596 | - $results[ $input_key ]['value'] = self::get_option( $input_key, $default ); | |
| 2094 | + $results[ $input_key ]['value'] = $this->get_option( $input_key, $default ); | |
| 1597 | 2095 | } |
| 1598 | 2096 | |
| 1599 | 2097 | foreach ( $array as $i => $subarray ) { |
| 1600 | - self::get_typography_fields( $subarray, $key, $value, $results, $i ); | |
| 2098 | + $this->get_typography_fields( $subarray, $key, $value, $results, $i ); | |
| 1601 | 2099 | } |
| 1602 | 2100 | } |
| 1603 | 2101 | |
| 1604 | 2102 | function get_options_key() { |
| 1605 | - if ( ! empty( self::$opt_name ) ) { | |
| 1606 | - return self::$opt_name; | |
| 2103 | + if ( ! empty( $this->opt_name ) ) { | |
| 2104 | + return $this->opt_name; | |
| 1607 | 2105 | } |
| 1608 | 2106 | |
| 1609 | 2107 | return false; |
| 1610 | 2108 | } |
| @@ -1614,13 +2112,13 @@ | ||
| 1614 | 2112 | * @return bool |
| 1615 | 2113 | */ |
| 1616 | 2114 | function import_button_exists() { |
| 1617 | 2115 | |
| 1618 | - if ( empty( self::$options_list ) ) { | |
| 1619 | - self::$options_list = $this->get_options(); | |
| 2116 | + if ( empty( $this->options_list ) ) { | |
| 2117 | + $this->options_list = $this->get_options(); | |
| 1620 | 2118 | } |
| 1621 | 2119 | |
| 1622 | - foreach ( self::$options_list as $option ) { | |
| 2120 | + foreach ( $this->options_list as $option ) { | |
| 1623 | 2121 | if ( isset( $option['type'] ) && 'import_demo_data' === $option['type'] ) { |
| 1624 | 2122 | return true; |
| 1625 | 2123 | break; |
| 1626 | 2124 | } |
| @@ -1630,15 +2128,27 @@ | ||
| 1630 | 2128 | } |
| 1631 | 2129 | |
| 1632 | 2130 | /** == Helpers == */ |
| 1633 | 2131 | |
| 1634 | - protected static function get_current_values() { | |
| 1635 | - $store_type = self::get_plugin_option( 'values_store_mod', 'option' ); | |
| 2132 | + protected function get_current_values( $opt_name = null ) { | |
| 2133 | + // Fallback to the global $opt_name | |
| 2134 | + if ( empty( $opt_name ) ) { | |
| 2135 | + $opt_name = $this->opt_name; | |
| 2136 | + } | |
| 2137 | + | |
| 2138 | + // Bail as we have nothing to work with | |
| 2139 | + if ( empty( $opt_name ) ) { | |
| 2140 | + return false; | |
| 2141 | + } | |
| 2142 | + | |
| 2143 | + $store_type = $this->get_plugin_setting( 'values_store_mod', 'option' ); | |
| 1636 | 2144 | if ( $store_type === 'option' ) { |
| 1637 | - self::$current_values = get_option( self::$opt_name ); | |
| 2145 | + return get_option( $opt_name ); | |
| 1638 | 2146 | } elseif ( $store_type === 'theme_mod' ) { |
| 1639 | - self::$current_values = get_theme_mod( self::$opt_name ); | |
| 2147 | + return get_theme_mod( $opt_name ); | |
| 1640 | 2148 | } |
| 2149 | + | |
| 2150 | + return false; | |
| 1641 | 2151 | } |
| 1642 | 2152 | |
| 1643 | 2153 | public function get_options() { |
| 1644 | 2154 | |
| @@ -1643,11 +2153,11 @@ | ||
| 1643 | 2153 | public function get_options() { |
| 1644 | 2154 | |
| 1645 | 2155 | $settings = array(); |
| 1646 | 2156 | |
| 1647 | - if ( isset ( self::$customizer_config['panels'] ) ) { | |
| 2157 | + if ( isset ( $this->customizer_config['panels'] ) ) { | |
| 1648 | 2158 | |
| 1649 | - foreach ( self::$customizer_config['panels'] as $pane_id => $panel_settings ) { | |
| 2159 | + foreach ( $this->customizer_config['panels'] as $pane_id => $panel_settings ) { | |
| 1650 | 2160 | |
| 1651 | 2161 | if ( isset( $panel_settings['sections'] ) ) { |
| 1652 | 2162 | foreach ( $panel_settings['sections'] as $section_id => $section_settings ) { |
| 1653 | 2163 | if ( isset( $section_settings['options'] ) ) { |
| @@ -1652,10 +2162,10 @@ | ||
| 1652 | 2162 | foreach ( $panel_settings['sections'] as $section_id => $section_settings ) { |
| 1653 | 2163 | if ( isset( $section_settings['options'] ) ) { |
| 1654 | 2164 | foreach ( $section_settings['options'] as $option_id => $option ) { |
| 1655 | 2165 | $settings[ $option_id ] = $option; |
| 1656 | - if ( isset( self::$current_values[ $option_id ] ) ) { | |
| 1657 | - $settings[ $option_id ]['value'] = self::$current_values[ $option_id ]; | |
| 2166 | + if ( isset( $this->current_values[ $option_id ] ) ) { | |
| 2167 | + $settings[ $option_id ]['value'] = $this->current_values[ $option_id ]; | |
| 1658 | 2168 | } |
| 1659 | 2169 | } |
| 1660 | 2170 | } |
| 1661 | 2171 | } |
| @@ -1662,15 +2172,15 @@ | ||
| 1662 | 2172 | } |
| 1663 | 2173 | } |
| 1664 | 2174 | } |
| 1665 | 2175 | |
| 1666 | - if ( isset ( self::$customizer_config['sections'] ) ) { | |
| 1667 | - foreach ( self::$customizer_config['sections'] as $section_id => $section_settings ) { | |
| 2176 | + if ( isset ( $this->customizer_config['sections'] ) ) { | |
| 2177 | + foreach ( $this->customizer_config['sections'] as $section_id => $section_settings ) { | |
| 1668 | 2178 | if ( isset( $section_settings['options'] ) ) { |
| 1669 | 2179 | foreach ( $section_settings['options'] as $option_id => $option ) { |
| 1670 | 2180 | $settings[ $option_id ] = $option; |
| 1671 | - if ( isset( self::$current_values[ $option_id ] ) ) { | |
| 1672 | - $settings[ $option_id ]['value'] = self::$current_values[ $option_id ]; | |
| 2181 | + if ( isset( $this->current_values[ $option_id ] ) ) { | |
| 2182 | + $settings[ $option_id ]['value'] = $this->current_values[ $option_id ]; | |
| 1673 | 2183 | } |
| 1674 | 2184 | } |
| 1675 | 2185 | } |
| 1676 | 2186 | } |
| @@ -1678,30 +2188,50 @@ | ||
| 1678 | 2188 | |
| 1679 | 2189 | return $settings; |
| 1680 | 2190 | } |
| 1681 | 2191 | |
| 1682 | - protected static function get_value( $option ) { | |
| 2192 | + protected function get_value( $option_id, $alt_opt_name ) { | |
| 1683 | 2193 | global $wp_customize; |
| 1684 | 2194 | |
| 1685 | - if ( ! empty( $wp_customize ) && method_exists( $wp_customize, 'get_setting') ) { | |
| 2195 | + $options_name = $this->opt_name; | |
| 2196 | + $values = $this->current_values; | |
| 1686 | 2197 | |
| 1687 | - $option_key = self::$opt_name . '[' . $option . ']'; | |
| 1688 | - $setting = $wp_customize->get_setting( $option_key ); | |
| 2198 | + // In case someone asked for a DB value too early but it has given us the options_name under which to search, let's do it | |
| 2199 | + if ( empty( $options_name ) && ! empty( $alt_opt_name ) ) { | |
| 2200 | + $options_name = $alt_opt_name; | |
| 2201 | + $values = $this->get_current_values( $options_name ); | |
| 2202 | + } | |
| 2203 | + | |
| 2204 | + if ( ! empty( $wp_customize ) && method_exists( $wp_customize, 'get_setting' ) ) { | |
| 2205 | + // Get the field config. | |
| 2206 | + $option_config = $this->get_option_customizer_config( $option_id ); | |
| 2207 | + | |
| 2208 | + if ( empty( $option_id ) || ! isset( $option_config['type'] ) ) { | |
| 2209 | + return null; | |
| 2210 | + } | |
| 2211 | + | |
| 2212 | + // If we have been explicitly given a setting ID we will use that | |
| 2213 | + if ( ! empty( $option_config['setting_id'] ) ) { | |
| 2214 | + $setting_id = $option_config['setting_id']; | |
| 2215 | + } else { | |
| 2216 | + $setting_id = $options_name . '[' . $option_id . ']'; | |
| 2217 | + } | |
| 2218 | + | |
| 2219 | + $setting = $wp_customize->get_setting( $setting_id ); | |
| 1689 | 2220 | if ( ! empty( $setting ) ) { |
| 1690 | - $value = $setting->value(); | |
| 1691 | - return $value; | |
| 2221 | + return $setting->value(); | |
| 1692 | 2222 | } |
| 1693 | 2223 | } |
| 1694 | 2224 | |
| 1695 | 2225 | // shim |
| 1696 | - if ( strpos( $option, self::$opt_name . '[' ) !== false ) { | |
| 2226 | + if ( strpos( $option_id, $options_name . '[' ) !== false ) { | |
| 1697 | 2227 | // get only the setting id |
| 1698 | - $option = explode( '[', $option ); | |
| 1699 | - $option = rtrim( $option[1], ']' ); | |
| 2228 | + $option_id = explode( '[', $option_id ); | |
| 2229 | + $option_id = rtrim( $option_id[1], ']' ); | |
| 1700 | 2230 | } |
| 1701 | 2231 | |
| 1702 | - if ( isset( self::$current_values[ $option ] ) ) { | |
| 1703 | - return self::$current_values[ $option ]; | |
| 2232 | + if ( isset( $values[ $option_id ] ) ) { | |
| 2233 | + return $values[ $option_id ]; | |
| 1704 | 2234 | } |
| 1705 | 2235 | |
| 1706 | 2236 | return null; |
| 1707 | 2237 | } |
| @@ -1706,35 +2236,61 @@ | ||
| 1706 | 2236 | return null; |
| 1707 | 2237 | } |
| 1708 | 2238 | |
| 1709 | 2239 | /** |
| 1710 | - * A public function to retreat an option's value | |
| 1711 | - * If there is a value and return it | |
| 1712 | - * Otherwise try to get the default parameter or the default from config | |
| 2240 | + * A public function to get an option's value. | |
| 2241 | + * If there is a value and return it. | |
| 2242 | + * Otherwise try to get the default parameter or the default from config. | |
| 1713 | 2243 | * |
| 1714 | - * @param $option | |
| 1715 | - * @param null $default | |
| 2244 | + * @param $option_id | |
| 2245 | + * @param mixed $default Optional. | |
| 2246 | + * @param string $alt_opt_name Optional. We can use this to bypass the fact that the DB values are loaded on after_setup_theme, if we know what to look for. | |
| 1716 | 2247 | * |
| 1717 | 2248 | * @return bool|null|string |
| 1718 | 2249 | */ |
| 1719 | - static function get_option( $option, $default = null ) { | |
| 2250 | + public function get_option( $option_id, $default = null, $alt_opt_name = null ) { | |
| 2251 | + // If the development constant CUSTOMIFY_DEV_FORCE_DEFAULTS has been defined we will not retrieve anything from the database | |
| 2252 | + // Always go with the default | |
| 2253 | + if ( defined( 'CUSTOMIFY_DEV_FORCE_DEFAULTS' ) && true === CUSTOMIFY_DEV_FORCE_DEFAULTS && ! $this->skip_dev_mode_force_defaults( $option_id ) ) { | |
| 2254 | + $return = null; | |
| 2255 | + } else { | |
| 2256 | + // Get the field config. | |
| 2257 | + $option_config = $this->get_option_customizer_config( $option_id ); | |
| 1720 | 2258 | |
| 1721 | - $return = self::get_value( $option ); | |
| 2259 | + if ( empty( $option_id ) ) { | |
| 2260 | + $return = null; | |
| 2261 | + } elseif ( isset( $option_config['setting_type'] ) && $option_config['setting_type'] === 'option' ) { | |
| 2262 | + // We have a setting that is saved in the wp_options table, not in theme_mods. | |
| 2263 | + // We will fetch it directly. | |
| 1722 | 2264 | |
| 2265 | + // If we have been explicitly given a setting ID we will use that | |
| 2266 | + if ( ! empty( $option_config['setting_id'] ) ) { | |
| 2267 | + $setting_id = $option_config['setting_id']; | |
| 2268 | + } else { | |
| 2269 | + $setting_id = $this->opt_name . '[' . $option_id . ']'; | |
| 2270 | + } | |
| 2271 | + | |
| 2272 | + $return = get_option( $setting_id, null ); | |
| 2273 | + } else { | |
| 2274 | + // Get the value stores in theme_mods. | |
| 2275 | + $return = $this->get_value( $option_id, $alt_opt_name ); | |
| 2276 | + } | |
| 2277 | + } | |
| 2278 | + | |
| 1723 | 2279 | if ( $return !== null ) { |
| 1724 | 2280 | return $return; |
| 1725 | 2281 | } elseif ( $default !== null ) { |
| 1726 | 2282 | return $default; |
| 1727 | - } elseif ( isset( self::$options_list[ $option ] ) && isset( self::$options_list[ $option ]['default'] ) ) { | |
| 1728 | - return self::$options_list[ $option ]['default']; | |
| 2283 | + } elseif ( isset( $this->options_list[ $option_id ] ) && isset( $this->options_list[ $option_id ]['default'] ) ) { | |
| 2284 | + return $this->options_list[ $option_id ]['default']; | |
| 1729 | 2285 | } |
| 1730 | 2286 | |
| 1731 | 2287 | return null; |
| 1732 | 2288 | } |
| 1733 | 2289 | |
| 1734 | - static function has_option( $option ) { | |
| 2290 | + public function has_option( $option ) { | |
| 1735 | 2291 | |
| 1736 | - if ( isset( self::$options_list[ $option ] ) ) { | |
| 2292 | + if ( isset( $this->options_list[ $option ] ) ) { | |
| 1737 | 2293 | return true; |
| 1738 | 2294 | } |
| 1739 | 2295 | |
| 1740 | 2296 | return false; |
| @@ -1739,14 +2295,61 @@ | ||
| 1739 | 2295 | |
| 1740 | 2296 | return false; |
| 1741 | 2297 | } |
| 1742 | 2298 | |
| 1743 | - protected static function get_config() { | |
| 1744 | - // @TODO maybe check this | |
| 1745 | - return include 'plugin-config.php'; | |
| 2299 | + public function get_customizer_config() { | |
| 2300 | + return $this->customizer_config; | |
| 1746 | 2301 | } |
| 1747 | 2302 | |
| 1748 | 2303 | /** |
| 2304 | + * Get the Customify configuration of a certain option. | |
| 2305 | + * | |
| 2306 | + * @param string $option_id | |
| 2307 | + * | |
| 2308 | + * @return array|false The option config or false on failure. | |
| 2309 | + */ | |
| 2310 | + public function get_option_customizer_config( $option_id ) { | |
| 2311 | + // We need to search for the option configured under the given id (the array key) | |
| 2312 | + if ( isset ( $this->customizer_config['panels'] ) ) { | |
| 2313 | + foreach ( $this->customizer_config['panels'] as $panel_id => $panel_settings ) { | |
| 2314 | + if ( isset( $panel_settings['sections'] ) ) { | |
| 2315 | + foreach ( $panel_settings['sections'] as $section_id => $section_settings ) { | |
| 2316 | + if ( isset( $section_settings['options'] ) ) { | |
| 2317 | + foreach ( $section_settings['options'] as $id => $option_config ) { | |
| 2318 | + if ( $id === $option_id ) { | |
| 2319 | + return $option_config; | |
| 2320 | + } | |
| 2321 | + } | |
| 2322 | + } | |
| 2323 | + } | |
| 2324 | + } | |
| 2325 | + } | |
| 2326 | + } | |
| 2327 | + | |
| 2328 | + if ( isset ( $this->customizer_config['sections'] ) ) { | |
| 2329 | + foreach ( $this->customizer_config['sections'] as $section_id => $section_settings ) { | |
| 2330 | + if ( isset( $section_settings['options'] ) ) { | |
| 2331 | + foreach ( $section_settings['options'] as $id => $option_config ) { | |
| 2332 | + if ( $id === $option_id ) { | |
| 2333 | + return $option_config; | |
| 2334 | + } | |
| 2335 | + } | |
| 2336 | + } | |
| 2337 | + } | |
| 2338 | + } | |
| 2339 | + | |
| 2340 | + return false; | |
| 2341 | + } | |
| 2342 | + | |
| 2343 | + protected function get_config() { | |
| 2344 | + if ( file_exists( $this->get_base_path() . 'plugin-config.php' ) ) { | |
| 2345 | + return include( $this->get_base_path() . 'plugin-config.php' ); | |
| 2346 | + } | |
| 2347 | + | |
| 2348 | + return false; | |
| 2349 | + } | |
| 2350 | + | |
| 2351 | + /** | |
| 1749 | 2352 | * Get an option's value from the config file |
| 1750 | 2353 | * |
| 1751 | 2354 | * @param $option |
| 1752 | 2355 | * @param null $default |
| @@ -1752,12 +2355,12 @@ | ||
| 1752 | 2355 | * @param null $default |
| 1753 | 2356 | * |
| 1754 | 2357 | * @return bool|null |
| 1755 | 2358 | */ |
| 1756 | - public static function get_config_option( $option, $default = null ) { | |
| 2359 | + public function get_config_option( $option, $default = null ) { | |
| 1757 | 2360 | |
| 1758 | - if ( isset( self::$config[ $option ] ) ) { | |
| 1759 | - return self::$config[ $option ]; | |
| 2361 | + if ( isset( $this->config[ $option ] ) ) { | |
| 2362 | + return $this->config[ $option ]; | |
| 1760 | 2363 | } elseif ( $default !== null ) { |
| 1761 | 2364 | return $default; |
| 1762 | 2365 | } |
| 1763 | 2366 | |
| @@ -1763,12 +2366,12 @@ | ||
| 1763 | 2366 | |
| 1764 | 2367 | return false; |
| 1765 | 2368 | } |
| 1766 | 2369 | |
| 1767 | - static function get_plugin_option( $option, $default = null ) { | |
| 2370 | + public function get_plugin_setting( $option, $default = null ) { | |
| 1768 | 2371 | |
| 1769 | - if ( isset( self::$plugin_settings[ $option ] ) ) { | |
| 1770 | - return self::$plugin_settings[ $option ]; | |
| 2372 | + if ( isset( $this->plugin_settings[ $option ] ) ) { | |
| 2373 | + return $this->plugin_settings[ $option ]; | |
| 1771 | 2374 | } elseif ( $default !== null ) { |
| 1772 | 2375 | return $default; |
| 1773 | 2376 | } |
| 1774 | 2377 | |
| @@ -1800,9 +2403,9 @@ | ||
| 1800 | 2403 | * @param array $array |
| 1801 | 2404 | * |
| 1802 | 2405 | * @return bool |
| 1803 | 2406 | */ |
| 1804 | - public static function is_assoc( $array ) { | |
| 2407 | + public function is_assoc( $array ) { | |
| 1805 | 2408 | |
| 1806 | 2409 | if ( ! is_array( $array ) ) { |
| 1807 | 2410 | return false; |
| 1808 | 2411 | } |
| @@ -1814,17 +2417,17 @@ | ||
| 1814 | 2417 | // not be associative (e.g. the keys array looked like {0:0, 1:1...}). |
| 1815 | 2418 | return array_keys( $keys ) !== $keys; |
| 1816 | 2419 | } |
| 1817 | 2420 | |
| 1818 | - function register_import_api(){ | |
| 2421 | + function register_import_api() { | |
| 1819 | 2422 | |
| 1820 | - include_once( self::get_base_path() . '/features/class-Customify_Importer.php' ); | |
| 2423 | + include_once( $this->get_base_path() . '/features/class-Customify_Importer.php' ); | |
| 1821 | 2424 | $controller = new Customify_Importer_Controller(); |
| 1822 | 2425 | $controller->init(); |
| 1823 | 2426 | } |
| 1824 | 2427 | |
| 1825 | - function get_options_configs () { | |
| 1826 | - return self::$options_list; | |
| 2428 | + public function get_options_configs() { | |
| 2429 | + return $this->options_list; | |
| 1827 | 2430 | } |
| 1828 | 2431 | |
| 1829 | 2432 | /** |
| 1830 | 2433 | * Does the same thing the JS encodeURIComponent() does |
| @@ -1839,9 +2442,10 @@ | ||
| 1839 | 2442 | $revert = array( '%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')' ); |
| 1840 | 2443 | |
| 1841 | 2444 | $str = strtr( rawurlencode( $str ), $revert ); |
| 1842 | 2445 | } else { |
| 1843 | - var_dump('boooom');die; | |
| 2446 | + var_dump( 'boooom' ); | |
| 2447 | + die; | |
| 1844 | 2448 | } |
| 1845 | 2449 | |
| 1846 | 2450 | return $str; |
| 1847 | 2451 | } |
| @@ -1853,13 +2457,157 @@ | ||
| 1853 | 2457 | * |
| 1854 | 2458 | * @return string |
| 1855 | 2459 | */ |
| 1856 | 2460 | public static function decodeURIComponent( $str ) { |
| 1857 | - //if we get an array we just let it be | |
| 2461 | + // If we get an array we just let it be | |
| 1858 | 2462 | if ( is_string( $str ) ) { |
| 1859 | 2463 | $revert = array( '!' => '%21', '*' => '%2A', "'" => '%27', '(' => '%28', ')' => '%29' ); |
| 1860 | - $str = rawurldecode( strtr( $str, $revert ) ); | |
| 2464 | + $str = rawurldecode( strtr( $str, $revert ) ); | |
| 1861 | 2465 | } |
| 1862 | 2466 | |
| 1863 | 2467 | return $str; |
| 1864 | 2468 | } |
| 2469 | + | |
| 2470 | + /** | |
| 2471 | + * PHP version check | |
| 2472 | + */ | |
| 2473 | + protected function php_version_check() { | |
| 2474 | + | |
| 2475 | + if ( version_compare( phpversion(), $this->minimalRequiredPhpVersion ) < 0 ) { | |
| 2476 | + add_action( 'admin_notices', array( $this, 'notice_php_version_wrong' ) ); | |
| 2477 | + | |
| 2478 | + return false; | |
| 2479 | + } | |
| 2480 | + | |
| 2481 | + return true; | |
| 2482 | + } | |
| 2483 | + | |
| 2484 | + /** | |
| 2485 | + * Prevent saving of plugin options in the Customizer | |
| 2486 | + * | |
| 2487 | + * @param array $data The data to save | |
| 2488 | + * @param array $filter_context | |
| 2489 | + * | |
| 2490 | + * @return array | |
| 2491 | + */ | |
| 2492 | + public function prevent_changeset_save_in_devmode( $data, $filter_context ) { | |
| 2493 | + // Get the options key | |
| 2494 | + $options_key = $this->customizer_config['opt-name']; | |
| 2495 | + if ( ! empty( $options_key ) ) { | |
| 2496 | + // Remove any Customify data thus preventing it from saving | |
| 2497 | + foreach ( $data as $option_id => $value ) { | |
| 2498 | + if ( false !== strpos( $option_id, $options_key ) && ! $this->skip_dev_mode_force_defaults( $option_id ) ) { | |
| 2499 | + unset( $data[ $option_id ] ); | |
| 2500 | + } | |
| 2501 | + } | |
| 2502 | + } | |
| 2503 | + | |
| 2504 | + return $data; | |
| 2505 | + } | |
| 2506 | + | |
| 2507 | + /** | |
| 2508 | + * Determine if we should NOT enforce the CUSTOMIFY_DEV_FORCE_DEFAULTS behavior on a certain option. | |
| 2509 | + * | |
| 2510 | + * @param string $option_id | |
| 2511 | + * | |
| 2512 | + * @return bool | |
| 2513 | + */ | |
| 2514 | + private function skip_dev_mode_force_defaults( $option_id ) { | |
| 2515 | + // Preprocess the $option_id. | |
| 2516 | + if ( false !== strpos( $option_id, '::' ) ) { | |
| 2517 | + $option_id = substr( $option_id, strpos( $option_id, '::' ) + 2 ); | |
| 2518 | + } | |
| 2519 | + if ( false !== strpos( $option_id, '[' ) ) { | |
| 2520 | + $option_id = explode( '[', $option_id ); | |
| 2521 | + $option_id = rtrim( $option_id[1], ']' ); | |
| 2522 | + } | |
| 2523 | + | |
| 2524 | + $option_config = $this->get_option_customizer_config( $option_id ); | |
| 2525 | + if ( empty( $option_config ) ) { | |
| 2526 | + return false; | |
| 2527 | + } | |
| 2528 | + | |
| 2529 | + // We will skip certain field types that generally don't have a default value. | |
| 2530 | + if ( ! empty( $option_config['type'] ) ) { | |
| 2531 | + switch ( $option_config['type'] ) { | |
| 2532 | + case 'cropped_image': | |
| 2533 | + case 'cropped_media': | |
| 2534 | + case 'image': | |
| 2535 | + case 'media': | |
| 2536 | + case 'custom_background': | |
| 2537 | + case 'upload': | |
| 2538 | + return true; | |
| 2539 | + break; | |
| 2540 | + default: | |
| 2541 | + break; | |
| 2542 | + } | |
| 2543 | + } | |
| 2544 | + | |
| 2545 | + return false; | |
| 2546 | + } | |
| 2547 | + | |
| 2548 | + public function prevent_changeset_save_in_devmode_notification() { ?> | |
| 2549 | + <script type="application/javascript"> | |
| 2550 | + (function ( $, exports, wp ) { | |
| 2551 | + 'use strict'; | |
| 2552 | + // when the customizer is ready add our notification | |
| 2553 | + wp.customize.bind('ready', function () { | |
| 2554 | + wp.customize.notifications.add( 'customify_force_defaults', new wp.customize.Notification( | |
| 2555 | + 'customify_force_defaults', | |
| 2556 | + { | |
| 2557 | + type: 'warning', | |
| 2558 | + message: '<strong style="margin-bottom: ">Customify: Development Mode</strong><p>All the options are switched to default. While they are changing in the live preview, they will not be kept when publish.</p>' | |
| 2559 | + } | |
| 2560 | + ) ); | |
| 2561 | + }); | |
| 2562 | + })(jQuery, window, wp); | |
| 2563 | + </script> | |
| 2564 | + <?php } | |
| 2565 | + | |
| 2566 | + /** | |
| 2567 | + * Return an instance of this class. | |
| 2568 | + * @since 1.0.0 | |
| 2569 | + * @return PixCustomifyPlugin A single instance of this class. | |
| 2570 | + */ | |
| 2571 | + /** | |
| 2572 | + * Main PixCustomifyPlugin Instance | |
| 2573 | + * | |
| 2574 | + * Ensures only one instance of PixCustomifyPlugin is loaded or can be loaded. | |
| 2575 | + * | |
| 2576 | + * @since 1.0.0 | |
| 2577 | + * @static | |
| 2578 | + * | |
| 2579 | + * @param string $file File. | |
| 2580 | + * @param string $version Version. | |
| 2581 | + * | |
| 2582 | + * @see PixCustomifyPlugin() | |
| 2583 | + * @return PixCustomifyPlugin Main PixCustomifyPlugin instance | |
| 2584 | + */ | |
| 2585 | + public static function instance( $file = '', $version = '1.0.0' ) { | |
| 2586 | + // If the single instance hasn't been set, set it now. | |
| 2587 | + if ( is_null( self::$_instance ) ) { | |
| 2588 | + self::$_instance = new self( $file, $version ); | |
| 2589 | + } | |
| 2590 | + | |
| 2591 | + return self::$_instance; | |
| 2592 | + } | |
| 2593 | + | |
| 2594 | + /** | |
| 2595 | + * Cloning is forbidden. | |
| 2596 | + * | |
| 2597 | + * @since 1.5.0 | |
| 2598 | + */ | |
| 2599 | + public function __clone() { | |
| 2600 | + | |
| 2601 | + _doing_it_wrong( __FUNCTION__, esc_html( __( 'Cheatin’ huh?' ) ), esc_html( $this->_version ) ); | |
| 2602 | + } // End __clone () | |
| 2603 | + | |
| 2604 | + /** | |
| 2605 | + * Unserializing instances of this class is forbidden. | |
| 2606 | + * | |
| 2607 | + * @since 1.5.0 | |
| 2608 | + */ | |
| 2609 | + public function __wakeup() { | |
| 2610 | + | |
| 2611 | + _doing_it_wrong( __FUNCTION__, esc_html( __( 'Cheatin’ huh?' ) ), esc_html( $this->_version ) ); | |
| 2612 | + } // End __wakeup () | |
| 1865 | 2613 | } |