25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

687 satır
27 KiB

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