| 1 |
<?php |
| 2 |
/** |
| 3 |
* RestAPI Global Settings Endpoint. |
| 4 |
* |
| 5 |
* @copyright (c) 2021, Code Atlantic LLC. |
| 6 |
* @package ContentControl |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ContentControl\RestAPI; |
| 10 |
|
| 11 |
use WP_REST_Controller, WP_REST_Response, WP_REST_Server, WP_Error; |
| 12 |
use function ContentControl\plugin; |
| 13 |
use function ContentControl\get_block_types; |
| 14 |
use function ContentControl\sanitize_block_type; |
| 15 |
use function ContentControl\update_block_types; |
| 16 |
|
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
/** |
| 20 |
* Rest API Settings Controller Class. |
| 21 |
*/ |
| 22 |
class BlockTypes extends WP_REST_Controller { |
| 23 |
|
| 24 |
/** |
| 25 |
* Endpoint namespace. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $namespace = 'content-control/v2'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Route base. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected $base = 'blockTypes'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Register API endpoint routes. |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function register_routes() { |
| 44 |
register_rest_route( |
| 45 |
$this->namespace, |
| 46 |
'/' . $this->base, |
| 47 |
[ |
| 48 |
[ |
| 49 |
'methods' => WP_REST_Server::READABLE, |
| 50 |
'callback' => [ $this, 'get_block_types' ], |
| 51 |
'permission_callback' => '__return_true', // Read only, so anyone can view. |
| 52 |
], |
| 53 |
[ |
| 54 |
'methods' => WP_REST_Server::EDITABLE, |
| 55 |
'callback' => [ $this, 'update_block_types' ], |
| 56 |
'permission_callback' => [ $this, 'update_block_types_permissions' ], |
| 57 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 58 |
], |
| 59 |
'schema' => [ $this, 'get_schema' ], |
| 60 |
] |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get block type list. |
| 66 |
* |
| 67 |
* @return WP_Error|WP_REST_Response |
| 68 |
*/ |
| 69 |
public function get_block_types() { |
| 70 |
$block_types = get_block_types(); |
| 71 |
|
| 72 |
if ( $block_types ) { |
| 73 |
return new WP_REST_Response( $block_types, 200 ); |
| 74 |
} else { |
| 75 |
return new WP_Error( '404', __( 'Something went wrong, the block types could not be found.', 'content-control' ), [ 'status' => 404 ] ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Update plugin settings. |
| 81 |
* |
| 82 |
* @param \WP_REST_Request<array<string,mixed>> $request Request object. |
| 83 |
* |
| 84 |
* @return \WP_Error|\WP_REST_Response |
| 85 |
*/ |
| 86 |
public function update_block_types( $request ) { |
| 87 |
// Get request json params. |
| 88 |
|
| 89 |
/** |
| 90 |
* Block types. |
| 91 |
* |
| 92 |
* @var array<int,array<string,string|string[]>> $block_types |
| 93 |
*/ |
| 94 |
$block_types = $request->get_json_params(); |
| 95 |
|
| 96 |
$error_message = __( 'Something went wrong, the block types could not be updated.', 'content-control' ); |
| 97 |
|
| 98 |
// Following line is sanity check to ensure get_block_types() returns an array to prevent runtime errors. |
| 99 |
// @phpstan-ignore-next-line . |
| 100 |
if ( ! is_array( get_block_types() ) ) { |
| 101 |
return new WP_Error( '500', $error_message, [ 'status' => 500 ] ); |
| 102 |
} |
| 103 |
|
| 104 |
foreach ( $block_types as $key => $type ) { |
| 105 |
// Sanitize each new block type. |
| 106 |
$block_types[ $key ] = sanitize_block_type( $type ); |
| 107 |
} |
| 108 |
|
| 109 |
// Add or update incoming block types into the array. |
| 110 |
update_block_types( $block_types ); |
| 111 |
|
| 112 |
$new_block_types = get_block_types(); |
| 113 |
|
| 114 |
if ( $new_block_types ) { |
| 115 |
return new WP_REST_Response( $new_block_types, 200 ); |
| 116 |
} else { |
| 117 |
return new WP_Error( '404', $error_message, [ 'status' => 404 ] ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Check update settings permissions. |
| 123 |
* |
| 124 |
* @return WP_Error|bool |
| 125 |
*/ |
| 126 |
public function update_block_types_permissions() { |
| 127 |
return current_user_can( plugin()->get_permission( 'manage_settings' ) ) |
| 128 |
|| current_user_can( 'manage_options' ) |
| 129 |
|| current_user_can( 'activate_plugins' ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get settings schema. |
| 134 |
* |
| 135 |
* @return array<string,mixed> |
| 136 |
*/ |
| 137 |
public function get_schema() { |
| 138 |
if ( $this->schema ) { |
| 139 |
// Bail early if already cached. |
| 140 |
return $this->schema; |
| 141 |
} |
| 142 |
|
| 143 |
$this->schema = apply_filters( |
| 144 |
'content_control_rest_settings_schema', |
| 145 |
[ |
| 146 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 147 |
'title' => 'settings', |
| 148 |
'type' => 'object', |
| 149 |
'properties' => [ |
| 150 |
'block_controls' => [ |
| 151 |
'type' => 'object', |
| 152 |
'properties' => [ |
| 153 |
'enable' => [ |
| 154 |
'type' => 'boolean', |
| 155 |
], |
| 156 |
'controls' => [ |
| 157 |
'type' => 'object', |
| 158 |
'properties' => [ |
| 159 |
'device_rules' => [ |
| 160 |
'type' => 'object', |
| 161 |
'properties' => [ |
| 162 |
'enable' => [ |
| 163 |
'type' => 'boolean', |
| 164 |
], |
| 165 |
], |
| 166 |
], |
| 167 |
], |
| 168 |
], |
| 169 |
'disabled_blocks' => [ |
| 170 |
'type' => 'array', |
| 171 |
'items' => [ |
| 172 |
'type' => 'string', |
| 173 |
], |
| 174 |
], |
| 175 |
], |
| 176 |
], |
| 177 |
], |
| 178 |
] |
| 179 |
); |
| 180 |
|
| 181 |
return $this->schema; |
| 182 |
} |
| 183 |
} |
| 184 |
|