wp/wp-admin/customize.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
    11 
    11 
    12 /** Load WordPress Administration Bootstrap */
    12 /** Load WordPress Administration Bootstrap */
    13 require_once( dirname( __FILE__ ) . '/admin.php' );
    13 require_once( dirname( __FILE__ ) . '/admin.php' );
    14 
    14 
    15 if ( ! current_user_can( 'customize' ) ) {
    15 if ( ! current_user_can( 'customize' ) ) {
    16 	wp_die( __( 'Cheatin’ uh?' ), 403 );
    16 	wp_die(
    17 }
    17 		'<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' .
    18 
    18 		'<p>' . __( 'Sorry, you are not allowed to customize this site.' ) . '</p>',
    19 wp_reset_vars( array( 'url', 'return' ) );
    19 		403
    20 $url = wp_unslash( $url );
    20 	);
    21 $url = wp_validate_redirect( $url, home_url( '/' ) );
    21 }
    22 if ( $return ) {
    22 
    23 	$return = wp_unslash( $return );
    23 /**
    24 	$return = wp_validate_redirect( $return );
    24  * @global WP_Scripts           $wp_scripts
    25 }
    25  * @global WP_Customize_Manager $wp_customize
    26 if ( ! $return ) {
    26  */
    27 	if ( $url ) {
    27 global $wp_scripts, $wp_customize;
    28 		$return = $url;
    28 
    29 	} elseif ( current_user_can( 'edit_theme_options' ) || current_user_can( 'switch_themes' ) ) {
    29 if ( $wp_customize->changeset_post_id() ) {
    30 		$return = admin_url( 'themes.php' );
    30 	$changeset_post = get_post( $wp_customize->changeset_post_id() );
    31 	} else {
    31 
    32 		$return = admin_url();
    32 	if ( ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->edit_post, $changeset_post->ID ) ) {
       
    33 		wp_die(
       
    34 			'<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' .
       
    35 			'<p>' . __( 'Sorry, you are not allowed to edit this changeset.' ) . '</p>',
       
    36 			403
       
    37 		);
    33 	}
    38 	}
    34 }
    39 
    35 
    40 	$missed_schedule = (
    36 global $wp_scripts, $wp_customize;
    41 		'future' === $changeset_post->post_status &&
       
    42 		get_post_time( 'G', true, $changeset_post ) < time()
       
    43 	);
       
    44 	if ( $missed_schedule ) {
       
    45 		/*
       
    46 		 * Note that an Ajax request spawns here instead of just calling `wp_publish_post( $changeset_post->ID )`.
       
    47 		 *
       
    48 		 * Because WP_Customize_Manager is not instantiated for customize.php with the `settings_previewed=false`
       
    49 		 * argument, settings cannot be reliably saved. Some logic short-circuits if the current value is the
       
    50 		 * same as the value being saved. This is particularly true for options via `update_option()`.
       
    51 		 *
       
    52 		 * By opening an Ajax request, this is avoided and the changeset is published. See #39221.
       
    53 		 */
       
    54 		$nonces = $wp_customize->get_nonces();
       
    55 		$request_args = array(
       
    56 			'nonce' => $nonces['save'],
       
    57 			'customize_changeset_uuid' => $wp_customize->changeset_uuid(),
       
    58 			'wp_customize' => 'on',
       
    59 			'customize_changeset_status' => 'publish',
       
    60 		);
       
    61 		ob_start();
       
    62 		?>
       
    63 		<?php wp_print_scripts( array( 'wp-util' ) ); ?>
       
    64 		<script>
       
    65 			wp.ajax.post( 'customize_save', <?php echo wp_json_encode( $request_args ); ?> );
       
    66 		</script>
       
    67 		<?php
       
    68 		$script = ob_get_clean();
       
    69 
       
    70 		wp_die(
       
    71 			'<h1>' . __( 'Your scheduled changes just published' ) . '</h1>' .
       
    72 			'<p><a href="' . esc_url( remove_query_arg( 'changeset_uuid' ) ) . '">' . __( 'Customize New Changes' ) . '</a></p>' . $script,
       
    73 			200
       
    74 		);
       
    75 	}
       
    76 
       
    77 	if ( in_array( get_post_status( $changeset_post->ID ), array( 'publish', 'trash' ), true ) ) {
       
    78 		wp_die(
       
    79 			'<h1>' . __( 'Something went wrong.' ) . '</h1>' .
       
    80 			'<p>' . __( 'This changeset cannot be further modified.' ) . '</p>' .
       
    81 			'<p><a href="' . esc_url( remove_query_arg( 'changeset_uuid' ) ) . '">' . __( 'Customize New Changes' ) . '</a></p>',
       
    82 			403
       
    83 		);
       
    84 	}
       
    85 }
       
    86 
       
    87 
       
    88 wp_reset_vars( array( 'url', 'return', 'autofocus' ) );
       
    89 if ( ! empty( $url ) ) {
       
    90 	$wp_customize->set_preview_url( wp_unslash( $url ) );
       
    91 }
       
    92 if ( ! empty( $return ) ) {
       
    93 	$wp_customize->set_return_url( wp_unslash( $return ) );
       
    94 }
       
    95 if ( ! empty( $autofocus ) && is_array( $autofocus ) ) {
       
    96 	$wp_customize->set_autofocus( wp_unslash( $autofocus ) );
       
    97 }
    37 
    98 
    38 $registered = $wp_scripts->registered;
    99 $registered = $wp_scripts->registered;
    39 $wp_scripts = new WP_Scripts;
   100 $wp_scripts = new WP_Scripts;
    40 $wp_scripts->registered = $registered;
   101 $wp_scripts->registered = $registered;
    41 
   102 
    48  *
   109  *
    49  * @since 3.4.0
   110  * @since 3.4.0
    50  */
   111  */
    51 do_action( 'customize_controls_init' );
   112 do_action( 'customize_controls_init' );
    52 
   113 
       
   114 wp_enqueue_script( 'heartbeat' );
    53 wp_enqueue_script( 'customize-controls' );
   115 wp_enqueue_script( 'customize-controls' );
    54 wp_enqueue_style( 'customize-controls' );
   116 wp_enqueue_style( 'customize-controls' );
    55 
   117 
    56 /**
   118 /**
    57  * Enqueue Customizer control scripts.
   119  * Enqueue Customizer control scripts.
    72 	$body_class .= ' mobile';
   134 	$body_class .= ' mobile';
    73 
   135 
    74 	?><meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=1.2" /><?php
   136 	?><meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=1.2" /><?php
    75 endif;
   137 endif;
    76 
   138 
    77 $is_ios = wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] );
   139 if ( $wp_customize->is_ios() ) {
    78 
       
    79 if ( $is_ios ) {
       
    80 	$body_class .= ' ios';
   140 	$body_class .= ' ios';
    81 }
   141 }
    82 
   142 
    83 if ( is_rtl() ) {
   143 if ( is_rtl() ) {
    84 	$body_class .= ' rtl';
   144 	$body_class .= ' rtl';
    85 }
   145 }
    86 $body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
   146 $body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) );
    87 
   147 
    88 if ( $wp_customize->is_theme_active() ) {
   148 $admin_title = sprintf( $wp_customize->get_document_title_template(), __( 'Loading&hellip;' ) );
    89 	$document_title_tmpl = _x( 'Customize: %s', 'Placeholder is the document title from the preview' );
       
    90 } else {
       
    91 	$document_title_tmpl = _x( 'Live Preview: %s', 'Placeholder is the document title from the preview' );
       
    92 }
       
    93 $document_title_tmpl = html_entity_decode( $document_title_tmpl, ENT_QUOTES, 'UTF-8' ); // because exported to JS and assigned to document.title
       
    94 $admin_title = sprintf( $document_title_tmpl, __( 'Loading&hellip;' ) );
       
    95 
   149 
    96 ?><title><?php echo $admin_title; ?></title>
   150 ?><title><?php echo $admin_title; ?></title>
    97 
   151 
    98 <script type="text/javascript">
   152 <script type="text/javascript">
    99 var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>';
   153 var ajaxurl = <?php echo wp_json_encode( admin_url( 'admin-ajax.php', 'relative' ) ); ?>,
       
   154 	pagenow = 'customize';
   100 </script>
   155 </script>
   101 
   156 
   102 <?php
   157 <?php
   103 /**
   158 /**
   104  * Fires when Customizer control styles are printed.
   159  * Fires when Customizer control styles are printed.
   117 </head>
   172 </head>
   118 <body class="<?php echo esc_attr( $body_class ); ?>">
   173 <body class="<?php echo esc_attr( $body_class ); ?>">
   119 <div class="wp-full-overlay expanded">
   174 <div class="wp-full-overlay expanded">
   120 	<form id="customize-controls" class="wrap wp-full-overlay-sidebar">
   175 	<form id="customize-controls" class="wrap wp-full-overlay-sidebar">
   121 		<div id="customize-header-actions" class="wp-full-overlay-header">
   176 		<div id="customize-header-actions" class="wp-full-overlay-header">
   122 			<div class="primary-actions">
   177 			<?php $save_text = $wp_customize->is_theme_active() ? __( 'Publish' ) : __( 'Activate &amp; Publish' ); ?>
   123 				<?php
   178 			<div id="customize-save-button-wrapper" class="customize-save-button-wrapper" >
   124 					$save_text = $wp_customize->is_theme_active() ? __( 'Save &amp; Publish' ) : __( 'Save &amp; Activate' );
   179 				<?php submit_button( $save_text, 'primary save', 'save', false ); ?>
   125 					submit_button( $save_text, 'primary save', 'save', false );
   180 				<button id="publish-settings" class="publish-settings button-primary button dashicons dashicons-admin-generic" aria-label="<?php esc_attr_e( 'Publish Settings' ); ?>" aria-expanded="false" disabled></button>
   126 				?>
   181 			</div>
   127 				<span class="spinner"></span>
   182 			<span class="spinner"></span>
   128 				<a class="customize-controls-preview-toggle" href="#">
   183 			<button type="button" class="customize-controls-preview-toggle">
   129 					<span class="controls"><?php _e( 'Customize' ); ?></span>
   184 				<span class="controls"><?php _e( 'Customize' ); ?></span>
   130 					<span class="preview"><?php _e( 'Preview' ); ?></span>
   185 				<span class="preview"><?php _e( 'Preview' ); ?></span>
   131 				</a>
   186 			</button>
   132 				<a class="customize-controls-close" href="<?php echo esc_url( $return ); ?>">
   187 			<a class="customize-controls-close" href="<?php echo esc_url( $wp_customize->get_return_url() ); ?>">
   133 					<span class="screen-reader-text"><?php _e( 'Cancel' ); ?></span>
   188 				<span class="screen-reader-text"><?php _e( 'Close the Customizer and go back to the previous page' ); ?></span>
   134 				</a>
   189 			</a>
   135 				<span class="control-panel-back" tabindex="-1"><span class="screen-reader-text"><?php _e( 'Back' ); ?></span></span>
   190 		</div>
   136 			</div>
   191 
   137 			<div class="secondary-actions">
   192 		<div id="customize-sidebar-outer-content">
   138 				<button type="button" class="customize-overlay-close">
   193 			<div id="customize-outer-theme-controls">
   139 					<span class="screen-reader-text"><?php _e( 'Close overlay' ); ?></span>
   194 				<ul class="customize-outer-pane-parent"><?php // Outer panel and sections are not implemented, but its here as a placeholder to avoid any side-effect in api.Section. ?></ul>
   140 				</button>
   195 			</div>
   141 			</div>
   196 		</div>
   142 		</div>
   197 
   143 
   198 		<div id="widgets-right" class="wp-clearfix"><!-- For Widget Customizer, many widgets try to look for instances under div#widgets-right, so we have to add that ID to a container div in the Customizer for compat -->
   144 		<div id="widgets-right"><!-- For Widget Customizer, many widgets try to look for instances under div#widgets-right, so we have to add that ID to a container div in the Customizer for compat -->
   199 			<div id="customize-notifications-area" class="customize-control-notifications-container">
   145 		<div class="wp-full-overlay-sidebar-content" tabindex="-1">
   200 				<ul></ul>
   146 			<div id="customize-info" class="accordion-section">
   201 			</div>
   147 				<div class="accordion-section-title" aria-label="<?php esc_attr_e( 'Customizer Options' ); ?>" tabindex="0">
   202 			<div class="wp-full-overlay-sidebar-content" tabindex="-1">
   148 					<span class="preview-notice"><?php
   203 				<div id="customize-info" class="accordion-section customize-info">
   149 						echo sprintf( __( 'You are customizing %s' ), '<strong class="theme-name site-title">' . get_bloginfo( 'name' ) . '</strong>' );
   204 					<div class="accordion-section-title">
   150 					?></span>
   205 						<span class="preview-notice"><?php
       
   206 							echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title site-title">' . get_bloginfo( 'name', 'display' ) . '</strong>' );
       
   207 						?></span>
       
   208 						<button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
       
   209 					</div>
       
   210 					<div class="customize-panel-description"><?php
       
   211 						_e( 'The Customizer allows you to preview changes to your site before publishing them. You can navigate to different pages on your site within the preview. Edit shortcuts are shown for some editable elements.' );
       
   212 					?></div>
   151 				</div>
   213 				</div>
   152 				<div class="accordion-section-content"><?php
   214 
   153 					echo __( 'The Customizer allows you to preview changes to your site before publishing them. You can also navigate to different pages on your site to preview them.' );
   215 				<div id="customize-theme-controls">
   154 				?></div>
   216 					<ul class="customize-pane-parent"><?php // Panels and sections are managed here via JavaScript ?></ul>
   155 			</div>
   217 				</div>
   156 
   218 			</div>
   157 			<div id="customize-theme-controls">
       
   158 				<ul><?php // Panels and sections are managed here via JavaScript ?></ul>
       
   159 			</div>
       
   160 		</div>
       
   161 		</div>
   219 		</div>
   162 
   220 
   163 		<div id="customize-footer-actions" class="wp-full-overlay-footer">
   221 		<div id="customize-footer-actions" class="wp-full-overlay-footer">
   164 			<a href="#" class="collapse-sidebar button-secondary" title="<?php esc_attr_e('Collapse Sidebar'); ?>">
   222 			<button type="button" class="collapse-sidebar button" aria-expanded="true" aria-label="<?php echo esc_attr( _x( 'Hide Controls', 'label for hide controls button without length constraints' ) ); ?>">
   165 				<span class="collapse-sidebar-arrow"></span>
   223 				<span class="collapse-sidebar-arrow"></span>
   166 				<span class="collapse-sidebar-label"><?php _e('Collapse'); ?></span>
   224 				<span class="collapse-sidebar-label"><?php _ex( 'Hide Controls', 'short (~12 characters) label for hide controls button' ); ?></span>
   167 			</a>
   225 			</button>
       
   226 			<?php $previewable_devices = $wp_customize->get_previewable_devices(); ?>
       
   227 			<?php if ( ! empty( $previewable_devices ) ) : ?>
       
   228 			<div class="devices-wrapper">
       
   229 				<div class="devices">
       
   230 					<?php foreach ( (array) $previewable_devices as $device => $settings ) : ?>
       
   231 						<?php
       
   232 						if ( empty( $settings['label'] ) ) {
       
   233 							continue;
       
   234 						}
       
   235 						$active = ! empty( $settings['default'] );
       
   236 						$class = 'preview-' . $device;
       
   237 						if ( $active ) {
       
   238 							$class .= ' active';
       
   239 						}
       
   240 						?>
       
   241 						<button type="button" class="<?php echo esc_attr( $class ); ?>" aria-pressed="<?php echo esc_attr( $active ) ?>" data-device="<?php echo esc_attr( $device ); ?>">
       
   242 							<span class="screen-reader-text"><?php echo esc_html( $settings['label'] ); ?></span>
       
   243 						</button>
       
   244 					<?php endforeach; ?>
       
   245 				</div>
       
   246 			</div>
       
   247 			<?php endif; ?>
   168 		</div>
   248 		</div>
   169 	</form>
   249 	</form>
   170 	<div id="customize-preview" class="wp-full-overlay-main"></div>
   250 	<div id="customize-preview" class="wp-full-overlay-main"></div>
   171 	<?php
   251 	<?php
   172 
   252 
   173 	// Render control templates.
       
   174 	$wp_customize->render_control_templates();
       
   175 
       
   176 	/**
   253 	/**
   177 	 * Print Customizer control scripts in the footer.
   254 	 * Prints templates, control scripts, and settings in the footer.
   178 	 *
   255 	 *
   179 	 * @since 3.4.0
   256 	 * @since 3.4.0
   180 	 */
   257 	 */
   181 	do_action( 'customize_controls_print_footer_scripts' );
   258 	do_action( 'customize_controls_print_footer_scripts' );
   182 
       
   183 	/*
       
   184 	 * If the frontend and the admin are served from the same domain, load the
       
   185 	 * preview over ssl if the Customizer is being loaded over ssl. This avoids
       
   186 	 * insecure content warnings. This is not attempted if the admin and frontend
       
   187 	 * are on different domains to avoid the case where the frontend doesn't have
       
   188 	 * ssl certs. Domain mapping plugins can allow other urls in these conditions
       
   189 	 * using the customize_allowed_urls filter.
       
   190 	 */
       
   191 
       
   192 	$allowed_urls = array( home_url('/') );
       
   193 	$admin_origin = parse_url( admin_url() );
       
   194 	$home_origin  = parse_url( home_url() );
       
   195 	$cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
       
   196 
       
   197 	if ( is_ssl() && ! $cross_domain )
       
   198 		$allowed_urls[] = home_url( '/', 'https' );
       
   199 
       
   200 	/**
       
   201 	 * Filter the list of URLs allowed to be clicked and followed in the Customizer preview.
       
   202 	 *
       
   203 	 * @since 3.4.0
       
   204 	 *
       
   205 	 * @param array $allowed_urls An array of allowed URLs.
       
   206 	 */
       
   207 	$allowed_urls = array_unique( apply_filters( 'customize_allowed_urls', $allowed_urls ) );
       
   208 
       
   209 	$fallback_url = add_query_arg( array(
       
   210 		'preview'        => 1,
       
   211 		'template'       => $wp_customize->get_template(),
       
   212 		'stylesheet'     => $wp_customize->get_stylesheet(),
       
   213 		'preview_iframe' => true,
       
   214 		'TB_iframe'      => 'true'
       
   215 	), home_url( '/' ) );
       
   216 
       
   217 	$login_url = add_query_arg( array(
       
   218 		'interim-login' => 1,
       
   219 		'customize-login' => 1
       
   220 	), wp_login_url() );
       
   221 
       
   222 	// Prepare Customizer settings to pass to JavaScript.
       
   223 	$settings = array(
       
   224 		'theme'    => array(
       
   225 			'stylesheet' => $wp_customize->get_stylesheet(),
       
   226 			'active'     => $wp_customize->is_theme_active(),
       
   227 		),
       
   228 		'url'      => array(
       
   229 			'preview'       => esc_url_raw( $url ? $url : home_url( '/' ) ),
       
   230 			'parent'        => esc_url_raw( admin_url() ),
       
   231 			'activated'     => esc_url_raw( home_url( '/' ) ),
       
   232 			'ajax'          => esc_url_raw( admin_url( 'admin-ajax.php', 'relative' ) ),
       
   233 			'allowed'       => array_map( 'esc_url_raw', $allowed_urls ),
       
   234 			'isCrossDomain' => $cross_domain,
       
   235 			'fallback'      => esc_url_raw( $fallback_url ),
       
   236 			'home'          => esc_url_raw( home_url( '/' ) ),
       
   237 			'login'         => esc_url_raw( $login_url ),
       
   238 		),
       
   239 		'browser'  => array(
       
   240 			'mobile' => wp_is_mobile(),
       
   241 			'ios'    => $is_ios,
       
   242 		),
       
   243 		'settings' => array(),
       
   244 		'controls' => array(),
       
   245 		'panels'   => array(),
       
   246 		'sections' => array(),
       
   247 		'nonce'    => array(
       
   248 			'save'    => wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() ),
       
   249 			'preview' => wp_create_nonce( 'preview-customize_' . $wp_customize->get_stylesheet() )
       
   250 		),
       
   251 		'autofocus' => array(),
       
   252 		'documentTitleTmpl' => $document_title_tmpl,
       
   253 	);
       
   254 
       
   255 	// Prepare Customize Setting objects to pass to JavaScript.
       
   256 	foreach ( $wp_customize->settings() as $id => $setting ) {
       
   257 		$settings['settings'][ $id ] = array(
       
   258 			'value'     => $setting->js_value(),
       
   259 			'transport' => $setting->transport,
       
   260 			'dirty'     => $setting->dirty,
       
   261 		);
       
   262 	}
       
   263 
       
   264 	// Prepare Customize Control objects to pass to JavaScript.
       
   265 	foreach ( $wp_customize->controls() as $id => $control ) {
       
   266 		$settings['controls'][ $id ] = $control->json();
       
   267 	}
       
   268 
       
   269 	// Prepare Customize Section objects to pass to JavaScript.
       
   270 	foreach ( $wp_customize->sections() as $id => $section ) {
       
   271 		$settings['sections'][ $id ] = $section->json();
       
   272 	}
       
   273 
       
   274 	// Prepare Customize Panel objects to pass to JavaScript.
       
   275 	foreach ( $wp_customize->panels() as $id => $panel ) {
       
   276 		$settings['panels'][ $id ] = $panel->json();
       
   277 		foreach ( $panel->sections as $section_id => $section ) {
       
   278 			$settings['sections'][ $section_id ] = $section->json();
       
   279 		}
       
   280 	}
       
   281 
       
   282 	// Pass to frontend the Customizer construct being deeplinked
       
   283 	if ( isset( $_GET['autofocus'] ) ) {
       
   284 		$autofocus = wp_unslash( $_GET['autofocus'] );
       
   285 		if ( is_array( $autofocus ) ) {
       
   286 			foreach ( $autofocus as $type => $id ) {
       
   287 				if ( isset( $settings[ $type . 's' ][ $id ] ) ) {
       
   288 					$settings['autofocus'][ $type ] = $id;
       
   289 				}
       
   290 			}
       
   291 		}
       
   292 	}
       
   293 
       
   294 	?>
   259 	?>
   295 	<script type="text/javascript">
       
   296 		var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
       
   297 	</script>
       
   298 </div>
   260 </div>
   299 </body>
   261 </body>
   300 </html>
   262 </html>