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