| @@ -7,9 +7,10 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ContentControl\RestAPI; |
| 10 | 10 | |
| 11 | -use WP_Rest_Controller, WP_REST_Response, WP_REST_Server, WP_Error; | |
| 11 | +use WP_REST_Controller, WP_REST_Response, WP_REST_Server, WP_Error; | |
| 12 | +use function ContentControl\plugin; | |
| 12 | 13 | use function ContentControl\get_block_types; |
| 13 | 14 | use function ContentControl\sanitize_block_type; |
| 14 | 15 | use function ContentControl\update_block_types; |
| 15 | 16 | |
| @@ -35,8 +36,10 @@ | ||
| 35 | 36 | protected $base = 'blockTypes'; |
| 36 | 37 | |
| 37 | 38 | /** |
| 38 | 39 | * Register API endpoint routes. |
| 40 | + * | |
| 41 | + * @return void | |
| 39 | 42 | */ |
| 40 | 43 | public function register_routes() { |
| 41 | 44 | register_rest_route( |
| 42 | 45 | $this->namespace, |
| @@ -50,9 +53,9 @@ | ||
| 50 | 53 | [ |
| 51 | 54 | 'methods' => WP_REST_Server::EDITABLE, |
| 52 | 55 | 'callback' => [ $this, 'update_block_types' ], |
| 53 | 56 | 'permission_callback' => [ $this, 'update_block_types_permissions' ], |
| 54 | - 'args' => $this->get_endpoint_args_for_item_schema( true ), | |
| 57 | + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), | |
| 55 | 58 | ], |
| 56 | 59 | 'schema' => [ $this, 'get_schema' ], |
| 57 | 60 | ] |
| 58 | 61 | ); |
| @@ -75,14 +78,20 @@ | ||
| 75 | 78 | |
| 76 | 79 | /** |
| 77 | 80 | * Update plugin settings. |
| 78 | 81 | * |
| 79 | - * @param WP_REST_Request $request Request object. | |
| 82 | + * @param \WP_REST_Request<array<string,mixed>> $request Request object. | |
| 80 | 83 | * |
| 81 | - * @return WP_Error|WP_REST_Response | |
| 84 | + * @return \WP_Error|\WP_REST_Response | |
| 82 | 85 | */ |
| 83 | 86 | public function update_block_types( $request ) { |
| 84 | 87 | // Get request json params. |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Block types. | |
| 91 | + * | |
| 92 | + * @var array<int,array<string,string|string[]>> $block_types | |
| 93 | + */ | |
| 85 | 94 | $block_types = $request->get_json_params(); |
| 86 | 95 | |
| 87 | 96 | $error_message = __( 'Something went wrong, the block types could not be updated.', 'content-control' ); |
| 88 | 97 | |
| @@ -89,14 +98,14 @@ | ||
| 89 | 98 | if ( ! is_array( get_block_types() ) ) { |
| 90 | 99 | return new WP_Error( '500', $error_message, [ 'status' => 500 ] ); |
| 91 | 100 | } |
| 92 | 101 | |
| 93 | - // Add or update incoming block types into the array. | |
| 94 | - foreach ( $block_types as $type ) { | |
| 102 | + foreach ( $block_types as $key => $type ) { | |
| 95 | 103 | // Sanitize each new block type. |
| 96 | - $block_types[ sanitize_key( $type['name'] ) ] = sanitize_block_type( $type ); | |
| 104 | + $block_types[ $key ] = sanitize_block_type( $type ); | |
| 97 | 105 | } |
| 98 | 106 | |
| 107 | + // Add or update incoming block types into the array. | |
| 99 | 108 | update_block_types( $block_types ); |
| 100 | 109 | |
| 101 | 110 | $new_block_types = get_block_types(); |
| 102 | 111 | |
| @@ -112,15 +121,17 @@ | ||
| 112 | 121 | * |
| 113 | 122 | * @return WP_Error|bool |
| 114 | 123 | */ |
| 115 | 124 | public function update_block_types_permissions() { |
| 116 | - return current_user_can( 'manage_options' ) || current_user_can( 'activate_plugins' ); | |
| 125 | + return current_user_can( plugin()->get_permission( 'manage_settings' ) ) | |
| 126 | + || current_user_can( 'manage_options' ) | |
| 127 | + || current_user_can( 'activate_plugins' ); | |
| 117 | 128 | } |
| 118 | 129 | |
| 119 | 130 | /** |
| 120 | 131 | * Get settings schema. |
| 121 | 132 | * |
| 122 | - * @return array | |
| 133 | + * @return array<string,mixed> | |
| 123 | 134 | */ |
| 124 | 135 | public function get_schema() { |
| 125 | 136 | if ( $this->schema ) { |
| 126 | 137 | // Bail early if already cached. |