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

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

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