1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
<?php /** * WPSEO plugin file. * * @package WPSEO\Endpoint */
/** * Represents an implementation of the WPSEO_Endpoint interface to register one or multiple endpoints. */ class WPSEO_Endpoint_MyYoast_Connect implements WPSEO_Endpoint {
/** * The namespace to use. * * @var string */ const REST_NAMESPACE = 'yoast/v1/myyoast';
/** * Registers the REST routes that are available on the endpoint. * * @codeCoverageIgnore * * @return void */ public function register() { register_rest_route( self::REST_NAMESPACE, 'connect', array( 'methods' => 'POST', 'callback' => array( $this, 'handle_request' ), 'permission_callback' => array( $this, 'can_retrieve_data' ), ) ); }
/** * Determines whether or not data can be retrieved for the registered endpoints. * * @param WP_REST_Request $request The current request. * * @return WP_REST_Response The response. */ public function handle_request( WP_REST_Request $request ) { if ( $request->get_param( 'url' ) !== $this->get_home_url() ) { return new WP_REST_Response( 'Bad request: URL mismatch.', 403 ); }
if ( $request->get_param( 'clientId' ) !== $this->get_client_id() ) { return new WP_REST_Response( 'Bad request: ClientID mismatch.', 403 ); }
$client_secret = $request->get_param( 'clientSecret' ); if ( empty( $client_secret ) ) { return new WP_REST_Response( 'Bad request: ClientSecret missing.', 403 ); }
$this->save_secret( $client_secret );
return new WP_REST_Response( 'Connection successful established.' ); }
/** * Determines whether or not data can be retrieved for the registered endpoints. * * @return bool Whether or not data can be retrieved. */ public function can_retrieve_data() { return true; }
/** * Saves the client secret. * * @codeCoverageIgnore * * @param string $client_secret The secret to save. * * @return void */ protected function save_secret( $client_secret ) { $this->get_client()->save_configuration( array( 'secret' => $client_secret, ) ); }
/** * Retrieves the current client ID. * * @codeCoverageIgnore * * @return array The client ID. */ protected function get_client_id() { $config = $this->get_client()->get_configuration();
return $config['clientId']; }
/** * Retrieves an instance of the client. * * @codeCoverageIgnore * * @return WPSEO_MyYoast_Client Instance of client. */ protected function get_client() { static $client;
if ( ! $client ) { $client = new WPSEO_MyYoast_Client(); }
return $client; }
/** * Wraps the method for retrieving the home URL. * * @codeCoverageIgnore * * @return string Home URL. */ protected function get_home_url() { return WPSEO_Utils::get_home_url(); } }
|