class-wp-ms-themes-list-table.php 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. <?php
  2. /**
  3. * List Table API: WP_MS_Themes_List_Table class
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 3.1.0
  8. */
  9. /**
  10. * Core class used to implement displaying themes in a list table for the network admin.
  11. *
  12. * @since 3.1.0
  13. *
  14. * @see WP_List_Table
  15. */
  16. class WP_MS_Themes_List_Table extends WP_List_Table {
  17. public $site_id;
  18. public $is_site_themes;
  19. private $has_items;
  20. /**
  21. * Whether to show the auto-updates UI.
  22. *
  23. * @since 5.5.0
  24. *
  25. * @var bool True if auto-updates UI is to be shown, false otherwise.
  26. */
  27. protected $show_autoupdates = true;
  28. /**
  29. * Constructor.
  30. *
  31. * @since 3.1.0
  32. *
  33. * @see WP_List_Table::__construct() for more information on default arguments.
  34. *
  35. * @global string $status
  36. * @global int $page
  37. *
  38. * @param array $args An associative array of arguments.
  39. */
  40. public function __construct( $args = array() ) {
  41. global $status, $page;
  42. parent::__construct(
  43. array(
  44. 'plural' => 'themes',
  45. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  46. )
  47. );
  48. $status = isset( $_REQUEST['theme_status'] ) ? $_REQUEST['theme_status'] : 'all';
  49. if ( ! in_array( $status, array( 'all', 'enabled', 'disabled', 'upgrade', 'search', 'broken', 'auto-update-enabled', 'auto-update-disabled' ), true ) ) {
  50. $status = 'all';
  51. }
  52. $page = $this->get_pagenum();
  53. $this->is_site_themes = ( 'site-themes-network' === $this->screen->id ) ? true : false;
  54. if ( $this->is_site_themes ) {
  55. $this->site_id = isset( $_REQUEST['id'] ) ? (int) $_REQUEST['id'] : 0;
  56. }
  57. $this->show_autoupdates = wp_is_auto_update_enabled_for_type( 'theme' ) &&
  58. ! $this->is_site_themes && current_user_can( 'update_themes' );
  59. }
  60. /**
  61. * @return array
  62. */
  63. protected function get_table_classes() {
  64. // @todo Remove and add CSS for .themes.
  65. return array( 'widefat', 'plugins' );
  66. }
  67. /**
  68. * @return bool
  69. */
  70. public function ajax_user_can() {
  71. if ( $this->is_site_themes ) {
  72. return current_user_can( 'manage_sites' );
  73. } else {
  74. return current_user_can( 'manage_network_themes' );
  75. }
  76. }
  77. /**
  78. * @global string $status
  79. * @global array $totals
  80. * @global int $page
  81. * @global string $orderby
  82. * @global string $order
  83. * @global string $s
  84. */
  85. public function prepare_items() {
  86. global $status, $totals, $page, $orderby, $order, $s;
  87. wp_reset_vars( array( 'orderby', 'order', 's' ) );
  88. $themes = array(
  89. /**
  90. * Filters the full array of WP_Theme objects to list in the Multisite
  91. * themes list table.
  92. *
  93. * @since 3.1.0
  94. *
  95. * @param WP_Theme[] $all Array of WP_Theme objects to display in the list table.
  96. */
  97. 'all' => apply_filters( 'all_themes', wp_get_themes() ),
  98. 'search' => array(),
  99. 'enabled' => array(),
  100. 'disabled' => array(),
  101. 'upgrade' => array(),
  102. 'broken' => $this->is_site_themes ? array() : wp_get_themes( array( 'errors' => true ) ),
  103. );
  104. if ( $this->show_autoupdates ) {
  105. $auto_updates = (array) get_site_option( 'auto_update_themes', array() );
  106. $themes['auto-update-enabled'] = array();
  107. $themes['auto-update-disabled'] = array();
  108. }
  109. if ( $this->is_site_themes ) {
  110. $themes_per_page = $this->get_items_per_page( 'site_themes_network_per_page' );
  111. $allowed_where = 'site';
  112. } else {
  113. $themes_per_page = $this->get_items_per_page( 'themes_network_per_page' );
  114. $allowed_where = 'network';
  115. }
  116. $current = get_site_transient( 'update_themes' );
  117. $maybe_update = current_user_can( 'update_themes' ) && ! $this->is_site_themes && $current;
  118. foreach ( (array) $themes['all'] as $key => $theme ) {
  119. if ( $this->is_site_themes && $theme->is_allowed( 'network' ) ) {
  120. unset( $themes['all'][ $key ] );
  121. continue;
  122. }
  123. if ( $maybe_update && isset( $current->response[ $key ] ) ) {
  124. $themes['all'][ $key ]->update = true;
  125. $themes['upgrade'][ $key ] = $themes['all'][ $key ];
  126. }
  127. $filter = $theme->is_allowed( $allowed_where, $this->site_id ) ? 'enabled' : 'disabled';
  128. $themes[ $filter ][ $key ] = $themes['all'][ $key ];
  129. $theme_data = array(
  130. 'update_supported' => isset( $theme->update_supported ) ? $theme->update_supported : true,
  131. );
  132. // Extra info if known. array_merge() ensures $theme_data has precedence if keys collide.
  133. if ( isset( $current->response[ $key ] ) ) {
  134. $theme_data = array_merge( (array) $current->response[ $key ], $theme_data );
  135. } elseif ( isset( $current->no_update[ $key ] ) ) {
  136. $theme_data = array_merge( (array) $current->no_update[ $key ], $theme_data );
  137. } else {
  138. $theme_data['update_supported'] = false;
  139. }
  140. $theme->update_supported = $theme_data['update_supported'];
  141. /*
  142. * Create the expected payload for the auto_update_theme filter, this is the same data
  143. * as contained within $updates or $no_updates but used when the Theme is not known.
  144. */
  145. $filter_payload = array(
  146. 'theme' => $key,
  147. 'new_version' => '',
  148. 'url' => '',
  149. 'package' => '',
  150. 'requires' => '',
  151. 'requires_php' => '',
  152. );
  153. $filter_payload = (object) array_merge( $filter_payload, array_intersect_key( $theme_data, $filter_payload ) );
  154. $auto_update_forced = wp_is_auto_update_forced_for_item( 'theme', null, $filter_payload );
  155. if ( ! is_null( $auto_update_forced ) ) {
  156. $theme->auto_update_forced = $auto_update_forced;
  157. }
  158. if ( $this->show_autoupdates ) {
  159. $enabled = in_array( $key, $auto_updates, true ) && $theme->update_supported;
  160. if ( isset( $theme->auto_update_forced ) ) {
  161. $enabled = (bool) $theme->auto_update_forced;
  162. }
  163. if ( $enabled ) {
  164. $themes['auto-update-enabled'][ $key ] = $theme;
  165. } else {
  166. $themes['auto-update-disabled'][ $key ] = $theme;
  167. }
  168. }
  169. }
  170. if ( $s ) {
  171. $status = 'search';
  172. $themes['search'] = array_filter( array_merge( $themes['all'], $themes['broken'] ), array( $this, '_search_callback' ) );
  173. }
  174. $totals = array();
  175. $js_themes = array();
  176. foreach ( $themes as $type => $list ) {
  177. $totals[ $type ] = count( $list );
  178. $js_themes[ $type ] = array_keys( $list );
  179. }
  180. if ( empty( $themes[ $status ] ) && ! in_array( $status, array( 'all', 'search' ), true ) ) {
  181. $status = 'all';
  182. }
  183. $this->items = $themes[ $status ];
  184. WP_Theme::sort_by_name( $this->items );
  185. $this->has_items = ! empty( $themes['all'] );
  186. $total_this_page = $totals[ $status ];
  187. wp_localize_script(
  188. 'updates',
  189. '_wpUpdatesItemCounts',
  190. array(
  191. 'themes' => $js_themes,
  192. 'totals' => wp_get_update_data(),
  193. )
  194. );
  195. if ( $orderby ) {
  196. $orderby = ucfirst( $orderby );
  197. $order = strtoupper( $order );
  198. if ( 'Name' === $orderby ) {
  199. if ( 'ASC' === $order ) {
  200. $this->items = array_reverse( $this->items );
  201. }
  202. } else {
  203. uasort( $this->items, array( $this, '_order_callback' ) );
  204. }
  205. }
  206. $start = ( $page - 1 ) * $themes_per_page;
  207. if ( $total_this_page > $themes_per_page ) {
  208. $this->items = array_slice( $this->items, $start, $themes_per_page, true );
  209. }
  210. $this->set_pagination_args(
  211. array(
  212. 'total_items' => $total_this_page,
  213. 'per_page' => $themes_per_page,
  214. )
  215. );
  216. }
  217. /**
  218. * @param WP_Theme $theme
  219. * @return bool
  220. */
  221. public function _search_callback( $theme ) {
  222. static $term = null;
  223. if ( is_null( $term ) ) {
  224. $term = wp_unslash( $_REQUEST['s'] );
  225. }
  226. foreach ( array( 'Name', 'Description', 'Author', 'Author', 'AuthorURI' ) as $field ) {
  227. // Don't mark up; Do translate.
  228. if ( false !== stripos( $theme->display( $field, false, true ), $term ) ) {
  229. return true;
  230. }
  231. }
  232. if ( false !== stripos( $theme->get_stylesheet(), $term ) ) {
  233. return true;
  234. }
  235. if ( false !== stripos( $theme->get_template(), $term ) ) {
  236. return true;
  237. }
  238. return false;
  239. }
  240. // Not used by any core columns.
  241. /**
  242. * @global string $orderby
  243. * @global string $order
  244. * @param array $theme_a
  245. * @param array $theme_b
  246. * @return int
  247. */
  248. public function _order_callback( $theme_a, $theme_b ) {
  249. global $orderby, $order;
  250. $a = $theme_a[ $orderby ];
  251. $b = $theme_b[ $orderby ];
  252. if ( $a === $b ) {
  253. return 0;
  254. }
  255. if ( 'DESC' === $order ) {
  256. return ( $a < $b ) ? 1 : -1;
  257. } else {
  258. return ( $a < $b ) ? -1 : 1;
  259. }
  260. }
  261. /**
  262. */
  263. public function no_items() {
  264. if ( $this->has_items ) {
  265. _e( 'No themes found.' );
  266. } else {
  267. _e( 'No themes are currently available.' );
  268. }
  269. }
  270. /**
  271. * @return array
  272. */
  273. public function get_columns() {
  274. $columns = array(
  275. 'cb' => '<input type="checkbox" />',
  276. 'name' => __( 'Theme' ),
  277. 'description' => __( 'Description' ),
  278. );
  279. if ( $this->show_autoupdates ) {
  280. $columns['auto-updates'] = __( 'Automatic Updates' );
  281. }
  282. return $columns;
  283. }
  284. /**
  285. * @return array
  286. */
  287. protected function get_sortable_columns() {
  288. return array(
  289. 'name' => 'name',
  290. );
  291. }
  292. /**
  293. * Gets the name of the primary column.
  294. *
  295. * @since 4.3.0
  296. *
  297. * @return string Unalterable name of the primary column name, in this case, 'name'.
  298. */
  299. protected function get_primary_column_name() {
  300. return 'name';
  301. }
  302. /**
  303. * @global array $totals
  304. * @global string $status
  305. * @return array
  306. */
  307. protected function get_views() {
  308. global $totals, $status;
  309. $status_links = array();
  310. foreach ( $totals as $type => $count ) {
  311. if ( ! $count ) {
  312. continue;
  313. }
  314. switch ( $type ) {
  315. case 'all':
  316. /* translators: %s: Number of themes. */
  317. $text = _nx(
  318. 'All <span class="count">(%s)</span>',
  319. 'All <span class="count">(%s)</span>',
  320. $count,
  321. 'themes'
  322. );
  323. break;
  324. case 'enabled':
  325. /* translators: %s: Number of themes. */
  326. $text = _nx(
  327. 'Enabled <span class="count">(%s)</span>',
  328. 'Enabled <span class="count">(%s)</span>',
  329. $count,
  330. 'themes'
  331. );
  332. break;
  333. case 'disabled':
  334. /* translators: %s: Number of themes. */
  335. $text = _nx(
  336. 'Disabled <span class="count">(%s)</span>',
  337. 'Disabled <span class="count">(%s)</span>',
  338. $count,
  339. 'themes'
  340. );
  341. break;
  342. case 'upgrade':
  343. /* translators: %s: Number of themes. */
  344. $text = _nx(
  345. 'Update Available <span class="count">(%s)</span>',
  346. 'Update Available <span class="count">(%s)</span>',
  347. $count,
  348. 'themes'
  349. );
  350. break;
  351. case 'broken':
  352. /* translators: %s: Number of themes. */
  353. $text = _nx(
  354. 'Broken <span class="count">(%s)</span>',
  355. 'Broken <span class="count">(%s)</span>',
  356. $count,
  357. 'themes'
  358. );
  359. break;
  360. case 'auto-update-enabled':
  361. /* translators: %s: Number of themes. */
  362. $text = _n(
  363. 'Auto-updates Enabled <span class="count">(%s)</span>',
  364. 'Auto-updates Enabled <span class="count">(%s)</span>',
  365. $count
  366. );
  367. break;
  368. case 'auto-update-disabled':
  369. /* translators: %s: Number of themes. */
  370. $text = _n(
  371. 'Auto-updates Disabled <span class="count">(%s)</span>',
  372. 'Auto-updates Disabled <span class="count">(%s)</span>',
  373. $count
  374. );
  375. break;
  376. }
  377. if ( $this->is_site_themes ) {
  378. $url = 'site-themes.php?id=' . $this->site_id;
  379. } else {
  380. $url = 'themes.php';
  381. }
  382. if ( 'search' !== $type ) {
  383. $status_links[ $type ] = array(
  384. 'url' => esc_url( add_query_arg( 'theme_status', $type, $url ) ),
  385. 'label' => sprintf( $text, number_format_i18n( $count ) ),
  386. 'current' => $type === $status,
  387. );
  388. }
  389. }
  390. return $this->get_views_links( $status_links );
  391. }
  392. /**
  393. * @global string $status
  394. *
  395. * @return array
  396. */
  397. protected function get_bulk_actions() {
  398. global $status;
  399. $actions = array();
  400. if ( 'enabled' !== $status ) {
  401. $actions['enable-selected'] = $this->is_site_themes ? __( 'Enable' ) : __( 'Network Enable' );
  402. }
  403. if ( 'disabled' !== $status ) {
  404. $actions['disable-selected'] = $this->is_site_themes ? __( 'Disable' ) : __( 'Network Disable' );
  405. }
  406. if ( ! $this->is_site_themes ) {
  407. if ( current_user_can( 'update_themes' ) ) {
  408. $actions['update-selected'] = __( 'Update' );
  409. }
  410. if ( current_user_can( 'delete_themes' ) ) {
  411. $actions['delete-selected'] = __( 'Delete' );
  412. }
  413. }
  414. if ( $this->show_autoupdates ) {
  415. if ( 'auto-update-enabled' !== $status ) {
  416. $actions['enable-auto-update-selected'] = __( 'Enable Auto-updates' );
  417. }
  418. if ( 'auto-update-disabled' !== $status ) {
  419. $actions['disable-auto-update-selected'] = __( 'Disable Auto-updates' );
  420. }
  421. }
  422. return $actions;
  423. }
  424. /**
  425. */
  426. public function display_rows() {
  427. foreach ( $this->items as $theme ) {
  428. $this->single_row( $theme );
  429. }
  430. }
  431. /**
  432. * Handles the checkbox column output.
  433. *
  434. * @since 4.3.0
  435. * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support.
  436. *
  437. * @param WP_Theme $item The current WP_Theme object.
  438. */
  439. public function column_cb( $item ) {
  440. // Restores the more descriptive, specific name for use within this method.
  441. $theme = $item;
  442. $checkbox_id = 'checkbox_' . md5( $theme->get( 'Name' ) );
  443. ?>
  444. <input type="checkbox" name="checked[]" value="<?php echo esc_attr( $theme->get_stylesheet() ); ?>" id="<?php echo $checkbox_id; ?>" />
  445. <label class="screen-reader-text" for="<?php echo $checkbox_id; ?>" ><?php _e( 'Select' ); ?> <?php echo $theme->display( 'Name' ); ?></label>
  446. <?php
  447. }
  448. /**
  449. * Handles the name column output.
  450. *
  451. * @since 4.3.0
  452. *
  453. * @global string $status
  454. * @global int $page
  455. * @global string $s
  456. *
  457. * @param WP_Theme $theme The current WP_Theme object.
  458. */
  459. public function column_name( $theme ) {
  460. global $status, $page, $s;
  461. $context = $status;
  462. if ( $this->is_site_themes ) {
  463. $url = "site-themes.php?id={$this->site_id}&amp;";
  464. $allowed = $theme->is_allowed( 'site', $this->site_id );
  465. } else {
  466. $url = 'themes.php?';
  467. $allowed = $theme->is_allowed( 'network' );
  468. }
  469. // Pre-order.
  470. $actions = array(
  471. 'enable' => '',
  472. 'disable' => '',
  473. 'delete' => '',
  474. );
  475. $stylesheet = $theme->get_stylesheet();
  476. $theme_key = urlencode( $stylesheet );
  477. if ( ! $allowed ) {
  478. if ( ! $theme->errors() ) {
  479. $url = add_query_arg(
  480. array(
  481. 'action' => 'enable',
  482. 'theme' => $theme_key,
  483. 'paged' => $page,
  484. 's' => $s,
  485. ),
  486. $url
  487. );
  488. if ( $this->is_site_themes ) {
  489. /* translators: %s: Theme name. */
  490. $aria_label = sprintf( __( 'Enable %s' ), $theme->display( 'Name' ) );
  491. } else {
  492. /* translators: %s: Theme name. */
  493. $aria_label = sprintf( __( 'Network Enable %s' ), $theme->display( 'Name' ) );
  494. }
  495. $actions['enable'] = sprintf(
  496. '<a href="%s" class="edit" aria-label="%s">%s</a>',
  497. esc_url( wp_nonce_url( $url, 'enable-theme_' . $stylesheet ) ),
  498. esc_attr( $aria_label ),
  499. ( $this->is_site_themes ? __( 'Enable' ) : __( 'Network Enable' ) )
  500. );
  501. }
  502. } else {
  503. $url = add_query_arg(
  504. array(
  505. 'action' => 'disable',
  506. 'theme' => $theme_key,
  507. 'paged' => $page,
  508. 's' => $s,
  509. ),
  510. $url
  511. );
  512. if ( $this->is_site_themes ) {
  513. /* translators: %s: Theme name. */
  514. $aria_label = sprintf( __( 'Disable %s' ), $theme->display( 'Name' ) );
  515. } else {
  516. /* translators: %s: Theme name. */
  517. $aria_label = sprintf( __( 'Network Disable %s' ), $theme->display( 'Name' ) );
  518. }
  519. $actions['disable'] = sprintf(
  520. '<a href="%s" aria-label="%s">%s</a>',
  521. esc_url( wp_nonce_url( $url, 'disable-theme_' . $stylesheet ) ),
  522. esc_attr( $aria_label ),
  523. ( $this->is_site_themes ? __( 'Disable' ) : __( 'Network Disable' ) )
  524. );
  525. }
  526. if ( ! $allowed && ! $this->is_site_themes
  527. && current_user_can( 'delete_themes' )
  528. && get_option( 'stylesheet' ) !== $stylesheet
  529. && get_option( 'template' ) !== $stylesheet
  530. ) {
  531. $url = add_query_arg(
  532. array(
  533. 'action' => 'delete-selected',
  534. 'checked[]' => $theme_key,
  535. 'theme_status' => $context,
  536. 'paged' => $page,
  537. 's' => $s,
  538. ),
  539. 'themes.php'
  540. );
  541. /* translators: %s: Theme name. */
  542. $aria_label = sprintf( _x( 'Delete %s', 'theme' ), $theme->display( 'Name' ) );
  543. $actions['delete'] = sprintf(
  544. '<a href="%s" class="delete" aria-label="%s">%s</a>',
  545. esc_url( wp_nonce_url( $url, 'bulk-themes' ) ),
  546. esc_attr( $aria_label ),
  547. __( 'Delete' )
  548. );
  549. }
  550. /**
  551. * Filters the action links displayed for each theme in the Multisite
  552. * themes list table.
  553. *
  554. * The action links displayed are determined by the theme's status, and
  555. * which Multisite themes list table is being displayed - the Network
  556. * themes list table (themes.php), which displays all installed themes,
  557. * or the Site themes list table (site-themes.php), which displays the
  558. * non-network enabled themes when editing a site in the Network admin.
  559. *
  560. * The default action links for the Network themes list table include
  561. * 'Network Enable', 'Network Disable', and 'Delete'.
  562. *
  563. * The default action links for the Site themes list table include
  564. * 'Enable', and 'Disable'.
  565. *
  566. * @since 2.8.0
  567. *
  568. * @param string[] $actions An array of action links.
  569. * @param WP_Theme $theme The current WP_Theme object.
  570. * @param string $context Status of the theme, one of 'all', 'enabled', or 'disabled'.
  571. */
  572. $actions = apply_filters( 'theme_action_links', array_filter( $actions ), $theme, $context );
  573. /**
  574. * Filters the action links of a specific theme in the Multisite themes
  575. * list table.
  576. *
  577. * The dynamic portion of the hook name, `$stylesheet`, refers to the
  578. * directory name of the theme, which in most cases is synonymous
  579. * with the template name.
  580. *
  581. * @since 3.1.0
  582. *
  583. * @param string[] $actions An array of action links.
  584. * @param WP_Theme $theme The current WP_Theme object.
  585. * @param string $context Status of the theme, one of 'all', 'enabled', or 'disabled'.
  586. */
  587. $actions = apply_filters( "theme_action_links_{$stylesheet}", $actions, $theme, $context );
  588. echo $this->row_actions( $actions, true );
  589. }
  590. /**
  591. * Handles the description column output.
  592. *
  593. * @since 4.3.0
  594. *
  595. * @global string $status
  596. * @global array $totals
  597. *
  598. * @param WP_Theme $theme The current WP_Theme object.
  599. */
  600. public function column_description( $theme ) {
  601. global $status, $totals;
  602. if ( $theme->errors() ) {
  603. $pre = 'broken' === $status ? __( 'Broken Theme:' ) . ' ' : '';
  604. echo '<p><strong class="error-message">' . $pre . $theme->errors()->get_error_message() . '</strong></p>';
  605. }
  606. if ( $this->is_site_themes ) {
  607. $allowed = $theme->is_allowed( 'site', $this->site_id );
  608. } else {
  609. $allowed = $theme->is_allowed( 'network' );
  610. }
  611. $class = ! $allowed ? 'inactive' : 'active';
  612. if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) ) {
  613. $class .= ' update';
  614. }
  615. echo "<div class='theme-description'><p>" . $theme->display( 'Description' ) . "</p></div>
  616. <div class='$class second theme-version-author-uri'>";
  617. $stylesheet = $theme->get_stylesheet();
  618. $theme_meta = array();
  619. if ( $theme->get( 'Version' ) ) {
  620. /* translators: %s: Theme version. */
  621. $theme_meta[] = sprintf( __( 'Version %s' ), $theme->display( 'Version' ) );
  622. }
  623. /* translators: %s: Theme author. */
  624. $theme_meta[] = sprintf( __( 'By %s' ), $theme->display( 'Author' ) );
  625. if ( $theme->get( 'ThemeURI' ) ) {
  626. /* translators: %s: Theme name. */
  627. $aria_label = sprintf( __( 'Visit theme site for %s' ), $theme->display( 'Name' ) );
  628. $theme_meta[] = sprintf(
  629. '<a href="%s" aria-label="%s">%s</a>',
  630. $theme->display( 'ThemeURI' ),
  631. esc_attr( $aria_label ),
  632. __( 'Visit Theme Site' )
  633. );
  634. }
  635. if ( $theme->parent() ) {
  636. $theme_meta[] = sprintf(
  637. /* translators: %s: Theme name. */
  638. __( 'Child theme of %s' ),
  639. '<strong>' . $theme->parent()->display( 'Name' ) . '</strong>'
  640. );
  641. }
  642. /**
  643. * Filters the array of row meta for each theme in the Multisite themes
  644. * list table.
  645. *
  646. * @since 3.1.0
  647. *
  648. * @param string[] $theme_meta An array of the theme's metadata, including
  649. * the version, author, and theme URI.
  650. * @param string $stylesheet Directory name of the theme.
  651. * @param WP_Theme $theme WP_Theme object.
  652. * @param string $status Status of the theme.
  653. */
  654. $theme_meta = apply_filters( 'theme_row_meta', $theme_meta, $stylesheet, $theme, $status );
  655. echo implode( ' | ', $theme_meta );
  656. echo '</div>';
  657. }
  658. /**
  659. * Handles the auto-updates column output.
  660. *
  661. * @since 5.5.0
  662. *
  663. * @global string $status
  664. * @global int $page
  665. *
  666. * @param WP_Theme $theme The current WP_Theme object.
  667. */
  668. public function column_autoupdates( $theme ) {
  669. global $status, $page;
  670. static $auto_updates, $available_updates;
  671. if ( ! $auto_updates ) {
  672. $auto_updates = (array) get_site_option( 'auto_update_themes', array() );
  673. }
  674. if ( ! $available_updates ) {
  675. $available_updates = get_site_transient( 'update_themes' );
  676. }
  677. $stylesheet = $theme->get_stylesheet();
  678. if ( isset( $theme->auto_update_forced ) ) {
  679. if ( $theme->auto_update_forced ) {
  680. // Forced on.
  681. $text = __( 'Auto-updates enabled' );
  682. } else {
  683. $text = __( 'Auto-updates disabled' );
  684. }
  685. $action = 'unavailable';
  686. $time_class = ' hidden';
  687. } elseif ( empty( $theme->update_supported ) ) {
  688. $text = '';
  689. $action = 'unavailable';
  690. $time_class = ' hidden';
  691. } elseif ( in_array( $stylesheet, $auto_updates, true ) ) {
  692. $text = __( 'Disable auto-updates' );
  693. $action = 'disable';
  694. $time_class = '';
  695. } else {
  696. $text = __( 'Enable auto-updates' );
  697. $action = 'enable';
  698. $time_class = ' hidden';
  699. }
  700. $query_args = array(
  701. 'action' => "{$action}-auto-update",
  702. 'theme' => $stylesheet,
  703. 'paged' => $page,
  704. 'theme_status' => $status,
  705. );
  706. $url = add_query_arg( $query_args, 'themes.php' );
  707. if ( 'unavailable' === $action ) {
  708. $html[] = '<span class="label">' . $text . '</span>';
  709. } else {
  710. $html[] = sprintf(
  711. '<a href="%s" class="toggle-auto-update aria-button-if-js" data-wp-action="%s">',
  712. wp_nonce_url( $url, 'updates' ),
  713. $action
  714. );
  715. $html[] = '<span class="dashicons dashicons-update spin hidden" aria-hidden="true"></span>';
  716. $html[] = '<span class="label">' . $text . '</span>';
  717. $html[] = '</a>';
  718. }
  719. if ( isset( $available_updates->response[ $stylesheet ] ) ) {
  720. $html[] = sprintf(
  721. '<div class="auto-update-time%s">%s</div>',
  722. $time_class,
  723. wp_get_auto_update_message()
  724. );
  725. }
  726. $html = implode( '', $html );
  727. /**
  728. * Filters the HTML of the auto-updates setting for each theme in the Themes list table.
  729. *
  730. * @since 5.5.0
  731. *
  732. * @param string $html The HTML for theme's auto-update setting, including
  733. * toggle auto-update action link and time to next update.
  734. * @param string $stylesheet Directory name of the theme.
  735. * @param WP_Theme $theme WP_Theme object.
  736. */
  737. echo apply_filters( 'theme_auto_update_setting_html', $html, $stylesheet, $theme );
  738. echo '<div class="notice notice-error notice-alt inline hidden"><p></p></div>';
  739. }
  740. /**
  741. * Handles default column output.
  742. *
  743. * @since 4.3.0
  744. * @since 5.9.0 Renamed `$theme` to `$item` to match parent class for PHP 8 named parameter support.
  745. *
  746. * @param WP_Theme $item The current WP_Theme object.
  747. * @param string $column_name The current column name.
  748. */
  749. public function column_default( $item, $column_name ) {
  750. /**
  751. * Fires inside each custom column of the Multisite themes list table.
  752. *
  753. * @since 3.1.0
  754. *
  755. * @param string $column_name Name of the column.
  756. * @param string $stylesheet Directory name of the theme.
  757. * @param WP_Theme $theme Current WP_Theme object.
  758. */
  759. do_action(
  760. 'manage_themes_custom_column',
  761. $column_name,
  762. $item->get_stylesheet(), // Directory name of the theme.
  763. $item // Theme object.
  764. );
  765. }
  766. /**
  767. * Handles the output for a single table row.
  768. *
  769. * @since 4.3.0
  770. *
  771. * @param WP_Theme $item The current WP_Theme object.
  772. */
  773. public function single_row_columns( $item ) {
  774. list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
  775. foreach ( $columns as $column_name => $column_display_name ) {
  776. $extra_classes = '';
  777. if ( in_array( $column_name, $hidden, true ) ) {
  778. $extra_classes .= ' hidden';
  779. }
  780. switch ( $column_name ) {
  781. case 'cb':
  782. echo '<th scope="row" class="check-column">';
  783. $this->column_cb( $item );
  784. echo '</th>';
  785. break;
  786. case 'name':
  787. $active_theme_label = '';
  788. /* The presence of the site_id property means that this is a subsite view and a label for the active theme needs to be added */
  789. if ( ! empty( $this->site_id ) ) {
  790. $stylesheet = get_blog_option( $this->site_id, 'stylesheet' );
  791. $template = get_blog_option( $this->site_id, 'template' );
  792. /* Add a label for the active template */
  793. if ( $item->get_template() === $template ) {
  794. $active_theme_label = ' &mdash; ' . __( 'Active Theme' );
  795. }
  796. /* In case this is a child theme, label it properly */
  797. if ( $stylesheet !== $template && $item->get_stylesheet() === $stylesheet ) {
  798. $active_theme_label = ' &mdash; ' . __( 'Active Child Theme' );
  799. }
  800. }
  801. echo "<td class='theme-title column-primary{$extra_classes}'><strong>" . $item->display( 'Name' ) . $active_theme_label . '</strong>';
  802. $this->column_name( $item );
  803. echo '</td>';
  804. break;
  805. case 'description':
  806. echo "<td class='column-description desc{$extra_classes}'>";
  807. $this->column_description( $item );
  808. echo '</td>';
  809. break;
  810. case 'auto-updates':
  811. echo "<td class='column-auto-updates{$extra_classes}'>";
  812. $this->column_autoupdates( $item );
  813. echo '</td>';
  814. break;
  815. default:
  816. echo "<td class='$column_name column-$column_name{$extra_classes}'>";
  817. $this->column_default( $item, $column_name );
  818. echo '</td>';
  819. break;
  820. }
  821. }
  822. }
  823. /**
  824. * @global string $status
  825. * @global array $totals
  826. *
  827. * @param WP_Theme $theme
  828. */
  829. public function single_row( $theme ) {
  830. global $status, $totals;
  831. if ( $this->is_site_themes ) {
  832. $allowed = $theme->is_allowed( 'site', $this->site_id );
  833. } else {
  834. $allowed = $theme->is_allowed( 'network' );
  835. }
  836. $stylesheet = $theme->get_stylesheet();
  837. $class = ! $allowed ? 'inactive' : 'active';
  838. if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) ) {
  839. $class .= ' update';
  840. }
  841. printf(
  842. '<tr class="%s" data-slug="%s">',
  843. esc_attr( $class ),
  844. esc_attr( $stylesheet )
  845. );
  846. $this->single_row_columns( $theme );
  847. echo '</tr>';
  848. if ( $this->is_site_themes ) {
  849. remove_action( "after_theme_row_$stylesheet", 'wp_theme_update_row' );
  850. }
  851. /**
  852. * Fires after each row in the Multisite themes list table.
  853. *
  854. * @since 3.1.0
  855. *
  856. * @param string $stylesheet Directory name of the theme.
  857. * @param WP_Theme $theme Current WP_Theme object.
  858. * @param string $status Status of the theme.
  859. */
  860. do_action( 'after_theme_row', $stylesheet, $theme, $status );
  861. /**
  862. * Fires after each specific row in the Multisite themes list table.
  863. *
  864. * The dynamic portion of the hook name, `$stylesheet`, refers to the
  865. * directory name of the theme, most often synonymous with the template
  866. * name of the theme.
  867. *
  868. * @since 3.5.0
  869. *
  870. * @param string $stylesheet Directory name of the theme.
  871. * @param WP_Theme $theme Current WP_Theme object.
  872. * @param string $status Status of the theme.
  873. */
  874. do_action( "after_theme_row_{$stylesheet}", $stylesheet, $theme, $status );
  875. }
  876. }