PluginProbe
Plugin Groups / 2.0.5
Plugin Groups v2.0.5
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-rest.php

class-rest.php in Plugin Groups 2.0.5, at classes/class-rest.php

170 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Core class for Plugin Groups.
4 *
5 * @package plugin_groups
6 */
7
8 namespace Plugin_Groups;
9
10 /**
11 * Rest Class.
12 */
13 class Rest {
14
15 /**
16 * The single instance of the class.
17 *
18 * @var Plugin_Groups
19 */
20 protected $plugin_groups = null;
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_action( 'rest_api_init', array( $this, 'register_routes' ) );
40 }
41
42 /**
43 * Register REST Endpoint for saving config.
44 */
45 public function register_routes() {
46
47 register_rest_route(
48 Plugin_Groups::$slug,
49 'save',
50 array(
51 'methods' => \WP_REST_Server::CREATABLE,
52 'args' => array(),
53 'callback' => array( $this, 'rest_save_config' ),
54 'permission_callback' => function( \WP_REST_Request $request ) {
55
56 if ( is_multisite() ) {
57 $data = $request->get_json_params();
58 $can = current_user_can_for_blog( $data['siteID'], 'manage_options' );
59 } else {
60 $can = current_user_can( 'manage_options' );
61 }
62
63 return $can;
64 },
65 )
66 );
67
68 register_rest_route(
69 Plugin_Groups::$slug,
70 'load',
71 array(
72 'methods' => \WP_REST_Server::READABLE,
73 'args' => array(),
74 'callback' => array( $this, 'rest_load_config' ),
75 'permission_callback' => function( \WP_REST_Request $request ) {
76
77 $id = $request->get_param( 'siteID' );
78
79 return current_user_can_for_blog( $id, 'manage_options' );
80 },
81 )
82 );
83
84 register_rest_route(
85 Plugin_Groups::$slug,
86 'add',
87 array(
88 'methods' => \WP_REST_Server::CREATABLE,
89 'args' => array(),
90 'callback' => array( $this, 'rest_add_to_group' ),
91 'permission_callback' => function( \WP_REST_Request $request ) {
92
93 if ( is_multisite() ) {
94 $data = $request->get_json_params();
95 $can = current_user_can_for_blog( $data['siteID'], 'manage_options' );
96 } else {
97 $can = current_user_can( 'manage_options' );
98 }
99
100 return $can;
101 },
102 )
103 );
104 }
105
106 /**
107 * Endpoint to allow adding to a group.
108 *
109 * @param \WP_REST_Request $request The original request.
110 *
111 * @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response
112 */
113 public function rest_add_to_group( \WP_REST_Request $request ) {
114
115 $data = $request->get_json_params();
116
117 wp_parse_str( wp_parse_url( $data['url'], PHP_URL_QUERY ), $query );
118
119 $return = array(
120 'success' => $this->plugin_groups->add_to_group( $data['id'], array( $query['plugin'] ) ),
121 );
122
123 return rest_ensure_response( $return );
124 }
125
126 /**
127 * Load a config for a specific site.
128 *
129 * @param \WP_REST_Request $request
130 *
131 * @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response
132 */
133 public function rest_load_config( \WP_REST_Request $request ) {
134
135 $id = $request->get_param( 'siteID' );
136 $json = $this->plugin_groups->build_config_object( $id );
137
138 return rest_ensure_response( json_decode( $json ) );
139 }
140
141 /**
142 * Save endpoint.
143 *
144 * @param \WP_REST_Request $request The request.
145 *
146 * @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response
147 */
148 public function rest_save_config( \WP_REST_Request $request ) {
149
150 $data = $request->get_json_params();
151 $site_id = get_current_blog_id();
152 if ( is_multisite() ) {
153 if ( ! empty( $data['siteID'] ) ) {
154 $site_id = $data['siteID'];
155 unset( $data['siteID'] );
156 }
157 if ( ! empty( $data['sitesEnabled'] ) ) {
158 // Ensure we have the same types.
159 $data['sitesEnabled'] = array_map( 'intval', $data['sitesEnabled'] );
160 }
161 }
162
163 $config = wp_parse_args( $data, $this->plugin_groups->load_config( $site_id ) );
164 $this->plugin_groups->set_config( $config, $site_id );
165 $success = $this->plugin_groups->save_config( $site_id );
166
167 return rest_ensure_response( array( 'success' => $success ) );
168 }
169 }
170