author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
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(); |
|
5 | 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() ) { |
|
0 | 33 |
parent::__construct( array( |
34 |
'ajax' => true, |
|
35 |
'screen' => isset( $args['screen'] ) ? $args['screen'] : null, |
|
36 |
) ); |
|
37 |
} |
|
38 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
/** |
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 |
||
53 |
if ( ! empty( $_REQUEST['s'] ) ) |
|
54 |
$this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) ); |
|
55 |
||
56 |
if ( ! empty( $_REQUEST['features'] ) ) |
|
57 |
$this->features = $_REQUEST['features']; |
|
58 |
||
59 |
if ( $this->search_terms || $this->features ) { |
|
60 |
foreach ( $themes as $key => $theme ) { |
|
61 |
if ( ! $this->search_theme( $theme ) ) |
|
62 |
unset( $themes[ $key ] ); |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
unset( $themes[ get_option( 'stylesheet' ) ] ); |
|
67 |
WP_Theme::sort_by_name( $themes ); |
|
68 |
||
69 |
$per_page = 36; |
|
70 |
$page = $this->get_pagenum(); |
|
71 |
||
72 |
$start = ( $page - 1 ) * $per_page; |
|
73 |
||
74 |
$this->items = array_slice( $themes, $start, $per_page, true ); |
|
75 |
||
76 |
$this->set_pagination_args( array( |
|
77 |
'total_items' => count( $themes ), |
|
78 |
'per_page' => $per_page, |
|
79 |
'infinite_scroll' => true, |
|
80 |
) ); |
|
81 |
} |
|
82 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
*/ |
5 | 85 |
public function no_items() { |
0 | 86 |
if ( $this->search_terms || $this->features ) { |
87 |
_e( 'No items found.' ); |
|
88 |
return; |
|
89 |
} |
|
90 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
$blog_id = get_current_blog_id(); |
0 | 92 |
if ( is_multisite() ) { |
93 |
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
|
94 |
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 | 95 |
|
96 |
return; |
|
97 |
} elseif ( current_user_can( 'manage_network_themes' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
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 | 99 |
|
100 |
return; |
|
101 |
} |
|
5 | 102 |
// Else, fallthrough. install_themes doesn't help if you can't enable it. |
0 | 103 |
} else { |
104 |
if ( current_user_can( 'install_themes' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
105 |
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 | 106 |
|
107 |
return; |
|
108 |
} |
|
109 |
} |
|
110 |
// Fallthrough. |
|
111 |
printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) ); |
|
112 |
} |
|
113 |
||
5 | 114 |
/** |
115 |
* @param string $which |
|
116 |
*/ |
|
117 |
public function tablenav( $which = 'top' ) { |
|
0 | 118 |
if ( $this->get_pagination_arg( 'total_pages' ) <= 1 ) |
119 |
return; |
|
120 |
?> |
|
121 |
<div class="tablenav themes <?php echo $which; ?>"> |
|
122 |
<?php $this->pagination( $which ); ?> |
|
123 |
<span class="spinner"></span> |
|
124 |
<br class="clear" /> |
|
125 |
</div> |
|
126 |
<?php |
|
127 |
} |
|
128 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
129 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
*/ |
5 | 131 |
public function display() { |
0 | 132 |
wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' ); |
133 |
?> |
|
134 |
<?php $this->tablenav( 'top' ); ?> |
|
135 |
||
136 |
<div id="availablethemes"> |
|
137 |
<?php $this->display_rows_or_placeholder(); ?> |
|
138 |
</div> |
|
139 |
||
140 |
<?php $this->tablenav( 'bottom' ); ?> |
|
141 |
<?php |
|
142 |
} |
|
143 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
*/ |
5 | 148 |
public function get_columns() { |
0 | 149 |
return array(); |
150 |
} |
|
151 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
*/ |
5 | 154 |
public function display_rows_or_placeholder() { |
0 | 155 |
if ( $this->has_items() ) { |
156 |
$this->display_rows(); |
|
157 |
} else { |
|
158 |
echo '<div class="no-items">'; |
|
159 |
$this->no_items(); |
|
160 |
echo '</div>'; |
|
161 |
} |
|
162 |
} |
|
163 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
*/ |
5 | 166 |
public function display_rows() { |
0 | 167 |
$themes = $this->items; |
168 |
||
169 |
foreach ( $themes as $theme ): |
|
170 |
?><div class="available-theme"><?php |
|
171 |
||
172 |
$template = $theme->get_template(); |
|
173 |
$stylesheet = $theme->get_stylesheet(); |
|
174 |
$title = $theme->display('Name'); |
|
175 |
$version = $theme->display('Version'); |
|
176 |
$author = $theme->display('Author'); |
|
177 |
||
178 |
$activate_link = wp_nonce_url( "themes.php?action=activate&template=" . urlencode( $template ) . "&stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet ); |
|
179 |
||
180 |
$actions = array(); |
|
181 |
$actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="' |
|
182 |
. esc_attr( sprintf( __( 'Activate “%s”' ), $title ) ) . '">' . __( 'Activate' ) . '</a>'; |
|
183 |
||
5 | 184 |
if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { |
0 | 185 |
$actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">' |
186 |
. __( 'Live Preview' ) . '</a>'; |
|
5 | 187 |
} |
0 | 188 |
|
189 |
if ( ! is_multisite() && current_user_can( 'delete_themes' ) ) |
|
190 |
$actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet ) |
|
191 |
. '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) ) |
|
192 |
. "' );" . '">' . __( 'Delete' ) . '</a>'; |
|
193 |
||
5 | 194 |
/** 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
|
195 |
$actions = apply_filters( 'theme_action_links', $actions, $theme, 'all' ); |
5 | 196 |
|
197 |
/** 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
|
198 |
$actions = apply_filters( "theme_action_links_$stylesheet", $actions, $theme, 'all' ); |
0 | 199 |
$delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : ''; |
200 |
unset( $actions['delete'] ); |
|
201 |
||
202 |
?> |
|
203 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
<span class="screenshot hide-if-customize"> |
0 | 205 |
<?php if ( $screenshot = $theme->get_screenshot() ) : ?> |
206 |
<img src="<?php echo esc_url( $screenshot ); ?>" alt="" /> |
|
207 |
<?php endif; ?> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
</span> |
0 | 209 |
<a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize"> |
210 |
<?php if ( $screenshot = $theme->get_screenshot() ) : ?> |
|
211 |
<img src="<?php echo esc_url( $screenshot ); ?>" alt="" /> |
|
212 |
<?php endif; ?> |
|
213 |
</a> |
|
214 |
||
215 |
<h3><?php echo $title; ?></h3> |
|
216 |
<div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div> |
|
217 |
<div class="action-links"> |
|
218 |
<ul> |
|
219 |
<?php foreach ( $actions as $action ): ?> |
|
220 |
<li><?php echo $action; ?></li> |
|
221 |
<?php endforeach; ?> |
|
222 |
<li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li> |
|
223 |
</ul> |
|
224 |
<?php echo $delete_action; ?> |
|
225 |
||
226 |
<?php theme_update_available( $theme ); ?> |
|
227 |
</div> |
|
228 |
||
229 |
<div class="themedetaildiv hide-if-js"> |
|
5 | 230 |
<p><strong><?php _e('Version:'); ?></strong> <?php echo $version; ?></p> |
0 | 231 |
<p><?php echo $theme->display('Description'); ?></p> |
232 |
<?php if ( $theme->parent() ) { |
|
233 |
printf( ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>', |
|
5 | 234 |
__( 'https://codex.wordpress.org/Child_Themes' ), |
0 | 235 |
$theme->parent()->display( 'Name' ) ); |
236 |
} ?> |
|
237 |
</div> |
|
238 |
||
239 |
</div> |
|
240 |
<?php |
|
241 |
endforeach; |
|
242 |
} |
|
243 |
||
5 | 244 |
/** |
245 |
* @param WP_Theme $theme |
|
246 |
* @return bool |
|
247 |
*/ |
|
248 |
public function search_theme( $theme ) { |
|
0 | 249 |
// Search the features |
250 |
foreach ( $this->features as $word ) { |
|
251 |
if ( ! in_array( $word, $theme->get('Tags') ) ) |
|
252 |
return false; |
|
253 |
} |
|
254 |
||
255 |
// Match all phrases |
|
256 |
foreach ( $this->search_terms as $word ) { |
|
257 |
if ( in_array( $word, $theme->get('Tags') ) ) |
|
258 |
continue; |
|
259 |
||
260 |
foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) { |
|
261 |
// Don't mark up; Do translate. |
|
5 | 262 |
if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) { |
0 | 263 |
continue 2; |
5 | 264 |
} |
0 | 265 |
} |
266 |
||
267 |
if ( false !== stripos( $theme->get_stylesheet(), $word ) ) |
|
268 |
continue; |
|
269 |
||
270 |
if ( false !== stripos( $theme->get_template(), $word ) ) |
|
271 |
continue; |
|
272 |
||
273 |
return false; |
|
274 |
} |
|
275 |
||
276 |
return true; |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Send required variables to JavaScript land |
|
281 |
* |
|
5 | 282 |
* @since 3.4.0 |
0 | 283 |
* |
5 | 284 |
* @param array $extra_args |
0 | 285 |
*/ |
5 | 286 |
public function _js_vars( $extra_args = array() ) { |
0 | 287 |
$search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : ''; |
288 |
||
289 |
$args = array( |
|
290 |
'search' => $search_string, |
|
291 |
'features' => $this->features, |
|
292 |
'paged' => $this->get_pagenum(), |
|
293 |
'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1, |
|
294 |
); |
|
295 |
||
296 |
if ( is_array( $extra_args ) ) |
|
297 |
$args = array_merge( $args, $extra_args ); |
|
298 |
||
5 | 299 |
printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) ); |
0 | 300 |
parent::_js_vars(); |
301 |
} |
|
302 |
} |