| 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 = $this->plugin_groups->can_manage_site_config( $data['siteID'] ?? get_current_blog_id() ); |
| 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 $this->plugin_groups->can_manage_site_config( $id ); |
| 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 = $this->plugin_groups->can_manage_site_config( $data['siteID'] ?? get_current_blog_id() ); |
| 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 = (int) $data['siteID']; |
| 155 |
unset( $data['siteID'] ); |
| 156 |
} |
| 157 |
if ( isset( $data['sitesEnabled'] ) ) { |
| 158 |
// Ensure we have the same types. |
| 159 |
$data['sitesEnabled'] = array_map( 'intval', $data['sitesEnabled'] ); |
| 160 |
$main_site_id = get_main_site_id(); |
| 161 |
$this->plugin_groups->save_sites_enabled( $data['sitesEnabled'] ); |
| 162 |
|
| 163 |
if ( $main_site_id !== $site_id ) { |
| 164 |
unset( $data['sitesEnabled'] ); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
$config = wp_parse_args( $data, $this->plugin_groups->load_config( $site_id ) ); |
| 170 |
$this->plugin_groups->set_config( $config, $site_id ); |
| 171 |
$success = $this->plugin_groups->save_config( $site_id ); |
| 172 |
|
| 173 |
return rest_ensure_response( array( 'success' => $success ) ); |
| 174 |
} |
| 175 |
} |
| 176 |
|