PluginProbe
Plugin Groups / 2.0.6
Plugin Groups v2.0.6
trunk 1.0.3 1.1.0 1.2.1 1.2.2 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.9 3.0.0
plugin-groups / classes / class-bulk-actions.php

class-bulk-actions.php in Plugin Groups 2.0.6, at classes/class-bulk-actions.php

116 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Bulk Actions class for Plugin Groups.
4 *
5 * @package plugin_groups
6 */
7
8 namespace Plugin_Groups;
9
10 /**
11 * Plugin_Groups Class.
12 */
13 class Bulk_Actions {
14
15 /**
16 * The single instance of the class.
17 *
18 * @var Plugin_Groups
19 */
20 protected $plugin_groups;
21
22 /**
23 * Initiate the bulk_actions object.
24 *
25 * @param Plugin_Groups $plugin_groups The instance of the main plugin.
26 */
27 public function __construct( Plugin_Groups $plugin_groups ) {
28
29 $this->plugin_groups = $plugin_groups;
30 // Start hooks.
31 $this->setup_hooks();
32 }
33
34 /**
35 * Setup and register WordPress hooks.
36 */
37 protected function setup_hooks() {
38
39 add_filter( 'bulk_actions-plugins', array( $this, 'add_actions' ) );
40 add_filter( 'handle_bulk_actions-plugins', array( $this, 'handle_bulk_action' ), 10, 3 );
41 }
42
43 /**
44 * Enqueue our scripts and data for the bulk actions JS.
45 */
46 protected function enqueue_script() {
47
48 $asset = include PLGGRP_PATH . 'js/bulk-handler.asset.php';
49 wp_enqueue_script( 'plugin-groups-bulk', PLGGRP_URL . 'js/bulk-handler.js', $asset['dependencies'], $asset['version'], true );
50 $groups = $this->plugin_groups->get_groups();
51 wp_add_inline_script( 'plugin-groups-bulk', 'var plgData = ' . wp_json_encode( $groups ), 'before' );
52 }
53
54 /**
55 * Handle our bulk actions.
56 *
57 * @param bool $sendback always false, we'll return the redirect url.
58 * @param string $action The action to do.
59 * @param array $plugins The list of selected plugins.
60 *
61 * @return false|string
62 */
63 public function handle_bulk_action( $sendback, $action, $plugins ) {
64
65 $referer = wp_get_raw_referer();// Get the referer.
66 switch ( $action ) {
67 case 'add-to-group':
68 $selected_group = filter_input( INPUT_POST, 'group_id', FILTER_SANITIZE_STRING );
69 if ( '__new' === $selected_group ) {
70 // Create a new group.
71 $new_group_name = filter_input( INPUT_POST, 'new_group_name', FILTER_SANITIZE_STRING );
72 $succeed = $this->plugin_groups->create_group( $new_group_name, $plugins );
73 if ( $succeed ) {
74 $selected_group = $succeed;
75 }
76 } else {
77 // Add to existing group.
78 $this->plugin_groups->add_to_group( $selected_group, $plugins );
79 }
80 // Change the group to the new group.
81 $sendback = add_query_arg( Plugin_Groups::$slug, $selected_group, $referer );
82 break;
83 case 'remove-from-group':
84 wp_parse_str( wp_parse_url( $referer, PHP_URL_QUERY ), $query );
85 if ( isset( $query[ Plugin_Groups::$slug ] ) ) {
86 $selected_group = $query[ Plugin_Groups::$slug ];
87 $this->plugin_groups->remove_from_group( $selected_group, $plugins );
88 }
89 $sendback = $referer;
90 break;
91 }
92
93 return $sendback;
94 }
95
96 /**
97 * Add our actions to the bulk actions.
98 *
99 * @param array $actions Current array of actions.
100 *
101 * @return array
102 */
103 public function add_actions( $actions ) {
104
105 $current_group = $this->plugin_groups->get_current_group();
106 if ( $current_group ) {
107 $actions['remove-from-group'] = sprintf( __( 'Remove from %s', 'plugin-groups' ), $current_group['name'] );
108 }
109 $actions['add-to-group'] = __( 'Add to group', 'plugin-groups' );
110
111 $this->enqueue_script();
112
113 return $actions;
114 }
115 }
116