wp/wp-admin/includes/theme.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    53 		return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    53 		return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    54 
    54 
    55 	if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
    55 	if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
    56 		return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
    56 		return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
    57 
    57 
    58 	//Get the base plugin folder
    58 	// Get the base plugin folder.
    59 	$themes_dir = $wp_filesystem->wp_themes_dir();
    59 	$themes_dir = $wp_filesystem->wp_themes_dir();
    60 	if ( empty($themes_dir) )
    60 	if ( empty( $themes_dir ) ) {
    61 		return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress theme directory.'));
    61 		return new WP_Error( 'fs_no_themes_dir', __( 'Unable to locate WordPress theme directory.' ) );
       
    62 	}
    62 
    63 
    63 	$themes_dir = trailingslashit( $themes_dir );
    64 	$themes_dir = trailingslashit( $themes_dir );
    64 	$theme_dir = trailingslashit($themes_dir . $stylesheet);
    65 	$theme_dir = trailingslashit( $themes_dir . $stylesheet );
    65 	$deleted = $wp_filesystem->delete($theme_dir, true);
    66 	$deleted = $wp_filesystem->delete( $theme_dir, true );
    66 
    67 
    67 	if ( ! $deleted )
    68 	if ( ! $deleted ) {
    68 		return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $stylesheet) );
    69 		return new WP_Error( 'could_not_remove_theme', sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet ) );
    69 
    70 	}
    70 	// Force refresh of theme update information
    71 
    71 	delete_site_transient('update_themes');
    72 	$theme_translations = wp_get_installed_translations( 'themes' );
       
    73 
       
    74 	// Remove language files, silently.
       
    75 	if ( ! empty( $theme_translations[ $stylesheet ] ) ) {
       
    76 		$translations = $theme_translations[ $stylesheet ];
       
    77 
       
    78 		foreach ( $translations as $translation => $data ) {
       
    79 			$wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' );
       
    80 			$wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' );
       
    81 		}
       
    82 	}
       
    83 
       
    84 	// Force refresh of theme update information.
       
    85 	delete_site_transient( 'update_themes' );
    72 
    86 
    73 	return true;
    87 	return true;
    74 }
    88 }
    75 
    89 
    76 /**
    90 /**
    77  * Get the Page Templates available in this theme
    91  * Get the Page Templates available in this theme
    78  *
    92  *
    79  * @since 1.5.0
    93  * @since 1.5.0
    80  *
    94  *
       
    95  * @param WP_Post|null $post Optional. The post being edited, provided for context.
    81  * @return array Key is the template name, value is the filename of the template
    96  * @return array Key is the template name, value is the filename of the template
    82  */
    97  */
    83 function get_page_templates() {
    98 function get_page_templates( $post = null ) {
    84 	return array_flip( wp_get_theme()->get_page_templates() );
    99 	return array_flip( wp_get_theme()->get_page_templates( $post ) );
    85 }
   100 }
    86 
   101 
    87 /**
   102 /**
    88  * Tidies a filename for url display by the theme editor.
   103  * Tidies a filename for url display by the theme editor.
    89  *
   104  *
   102  * Check if there is an update for a theme available.
   117  * Check if there is an update for a theme available.
   103  *
   118  *
   104  * Will display link, if there is an update available.
   119  * Will display link, if there is an update available.
   105  *
   120  *
   106  * @since 2.7.0
   121  * @since 2.7.0
       
   122  * @see get_theme_update_available()
   107  *
   123  *
   108  * @param object $theme Theme data object.
   124  * @param object $theme Theme data object.
   109  * @return bool False if no valid info was passed.
       
   110  */
   125  */
   111 function theme_update_available( $theme ) {
   126 function theme_update_available( $theme ) {
       
   127 	echo get_theme_update_available( $theme );
       
   128 }
       
   129 
       
   130 /**
       
   131  * Retrieve the update link if there is a theme update available.
       
   132  *
       
   133  * Will return a link if there is an update available.
       
   134  *
       
   135  * @since 3.8.0
       
   136  *
       
   137  * @param WP_Theme $theme WP_Theme object.
       
   138  * @return false|string HTML for the update link, or false if invalid info was passed.
       
   139  */
       
   140 function get_theme_update_available( $theme ) {
   112 	static $themes_update;
   141 	static $themes_update;
   113 
   142 
   114 	if ( !current_user_can('update_themes' ) )
   143 	if ( !current_user_can('update_themes' ) )
   115 		return;
   144 		return false;
   116 
   145 
   117 	if ( !isset($themes_update) )
   146 	if ( !isset($themes_update) )
   118 		$themes_update = get_site_transient('update_themes');
   147 		$themes_update = get_site_transient('update_themes');
   119 
   148 
   120 	if ( ! is_a( $theme, 'WP_Theme' ) )
   149 	if ( ! ( $theme instanceof WP_Theme ) ) {
   121 		return;
   150 		return false;
       
   151 	}
   122 
   152 
   123 	$stylesheet = $theme->get_stylesheet();
   153 	$stylesheet = $theme->get_stylesheet();
       
   154 
       
   155 	$html = '';
   124 
   156 
   125 	if ( isset($themes_update->response[ $stylesheet ]) ) {
   157 	if ( isset($themes_update->response[ $stylesheet ]) ) {
   126 		$update = $themes_update->response[ $stylesheet ];
   158 		$update = $themes_update->response[ $stylesheet ];
   127 		$theme_name = $theme->display('Name');
   159 		$theme_name = $theme->display('Name');
   128 		$details_url = add_query_arg(array('TB_iframe' => 'true', 'width' => 1024, 'height' => 800), $update['url']); //Theme browser inside WP? replace this, Also, theme preview JS will override this on the available list.
   160 		$details_url = add_query_arg(array('TB_iframe' => 'true', 'width' => 1024, 'height' => 800), $update['url']); //Theme browser inside WP? replace this, Also, theme preview JS will override this on the available list.
   129 		$update_url = wp_nonce_url('update.php?action=upgrade-theme&theme=' . urlencode($stylesheet), 'upgrade-theme_' . $stylesheet);
   161 		$update_url = wp_nonce_url( admin_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $stylesheet ) ), 'upgrade-theme_' . $stylesheet );
   130 		$update_onclick = 'onclick="if ( confirm(\'' . esc_js( __("Updating this theme will lose any customizations you have made. 'Cancel' to stop, 'OK' to update.") ) . '\') ) {return true;}return false;"';
   162 		$update_onclick = 'onclick="if ( confirm(\'' . esc_js( __("Updating this theme will lose any customizations you have made. 'Cancel' to stop, 'OK' to update.") ) . '\') ) {return true;}return false;"';
   131 
   163 
   132 		if ( !is_multisite() ) {
   164 		if ( !is_multisite() ) {
   133 			if ( ! current_user_can('update_themes') )
   165 			if ( ! current_user_can('update_themes') ) {
   134 				printf( '<p><strong>' . __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s details</a>.') . '</strong></p>', $theme_name, $details_url, $update['new_version']);
   166 				$html = sprintf( '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>.' ) . '</strong></p>',
   135 			else if ( empty($update['package']) )
   167 					$theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'] );
   136 				printf( '<p><strong>' . __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s details</a>. <em>Automatic update is unavailable for this theme.</em>') . '</strong></p>', $theme_name, $details_url, $update['new_version']);
   168 			} elseif ( empty( $update['package'] ) ) {
   137 			else
   169 				$html = sprintf( '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this theme.</em>' ) . '</strong></p>',
   138 				printf( '<p><strong>' . __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s details</a> or <a href="%4$s" %5$s>update now</a>.') . '</strong></p>', $theme_name, $details_url, $update['new_version'], $update_url, $update_onclick );
   170 					$theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'] );
   139 		}
   171 			} else {
   140 	}
   172 				$html = sprintf( '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a> or <a href="%5$s">update now</a>.' ) . '</strong></p>',
       
   173 					$theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'], $update_url, $update_onclick );
       
   174 			}
       
   175 		}
       
   176 	}
       
   177 
       
   178 	return $html;
   141 }
   179 }
   142 
   180 
   143 /**
   181 /**
   144  * Retrieve list of WordPress theme features (aka theme tags)
   182  * Retrieve list of WordPress theme features (aka theme tags)
   145  *
   183  *
   167 				'yellow'  => __( 'Yellow' ),
   205 				'yellow'  => __( 'Yellow' ),
   168 				'dark'    => __( 'Dark' ),
   206 				'dark'    => __( 'Dark' ),
   169 				'light'   => __( 'Light' ),
   207 				'light'   => __( 'Light' ),
   170 			),
   208 			),
   171 
   209 
   172 		__( 'Columns' ) => array(
   210 		__( 'Layout' ) => array(
       
   211 			'fixed-layout'      => __( 'Fixed Layout' ),
       
   212 			'fluid-layout'      => __( 'Fluid Layout' ),
       
   213 			'responsive-layout' => __( 'Responsive Layout' ),
   173 			'one-column'    => __( 'One Column' ),
   214 			'one-column'    => __( 'One Column' ),
   174 			'two-columns'   => __( 'Two Columns' ),
   215 			'two-columns'   => __( 'Two Columns' ),
   175 			'three-columns' => __( 'Three Columns' ),
   216 			'three-columns' => __( 'Three Columns' ),
   176 			'four-columns'  => __( 'Four Columns' ),
   217 			'four-columns'  => __( 'Four Columns' ),
   177 			'left-sidebar'  => __( 'Left Sidebar' ),
   218 			'left-sidebar'  => __( 'Left Sidebar' ),
   178 			'right-sidebar' => __( 'Right Sidebar' ),
   219 			'right-sidebar' => __( 'Right Sidebar' ),
   179 		),
   220 		),
   180 
   221 
   181 		__( 'Width' ) => array(
       
   182 			'fixed-width'    => __( 'Fixed Width' ),
       
   183 			'flexible-width' => __( 'Flexible Width' ),
       
   184 		),
       
   185 
       
   186 		__( 'Features' ) => array(
   222 		__( 'Features' ) => array(
       
   223 			'accessibility-ready'   => __( 'Accessibility Ready' ),
   187 			'blavatar'              => __( 'Blavatar' ),
   224 			'blavatar'              => __( 'Blavatar' ),
   188 			'buddypress'            => __( 'BuddyPress' ),
   225 			'buddypress'            => __( 'BuddyPress' ),
   189 			'custom-background'     => __( 'Custom Background' ),
   226 			'custom-background'     => __( 'Custom Background' ),
   190 			'custom-colors'         => __( 'Custom Colors' ),
   227 			'custom-colors'         => __( 'Custom Colors' ),
   191 			'custom-header'         => __( 'Custom Header' ),
   228 			'custom-header'         => __( 'Custom Header' ),
   214 
   251 
   215 	if ( ! $api || ! current_user_can( 'install_themes' ) )
   252 	if ( ! $api || ! current_user_can( 'install_themes' ) )
   216 		return $features;
   253 		return $features;
   217 
   254 
   218 	if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
   255 	if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
   219 		set_site_transient( 'wporg_theme_feature_list', array(), 10800);
   256 		set_site_transient( 'wporg_theme_feature_list', array(), 3 * HOUR_IN_SECONDS );
   220 
   257 
   221 	if ( !$feature_list ) {
   258 	if ( !$feature_list ) {
   222 		$feature_list = themes_api( 'feature_list', array() );
   259 		$feature_list = themes_api( 'feature_list', array() );
   223 		if ( is_wp_error( $feature_list ) )
   260 		if ( is_wp_error( $feature_list ) )
   224 			return $features;
   261 			return $features;
   225 	}
   262 	}
   226 
   263 
   227 	if ( !$feature_list )
   264 	if ( !$feature_list )
   228 		return $features;
   265 		return $features;
   229 
   266 
   230 	set_site_transient( 'wporg_theme_feature_list', $feature_list, 10800 );
   267 	set_site_transient( 'wporg_theme_feature_list', $feature_list, 3 * HOUR_IN_SECONDS );
   231 
   268 
   232 	$category_translations = array( 'Colors' => __('Colors'), 'Columns' => __('Columns'), 'Width' => __('Width'),
   269 	$category_translations = array(
   233 								   'Features' => __('Features'), 'Subject' => __('Subject') );
   270 		'Colors'   => __( 'Colors' ),
       
   271 		'Layout'   => __( 'Layout' ),
       
   272 		'Features' => __( 'Features' ),
       
   273 		'Subject'  => __( 'Subject' )
       
   274 	);
   234 
   275 
   235 	// Loop over the wporg canonical list and apply translations
   276 	// Loop over the wporg canonical list and apply translations
   236 	$wporg_features = array();
   277 	$wporg_features = array();
   237 	foreach ( (array) $feature_list as $feature_category => $feature_items ) {
   278 	foreach ( (array) $feature_list as $feature_category => $feature_items ) {
   238 		if ( isset($category_translations[$feature_category]) )
   279 		if ( isset($category_translations[$feature_category]) )
   254  * Retrieve theme installer pages from WordPress Themes API.
   295  * Retrieve theme installer pages from WordPress Themes API.
   255  *
   296  *
   256  * It is possible for a theme to override the Themes API result with three
   297  * It is possible for a theme to override the Themes API result with three
   257  * filters. Assume this is for themes, which can extend on the Theme Info to
   298  * filters. Assume this is for themes, which can extend on the Theme Info to
   258  * offer more choices. This is very powerful and must be used with care, when
   299  * offer more choices. This is very powerful and must be used with care, when
   259  * overridding the filters.
   300  * overriding the filters.
   260  *
   301  *
   261  * The first filter, 'themes_api_args', is for the args and gives the action as
   302  * The first filter, 'themes_api_args', is for the args and gives the action as
   262  * the second parameter. The hook for 'themes_api_args' must ensure that an
   303  * the second parameter. The hook for 'themes_api_args' must ensure that an
   263  * object is returned.
   304  * object is returned.
   264  *
   305  *
   265  * The second filter, 'themes_api', is the result that would be returned.
   306  * The second filter, 'themes_api', is the result that would be returned.
   266  *
   307  *
   267  * @since 2.8.0
   308  * @since 2.8.0
   268  *
   309  *
   269  * @param string $action
   310  * @param string       $action The requested action. Likely values are 'theme_information',
   270  * @param array|object $args Optional. Arguments to serialize for the Theme Info API.
   311  *                             'feature_list', or 'query_themes'.
       
   312  * @param array|object $args   Optional. Arguments to serialize for the Theme Info API.
   271  * @return mixed
   313  * @return mixed
   272  */
   314  */
   273 function themes_api($action, $args = null) {
   315 function themes_api( $action, $args = null ) {
   274 
   316 
   275 	if ( is_array($args) )
   317 	if ( is_array( $args ) ) {
   276 		$args = (object)$args;
   318 		$args = (object) $args;
   277 
   319 	}
   278 	if ( !isset($args->per_page) )
   320 
       
   321 	if ( ! isset( $args->per_page ) ) {
   279 		$args->per_page = 24;
   322 		$args->per_page = 24;
   280 
   323 	}
   281 	$args = apply_filters('themes_api_args', $args, $action); //NOTE: Ensure that an object is returned via this filter.
   324 
   282 	$res = apply_filters('themes_api', false, $action, $args); //NOTE: Allows a theme to completely override the builtin WordPress.org API.
   325 	if ( ! isset( $args->locale ) ) {
       
   326 		$args->locale = get_locale();
       
   327 	}
       
   328 
       
   329 	/**
       
   330 	 * Filter arguments used to query for installer pages from the WordPress.org Themes API.
       
   331 	 *
       
   332 	 * Important: An object MUST be returned to this filter.
       
   333 	 *
       
   334 	 * @since 2.8.0
       
   335 	 *
       
   336 	 * @param object $args   Arguments used to query for installer pages from the WordPress.org Themes API.
       
   337 	 * @param string $action Requested action. Likely values are 'theme_information',
       
   338 	 *                       'feature_list', or 'query_themes'.
       
   339 	 */
       
   340 	$args = apply_filters( 'themes_api_args', $args, $action );
       
   341 
       
   342 	/**
       
   343 	 * Filter whether to override the WordPress.org Themes API.
       
   344 	 *
       
   345 	 * Returning a value of true to this filter allows a theme to completely
       
   346 	 * override the built-in WordPress.org API.
       
   347 	 *
       
   348 	 * @since 2.8.0
       
   349 	 *
       
   350 	 * @param bool   $bool   Whether to override the WordPress.org Themes API. Default false.
       
   351 	 * @param string $action Requested action. Likely values are 'theme_information',
       
   352 	 *                       'feature_list', or 'query_themes'.
       
   353 	 * @param object $args   Arguments used to query for installer pages from the Themes API.
       
   354 	 */
       
   355 	$res = apply_filters( 'themes_api', false, $action, $args );
   283 
   356 
   284 	if ( ! $res ) {
   357 	if ( ! $res ) {
   285 		$url = $http_url = 'http://api.wordpress.org/themes/info/1.0/';
   358 		$url = $http_url = 'http://api.wordpress.org/themes/info/1.0/';
   286 		if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
   359 		if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
   287 			$url = set_url_scheme( $url, 'https' );
   360 			$url = set_url_scheme( $url, 'https' );
   288 
   361 
   289 		$args = array(
   362 		$http_args = array(
   290 			'body' => array(
   363 			'body' => array(
   291 				'action' => $action,
   364 				'action' => $action,
   292 				'request' => serialize( $args )
   365 				'request' => serialize( $args )
   293 			)
   366 			)
   294 		);
   367 		);
   295 		$request = wp_remote_post( $url, $args );
   368 		$request = wp_remote_post( $url, $http_args );
   296 
   369 
   297 		if ( $ssl && is_wp_error( $request ) ) {
   370 		if ( $ssl && is_wp_error( $request ) ) {
   298 			trigger_error( __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ) . ' ' . '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)', headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE );
   371 			if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
   299 			$request = wp_remote_post( $http_url, $args );
   372 				trigger_error( __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="https://wordpress.org/support/">support forums</a>.' ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ), headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE );
       
   373 			}
       
   374 			$request = wp_remote_post( $http_url, $http_args );
   300 		}
   375 		}
   301 
   376 
   302 		if ( is_wp_error($request) ) {
   377 		if ( is_wp_error($request) ) {
   303 			$res = new WP_Error('themes_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), $request->get_error_message() );
   378 			$res = new WP_Error('themes_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="https://wordpress.org/support/">support forums</a>.' ), $request->get_error_message() );
   304 		} else {
   379 		} else {
   305 			$res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
   380 			$res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
   306 			if ( ! is_object( $res ) && ! is_array( $res ) )
   381 			if ( ! is_object( $res ) && ! is_array( $res ) )
   307 				$res = new WP_Error('themes_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), wp_remote_retrieve_body( $request ) );
   382 				$res = new WP_Error('themes_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="https://wordpress.org/support/">support forums</a>.' ), wp_remote_retrieve_body( $request ) );
   308 		}
   383 		}
   309 	}
   384 	}
   310 
   385 
   311 	return apply_filters('themes_api_result', $res, $action, $args);
   386 	/**
   312 }
   387 	 * Filter the returned WordPress.org Themes API response.
       
   388 	 *
       
   389 	 * @since 2.8.0
       
   390 	 *
       
   391 	 * @param array|object $res    WordPress.org Themes API response.
       
   392 	 * @param string       $action Requested action. Likely values are 'theme_information',
       
   393 	 *                             'feature_list', or 'query_themes'.
       
   394 	 * @param object       $args   Arguments used to query for installer pages from the WordPress.org Themes API.
       
   395 	 */
       
   396 	return apply_filters( 'themes_api_result', $res, $action, $args );
       
   397 }
       
   398 
       
   399 /**
       
   400  * Prepare themes for JavaScript.
       
   401  *
       
   402  * @since 3.8.0
       
   403  *
       
   404  * @param array $themes Optional. Array of WP_Theme objects to prepare.
       
   405  *                      Defaults to all allowed themes.
       
   406  *
       
   407  * @return array An associative array of theme data, sorted by name.
       
   408  */
       
   409 function wp_prepare_themes_for_js( $themes = null ) {
       
   410 	$current_theme = get_stylesheet();
       
   411 
       
   412 	/**
       
   413 	 * Filter theme data before it is prepared for JavaScript.
       
   414 	 *
       
   415 	 * Passing a non-empty array will result in wp_prepare_themes_for_js() returning
       
   416 	 * early with that value instead.
       
   417 	 *
       
   418 	 * @since 4.2.0
       
   419 	 *
       
   420 	 * @param array      $prepared_themes An associative array of theme data. Default empty array.
       
   421 	 * @param null|array $themes          An array of WP_Theme objects to prepare, if any.
       
   422 	 * @param string     $current_theme   The current theme slug.
       
   423 	 */
       
   424 	$prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme );
       
   425 
       
   426 	if ( ! empty( $prepared_themes ) ) {
       
   427 		return $prepared_themes;
       
   428 	}
       
   429 
       
   430 	// Make sure the current theme is listed first.
       
   431 	$prepared_themes[ $current_theme ] = array();
       
   432 
       
   433 	if ( null === $themes ) {
       
   434 		$themes = wp_get_themes( array( 'allowed' => true ) );
       
   435 		if ( ! isset( $themes[ $current_theme ] ) ) {
       
   436 			$themes[ $current_theme ] = wp_get_theme();
       
   437 		}
       
   438 	}
       
   439 
       
   440 	$updates = array();
       
   441 	if ( current_user_can( 'update_themes' ) ) {
       
   442 		$updates_transient = get_site_transient( 'update_themes' );
       
   443 		if ( isset( $updates_transient->response ) ) {
       
   444 			$updates = $updates_transient->response;
       
   445 		}
       
   446 	}
       
   447 
       
   448 	WP_Theme::sort_by_name( $themes );
       
   449 
       
   450 	$parents = array();
       
   451 
       
   452 	foreach ( $themes as $theme ) {
       
   453 		$slug = $theme->get_stylesheet();
       
   454 		$encoded_slug = urlencode( $slug );
       
   455 
       
   456 		$parent = false;
       
   457 		if ( $theme->parent() ) {
       
   458 			$parent = $theme->parent()->display( 'Name' );
       
   459 			$parents[ $slug ] = $theme->parent()->get_stylesheet();
       
   460 		}
       
   461 
       
   462 		$prepared_themes[ $slug ] = array(
       
   463 			'id'           => $slug,
       
   464 			'name'         => $theme->display( 'Name' ),
       
   465 			'screenshot'   => array( $theme->get_screenshot() ), // @todo multiple
       
   466 			'description'  => $theme->display( 'Description' ),
       
   467 			'author'       => $theme->display( 'Author', false, true ),
       
   468 			'authorAndUri' => $theme->display( 'Author' ),
       
   469 			'version'      => $theme->display( 'Version' ),
       
   470 			'tags'         => $theme->display( 'Tags' ),
       
   471 			'parent'       => $parent,
       
   472 			'active'       => $slug === $current_theme,
       
   473 			'hasUpdate'    => isset( $updates[ $slug ] ),
       
   474 			'update'       => get_theme_update_available( $theme ),
       
   475 			'actions'      => array(
       
   476 				'activate' => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&amp;stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
       
   477 				'customize' => ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) ? wp_customize_url( $slug ) : null,
       
   478 				'preview'   => add_query_arg( array(
       
   479 					'preview'        => 1,
       
   480 					'template'       => urlencode( $theme->get_template() ),
       
   481 					'stylesheet'     => urlencode( $slug ),
       
   482 					'preview_iframe' => true,
       
   483 					'TB_iframe'      => true,
       
   484 				), home_url( '/' ) ),
       
   485 				'delete'   => current_user_can( 'delete_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&amp;stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
       
   486 			),
       
   487 		);
       
   488 	}
       
   489 
       
   490 	// Remove 'delete' action if theme has an active child
       
   491 	if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) {
       
   492 		unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] );
       
   493 	}
       
   494 
       
   495 	/**
       
   496 	 * Filter the themes prepared for JavaScript, for themes.php.
       
   497 	 *
       
   498 	 * Could be useful for changing the order, which is by name by default.
       
   499 	 *
       
   500 	 * @since 3.8.0
       
   501 	 *
       
   502 	 * @param array $prepared_themes Array of themes.
       
   503 	 */
       
   504 	$prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
       
   505 	$prepared_themes = array_values( $prepared_themes );
       
   506 	return array_filter( $prepared_themes );
       
   507 }
       
   508 
       
   509 /**
       
   510  * Print JS templates for the theme-browsing UI in the Customizer.
       
   511  *
       
   512  * @since 4.2.0
       
   513  */
       
   514 function customize_themes_print_templates() {
       
   515 	$preview_url = esc_url( add_query_arg( 'theme', '__THEME__' ) ); // Token because esc_url() strips curly braces.
       
   516 	$preview_url = str_replace( '__THEME__', '{{ data.id }}', $preview_url );
       
   517 	?>
       
   518 	<script type="text/html" id="tmpl-customize-themes-details-view">
       
   519 		<div class="theme-backdrop"></div>
       
   520 		<div class="theme-wrap">
       
   521 			<div class="theme-header">
       
   522 				<button type="button" class="left dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show previous theme' ); ?></span></button>
       
   523 				<button type="button" class="right dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show next theme' ); ?></span></button>
       
   524 				<button type="button" class="close dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Close details dialog' ); ?></span></button>
       
   525 			</div>
       
   526 			<div class="theme-about">
       
   527 				<div class="theme-screenshots">
       
   528 				<# if ( data.screenshot[0] ) { #>
       
   529 					<div class="screenshot"><img src="{{ data.screenshot[0] }}" alt="" /></div>
       
   530 				<# } else { #>
       
   531 					<div class="screenshot blank"></div>
       
   532 				<# } #>
       
   533 				</div>
       
   534 
       
   535 				<div class="theme-info">
       
   536 					<# if ( data.active ) { #>
       
   537 						<span class="current-label"><?php _e( 'Current Theme' ); ?></span>
       
   538 					<# } #>
       
   539 					<h3 class="theme-name">{{{ data.name }}}<span class="theme-version"><?php printf( __( 'Version: %s' ), '{{ data.version }}' ); ?></span></h3>
       
   540 					<h4 class="theme-author"><?php printf( __( 'By %s' ), '{{{ data.authorAndUri }}}' ); ?></h4>
       
   541 					<p class="theme-description">{{{ data.description }}}</p>
       
   542 
       
   543 					<# if ( data.parent ) { #>
       
   544 						<p class="parent-theme"><?php printf( __( 'This is a child theme of %s.' ), '<strong>{{{ data.parent }}}</strong>' ); ?></p>
       
   545 					<# } #>
       
   546 
       
   547 					<# if ( data.tags ) { #>
       
   548 						<p class="theme-tags"><span><?php _e( 'Tags:' ); ?></span> {{ data.tags }}</p>
       
   549 					<# } #>
       
   550 				</div>
       
   551 			</div>
       
   552 
       
   553 			<# if ( ! data.active ) { #>
       
   554 				<div class="theme-actions">
       
   555 					<div class="inactive-theme">
       
   556 						<a href="<?php echo $preview_url; ?>" target="_top" class="button button-primary"><?php _e( 'Live Preview' ); ?></a>
       
   557 					</div>
       
   558 				</div>
       
   559 			<# } #>
       
   560 		</div>
       
   561 	</script>
       
   562 	<?php
       
   563 }
       
   564 add_action( 'customize_controls_print_footer_scripts', 'customize_themes_print_templates' );