author | ymh <ymh.work@gmail.com> |
Tue, 15 Oct 2019 15:48:13 +0200 | |
changeset 13 | d255fe9cd479 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
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' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
printf( __( '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.' ), network_admin_url( 'site-themes.php?id=' . $blog_id ), network_admin_url( 'theme-install.php' ) ); |
0 | 101 |
|
102 |
return; |
|
103 |
} elseif ( current_user_can( 'manage_network_themes' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
printf( __( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%1$s">enable</a> more themes.' ), network_admin_url( 'site-themes.php?id=' . $blog_id ) ); |
0 | 105 |
|
106 |
return; |
|
107 |
} |
|
5 | 108 |
// Else, fallthrough. install_themes doesn't help if you can't enable it. |
0 | 109 |
} else { |
110 |
if ( current_user_can( 'install_themes' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
printf( __( '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.' ), admin_url( 'theme-install.php' ) ); |
0 | 112 |
|
113 |
return; |
|
114 |
} |
|
115 |
} |
|
116 |
// Fallthrough. |
|
117 |
printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) ); |
|
118 |
} |
|
119 |
||
5 | 120 |
/** |
121 |
* @param string $which |
|
122 |
*/ |
|
123 |
public function tablenav( $which = 'top' ) { |
|
9 | 124 |
if ( $this->get_pagination_arg( 'total_pages' ) <= 1 ) { |
0 | 125 |
return; |
9 | 126 |
} |
0 | 127 |
?> |
128 |
<div class="tablenav themes <?php echo $which; ?>"> |
|
129 |
<?php $this->pagination( $which ); ?> |
|
130 |
<span class="spinner"></span> |
|
131 |
<br class="clear" /> |
|
132 |
</div> |
|
133 |
<?php |
|
134 |
} |
|
135 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
*/ |
5 | 138 |
public function display() { |
9 | 139 |
wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' ); |
140 |
?> |
|
0 | 141 |
<?php $this->tablenav( 'top' ); ?> |
142 |
||
143 |
<div id="availablethemes"> |
|
144 |
<?php $this->display_rows_or_placeholder(); ?> |
|
145 |
</div> |
|
146 |
||
147 |
<?php $this->tablenav( 'bottom' ); ?> |
|
9 | 148 |
<?php |
0 | 149 |
} |
150 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
*/ |
5 | 154 |
public function get_columns() { |
0 | 155 |
return array(); |
156 |
} |
|
157 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
*/ |
5 | 160 |
public function display_rows_or_placeholder() { |
0 | 161 |
if ( $this->has_items() ) { |
162 |
$this->display_rows(); |
|
163 |
} else { |
|
164 |
echo '<div class="no-items">'; |
|
165 |
$this->no_items(); |
|
166 |
echo '</div>'; |
|
167 |
} |
|
168 |
} |
|
169 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
*/ |
5 | 172 |
public function display_rows() { |
0 | 173 |
$themes = $this->items; |
174 |
||
9 | 175 |
foreach ( $themes as $theme ) : |
176 |
?> |
|
177 |
<div class="available-theme"> |
|
178 |
<?php |
|
0 | 179 |
|
180 |
$template = $theme->get_template(); |
|
181 |
$stylesheet = $theme->get_stylesheet(); |
|
9 | 182 |
$title = $theme->display( 'Name' ); |
183 |
$version = $theme->display( 'Version' ); |
|
184 |
$author = $theme->display( 'Author' ); |
|
0 | 185 |
|
9 | 186 |
$activate_link = wp_nonce_url( 'themes.php?action=activate&template=' . urlencode( $template ) . '&stylesheet=' . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet ); |
0 | 187 |
|
9 | 188 |
$actions = array(); |
0 | 189 |
$actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="' |
190 |
. esc_attr( sprintf( __( 'Activate “%s”' ), $title ) ) . '">' . __( 'Activate' ) . '</a>'; |
|
191 |
||
5 | 192 |
if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { |
0 | 193 |
$actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">' |
194 |
. __( 'Live Preview' ) . '</a>'; |
|
5 | 195 |
} |
0 | 196 |
|
9 | 197 |
if ( ! is_multisite() && current_user_can( 'delete_themes' ) ) { |
0 | 198 |
$actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet ) |
199 |
. '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) ) |
|
200 |
. "' );" . '">' . __( 'Delete' ) . '</a>'; |
|
9 | 201 |
} |
0 | 202 |
|
5 | 203 |
/** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */ |
9 | 204 |
$actions = apply_filters( 'theme_action_links', $actions, $theme, 'all' ); |
5 | 205 |
|
206 |
/** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
207 |
$actions = apply_filters( "theme_action_links_$stylesheet", $actions, $theme, 'all' ); |
0 | 208 |
$delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : ''; |
209 |
unset( $actions['delete'] ); |
|
210 |
||
211 |
?> |
|
212 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
<span class="screenshot hide-if-customize"> |
0 | 214 |
<?php if ( $screenshot = $theme->get_screenshot() ) : ?> |
215 |
<img src="<?php echo esc_url( $screenshot ); ?>" alt="" /> |
|
216 |
<?php endif; ?> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
217 |
</span> |
0 | 218 |
<a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize"> |
219 |
<?php if ( $screenshot = $theme->get_screenshot() ) : ?> |
|
220 |
<img src="<?php echo esc_url( $screenshot ); ?>" alt="" /> |
|
221 |
<?php endif; ?> |
|
222 |
</a> |
|
223 |
||
224 |
<h3><?php echo $title; ?></h3> |
|
225 |
<div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div> |
|
226 |
<div class="action-links"> |
|
227 |
<ul> |
|
9 | 228 |
<?php foreach ( $actions as $action ) : ?> |
0 | 229 |
<li><?php echo $action; ?></li> |
230 |
<?php endforeach; ?> |
|
9 | 231 |
<li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e( 'Details' ); ?></a></li> |
0 | 232 |
</ul> |
233 |
<?php echo $delete_action; ?> |
|
234 |
||
235 |
<?php theme_update_available( $theme ); ?> |
|
236 |
</div> |
|
237 |
||
238 |
<div class="themedetaildiv hide-if-js"> |
|
9 | 239 |
<p><strong><?php _e( 'Version:' ); ?></strong> <?php echo $version; ?></p> |
240 |
<p><?php echo $theme->display( 'Description' ); ?></p> |
|
241 |
<?php |
|
242 |
if ( $theme->parent() ) { |
|
243 |
printf( |
|
244 |
/* translators: %s: link to documentation on child themes */ |
|
245 |
' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>', |
|
246 |
__( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ), |
|
247 |
$theme->parent()->display( 'Name' ) |
|
248 |
); |
|
249 |
} |
|
250 |
?> |
|
0 | 251 |
</div> |
252 |
||
253 |
</div> |
|
9 | 254 |
<?php |
0 | 255 |
endforeach; |
256 |
} |
|
257 |
||
5 | 258 |
/** |
259 |
* @param WP_Theme $theme |
|
260 |
* @return bool |
|
261 |
*/ |
|
262 |
public function search_theme( $theme ) { |
|
0 | 263 |
// Search the features |
264 |
foreach ( $this->features as $word ) { |
|
9 | 265 |
if ( ! in_array( $word, $theme->get( 'Tags' ) ) ) { |
0 | 266 |
return false; |
9 | 267 |
} |
0 | 268 |
} |
269 |
||
270 |
// Match all phrases |
|
271 |
foreach ( $this->search_terms as $word ) { |
|
9 | 272 |
if ( in_array( $word, $theme->get( 'Tags' ) ) ) { |
0 | 273 |
continue; |
9 | 274 |
} |
0 | 275 |
|
276 |
foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) { |
|
277 |
// Don't mark up; Do translate. |
|
5 | 278 |
if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) { |
0 | 279 |
continue 2; |
5 | 280 |
} |
0 | 281 |
} |
282 |
||
9 | 283 |
if ( false !== stripos( $theme->get_stylesheet(), $word ) ) { |
0 | 284 |
continue; |
9 | 285 |
} |
0 | 286 |
|
9 | 287 |
if ( false !== stripos( $theme->get_template(), $word ) ) { |
0 | 288 |
continue; |
9 | 289 |
} |
0 | 290 |
|
291 |
return false; |
|
292 |
} |
|
293 |
||
294 |
return true; |
|
295 |
} |
|
296 |
||
297 |
/** |
|
298 |
* Send required variables to JavaScript land |
|
299 |
* |
|
5 | 300 |
* @since 3.4.0 |
0 | 301 |
* |
5 | 302 |
* @param array $extra_args |
0 | 303 |
*/ |
5 | 304 |
public function _js_vars( $extra_args = array() ) { |
0 | 305 |
$search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : ''; |
306 |
||
307 |
$args = array( |
|
9 | 308 |
'search' => $search_string, |
309 |
'features' => $this->features, |
|
310 |
'paged' => $this->get_pagenum(), |
|
0 | 311 |
'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1, |
312 |
); |
|
313 |
||
9 | 314 |
if ( is_array( $extra_args ) ) { |
0 | 315 |
$args = array_merge( $args, $extra_args ); |
9 | 316 |
} |
0 | 317 |
|
5 | 318 |
printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) ); |
0 | 319 |
parent::_js_vars(); |
320 |
} |
|
321 |
} |