/wp-admin/includes/theme.php

https://gitlab.com/geyson/geyson · PHP · 560 lines · 327 code · 70 blank · 163 comment · 50 complexity · 357412fa0b8b8ea11937221508bdc020 MD5 · raw file

  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. ob_start();
  24. if ( empty( $redirect ) )
  25. $redirect = wp_nonce_url('themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet);
  26. if ( false === ($credentials = request_filesystem_credentials($redirect)) ) {
  27. $data = ob_get_clean();
  28. if ( ! empty($data) ){
  29. include_once( ABSPATH . 'wp-admin/admin-header.php');
  30. echo $data;
  31. include( ABSPATH . 'wp-admin/admin-footer.php');
  32. exit;
  33. }
  34. return;
  35. }
  36. if ( ! WP_Filesystem($credentials) ) {
  37. request_filesystem_credentials($redirect, '', true); // Failed to connect, Error and request again
  38. $data = ob_get_clean();
  39. if ( ! empty($data) ) {
  40. include_once( ABSPATH . 'wp-admin/admin-header.php');
  41. echo $data;
  42. include( ABSPATH . 'wp-admin/admin-footer.php');
  43. exit;
  44. }
  45. return;
  46. }
  47. if ( ! is_object($wp_filesystem) )
  48. return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
  49. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
  50. return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);
  51. // Get the base plugin folder.
  52. $themes_dir = $wp_filesystem->wp_themes_dir();
  53. if ( empty( $themes_dir ) ) {
  54. return new WP_Error( 'fs_no_themes_dir', __( 'Unable to locate WordPress theme directory.' ) );
  55. }
  56. $themes_dir = trailingslashit( $themes_dir );
  57. $theme_dir = trailingslashit( $themes_dir . $stylesheet );
  58. $deleted = $wp_filesystem->delete( $theme_dir, true );
  59. if ( ! $deleted ) {
  60. return new WP_Error( 'could_not_remove_theme', sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet ) );
  61. }
  62. $theme_translations = wp_get_installed_translations( 'themes' );
  63. // Remove language files, silently.
  64. if ( ! empty( $theme_translations[ $stylesheet ] ) ) {
  65. $translations = $theme_translations[ $stylesheet ];
  66. foreach ( $translations as $translation => $data ) {
  67. $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' );
  68. $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' );
  69. }
  70. }
  71. // Force refresh of theme update information.
  72. delete_site_transient( 'update_themes' );
  73. return true;
  74. }
  75. /**
  76. * Get the Page Templates available in this theme
  77. *
  78. * @since 1.5.0
  79. *
  80. * @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
  82. */
  83. function get_page_templates( $post = null ) {
  84. return array_flip( wp_get_theme()->get_page_templates( $post ) );
  85. }
  86. /**
  87. * Tidies a filename for url display by the theme editor.
  88. *
  89. * @since 2.9.0
  90. * @access private
  91. *
  92. * @param string $fullpath Full path to the theme file
  93. * @param string $containingfolder Path of the theme parent folder
  94. * @return string
  95. */
  96. function _get_template_edit_filename($fullpath, $containingfolder) {
  97. return str_replace(dirname(dirname( $containingfolder )) , '', $fullpath);
  98. }
  99. /**
  100. * Check if there is an update for a theme available.
  101. *
  102. * Will display link, if there is an update available.
  103. *
  104. * @since 2.7.0
  105. * @see get_theme_update_available()
  106. *
  107. * @param WP_Theme $theme Theme data object.
  108. */
  109. function theme_update_available( $theme ) {
  110. echo get_theme_update_available( $theme );
  111. }
  112. /**
  113. * Retrieve the update link if there is a theme update available.
  114. *
  115. * Will return a link if there is an update available.
  116. *
  117. * @since 3.8.0
  118. *
  119. * @staticvar object $themes_update
  120. *
  121. * @param WP_Theme $theme WP_Theme object.
  122. * @return false|string HTML for the update link, or false if invalid info was passed.
  123. */
  124. function get_theme_update_available( $theme ) {
  125. static $themes_update = null;
  126. if ( !current_user_can('update_themes' ) )
  127. return false;
  128. if ( !isset($themes_update) )
  129. $themes_update = get_site_transient('update_themes');
  130. if ( ! ( $theme instanceof WP_Theme ) ) {
  131. return false;
  132. }
  133. $stylesheet = $theme->get_stylesheet();
  134. $html = '';
  135. if ( isset($themes_update->response[ $stylesheet ]) ) {
  136. $update = $themes_update->response[ $stylesheet ];
  137. $theme_name = $theme->display('Name');
  138. $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.
  139. $update_url = wp_nonce_url( admin_url( 'update.php?action=upgrade-theme&amp;theme=' . urlencode( $stylesheet ) ), 'upgrade-theme_' . $stylesheet );
  140. $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;"';
  141. if ( !is_multisite() ) {
  142. if ( ! current_user_can('update_themes') ) {
  143. $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>',
  144. $theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'] );
  145. } elseif ( empty( $update['package'] ) ) {
  146. $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>',
  147. $theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'] );
  148. } else {
  149. $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>',
  150. $theme_name, esc_url( $details_url ), esc_attr( $theme['Name'] ), $update['new_version'], $update_url, $update_onclick );
  151. }
  152. }
  153. }
  154. return $html;
  155. }
  156. /**
  157. * Retrieve list of WordPress theme features (aka theme tags)
  158. *
  159. * @since 3.1.0
  160. *
  161. * @param bool $api Optional. Whether try to fetch tags from the WP.org API. Defaults to true.
  162. * @return array Array of features keyed by category with translations keyed by slug.
  163. */
  164. function get_theme_feature_list( $api = true ) {
  165. // Hard-coded list is used if api not accessible.
  166. $features = array(
  167. __( 'Colors' ) => array(
  168. 'black' => __( 'Black' ),
  169. 'blue' => __( 'Blue' ),
  170. 'brown' => __( 'Brown' ),
  171. 'gray' => __( 'Gray' ),
  172. 'green' => __( 'Green' ),
  173. 'orange' => __( 'Orange' ),
  174. 'pink' => __( 'Pink' ),
  175. 'purple' => __( 'Purple' ),
  176. 'red' => __( 'Red' ),
  177. 'silver' => __( 'Silver' ),
  178. 'tan' => __( 'Tan' ),
  179. 'white' => __( 'White' ),
  180. 'yellow' => __( 'Yellow' ),
  181. 'dark' => __( 'Dark' ),
  182. 'light' => __( 'Light' ),
  183. ),
  184. __( 'Layout' ) => array(
  185. 'fixed-layout' => __( 'Fixed Layout' ),
  186. 'fluid-layout' => __( 'Fluid Layout' ),
  187. 'responsive-layout' => __( 'Responsive Layout' ),
  188. 'one-column' => __( 'One Column' ),
  189. 'two-columns' => __( 'Two Columns' ),
  190. 'three-columns' => __( 'Three Columns' ),
  191. 'four-columns' => __( 'Four Columns' ),
  192. 'left-sidebar' => __( 'Left Sidebar' ),
  193. 'right-sidebar' => __( 'Right Sidebar' ),
  194. ),
  195. __( 'Features' ) => array(
  196. 'accessibility-ready' => __( 'Accessibility Ready' ),
  197. 'blavatar' => __( 'Blavatar' ),
  198. 'buddypress' => __( 'BuddyPress' ),
  199. 'custom-background' => __( 'Custom Background' ),
  200. 'custom-colors' => __( 'Custom Colors' ),
  201. 'custom-header' => __( 'Custom Header' ),
  202. 'custom-menu' => __( 'Custom Menu' ),
  203. 'editor-style' => __( 'Editor Style' ),
  204. 'featured-image-header' => __( 'Featured Image Header' ),
  205. 'featured-images' => __( 'Featured Images' ),
  206. 'flexible-header' => __( 'Flexible Header' ),
  207. 'front-page-post-form' => __( 'Front Page Posting' ),
  208. 'full-width-template' => __( 'Full Width Template' ),
  209. 'microformats' => __( 'Microformats' ),
  210. 'post-formats' => __( 'Post Formats' ),
  211. 'rtl-language-support' => __( 'RTL Language Support' ),
  212. 'sticky-post' => __( 'Sticky Post' ),
  213. 'theme-options' => __( 'Theme Options' ),
  214. 'threaded-comments' => __( 'Threaded Comments' ),
  215. 'translation-ready' => __( 'Translation Ready' ),
  216. ),
  217. __( 'Subject' ) => array(
  218. 'holiday' => __( 'Holiday' ),
  219. 'photoblogging' => __( 'Photoblogging' ),
  220. 'seasonal' => __( 'Seasonal' ),
  221. )
  222. );
  223. if ( ! $api || ! current_user_can( 'install_themes' ) )
  224. return $features;
  225. if ( !$feature_list = get_site_transient( 'wporg_theme_feature_list' ) )
  226. set_site_transient( 'wporg_theme_feature_list', array(), 3 * HOUR_IN_SECONDS );
  227. if ( !$feature_list ) {
  228. $feature_list = themes_api( 'feature_list', array() );
  229. if ( is_wp_error( $feature_list ) )
  230. return $features;
  231. }
  232. if ( !$feature_list )
  233. return $features;
  234. set_site_transient( 'wporg_theme_feature_list', $feature_list, 3 * HOUR_IN_SECONDS );
  235. $category_translations = array(
  236. 'Colors' => __( 'Colors' ),
  237. 'Layout' => __( 'Layout' ),
  238. 'Features' => __( 'Features' ),
  239. 'Subject' => __( 'Subject' )
  240. );
  241. // Loop over the wporg canonical list and apply translations
  242. $wporg_features = array();
  243. foreach ( (array) $feature_list as $feature_category => $feature_items ) {
  244. if ( isset($category_translations[$feature_category]) )
  245. $feature_category = $category_translations[$feature_category];
  246. $wporg_features[$feature_category] = array();
  247. foreach ( $feature_items as $feature ) {
  248. if ( isset($features[$feature_category][$feature]) )
  249. $wporg_features[$feature_category][$feature] = $features[$feature_category][$feature];
  250. else
  251. $wporg_features[$feature_category][$feature] = $feature;
  252. }
  253. }
  254. return $wporg_features;
  255. }
  256. /**
  257. * Retrieve theme installer pages from WordPress Themes API.
  258. *
  259. * It is possible for a theme to override the Themes API result with three
  260. * filters. Assume this is for themes, which can extend on the Theme Info to
  261. * offer more choices. This is very powerful and must be used with care, when
  262. * overriding the filters.
  263. *
  264. * The first filter, 'themes_api_args', is for the args and gives the action as
  265. * the second parameter. The hook for 'themes_api_args' must ensure that an
  266. * object is returned.
  267. *
  268. * The second filter, 'themes_api', is the result that would be returned.
  269. *
  270. * @since 2.8.0
  271. *
  272. * @param string $action The requested action. Likely values are 'theme_information',
  273. * 'feature_list', or 'query_themes'.
  274. * @param array|object $args Optional. Arguments to serialize for the Theme Info API.
  275. * @return mixed
  276. */
  277. function themes_api( $action, $args = null ) {
  278. if ( is_array( $args ) ) {
  279. $args = (object) $args;
  280. }
  281. if ( ! isset( $args->per_page ) ) {
  282. $args->per_page = 24;
  283. }
  284. if ( ! isset( $args->locale ) ) {
  285. $args->locale = get_locale();
  286. }
  287. /**
  288. * Filter arguments used to query for installer pages from the WordPress.org Themes API.
  289. *
  290. * Important: An object MUST be returned to this filter.
  291. *
  292. * @since 2.8.0
  293. *
  294. * @param object $args Arguments used to query for installer pages from the WordPress.org Themes API.
  295. * @param string $action Requested action. Likely values are 'theme_information',
  296. * 'feature_list', or 'query_themes'.
  297. */
  298. $args = apply_filters( 'themes_api_args', $args, $action );
  299. /**
  300. * Filter whether to override the WordPress.org Themes API.
  301. *
  302. * Returning a value of true to this filter allows a theme to completely
  303. * override the built-in WordPress.org API.
  304. *
  305. * @since 2.8.0
  306. *
  307. * @param bool $bool Whether to override the WordPress.org Themes API. Default false.
  308. * @param string $action Requested action. Likely values are 'theme_information',
  309. * 'feature_list', or 'query_themes'.
  310. * @param object $args Arguments used to query for installer pages from the Themes API.
  311. */
  312. $res = apply_filters( 'themes_api', false, $action, $args );
  313. if ( ! $res ) {
  314. $url = $http_url = 'http://api.wordpress.org/themes/info/1.0/';
  315. if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
  316. $url = set_url_scheme( $url, 'https' );
  317. $http_args = array(
  318. 'body' => array(
  319. 'action' => $action,
  320. 'request' => serialize( $args )
  321. )
  322. );
  323. $request = wp_remote_post( $url, $http_args );
  324. if ( $ssl && is_wp_error( $request ) ) {
  325. if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
  326. 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 );
  327. }
  328. $request = wp_remote_post( $http_url, $http_args );
  329. }
  330. if ( is_wp_error($request) ) {
  331. $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() );
  332. } else {
  333. $res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
  334. if ( ! is_object( $res ) && ! is_array( $res ) )
  335. $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 ) );
  336. }
  337. }
  338. /**
  339. * Filter the returned WordPress.org Themes API response.
  340. *
  341. * @since 2.8.0
  342. *
  343. * @param array|object $res WordPress.org Themes API response.
  344. * @param string $action Requested action. Likely values are 'theme_information',
  345. * 'feature_list', or 'query_themes'.
  346. * @param object $args Arguments used to query for installer pages from the WordPress.org Themes API.
  347. */
  348. return apply_filters( 'themes_api_result', $res, $action, $args );
  349. }
  350. /**
  351. * Prepare themes for JavaScript.
  352. *
  353. * @since 3.8.0
  354. *
  355. * @param array $themes Optional. Array of WP_Theme objects to prepare.
  356. * Defaults to all allowed themes.
  357. *
  358. * @return array An associative array of theme data, sorted by name.
  359. */
  360. function wp_prepare_themes_for_js( $themes = null ) {
  361. $current_theme = get_stylesheet();
  362. /**
  363. * Filter theme data before it is prepared for JavaScript.
  364. *
  365. * Passing a non-empty array will result in wp_prepare_themes_for_js() returning
  366. * early with that value instead.
  367. *
  368. * @since 4.2.0
  369. *
  370. * @param array $prepared_themes An associative array of theme data. Default empty array.
  371. * @param null|array $themes An array of WP_Theme objects to prepare, if any.
  372. * @param string $current_theme The current theme slug.
  373. */
  374. $prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme );
  375. if ( ! empty( $prepared_themes ) ) {
  376. return $prepared_themes;
  377. }
  378. // Make sure the current theme is listed first.
  379. $prepared_themes[ $current_theme ] = array();
  380. if ( null === $themes ) {
  381. $themes = wp_get_themes( array( 'allowed' => true ) );
  382. if ( ! isset( $themes[ $current_theme ] ) ) {
  383. $themes[ $current_theme ] = wp_get_theme();
  384. }
  385. }
  386. $updates = array();
  387. if ( current_user_can( 'update_themes' ) ) {
  388. $updates_transient = get_site_transient( 'update_themes' );
  389. if ( isset( $updates_transient->response ) ) {
  390. $updates = $updates_transient->response;
  391. }
  392. }
  393. WP_Theme::sort_by_name( $themes );
  394. $parents = array();
  395. foreach ( $themes as $theme ) {
  396. $slug = $theme->get_stylesheet();
  397. $encoded_slug = urlencode( $slug );
  398. $parent = false;
  399. if ( $theme->parent() ) {
  400. $parent = $theme->parent()->display( 'Name' );
  401. $parents[ $slug ] = $theme->parent()->get_stylesheet();
  402. }
  403. $prepared_themes[ $slug ] = array(
  404. 'id' => $slug,
  405. 'name' => $theme->display( 'Name' ),
  406. 'screenshot' => array( $theme->get_screenshot() ), // @todo multiple
  407. 'description' => $theme->display( 'Description' ),
  408. 'author' => $theme->display( 'Author', false, true ),
  409. 'authorAndUri' => $theme->display( 'Author' ),
  410. 'version' => $theme->display( 'Version' ),
  411. 'tags' => $theme->display( 'Tags' ),
  412. 'parent' => $parent,
  413. 'active' => $slug === $current_theme,
  414. 'hasUpdate' => isset( $updates[ $slug ] ),
  415. 'update' => get_theme_update_available( $theme ),
  416. 'actions' => array(
  417. 'activate' => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&amp;stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
  418. 'customize' => ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) ? wp_customize_url( $slug ) : null,
  419. 'delete' => current_user_can( 'delete_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&amp;stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
  420. ),
  421. );
  422. }
  423. // Remove 'delete' action if theme has an active child
  424. if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) {
  425. unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] );
  426. }
  427. /**
  428. * Filter the themes prepared for JavaScript, for themes.php.
  429. *
  430. * Could be useful for changing the order, which is by name by default.
  431. *
  432. * @since 3.8.0
  433. *
  434. * @param array $prepared_themes Array of themes.
  435. */
  436. $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
  437. $prepared_themes = array_values( $prepared_themes );
  438. return array_filter( $prepared_themes );
  439. }
  440. /**
  441. * Print JS templates for the theme-browsing UI in the Customizer.
  442. *
  443. * @since 4.2.0
  444. */
  445. function customize_themes_print_templates() {
  446. $preview_url = esc_url( add_query_arg( 'theme', '__THEME__' ) ); // Token because esc_url() strips curly braces.
  447. $preview_url = str_replace( '__THEME__', '{{ data.id }}', $preview_url );
  448. ?>
  449. <script type="text/html" id="tmpl-customize-themes-details-view">
  450. <div class="theme-backdrop"></div>
  451. <div class="theme-wrap">
  452. <div class="theme-header">
  453. <button type="button" class="left dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show previous theme' ); ?></span></button>
  454. <button type="button" class="right dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show next theme' ); ?></span></button>
  455. <button type="button" class="close dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Close details dialog' ); ?></span></button>
  456. </div>
  457. <div class="theme-about">
  458. <div class="theme-screenshots">
  459. <# if ( data.screenshot[0] ) { #>
  460. <div class="screenshot"><img src="{{ data.screenshot[0] }}" alt="" /></div>
  461. <# } else { #>
  462. <div class="screenshot blank"></div>
  463. <# } #>
  464. </div>
  465. <div class="theme-info">
  466. <# if ( data.active ) { #>
  467. <span class="current-label"><?php _e( 'Current Theme' ); ?></span>
  468. <# } #>
  469. <h3 class="theme-name">{{{ data.name }}}<span class="theme-version"><?php printf( __( 'Version: %s' ), '{{ data.version }}' ); ?></span></h3>
  470. <h4 class="theme-author"><?php printf( __( 'By %s' ), '{{{ data.authorAndUri }}}' ); ?></h4>
  471. <p class="theme-description">{{{ data.description }}}</p>
  472. <# if ( data.parent ) { #>
  473. <p class="parent-theme"><?php printf( __( 'This is a child theme of %s.' ), '<strong>{{{ data.parent }}}</strong>' ); ?></p>
  474. <# } #>
  475. <# if ( data.tags ) { #>
  476. <p class="theme-tags"><span><?php _e( 'Tags:' ); ?></span> {{ data.tags }}</p>
  477. <# } #>
  478. </div>
  479. </div>
  480. <# if ( ! data.active ) { #>
  481. <div class="theme-actions">
  482. <div class="inactive-theme">
  483. <a href="<?php echo $preview_url; ?>" target="_top" class="button button-primary"><?php _e( 'Live Preview' ); ?></a>
  484. </div>
  485. </div>
  486. <# } #>
  487. </div>
  488. </script>
  489. <?php
  490. }