PluginProbe
Code Snippets / 2.13.0
Code Snippets v2.13.0
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 3.0.1 All 64 releases
code-snippets / php / class-list-table.php

class-list-table.php in Code Snippets 2.13.0, at php/class-list-table.php

1,087 lines 30.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Contains the class for handling the snippets table
5 *
6 * @package Code_Snippets
7 */
8
9 /* The WP_List_Table base class is not included by default, so we need to load it */
10 if ( ! class_exists( 'WP_List_Table' ) ) {
11 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
12 }
13
14 /**
15 * This class handles the table for the manage snippets menu
16 *
17 * @since 1.5
18 * @package Code_Snippets
19 */
20 class Code_Snippets_List_Table extends WP_List_Table {
21
22 /**
23 * true if the current screen is in the network admin
24 * @var bool
25 */
26 public $is_network;
27
28 /**
29 * A list of statuses (views)
30 * @var array
31 */
32 public $statuses = array( 'all', 'active', 'inactive', 'recently_activated' );
33
34 /**
35 * The constructor function for our class.
36 * Adds hooks, initializes variables, setups class.
37 */
38 public function __construct() {
39 global $status, $page;
40 $screen = get_current_screen();
41 $this->is_network = is_network_admin();
42
43 /* Determine the status */
44 $status = apply_filters( 'code_snippets/list_table/default_view', 'all' );
45 if ( isset( $_REQUEST['status'] ) && in_array( $_REQUEST['status'], $this->statuses ) ) {
46 $status = $_REQUEST['status'];
47 }
48
49 /* Add the search query to the URL */
50 if ( isset( $_REQUEST['s'] ) ) {
51 $_SERVER['REQUEST_URI'] = add_query_arg( 's', stripslashes( $_REQUEST['s'] ) );
52 }
53
54 /* Add a snippets per page screen option */
55 $page = $this->get_pagenum();
56
57 add_screen_option( 'per_page', array(
58 'label' => __( 'Snippets per page', 'code-snippets' ),
59 'default' => 999,
60 'option' => 'snippets_per_page',
61 ) );
62
63 /* Set the table columns hidden in Screen Options by default */
64 if ( false === get_user_option( "manage{$screen->id}columnshidden" ) ) {
65 $user = wp_get_current_user();
66 update_user_option( $user->ID, "manage{$screen->id}columnshidden", array( 'id' ) );
67 }
68
69 /* Strip the result query arg from the URL */
70 $_SERVER['REQUEST_URI'] = remove_query_arg( 'result' );
71
72 /* Add filters to format the snippet description in the same way the post content is formatted */
73 $filters = array( 'wptexturize', 'convert_smilies', 'convert_chars', 'wpautop', 'shortcode_unautop', 'capital_P_dangit' );
74
75 foreach ( $filters as $filter ) {
76 add_filter( 'code_snippets/list_table/column_description', $filter );
77 }
78
79 /* Setup the class */
80 parent::__construct( array(
81 'ajax' => true,
82 'plural' => 'snippets',
83 'singular' => 'snippet',
84 ) );
85 }
86
87 /**
88 * Define the output of all columns that have no callback function
89 *
90 * @param Code_Snippet $snippet The snippet used for the current row
91 * @param string $column_name The name of the column being printed
92 *
93 * @return string The content of the column to output
94 */
95 protected function column_default( $snippet, $column_name ) {
96
97 switch ( $column_name ) {
98 case 'id':
99 return $snippet->id;
100 case 'description':
101 return empty( $snippet->desc ) ? '&#8212;' :
102 apply_filters( 'code_snippets/list_table/column_description', $snippet->desc );
103 default:
104 return apply_filters( "code_snippets/list_table/column_{$column_name}", $snippet );
105 }
106 }
107
108 /**
109 * Retrieve a URL to perform an action on a snippet
110 *
111 * @param string $action
112 * @param Code_Snippet $snippet
113 * @param bool $escape
114 *
115 * @return string
116 */
117 public function get_action_link( $action, $snippet, $escape = true ) {
118
119 // redirect actions to the network dashboard for shared network snippets
120 $network_redirect = $snippet->shared_network && ! $this->is_network;
121
122 // edit links go to a different menu
123 if ( 'edit' === $action ) {
124 return code_snippets()->get_snippet_edit_url( $snippet->id, $network_redirect ? 'network' : 'self' );
125 }
126
127 $query_args = array( 'action' => $action, 'id' => $snippet->id );
128
129 $url = $network_redirect ? add_query_arg( $query_args, code_snippets()->get_menu_url( 'manage', 'network' ) ) : add_query_arg( $query_args );
130
131 return $escape ? esc_url( $url ) : $url;
132 }
133
134 /**
135 * Build a list of action links for individual snippets
136 *
137 * @param Code_Snippet $snippet The current snippet
138 *
139 * @return array The action links HTML
140 */
141 private function get_snippet_action_links( Code_Snippet $snippet ) {
142 $actions = array();
143
144 if ( ! $this->is_network && $snippet->network && ! $snippet->shared_network ) {
145 // display special links if on a subsite and dealing with a network-active snippet
146
147 if ( $snippet->active ) {
148 $actions['network_active'] = esc_html__( 'Network Active', 'code-snippets' );
149 } else {
150 $actions['network_only'] = esc_html__( 'Network Only', 'code-snippets' );
151 }
152 } elseif ( ! $snippet->shared_network || current_user_can( code_snippets()->get_network_cap_name() ) ) {
153 // if the snippet is a shared network snippet, only display extra actions if the user has network permissions
154
155 $simple_actions = array(
156 'edit' => esc_html__( 'Edit', 'code-snippets' ),
157 'clone' => esc_html__( 'Clone', 'code-snippets' ),
158 'export' => esc_html__( 'Export', 'code-snippets' ),
159 );
160
161 foreach ( $simple_actions as $action => $label ) {
162 $actions[ $action ] = sprintf( '<a href="%s">%s</a>', $this->get_action_link( $action, $snippet ), $label );
163 }
164
165 $actions['delete'] = sprintf(
166 '<a href="%2$s" class="delete" onclick="%3$s">%1$s</a>',
167 esc_html__( 'Delete', 'code-snippets' ),
168 $this->get_action_link( 'delete', $snippet ),
169 esc_js( sprintf(
170 'return confirm("%s");',
171 esc_html__( 'You are about to permanently delete the selected item.', 'code-snippets' ) . "\n" .
172 esc_html__( "'Cancel' to stop, 'OK' to delete.", 'code-snippets' )
173 ) )
174 );
175 }
176
177 return apply_filters( 'code_snippets/list_table/row_actions', $actions );
178 }
179
180 /**
181 * Retrieve the code for a snippet activation switch
182 *
183 * @param Code_Snippet $snippet
184 *
185 * @return string
186 */
187 protected function get_activation_switch( $snippet ) {
188 $action = $snippet->active ? 'deactivate' : 'activate';
189
190 if ( $this->is_network && $snippet->shared_network || ( ! $this->is_network && $snippet->network && ! $snippet ) ) {
191 return '';
192 }
193
194 if ( $snippet->shared_network ) {
195 $action .= 'shared';
196 }
197
198 /*
199 if ( $snippet->active ) {
200 $actions['deactivate'] = sprintf(
201 $link_format,
202 $snippet->network ? esc_html__( 'Network Deactivate', 'code-snippets' ) : esc_html__( 'Deactivate', 'code-snippets' ),
203 $this->get_action_link( 'deactivate', $snippet )
204 );
205 } elseif ( 'single-use' === $snippet->scope ) {
206
207 $actions['run_once'] = sprintf( $link_format, esc_html__( 'Run Once', 'code-snippets' ), $this->get_action_link( 'run-once', $snippet ) );
208
209 } else {
210 $actions['activate'] = sprintf(
211 $link_format,
212 $snippet->network ? esc_html__( 'Network Activate', 'code-snippets' ) : esc_html__( 'Activate', 'code-snippets' ),
213 $this->get_action_link( 'activate', $snippet )
214 );
215 }
216 */
217
218 return sprintf(
219 '<a class="snippet-activation-switch" href="%s"></a> ',
220 $this->get_action_link( $action, $snippet )
221 );
222 }
223
224 /**
225 * Build the content of the snippet name column
226 *
227 * @param Code_Snippet $snippet The snippet being used for the current row
228 *
229 * @return string The content of the column to output
230 */
231 protected function column_name( $snippet ) {
232
233 /* translators: %d: snippet identifier */
234 $title = empty( $snippet->name ) ? sprintf( __( 'Untitled #%d', 'code-snippets' ), $snippet->id ) : $snippet->name;
235
236 $row_actions = $this->row_actions(
237 $this->get_snippet_action_links( $snippet ),
238 apply_filters( 'code_snippets/list_table/row_actions_always_visible', true )
239 );
240
241 $out = esc_html( $title );
242
243 if ( 'global' !== $snippet->scope ) {
244 $out .= ' <span class="dashicons dashicons-' . $snippet->scope_icon . '"></span>';
245 }
246
247 /* Add a link to the snippet if it isn't an unreadable network-only snippet */
248 if ( $this->is_network || ! $snippet->network || current_user_can( code_snippets()->get_network_cap_name() ) ) {
249
250 $out = sprintf(
251 '<a href="%s" class="snippet-name">%s</a>',
252 code_snippets()->get_snippet_edit_url( $snippet->id, $snippet->network ? 'network' : 'admin' ),
253 $out
254 );
255 }
256
257 if ( $snippet->shared_network ) {
258 $out .= ' <span class="badge">' . esc_html__( 'Shared on Network', 'code-snippets' ) . '</span>';
259 }
260
261 /* Return the name contents */
262
263 $out = apply_filters( 'code_snippets/list_table/column_name', $out, $snippet );
264 return $this->get_activation_switch( $snippet ) . $out . $row_actions;
265 }
266
267 /**
268 * Builds the checkbox column content
269 *
270 * @param Code_Snippet $snippet The snippet being used for the current row
271 *
272 * @return string The column content to be printed
273 */
274 protected function column_cb( $snippet ) {
275
276 $out = sprintf(
277 '<input type="checkbox" name="%s[]" value="%s" />',
278 $snippet->shared_network ? 'shared_ids' : 'ids',
279 $snippet->id
280 );
281
282 return apply_filters( 'code_snippets/list_table/column_cb', $out, $snippet );
283 }
284
285 /**
286 * Output the content of the tags column
287 * This function is used once for each row
288 *
289 * @since 2.0
290 *
291 * @param Code_Snippet $snippet The snippet being used for the current row
292 *
293 * @return string The column output
294 */
295 protected function column_tags( $snippet ) {
296
297 /* Return a placeholder if there are no tags */
298 if ( empty( $snippet->tags ) ) {
299 return '&#8212;';
300 }
301
302 $out = array();
303
304 /* Loop through the tags and create a link for each one */
305 foreach ( $snippet->tags as $tag ) {
306 $out[] = sprintf( '<a href="%s">%s</a>',
307 esc_url( add_query_arg( 'tag', esc_attr( $tag ) ) ),
308 esc_html( $tag )
309 );
310 }
311
312 return join( ', ', $out );
313 }
314
315 /**
316 * Output the content of the priority column
317 *
318 * @param Code_Snippet $snippet
319 *
320 * @return string
321 */
322 protected function column_priority( $snippet ) {
323
324 return sprintf( '<input type="number" class="snippet-priority" value="%d" step="1" disabled>', $snippet->priority );
325 }
326
327 /**
328 * Define the column headers for the table
329 *
330 * @return array The column headers, ID paired with label
331 */
332 public function get_columns() {
333 $columns = array(
334 'cb' => '<input type="checkbox">',
335 'name' => __( 'Name', 'code-snippets' ),
336 'id' => __( 'ID', 'code-snippets' ),
337 'description' => __( 'Description', 'code-snippets' ),
338 'tags' => __( 'Tags', 'code-snippets' ),
339 'priority' => __( 'Priority', 'code-snippets' ),
340 );
341
342 if ( ! code_snippets_get_setting( 'general', 'enable_description' ) ) {
343 unset( $columns['description'] );
344 }
345
346 if ( ! code_snippets_get_setting( 'general', 'enable_tags' ) ) {
347 unset( $columns['tags'] );
348 }
349
350 return apply_filters( 'code_snippets/list_table/columns', $columns );
351 }
352
353 /**
354 * Define the columns that can be sorted
355 *
356 * @return array The IDs of the columns that can be sorted
357 */
358 public function get_sortable_columns() {
359 $sortable_columns = array(
360 'id' => array( 'id', true ),
361 'name' => array( 'name', false ),
362 'tags' => array( 'tags_list', true ),
363 'priority' => array( 'priority', true ),
364 );
365
366 return apply_filters( 'code_snippets/list_table/sortable_columns', $sortable_columns );
367 }
368
369 /**
370 * Define the bulk actions to include in the drop-down menus
371 *
372 * @return array An array of menu items with the ID paired to the label
373 */
374 public function get_bulk_actions() {
375 $actions = array(
376 'activate-selected' => $this->is_network ? __( 'Network Activate', 'code-snippets' ) : __( 'Activate', 'code-snippets' ),
377 'deactivate-selected' => $this->is_network ? __( 'Network Deactivate', 'code-snippets' ) : __( 'Deactivate', 'code-snippets' ),
378 'clone-selected' => __( 'Clone', 'code-snippets' ),
379 'download-selected' => __( 'Download', 'code-snippets' ),
380 'export-selected' => __( 'Export', 'code-snippets' ),
381 'delete-selected' => __( 'Delete', 'code-snippets' ),
382 );
383
384 return apply_filters( 'code_snippets/list_table/bulk_actions', $actions );
385 }
386
387 /**
388 * Retrieve the classes for the table
389 *
390 * We override this in order to add 'snippets' as a class for custom styling
391 *
392 * @return array The classes to include on the table element
393 */
394 public function get_table_classes() {
395 $classes = array( 'widefat', $this->_args['plural'] );
396
397 return apply_filters( 'code_snippets/list_table/table_classes', $classes );
398 }
399
400 /**
401 * Retrieve the 'views' of the table
402 *
403 * Example: active, inactive, recently active
404 *
405 * @return array A list of the view labels linked to the view
406 */
407 public function get_views() {
408 global $totals, $status;
409 $status_links = array();
410
411 /* Loop through the view counts */
412 foreach ( $totals as $type => $count ) {
413
414 /* Don't show the view if there is no count */
415 if ( ! $count ) {
416 continue;
417 }
418
419 /* Define the labels for each view */
420 $labels = array(
421
422 /* translators: %s: total number of snippets */
423 'all' => _n( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'code-snippets' ),
424
425 /* translators: %s: total number of active snippets */
426 'active' => _n( 'Active <span class="count">(%s)</span>', 'Active <span class="count">(%s)</span>', $count, 'code-snippets' ),
427
428 /* translators: %s: total number of inactive snippets */
429 'inactive' => _n( 'Inactive <span class="count">(%s)</span>', 'Inactive <span class="count">(%s)</span>', $count, 'code-snippets' ),
430
431 /* translators: %s: total number of recently activated snippets */
432 'recently_activated' => _n( 'Recently Active <span class="count">(%s)</span>', 'Recently Active <span class="count">(%s)</span>', $count, 'code-snippets' ),
433 );
434
435 /* The page URL with the status parameter */
436 $url = esc_url( add_query_arg( 'status', $type ) );
437
438 /* Add a class if this view is currently being viewed */
439 $class = $type === $status ? ' class="current"' : '';
440
441 /* Add the view count to the label */
442 $text = sprintf( $labels[ $type ], number_format_i18n( $count ) );
443
444 /* Construct the link */
445 $status_links[ $type ] = sprintf( '<a href="%s"%s>%s</a>', $url, $class, $text );
446 }
447
448 /* Filter and return the list of views */
449
450 return apply_filters( 'code_snippets/list_table/views', $status_links );
451 }
452
453 /**
454 * Gets the tags of the snippets currently being viewed in the table
455 * @since 2.0
456 */
457 public function get_current_tags() {
458 global $snippets, $status;
459
460 /* If we're not viewing a snippets table, get all used tags instead */
461 if ( ! isset( $snippets, $status ) ) {
462 $tags = get_all_snippet_tags();
463 } else {
464 $tags = array();
465
466 /* Merge all tags into a single array */
467 foreach ( $snippets[ $status ] as $snippet ) {
468 $tags = array_merge( $snippet->tags, $tags );
469 }
470
471 /* Remove duplicate tags */
472 $tags = array_unique( $tags );
473 }
474
475 sort( $tags );
476
477 return $tags;
478 }
479
480 /**
481 * Add filters and extra actions above and below the table
482 *
483 * @param string $which Are the actions displayed on the table top or bottom
484 */
485 public function extra_tablenav( $which ) {
486 global $status, $wpdb;
487
488 if ( 'top' === $which ) {
489
490 /* Tags dropdown filter */
491 $tags = $this->get_current_tags();
492
493 if ( count( $tags ) ) {
494 $query = isset( $_GET['tag'] ) ? $_GET['tag'] : '';
495
496 echo '<div class="alignleft actions">';
497 echo '<select name="tag">';
498
499 printf( "<option %s value=''>%s</option>\n",
500 selected( $query, '', false ),
501 __( 'Show all tags', 'code-snippets' )
502 );
503
504 foreach ( $tags as $tag ) {
505
506 printf( "<option %s value='%s'>%s</option>\n",
507 selected( $query, $tag, false ),
508 esc_attr( $tag ),
509 $tag
510 );
511 }
512
513 echo '</select>';
514
515 submit_button( __( 'Filter', 'code-snippets' ), 'button', 'filter_action', false );
516 echo '</div>';
517 }
518 }
519
520 echo '<div class="alignleft actions">';
521
522 if ( 'recently_activated' === $status ) {
523 submit_button( __( 'Clear List', 'code-snippets' ), 'secondary', 'clear-recent-list', false );
524 }
525
526 do_action( 'code_snippets/list_table/actions', $which );
527
528 echo '</div>';
529 }
530
531 /**
532 * Output form fields needed to preserve important
533 * query vars over form submissions
534 *
535 * @param string $context In what context are the fields being outputted?
536 */
537 public function required_form_fields( $context = 'main' ) {
538
539 $vars = apply_filters(
540 'code_snippets/list_table/required_form_fields',
541 array( 'page', 's', 'status', 'paged', 'tag' ),
542 $context
543 );
544
545 if ( 'search_box' === $context ) {
546 /* Remove the 's' var if we're doing this for the search box */
547 $vars = array_diff( $vars, array( 's' ) );
548 }
549
550 foreach ( $vars as $var ) {
551 if ( ! empty( $_REQUEST[ $var ] ) ) {
552 printf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $var ), esc_attr( $_REQUEST[ $var ] ) );
553 print "\n";
554 }
555 }
556
557 do_action( 'code_snippets/list_table/print_required_form_fields', $context );
558 }
559
560 /**
561 * Processes a bulk action
562 *
563 * @uses activate_snippet() to activate snippets
564 * @uses deactivate_snippet() to deactivate snippets
565 * @uses delete_snippet() to delete snippets
566 * @uses wp_redirect() to pass the results to the current page
567 * @uses add_query_arg() to append the results to the current URI
568 */
569 public function process_bulk_actions() {
570
571 if ( isset( $_POST['clear-recent-list'] ) ) {
572 if ( $this->is_network ) {
573 update_site_option( 'recently_activated_snippets', array() );
574 } else {
575 update_option( 'recently_activated_snippets', array() );
576 }
577 }
578
579 if ( isset( $_GET['action'], $_GET['id'] ) ) {
580
581 $id = absint( $_GET['id'] );
582 $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'action', 'id' ) );
583
584 switch ( sanitize_key( $_GET['action'] ) ) {
585
586 case 'activate':
587 activate_snippet( $id, $this->is_network );
588 $result = 'activated';
589 break;
590
591 case 'run-once':
592 activate_snippet( $id, $this->is_network );
593 $result = 'executed';
594 break;
595
596 case 'deactivate':
597 deactivate_snippet( $id, $this->is_network );
598 $result = 'deactivated';
599 break;
600
601 case 'activate-shared':
602 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
603
604 if ( ! in_array( $id, $active_shared_snippets ) ) {
605 $active_shared_snippets[] = $id;
606 update_option( 'active_shared_network_snippets', $active_shared_snippets );
607 }
608
609 $result = 'activated';
610 break;
611
612 case 'deactivate-shared':
613 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
614 update_option( 'active_shared_network_snippets', array_diff( $active_shared_snippets, array( $id ) ) );
615
616 $result = 'deactivated';
617 break;
618
619 case 'clone':
620 $this->clone_snippets( array( $id ) );
621 $result = 'cloned';
622 break;
623
624 case 'delete':
625 delete_snippet( $id, $this->is_network );
626 $result = 'deleted';
627 break;
628
629 case 'export':
630 export_snippets( array( $id ) );
631 break;
632
633 case 'download':
634 download_snippets( array( $id ) );
635 break;
636 }
637
638 if ( isset( $result ) ) {
639 wp_redirect( esc_url_raw( add_query_arg( 'result', $result ) ) );
640 exit;
641 }
642 }
643
644 if ( ! isset( $_POST['ids'] ) && ! isset( $_POST['shared_ids'] ) ) {
645 return;
646 }
647
648 $ids = isset( $_POST['ids'] ) ? $_POST['ids'] : array();
649 $_SERVER['REQUEST_URI'] = remove_query_arg( 'action' );
650
651 switch ( $this->current_action() ) {
652
653 case 'activate-selected':
654
655 foreach ( $ids as $id ) {
656 activate_snippet( $id, $this->is_network );
657 }
658
659 /* Process the shared network snippets */
660 if ( isset( $_POST['shared_ids'] ) && is_multisite() && ! $this->is_network ) {
661 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
662
663 foreach ( $_POST['shared_ids'] as $id ) {
664 if ( ! in_array( $id, $active_shared_snippets ) ) {
665 $active_shared_snippets[] = $id;
666 }
667 }
668
669 update_option( 'active_shared_network_snippets', $active_shared_snippets );
670 }
671
672 $result = 'activated-multi';
673 break;
674
675 case 'deactivate-selected':
676
677 foreach ( $ids as $id ) {
678 deactivate_snippet( $id, $this->is_network );
679 }
680
681 /* Process the shared network snippets */
682 if ( isset( $_POST['shared_ids'] ) && is_multisite() && ! $this->is_network ) {
683 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
684 $active_shared_snippets = ( '' === $active_shared_snippets ) ? array() : $active_shared_snippets;
685 $active_shared_snippets = array_diff( $active_shared_snippets, $_POST['shared_ids'] );
686 update_option( 'active_shared_network_snippets', $active_shared_snippets );
687 }
688
689 $result = 'deactivated-multi';
690 break;
691
692 case 'export-selected':
693 export_snippets( $ids );
694 break;
695
696 case 'download-selected':
697 download_snippets( $ids );
698 break;
699
700 case 'clone-selected':
701 $this->clone_snippets( $ids );
702 $result = 'cloned-multi';
703 break;
704
705 case 'delete-selected':
706 foreach ( $ids as $id ) {
707 delete_snippet( $id, $this->is_network );
708 }
709 $result = 'deleted-multi';
710 break;
711 }
712
713 if ( isset( $result ) ) {
714 wp_redirect( esc_url_raw( add_query_arg( 'result', $result ) ) );
715 exit;
716 }
717 }
718
719 /**
720 * Message to display if no snippets are found
721 */
722 public function no_items() {
723 esc_html_e( "It looks like you don't have any snippets.", 'code-snippets' );
724 printf(
725 ' <a href="%s">%s</a>',
726 esc_url( code_snippets()->get_menu_url( 'add' ) ),
727 esc_html__( 'Perhaps you would like to add a new one?', 'code-snippets' )
728 );
729 }
730
731 /**
732 *
733 */
734 private function fetch_shared_network_snippets() {
735 /** @var wpdb $wpdb */
736 global $snippets, $wpdb;
737 $db = code_snippets()->db;
738
739 if ( ! is_multisite() || ! $ids = get_site_option( 'shared_network_snippets', false ) ) {
740 return;
741 }
742
743 if ( $this->is_network ) {
744 $limit = count( $snippets['all'] );
745
746 /** @var Code_Snippet $snippet */
747 for ( $i = 0; $i < $limit; $i++ ) {
748 $snippet = &$snippets['all'][ $i ];
749
750 if ( in_array( $snippet->id, $ids ) ) {
751 $snippet->shared_network = true;
752 $snippet->tags = array_merge( $snippet->tags, array( 'shared on network' ) );
753 $snippet->active = false;
754 }
755 }
756 } else {
757
758 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
759
760 $sql = sprintf( "SELECT * FROM {$db->ms_table} WHERE id IN (%s)",
761 implode( ',', array_fill( 0, count( $ids ), '%d' ) )
762 );
763
764 $shared_snippets = $wpdb->get_results( $wpdb->prepare( $sql, $ids ), ARRAY_A );
765
766 foreach ( $shared_snippets as $index => $snippet ) {
767 $snippet = new Code_Snippet( $snippet );
768 $snippet->network = true;
769 $snippet->shared_network = true;
770 $snippet->tags = array_merge( $snippet->tags, array( 'shared on network' ) );
771 $snippet->active = in_array( $snippet->id, $active_shared_snippets );
772
773 $shared_snippets[ $index ] = $snippet;
774 }
775
776 $snippets['all'] = array_merge( $snippets['all'], $shared_snippets );
777 }
778 }
779
780 /**
781 * Prepares the items to later display in the table.
782 * Should run before any headers are sent.
783 */
784 public function prepare_items() {
785 global $status, $snippets, $totals, $s;
786
787 wp_reset_vars( array( 'orderby', 'order', 's' ) );
788
789 $screen = get_current_screen();
790 $user = get_current_user_id();
791
792 /* First, lets process the bulk actions */
793 $this->process_bulk_actions();
794
795 /* Initialize the $snippets array */
796 $snippets = array_fill_keys( $this->statuses, array() );
797
798 /* Fetch all snippets */
799 $snippets['all'] = apply_filters( 'code_snippets/list_table/get_snippets', get_snippets( array() ) );
800 $this->fetch_shared_network_snippets();
801
802 /* Redirect POST'ed tag filter to GET */
803 if ( isset( $_POST['tag'] ) ) {
804 $location = empty( $_POST['tag'] ) ? remove_query_arg( 'tag' ) : add_query_arg( 'tag', $_POST['tag'] );
805 wp_redirect( esc_url_raw( $location ) );
806 exit;
807 }
808
809 /* Add scope tags */
810 if ( code_snippets_get_setting( 'general', 'snippet_scope_enabled' ) ) {
811 foreach ( $snippets['all'] as $snippet ) {
812
813 if ( 'global' !== $snippet->scope ) {
814 $snippet->tags = array_merge( $snippet->tags, array( $snippet->scope ) );
815 }
816 }
817 }
818
819 /* Filter snippets by tag */
820 if ( ! empty( $_GET['tag'] ) ) {
821 $snippets['all'] = array_filter( $snippets['all'], array( $this, 'tags_filter_callback' ) );
822 }
823
824 /* Filter snippets based on search query */
825 if ( $s ) {
826 $snippets['all'] = array_filter( $snippets['all'], array( $this, 'search_callback' ) );
827 }
828
829 /* Clear recently activated snippets older than a week */
830 $recently_activated = $this->is_network ?
831 get_site_option( 'recently_activated_snippets', array() ) :
832 get_option( 'recently_activated_snippets', array() );
833
834 foreach ( $recently_activated as $key => $time ) {
835
836 if ( $time + WEEK_IN_SECONDS < time() ) {
837 unset( $recently_activated[ $key ] );
838 }
839 }
840
841 $this->is_network ?
842 update_site_option( 'recently_activated_snippets', $recently_activated ) :
843 update_option( 'recently_activated_snippets', $recently_activated );
844
845 /* Filter snippets into individual sections */
846 foreach ( $snippets['all'] as $snippet ) {
847
848 if ( $snippet->active ) {
849 $snippets['active'][] = $snippet;
850 } else {
851 $snippets['inactive'][] = $snippet;
852
853 /* Was the snippet recently deactivated? */
854 if ( isset( $recently_activated[ $snippet->id ] ) ) {
855 $snippets['recently_activated'][] = $snippet;
856 }
857 }
858 }
859
860 /* Count the totals for each section */
861 $totals = array();
862 foreach ( $snippets as $type => $list ) {
863 $totals[ $type ] = count( $list );
864 }
865
866 /* If the current status is empty, default tp all */
867 if ( empty( $snippets[ $status ] ) ) {
868 $status = 'all';
869 }
870
871 /* Get the current data */
872 $data = $snippets[ $status ];
873
874 /* Decide how many records per page to show by
875 getting the user's setting in the Screen Options panel */
876 $sort_by = $screen->get_option( 'per_page', 'option' );
877 $per_page = get_user_meta( $user, $sort_by, true );
878
879 if ( empty( $per_page ) || $per_page < 1 ) {
880 $per_page = $screen->get_option( 'per_page', 'default' );
881 }
882
883 $per_page = (int) $per_page;
884
885 $this->_column_headers = $this->get_column_info();
886
887 usort( $data, array( $this, 'usort_reorder_callback' ) );
888
889 /* Determine what page the user is currently looking at */
890 $current_page = $this->get_pagenum();
891
892 /* Check how many items are in the data array */
893 $total_items = count( $data );
894
895 /* The WP_List_Table class does not handle pagination for us, so we need
896 to ensure that the data is trimmed to only the current page. */
897 $data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page );
898
899 /* Now we can add our *sorted* data to the items property,
900 where it can be used by the rest of the class. */
901 $this->items = $data;
902
903 /* We register our pagination options and calculations */
904 $this->set_pagination_args( array(
905 'total_items' => $total_items, // Calculate the total number of items
906 'per_page' => $per_page, // Determine how many items to show on a page
907 'total_pages' => ceil( $total_items / $per_page ), // Calculate the total number of pages
908 ) );
909 }
910
911 /**
912 * Determine the sort ordering for two pieces of data
913 *
914 * @ignore
915 *
916 * @param string $field name of the field that the data belongs to
917 * @param string $a_data
918 * @param string $b_data
919 *
920 * @return int
921 */
922 private function get_sort_direction( $field, $a_data, $b_data ) {
923
924 // if the data is numeric, then calculate the ordering directly
925 if ( is_numeric( $a_data ) ) {
926 return $a_data - $b_data;
927 }
928
929 // if only one of the data points is empty, then place it before the one which is not
930 if ( '' === $a_data xor '' === $b_data ) {
931 return '' === $a_data ? 1 : -1;
932 }
933
934 // otherwise, sort using the default string sort order
935 return strcmp( $a_data, $b_data );
936 }
937
938 /**
939 * Callback for usort() used to sort snippets
940 *
941 * @ignore
942 *
943 * @param Code_Snippet $a The first snippet to compare
944 * @param Code_Snippet $b The second snippet to compare
945 *
946 * @return int The sort order
947 */
948 private function usort_reorder_callback( $a, $b ) {
949
950 // sort by ID by default
951 $orderby = (
952 ! empty( $_REQUEST['orderby'] )
953 ? $_REQUEST['orderby']
954 : apply_filters( 'code_snippets/list_table/default_orderby', 'priority' )
955 );
956
957 // sort ascending by default
958 $order = (
959 ! empty( $_REQUEST['order'] )
960 ? $_REQUEST['order']
961 : apply_filters( 'code_snippets/list_table/default_order', 'asc' )
962 );
963
964 $result = $this->get_sort_direction( $orderby, $a->$orderby, $b->$orderby );
965
966 if ( 0 === $result && 'id' !== $orderby ) {
967 $result = $this->get_sort_direction( 'id', $a->id, $b->id );
968 }
969
970 // apply the sort direction to the calculated order
971 return ( 'asc' === $order ) ? $result : -$result;
972 }
973
974 /**
975 * Callback for search function
976 * @ignore
977 *
978 * @param Code_Snippet $snippet The snippet being filtered
979 *
980 * @return bool The result of the filter
981 */
982 private function search_callback( $snippet ) {
983 static $term;
984 if ( is_null( $term ) ) {
985 $term = stripslashes( $_REQUEST['s'] );
986 }
987
988 $fields = array( 'name', 'desc', 'code', 'tags_list' );
989
990 foreach ( $fields as $field ) {
991 if ( false !== stripos( $snippet->$field, $term ) ) {
992 return true;
993 }
994 }
995
996 return false;
997 }
998
999 /**
1000 * Callback for filtering snippets by tag
1001 * @ignore
1002 *
1003 * @param Code_Snippet $snippet The snippet being filtered
1004 *
1005 * @return bool The result of the filter
1006 */
1007 private function tags_filter_callback( $snippet ) {
1008 $tags = explode( ',', $_GET['tag'] );
1009
1010 foreach ( $tags as $tag ) {
1011 if ( in_array( $tag, $snippet->tags ) ) {
1012 return true;
1013 }
1014 }
1015 }
1016
1017 /**
1018 * Display a notice showing the current search terms
1019 * @since 1.7
1020 */
1021 public function search_notice() {
1022 if ( ! empty( $_REQUEST['s'] ) || ! empty( $_GET['tag'] ) ) {
1023
1024 echo '<span class="subtitle">' . __( 'Search results', 'code-snippets' );
1025
1026 if ( ! empty( $_REQUEST['s'] ) ) {
1027 /* translators: %s: search query */
1028 echo sprintf( __( ' for &ldquo;%s&rdquo;', 'code-snippets' ), esc_html( $_REQUEST['s'] ) );
1029 }
1030
1031 if ( ! empty( $_GET['tag'] ) ) {
1032 /* translators: %s: tag name */
1033 echo sprintf( __( ' in tag &ldquo;%s&rdquo;', 'code-snippets' ), esc_html( $_GET['tag'] ) );
1034 }
1035
1036 echo '</span>';
1037
1038 printf(
1039 /* translators: 1: link URL, 2: link text */
1040 '&nbsp;<a class="button clear-filters" href="%s">%s</a>',
1041 esc_url( remove_query_arg( array( 's', 'tag' ) ) ),
1042 __( 'Clear Filters', 'code-snippets' )
1043 );
1044 }
1045 }
1046
1047 /**
1048 * Outputs content for a single row of the table
1049 *
1050 * @param Code_Snippet $snippet The snippet being used for the current row
1051 */
1052 public function single_row( $snippet ) {
1053 $row_class = ( $snippet->active ? 'active-snippet' : 'inactive-snippet' );
1054
1055 if ( code_snippets_get_setting( 'general', 'snippet_scope_enabled' ) ) {
1056 $row_class .= sprintf( ' %s-scope', $snippet->scope );
1057 }
1058
1059 if ( $snippet->shared_network ) {
1060 $row_class .= ' shared-network-snippet';
1061 }
1062
1063 printf( '<tr class="%s">', $row_class );
1064 $this->single_row_columns( $snippet );
1065 echo '</tr>';
1066 }
1067
1068 /**
1069 * Clone a selection of snippets
1070 *
1071 * @param array $ids
1072 */
1073 private function clone_snippets( $ids ) {
1074 $snippets = get_snippets( $ids, $this->is_network );
1075
1076 /** @var Code_Snippet $snippet */
1077 foreach ( $snippets as $snippet ) {
1078
1079 // copy all data from the previous snippet aside from the ID and active status
1080 $snippet->id = 0;
1081 $snippet->active = false;
1082
1083 save_snippet( $snippet );
1084 }
1085 }
1086 }
1087