PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 19.1 All 128 releases
wordpress-seo / admin / ajax.php

ajax.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.3, at admin/ajax.php

440 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Admin
6 */
7
8 if ( ! defined( 'WPSEO_VERSION' ) ) {
9 header( 'Status: 403 Forbidden' );
10 header( 'HTTP/1.1 403 Forbidden' );
11 exit();
12 }
13
14 /**
15 * Convenience function to JSON encode and echo results and then die.
16 *
17 * @param array $results Results array for encoding.
18 *
19 * @return void
20 */
21 function wpseo_ajax_json_echo_die( $results ) {
22 // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: WPSEO_Utils::format_json_encode is safe.
23 echo WPSEO_Utils::format_json_encode( $results );
24 exit();
25 }
26
27 /**
28 * Function used from AJAX calls, takes it variables from $_POST, dies on exit.
29 *
30 * @return void
31 */
32 function wpseo_set_option() {
33 if ( ! current_user_can( 'manage_options' ) ) {
34 exit( '-1' );
35 }
36
37 check_ajax_referer( 'wpseo-setoption' );
38
39 if ( ! isset( $_POST['option'] ) || ! is_string( $_POST['option'] ) ) {
40 exit( '-1' );
41 }
42
43 $option = sanitize_text_field( wp_unslash( $_POST['option'] ) );
44 if ( $option !== 'page_comments' ) {
45 exit( '-1' );
46 }
47
48 update_option( $option, 0 );
49 exit( '1' );
50 }
51
52 add_action( 'wp_ajax_wpseo_set_option', 'wpseo_set_option' );
53
54 /**
55 * Since 3.2 Notifications are dismissed in the Notification Center.
56 */
57 add_action( 'wp_ajax_yoast_dismiss_notification', [ 'Yoast_Notification_Center', 'ajax_dismiss_notification' ] );
58
59 /**
60 * Function used to remove the admin notices for several purposes, dies on exit.
61 *
62 * @return void
63 */
64 function wpseo_set_ignore() {
65 if ( ! current_user_can( 'manage_options' ) ) {
66 exit( '-1' );
67 }
68
69 check_ajax_referer( 'wpseo-ignore' );
70
71 if ( ! isset( $_POST['option'] ) || ! is_string( $_POST['option'] ) ) {
72 exit( '-1' );
73 }
74
75 $ignore_key = sanitize_text_field( wp_unslash( $_POST['option'] ) );
76 WPSEO_Options::set( 'ignore_' . $ignore_key, true );
77
78 exit( '1' );
79 }
80
81 add_action( 'wp_ajax_wpseo_set_ignore', 'wpseo_set_ignore' );
82
83 /**
84 * Save an individual SEO title from the Bulk Editor.
85 *
86 * @deprecated 28.1
87 * @codeCoverageIgnore
88 *
89 * @return void
90 */
91 function wpseo_save_title() {
92 _deprecated_function( __FUNCTION__, 'Yoast SEO 28.1' );
93 wpseo_save_what( 'title' );
94 }
95
96 add_action( 'wp_ajax_wpseo_save_title', 'wpseo_save_title' );
97
98 /**
99 * Save an individual meta description from the Bulk Editor.
100 *
101 * @deprecated 28.1
102 * @codeCoverageIgnore
103 *
104 * @return void
105 */
106 function wpseo_save_description() {
107 _deprecated_function( __FUNCTION__, 'Yoast SEO 28.1' );
108 wpseo_save_what( 'metadesc' );
109 }
110
111 add_action( 'wp_ajax_wpseo_save_metadesc', 'wpseo_save_description' );
112
113 /**
114 * Save titles & descriptions.
115 *
116 * @deprecated 28.1
117 * @codeCoverageIgnore
118 *
119 * @param string $what Type of item to save (title, description).
120 *
121 * @return void
122 */
123 function wpseo_save_what( $what ) {
124 check_ajax_referer( 'wpseo-bulk-editor' );
125
126 if ( ! isset( $_POST['new_value'], $_POST['wpseo_post_id'], $_POST['existing_value'] ) || ! is_string( $_POST['new_value'] ) || ! is_string( $_POST['existing_value'] ) ) {
127 exit( '-1' );
128 }
129
130 $new = sanitize_text_field( wp_unslash( $_POST['new_value'] ) );
131 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are casting the unsafe value to an integer.
132 $post_id = (int) wp_unslash( $_POST['wpseo_post_id'] );
133 $original = sanitize_text_field( wp_unslash( $_POST['existing_value'] ) );
134
135 if ( $post_id === 0 ) {
136 exit( '-1' );
137 }
138
139 $results = wpseo_upsert_new( $what, $post_id, $new, $original );
140
141 wpseo_ajax_json_echo_die( $results );
142 }
143
144 /**
145 * Helper function to update a post's meta data, returning relevant information
146 * about the information updated and the results or the meta update.
147 *
148 * @deprecated 28.1
149 * @codeCoverageIgnore
150 *
151 * @param int $post_id Post ID.
152 * @param string $new_meta_value New meta value to record.
153 * @param string $orig_meta_value Original meta value.
154 * @param string $meta_key Meta key string.
155 * @param string $return_key Return key string to use in results.
156 *
157 * @return array
158 */
159 function wpseo_upsert_meta( $post_id, $new_meta_value, $orig_meta_value, $meta_key, $return_key ) {
160
161 $post_id = (int) $post_id;
162 $sanitized_new_meta_value = wp_strip_all_tags( $new_meta_value );
163 $orig_meta_value = wp_strip_all_tags( $orig_meta_value );
164
165 $upsert_results = [
166 'status' => 'success',
167 'post_id' => $post_id,
168 "new_{$return_key}" => $sanitized_new_meta_value,
169 "original_{$return_key}" => $orig_meta_value,
170 ];
171
172 $the_post = get_post( $post_id );
173 if ( empty( $the_post ) ) {
174
175 $upsert_results['status'] = 'failure';
176 $upsert_results['results'] = __( 'Post doesn\'t exist.', 'wordpress-seo' );
177
178 return $upsert_results;
179 }
180
181 $post_type_object = get_post_type_object( $the_post->post_type );
182 if ( ! $post_type_object ) {
183
184 $upsert_results['status'] = 'failure';
185 $upsert_results['results'] = sprintf(
186 /* translators: %s expands to post type. */
187 __( 'Post has an invalid Content Type: %s.', 'wordpress-seo' ),
188 $the_post->post_type,
189 );
190
191 return $upsert_results;
192 }
193
194 if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) {
195
196 $upsert_results['status'] = 'failure';
197 $upsert_results['results'] = sprintf(
198 /* translators: %s expands to post type name. */
199 __( 'You can\'t edit %s.', 'wordpress-seo' ),
200 $post_type_object->label,
201 );
202
203 return $upsert_results;
204 }
205
206 if ( ! current_user_can( $post_type_object->cap->edit_others_posts ) && (int) $the_post->post_author !== get_current_user_id() ) {
207
208 $upsert_results['status'] = 'failure';
209 $upsert_results['results'] = sprintf(
210 /* translators: %s expands to the name of a post type (plural). */
211 __( 'You can\'t edit %s that aren\'t yours.', 'wordpress-seo' ),
212 $post_type_object->label,
213 );
214
215 return $upsert_results;
216 }
217
218 if ( $sanitized_new_meta_value === $orig_meta_value && $sanitized_new_meta_value !== $new_meta_value ) {
219 $upsert_results['status'] = 'failure';
220 $upsert_results['results'] = __( 'You have used HTML in your value which is not allowed.', 'wordpress-seo' );
221
222 return $upsert_results;
223 }
224
225 $res = update_post_meta( $post_id, $meta_key, $sanitized_new_meta_value );
226
227 $upsert_results['status'] = ( $res !== false ) ? 'success' : 'failure';
228 $upsert_results['results'] = $res;
229
230 return $upsert_results;
231 }
232
233 /**
234 * Save all titles sent from the Bulk Editor.
235 *
236 * @deprecated 28.1
237 * @codeCoverageIgnore
238 *
239 * @return void
240 */
241 function wpseo_save_all_titles() {
242 _deprecated_function( __FUNCTION__, 'Yoast SEO 28.1' );
243 wpseo_save_all( 'title' );
244 }
245
246 add_action( 'wp_ajax_wpseo_save_all_titles', 'wpseo_save_all_titles' );
247
248 /**
249 * Save all description sent from the Bulk Editor.
250 *
251 * @deprecated 28.1
252 * @codeCoverageIgnore
253 *
254 * @return void
255 */
256 function wpseo_save_all_descriptions() {
257 _deprecated_function( __FUNCTION__, 'Yoast SEO 28.1' );
258 wpseo_save_all( 'metadesc' );
259 }
260
261 add_action( 'wp_ajax_wpseo_save_all_descriptions', 'wpseo_save_all_descriptions' );
262
263 /**
264 * Utility function to save values.
265 *
266 * @deprecated 28.1
267 * @codeCoverageIgnore
268 *
269 * @param string $what Type of item so save.
270 *
271 * @return void
272 */
273 function wpseo_save_all( $what ) {
274 check_ajax_referer( 'wpseo-bulk-editor' );
275
276 $results = [];
277 if ( ! isset( $_POST['items'], $_POST['existingItems'] ) ) {
278 wpseo_ajax_json_echo_die( $results );
279 }
280
281 $new_values = array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], wp_unslash( (array) $_POST['items'] ) );
282 $original_values = array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], wp_unslash( (array) $_POST['existingItems'] ) );
283
284 foreach ( $new_values as $post_id => $new_value ) {
285 $original_value = $original_values[ $post_id ];
286 $results[] = wpseo_upsert_new( $what, $post_id, $new_value, $original_value );
287 }
288
289 wpseo_ajax_json_echo_die( $results );
290 }
291
292 /**
293 * Insert a new value.
294 *
295 * @deprecated 28.1
296 * @codeCoverageIgnore
297 *
298 * @param string $what Item type (such as title).
299 * @param int $post_id Post ID.
300 * @param string $new_value New value to record.
301 * @param string $original Original value.
302 *
303 * @return string
304 */
305 function wpseo_upsert_new( $what, $post_id, $new_value, $original ) {
306 $meta_key = WPSEO_Meta::$meta_prefix . $what;
307
308 return wpseo_upsert_meta( $post_id, $new_value, $original, $meta_key, $what );
309 }
310
311 /**
312 * Retrieves the post ids where the keyword is used before as well as the types of those posts.
313 *
314 * @return void
315 */
316 function ajax_get_keyword_usage_and_post_types() {
317 check_ajax_referer( 'wpseo-keyword-usage-and-post-types', 'nonce' );
318
319 if ( ! isset( $_POST['post_id'], $_POST['keyword'] ) || ! is_string( $_POST['keyword'] ) ) {
320 exit( '-1' );
321 }
322
323 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We are casting to an integer.
324 $post_id = (int) wp_unslash( $_POST['post_id'] );
325
326 if ( $post_id === 0 || ! current_user_can( 'edit_post', $post_id ) ) {
327 exit( '-1' );
328 }
329
330 $keyword = sanitize_text_field( wp_unslash( $_POST['keyword'] ) );
331
332 $post_ids = WPSEO_Meta::keyword_usage( $keyword, $post_id );
333
334 $return_object = [
335 'keyword_usage' => $post_ids,
336 'post_types' => WPSEO_Meta::post_types_for_ids( $post_ids ),
337 ];
338
339 wp_die(
340 // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: WPSEO_Utils::format_json_encode is safe.
341 WPSEO_Utils::format_json_encode( $return_object ),
342 );
343 }
344
345 add_action( 'wp_ajax_get_focus_keyword_usage_and_post_types', 'ajax_get_keyword_usage_and_post_types' );
346
347 /**
348 * Retrieves the keyword for the keyword doubles of the termpages.
349 *
350 * @return void
351 */
352 function ajax_get_term_keyword_usage() {
353 check_ajax_referer( 'wpseo-keyword-usage', 'nonce' );
354
355 if ( ! isset( $_POST['post_id'], $_POST['keyword'], $_POST['taxonomy'] ) || ! is_string( $_POST['keyword'] ) || ! is_string( $_POST['taxonomy'] ) ) {
356 wp_die( -1 );
357 }
358
359 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are casting the unsafe input to an integer.
360 $post_id = (int) wp_unslash( $_POST['post_id'] );
361
362 if ( $post_id === 0 ) {
363 wp_die( -1 );
364 }
365
366 $keyword = sanitize_text_field( wp_unslash( $_POST['keyword'] ) );
367 $taxonomy_name = sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) );
368
369 $taxonomy = get_taxonomy( $taxonomy_name );
370
371 if ( ! $taxonomy ) {
372 wp_die( 0 );
373 }
374
375 if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) {
376 wp_die( -1 );
377 }
378
379 $usage = WPSEO_Taxonomy_Meta::get_keyword_usage( $keyword, $post_id, $taxonomy_name );
380
381 // Normalize the result so it is the same as the post keyword usage AJAX request.
382 $usage = $usage[ $keyword ];
383
384 wp_die(
385 // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: WPSEO_Utils::format_json_encode is safe.
386 WPSEO_Utils::format_json_encode( $usage ),
387 );
388 }
389
390 add_action( 'wp_ajax_get_term_keyword_usage', 'ajax_get_term_keyword_usage' );
391
392 /**
393 * Registers hooks for all AJAX integrations.
394 *
395 * @return void
396 */
397 function wpseo_register_ajax_integrations() {
398 $integrations = [ new Yoast_Network_Admin() ];
399
400 foreach ( $integrations as $integration ) {
401 $integration->register_ajax_hooks();
402 }
403 }
404
405 wpseo_register_ajax_integrations();
406
407 new WPSEO_Shortcode_Filter();
408
409 new WPSEO_Taxonomy_Columns();
410
411 /* ********************* DEPRECATED FUNCTIONS ********************* */
412
413 /**
414 * Retrieves the keyword for the keyword doubles.
415 *
416 * @return void
417 */
418 function ajax_get_keyword_usage() {
419 _deprecated_function( __METHOD__, 'WPSEO 20.4' );
420 check_ajax_referer( 'wpseo-keyword-usage', 'nonce' );
421
422 if ( ! isset( $_POST['post_id'], $_POST['keyword'] ) || ! is_string( $_POST['keyword'] ) ) {
423 exit( '-1' );
424 }
425
426 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We are casting to an integer.
427 $post_id = (int) wp_unslash( $_POST['post_id'] );
428
429 if ( $post_id === 0 || ! current_user_can( 'edit_post', $post_id ) ) {
430 exit( '-1' );
431 }
432
433 $keyword = sanitize_text_field( wp_unslash( $_POST['keyword'] ) );
434
435 wp_die(
436 // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: WPSEO_Utils::format_json_encode is safe.
437 WPSEO_Utils::format_json_encode( WPSEO_Meta::keyword_usage( $keyword, $post_id ) ),
438 );
439 }
440