theme.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. <?php
  2. /**
  3. * WordPress Theme Administration API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Removes a theme.
  10. *
  11. * @since 2.8.0
  12. *
  13. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  14. *
  15. * @param string $stylesheet Stylesheet of the theme to delete.
  16. * @param string $redirect Redirect to page when complete.
  17. * @return bool|null|WP_Error True on success, false if `$stylesheet` is empty, WP_Error on failure.
  18. * Null if filesystem credentials are required to proceed.
  19. */
  20. function delete_theme( $stylesheet, $redirect = '' ) {
  21. global $wp_filesystem;
  22. if ( empty( $stylesheet ) ) {
  23. return false;
  24. }
  25. if ( empty( $redirect ) ) {
  26. $redirect = wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet );
  27. }
  28. ob_start();
  29. $credentials = request_filesystem_credentials( $redirect );
  30. $data = ob_get_clean();
  31. if ( false === $credentials ) {
  32. if ( ! empty( $data ) ) {
  33. require_once ABSPATH . 'wp-admin/admin-header.php';
  34. echo $data;
  35. require_once ABSPATH . 'wp-admin/admin-footer.php';
  36. exit;
  37. }
  38. return;
  39. }
  40. if ( ! WP_Filesystem( $credentials ) ) {
  41. ob_start();
  42. // Failed to connect. Error and request again.
  43. request_filesystem_credentials( $redirect, '', true );
  44. $data = ob_get_clean();
  45. if ( ! empty( $data ) ) {
  46. require_once ABSPATH . 'wp-admin/admin-header.php';
  47. echo $data;
  48. require_once ABSPATH . 'wp-admin/admin-footer.php';
  49. exit;
  50. }
  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->has_errors() ) {
  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. /**
  65. * Fires immediately before a theme deletion attempt.
  66. *
  67. * @since 5.8.0
  68. *
  69. * @param string $stylesheet Stylesheet of the theme to delete.
  70. */
  71. do_action( 'delete_theme', $stylesheet );
  72. $themes_dir = trailingslashit( $themes_dir );
  73. $theme_dir = trailingslashit( $themes_dir . $stylesheet );
  74. $deleted = $wp_filesystem->delete( $theme_dir, true );
  75. /**
  76. * Fires immediately after a theme deletion attempt.
  77. *
  78. * @since 5.8.0
  79. *
  80. * @param string $stylesheet Stylesheet of the theme to delete.
  81. * @param bool $deleted Whether the theme deletion was successful.
  82. */
  83. do_action( 'deleted_theme', $stylesheet, $deleted );
  84. if ( ! $deleted ) {
  85. return new WP_Error(
  86. 'could_not_remove_theme',
  87. /* translators: %s: Theme name. */
  88. sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet )
  89. );
  90. }
  91. $theme_translations = wp_get_installed_translations( 'themes' );
  92. // Remove language files, silently.
  93. if ( ! empty( $theme_translations[ $stylesheet ] ) ) {
  94. $translations = $theme_translations[ $stylesheet ];
  95. foreach ( $translations as $translation => $data ) {
  96. $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' );
  97. $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' );
  98. $json_translation_files = glob( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '-*.json' );
  99. if ( $json_translation_files ) {
  100. array_map( array( $wp_filesystem, 'delete' ), $json_translation_files );
  101. }
  102. }
  103. }
  104. // Remove the theme from allowed themes on the network.
  105. if ( is_multisite() ) {
  106. WP_Theme::network_disable_theme( $stylesheet );
  107. }
  108. // Force refresh of theme update information.
  109. delete_site_transient( 'update_themes' );
  110. return true;
  111. }
  112. /**
  113. * Gets the page templates available in this theme.
  114. *
  115. * @since 1.5.0
  116. * @since 4.7.0 Added the `$post_type` parameter.
  117. *
  118. * @param WP_Post|null $post Optional. The post being edited, provided for context.
  119. * @param string $post_type Optional. Post type to get the templates for. Default 'page'.
  120. * @return string[] Array of template file names keyed by the template header name.
  121. */
  122. function get_page_templates( $post = null, $post_type = 'page' ) {
  123. return array_flip( wp_get_theme()->get_page_templates( $post, $post_type ) );
  124. }
  125. /**
  126. * Tidies a filename for url display by the theme file editor.
  127. *
  128. * @since 2.9.0
  129. * @access private
  130. *
  131. * @param string $fullpath Full path to the theme file
  132. * @param string $containingfolder Path of the theme parent folder
  133. * @return string
  134. */
  135. function _get_template_edit_filename( $fullpath, $containingfolder ) {
  136. return str_replace( dirname( dirname( $containingfolder ) ), '', $fullpath );
  137. }
  138. /**
  139. * Check if there is an update for a theme available.
  140. *
  141. * Will display link, if there is an update available.
  142. *
  143. * @since 2.7.0
  144. *
  145. * @see get_theme_update_available()
  146. *
  147. * @param WP_Theme $theme Theme data object.
  148. */
  149. function theme_update_available( $theme ) {
  150. echo get_theme_update_available( $theme );
  151. }
  152. /**
  153. * Retrieves the update link if there is a theme update available.
  154. *
  155. * Will return a link if there is an update available.
  156. *
  157. * @since 3.8.0
  158. *
  159. * @param WP_Theme $theme WP_Theme object.
  160. * @return string|false HTML for the update link, or false if invalid info was passed.
  161. */
  162. function get_theme_update_available( $theme ) {
  163. static $themes_update = null;
  164. if ( ! current_user_can( 'update_themes' ) ) {
  165. return false;
  166. }
  167. if ( ! isset( $themes_update ) ) {
  168. $themes_update = get_site_transient( 'update_themes' );
  169. }
  170. if ( ! ( $theme instanceof WP_Theme ) ) {
  171. return false;
  172. }
  173. $stylesheet = $theme->get_stylesheet();
  174. $html = '';
  175. if ( isset( $themes_update->response[ $stylesheet ] ) ) {
  176. $update = $themes_update->response[ $stylesheet ];
  177. $theme_name = $theme->display( 'Name' );
  178. $details_url = add_query_arg(
  179. array(
  180. 'TB_iframe' => 'true',
  181. 'width' => 1024,
  182. 'height' => 800,
  183. ),
  184. $update['url']
  185. ); // Theme browser inside WP? Replace this. Also, theme preview JS will override this on the available list.
  186. $update_url = wp_nonce_url( admin_url( 'update.php?action=upgrade-theme&amp;theme=' . urlencode( $stylesheet ) ), 'upgrade-theme_' . $stylesheet );
  187. if ( ! is_multisite() ) {
  188. if ( ! current_user_can( 'update_themes' ) ) {
  189. $html = sprintf(
  190. /* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number. */
  191. '<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>',
  192. $theme_name,
  193. esc_url( $details_url ),
  194. sprintf(
  195. 'class="thickbox open-plugin-details-modal" aria-label="%s"',
  196. /* translators: 1: Theme name, 2: Version number. */
  197. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
  198. ),
  199. $update['new_version']
  200. );
  201. } elseif ( empty( $update['package'] ) ) {
  202. $html = sprintf(
  203. /* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number. */
  204. '<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>',
  205. $theme_name,
  206. esc_url( $details_url ),
  207. sprintf(
  208. 'class="thickbox open-plugin-details-modal" aria-label="%s"',
  209. /* translators: 1: Theme name, 2: Version number. */
  210. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
  211. ),
  212. $update['new_version']
  213. );
  214. } else {
  215. $html = sprintf(
  216. /* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number, 5: Update URL, 6: Additional link attributes. */
  217. '<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>',
  218. $theme_name,
  219. esc_url( $details_url ),
  220. sprintf(
  221. 'class="thickbox open-plugin-details-modal" aria-label="%s"',
  222. /* translators: 1: Theme name, 2: Version number. */
  223. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
  224. ),
  225. $update['new_version'],
  226. $update_url,
  227. sprintf(
  228. 'aria-label="%s" id="update-theme" data-slug="%s"',
  229. /* translators: %s: Theme name. */
  230. esc_attr( sprintf( _x( 'Update %s now', 'theme' ), $theme_name ) ),
  231. $stylesheet
  232. )
  233. );
  234. }
  235. }
  236. }
  237. return $html;
  238. }
  239. /**
  240. * Retrieves list of WordPress theme features (aka theme tags).
  241. *
  242. * @since 3.1.0
  243. * @since 3.2.0 Added 'Gray' color and 'Featured Image Header', 'Featured Images',
  244. * 'Full Width Template', and 'Post Formats' features.
  245. * @since 3.5.0 Added 'Flexible Header' feature.
  246. * @since 3.8.0 Renamed 'Width' filter to 'Layout'.
  247. * @since 3.8.0 Renamed 'Fixed Width' and 'Flexible Width' options
  248. * to 'Fixed Layout' and 'Fluid Layout'.
  249. * @since 3.8.0 Added 'Accessibility Ready' feature and 'Responsive Layout' option.
  250. * @since 3.9.0 Combined 'Layout' and 'Columns' filters.
  251. * @since 4.6.0 Removed 'Colors' filter.
  252. * @since 4.6.0 Added 'Grid Layout' option.
  253. * Removed 'Fixed Layout', 'Fluid Layout', and 'Responsive Layout' options.
  254. * @since 4.6.0 Added 'Custom Logo' and 'Footer Widgets' features.
  255. * Removed 'Blavatar' feature.
  256. * @since 4.6.0 Added 'Blog', 'E-Commerce', 'Education', 'Entertainment', 'Food & Drink',
  257. * 'Holiday', 'News', 'Photography', and 'Portfolio' subjects.
  258. * Removed 'Photoblogging' and 'Seasonal' subjects.
  259. * @since 4.9.0 Reordered the filters from 'Layout', 'Features', 'Subject'
  260. * to 'Subject', 'Features', 'Layout'.
  261. * @since 4.9.0 Removed 'BuddyPress', 'Custom Menu', 'Flexible Header',
  262. * 'Front Page Posting', 'Microformats', 'RTL Language Support',
  263. * 'Threaded Comments', and 'Translation Ready' features.
  264. * @since 5.5.0 Added 'Block Editor Patterns', 'Block Editor Styles',
  265. * and 'Full Site Editing' features.
  266. * @since 5.5.0 Added 'Wide Blocks' layout option.
  267. * @since 5.8.1 Added 'Template Editing' feature.
  268. * @since 6.1.1 Replaced 'Full Site Editing' feature name with 'Site Editor'.
  269. *
  270. * @param bool $api Optional. Whether try to fetch tags from the WordPress.org API. Defaults to true.
  271. * @return array Array of features keyed by category with translations keyed by slug.
  272. */
  273. function get_theme_feature_list( $api = true ) {
  274. // Hard-coded list is used if API is not accessible.
  275. $features = array(
  276. __( 'Subject' ) => array(
  277. 'blog' => __( 'Blog' ),
  278. 'e-commerce' => __( 'E-Commerce' ),
  279. 'education' => __( 'Education' ),
  280. 'entertainment' => __( 'Entertainment' ),
  281. 'food-and-drink' => __( 'Food & Drink' ),
  282. 'holiday' => __( 'Holiday' ),
  283. 'news' => __( 'News' ),
  284. 'photography' => __( 'Photography' ),
  285. 'portfolio' => __( 'Portfolio' ),
  286. ),
  287. __( 'Features' ) => array(
  288. 'accessibility-ready' => __( 'Accessibility Ready' ),
  289. 'block-patterns' => __( 'Block Editor Patterns' ),
  290. 'block-styles' => __( 'Block Editor Styles' ),
  291. 'custom-background' => __( 'Custom Background' ),
  292. 'custom-colors' => __( 'Custom Colors' ),
  293. 'custom-header' => __( 'Custom Header' ),
  294. 'custom-logo' => __( 'Custom Logo' ),
  295. 'editor-style' => __( 'Editor Style' ),
  296. 'featured-image-header' => __( 'Featured Image Header' ),
  297. 'featured-images' => __( 'Featured Images' ),
  298. 'footer-widgets' => __( 'Footer Widgets' ),
  299. 'full-site-editing' => __( 'Site Editor' ),
  300. 'full-width-template' => __( 'Full Width Template' ),
  301. 'post-formats' => __( 'Post Formats' ),
  302. 'sticky-post' => __( 'Sticky Post' ),
  303. 'template-editing' => __( 'Template Editing' ),
  304. 'theme-options' => __( 'Theme Options' ),
  305. ),
  306. __( 'Layout' ) => array(
  307. 'grid-layout' => __( 'Grid Layout' ),
  308. 'one-column' => __( 'One Column' ),
  309. 'two-columns' => __( 'Two Columns' ),
  310. 'three-columns' => __( 'Three Columns' ),
  311. 'four-columns' => __( 'Four Columns' ),
  312. 'left-sidebar' => __( 'Left Sidebar' ),
  313. 'right-sidebar' => __( 'Right Sidebar' ),
  314. 'wide-blocks' => __( 'Wide Blocks' ),
  315. ),
  316. );
  317. if ( ! $api || ! current_user_can( 'install_themes' ) ) {
  318. return $features;
  319. }
  320. $feature_list = get_site_transient( 'wporg_theme_feature_list' );
  321. if ( ! $feature_list ) {
  322. set_site_transient( 'wporg_theme_feature_list', array(), 3 * HOUR_IN_SECONDS );
  323. }
  324. if ( ! $feature_list ) {
  325. $feature_list = themes_api( 'feature_list', array() );
  326. if ( is_wp_error( $feature_list ) ) {
  327. return $features;
  328. }
  329. }
  330. if ( ! $feature_list ) {
  331. return $features;
  332. }
  333. set_site_transient( 'wporg_theme_feature_list', $feature_list, 3 * HOUR_IN_SECONDS );
  334. $category_translations = array(
  335. 'Layout' => __( 'Layout' ),
  336. 'Features' => __( 'Features' ),
  337. 'Subject' => __( 'Subject' ),
  338. );
  339. $wporg_features = array();
  340. // Loop over the wp.org canonical list and apply translations.
  341. foreach ( (array) $feature_list as $feature_category => $feature_items ) {
  342. if ( isset( $category_translations[ $feature_category ] ) ) {
  343. $feature_category = $category_translations[ $feature_category ];
  344. }
  345. $wporg_features[ $feature_category ] = array();
  346. foreach ( $feature_items as $feature ) {
  347. if ( isset( $features[ $feature_category ][ $feature ] ) ) {
  348. $wporg_features[ $feature_category ][ $feature ] = $features[ $feature_category ][ $feature ];
  349. } else {
  350. $wporg_features[ $feature_category ][ $feature ] = $feature;
  351. }
  352. }
  353. }
  354. return $wporg_features;
  355. }
  356. /**
  357. * Retrieves theme installer pages from the WordPress.org Themes API.
  358. *
  359. * It is possible for a theme to override the Themes API result with three
  360. * filters. Assume this is for themes, which can extend on the Theme Info to
  361. * offer more choices. This is very powerful and must be used with care, when
  362. * overriding the filters.
  363. *
  364. * The first filter, {@see 'themes_api_args'}, is for the args and gives the action
  365. * as the second parameter. The hook for {@see 'themes_api_args'} must ensure that
  366. * an object is returned.
  367. *
  368. * The second filter, {@see 'themes_api'}, allows a plugin to override the WordPress.org
  369. * Theme API entirely. If `$action` is 'query_themes', 'theme_information', or 'feature_list',
  370. * an object MUST be passed. If `$action` is 'hot_tags', an array should be passed.
  371. *
  372. * Finally, the third filter, {@see 'themes_api_result'}, makes it possible to filter the
  373. * response object or array, depending on the `$action` type.
  374. *
  375. * Supported arguments per action:
  376. *
  377. * | Argument Name | 'query_themes' | 'theme_information' | 'hot_tags' | 'feature_list' |
  378. * | -------------------| :------------: | :-----------------: | :--------: | :--------------: |
  379. * | `$slug` | No | Yes | No | No |
  380. * | `$per_page` | Yes | No | No | No |
  381. * | `$page` | Yes | No | No | No |
  382. * | `$number` | No | No | Yes | No |
  383. * | `$search` | Yes | No | No | No |
  384. * | `$tag` | Yes | No | No | No |
  385. * | `$author` | Yes | No | No | No |
  386. * | `$user` | Yes | No | No | No |
  387. * | `$browse` | Yes | No | No | No |
  388. * | `$locale` | Yes | Yes | No | No |
  389. * | `$fields` | Yes | Yes | No | No |
  390. *
  391. * @since 2.8.0
  392. *
  393. * @param string $action API action to perform: 'query_themes', 'theme_information',
  394. * 'hot_tags' or 'feature_list'.
  395. * @param array|object $args {
  396. * Optional. Array or object of arguments to serialize for the Themes API.
  397. *
  398. * @type string $slug The theme slug. Default empty.
  399. * @type int $per_page Number of themes per page. Default 24.
  400. * @type int $page Number of current page. Default 1.
  401. * @type int $number Number of tags to be queried.
  402. * @type string $search A search term. Default empty.
  403. * @type string $tag Tag to filter themes. Default empty.
  404. * @type string $author Username of an author to filter themes. Default empty.
  405. * @type string $user Username to query for their favorites. Default empty.
  406. * @type string $browse Browse view: 'featured', 'popular', 'updated', 'favorites'.
  407. * @type string $locale Locale to provide context-sensitive results. Default is the value of get_locale().
  408. * @type array $fields {
  409. * Array of fields which should or should not be returned.
  410. *
  411. * @type bool $description Whether to return the theme full description. Default false.
  412. * @type bool $sections Whether to return the theme readme sections: description, installation,
  413. * FAQ, screenshots, other notes, and changelog. Default false.
  414. * @type bool $rating Whether to return the rating in percent and total number of ratings.
  415. * Default false.
  416. * @type bool $ratings Whether to return the number of rating for each star (1-5). Default false.
  417. * @type bool $downloaded Whether to return the download count. Default false.
  418. * @type bool $downloadlink Whether to return the download link for the package. Default false.
  419. * @type bool $last_updated Whether to return the date of the last update. Default false.
  420. * @type bool $tags Whether to return the assigned tags. Default false.
  421. * @type bool $homepage Whether to return the theme homepage link. Default false.
  422. * @type bool $screenshots Whether to return the screenshots. Default false.
  423. * @type int $screenshot_count Number of screenshots to return. Default 1.
  424. * @type bool $screenshot_url Whether to return the URL of the first screenshot. Default false.
  425. * @type bool $photon_screenshots Whether to return the screenshots via Photon. Default false.
  426. * @type bool $template Whether to return the slug of the parent theme. Default false.
  427. * @type bool $parent Whether to return the slug, name and homepage of the parent theme. Default false.
  428. * @type bool $versions Whether to return the list of all available versions. Default false.
  429. * @type bool $theme_url Whether to return theme's URL. Default false.
  430. * @type bool $extended_author Whether to return nicename or nicename and display name. Default false.
  431. * }
  432. * }
  433. * @return object|array|WP_Error Response object or array on success, WP_Error on failure. See the
  434. * {@link https://developer.wordpress.org/reference/functions/themes_api/ function reference article}
  435. * for more information on the make-up of possible return objects depending on the value of `$action`.
  436. */
  437. function themes_api( $action, $args = array() ) {
  438. // Include an unmodified $wp_version.
  439. require ABSPATH . WPINC . '/version.php';
  440. if ( is_array( $args ) ) {
  441. $args = (object) $args;
  442. }
  443. if ( 'query_themes' === $action ) {
  444. if ( ! isset( $args->per_page ) ) {
  445. $args->per_page = 24;
  446. }
  447. }
  448. if ( ! isset( $args->locale ) ) {
  449. $args->locale = get_user_locale();
  450. }
  451. if ( ! isset( $args->wp_version ) ) {
  452. $args->wp_version = substr( $wp_version, 0, 3 ); // x.y
  453. }
  454. /**
  455. * Filters arguments used to query for installer pages from the WordPress.org Themes API.
  456. *
  457. * Important: An object MUST be returned to this filter.
  458. *
  459. * @since 2.8.0
  460. *
  461. * @param object $args Arguments used to query for installer pages from the WordPress.org Themes API.
  462. * @param string $action Requested action. Likely values are 'theme_information',
  463. * 'feature_list', or 'query_themes'.
  464. */
  465. $args = apply_filters( 'themes_api_args', $args, $action );
  466. /**
  467. * Filters whether to override the WordPress.org Themes API.
  468. *
  469. * Returning a non-false value will effectively short-circuit the WordPress.org API request.
  470. *
  471. * If `$action` is 'query_themes', 'theme_information', or 'feature_list', an object MUST
  472. * be passed. If `$action` is 'hot_tags', an array should be passed.
  473. *
  474. * @since 2.8.0
  475. *
  476. * @param false|object|array $override Whether to override the WordPress.org Themes API. Default false.
  477. * @param string $action Requested action. Likely values are 'theme_information',
  478. * 'feature_list', or 'query_themes'.
  479. * @param object $args Arguments used to query for installer pages from the Themes API.
  480. */
  481. $res = apply_filters( 'themes_api', false, $action, $args );
  482. if ( ! $res ) {
  483. $url = 'http://api.wordpress.org/themes/info/1.2/';
  484. $url = add_query_arg(
  485. array(
  486. 'action' => $action,
  487. 'request' => $args,
  488. ),
  489. $url
  490. );
  491. $http_url = $url;
  492. $ssl = wp_http_supports( array( 'ssl' ) );
  493. if ( $ssl ) {
  494. $url = set_url_scheme( $url, 'https' );
  495. }
  496. $http_args = array(
  497. 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
  498. );
  499. $request = wp_remote_get( $url, $http_args );
  500. if ( $ssl && is_wp_error( $request ) ) {
  501. if ( ! wp_doing_ajax() ) {
  502. trigger_error(
  503. sprintf(
  504. /* translators: %s: Support forums URL. */
  505. __( '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>.' ),
  506. __( 'https://wordpress.org/support/forums/' )
  507. ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
  508. headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
  509. );
  510. }
  511. $request = wp_remote_get( $http_url, $http_args );
  512. }
  513. if ( is_wp_error( $request ) ) {
  514. $res = new WP_Error(
  515. 'themes_api_failed',
  516. sprintf(
  517. /* translators: %s: Support forums URL. */
  518. __( '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>.' ),
  519. __( 'https://wordpress.org/support/forums/' )
  520. ),
  521. $request->get_error_message()
  522. );
  523. } else {
  524. $res = json_decode( wp_remote_retrieve_body( $request ), true );
  525. if ( is_array( $res ) ) {
  526. // Object casting is required in order to match the info/1.0 format.
  527. $res = (object) $res;
  528. } elseif ( null === $res ) {
  529. $res = new WP_Error(
  530. 'themes_api_failed',
  531. sprintf(
  532. /* translators: %s: Support forums URL. */
  533. __( '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>.' ),
  534. __( 'https://wordpress.org/support/forums/' )
  535. ),
  536. wp_remote_retrieve_body( $request )
  537. );
  538. }
  539. if ( isset( $res->error ) ) {
  540. $res = new WP_Error( 'themes_api_failed', $res->error );
  541. }
  542. }
  543. if ( ! is_wp_error( $res ) ) {
  544. // Back-compat for info/1.2 API, upgrade the theme objects in query_themes to objects.
  545. if ( 'query_themes' === $action ) {
  546. foreach ( $res->themes as $i => $theme ) {
  547. $res->themes[ $i ] = (object) $theme;
  548. }
  549. }
  550. // Back-compat for info/1.2 API, downgrade the feature_list result back to an array.
  551. if ( 'feature_list' === $action ) {
  552. $res = (array) $res;
  553. }
  554. }
  555. }
  556. /**
  557. * Filters the returned WordPress.org Themes API response.
  558. *
  559. * @since 2.8.0
  560. *
  561. * @param array|stdClass|WP_Error $res WordPress.org Themes API response.
  562. * @param string $action Requested action. Likely values are 'theme_information',
  563. * 'feature_list', or 'query_themes'.
  564. * @param stdClass $args Arguments used to query for installer pages from the WordPress.org Themes API.
  565. */
  566. return apply_filters( 'themes_api_result', $res, $action, $args );
  567. }
  568. /**
  569. * Prepares themes for JavaScript.
  570. *
  571. * @since 3.8.0
  572. *
  573. * @param WP_Theme[] $themes Optional. Array of theme objects to prepare.
  574. * Defaults to all allowed themes.
  575. *
  576. * @return array An associative array of theme data, sorted by name.
  577. */
  578. function wp_prepare_themes_for_js( $themes = null ) {
  579. $current_theme = get_stylesheet();
  580. /**
  581. * Filters theme data before it is prepared for JavaScript.
  582. *
  583. * Passing a non-empty array will result in wp_prepare_themes_for_js() returning
  584. * early with that value instead.
  585. *
  586. * @since 4.2.0
  587. *
  588. * @param array $prepared_themes An associative array of theme data. Default empty array.
  589. * @param WP_Theme[]|null $themes An array of theme objects to prepare, if any.
  590. * @param string $current_theme The active theme slug.
  591. */
  592. $prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme );
  593. if ( ! empty( $prepared_themes ) ) {
  594. return $prepared_themes;
  595. }
  596. // Make sure the active theme is listed first.
  597. $prepared_themes[ $current_theme ] = array();
  598. if ( null === $themes ) {
  599. $themes = wp_get_themes( array( 'allowed' => true ) );
  600. if ( ! isset( $themes[ $current_theme ] ) ) {
  601. $themes[ $current_theme ] = wp_get_theme();
  602. }
  603. }
  604. $updates = array();
  605. $no_updates = array();
  606. if ( ! is_multisite() && current_user_can( 'update_themes' ) ) {
  607. $updates_transient = get_site_transient( 'update_themes' );
  608. if ( isset( $updates_transient->response ) ) {
  609. $updates = $updates_transient->response;
  610. }
  611. if ( isset( $updates_transient->no_update ) ) {
  612. $no_updates = $updates_transient->no_update;
  613. }
  614. }
  615. WP_Theme::sort_by_name( $themes );
  616. $parents = array();
  617. $auto_updates = (array) get_site_option( 'auto_update_themes', array() );
  618. foreach ( $themes as $theme ) {
  619. $slug = $theme->get_stylesheet();
  620. $encoded_slug = urlencode( $slug );
  621. $parent = false;
  622. if ( $theme->parent() ) {
  623. $parent = $theme->parent();
  624. $parents[ $slug ] = $parent->get_stylesheet();
  625. $parent = $parent->display( 'Name' );
  626. }
  627. $customize_action = null;
  628. $can_edit_theme_options = current_user_can( 'edit_theme_options' );
  629. $can_customize = current_user_can( 'customize' );
  630. $is_block_theme = $theme->is_block_theme();
  631. if ( $is_block_theme && $can_edit_theme_options ) {
  632. $customize_action = esc_url( admin_url( 'site-editor.php' ) );
  633. } elseif ( ! $is_block_theme && $can_customize && $can_edit_theme_options ) {
  634. $customize_action = esc_url(
  635. add_query_arg(
  636. array(
  637. 'return' => urlencode( sanitize_url( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ),
  638. ),
  639. wp_customize_url( $slug )
  640. )
  641. );
  642. }
  643. $update_requires_wp = isset( $updates[ $slug ]['requires'] ) ? $updates[ $slug ]['requires'] : null;
  644. $update_requires_php = isset( $updates[ $slug ]['requires_php'] ) ? $updates[ $slug ]['requires_php'] : null;
  645. $auto_update = in_array( $slug, $auto_updates, true );
  646. $auto_update_action = $auto_update ? 'disable-auto-update' : 'enable-auto-update';
  647. if ( isset( $updates[ $slug ] ) ) {
  648. $auto_update_supported = true;
  649. $auto_update_filter_payload = (object) $updates[ $slug ];
  650. } elseif ( isset( $no_updates[ $slug ] ) ) {
  651. $auto_update_supported = true;
  652. $auto_update_filter_payload = (object) $no_updates[ $slug ];
  653. } else {
  654. $auto_update_supported = false;
  655. /*
  656. * Create the expected payload for the auto_update_theme filter, this is the same data
  657. * as contained within $updates or $no_updates but used when the Theme is not known.
  658. */
  659. $auto_update_filter_payload = (object) array(
  660. 'theme' => $slug,
  661. 'new_version' => $theme->get( 'Version' ),
  662. 'url' => '',
  663. 'package' => '',
  664. 'requires' => $theme->get( 'RequiresWP' ),
  665. 'requires_php' => $theme->get( 'RequiresPHP' ),
  666. );
  667. }
  668. $auto_update_forced = wp_is_auto_update_forced_for_item( 'theme', null, $auto_update_filter_payload );
  669. $prepared_themes[ $slug ] = array(
  670. 'id' => $slug,
  671. 'name' => $theme->display( 'Name' ),
  672. 'screenshot' => array( $theme->get_screenshot() ), // @todo Multiple screenshots.
  673. 'description' => $theme->display( 'Description' ),
  674. 'author' => $theme->display( 'Author', false, true ),
  675. 'authorAndUri' => $theme->display( 'Author' ),
  676. 'tags' => $theme->display( 'Tags' ),
  677. 'version' => $theme->get( 'Version' ),
  678. 'compatibleWP' => is_wp_version_compatible( $theme->get( 'RequiresWP' ) ),
  679. 'compatiblePHP' => is_php_version_compatible( $theme->get( 'RequiresPHP' ) ),
  680. 'updateResponse' => array(
  681. 'compatibleWP' => is_wp_version_compatible( $update_requires_wp ),
  682. 'compatiblePHP' => is_php_version_compatible( $update_requires_php ),
  683. ),
  684. 'parent' => $parent,
  685. 'active' => $slug === $current_theme,
  686. 'hasUpdate' => isset( $updates[ $slug ] ),
  687. 'hasPackage' => isset( $updates[ $slug ] ) && ! empty( $updates[ $slug ]['package'] ),
  688. 'update' => get_theme_update_available( $theme ),
  689. 'autoupdate' => array(
  690. 'enabled' => $auto_update || $auto_update_forced,
  691. 'supported' => $auto_update_supported,
  692. 'forced' => $auto_update_forced,
  693. ),
  694. 'actions' => array(
  695. 'activate' => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&amp;stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
  696. 'customize' => $customize_action,
  697. 'delete' => ( ! is_multisite() && current_user_can( 'delete_themes' ) ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&amp;stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
  698. 'autoupdate' => wp_is_auto_update_enabled_for_type( 'theme' ) && ! is_multisite() && current_user_can( 'update_themes' )
  699. ? wp_nonce_url( admin_url( 'themes.php?action=' . $auto_update_action . '&amp;stylesheet=' . $encoded_slug ), 'updates' )
  700. : null,
  701. ),
  702. 'blockTheme' => $theme->is_block_theme(),
  703. );
  704. }
  705. // Remove 'delete' action if theme has an active child.
  706. if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) {
  707. unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] );
  708. }
  709. /**
  710. * Filters the themes prepared for JavaScript, for themes.php.
  711. *
  712. * Could be useful for changing the order, which is by name by default.
  713. *
  714. * @since 3.8.0
  715. *
  716. * @param array $prepared_themes Array of theme data.
  717. */
  718. $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
  719. $prepared_themes = array_values( $prepared_themes );
  720. return array_filter( $prepared_themes );
  721. }
  722. /**
  723. * Prints JS templates for the theme-browsing UI in the Customizer.
  724. *
  725. * @since 4.2.0
  726. */
  727. function customize_themes_print_templates() {
  728. ?>
  729. <script type="text/html" id="tmpl-customize-themes-details-view">
  730. <div class="theme-backdrop"></div>
  731. <div class="theme-wrap wp-clearfix" role="document">
  732. <div class="theme-header">
  733. <button type="button" class="left dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show previous theme' ); ?></span></button>
  734. <button type="button" class="right dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Show next theme' ); ?></span></button>
  735. <button type="button" class="close dashicons dashicons-no"><span class="screen-reader-text"><?php _e( 'Close details dialog' ); ?></span></button>
  736. </div>
  737. <div class="theme-about wp-clearfix">
  738. <div class="theme-screenshots">
  739. <# if ( data.screenshot && data.screenshot[0] ) { #>
  740. <div class="screenshot"><img src="{{ data.screenshot[0] }}?ver={{ data.version }}" alt="" /></div>
  741. <# } else { #>
  742. <div class="screenshot blank"></div>
  743. <# } #>
  744. </div>
  745. <div class="theme-info">
  746. <# if ( data.active ) { #>
  747. <span class="current-label"><?php _e( 'Active Theme' ); ?></span>
  748. <# } #>
  749. <h2 class="theme-name">{{{ data.name }}}<span class="theme-version">
  750. <?php
  751. /* translators: %s: Theme version. */
  752. printf( __( 'Version: %s' ), '{{ data.version }}' );
  753. ?>
  754. </span></h2>
  755. <h3 class="theme-author">
  756. <?php
  757. /* translators: %s: Theme author link. */
  758. printf( __( 'By %s' ), '{{{ data.authorAndUri }}}' );
  759. ?>
  760. </h3>
  761. <# if ( data.stars && 0 != data.num_ratings ) { #>
  762. <div class="theme-rating">
  763. {{{ data.stars }}}
  764. <a class="num-ratings" target="_blank" href="{{ data.reviews_url }}">
  765. <?php
  766. printf(
  767. '%1$s <span class="screen-reader-text">%2$s</span>',
  768. /* translators: %s: Number of ratings. */
  769. sprintf( __( '(%s ratings)' ), '{{ data.num_ratings }}' ),
  770. /* translators: Accessibility text. */
  771. __( '(opens in a new tab)' )
  772. );
  773. ?>
  774. </a>
  775. </div>
  776. <# } #>
  777. <# if ( data.hasUpdate ) { #>
  778. <# if ( data.updateResponse.compatibleWP && data.updateResponse.compatiblePHP ) { #>
  779. <div class="notice notice-warning notice-alt notice-large" data-slug="{{ data.id }}">
  780. <h3 class="notice-title"><?php _e( 'Update Available' ); ?></h3>
  781. {{{ data.update }}}
  782. </div>
  783. <# } else { #>
  784. <div class="notice notice-error notice-alt notice-large" data-slug="{{ data.id }}">
  785. <h3 class="notice-title"><?php _e( 'Update Incompatible' ); ?></h3>
  786. <p>
  787. <# if ( ! data.updateResponse.compatibleWP && ! data.updateResponse.compatiblePHP ) { #>
  788. <?php
  789. printf(
  790. /* translators: %s: Theme name. */
  791. __( 'There is a new version of %s available, but it does not work with your versions of WordPress and PHP.' ),
  792. '{{{ data.name }}}'
  793. );
  794. if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
  795. printf(
  796. /* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
  797. ' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
  798. self_admin_url( 'update-core.php' ),
  799. esc_url( wp_get_update_php_url() )
  800. );
  801. wp_update_php_annotation( '</p><p><em>', '</em>' );
  802. } elseif ( current_user_can( 'update_core' ) ) {
  803. printf(
  804. /* translators: %s: URL to WordPress Updates screen. */
  805. ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
  806. self_admin_url( 'update-core.php' )
  807. );
  808. } elseif ( current_user_can( 'update_php' ) ) {
  809. printf(
  810. /* translators: %s: URL to Update PHP page. */
  811. ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
  812. esc_url( wp_get_update_php_url() )
  813. );
  814. wp_update_php_annotation( '</p><p><em>', '</em>' );
  815. }
  816. ?>
  817. <# } else if ( ! data.updateResponse.compatibleWP ) { #>
  818. <?php
  819. printf(
  820. /* translators: %s: Theme name. */
  821. __( 'There is a new version of %s available, but it does not work with your version of WordPress.' ),
  822. '{{{ data.name }}}'
  823. );
  824. if ( current_user_can( 'update_core' ) ) {
  825. printf(
  826. /* translators: %s: URL to WordPress Updates screen. */
  827. ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
  828. self_admin_url( 'update-core.php' )
  829. );
  830. }
  831. ?>
  832. <# } else if ( ! data.updateResponse.compatiblePHP ) { #>
  833. <?php
  834. printf(
  835. /* translators: %s: Theme name. */
  836. __( 'There is a new version of %s available, but it does not work with your version of PHP.' ),
  837. '{{{ data.name }}}'
  838. );
  839. if ( current_user_can( 'update_php' ) ) {
  840. printf(
  841. /* translators: %s: URL to Update PHP page. */
  842. ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
  843. esc_url( wp_get_update_php_url() )
  844. );
  845. wp_update_php_annotation( '</p><p><em>', '</em>' );
  846. }
  847. ?>
  848. <# } #>
  849. </p>
  850. </div>
  851. <# } #>
  852. <# } #>
  853. <# if ( data.parent ) { #>
  854. <p class="parent-theme">
  855. <?php
  856. printf(
  857. /* translators: %s: Theme name. */
  858. __( 'This is a child theme of %s.' ),
  859. '<strong>{{{ data.parent }}}</strong>'
  860. );
  861. ?>
  862. </p>
  863. <# } #>
  864. <# if ( ! data.compatibleWP || ! data.compatiblePHP ) { #>
  865. <div class="notice notice-error notice-alt notice-large"><p>
  866. <# if ( ! data.compatibleWP && ! data.compatiblePHP ) { #>
  867. <?php
  868. _e( 'This theme does not work with your versions of WordPress and PHP.' );
  869. if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
  870. printf(
  871. /* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
  872. ' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
  873. self_admin_url( 'update-core.php' ),
  874. esc_url( wp_get_update_php_url() )
  875. );
  876. wp_update_php_annotation( '</p><p><em>', '</em>' );
  877. } elseif ( current_user_can( 'update_core' ) ) {
  878. printf(
  879. /* translators: %s: URL to WordPress Updates screen. */
  880. ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
  881. self_admin_url( 'update-core.php' )
  882. );
  883. } elseif ( current_user_can( 'update_php' ) ) {
  884. printf(
  885. /* translators: %s: URL to Update PHP page. */
  886. ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
  887. esc_url( wp_get_update_php_url() )
  888. );
  889. wp_update_php_annotation( '</p><p><em>', '</em>' );
  890. }
  891. ?>
  892. <# } else if ( ! data.compatibleWP ) { #>
  893. <?php
  894. _e( 'This theme does not work with your version of WordPress.' );
  895. if ( current_user_can( 'update_core' ) ) {
  896. printf(
  897. /* translators: %s: URL to WordPress Updates screen. */
  898. ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
  899. self_admin_url( 'update-core.php' )
  900. );
  901. }
  902. ?>
  903. <# } else if ( ! data.compatiblePHP ) { #>
  904. <?php
  905. _e( 'This theme does not work with your version of PHP.' );
  906. if ( current_user_can( 'update_php' ) ) {
  907. printf(
  908. /* translators: %s: URL to Update PHP page. */
  909. ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
  910. esc_url( wp_get_update_php_url() )
  911. );
  912. wp_update_php_annotation( '</p><p><em>', '</em>' );
  913. }
  914. ?>
  915. <# } #>
  916. </p></div>
  917. <# } else if ( ! data.active && data.blockTheme ) { #>
  918. <div class="notice notice-error notice-alt notice-large"><p>
  919. <?php
  920. _e( 'This theme doesn\'t support Customizer.' );
  921. ?>
  922. <# if ( data.actions.activate ) { #>
  923. <?php
  924. printf(
  925. /* translators: %s: URL to the themes page (also it activates the theme). */
  926. ' ' . __( 'However, you can still <a href="%s">activate this theme</a>, and use the Site Editor to customize it.' ),
  927. '{{{ data.actions.activate }}}'
  928. );
  929. ?>
  930. <# } #>
  931. </p></div>
  932. <# } #>
  933. <p class="theme-description">{{{ data.description }}}</p>
  934. <# if ( data.tags ) { #>
  935. <p class="theme-tags"><span><?php _e( 'Tags:' ); ?></span> {{{ data.tags }}}</p>
  936. <# } #>
  937. </div>
  938. </div>
  939. <div class="theme-actions">
  940. <# if ( data.active ) { #>
  941. <button type="button" class="button button-primary customize-theme"><?php _e( 'Customize' ); ?></button>
  942. <# } else if ( 'installed' === data.type ) { #>
  943. <?php if ( current_user_can( 'delete_themes' ) ) { ?>
  944. <# if ( data.actions && data.actions['delete'] ) { #>
  945. <a href="{{{ data.actions['delete'] }}}" data-slug="{{ data.id }}" class="button button-secondary delete-theme"><?php _e( 'Delete' ); ?></a>
  946. <# } #>
  947. <?php } ?>
  948. <# if ( data.blockTheme ) { #>
  949. <?php
  950. /* translators: %s: Theme name. */
  951. $aria_label = sprintf( _x( 'Activate %s', 'theme' ), '{{ data.name }}' );
  952. ?>
  953. <# if ( data.compatibleWP && data.compatiblePHP && data.actions.activate ) { #>
  954. <a href="{{{ data.actions.activate }}}" class="button button-primary activate" aria-label="<?php echo esc_attr( $aria_label ); ?>"><?php _e( 'Activate' ); ?></a>
  955. <# } #>
  956. <# } else { #>
  957. <# if ( data.compatibleWP && data.compatiblePHP ) { #>
  958. <button type="button" class="button button-primary preview-theme" data-slug="{{ data.id }}"><?php _e( 'Live Preview' ); ?></button>
  959. <# } else { #>
  960. <button class="button button-primary disabled"><?php _e( 'Live Preview' ); ?></button>
  961. <# } #>
  962. <# } #>
  963. <# } else { #>
  964. <# if ( data.compatibleWP && data.compatiblePHP ) { #>
  965. <button type="button" class="button theme-install" data-slug="{{ data.id }}"><?php _e( 'Install' ); ?></button>
  966. <button type="button" class="button button-primary theme-install preview" data-slug="{{ data.id }}"><?php _e( 'Install &amp; Preview' ); ?></button>
  967. <# } else { #>
  968. <button type="button" class="button disabled"><?php _ex( 'Cannot Install', 'theme' ); ?></button>
  969. <button type="button" class="button button-primary disabled"><?php _e( 'Install &amp; Preview' ); ?></button>
  970. <# } #>
  971. <# } #>
  972. </div>
  973. </div>
  974. </script>
  975. <?php
  976. }
  977. /**
  978. * Determines whether a theme is technically active but was paused while
  979. * loading.
  980. *
  981. * For more information on this and similar theme functions, check out
  982. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  983. * Conditional Tags} article in the Theme Developer Handbook.
  984. *
  985. * @since 5.2.0
  986. *
  987. * @param string $theme Path to the theme directory relative to the themes directory.
  988. * @return bool True, if in the list of paused themes. False, not in the list.
  989. */
  990. function is_theme_paused( $theme ) {
  991. if ( ! isset( $GLOBALS['_paused_themes'] ) ) {
  992. return false;
  993. }
  994. if ( get_stylesheet() !== $theme && get_template() !== $theme ) {
  995. return false;
  996. }
  997. return array_key_exists( $theme, $GLOBALS['_paused_themes'] );
  998. }
  999. /**
  1000. * Gets the error that was recorded for a paused theme.
  1001. *
  1002. * @since 5.2.0
  1003. *
  1004. * @param string $theme Path to the theme directory relative to the themes
  1005. * directory.
  1006. * @return array|false Array of error information as it was returned by
  1007. * `error_get_last()`, or false if none was recorded.
  1008. */
  1009. function wp_get_theme_error( $theme ) {
  1010. if ( ! isset( $GLOBALS['_paused_themes'] ) ) {
  1011. return false;
  1012. }
  1013. if ( ! array_key_exists( $theme, $GLOBALS['_paused_themes'] ) ) {
  1014. return false;
  1015. }
  1016. return $GLOBALS['_paused_themes'][ $theme ];
  1017. }
  1018. /**
  1019. * Tries to resume a single theme.
  1020. *
  1021. * If a redirect was provided and a functions.php file was found, we first ensure that
  1022. * functions.php file does not throw fatal errors anymore.
  1023. *
  1024. * The way it works is by setting the redirection to the error before trying to
  1025. * include the file. If the theme fails, then the redirection will not be overwritten
  1026. * with the success message and the theme will not be resumed.
  1027. *
  1028. * @since 5.2.0
  1029. *
  1030. * @param string $theme Single theme to resume.
  1031. * @param string $redirect Optional. URL to redirect to. Default empty string.
  1032. * @return bool|WP_Error True on success, false if `$theme` was not paused,
  1033. * `WP_Error` on failure.
  1034. */
  1035. function resume_theme( $theme, $redirect = '' ) {
  1036. list( $extension ) = explode( '/', $theme );
  1037. /*
  1038. * We'll override this later if the theme could be resumed without
  1039. * creating a fatal error.
  1040. */
  1041. if ( ! empty( $redirect ) ) {
  1042. $functions_path = '';
  1043. if ( strpos( STYLESHEETPATH, $extension ) ) {
  1044. $functions_path = STYLESHEETPATH . '/functions.php';
  1045. } elseif ( strpos( TEMPLATEPATH, $extension ) ) {
  1046. $functions_path = TEMPLATEPATH . '/functions.php';
  1047. }
  1048. if ( ! empty( $functions_path ) ) {
  1049. wp_redirect(
  1050. add_query_arg(
  1051. '_error_nonce',
  1052. wp_create_nonce( 'theme-resume-error_' . $theme ),
  1053. $redirect
  1054. )
  1055. );
  1056. // Load the theme's functions.php to test whether it throws a fatal error.
  1057. ob_start();
  1058. if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) {
  1059. define( 'WP_SANDBOX_SCRAPING', true );
  1060. }
  1061. include $functions_path;
  1062. ob_clean();
  1063. }
  1064. }
  1065. $result = wp_paused_themes()->delete( $extension );
  1066. if ( ! $result ) {
  1067. return new WP_Error(
  1068. 'could_not_resume_theme',
  1069. __( 'Could not resume the theme.' )
  1070. );
  1071. }
  1072. return true;
  1073. }
  1074. /**
  1075. * Renders an admin notice in case some themes have been paused due to errors.
  1076. *
  1077. * @since 5.2.0
  1078. *
  1079. * @global string $pagenow The filename of the current screen.
  1080. */
  1081. function paused_themes_notice() {
  1082. if ( 'themes.php' === $GLOBALS['pagenow'] ) {
  1083. return;
  1084. }
  1085. if ( ! current_user_can( 'resume_themes' ) ) {
  1086. return;
  1087. }
  1088. if ( ! isset( $GLOBALS['_paused_themes'] ) || empty( $GLOBALS['_paused_themes'] ) ) {
  1089. return;
  1090. }
  1091. printf(
  1092. '<div class="notice notice-error"><p><strong>%s</strong><br>%s</p><p><a href="%s">%s</a></p></div>',
  1093. __( 'One or more themes failed to load properly.' ),
  1094. __( 'You can find more details and make changes on the Themes screen.' ),
  1095. esc_url( admin_url( 'themes.php' ) ),
  1096. __( 'Go to the Themes screen' )
  1097. );
  1098. }