author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* List Table API: WP_Themes_List_Table class |
0 | 4 |
* |
5 |
* @package WordPress |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6 |
* @subpackage Administration |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* @since 3.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
8 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
9 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
10 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
* Core class used to implement displaying installed themes in a list table. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
12 |
* |
0 | 13 |
* @since 3.1.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* @see WP_List_Table |
0 | 16 |
*/ |
17 |
class WP_Themes_List_Table extends WP_List_Table { |
|
18 |
||
19 |
protected $search_terms = array(); |
|
9 | 20 |
public $features = array(); |
0 | 21 |
|
5 | 22 |
/** |
23 |
* Constructor. |
|
24 |
* |
|
25 |
* @since 3.1.0 |
|
26 |
* |
|
27 |
* @see WP_List_Table::__construct() for more information on default arguments. |
|
28 |
* |
|
29 |
* @param array $args An associative array of arguments. |
|
30 |
*/ |
|
31 |
public function __construct( $args = array() ) { |
|
9 | 32 |
parent::__construct( |
33 |
array( |
|
34 |
'ajax' => true, |
|
35 |
'screen' => isset( $args['screen'] ) ? $args['screen'] : null, |
|
36 |
) |
|
37 |
); |
|
0 | 38 |
} |
39 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
41 |
* @return bool |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
*/ |
5 | 43 |
public function ajax_user_can() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
// Do not check edit_theme_options here. Ajax calls for available themes require switch_themes. |
0 | 45 |
return current_user_can( 'switch_themes' ); |
46 |
} |
|
47 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
*/ |
5 | 50 |
public function prepare_items() { |
0 | 51 |
$themes = wp_get_themes( array( 'allowed' => true ) ); |
52 |
||
9 | 53 |
if ( ! empty( $_REQUEST['s'] ) ) { |
0 | 54 |
$this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) ); |
9 | 55 |
} |
0 | 56 |
|
9 | 57 |
if ( ! empty( $_REQUEST['features'] ) ) { |
0 | 58 |
$this->features = $_REQUEST['features']; |
9 | 59 |
} |
0 | 60 |
|
61 |
if ( $this->search_terms || $this->features ) { |
|
62 |
foreach ( $themes as $key => $theme ) { |
|
9 | 63 |
if ( ! $this->search_theme( $theme ) ) { |
0 | 64 |
unset( $themes[ $key ] ); |
9 | 65 |
} |
0 | 66 |
} |
67 |
} |
|
68 |
||
69 |
unset( $themes[ get_option( 'stylesheet' ) ] ); |
|
70 |
WP_Theme::sort_by_name( $themes ); |
|
71 |
||
72 |
$per_page = 36; |
|
9 | 73 |
$page = $this->get_pagenum(); |
0 | 74 |
|
75 |
$start = ( $page - 1 ) * $per_page; |
|
76 |
||
77 |
$this->items = array_slice( $themes, $start, $per_page, true ); |
|
78 |
||
9 | 79 |
$this->set_pagination_args( |
80 |
array( |
|
81 |
'total_items' => count( $themes ), |
|
82 |
'per_page' => $per_page, |
|
83 |
'infinite_scroll' => true, |
|
84 |
) |
|
85 |
); |
|
0 | 86 |
} |
87 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
*/ |
5 | 90 |
public function no_items() { |
0 | 91 |
if ( $this->search_terms || $this->features ) { |
92 |
_e( 'No items found.' ); |
|
93 |
return; |
|
94 |
} |
|
95 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
$blog_id = get_current_blog_id(); |
0 | 97 |
if ( is_multisite() ) { |
98 |
if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) { |
|
16 | 99 |
printf( |
100 |
/* translators: 1: URL to Themes tab on Edit Site screen, 2: URL to Add Themes screen. */ |
|
101 |
__( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%1$s">enable</a> or <a href="%2$s">install</a> more themes.' ), |
|
102 |
network_admin_url( 'site-themes.php?id=' . $blog_id ), |
|
103 |
network_admin_url( 'theme-install.php' ) |
|
104 |
); |
|
0 | 105 |
|
106 |
return; |
|
107 |
} elseif ( current_user_can( 'manage_network_themes' ) ) { |
|
16 | 108 |
printf( |
109 |
/* translators: %s: URL to Themes tab on Edit Site screen. */ |
|
110 |
__( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%s">enable</a> more themes.' ), |
|
111 |
network_admin_url( 'site-themes.php?id=' . $blog_id ) |
|
112 |
); |
|
0 | 113 |
|
114 |
return; |
|
115 |
} |
|
5 | 116 |
// Else, fallthrough. install_themes doesn't help if you can't enable it. |
0 | 117 |
} else { |
118 |
if ( current_user_can( 'install_themes' ) ) { |
|
16 | 119 |
printf( |
120 |
/* translators: %s: URL to Add Themes screen. */ |
|
121 |
__( 'You only have one theme installed right now. Live a little! You can choose from over 1,000 free themes in the WordPress Theme Directory at any time: just click on the <a href="%s">Install Themes</a> tab above.' ), |
|
122 |
admin_url( 'theme-install.php' ) |
|
123 |
); |
|
0 | 124 |
|
125 |
return; |
|
126 |
} |
|
127 |
} |
|
128 |
// Fallthrough. |
|
16 | 129 |
printf( |
130 |
/* translators: %s: Network title. */ |
|
19 | 131 |
__( 'Only the active theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), |
16 | 132 |
get_site_option( 'site_name' ) |
133 |
); |
|
0 | 134 |
} |
135 |
||
5 | 136 |
/** |
137 |
* @param string $which |
|
138 |
*/ |
|
139 |
public function tablenav( $which = 'top' ) { |
|
9 | 140 |
if ( $this->get_pagination_arg( 'total_pages' ) <= 1 ) { |
0 | 141 |
return; |
9 | 142 |
} |
0 | 143 |
?> |
144 |
<div class="tablenav themes <?php echo $which; ?>"> |
|
145 |
<?php $this->pagination( $which ); ?> |
|
146 |
<span class="spinner"></span> |
|
147 |
<br class="clear" /> |
|
148 |
</div> |
|
149 |
<?php |
|
150 |
} |
|
151 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
/** |
16 | 153 |
* Displays the themes table. |
154 |
* |
|
155 |
* Overrides the parent display() method to provide a different container. |
|
156 |
* |
|
157 |
* @since 3.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
*/ |
5 | 159 |
public function display() { |
9 | 160 |
wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' ); |
161 |
?> |
|
0 | 162 |
<?php $this->tablenav( 'top' ); ?> |
163 |
||
164 |
<div id="availablethemes"> |
|
165 |
<?php $this->display_rows_or_placeholder(); ?> |
|
166 |
</div> |
|
167 |
||
168 |
<?php $this->tablenav( 'bottom' ); ?> |
|
9 | 169 |
<?php |
0 | 170 |
} |
171 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
172 |
/** |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
173 |
* @return string[] Array of column titles keyed by their column name. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
*/ |
5 | 175 |
public function get_columns() { |
0 | 176 |
return array(); |
177 |
} |
|
178 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
180 |
*/ |
5 | 181 |
public function display_rows_or_placeholder() { |
0 | 182 |
if ( $this->has_items() ) { |
183 |
$this->display_rows(); |
|
184 |
} else { |
|
185 |
echo '<div class="no-items">'; |
|
186 |
$this->no_items(); |
|
187 |
echo '</div>'; |
|
188 |
} |
|
189 |
} |
|
190 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
/** |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
192 |
* Generates the list table rows. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
193 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
194 |
* @since 3.1.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
*/ |
5 | 196 |
public function display_rows() { |
0 | 197 |
$themes = $this->items; |
198 |
||
9 | 199 |
foreach ( $themes as $theme ) : |
200 |
?> |
|
201 |
<div class="available-theme"> |
|
202 |
<?php |
|
0 | 203 |
|
204 |
$template = $theme->get_template(); |
|
205 |
$stylesheet = $theme->get_stylesheet(); |
|
9 | 206 |
$title = $theme->display( 'Name' ); |
207 |
$version = $theme->display( 'Version' ); |
|
208 |
$author = $theme->display( 'Author' ); |
|
0 | 209 |
|
9 | 210 |
$activate_link = wp_nonce_url( 'themes.php?action=activate&template=' . urlencode( $template ) . '&stylesheet=' . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet ); |
0 | 211 |
|
9 | 212 |
$actions = array(); |
16 | 213 |
$actions['activate'] = sprintf( |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
214 |
'<a href="%s" class="activatelink" aria-label="%s">%s</a>', |
16 | 215 |
$activate_link, |
216 |
/* translators: %s: Theme name. */ |
|
217 |
esc_attr( sprintf( _x( 'Activate “%s”', 'theme' ), $title ) ), |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
218 |
_x( 'Activate', 'theme' ) |
16 | 219 |
); |
0 | 220 |
|
5 | 221 |
if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { |
16 | 222 |
$actions['preview'] .= sprintf( |
223 |
'<a href="%s" class="load-customize hide-if-no-customize">%s</a>', |
|
224 |
wp_customize_url( $stylesheet ), |
|
225 |
__( 'Live Preview' ) |
|
226 |
); |
|
5 | 227 |
} |
0 | 228 |
|
9 | 229 |
if ( ! is_multisite() && current_user_can( 'delete_themes' ) ) { |
16 | 230 |
$actions['delete'] = sprintf( |
231 |
'<a class="submitdelete deletion" href="%s" onclick="return confirm( \'%s\' );">%s</a>', |
|
232 |
wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet ), |
|
233 |
/* translators: %s: Theme name. */ |
|
234 |
esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) ), |
|
235 |
__( 'Delete' ) |
|
236 |
); |
|
9 | 237 |
} |
0 | 238 |
|
5 | 239 |
/** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */ |
9 | 240 |
$actions = apply_filters( 'theme_action_links', $actions, $theme, 'all' ); |
5 | 241 |
|
242 |
/** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */ |
|
16 | 243 |
$actions = apply_filters( "theme_action_links_{$stylesheet}", $actions, $theme, 'all' ); |
0 | 244 |
$delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : ''; |
245 |
unset( $actions['delete'] ); |
|
246 |
||
16 | 247 |
$screenshot = $theme->get_screenshot(); |
0 | 248 |
?> |
249 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
<span class="screenshot hide-if-customize"> |
16 | 251 |
<?php if ( $screenshot ) : ?> |
19 | 252 |
<img src="<?php echo esc_url( $screenshot . '?ver=' . $theme->version ); ?>" alt="" /> |
0 | 253 |
<?php endif; ?> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
</span> |
0 | 255 |
<a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize"> |
16 | 256 |
<?php if ( $screenshot ) : ?> |
19 | 257 |
<img src="<?php echo esc_url( $screenshot . '?ver=' . $theme->version ); ?>" alt="" /> |
0 | 258 |
<?php endif; ?> |
259 |
</a> |
|
260 |
||
261 |
<h3><?php echo $title; ?></h3> |
|
16 | 262 |
<div class="theme-author"> |
263 |
<?php |
|
264 |
/* translators: %s: Theme author. */ |
|
265 |
printf( __( 'By %s' ), $author ); |
|
266 |
?> |
|
267 |
</div> |
|
0 | 268 |
<div class="action-links"> |
269 |
<ul> |
|
9 | 270 |
<?php foreach ( $actions as $action ) : ?> |
0 | 271 |
<li><?php echo $action; ?></li> |
272 |
<?php endforeach; ?> |
|
9 | 273 |
<li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e( 'Details' ); ?></a></li> |
0 | 274 |
</ul> |
275 |
<?php echo $delete_action; ?> |
|
276 |
||
277 |
<?php theme_update_available( $theme ); ?> |
|
278 |
</div> |
|
279 |
||
280 |
<div class="themedetaildiv hide-if-js"> |
|
9 | 281 |
<p><strong><?php _e( 'Version:' ); ?></strong> <?php echo $version; ?></p> |
282 |
<p><?php echo $theme->display( 'Description' ); ?></p> |
|
283 |
<?php |
|
284 |
if ( $theme->parent() ) { |
|
285 |
printf( |
|
16 | 286 |
/* translators: 1: Link to documentation on child themes, 2: Name of parent theme. */ |
9 | 287 |
' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>', |
288 |
__( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ), |
|
289 |
$theme->parent()->display( 'Name' ) |
|
290 |
); |
|
291 |
} |
|
292 |
?> |
|
0 | 293 |
</div> |
294 |
||
295 |
</div> |
|
9 | 296 |
<?php |
0 | 297 |
endforeach; |
298 |
} |
|
299 |
||
5 | 300 |
/** |
301 |
* @param WP_Theme $theme |
|
302 |
* @return bool |
|
303 |
*/ |
|
304 |
public function search_theme( $theme ) { |
|
16 | 305 |
// Search the features. |
0 | 306 |
foreach ( $this->features as $word ) { |
16 | 307 |
if ( ! in_array( $word, $theme->get( 'Tags' ), true ) ) { |
0 | 308 |
return false; |
9 | 309 |
} |
0 | 310 |
} |
311 |
||
16 | 312 |
// Match all phrases. |
0 | 313 |
foreach ( $this->search_terms as $word ) { |
16 | 314 |
if ( in_array( $word, $theme->get( 'Tags' ), true ) ) { |
0 | 315 |
continue; |
9 | 316 |
} |
0 | 317 |
|
318 |
foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) { |
|
319 |
// Don't mark up; Do translate. |
|
5 | 320 |
if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) { |
0 | 321 |
continue 2; |
5 | 322 |
} |
0 | 323 |
} |
324 |
||
9 | 325 |
if ( false !== stripos( $theme->get_stylesheet(), $word ) ) { |
0 | 326 |
continue; |
9 | 327 |
} |
0 | 328 |
|
9 | 329 |
if ( false !== stripos( $theme->get_template(), $word ) ) { |
0 | 330 |
continue; |
9 | 331 |
} |
0 | 332 |
|
333 |
return false; |
|
334 |
} |
|
335 |
||
336 |
return true; |
|
337 |
} |
|
338 |
||
339 |
/** |
|
340 |
* Send required variables to JavaScript land |
|
341 |
* |
|
5 | 342 |
* @since 3.4.0 |
0 | 343 |
* |
5 | 344 |
* @param array $extra_args |
0 | 345 |
*/ |
5 | 346 |
public function _js_vars( $extra_args = array() ) { |
0 | 347 |
$search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : ''; |
348 |
||
349 |
$args = array( |
|
9 | 350 |
'search' => $search_string, |
351 |
'features' => $this->features, |
|
352 |
'paged' => $this->get_pagenum(), |
|
0 | 353 |
'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1, |
354 |
); |
|
355 |
||
9 | 356 |
if ( is_array( $extra_args ) ) { |
0 | 357 |
$args = array_merge( $args, $extra_args ); |
9 | 358 |
} |
0 | 359 |
|
5 | 360 |
printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) ); |
0 | 361 |
parent::_js_vars(); |
362 |
} |
|
363 |
} |