web/wp-includes/class-wp-customize-manager.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * Customize
     3  * Customize Manager.
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Customize
     6  * @subpackage Customize
     7  * @since 3.4.0
     7  * @since 3.4.0
     8  */
     8  */
     9 
       
    10 final class WP_Customize_Manager {
     9 final class WP_Customize_Manager {
    11 	protected $theme;
    10 	protected $theme;
    12 	protected $original_stylesheet;
    11 	protected $original_stylesheet;
    13 
    12 
    14 	protected $previewing = false;
    13 	protected $previewing = false;
    54 		add_action( 'customize_register',                 array( $this, 'register_controls' ) );
    53 		add_action( 'customize_register',                 array( $this, 'register_controls' ) );
    55 		add_action( 'customize_controls_init',            array( $this, 'prepare_controls' ) );
    54 		add_action( 'customize_controls_init',            array( $this, 'prepare_controls' ) );
    56 		add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
    55 		add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
    57 	}
    56 	}
    58 
    57 
    59  	/**
    58 	/**
    60 	 * Return true if it's an AJAX request.
    59 	 * Return true if it's an AJAX request.
    61 	 *
    60 	 *
    62 	 * @since 3.4.0
    61 	 * @since 3.4.0
       
    62 	 *
       
    63 	 * @return bool
    63 	 */
    64 	 */
    64 	public function doing_ajax() {
    65 	public function doing_ajax() {
    65 		return isset( $_POST['customized'] ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX );
    66 		return isset( $_POST['customized'] ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX );
    66 	}
    67 	}
    67 
    68 
    68 	/**
    69 	/**
    69 	 * Custom wp_die wrapper. Returns either the standard message for UI
    70 	 * Custom wp_die wrapper. Returns either the standard message for UI
    70 	 * or the AJAX message.
    71 	 * or the AJAX message.
    71 	 *
    72 	 *
    72 	 * @param  mixed $ajax_message AJAX return
    73 	 * @since 3.4.0
    73 	 * @param  mixed $message      UI message
    74 	 *
    74 	 *
    75 	 * @param mixed $ajax_message AJAX return
    75 	 * @since 3.4.0
    76 	 * @param mixed $message UI message
    76 	 */
    77 	 */
    77 	protected function wp_die( $ajax_message, $message = null ) {
    78 	protected function wp_die( $ajax_message, $message = null ) {
    78 		if ( $this->doing_ajax() )
    79 		if ( $this->doing_ajax() )
    79 			wp_die( $ajax_message );
    80 			wp_die( $ajax_message );
    80 
    81 
    86 
    87 
    87 	/**
    88 	/**
    88 	 * Return the AJAX wp_die() handler if it's a customized request.
    89 	 * Return the AJAX wp_die() handler if it's a customized request.
    89 	 *
    90 	 *
    90 	 * @since 3.4.0
    91 	 * @since 3.4.0
       
    92 	 *
       
    93 	 * @return string
    91 	 */
    94 	 */
    92 	public function wp_die_handler() {
    95 	public function wp_die_handler() {
    93 		if ( $this->doing_ajax() )
    96 		if ( $this->doing_ajax() )
    94 			return '_ajax_wp_die_handler';
    97 			return '_ajax_wp_die_handler';
    95 
    98 
    96 		return '_default_wp_die_handler';
    99 		return '_default_wp_die_handler';
    97 	}
   100 	}
    98 	/**
   101 
    99 	* Start preview and customize theme.
   102 	/**
   100 	*
   103 	 * Start preview and customize theme.
   101 	* Check if customize query variable exist. Init filters to filter the current theme.
   104 	 *
       
   105 	 * Check if customize query variable exist. Init filters to filter the current theme.
   102 	 *
   106 	 *
   103 	 * @since 3.4.0
   107 	 * @since 3.4.0
   104 	 */
   108 	 */
   105 	public function setup_theme() {
   109 	public function setup_theme() {
   106 		send_origin_headers();
   110 		send_origin_headers();
   135 		}
   139 		}
   136 
   140 
   137 		$this->start_previewing_theme();
   141 		$this->start_previewing_theme();
   138 	}
   142 	}
   139 
   143 
       
   144 	/**
       
   145 	 * Callback to validate a theme once it is loaded
       
   146 	 *
       
   147 	 * @since 3.4.0
       
   148 	 */
   140 	function after_setup_theme() {
   149 	function after_setup_theme() {
   141 		if ( ! $this->doing_ajax() && ! validate_current_theme() ) {
   150 		if ( ! $this->doing_ajax() && ! validate_current_theme() ) {
   142 			wp_redirect( 'themes.php?broken=true' );
   151 			wp_redirect( 'themes.php?broken=true' );
   143 			exit;
   152 			exit;
   144 		}
   153 		}
   277 	 * by issuing a 200 response instead of a 30x.
   286 	 * by issuing a 200 response instead of a 30x.
   278 	 *
   287 	 *
   279 	 * Instead, the JS will sniff out the location header.
   288 	 * Instead, the JS will sniff out the location header.
   280 	 *
   289 	 *
   281 	 * @since 3.4.0
   290 	 * @since 3.4.0
       
   291 	 *
       
   292 	 * @param $status
       
   293 	 * @return int
   282 	 */
   294 	 */
   283 	public function wp_redirect_status( $status ) {
   295 	public function wp_redirect_status( $status ) {
   284 		if ( $this->is_preview() && ! is_admin() )
   296 		if ( $this->is_preview() && ! is_admin() )
   285 			return 200;
   297 			return 200;
   286 
   298 
   289 
   301 
   290 	/**
   302 	/**
   291 	 * Decode the $_POST attribute used to override the WP_Customize_Setting values.
   303 	 * Decode the $_POST attribute used to override the WP_Customize_Setting values.
   292 	 *
   304 	 *
   293 	 * @since 3.4.0
   305 	 * @since 3.4.0
       
   306 	 *
       
   307 	 * @param mixed $setting A WP_Customize_Setting derived object
       
   308 	 * @return string Sanitized attribute
   294 	 */
   309 	 */
   295 	public function post_value( $setting ) {
   310 	public function post_value( $setting ) {
   296 		if ( ! isset( $this->_post_values ) ) {
   311 		if ( ! isset( $this->_post_values ) ) {
   297 			if ( isset( $_POST['customized'] ) )
   312 			if ( isset( $_POST['customized'] ) )
   298 				$this->_post_values = json_decode( stripslashes( $_POST['customized'] ), true );
   313 				$this->_post_values = json_decode( stripslashes( $_POST['customized'] ), true );
   365 			'values'  => array(),
   380 			'values'  => array(),
   366 			'channel' => esc_js( $_POST['customize_messenger_channel'] ),
   381 			'channel' => esc_js( $_POST['customize_messenger_channel'] ),
   367 		);
   382 		);
   368 
   383 
   369 		if ( 2 == $this->nonce_tick ) {
   384 		if ( 2 == $this->nonce_tick ) {
   370  			$settings['nonce'] = array(
   385 			$settings['nonce'] = array(
   371  				'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
   386 				'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
   372  				'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() )
   387 				'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() )
   373  			);
   388 			);
   374  		}
   389 		}
   375 
   390 
   376 		foreach ( $this->settings as $id => $setting ) {
   391 		foreach ( $this->settings as $id => $setting ) {
   377 			$settings['values'][ $id ] = $setting->js_value();
   392 			$settings['values'][ $id ] = $setting->js_value();
   378 		}
   393 		}
   379 
   394 
   462 	/**
   477 	/**
   463 	 * Filter the current theme and return the name of the previewed theme.
   478 	 * Filter the current theme and return the name of the previewed theme.
   464 	 *
   479 	 *
   465 	 * @since 3.4.0
   480 	 * @since 3.4.0
   466 	 *
   481 	 *
       
   482 	 * @param $current_theme {@internal Parameter is not used}
   467 	 * @return string Theme name.
   483 	 * @return string Theme name.
   468 	 */
   484 	 */
   469 	public function current_theme( $current_theme ) {
   485 	public function current_theme( $current_theme ) {
   470 		return $this->theme()->display('Name');
   486 		return $this->theme()->display('Name');
   471 	}
   487 	}
   484 		// Do we have to switch themes?
   500 		// Do we have to switch themes?
   485 		if ( ! $this->is_theme_active() ) {
   501 		if ( ! $this->is_theme_active() ) {
   486 			// Temporarily stop previewing the theme to allow switch_themes()
   502 			// Temporarily stop previewing the theme to allow switch_themes()
   487 			// to operate properly.
   503 			// to operate properly.
   488 			$this->stop_previewing_theme();
   504 			$this->stop_previewing_theme();
   489 			switch_theme( $this->get_template(), $this->get_stylesheet() );
   505 			switch_theme( $this->get_stylesheet() );
   490 			$this->start_previewing_theme();
   506 			$this->start_previewing_theme();
   491 		}
   507 		}
   492 
   508 
   493 		do_action( 'customize_save', $this );
   509 		do_action( 'customize_save', $this );
   494 
   510 
   628 	 *
   644 	 *
   629 	 * @since 3.4.0
   645 	 * @since 3.4.0
   630 	 *
   646 	 *
   631 	 * @param object $a Object A.
   647 	 * @param object $a Object A.
   632 	 * @param object $b Object B.
   648 	 * @param object $b Object B.
       
   649 	 * @return int
   633 	 */
   650 	 */
   634 	protected final function _cmp_priority( $a, $b ) {
   651 	protected final function _cmp_priority( $a, $b ) {
   635 		$ap = $a->priority;
   652 		$ap = $a->priority;
   636 		$bp = $b->priority;
   653 		$bp = $b->priority;
   637 
   654 
   958 	 * Callback for validating the header_textcolor value.
   975 	 * Callback for validating the header_textcolor value.
   959 	 *
   976 	 *
   960 	 * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
   977 	 * Accepts 'blank', and otherwise uses sanitize_hex_color_no_hash().
   961 	 *
   978 	 *
   962 	 * @since 3.4.0
   979 	 * @since 3.4.0
       
   980 	 *
       
   981 	 * @param string $color
       
   982 	 * @return string
   963 	 */
   983 	 */
   964 	public function _sanitize_header_textcolor( $color ) {
   984 	public function _sanitize_header_textcolor( $color ) {
   965 		return ( 'blank' === $color ) ? 'blank' : sanitize_hex_color_no_hash( $color );
   985 		return ( 'blank' === $color ) ? 'blank' : sanitize_hex_color_no_hash( $color );
   966 	}
   986 	}
   967 };
   987 };
   971  *
   991  *
   972  * Returns either '', a 3 or 6 digit hex color (with #), or null.
   992  * Returns either '', a 3 or 6 digit hex color (with #), or null.
   973  * For validating values without a #, see sanitize_hex_color_no_hash().
   993  * For validating values without a #, see sanitize_hex_color_no_hash().
   974  *
   994  *
   975  * @since 3.4.0
   995  * @since 3.4.0
       
   996  *
       
   997  * @param string $color
       
   998  * @return string|null
   976  */
   999  */
   977 function sanitize_hex_color( $color ) {
  1000 function sanitize_hex_color( $color ) {
   978 	if ( '' === $color )
  1001 	if ( '' === $color )
   979 		return '';
  1002 		return '';
   980 
  1003 
   993  * rgba, hsl, rgb, and html color names.
  1016  * rgba, hsl, rgb, and html color names.
   994  *
  1017  *
   995  * Returns either '', a 3 or 6 digit hex color (without a #), or null.
  1018  * Returns either '', a 3 or 6 digit hex color (without a #), or null.
   996  *
  1019  *
   997  * @since 3.4.0
  1020  * @since 3.4.0
       
  1021  * @uses sanitize_hex_color()
       
  1022  *
       
  1023  * @param string $color
       
  1024  * @return string|null
   998  */
  1025  */
   999 function sanitize_hex_color_no_hash( $color ) {
  1026 function sanitize_hex_color_no_hash( $color ) {
  1000 	$color = ltrim( $color, '#' );
  1027 	$color = ltrim( $color, '#' );
  1001 
  1028 
  1002 	if ( '' === $color )
  1029 	if ( '' === $color )
  1010  * Otherwise, returns value untouched.
  1037  * Otherwise, returns value untouched.
  1011  *
  1038  *
  1012  * This method should only be necessary if using sanitize_hex_color_no_hash().
  1039  * This method should only be necessary if using sanitize_hex_color_no_hash().
  1013  *
  1040  *
  1014  * @since 3.4.0
  1041  * @since 3.4.0
       
  1042  *
       
  1043  * @param string $color
       
  1044  * @return string
  1015  */
  1045  */
  1016 function maybe_hash_hex_color( $color ) {
  1046 function maybe_hash_hex_color( $color ) {
  1017 	if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
  1047 	if ( $unhashed = sanitize_hex_color_no_hash( $color ) )
  1018 		return '#' . $unhashed;
  1048 		return '#' . $unhashed;
  1019 
  1049