]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/class-wp-customize-manager.php
WordPress 4.1.4-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp-customize-manager.php
index 4465a80545c5bfcef01059d320dd6300b2b43921..c14d61652ab28e114cb7b66d8e57b1e6f0d4125d 100644 (file)
@@ -37,7 +37,7 @@ final class WP_Customize_Manager {
        protected $previewing = false;
 
        /**
-        * Methods and properties deailing with managing widgets in the customizer.
+        * Methods and properties deailing with managing widgets in the Customizer.
         *
         * @var WP_Customize_Widgets
         */
@@ -54,10 +54,19 @@ final class WP_Customize_Manager {
        protected $customized;
 
        /**
-        * $_POST values for Customize Settings.
+        * Controls that may be rendered from JS templates.
         *
+        * @since 4.1.0
+        * @access protected
         * @var array
         */
+       protected $registered_control_types = array();
+
+       /**
+        * Unsanitized values for Customize Settings parsed from $_POST['customized'].
+        *
+        * @var array|false
+        */
        private $_post_values;
 
        /**
@@ -66,11 +75,11 @@ final class WP_Customize_Manager {
         * @since 3.4.0
         */
        public function __construct() {
-               require( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
-               require( ABSPATH . WPINC . '/class-wp-customize-panel.php' );
-               require( ABSPATH . WPINC . '/class-wp-customize-section.php' );
-               require( ABSPATH . WPINC . '/class-wp-customize-control.php' );
-               require( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
+               require_once( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
+               require_once( ABSPATH . WPINC . '/class-wp-customize-panel.php' );
+               require_once( ABSPATH . WPINC . '/class-wp-customize-section.php' );
+               require_once( ABSPATH . WPINC . '/class-wp-customize-control.php' );
+               require_once( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
 
                $this->widgets = new WP_Customize_Widgets( $this );
 
@@ -82,7 +91,7 @@ final class WP_Customize_Manager {
                // Run wp_redirect_status late to make sure we override the status last.
                add_action( 'wp_redirect_status', array( $this, 'wp_redirect_status' ), 1000 );
 
-               // Do not spawn cron (especially the alternate cron) while running the customizer.
+               // Do not spawn cron (especially the alternate cron) while running the Customizer.
                remove_action( 'init', 'wp_cron' );
 
                // Do not run update checks when rendering the controls.
@@ -217,7 +226,7 @@ final class WP_Customize_Manager {
                        add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
                        add_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
 
-                       // @link: http://core.trac.wordpress.org/ticket/20027
+                       // @link: https://core.trac.wordpress.org/ticket/20027
                        add_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
                        add_filter( 'pre_option_template', array( $this, 'get_template' ) );
 
@@ -254,7 +263,7 @@ final class WP_Customize_Manager {
                        remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
                        remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
 
-                       // @link: http://core.trac.wordpress.org/ticket/20027
+                       // @link: https://core.trac.wordpress.org/ticket/20027
                        remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
                        remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
 
@@ -390,27 +399,50 @@ final class WP_Customize_Manager {
        }
 
        /**
-        * Decode the $_POST['customized'] values for a specific Customize Setting.
+        * Parse the incoming $_POST['customized'] JSON data and store the unsanitized
+        * settings for subsequent post_value() lookups.
         *
-        * @since 3.4.0
+        * @since 4.1.1
         *
-        * @param mixed $setting A WP_Customize_Setting derived object
-        * @return string $post_value Sanitized value
+        * @return array
         */
-       public function post_value( $setting ) {
+       public function unsanitized_post_values() {
                if ( ! isset( $this->_post_values ) ) {
-                       if ( isset( $_POST['customized'] ) )
+                       if ( isset( $_POST['customized'] ) ) {
                                $this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true );
-                       else
+                       }
+                       if ( empty( $this->_post_values ) ) { // if not isset or of JSON error
                                $this->_post_values = false;
+                       }
+               }
+               if ( empty( $this->_post_values ) ) {
+                       return array();
+               } else {
+                       return $this->_post_values;
                }
+       }
 
-               if ( isset( $this->_post_values[ $setting->id ] ) )
-                       return $setting->sanitize( $this->_post_values[ $setting->id ] );
+       /**
+        * Return the sanitized value for a given setting from the request's POST data.
+        *
+        * @since 3.4.0
+        * @since 4.1.1 Introduced 'default' parameter.
+        *
+        * @param WP_Customize_Setting $setting A WP_Customize_Setting derived object
+        * @param mixed $default value returned $setting has no post value (added in 4.2.0).
+        * @return string|mixed $post_value Sanitized value or the $default provided
+        */
+       public function post_value( $setting, $default = null ) {
+               $post_values = $this->unsanitized_post_values();
+               if ( array_key_exists( $setting->id, $post_values ) ) {
+                       return $setting->sanitize( $post_values[ $setting->id ] );
+               } else {
+                       return $default;
+               }
        }
 
        /**
-        * Print javascript settings.
+        * Print JavaScript settings.
         *
         * @since 3.4.0
         */
@@ -483,7 +515,7 @@ final class WP_Customize_Manager {
        }
 
        /**
-        * Print javascript settings for preview frame.
+        * Print JavaScript settings for preview frame.
         *
         * @since 3.4.0
         */
@@ -491,6 +523,8 @@ final class WP_Customize_Manager {
                $settings = array(
                        'values'  => array(),
                        'channel' => wp_unslash( $_POST['customize_messenger_channel'] ),
+                       'activePanels' => array(),
+                       'activeSections' => array(),
                        'activeControls' => array(),
                );
 
@@ -504,19 +538,28 @@ final class WP_Customize_Manager {
                foreach ( $this->settings as $id => $setting ) {
                        $settings['values'][ $id ] = $setting->js_value();
                }
+               foreach ( $this->panels as $id => $panel ) {
+                       $settings['activePanels'][ $id ] = $panel->active();
+                       foreach ( $panel->sections as $id => $section ) {
+                               $settings['activeSections'][ $id ] = $section->active();
+                       }
+               }
+               foreach ( $this->sections as $id => $section ) {
+                       $settings['activeSections'][ $id ] = $section->active();
+               }
                foreach ( $this->controls as $id => $control ) {
                        $settings['activeControls'][ $id ] = $control->active();
                }
 
                ?>
                <script type="text/javascript">
-                       var _wpCustomizeSettings = <?php echo json_encode( $settings ); ?>;
+                       var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
                </script>
                <?php
        }
 
        /**
-        * Prints a signature so we can ensure the customizer was properly executed.
+        * Prints a signature so we can ensure the Customizer was properly executed.
         *
         * @since 3.4.0
         */
@@ -525,7 +568,7 @@ final class WP_Customize_Manager {
        }
 
        /**
-        * Removes the signature in case we experience a case where the customizer was not properly executed.
+        * Removes the signature in case we experience a case where the Customizer was not properly executed.
         *
         * @since 3.4.0
         */
@@ -822,21 +865,48 @@ final class WP_Customize_Manager {
        }
 
        /**
-        * Helper function to compare two objects by priority.
+        * Register a customize control type.
+        *
+        * Registered types are eligible to be rendered via JS and created dynamically.
+        *
+        * @since 4.1.0
+        * @access public
+        *
+        * @param string $control Name of a custom control which is a subclass of
+        *                        {@see WP_Customize_Control}.
+        */
+       public function register_control_type( $control ) {
+               $this->registered_control_types[] = $control;
+       }
+
+       /**
+        * Render JS templates for all registered control types.
+        *
+        * @since 4.1.0
+        * @access public
+        */
+       public function render_control_templates() {
+               foreach ( $this->registered_control_types as $control_type ) {
+                       $control = new $control_type( $this, 'temp', array() );
+                       $control->print_template();
+               }
+       }
+
+       /**
+        * Helper function to compare two objects by priority, ensuring sort stability via instance_number.
         *
         * @since 3.4.0
         *
-        * @param object $a Object A.
-        * @param object $b Object B.
+        * @param {WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control} $a Object A.
+        * @param {WP_Customize_Panel|WP_Customize_Section|WP_Customize_Control} $b Object B.
         * @return int
         */
        protected final function _cmp_priority( $a, $b ) {
-               $ap = $a->priority;
-               $bp = $b->priority;
-
-               if ( $ap == $bp )
-                       return 0;
-               return ( $ap > $bp ) ? 1 : -1;
+               if ( $a->priority === $b->priority ) {
+                       return $a->instance_number - $a->instance_number;
+               } else {
+                       return $a->priority - $b->priority;
+               }
        }
 
        /**
@@ -850,8 +920,8 @@ final class WP_Customize_Manager {
         */
        public function prepare_controls() {
 
-               $this->controls = array_reverse( $this->controls );
                $controls = array();
+               uasort( $this->controls, array( $this, '_cmp_priority' ) );
 
                foreach ( $this->controls as $id => $control ) {
                        if ( ! isset( $this->sections[ $control->section ] ) || ! $control->check_capabilities() ) {
@@ -864,8 +934,6 @@ final class WP_Customize_Manager {
                $this->controls = $controls;
 
                // Prepare sections.
-               // Reversing makes uasort sort by time added when conflicts occur.
-               $this->sections = array_reverse( $this->sections );
                uasort( $this->sections, array( $this, '_cmp_priority' ) );
                $sections = array();
 
@@ -878,19 +946,17 @@ final class WP_Customize_Manager {
 
                        if ( ! $section->panel ) {
                                // Top-level section.
-                               $sections[] = $section;
+                               $sections[ $section->id ] = $section;
                        } else {
                                // This section belongs to a panel.
                                if ( isset( $this->panels [ $section->panel ] ) ) {
-                                       $this->panels[ $section->panel ]->sections[] = $section;
+                                       $this->panels[ $section->panel ]->sections[ $section->id ] = $section;
                                }
                        }
                }
                $this->sections = $sections;
 
                // Prepare panels.
-               // Reversing makes uasort sort by time added when conflicts occur.
-               $this->panels = array_reverse( $this->panels );
                uasort( $this->panels, array( $this, '_cmp_priority' ) );
                $panels = array();
 
@@ -899,8 +965,8 @@ final class WP_Customize_Manager {
                                continue;
                        }
 
-                       usort( $panel->sections, array( $this, '_cmp_priority' ) );
-                       $panels[] = $panel;
+                       uasort( $panel->sections, array( $this, '_cmp_priority' ) );
+                       $panels[ $panel->id ] = $panel;
                }
                $this->panels = $panels;
 
@@ -927,6 +993,12 @@ final class WP_Customize_Manager {
         */
        public function register_controls() {
 
+               /* Control Types (custom control classes) */
+               $this->register_control_type( 'WP_Customize_Color_Control' );
+               $this->register_control_type( 'WP_Customize_Upload_Control' );
+               $this->register_control_type( 'WP_Customize_Image_Control' );
+               $this->register_control_type( 'WP_Customize_Background_Image_Control' );
+
                /* Site Title & Tagline */
 
                $this->add_section( 'title_tagline', array(
@@ -1239,7 +1311,6 @@ function sanitize_hex_color( $color ) {
  * Returns either '', a 3 or 6 digit hex color (without a #), or null.
  *
  * @since 3.4.0
- * @uses sanitize_hex_color()
  *
  * @param string $color
  * @return string|null