暫無描述

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

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