Another attempt to fix the http blog redirects.
[WebKit-https.git] / Websites / webkit.org / blog / wp-admin / includes / class-wp-themes-list-table.php
1 <?php
2 /**
3  * Themes List Table class.
4  *
5  * @package WordPress
6  * @subpackage List_Table
7  * @since 3.1.0
8  * @access private
9  */
10 class WP_Themes_List_Table extends WP_List_Table {
11
12         protected $search_terms = array();
13         public $features = array();
14
15         /**
16          * Constructor.
17          *
18          * @since 3.1.0
19          * @access public
20          *
21          * @see WP_List_Table::__construct() for more information on default arguments.
22          *
23          * @param array $args An associative array of arguments.
24          */
25         public function __construct( $args = array() ) {
26                 parent::__construct( array(
27                         'ajax' => true,
28                         'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
29                 ) );
30         }
31
32         public function ajax_user_can() {
33                 // Do not check edit_theme_options here. AJAX calls for available themes require switch_themes.
34                 return current_user_can( 'switch_themes' );
35         }
36
37         public function prepare_items() {
38                 $themes = wp_get_themes( array( 'allowed' => true ) );
39
40                 if ( ! empty( $_REQUEST['s'] ) )
41                         $this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) );
42
43                 if ( ! empty( $_REQUEST['features'] ) )
44                         $this->features = $_REQUEST['features'];
45
46                 if ( $this->search_terms || $this->features ) {
47                         foreach ( $themes as $key => $theme ) {
48                                 if ( ! $this->search_theme( $theme ) )
49                                         unset( $themes[ $key ] );
50                         }
51                 }
52
53                 unset( $themes[ get_option( 'stylesheet' ) ] );
54                 WP_Theme::sort_by_name( $themes );
55
56                 $per_page = 36;
57                 $page = $this->get_pagenum();
58
59                 $start = ( $page - 1 ) * $per_page;
60
61                 $this->items = array_slice( $themes, $start, $per_page, true );
62
63                 $this->set_pagination_args( array(
64                         'total_items' => count( $themes ),
65                         'per_page' => $per_page,
66                         'infinite_scroll' => true,
67                 ) );
68         }
69
70         public function no_items() {
71                 if ( $this->search_terms || $this->features ) {
72                         _e( 'No items found.' );
73                         return;
74                 }
75
76                 if ( is_multisite() ) {
77                         if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
78                                 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=' . $GLOBALS['blog_id'] ), network_admin_url( 'theme-install.php' ) );
79
80                                 return;
81                         } elseif ( current_user_can( 'manage_network_themes' ) ) {
82                                 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=' . $GLOBALS['blog_id'] ) );
83
84                                 return;
85                         }
86                         // Else, fallthrough. install_themes doesn't help if you can't enable it.
87                 } else {
88                         if ( current_user_can( 'install_themes' ) ) {
89                                 printf( __( 'You only have one theme installed right now. Live a little! You can choose from over 1,000 free themes in the WordPress.org Theme Directory at any time: just click on the <a href="%s">Install Themes</a> tab above.' ), admin_url( 'theme-install.php' ) );
90
91                                 return;
92                         }
93                 }
94                 // Fallthrough.
95                 printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) );
96         }
97
98         /**
99          * @param string $which
100          * @return null
101          */
102         public function tablenav( $which = 'top' ) {
103                 if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
104                         return;
105                 ?>
106                 <div class="tablenav themes <?php echo $which; ?>">
107                         <?php $this->pagination( $which ); ?>
108                         <span class="spinner"></span>
109                         <br class="clear" />
110                 </div>
111                 <?php
112         }
113
114         public function display() {
115                 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
116 ?>
117                 <?php $this->tablenav( 'top' ); ?>
118
119                 <div id="availablethemes">
120                         <?php $this->display_rows_or_placeholder(); ?>
121                 </div>
122
123                 <?php $this->tablenav( 'bottom' ); ?>
124 <?php
125         }
126
127         public function get_columns() {
128                 return array();
129         }
130
131         public function display_rows_or_placeholder() {
132                 if ( $this->has_items() ) {
133                         $this->display_rows();
134                 } else {
135                         echo '<div class="no-items">';
136                         $this->no_items();
137                         echo '</div>';
138                 }
139         }
140
141         public function display_rows() {
142                 $themes = $this->items;
143
144                 foreach ( $themes as $theme ):
145                         ?><div class="available-theme"><?php
146
147                         $template   = $theme->get_template();
148                         $stylesheet = $theme->get_stylesheet();
149                         $title      = $theme->display('Name');
150                         $version    = $theme->display('Version');
151                         $author     = $theme->display('Author');
152
153                         $activate_link = wp_nonce_url( "themes.php?action=activate&amp;template=" . urlencode( $template ) . "&amp;stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
154
155                         $preview_link = esc_url( add_query_arg(
156                                 array( 'preview' => 1, 'template' => urlencode( $template ), 'stylesheet' => urlencode( $stylesheet ), 'preview_iframe' => true, 'TB_iframe' => 'true' ),
157                                 home_url( '/' ) ) );
158
159                         $actions = array();
160                         $actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="'
161                                 . esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
162
163                         $actions['preview'] = '<a href="' . $preview_link . '" class="hide-if-customize" title="'
164                                 . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Preview' ) . '</a>';
165
166                         if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
167                                 $actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
168                                         . __( 'Live Preview' ) . '</a>';
169                         }
170
171                         if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
172                                 $actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( 'themes.php?action=delete&amp;stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet )
173                                         . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n  'Cancel' to stop, 'OK' to delete." ), $title ) )
174                                         . "' );" . '">' . __( 'Delete' ) . '</a>';
175
176                         /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
177                         $actions       = apply_filters( 'theme_action_links', $actions, $theme );
178
179                         /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
180                         $actions       = apply_filters( "theme_action_links_$stylesheet", $actions, $theme );
181                         $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
182                         unset( $actions['delete'] );
183
184                         ?>
185
186                         <a href="<?php echo $preview_link; ?>" class="screenshot hide-if-customize">
187                                 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
188                                         <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
189                                 <?php endif; ?>
190                         </a>
191                         <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
192                                 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
193                                         <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
194                                 <?php endif; ?>
195                         </a>
196
197                         <h3><?php echo $title; ?></h3>
198                         <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
199                         <div class="action-links">
200                                 <ul>
201                                         <?php foreach ( $actions as $action ): ?>
202                                                 <li><?php echo $action; ?></li>
203                                         <?php endforeach; ?>
204                                         <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
205                                 </ul>
206                                 <?php echo $delete_action; ?>
207
208                                 <?php theme_update_available( $theme ); ?>
209                         </div>
210
211                         <div class="themedetaildiv hide-if-js">
212                                 <p><strong><?php _e('Version:'); ?></strong> <?php echo $version; ?></p>
213                                 <p><?php echo $theme->display('Description'); ?></p>
214                                 <?php if ( $theme->parent() ) {
215                                         printf( ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
216                                                 __( 'https://codex.wordpress.org/Child_Themes' ),
217                                                 $theme->parent()->display( 'Name' ) );
218                                 } ?>
219                         </div>
220
221                         </div>
222                 <?php
223                 endforeach;
224         }
225
226         /**
227          * @param WP_Theme $theme
228          * @return bool
229          */
230         public function search_theme( $theme ) {
231                 // Search the features
232                 foreach ( $this->features as $word ) {
233                         if ( ! in_array( $word, $theme->get('Tags') ) )
234                                 return false;
235                 }
236
237                 // Match all phrases
238                 foreach ( $this->search_terms as $word ) {
239                         if ( in_array( $word, $theme->get('Tags') ) )
240                                 continue;
241
242                         foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
243                                 // Don't mark up; Do translate.
244                                 if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
245                                         continue 2;
246                                 }
247                         }
248
249                         if ( false !== stripos( $theme->get_stylesheet(), $word ) )
250                                 continue;
251
252                         if ( false !== stripos( $theme->get_template(), $word ) )
253                                 continue;
254
255                         return false;
256                 }
257
258                 return true;
259         }
260
261         /**
262          * Send required variables to JavaScript land
263          *
264          * @since 3.4.0
265          * @access public
266          *
267          * @param array $extra_args
268          */
269         public function _js_vars( $extra_args = array() ) {
270                 $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
271
272                 $args = array(
273                         'search' => $search_string,
274                         'features' => $this->features,
275                         'paged' => $this->get_pagenum(),
276                         'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
277                 );
278
279                 if ( is_array( $extra_args ) )
280                         $args = array_merge( $args, $extra_args );
281
282                 printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) );
283                 parent::_js_vars();
284         }
285 }