PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / cloud / class-cloud-search-list-table.php

class-cloud-search-list-table.php in Code Snippets 4.0.0-beta.1, at php/cloud/class-cloud-search-list-table.php

362 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Contains the class for handling the cloud search results table
4 *
5 * @package Code_Snippets
6 */
7
8 namespace Code_Snippets\Cloud;
9
10 use WP_Plugin_Install_List_Table;
11 use function Code_Snippets\code_snippets;
12
13 if ( ! class_exists( 'WP_Plugin_Install_List_Table' ) ) {
14 require_once ABSPATH . 'wp-admin/includes/class-wp-plugin-install-list-table.php';
15 }
16
17 /**
18 * Class for handling the cloud search results table.
19 *
20 * @property string $_pagination Pagination HTML.
21 *
22 * @package Code_Snippets
23 */
24 class Cloud_Search_List_Table extends WP_Plugin_Install_List_Table {
25
26 /**
27 * Instance of Cloud API class.
28 *
29 * @var Cloud_API
30 */
31 protected Cloud_API $cloud_api;
32
33 /**
34 * Items for the cloud list table.
35 *
36 * @var Cloud_Snippets
37 */
38 protected Cloud_Snippets $cloud_snippets;
39
40 /**
41 * Class constructor.
42 */
43 public function __construct() {
44 /**
45 * Declare global variable due to undeclared warning.
46 *
47 * @noinspection PhpUnusedLocalVariableInspection
48 */
49 global $tab;
50
51 parent::__construct(
52 [
53 'singular' => 'cloud-snippet',
54 'plural' => 'cloud-snippets',
55 'ajax' => false,
56 ]
57 );
58
59 // Strip the result query arg from the URL.
60 $_SERVER['REQUEST_URI'] = remove_query_arg( [ 'result' ] );
61
62 $this->cloud_api = code_snippets()->cloud_api;
63 }
64
65 /**
66 * Prepare items for the table.
67 *
68 * @return void
69 */
70 public function prepare_items() {
71 $per_page = $this->get_items_per_page( 'snippets_per_page', 10 );
72 $user_per_page = (int) get_user_option( 'snippets_per_page', get_current_user_id() );
73 if ( $user_per_page > 0 ) {
74 $per_page = $user_per_page;
75 }
76
77 // Fetch snippets, passing a 0-based page index to the Cloud API (WP list tables are 1-based).
78 $page_index = max( 0, $this->get_pagenum() - 1 );
79 $this->cloud_snippets = $this->fetch_snippets( $per_page, $page_index );
80 $this->items = $this->cloud_snippets->snippets;
81
82 $this->process_actions();
83
84 $this->set_pagination_args(
85 [
86 'per_page' => $per_page,
87 'total_items' => $this->cloud_snippets->total_snippets,
88 'total_pages' => $this->cloud_snippets->total_pages,
89 ]
90 );
91 }
92
93 /**
94 * Process any actions that have been submitted, such as downloading cloud snippets to the local database.
95 *
96 * @return void
97 */
98 public function process_actions() {
99 $_SERVER['REQUEST_URI'] = remove_query_arg(
100 [ 'action', 'snippet', '_wpnonce', 'source', 'cloud-bundle-run', 'cloud-bundle-show', 'bundle_share_name', 'cloud_bundles' ]
101 );
102
103 // Check request is coming from the cloud search page.
104 if ( ! isset( $_REQUEST['type'] ) || 'cloud_search' !== sanitize_key( wp_unslash( $_REQUEST['type'] ) ) ) {
105 return;
106 }
107
108 if ( ! isset( $_REQUEST['action'], $_REQUEST['snippet'], $_REQUEST['source'] ) ) {
109 return;
110 }
111
112 $action = sanitize_key( wp_unslash( $_REQUEST['action'] ) );
113 $source = sanitize_key( wp_unslash( $_REQUEST['source'] ) );
114 $snippet_id = absint( wp_unslash( $_REQUEST['snippet'] ) );
115
116 if ( ! in_array( $action, [ 'download', 'update' ], true ) ) {
117 return;
118 }
119
120 if ( ! $snippet_id ) {
121 return;
122 }
123
124 check_admin_referer( cloud_lts_get_snippet_action_nonce_action( $action, $snippet_id, $source ) );
125
126 cloud_lts_process_download_action(
127 $action,
128 $source,
129 (string) $snippet_id,
130 );
131 }
132
133 /**
134 * Output table rows.
135 *
136 * @return void
137 */
138 public function display_rows() {
139 $status_descriptions = [
140 Cloud_API::STATUS_PUBLIC =>
141 __( 'Snippet has passed basic review.', 'code-snippets' ),
142 Cloud_API::STATUS_AI_VERIFIED =>
143 __( 'Snippet has been tested by our AI bot.', 'code-snippets' ),
144 Cloud_API::STATUS_UNVERIFIED =>
145 __( 'Snippet has not undergone any review yet.', 'code-snippets' ),
146 ];
147
148 /**
149 * The current table item.
150 *
151 * @var $item Cloud_Snippet
152 */
153 foreach ( $this->items as $item ) {
154 ?>
155 <div class="plugin-card cloud-snippet-card plugin-card-<?php echo esc_attr( $item->id ); ?>">
156 <?php
157 cloud_lts_display_column_hidden_input( 'code', $item );
158 cloud_lts_display_column_hidden_input( 'name', $item );
159 ?>
160 <div class="plugin-card-top">
161 <div class="column-name">
162 <h3>
163 <?php
164
165 // Grab first tag in array of tags.
166 $category = count( $item->tags ) > 0 ? strtolower( esc_attr( $item->tags[0] ) ) : 'general';
167
168 printf(
169 '<img src="%s" class="title-icon" alt="%s">',
170 esc_url( "https://codesnippets.cloud/images/plugin-icons/$category-logo.png" ),
171 esc_attr( $category )
172 );
173
174 $link = code_snippets()->cloud_api->get_link_for_cloud_snippet( $item );
175
176 if ( $link ) {
177 printf( '<a href="%s">', esc_url( code_snippets()->get_snippet_edit_url( $link->local_id ) ) );
178 } else {
179 printf(
180 '<a href="%s" title="%s" class="cloud-snippet-preview thickbox" data-snippet="%s" data-lang="%s">',
181 '#TB_inline?&width=700&height=500&inlineId=show-code-preview',
182 esc_attr__( 'Preview this snippet', 'code-snippets' ),
183 esc_attr( $item->id ),
184 esc_attr( Cloud_API::get_type_from_scope( $item->scope ) )
185 );
186 }
187
188 echo esc_html( $item->name );
189
190
191
192 echo '</a>';
193 ?>
194 </h3>
195 <ul class="action-buttons">
196 <?php echo cloud_lts_build_action_links( $item, 'search' ); ?>
197 </ul>
198 </div>
199 <div class="column-description">
200 <p><?php echo wp_kses_post( $this->process_description( $item->description ) ); ?></p>
201 <p class="authors">
202 <cite>
203 <?php
204 printf(
205 '%s <a target="_blank" href="%s">%s</a>',
206 esc_html__( 'Codevault:', 'code-snippets' ),
207 esc_url( sprintf( 'https://codesnippets.cloud/codevault/%s', $item->codevault ) ),
208 esc_html( $item->codevault )
209 );
210 ?>
211 </cite>
212 </p>
213 </div>
214 </div>
215 <div class="plugin-card-bottom cloud-search-card-bottom">
216 <div class="cloud-meta-row">
217 <div class="column-downloaded">
218 <div class="badge <?php echo esc_attr( $this->cloud_api->get_status_badge( $item->status ) ); ?>-badge tooltip tooltip-block tooltip-end">
219 <?php
220
221 echo esc_html( $this->cloud_api->get_status_label( $item->status ) );
222
223 if ( isset( $status_descriptions[ $item->status ] ) ) {
224 echo '<span class="dashicons dashicons-info-outline"></span>';
225 printf( '<div class="tooltip-content">%s</div>', esc_html( $status_descriptions[ $item->status ] ) );
226 }
227 ?>
228 </div>
229 </div>
230
231 <div class="column-votes">
232 <svg xmlns="http://www.w3.org/2000/svg"
233 fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="thumbs-up">
234 <path stroke-linecap="round" stroke-linejoin="round"
235 d="M6.633 10.5c.806 0 1.533-.446 2.031-1.08a9.041 9.041 0 012.861-2.4c.723-.384 1.35-.956 1.653-1.715a4.498 4.498 0 00.322-1.672V3a.75.75 0 01.75-.75A2.25 2.25 0 0116.5 4.5c0 1.152-.26 2.243-.723 3.218-.266.558.107 1.282.725 1.282h3.126c1.026 0 1.945.694 2.054 1.715.045.422.068.85.068 1.285a11.95 11.95 0 01-2.649 7.521c-.388.482-.987.729-1.605.729H13.48c-.483 0-.964-.078-1.423-.23l-3.114-1.04a4.501 4.501 0 00-1.423-.23H5.904M14.25 9h2.25M5.904 18.75c.083.205.173.405.27.602.197.4-.078.898-.523.898h-.908c-.889 0-1.713-.518-1.972-1.368a12 12 0 01-.521-3.507c0-1.553.295-3.036.831-4.398C3.387 10.203 4.167 9.75 5 9.75h1.053c.472 0 .745.556.5.96a8.958 8.958 0 00-1.302 4.665c0 1.194.232 2.333.654 3.375z"></path>
236 </svg>
237 <span class="num-votes" aria-hidden="true">
238 <?php
239 echo esc_html( number_format_i18n( $item->vote_count ) );
240 ?>
241 </span>
242 </div>
243
244 <div class="column-updated">
245 <strong><?php esc_html_e( 'Last Updated:', 'code-snippets' ); ?></strong>
246 <?php
247 // translators: %s: Human-readable time difference.
248 echo esc_html( sprintf( __( '%s ago', 'code-snippets' ), human_time_diff( strtotime( $item->updated ) ) ) );
249 ?>
250 </div>
251 </div>
252 </div>
253 </div>
254 <?php
255 }
256 }
257
258 /**
259 * Process the description text - limit to 150 characters.
260 *
261 * @param string|null $description Description as provided by the API.
262 *
263 * @return string formatted description string max 150 chars.
264 */
265 protected function process_description( ?string $description ): string {
266 $description = wp_strip_all_tags( $description );
267 return strlen( $description ) > 150 ? substr( $description, 0, 150 ) . '…' : $description;
268 }
269
270 /**
271 * Text displayed when no snippet data is available.
272 *
273 * @return void
274 */
275 public function no_items() {
276 if ( ! empty( $_REQUEST['cloud_search'] ) && count( $this->cloud_snippets->snippets ) < 1 ) {
277 echo '<p class="no-results">',
278 esc_html__( 'No snippets or codevault could be found with that search term. Please try again.', 'code-snippets' ),
279 '</p>';
280 } else {
281 echo '<p>', esc_html__( 'Please enter a term to start searching code snippets in the cloud.', 'code-snippets' ), '</p>';
282 }
283 }
284
285 /**
286 * Fetch the snippets used to populate the table.
287 *
288 * @return Cloud_Snippets
289 */
290 public function fetch_snippets( int $per_page = 10, int $page_index = 0 ): Cloud_Snippets {
291 // Check if search term has been entered.
292 if ( isset( $_REQUEST['type'], $_REQUEST['cloud_search'], $_REQUEST['cloud_select'] ) &&
293 'cloud_search' === sanitize_key( wp_unslash( $_REQUEST['type'] ) )
294 ) {
295 // If we have a search query, then send a search request to cloud server API search endpoint.
296 $search_query = sanitize_text_field( wp_unslash( $_REQUEST['cloud_search'] ) );
297 $search_by = sanitize_text_field( wp_unslash( $_REQUEST['cloud_select'] ) );
298
299 // Pass the provided 0-based page index to the API.
300 return Cloud_API::fetch_search_results( $search_by, $search_query, $page_index );
301 }
302
303 // If no search results, then return empty object.
304 return new Cloud_Snippets();
305 }
306
307 /**
308 * Gets the current search result page number.
309 *
310 * @return integer
311 */
312 public function get_pagenum(): int {
313 $page = isset( $_REQUEST['search_page'] ) ? absint( $_REQUEST['search_page'] ) : 0;
314
315 if ( isset( $this->_pagination_args['total_pages'] ) && $page > $this->_pagination_args['total_pages'] ) {
316 $page = $this->_pagination_args['total_pages'];
317 }
318
319 return max( 1, $page );
320 }
321
322 /**
323 * Display the table.
324 *
325 * @return void
326 */
327 public function display() {
328 Cloud_API::render_cloud_snippet_thickbox();
329 parent::display();
330 }
331
332 /**
333 * Displays the pagination.
334 *
335 * @param string $which Context where the pagination will be displayed.
336 *
337 * @return void
338 */
339 protected function pagination( $which ) {
340 if ( empty( $this->_pagination_args ) ) {
341 return;
342 }
343
344 $total_items = $this->_pagination_args['total_items'] ?? 0;
345 $total_pages = $this->_pagination_args['total_pages'] ?? 0;
346 // get_pagenum already returns a 1-based page number used for display.
347 $pagenum_display = $this->get_pagenum();
348
349 if ( 'top' === $which && $total_pages >= 1 ) {
350 $this->screen->render_screen_reader_content( 'heading_pagination' );
351 }
352
353 $paginate = cloud_lts_pagination( $which, 'search', $total_items, $total_pages, $pagenum_display );
354 $page_class = $paginate['page_class'];
355 $output = $paginate['output'];
356
357 $this->_pagination = "<div class='tablenav-pages$page_class'>$output</div>";
358
359 echo wp_kses_post( $this->_pagination );
360 }
361 }
362