PluginProbe
Code Snippets / 2.12.1
Code Snippets v2.12.1
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.1, at php/class-list-table.php

1,102 lines 31.0 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 /**
562 * Clear the recently activated snippets list if we've clicked the button
563 * @return string The action to execute
564 */
565 public function current_action() {
566 if ( isset( $_POST['clear-recent-list'] ) ) {
567 $action = 'clear-recent-list';
568 } else {
569 $action = parent::current_action();
570 }
571
572 return apply_filters( 'code_snippets/list_table/current_action', $action );
573 }
574
575 /**
576 * Processes a bulk action
577 *
578 * @uses activate_snippet() to activate snippets
579 * @uses deactivate_snippet() to deactivate snippets
580 * @uses delete_snippet() to delete snippets
581 * @uses wp_redirect() to pass the results to the current page
582 * @uses add_query_arg() to append the results to the current URI
583 */
584 public function process_bulk_actions() {
585
586 if ( isset( $_GET['action'], $_GET['id'] ) ) {
587
588 $id = absint( $_GET['id'] );
589 $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'action', 'id' ) );
590
591 switch ( sanitize_key( $_GET['action'] ) ) {
592
593 case 'activate':
594 activate_snippet( $id, $this->is_network );
595 $result = 'activated';
596 break;
597
598 case 'run-once':
599 activate_snippet( $id, $this->is_network );
600 $result = 'executed';
601 break;
602
603 case 'deactivate':
604 deactivate_snippet( $id, $this->is_network );
605 $result = 'deactivated';
606 break;
607
608 case 'activate-shared':
609 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
610
611 if ( ! in_array( $id, $active_shared_snippets ) ) {
612 $active_shared_snippets[] = $id;
613 update_option( 'active_shared_network_snippets', $active_shared_snippets );
614 }
615
616 $result = 'activated';
617 break;
618
619 case 'deactivate-shared':
620 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
621 update_option( 'active_shared_network_snippets', array_diff( $active_shared_snippets, array( $id ) ) );
622
623 $result = 'deactivated';
624 break;
625
626 case 'clone':
627 $this->clone_snippets( array( $id ) );
628 $result = 'cloned';
629 break;
630
631 case 'delete':
632 delete_snippet( $id, $this->is_network );
633 $result = 'deleted';
634 break;
635
636 case 'export':
637 export_snippets( array( $id ) );
638 break;
639
640 case 'download':
641 download_snippets( array( $id ) );
642 break;
643 }
644
645 if ( isset( $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 case 'clear-recent-list':
720 if ( $this->is_network ) {
721 update_site_option( 'recently_activated_snippets', array() );
722 } else {
723 update_option( 'recently_activated_snippets', array() );
724 }
725 break;
726 }
727
728 if ( isset( $result ) ) {
729 wp_redirect( esc_url_raw( add_query_arg( 'result', $result ) ) );
730 exit;
731 }
732 }
733
734 /**
735 * Message to display if no snippets are found
736 */
737 public function no_items() {
738 esc_html_e( "It looks like you don't have any snippets.", 'code-snippets' );
739 printf(
740 ' <a href="%s">%s</a>',
741 esc_url( code_snippets()->get_menu_url( 'add' ) ),
742 esc_html__( 'Perhaps you would like to add a new one?', 'code-snippets' )
743 );
744 }
745
746 /**
747 *
748 */
749 private function fetch_shared_network_snippets() {
750 /** @var wpdb $wpdb */
751 global $snippets, $wpdb;
752 $db = code_snippets()->db;
753
754 if ( ! is_multisite() || ! $ids = get_site_option( 'shared_network_snippets', false ) ) {
755 return;
756 }
757
758 if ( $this->is_network ) {
759 $limit = count( $snippets['all'] );
760
761 /** @var Code_Snippet $snippet */
762 for ( $i = 0; $i < $limit; $i++ ) {
763 $snippet = &$snippets['all'][ $i ];
764
765 if ( in_array( $snippet->id, $ids ) ) {
766 $snippet->shared_network = true;
767 $snippet->tags = array_merge( $snippet->tags, array( 'shared on network' ) );
768 $snippet->active = false;
769 }
770 }
771 } else {
772
773 $active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
774
775 $sql = sprintf( "SELECT * FROM {$db->ms_table} WHERE id IN (%s)",
776 implode( ',', array_fill( 0, count( $ids ), '%d' ) )
777 );
778
779 $shared_snippets = $wpdb->get_results( $wpdb->prepare( $sql, $ids ), ARRAY_A );
780
781 foreach ( $shared_snippets as $index => $snippet ) {
782 $snippet = new Code_Snippet( $snippet );
783 $snippet->network = true;
784 $snippet->shared_network = true;
785 $snippet->tags = array_merge( $snippet->tags, array( 'shared on network' ) );
786 $snippet->active = in_array( $snippet->id, $active_shared_snippets );
787
788 $shared_snippets[ $index ] = $snippet;
789 }
790
791 $snippets['all'] = array_merge( $snippets['all'], $shared_snippets );
792 }
793 }
794
795 /**
796 * Prepares the items to later display in the table.
797 * Should run before any headers are sent.
798 */
799 public function prepare_items() {
800 global $status, $snippets, $totals, $s;
801
802 wp_reset_vars( array( 'orderby', 'order', 's' ) );
803
804 $screen = get_current_screen();
805 $user = get_current_user_id();
806
807 /* First, lets process the bulk actions */
808 $this->process_bulk_actions();
809
810 /* Initialize the $snippets array */
811 $snippets = array_fill_keys( $this->statuses, array() );
812
813 /* Fetch all snippets */
814 $snippets['all'] = apply_filters( 'code_snippets/list_table/get_snippets', get_snippets( array() ) );
815 $this->fetch_shared_network_snippets();
816
817 /* Redirect POST'ed tag filter to GET */
818 if ( isset( $_POST['tag'] ) ) {
819 $location = empty( $_POST['tag'] ) ? remove_query_arg( 'tag' ) : add_query_arg( 'tag', $_POST['tag'] );
820 wp_redirect( esc_url_raw( $location ) );
821 exit;
822 }
823
824 /* Add scope tags */
825 if ( code_snippets_get_setting( 'general', 'snippet_scope_enabled' ) ) {
826 foreach ( $snippets['all'] as $snippet ) {
827
828 if ( 'global' !== $snippet->scope ) {
829 $snippet->tags = array_merge( $snippet->tags, array( $snippet->scope ) );
830 }
831 }
832 }
833
834 /* Filter snippets by tag */
835 if ( ! empty( $_GET['tag'] ) ) {
836 $snippets['all'] = array_filter( $snippets['all'], array( $this, 'tags_filter_callback' ) );
837 }
838
839 /* Filter snippets based on search query */
840 if ( $s ) {
841 $snippets['all'] = array_filter( $snippets['all'], array( $this, 'search_callback' ) );
842 }
843
844 /* Clear recently activated snippets older than a week */
845 $recently_activated = $this->is_network ?
846 get_site_option( 'recently_activated_snippets', array() ) :
847 get_option( 'recently_activated_snippets', array() );
848
849 foreach ( $recently_activated as $key => $time ) {
850
851 if ( $time + WEEK_IN_SECONDS < time() ) {
852 unset( $recently_activated[ $key ] );
853 }
854 }
855
856 $this->is_network ?
857 update_site_option( 'recently_activated_snippets', $recently_activated ) :
858 update_option( 'recently_activated_snippets', $recently_activated );
859
860 /* Filter snippets into individual sections */
861 foreach ( $snippets['all'] as $snippet ) {
862
863 if ( $snippet->active ) {
864 $snippets['active'][] = $snippet;
865 } else {
866 $snippets['inactive'][] = $snippet;
867
868 /* Was the snippet recently activated? */
869 if ( isset( $recently_activated[ $snippet->id ] ) ) {
870 $snippets['recently_activated'][] = $snippet;
871 }
872 }
873 }
874
875 /* Count the totals for each section */
876 $totals = array();
877 foreach ( $snippets as $type => $list ) {
878 $totals[ $type ] = count( $list );
879 }
880
881 /* If the current status is empty, default tp all */
882 if ( empty( $snippets[ $status ] ) ) {
883 $status = 'all';
884 }
885
886 /* Get the current data */
887 $data = $snippets[ $status ];
888
889 /* Decide how many records per page to show by
890 getting the user's setting in the Screen Options panel */
891 $sort_by = $screen->get_option( 'per_page', 'option' );
892 $per_page = get_user_meta( $user, $sort_by, true );
893
894 if ( empty( $per_page ) || $per_page < 1 ) {
895 $per_page = $screen->get_option( 'per_page', 'default' );
896 }
897
898 $per_page = (int) $per_page;
899
900 $this->_column_headers = $this->get_column_info();
901
902 usort( $data, array( $this, 'usort_reorder_callback' ) );
903
904 /* Determine what page the user is currently looking at */
905 $current_page = $this->get_pagenum();
906
907 /* Check how many items are in the data array */
908 $total_items = count( $data );
909
910 /* The WP_List_Table class does not handle pagination for us, so we need
911 to ensure that the data is trimmed to only the current page. */
912 $data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page );
913
914 /* Now we can add our *sorted* data to the items property,
915 where it can be used by the rest of the class. */
916 $this->items = $data;
917
918 /* We register our pagination options and calculations */
919 $this->set_pagination_args( array(
920 'total_items' => $total_items, // Calculate the total number of items
921 'per_page' => $per_page, // Determine how many items to show on a page
922 'total_pages' => ceil( $total_items / $per_page ), // Calculate the total number of pages
923 ) );
924 }
925
926 /**
927 * Determine the sort ordering for two pieces of data
928 *
929 * @ignore
930 *
931 * @param string $field name of the field that the data belongs to
932 * @param string $a_data
933 * @param string $b_data
934 *
935 * @return int
936 */
937 private function get_sort_direction( $field, $a_data, $b_data ) {
938
939 // if the data is numeric, then calculate the ordering directly
940 if ( is_numeric( $a_data ) ) {
941 return $a_data - $b_data;
942 }
943
944 // if only one of the data points is empty, then place it before the one which is not
945 if ( '' === $a_data xor '' === $b_data ) {
946 return '' === $a_data ? 1 : -1;
947 }
948
949 // otherwise, sort using the default string sort order
950 return strcmp( $a_data, $b_data );
951 }
952
953 /**
954 * Callback for usort() used to sort snippets
955 *
956 * @ignore
957 *
958 * @param Code_Snippet $a The first snippet to compare
959 * @param Code_Snippet $b The second snippet to compare
960 *
961 * @return int The sort order
962 */
963 private function usort_reorder_callback( $a, $b ) {
964
965 // sort by ID by default
966 $orderby = (
967 ! empty( $_REQUEST['orderby'] )
968 ? $_REQUEST['orderby']
969 : apply_filters( 'code_snippets/list_table/default_orderby', 'priority' )
970 );
971
972 // sort ascending by default
973 $order = (
974 ! empty( $_REQUEST['order'] )
975 ? $_REQUEST['order']
976 : apply_filters( 'code_snippets/list_table/default_order', 'asc' )
977 );
978
979 $result = $this->get_sort_direction( $orderby, $a->$orderby, $b->$orderby );
980
981 if ( 0 === $result && 'id' !== $orderby ) {
982 $result = $this->get_sort_direction( 'id', $a->id, $b->id );
983 }
984
985 // apply the sort direction to the calculated order
986 return ( 'asc' === $order ) ? $result : -$result;
987 }
988
989 /**
990 * Callback for search function
991 * @ignore
992 *
993 * @param Code_Snippet $snippet The snippet being filtered
994 *
995 * @return bool The result of the filter
996 */
997 private function search_callback( $snippet ) {
998 static $term;
999 if ( is_null( $term ) ) {
1000 $term = stripslashes( $_REQUEST['s'] );
1001 }
1002
1003 $fields = array( 'name', 'desc', 'code', 'tags_list' );
1004
1005 foreach ( $fields as $field ) {
1006 if ( false !== stripos( $snippet->$field, $term ) ) {
1007 return true;
1008 }
1009 }
1010
1011 return false;
1012 }
1013
1014 /**
1015 * Callback for filtering snippets by tag
1016 * @ignore
1017 *
1018 * @param Code_Snippet $snippet The snippet being filtered
1019 *
1020 * @return bool The result of the filter
1021 */
1022 private function tags_filter_callback( $snippet ) {
1023 $tags = explode( ',', $_GET['tag'] );
1024
1025 foreach ( $tags as $tag ) {
1026 if ( in_array( $tag, $snippet->tags ) ) {
1027 return true;
1028 }
1029 }
1030 }
1031
1032 /**
1033 * Display a notice showing the current search terms
1034 * @since 1.7
1035 */
1036 public function search_notice() {
1037 if ( ! empty( $_REQUEST['s'] ) || ! empty( $_GET['tag'] ) ) {
1038
1039 echo '<span class="subtitle">' . __( 'Search results', 'code-snippets' );
1040
1041 if ( ! empty( $_REQUEST['s'] ) ) {
1042 /* translators: %s: search query */
1043 echo sprintf( __( ' for &ldquo;%s&rdquo;', 'code-snippets' ), esc_html( $_REQUEST['s'] ) );
1044 }
1045
1046 if ( ! empty( $_GET['tag'] ) ) {
1047 /* translators: %s: tag name */
1048 echo sprintf( __( ' in tag &ldquo;%s&rdquo;', 'code-snippets' ), esc_html( $_GET['tag'] ) );
1049 }
1050
1051 echo '</span>';
1052
1053 printf(
1054 /* translators: 1: link URL, 2: link text */
1055 '&nbsp;<a class="button clear-filters" href="%s">%s</a>',
1056 esc_url( remove_query_arg( array( 's', 'tag' ) ) ),
1057 __( 'Clear Filters', 'code-snippets' )
1058 );
1059 }
1060 }
1061
1062 /**
1063 * Outputs content for a single row of the table
1064 *
1065 * @param Code_Snippet $snippet The snippet being used for the current row
1066 */
1067 public function single_row( $snippet ) {
1068 $row_class = ( $snippet->active ? 'active-snippet' : 'inactive-snippet' );
1069
1070 if ( code_snippets_get_setting( 'general', 'snippet_scope_enabled' ) ) {
1071 $row_class .= sprintf( ' %s-scope', $snippet->scope );
1072 }
1073
1074 if ( $snippet->shared_network ) {
1075 $row_class .= ' shared-network-snippet';
1076 }
1077
1078 printf( '<tr class="%s">', $row_class );
1079 $this->single_row_columns( $snippet );
1080 echo '</tr>';
1081 }
1082
1083 /**
1084 * Clone a selection of snippets
1085 *
1086 * @param array $ids
1087 */
1088 private function clone_snippets( $ids ) {
1089 $snippets = get_snippets( $ids, $this->is_network );
1090
1091 /** @var Code_Snippet $snippet */
1092 foreach ( $snippets as $snippet ) {
1093
1094 // copy all data from the previous snippet aside from the ID and active status
1095 $snippet->id = 0;
1096 $snippet->active = false;
1097
1098 save_snippet( $snippet );
1099 }
1100 }
1101 }
1102