PluginProbe
Code Snippets / 2.12.0
Code Snippets v2.12.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.12.0, at php/class-list-table.php

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