class-controller.php
179 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Rest endpoint fetching Avatars. |
| 4 | * |
| 5 | * @link https://wordpress.org/plugins/broken-link-checker/ |
| 6 | * @since 2.0.0 |
| 7 | * |
| 8 | * @author WPMUDEV (https://wpmudev.com) |
| 9 | * @package WPMUDEV_BLC\App\Rest_Endpoints\Avatars |
| 10 | * |
| 11 | * @copyright (c) 2022, Incsub (http://incsub.com) |
| 12 | */ |
| 13 | |
| 14 | namespace WPMUDEV_BLC\App\Rest_Endpoints\Avatars; |
| 15 | |
| 16 | // Abort if called directly. |
| 17 | defined( 'WPINC' ) || die; |
| 18 | |
| 19 | use WP_Error; |
| 20 | use WP_REST_Request; |
| 21 | use WP_REST_Server; |
| 22 | use WPMUDEV_BLC\Core\Controllers\Rest_Api; |
| 23 | use WPMUDEV_BLC\App\Options\Settings\Model as Settings; |
| 24 | |
| 25 | /** |
| 26 | * Class Controller |
| 27 | * |
| 28 | * @package WPMUDEV_BLC\App\Rest_Endpoints\Avatars |
| 29 | */ |
| 30 | class Controller extends Rest_Api { |
| 31 | /** |
| 32 | * Settings keys. |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | protected $settings_keys = array(); |
| 37 | |
| 38 | public function init() { |
| 39 | $this->settings_keys = array_map( |
| 40 | function ( $settings_key ) { |
| 41 | return sanitize_key( $settings_key ); |
| 42 | }, |
| 43 | array_keys( Settings::instance()->default ) |
| 44 | ); |
| 45 | |
| 46 | $this->namespace = "wpmudev_blc/{$this->version}"; |
| 47 | $this->rest_base = 'avatars'; |
| 48 | |
| 49 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Register the routes for the objects of the controller. |
| 54 | * |
| 55 | * @since 2.0.0 |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function register_routes() { |
| 60 | register_rest_route( |
| 61 | $this->namespace, |
| 62 | '/' . $this->rest_base, |
| 63 | array( |
| 64 | array( |
| 65 | 'methods' => WP_REST_Server::EDITABLE, |
| 66 | 'callback' => array( $this, 'get_avatar' ), |
| 67 | 'permission_callback' => array( $this, 'get_avatar_permissions' ), |
| 68 | ), |
| 69 | 'schema' => array( $this, 'get_item_schema' ), |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns avatar. |
| 76 | * |
| 77 | * @since 2.0.0 |
| 78 | * |
| 79 | * @param object $request WP_REST_Request get data from request. |
| 80 | * |
| 81 | * @return mixed WP_REST_Response|WP_Error|WP_HTTP_Response|mixed $response |
| 82 | */ |
| 83 | public function get_avatar( $request ) { |
| 84 | $email = $request->get_param( 'email' ); |
| 85 | |
| 86 | $response_data = array( |
| 87 | 'message' => __( 'Avatar url', 'broken-link-checker' ), |
| 88 | 'status_code' => 200, |
| 89 | ); |
| 90 | |
| 91 | if ( ! is_email( $email ) ) { |
| 92 | $response_data['message'] = __( 'Invalid email address', 'broken-link-checker' ); |
| 93 | $response_data['status_code'] = 500; |
| 94 | } else { |
| 95 | $avatar = get_avatar_url( $email, array( 'size' => 24 ) ); |
| 96 | $response_data['avatar'] = $avatar; |
| 97 | } |
| 98 | |
| 99 | $response = $this->prepare_item_for_response( $response_data, $request ); |
| 100 | |
| 101 | return rest_ensure_response( $response ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Check permissions for fetching avatar. |
| 107 | * |
| 108 | * @since 2.0.0 |
| 109 | * |
| 110 | * @param object $request get data from request. |
| 111 | * |
| 112 | * @return bool|object Boolean or WP_Error. |
| 113 | */ |
| 114 | public function get_avatar_permissions( WP_REST_Request $request ) { |
| 115 | if ( ! current_user_can( 'manage_options' ) ) { |
| 116 | return new WP_Error( |
| 117 | 'rest_forbidden', |
| 118 | esc_html__( 'You can not fetch avatars.', 'broken-link-checker' ), |
| 119 | array( 'status' => $this->authorization_status_code() ) |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Retrieves the item's schema, conforming to JSON Schema. |
| 128 | * |
| 129 | * @since 2.0.0 |
| 130 | * |
| 131 | * @return array Item schema data. |
| 132 | */ |
| 133 | public function get_item_schema() { |
| 134 | if ( $this->schema ) { |
| 135 | return $this->add_additional_fields_schema( $this->schema ); |
| 136 | } |
| 137 | |
| 138 | $this->schema = array( |
| 139 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 140 | 'title' => isset( $args['rest_base'] ) ? $args['rest_base'] : '', |
| 141 | 'type' => 'object', |
| 142 | 'properties' => array(), |
| 143 | ); |
| 144 | |
| 145 | $this->schema['properties'] = array( |
| 146 | 'avatar' => array( |
| 147 | 'description' => esc_html__( 'Avatar by email.', 'broken-link-checker' ), |
| 148 | 'type' => 'string', |
| 149 | ), |
| 150 | |
| 151 | 'confirmed' => array( |
| 152 | 'description' => esc_html__( 'Auto-confirmed when email belongs to user.', 'broken-link-checker' ), |
| 153 | 'type' => 'boolean', |
| 154 | ), |
| 155 | |
| 156 | 'message' => array( |
| 157 | 'description' => esc_html__( 'Response message.', 'broken-link-checker' ), |
| 158 | 'type' => 'string', |
| 159 | ), |
| 160 | |
| 161 | 'status_code' => array( |
| 162 | 'description' => esc_html__( 'Response status code.', 'broken-link-checker' ), |
| 163 | 'type' => 'string', |
| 164 | 'context' => array( 'view', 'edit' ), |
| 165 | 'enum' => array( |
| 166 | '200', |
| 167 | '400', |
| 168 | '401', |
| 169 | '403', |
| 170 | ), |
| 171 | 'readonly' => true, |
| 172 | ), |
| 173 | ); |
| 174 | |
| 175 | return $this->add_additional_fields_schema( $this->schema ); |
| 176 | } |
| 177 | |
| 178 | } |
| 179 |