Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/seo-by-rank-math/includes/rest/class-front.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* The Global functionality of the plugin.
4
+
*
5
+
* Defines the functionality loaded both on frontend.
6
+
*
7
+
* @since 1.0.15
8
+
* @package RankMath
9
+
* @subpackage RankMath\Rest
10
+
* @author Rank Math <support@rankmath.com>
11
+
*/
12
+
13
+
namespace RankMath\Rest;
14
+
15
+
use WP_Error;
16
+
use WP_REST_Server;
17
+
use WP_REST_Request;
18
+
use WP_REST_Response;
19
+
use WP_REST_Controller;
20
+
use RankMath\Admin\Admin_Helper;
21
+
22
+
defined( 'ABSPATH' ) || exit;
23
+
24
+
/**
25
+
* Front class.
26
+
*/
27
+
class Front extends WP_REST_Controller {
28
+
29
+
/**
30
+
* Constructor.
31
+
*/
32
+
public function __construct() {
33
+
$this->namespace = \RankMath\Rest\Rest_Helper::BASE;
34
+
}
35
+
36
+
/**
37
+
* Registers the routes for the objects of the controller.
38
+
*/
39
+
public function register_routes() {
40
+
41
+
register_rest_route(
42
+
$this->namespace,
43
+
'/disconnectSite',
44
+
[
45
+
'methods' => WP_REST_Server::READABLE,
46
+
'callback' => [ $this, 'disconnect_site' ],
47
+
'permission_callback' => [ $this, 'check_api_key' ],
48
+
'args' => $this->get_disconnect_site_args(),
49
+
]
50
+
);
51
+
52
+
register_rest_route(
53
+
$this->namespace,
54
+
'/getFeaturedImageId',
55
+
[
56
+
'methods' => WP_REST_Server::EDITABLE,
57
+
'callback' => [ $this, 'get_featured_image_id' ],
58
+
'permission_callback' => function () {
59
+
return \RankMath\Helper::has_cap( 'onpage_general' );
60
+
},
61
+
'args' => $this->get_featured_image_id_args(),
62
+
]
63
+
);
64
+
}
65
+
66
+
/**
67
+
* Check API key in request.
68
+
*
69
+
* @param WP_REST_Request $request Full details about the request.
70
+
* @return bool Whether the API key matches or not.
71
+
*/
72
+
public function check_api_key( WP_REST_Request $request ) {
73
+
$token = $request->get_param( 'token' );
74
+
$data = Admin_Helper::get_registration_data();
75
+
76
+
return isset( $data['api_key'] ) && $token === $data['api_key'];
77
+
}
78
+
79
+
/**
80
+
* Disconnect website.
81
+
*
82
+
* @param WP_REST_Request $request Full details about the request.
83
+
*
84
+
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
85
+
*/
86
+
public function disconnect_site( WP_REST_Request $request ) {
87
+
Admin_Helper::get_registration_data( false );
88
+
89
+
return [
90
+
'code' => 'site_disconnected',
91
+
'message' => esc_html__( 'Site disconnected successfully.', 'rank-math' ),
92
+
];
93
+
}
94
+
95
+
/**
96
+
* Get featured image ID.
97
+
*
98
+
* @param WP_REST_Request $request Should include a postId parameter.
99
+
*
100
+
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
101
+
*/
102
+
public function get_featured_image_id( WP_REST_Request $request ) {
103
+
104
+
$resp = new WP_REST_Response();
105
+
106
+
if ( ! current_theme_supports( 'post-thumbnails' ) ) {
107
+
$resp->set_status( 200 );
108
+
$resp->set_data(
109
+
[
110
+
'success' => false,
111
+
'message' => esc_html__( 'The current theme does not have "post-thumbnails" support.', 'rank-math' ),
112
+
'featImgId' => 0,
113
+
]
114
+
);
115
+
return $resp;
116
+
}
117
+
118
+
$post_id = $request->get_param( 'postId' );
119
+
// Checks whether the current has permission to edit post.
120
+
$post_type_obj = get_post_type_object( get_post_type( $post_id ) );
121
+
if (
122
+
is_null( $post_type_obj ) ||
123
+
(
124
+
! current_user_can( $post_type_obj->cap->edit_post, $post_id ) &&
125
+
! current_user_can( $post_type_obj->cap->edit_others_posts )
126
+
)
127
+
) {
128
+
$resp->set_status( 401 );
129
+
$resp->set_data(
130
+
[
131
+
'success' => false,
132
+
'message' => esc_html__( 'Sorry, you don\'t have the required permissions to access this page.', 'rank-math' ),
133
+
'featImgId' => false,
134
+
]
135
+
);
136
+
return $resp->as_error();
137
+
}
138
+
139
+
$feat_img_id = get_post_thumbnail_id( $post_id ? $post_id : null );
140
+
if ( false === $feat_img_id ) {
141
+
$resp->set_status( 404 );
142
+
$resp->set_data(
143
+
[
144
+
'success' => false,
145
+
'message' => esc_html__( 'The post could not be found.', 'rank-math' ),
146
+
'featImgId' => false,
147
+
]
148
+
);
149
+
return $resp->as_error();
150
+
}
151
+
152
+
$resp->set_status( 200 );
153
+
$resp->set_data(
154
+
[
155
+
'success' => true,
156
+
'featImgId' => $feat_img_id,
157
+
]
158
+
);
159
+
return $resp;
160
+
}
161
+
162
+
/**
163
+
* Get featured image ID endpoint arguments.
164
+
*
165
+
* @return array
166
+
*/
167
+
private function get_featured_image_id_args() {
168
+
return [
169
+
'postId' => [
170
+
'type' => 'integer',
171
+
'required' => true,
172
+
'description' => esc_html__( 'Post ID', 'rank-math' ),
173
+
'validate_callback' => [ '\\RankMath\\Rest\\Rest_Helper', 'is_param_empty' ],
174
+
],
175
+
];
176
+
}
177
+
178
+
/**
179
+
* Get disconnect site endpoint arguments.
180
+
*
181
+
* @return array
182
+
*/
183
+
private function get_disconnect_site_args() {
184
+
return [
185
+
'token' => [
186
+
'type' => 'string',
187
+
'required' => true,
188
+
'description' => esc_html__( 'Site token', 'rank-math' ),
189
+
'validate_callback' => [ '\\RankMath\\Rest\\Rest_Helper', 'is_param_empty' ],
190
+
],
191
+
];
192
+
}
193
+
}
194
+