PluginProbe
Code Snippets / 2.13.3
Code Snippets v2.13.3
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 / class-list-table.php

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

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