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broken-link-checker / app / rest-endpoints / avatars / class-controller.php
broken-link-checker / app / rest-endpoints / avatars Last commit date
class-controller.php 3 years ago
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