Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 76 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| REST_Settings_Controller | |
0.00% |
0 / 74 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
| register | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| register_routes | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
2 | |||
| get_settings | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| update_settings | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| settings_permissions_callback | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| get_item_schema | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Class used to register REST API settings endpoints used by Social Image Generator. |
| 4 | * |
| 5 | * Flagged to be removed after deprecation. |
| 6 | * |
| 7 | * @deprecated 0.38.3 |
| 8 | * |
| 9 | * @package automattic/jetpack-publicize |
| 10 | */ |
| 11 | |
| 12 | namespace Automattic\Jetpack\Publicize\Social_Image_Generator; |
| 13 | |
| 14 | use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings as Jetpack_Social_Settings; |
| 15 | use WP_Error; |
| 16 | use WP_REST_Controller; |
| 17 | use WP_REST_Request; |
| 18 | use WP_REST_Response; |
| 19 | use WP_REST_Server; |
| 20 | |
| 21 | if ( ! defined( 'ABSPATH' ) ) { |
| 22 | exit( 0 ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Defines our endpoints. |
| 27 | */ |
| 28 | class REST_Settings_Controller extends WP_REST_Controller { |
| 29 | /** |
| 30 | * Registers the REST routes on the `rest_api_init` hook. |
| 31 | * |
| 32 | * Instantiated here, rather than eagerly, so the controller class only loads |
| 33 | * on requests that reach `rest_api_init`. Static so the callback can be |
| 34 | * unregistered. |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public static function register() { |
| 39 | ( new self() )->register_routes(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Register REST API endpoints. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function register_routes() { |
| 48 | register_rest_route( |
| 49 | 'jetpack/v4', |
| 50 | '/social-image-generator/settings', |
| 51 | array( |
| 52 | array( |
| 53 | 'methods' => WP_REST_Server::READABLE, |
| 54 | 'callback' => array( $this, 'get_settings' ), |
| 55 | 'permission_callback' => array( $this, 'settings_permissions_callback' ), |
| 56 | ), |
| 57 | array( |
| 58 | 'methods' => WP_REST_Server::CREATABLE, |
| 59 | 'callback' => array( $this, 'update_settings' ), |
| 60 | 'permission_callback' => array( $this, 'settings_permissions_callback' ), |
| 61 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
| 62 | ), |
| 63 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 64 | ) |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * GET `/jetpack/v4/social-image-generator/settings` |
| 70 | * |
| 71 | * @return WP_REST_Response |
| 72 | */ |
| 73 | public function get_settings() { |
| 74 | $settings = ( new Jetpack_Social_Settings() )->get_settings(); |
| 75 | $response = array(); |
| 76 | $schema = $this->get_item_schema(); |
| 77 | $properties = array_keys( $schema['properties'] ); |
| 78 | |
| 79 | if ( in_array( 'enabled', $properties, true ) ) { |
| 80 | $response['enabled'] = $settings['socialImageGeneratorSettings']['enabled']; |
| 81 | } |
| 82 | |
| 83 | if ( in_array( 'default_image_id', $properties, true ) ) { |
| 84 | $response['default_image_id'] = $settings['socialImageGeneratorSettings']['default_image_id']; |
| 85 | } |
| 86 | |
| 87 | if ( in_array( 'defaults', $properties, true ) ) { |
| 88 | $response['defaults'] = array( 'template' => $settings['socialImageGeneratorSettings']['template'] ); |
| 89 | } |
| 90 | |
| 91 | return rest_ensure_response( $response ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * POST `/jetpack/v4/social-image-generator/settings` |
| 96 | * |
| 97 | * @param WP_REST_Request $request The API request. |
| 98 | * |
| 99 | * @return WP_REST_Response|WP_Error |
| 100 | */ |
| 101 | public function update_settings( $request ) { |
| 102 | $settings = new Jetpack_Social_Settings(); |
| 103 | |
| 104 | if ( isset( $request['enabled'] ) ) { |
| 105 | $settings->update_social_image_generator_settings( array( 'enabled' => $request['enabled'] ) ); |
| 106 | } |
| 107 | |
| 108 | if ( isset( $request['default_image_id'] ) ) { |
| 109 | $settings->update_social_image_generator_settings( array( 'default_image_id' => $request['default_image_id'] ) ); |
| 110 | } |
| 111 | |
| 112 | if ( $request['defaults'] && $request['defaults']['template'] ) { |
| 113 | $settings->update_social_image_generator_settings( array( 'template' => $request['defaults']['template'] ) ); |
| 114 | } |
| 115 | |
| 116 | return rest_ensure_response( $this->get_settings() ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Check the permissions for accessing and updating the settings endpoint. |
| 121 | * |
| 122 | * @return bool|WP_Error True if user can manage options. |
| 123 | */ |
| 124 | public function settings_permissions_callback() { |
| 125 | if ( ! current_user_can( 'manage_options' ) ) { |
| 126 | return new WP_Error( |
| 127 | 'rest_forbidden_context', |
| 128 | __( 'Sorry, you are not allowed to access this endpoint.', 'jetpack-publicize-pkg' ), |
| 129 | array( 'status' => rest_authorization_required_code() ) |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Retrieves the Social Image Generator's settings schema, conforming to JSON Schema. |
| 138 | * |
| 139 | * @return array Schema data. |
| 140 | */ |
| 141 | public function get_item_schema() { |
| 142 | $schema = array( |
| 143 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 144 | 'title' => 'social-image-generator-settings', |
| 145 | 'type' => 'object', |
| 146 | 'properties' => array( |
| 147 | 'enabled' => array( |
| 148 | 'description' => __( 'Whether or not Social Image Generator is enabled.', 'jetpack-publicize-pkg' ), |
| 149 | 'type' => 'boolean', |
| 150 | 'context' => array( 'view', 'edit' ), |
| 151 | ), |
| 152 | 'default_image_id' => array( |
| 153 | 'description' => __( 'The default image ID for the Social Image Generator.', 'jetpack-publicize-pkg' ), |
| 154 | 'type' => 'number', |
| 155 | 'context' => array( 'view', 'edit' ), |
| 156 | ), |
| 157 | 'defaults' => array( |
| 158 | 'description' => __( 'The default settings for a new generated image.', 'jetpack-publicize-pkg' ), |
| 159 | 'type' => 'object', |
| 160 | 'context' => array( 'view', 'edit' ), |
| 161 | 'properties' => array( |
| 162 | 'template' => array( |
| 163 | 'type' => 'string', |
| 164 | 'enum' => Templates::TEMPLATES, |
| 165 | ), |
| 166 | ), |
| 167 | ), |
| 168 | ), |
| 169 | ); |
| 170 | |
| 171 | return rest_default_additional_properties_to_false( $schema ); |
| 172 | } |
| 173 | } |