Nav apraksta

theme.php 45KB

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