ajax.php 10 KB

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