ajax.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. if ( ! defined( 'WPSEO_VERSION' ) ) {
  8. header( 'Status: 403 Forbidden' );
  9. header( 'HTTP/1.1 403 Forbidden' );
  10. exit();
  11. }
  12. /**
  13. * Convenience function to JSON encode and echo results and then die.
  14. *
  15. * @param array $results Results array for encoding.
  16. */
  17. function wpseo_ajax_json_echo_die( $results ) {
  18. // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: WPSEO_Utils::format_json_encode is safe.
  19. echo WPSEO_Utils::format_json_encode( $results );
  20. die();
  21. }
  22. /**
  23. * Function used from AJAX calls, takes it variables from $_POST, dies on exit.
  24. */
  25. function wpseo_set_option() {
  26. if ( ! current_user_can( 'manage_options' ) ) {
  27. die( '-1' );
  28. }
  29. check_ajax_referer( 'wpseo-setoption' );
  30. $option = sanitize_text_field( filter_input( INPUT_POST, 'option' ) );
  31. if ( $option !== 'page_comments' ) {
  32. die( '-1' );
  33. }
  34. update_option( $option, 0 );
  35. die( '1' );
  36. }
  37. add_action( 'wp_ajax_wpseo_set_option', 'wpseo_set_option' );
  38. /**
  39. * Since 3.2 Notifications are dismissed in the Notification Center.
  40. */
  41. add_action( 'wp_ajax_yoast_dismiss_notification', [ 'Yoast_Notification_Center', 'ajax_dismiss_notification' ] );
  42. /**
  43. * Function used to remove the admin notices for several purposes, dies on exit.
  44. */
  45. function wpseo_set_ignore() {
  46. if ( ! current_user_can( 'manage_options' ) ) {
  47. die( '-1' );
  48. }
  49. check_ajax_referer( 'wpseo-ignore' );
  50. $ignore_key = sanitize_text_field( filter_input( INPUT_POST, 'option' ) );
  51. WPSEO_Options::set( 'ignore_' . $ignore_key, true );
  52. die( '1' );
  53. }
  54. add_action( 'wp_ajax_wpseo_set_ignore', 'wpseo_set_ignore' );
  55. /**
  56. * Hides the default tagline notice for a specific user.
  57. */
  58. function wpseo_dismiss_tagline_notice() {
  59. if ( ! current_user_can( 'manage_options' ) ) {
  60. die( '-1' );
  61. }
  62. check_ajax_referer( 'wpseo-dismiss-tagline-notice' );
  63. update_user_meta( get_current_user_id(), 'wpseo_seen_tagline_notice', 'seen' );
  64. die( '1' );
  65. }
  66. add_action( 'wp_ajax_wpseo_dismiss_tagline_notice', 'wpseo_dismiss_tagline_notice' );
  67. /**
  68. * Save an individual SEO title from the Bulk Editor.
  69. */
  70. function wpseo_save_title() {
  71. wpseo_save_what( 'title' );
  72. }
  73. add_action( 'wp_ajax_wpseo_save_title', 'wpseo_save_title' );
  74. /**
  75. * Save an individual meta description from the Bulk Editor.
  76. */
  77. function wpseo_save_description() {
  78. wpseo_save_what( 'metadesc' );
  79. }
  80. add_action( 'wp_ajax_wpseo_save_metadesc', 'wpseo_save_description' );
  81. /**
  82. * Save titles & descriptions.
  83. *
  84. * @param string $what Type of item to save (title, description).
  85. */
  86. function wpseo_save_what( $what ) {
  87. check_ajax_referer( 'wpseo-bulk-editor' );
  88. $new = filter_input( INPUT_POST, 'new_value' );
  89. $post_id = intval( filter_input( INPUT_POST, 'wpseo_post_id' ) );
  90. $original = filter_input( INPUT_POST, 'existing_value' );
  91. $results = wpseo_upsert_new( $what, $post_id, $new, $original );
  92. wpseo_ajax_json_echo_die( $results );
  93. }
  94. /**
  95. * Helper function to update a post's meta data, returning relevant information
  96. * about the information updated and the results or the meta update.
  97. *
  98. * @param int $post_id Post ID.
  99. * @param string $new_meta_value New meta value to record.
  100. * @param string $orig_meta_value Original meta value.
  101. * @param string $meta_key Meta key string.
  102. * @param string $return_key Return key string to use in results.
  103. *
  104. * @return string
  105. */
  106. function wpseo_upsert_meta( $post_id, $new_meta_value, $orig_meta_value, $meta_key, $return_key ) {
  107. $post_id = intval( $post_id );
  108. $sanitized_new_meta_value = wp_strip_all_tags( $new_meta_value );
  109. $orig_meta_value = wp_strip_all_tags( $orig_meta_value );
  110. $upsert_results = [
  111. 'status' => 'success',
  112. 'post_id' => $post_id,
  113. "new_{$return_key}" => $sanitized_new_meta_value,
  114. "original_{$return_key}" => $orig_meta_value,
  115. ];
  116. $the_post = get_post( $post_id );
  117. if ( empty( $the_post ) ) {
  118. $upsert_results['status'] = 'failure';
  119. $upsert_results['results'] = __( 'Post doesn\'t exist.', 'wordpress-seo' );
  120. return $upsert_results;
  121. }
  122. $post_type_object = get_post_type_object( $the_post->post_type );
  123. if ( ! $post_type_object ) {
  124. $upsert_results['status'] = 'failure';
  125. $upsert_results['results'] = sprintf(
  126. /* translators: %s expands to post type. */
  127. __( 'Post has an invalid Content Type: %s.', 'wordpress-seo' ),
  128. $the_post->post_type
  129. );
  130. return $upsert_results;
  131. }
  132. if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) {
  133. $upsert_results['status'] = 'failure';
  134. $upsert_results['results'] = sprintf(
  135. /* translators: %s expands to post type name. */
  136. __( 'You can\'t edit %s.', 'wordpress-seo' ),
  137. $post_type_object->label
  138. );
  139. return $upsert_results;
  140. }
  141. if ( ! current_user_can( $post_type_object->cap->edit_others_posts ) && (int) $the_post->post_author !== get_current_user_id() ) {
  142. $upsert_results['status'] = 'failure';
  143. $upsert_results['results'] = sprintf(
  144. /* translators: %s expands to the name of a post type (plural). */
  145. __( 'You can\'t edit %s that aren\'t yours.', 'wordpress-seo' ),
  146. $post_type_object->label
  147. );
  148. return $upsert_results;
  149. }
  150. if ( $sanitized_new_meta_value === $orig_meta_value && $sanitized_new_meta_value !== $new_meta_value ) {
  151. $upsert_results['status'] = 'failure';
  152. $upsert_results['results'] = __( 'You have used HTML in your value which is not allowed.', 'wordpress-seo' );
  153. return $upsert_results;
  154. }
  155. $res = update_post_meta( $post_id, $meta_key, $sanitized_new_meta_value );
  156. $upsert_results['status'] = ( $res !== false ) ? 'success' : 'failure';
  157. $upsert_results['results'] = $res;
  158. return $upsert_results;
  159. }
  160. /**
  161. * Save all titles sent from the Bulk Editor.
  162. */
  163. function wpseo_save_all_titles() {
  164. wpseo_save_all( 'title' );
  165. }
  166. add_action( 'wp_ajax_wpseo_save_all_titles', 'wpseo_save_all_titles' );
  167. /**
  168. * Save all description sent from the Bulk Editor.
  169. */
  170. function wpseo_save_all_descriptions() {
  171. wpseo_save_all( 'metadesc' );
  172. }
  173. add_action( 'wp_ajax_wpseo_save_all_descriptions', 'wpseo_save_all_descriptions' );
  174. /**
  175. * Utility function to save values.
  176. *
  177. * @param string $what Type of item so save.
  178. */
  179. function wpseo_save_all( $what ) {
  180. check_ajax_referer( 'wpseo-bulk-editor' );
  181. $results = [];
  182. if ( ! isset( $_POST['items'], $_POST['existingItems'] ) ) {
  183. wpseo_ajax_json_echo_die( $results );
  184. }
  185. $new_values = array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], wp_unslash( (array) $_POST['items'] ) );
  186. $original_values = array_map( [ 'WPSEO_Utils', 'sanitize_text_field' ], wp_unslash( (array) $_POST['existingItems'] ) );
  187. foreach ( $new_values as $post_id => $new_value ) {
  188. $original_value = $original_values[ $post_id ];
  189. $results[] = wpseo_upsert_new( $what, $post_id, $new_value, $original_value );
  190. }
  191. wpseo_ajax_json_echo_die( $results );
  192. }
  193. /**
  194. * Insert a new value.
  195. *
  196. * @param string $what Item type (such as title).
  197. * @param int $post_id Post ID.
  198. * @param string $new New value to record.
  199. * @param string $original Original value.
  200. *
  201. * @return string
  202. */
  203. function wpseo_upsert_new( $what, $post_id, $new, $original ) {
  204. $meta_key = WPSEO_Meta::$meta_prefix . $what;
  205. return wpseo_upsert_meta( $post_id, $new, $original, $meta_key, $what );
  206. }
  207. /**
  208. * Retrieves the keyword for the keyword doubles.
  209. */
  210. function ajax_get_keyword_usage() {
  211. $post_id = filter_input( INPUT_POST, 'post_id' );
  212. $keyword = filter_input( INPUT_POST, 'keyword' );
  213. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  214. die( '-1' );
  215. }
  216. wp_die(
  217. // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: WPSEO_Utils::format_json_encode is safe.
  218. WPSEO_Utils::format_json_encode( WPSEO_Meta::keyword_usage( $keyword, $post_id ) )
  219. );
  220. }
  221. add_action( 'wp_ajax_get_focus_keyword_usage', 'ajax_get_keyword_usage' );
  222. /**
  223. * Retrieves the keyword for the keyword doubles of the termpages.
  224. */
  225. function ajax_get_term_keyword_usage() {
  226. $post_id = filter_input( INPUT_POST, 'post_id' );
  227. $keyword = filter_input( INPUT_POST, 'keyword' );
  228. $taxonomy_name = filter_input( INPUT_POST, 'taxonomy' );
  229. $taxonomy = get_taxonomy( $taxonomy_name );
  230. if ( ! $taxonomy ) {
  231. wp_die( 0 );
  232. }
  233. if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) {
  234. wp_die( -1 );
  235. }
  236. $usage = WPSEO_Taxonomy_Meta::get_keyword_usage( $keyword, $post_id, $taxonomy_name );
  237. // Normalize the result so it it the same as the post keyword usage AJAX request.
  238. $usage = $usage[ $keyword ];
  239. wp_die(
  240. // phpcs:ignore WordPress.Security.EscapeOutput -- Reason: WPSEO_Utils::format_json_encode is safe.
  241. WPSEO_Utils::format_json_encode( $usage )
  242. );
  243. }
  244. add_action( 'wp_ajax_get_term_keyword_usage', 'ajax_get_term_keyword_usage' );
  245. /**
  246. * Registers hooks for all AJAX integrations.
  247. *
  248. * @return void
  249. */
  250. function wpseo_register_ajax_integrations() {
  251. $integrations = [ new Yoast_Network_Admin() ];
  252. foreach ( $integrations as $integration ) {
  253. $integration->register_ajax_hooks();
  254. }
  255. }
  256. wpseo_register_ajax_integrations();
  257. // SEO Score Recalculations.
  258. new WPSEO_Recalculate_Scores_Ajax();
  259. new Yoast_OnPage_Ajax();
  260. new WPSEO_Shortcode_Filter();
  261. new WPSEO_Taxonomy_Columns();
  262. // Setting the notice for the recalculate the posts.
  263. new Yoast_Dismissable_Notice_Ajax( 'recalculate', Yoast_Dismissable_Notice_Ajax::FOR_SITE );
  264. /* ********************* DEPRECATED FUNCTIONS ********************* */
  265. /**
  266. * Removes stopword from the sample permalink that is generated in an AJAX request.
  267. *
  268. * @deprecated 6.3
  269. * @codeCoverageIgnore
  270. */
  271. function wpseo_remove_stopwords_sample_permalink() {
  272. _deprecated_function( __FUNCTION__, 'WPSEO 6.3', 'This method is deprecated.' );
  273. wpseo_ajax_json_echo_die( '' );
  274. }
  275. /**
  276. * Function used to delete blocking files, dies on exit.
  277. *
  278. * @deprecated 7.0
  279. * @codeCoverageIgnore
  280. */
  281. function wpseo_kill_blocking_files() {
  282. _deprecated_function( __FUNCTION__, 'WPSEO 7.0', 'This method is deprecated.' );
  283. wpseo_ajax_json_echo_die( '' );
  284. }
  285. /**
  286. * Handles the posting of a new FB admin.
  287. *
  288. * @deprecated 7.1
  289. * @codeCoverageIgnore
  290. */
  291. function wpseo_add_fb_admin() {
  292. if ( ! current_user_can( 'manage_options' ) ) {
  293. die( '-1' );
  294. }
  295. _deprecated_function( __FUNCTION__, 'WPSEO 7.0', 'This method is deprecated.' );
  296. wpseo_ajax_json_echo_die( '' );
  297. }
  298. /**
  299. * Used in the editor to replace vars for the snippet preview.
  300. *
  301. * @deprecated 11.9
  302. * @codeCoverageIgnore
  303. */
  304. function wpseo_ajax_replace_vars() {
  305. _deprecated_function( __METHOD__, 'WPSEO 11.9' );
  306. global $post;
  307. check_ajax_referer( 'wpseo-replace-vars' );
  308. $post = get_post( intval( filter_input( INPUT_POST, 'post_id' ) ) );
  309. global $wp_query;
  310. $wp_query->queried_object = $post;
  311. $wp_query->queried_object_id = $post->ID;
  312. $omit = [ 'excerpt', 'excerpt_only', 'title' ];
  313. echo wpseo_replace_vars( stripslashes( filter_input( INPUT_POST, 'string' ) ), $post, $omit );
  314. die;
  315. }