/wp-admin/includes/theme.php

  1. <?php 
  2. /** 
  3. * WordPress Theme Administration API 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Administration 
  7. */ 
  8.  
  9. /** 
  10. * Remove a theme 
  11. * 
  12. * @since 2.8.0 
  13. * 
  14. * @global WP_Filesystem_Base $wp_filesystem Subclass 
  15. * 
  16. * @param string $stylesheet Stylesheet of the theme to delete 
  17. * @param string $redirect Redirect to page when complete. 
  18. * @return void|bool|WP_Error When void, echoes content. 
  19. */ 
  20. function delete_theme($stylesheet, $redirect = '') { 
  21. global $wp_filesystem; 
  22.  
  23. if ( empty($stylesheet) ) 
  24. return false; 
  25.  
  26. if ( empty( $redirect ) ) { 
  27. $redirect = wp_nonce_url('themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet); 
  28.  
  29. ob_start(); 
  30. $credentials = request_filesystem_credentials( $redirect ); 
  31. $data = ob_get_clean(); 
  32.  
  33. if ( false === $credentials ) { 
  34. if ( ! empty( $data ) ) { 
  35. include_once( ABSPATH . 'wp-admin/admin-header.php'); 
  36. echo $data; 
  37. include( ABSPATH . 'wp-admin/admin-footer.php'); 
  38. exit; 
  39. return; 
  40.  
  41. if ( ! WP_Filesystem( $credentials ) ) { 
  42. ob_start(); 
  43. request_filesystem_credentials( $redirect, '', true ); // Failed to connect, Error and request again. 
  44. $data = ob_get_clean(); 
  45.  
  46. if ( ! empty($data) ) { 
  47. include_once( ABSPATH . 'wp-admin/admin-header.php'); 
  48. echo $data; 
  49. include( ABSPATH . 'wp-admin/admin-footer.php'); 
  50. exit; 
  51. return; 
  52.  
  53. if ( ! is_object($wp_filesystem) ) 
  54. return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 
  55.  
  56. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() ) 
  57. return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors); 
  58.  
  59. // Get the base plugin folder. 
  60. $themes_dir = $wp_filesystem->wp_themes_dir(); 
  61. if ( empty( $themes_dir ) ) { 
  62. return new WP_Error( 'fs_no_themes_dir', __( 'Unable to locate WordPress theme directory.' ) ); 
  63.  
  64. $themes_dir = trailingslashit( $themes_dir ); 
  65. $theme_dir = trailingslashit( $themes_dir . $stylesheet ); 
  66. $deleted = $wp_filesystem->delete( $theme_dir, true ); 
  67.  
  68. if ( ! $deleted ) { 
  69. return new WP_Error( 'could_not_remove_theme', sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet ) ); 
  70.  
  71. $theme_translations = wp_get_installed_translations( 'themes' ); 
  72.  
  73. // Remove language files, silently. 
  74. if ( ! empty( $theme_translations[ $stylesheet ] ) ) { 
  75. $translations = $theme_translations[ $stylesheet ]; 
  76.  
  77. foreach ( $translations as $translation => $data ) { 
  78. $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' ); 
  79. $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' ); 
  80.  
  81. // Remove the theme from allowed themes on the network. 
  82. if ( is_multisite() ) { 
  83. WP_Theme::network_disable_theme( $stylesheet ); 
  84.  
  85. // Force refresh of theme update information. 
  86. delete_site_transient( 'update_themes' ); 
  87.  
  88. return true; 
  89.  
  90. /** 
  91. * Get the Page Templates available in this theme 
  92. * 
  93. * @since 1.5.0 
  94. * @since 4.7.0 Added the `$post_type` parameter. 
  95. * 
  96. * @param WP_Post|null $post Optional. The post being edited, provided for context. 
  97. * @param string $post_type Optional. Post type to get the templates for. Default 'page'. 
  98. * @return array Key is the template name, value is the filename of the template 
  99. */ 
  100. function get_page_templates( $post = null, $post_type = 'page' ) { 
  101. return array_flip( wp_get_theme()->get_page_templates( $post, $post_type ) ); 
  102.  
  103. /** 
  104. * Tidies a filename for url display by the theme editor. 
  105. * 
  106. * @since 2.9.0 
  107. * @access private 
  108. * 
  109. * @param string $fullpath Full path to the theme file 
  110. * @param string $containingfolder Path of the theme parent folder 
  111. * @return string 
  112. */ 
  113. function _get_template_edit_filename($fullpath, $containingfolder) { 
  114. return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath); 
  115.  
  116. /** 
  117. * Check if there is an update for a theme available. 
  118. * 
  119. * Will display link, if there is an update available. 
  120. * 
  121. * @since 2.7.0 
  122. * @see get_theme_update_available() 
  123. * 
  124. * @param WP_Theme $theme Theme data object. 
  125. */ 
  126. function theme_update_available( $theme ) { 
  127. echo get_theme_update_available( $theme ); 
  128.  
  129. /** 
  130. * Retrieve the update link if there is a theme update available. 
  131. * 
  132. * Will return a link if there is an update available. 
  133. * 
  134. * @since 3.8.0 
  135. * 
  136. * @staticvar object $themes_update 
  137. * 
  138. * @param WP_Theme $theme WP_Theme object. 
  139. * @return false|string HTML for the update link, or false if invalid info was passed. 
  140. */ 
  141. function get_theme_update_available( $theme ) { 
  142. static $themes_update = null; 
  143.  
  144. if ( !current_user_can('update_themes' ) ) 
  145. return false; 
  146.  
  147. if ( !isset($themes_update) ) 
  148. $themes_update = get_site_transient('update_themes'); 
  149.  
  150. if ( ! ( $theme instanceof WP_Theme ) ) { 
  151. return false; 
  152.  
  153. $stylesheet = $theme->get_stylesheet(); 
  154.  
  155. $html = ''; 
  156.  
  157. if ( isset($themes_update->response[ $stylesheet ]) ) { 
  158. $update = $themes_update->response[ $stylesheet ]; 
  159. $theme_name = $theme->display('Name'); 
  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. 
  161. $update_url = wp_nonce_url( admin_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $stylesheet ) ), 'upgrade-theme_' . $stylesheet ); 
  162.  
  163. if ( !is_multisite() ) { 
  164. if ( ! current_user_can('update_themes') ) { 
  165. /** translators: 1: theme name, 2: theme details URL, 3: additional link attributes, 4: version number */ 
  166. $html = sprintf( '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>.' ) . '</strong></p>',  
  167. $theme_name,  
  168. esc_url( $details_url ),  
  169. sprintf( 'class="thickbox open-plugin-details-modal" aria-label="%s"',  
  170. /** translators: 1: theme name, 2: version number */ 
  171. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) ) 
  172. ),  
  173. $update['new_version'] 
  174. ); 
  175. } elseif ( empty( $update['package'] ) ) { 
  176. /** translators: 1: theme name, 2: theme details URL, 3: additional link attributes, 4: version number */ 
  177. $html = sprintf( '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>. <em>Automatic update is unavailable for this theme.</em>' ) . '</strong></p>',  
  178. $theme_name,  
  179. esc_url( $details_url ),  
  180. sprintf( 'class="thickbox open-plugin-details-modal" aria-label="%s"',  
  181. /** translators: 1: theme name, 2: version number */ 
  182. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) ) 
  183. ),  
  184. $update['new_version'] 
  185. ); 
  186. } else { 
  187. /** translators: 1: theme name, 2: theme details URL, 3: additional link attributes, 4: version number, 5: update URL, 6: additional link attributes */ 
  188. $html = sprintf( '<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a> or <a href="%5$s" %6$s>update now</a>.' ) . '</strong></p>',  
  189. $theme_name,  
  190. esc_url( $details_url ),  
  191. sprintf( 'class="thickbox open-plugin-details-modal" aria-label="%s"',  
  192. /** translators: 1: theme name, 2: version number */ 
  193. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) ) 
  194. ),  
  195. $update['new_version'],  
  196. $update_url,  
  197. sprintf( 'aria-label="%s" id="update-theme" data-slug="%s"',  
  198. /** translators: %s: theme name */ 
  199. esc_attr( sprintf( __( 'Update %s now' ), $theme_name ) ),  
  200. $stylesheet 
  201. ); 
  202.  
  203. return $html; 
  204.  
  205. /** 
  206. * Retrieve list of WordPress theme features (aka theme tags) 
  207. * 
  208. * @since 3.1.0 
  209. * 
  210. * @param bool $api Optional. Whether try to fetch tags from the WordPress.org API. Defaults to true. 
  211. * @return array Array of features keyed by category with translations keyed by slug. 
  212. */ 
  213. function get_theme_feature_list( $api = true ) { 
  214. // Hard-coded list is used if api not accessible. 
  215. $features = array( 
  216.  
  217. __( 'Layout' ) => array( 
  218. 'grid-layout' => __( 'Grid Layout' ),  
  219. 'one-column' => __( 'One Column' ),  
  220. 'two-columns' => __( 'Two Columns' ),  
  221. 'three-columns' => __( 'Three Columns' ),  
  222. 'four-columns' => __( 'Four Columns' ),  
  223. 'left-sidebar' => __( 'Left Sidebar' ),  
  224. 'right-sidebar' => __( 'Right Sidebar' ),  
  225. ),  
  226.  
  227. __( 'Features' ) => array( 
  228. 'accessibility-ready' => __( 'Accessibility Ready' ),  
  229. 'buddypress' => __( 'BuddyPress' ),  
  230. 'custom-background' => __( 'Custom Background' ),  
  231. 'custom-colors' => __( 'Custom Colors' ),  
  232. 'custom-header' => __( 'Custom Header' ),  
  233. 'custom-logo' => __( 'Custom Logo' ),  
  234. 'custom-menu' => __( 'Custom Menu' ),  
  235. 'editor-style' => __( 'Editor Style' ),  
  236. 'featured-image-header' => __( 'Featured Image Header' ),  
  237. 'featured-images' => __( 'Featured Images' ),  
  238. 'flexible-header' => __( 'Flexible Header' ),  
  239. 'footer-widgets' => __( 'Footer Widgets' ),  
  240. 'front-page-post-form' => __( 'Front Page Posting' ),  
  241. 'full-width-template' => __( 'Full Width Template' ),  
  242. 'microformats' => __( 'Microformats' ),  
  243. 'post-formats' => __( 'Post Formats' ),  
  244. 'rtl-language-support' => __( 'RTL Language Support' ),  
  245. 'sticky-post' => __( 'Sticky Post' ),  
  246. 'theme-options' => __( 'Theme Options' ),  
  247. 'threaded-comments' => __( 'Threaded Comments' ),  
  248. 'translation-ready' => __( 'Translation Ready' ),  
  249. ),  
  250.  
  251. __( 'Subject' ) => array( 
  252. 'blog' => __( 'Blog' ),  
  253. 'e-commerce' => __( 'E-Commerce' ),  
  254. 'education' => __( 'Education' ),  
  255. 'entertainment' => __( 'Entertainment' ),  
  256. 'food-and-drink' => __( 'Food & Drink' ),  
  257. 'holiday' => __( 'Holiday' ),  
  258. 'news' => __( 'News' ),  
  259. 'photography' => __( 'Photography' ),  
  260. 'portfolio' => __( 'Portfolio' ),  
  261. ); 
  262.  
  263. if ( ! $api || ! current_user_can( 'install_themes' ) ) 
  264. return $features; 
  265.  
  266. if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) ) 
  267. set_site_transient( 'wporg_theme_feature_list', array(), 3 * HOUR_IN_SECONDS ); 
  268.  
  269. if ( !$feature_list ) { 
  270. $feature_list = themes_api( 'feature_list', array() ); 
  271. if ( is_wp_error( $feature_list ) ) 
  272. return $features; 
  273.  
  274. if ( !$feature_list ) 
  275. return $features; 
  276.  
  277. set_site_transient( 'wporg_theme_feature_list', $feature_list, 3 * HOUR_IN_SECONDS ); 
  278.  
  279. $category_translations = array( 
  280. 'Layout' => __( 'Layout' ),  
  281. 'Features' => __( 'Features' ),  
  282. 'Subject' => __( 'Subject' ),  
  283. ); 
  284.  
  285. // Loop over the wporg canonical list and apply translations 
  286. $wporg_features = array(); 
  287. foreach ( (array) $feature_list as $feature_category => $feature_items ) { 
  288. if ( isset($category_translations[$feature_category]) ) 
  289. $feature_category = $category_translations[$feature_category]; 
  290. $wporg_features[$feature_category] = array(); 
  291.  
  292. foreach ( $feature_items as $feature ) { 
  293. if ( isset($features[$feature_category][$feature]) ) 
  294. $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature]; 
  295. else 
  296. $wporg_features[$feature_category][$feature] = $feature; 
  297.  
  298. return $wporg_features; 
  299.  
  300. /** 
  301. * Retrieves theme installer pages from the WordPress.org Themes API. 
  302. * 
  303. * It is possible for a theme to override the Themes API result with three 
  304. * filters. Assume this is for themes, which can extend on the Theme Info to 
  305. * offer more choices. This is very powerful and must be used with care, when 
  306. * overriding the filters. 
  307. * 
  308. * The first filter, {@see 'themes_api_args'}, is for the args and gives the action 
  309. * as the second parameter. The hook for {@see 'themes_api_args'} must ensure that 
  310. * an object is returned. 
  311. * 
  312. * The second filter, {@see 'themes_api'}, allows a plugin to override the WordPress.org 
  313. * Theme API entirely. If `$action` is 'query_themes', 'theme_information', or 'feature_list',  
  314. * an object MUST be passed. If `$action` is 'hot_tags', an array should be passed. 
  315. * 
  316. * Finally, the third filter, {@see 'themes_api_result'}, makes it possible to filter the 
  317. * response object or array, depending on the `$action` type. 
  318. * 
  319. * Supported arguments per action: 
  320. * 
  321. * | Argument Name | 'query_themes' | 'theme_information' | 'hot_tags' | 'feature_list' | 
  322. * | -------------------| :------------: | :-----------------: | :--------: | :--------------: | 
  323. * | `$slug` | No | Yes | No | No | 
  324. * | `$per_page` | Yes | No | No | No | 
  325. * | `$page` | Yes | No | No | No | 
  326. * | `$number` | No | No | Yes | No | 
  327. * | `$search` | Yes | No | No | No | 
  328. * | `$tag` | Yes | No | No | No | 
  329. * | `$author` | Yes | No | No | No | 
  330. * | `$user` | Yes | No | No | No | 
  331. * | `$browse` | Yes | No | No | No | 
  332. * | `$locale` | Yes | Yes | No | No | 
  333. * | `$fields` | Yes | Yes | No | No | 
  334. * 
  335. * @since 2.8.0 
  336. * 
  337. * @param string $action API action to perform: 'query_themes', 'theme_information',  
  338. * 'hot_tags' or 'feature_list'. 
  339. * @param array|object $args { 
  340. * Optional. Array or object of arguments to serialize for the Themes API. 
  341. * 
  342. * @type string $slug The theme slug. Default empty. 
  343. * @type int $per_page Number of themes per page. Default 24. 
  344. * @type int $page Number of current page. Default 1. 
  345. * @type int $number Number of tags to be queried. 
  346. * @type string $search A search term. Default empty. 
  347. * @type string $tag Tag to filter themes. Default empty. 
  348. * @type string $author Username of an author to filter themes. Default empty. 
  349. * @type string $user Username to query for their favorites. Default empty. 
  350. * @type string $browse Browse view: 'featured', 'popular', 'updated', 'favorites'. 
  351. * @type string $locale Locale to provide context-sensitive results. Default is the value of get_locale(). 
  352. * @type array $fields { 
  353. * Array of fields which should or should not be returned. 
  354. * 
  355. * @type bool $description Whether to return the theme full description. Default false. 
  356. * @type bool $sections Whether to return the theme readme sections: description, installation,  
  357. * FAQ, screenshots, other notes, and changelog. Default false. 
  358. * @type bool $rating Whether to return the rating in percent and total number of ratings. 
  359. * Default false. 
  360. * @type bool $ratings Whether to return the number of rating for each star (1-5). Default false. 
  361. * @type bool $downloaded Whether to return the download count. Default false. 
  362. * @type bool $downloadlink Whether to return the download link for the package. Default false. 
  363. * @type bool $last_updated Whether to return the date of the last update. Default false. 
  364. * @type bool $tags Whether to return the assigned tags. Default false. 
  365. * @type bool $homepage Whether to return the theme homepage link. Default false. 
  366. * @type bool $screenshots Whether to return the screenshots. Default false. 
  367. * @type int $screenshot_count Number of screenshots to return. Default 1. 
  368. * @type bool $screenshot_url Whether to return the URL of the first screenshot. Default false. 
  369. * @type bool $photon_screenshots Whether to return the screenshots via Photon. Default false. 
  370. * @type bool $template Whether to return the slug of the parent theme. Default false. 
  371. * @type bool $parent Whether to return the slug, name and homepage of the parent theme. Default false. 
  372. * @type bool $versions Whether to return the list of all available versions. Default false. 
  373. * @type bool $theme_url Whether to return theme's URL. Default false. 
  374. * @type bool $extended_author Whether to return nicename or nicename and display name. Default false. 
  375. * } 
  376. * } 
  377. * @return object|array|WP_Error Response object or array on success, WP_Error on failure. See the 
  378. * {@link https://developer.wordpress.org/reference/functions/themes_api/ function reference article} 
  379. * for more information on the make-up of possible return objects depending on the value of `$action`. 
  380. */ 
  381. function themes_api( $action, $args = array() ) { 
  382.  
  383. if ( is_array( $args ) ) { 
  384. $args = (object) $args; 
  385.  
  386. if ( ! isset( $args->per_page ) ) { 
  387. $args->per_page = 24; 
  388.  
  389. if ( ! isset( $args->locale ) ) { 
  390. $args->locale = get_user_locale(); 
  391.  
  392. /** 
  393. * Filters arguments used to query for installer pages from the WordPress.org Themes API. 
  394. * 
  395. * Important: An object MUST be returned to this filter. 
  396. * 
  397. * @since 2.8.0 
  398. * 
  399. * @param object $args Arguments used to query for installer pages from the WordPress.org Themes API. 
  400. * @param string $action Requested action. Likely values are 'theme_information',  
  401. * 'feature_list', or 'query_themes'. 
  402. */ 
  403. $args = apply_filters( 'themes_api_args', $args, $action ); 
  404.  
  405. /** 
  406. * Filters whether to override the WordPress.org Themes API. 
  407. * 
  408. * Passing a non-false value will effectively short-circuit the WordPress.org API request. 
  409. * 
  410. * If `$action` is 'query_themes', 'theme_information', or 'feature_list', an object MUST 
  411. * be passed. If `$action` is 'hot_tags', an array should be passed. 
  412. * 
  413. * @since 2.8.0 
  414. * 
  415. * @param false|object|array $override Whether to override the WordPress.org Themes API. Default false. 
  416. * @param string $action Requested action. Likely values are 'theme_information',  
  417. * 'feature_list', or 'query_themes'. 
  418. * @param object $args Arguments used to query for installer pages from the Themes API. 
  419. */ 
  420. $res = apply_filters( 'themes_api', false, $action, $args ); 
  421.  
  422. if ( ! $res ) { 
  423. $url = $http_url = 'http://api.wordpress.org/themes/info/1.0/'; 
  424. if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) 
  425. $url = set_url_scheme( $url, 'https' ); 
  426.  
  427. $http_args = array( 
  428. 'body' => array( 
  429. 'action' => $action,  
  430. 'request' => serialize( $args ) 
  431. ); 
  432. $request = wp_remote_post( $url, $http_args ); 
  433.  
  434. if ( $ssl && is_wp_error( $request ) ) { 
  435. if ( ! wp_doing_ajax() ) { 
  436. trigger_error( 
  437. sprintf( 
  438. /** translators: %s: support forums URL */ 
  439. __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),  
  440. __( 'https://wordpress.org/support/' ) 
  441. ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),  
  442. headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE 
  443. ); 
  444. $request = wp_remote_post( $http_url, $http_args ); 
  445.  
  446. if ( is_wp_error($request) ) { 
  447. $res = new WP_Error( 'themes_api_failed',  
  448. sprintf( 
  449. /** translators: %s: support forums URL */ 
  450. __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),  
  451. __( 'https://wordpress.org/support/' ) 
  452. ),  
  453. $request->get_error_message() 
  454. ); 
  455. } else { 
  456. $res = maybe_unserialize( wp_remote_retrieve_body( $request ) ); 
  457. if ( ! is_object( $res ) && ! is_array( $res ) ) { 
  458. $res = new WP_Error( 'themes_api_failed',  
  459. sprintf( 
  460. /** translators: %s: support forums URL */ 
  461. __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),  
  462. __( 'https://wordpress.org/support/' ) 
  463. ),  
  464. wp_remote_retrieve_body( $request ) 
  465. ); 
  466.  
  467. /** 
  468. * Filters the returned WordPress.org Themes API response. 
  469. * 
  470. * @since 2.8.0 
  471. * 
  472. * @param array|object|WP_Error $res WordPress.org Themes API response. 
  473. * @param string $action Requested action. Likely values are 'theme_information',  
  474. * 'feature_list', or 'query_themes'. 
  475. * @param object $args Arguments used to query for installer pages from the WordPress.org Themes API. 
  476. */ 
  477. return apply_filters( 'themes_api_result', $res, $action, $args ); 
  478.  
  479. /** 
  480. * Prepare themes for JavaScript. 
  481. * 
  482. * @since 3.8.0 
  483. * 
  484. * @param array $themes Optional. Array of WP_Theme objects to prepare. 
  485. * Defaults to all allowed themes. 
  486. * 
  487. * @return array An associative array of theme data, sorted by name. 
  488. */ 
  489. function wp_prepare_themes_for_js( $themes = null ) { 
  490. $current_theme = get_stylesheet(); 
  491.  
  492. /** 
  493. * Filters theme data before it is prepared for JavaScript. 
  494. * 
  495. * Passing a non-empty array will result in wp_prepare_themes_for_js() returning 
  496. * early with that value instead. 
  497. * 
  498. * @since 4.2.0 
  499. * 
  500. * @param array $prepared_themes An associative array of theme data. Default empty array. 
  501. * @param null|array $themes An array of WP_Theme objects to prepare, if any. 
  502. * @param string $current_theme The current theme slug. 
  503. */ 
  504. $prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme ); 
  505.  
  506. if ( ! empty( $prepared_themes ) ) { 
  507. return $prepared_themes; 
  508.  
  509. // Make sure the current theme is listed first. 
  510. $prepared_themes[ $current_theme ] = array(); 
  511.  
  512. if ( null === $themes ) { 
  513. $themes = wp_get_themes( array( 'allowed' => true ) ); 
  514. if ( ! isset( $themes[ $current_theme ] ) ) { 
  515. $themes[ $current_theme ] = wp_get_theme(); 
  516.  
  517. $updates = array(); 
  518. if ( current_user_can( 'update_themes' ) ) { 
  519. $updates_transient = get_site_transient( 'update_themes' ); 
  520. if ( isset( $updates_transient->response ) ) { 
  521. $updates = $updates_transient->response; 
  522.  
  523. WP_Theme::sort_by_name( $themes ); 
  524.  
  525. $parents = array(); 
  526.  
  527. foreach ( $themes as $theme ) { 
  528. $slug = $theme->get_stylesheet(); 
  529. $encoded_slug = urlencode( $slug ); 
  530.  
  531. $parent = false; 
  532. if ( $theme->parent() ) { 
  533. $parent = $theme->parent()->display( 'Name' ); 
  534. $parents[ $slug ] = $theme->parent()->get_stylesheet(); 
  535.  
  536. $customize_action = null; 
  537. if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { 
  538. $customize_action = esc_url( add_query_arg( 
  539. array( 
  540. 'return' => urlencode( esc_url_raw( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ),  
  541. ),  
  542. wp_customize_url( $slug ) 
  543. ) ); 
  544.  
  545. $prepared_themes[ $slug ] = array( 
  546. 'id' => $slug,  
  547. 'name' => $theme->display( 'Name' ),  
  548. 'screenshot' => array( $theme->get_screenshot() ), // @todo multiple 
  549. 'description' => $theme->display( 'Description' ),  
  550. 'author' => $theme->display( 'Author', false, true ),  
  551. 'authorAndUri' => $theme->display( 'Author' ),  
  552. 'version' => $theme->display( 'Version' ),  
  553. 'tags' => $theme->display( 'Tags' ),  
  554. 'parent' => $parent,  
  555. 'active' => $slug === $current_theme,  
  556. 'hasUpdate' => isset( $updates[ $slug ] ),  
  557. 'hasPackage' => isset( $updates[ $slug ] ) && ! empty( $updates[ $slug ][ 'package' ] ),  
  558. 'update' => get_theme_update_available( $theme ),  
  559. 'actions' => array( 
  560. 'activate' => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,  
  561. 'customize' => $customize_action,  
  562. 'delete' => current_user_can( 'delete_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,  
  563. ),  
  564. ); 
  565.  
  566. // Remove 'delete' action if theme has an active child 
  567. if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) { 
  568. unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] ); 
  569.  
  570. /** 
  571. * Filters the themes prepared for JavaScript, for themes.php. 
  572. * 
  573. * Could be useful for changing the order, which is by name by default. 
  574. * 
  575. * @since 3.8.0 
  576. * 
  577. * @param array $prepared_themes Array of themes. 
  578. */ 
  579. $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes ); 
  580. $prepared_themes = array_values( $prepared_themes ); 
  581. return array_filter( $prepared_themes ); 
  582.  
  583. /** 
  584. * Print JS templates for the theme-browsing UI in the Customizer. 
  585. * 
  586. * @since 4.2.0 
  587. */ 
  588. function customize_themes_print_templates() { 
  589. $preview_url = esc_url( add_query_arg( 'theme', '__THEME__' ) ); // Token because esc_url() strips curly braces. 
  590. $preview_url = str_replace( '__THEME__', '{{ data.id }}', $preview_url ); 
  591. ?> 
  592. <script type="text/html" id="tmpl-customize-themes-details-view"> 
  593. <div class="theme-backdrop"></div> 
  594. <div class="theme-wrap wp-clearfix"> 
  595. <div class="theme-header"> 
  596. <button type="button" class="left dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show previous theme' ); ?></span></button> 
  597. <button type="button" class="right dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show next theme' ); ?></span></button> 
  598. <button type="button" class="close dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Close details dialog' ); ?></span></button> 
  599. </div> 
  600. <div class="theme-about wp-clearfix"> 
  601. <div class="theme-screenshots"> 
  602. <# if ( data.screenshot[0] ) { #> 
  603. <div class="screenshot"><img src="{{ data.screenshot[0] }}" alt="" /></div> 
  604. <# } else { #> 
  605. <div class="screenshot blank"></div> 
  606. <# } #> 
  607. </div> 
  608.  
  609. <div class="theme-info"> 
  610. <# if ( data.active ) { #> 
  611. <span class="current-label"><?php _e( 'Current Theme' ); ?></span> 
  612. <# } #> 
  613. <h2 class="theme-name">{{{ data.name }}}<span class="theme-version"><?php printf( __( 'Version: %s' ), '{{ data.version }}' ); ?></span></h2> 
  614. <h3 class="theme-author"><?php printf( __( 'By %s' ), '{{{ data.authorAndUri }}}' ); ?></h3> 
  615. <p class="theme-description">{{{ data.description }}}</p> 
  616.  
  617. <# if ( data.parent ) { #> 
  618. <p class="parent-theme"><?php printf( __( 'This is a child theme of %s.' ), '<strong>{{{ data.parent }}}</strong>' ); ?></p> 
  619. <# } #> 
  620.  
  621. <# if ( data.tags ) { #> 
  622. <p class="theme-tags"><span><?php _e( 'Tags:' ); ?></span> {{ data.tags }}</p> 
  623. <# } #> 
  624. </div> 
  625. </div> 
  626.  
  627. <# if ( ! data.active ) { #> 
  628. <div class="theme-actions"> 
  629. <div class="inactive-theme"> 
  630. <?php 
  631. /** translators: %s: Theme name */ 
  632. $aria_label = sprintf( __( 'Preview %s' ), '{{ data.name }}' ); 
  633. ?> 
  634. <a href="<?php echo $preview_url; ?>" target="_top" class="button button-primary" aria-label="<?php echo esc_attr( $aria_label ); ?>"><?php _e( 'Live Preview' ); ?></a> 
  635. </div> 
  636. </div> 
  637. <# } #> 
  638. </div> 
  639. </script> 
  640. <?php 
.