PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/ajax.php

https://gitlab.com/ngochuynh1991/cuacuon
PHP | 445 lines | 237 code | 102 blank | 106 comment | 30 complexity | a5d0f5038cd41b1e7a64bfa1ee5858c7 MD5 | raw file
  1. <?php
  2. /**
  3. * @package WPSEO\Admin
  4. */
  5. if ( ! defined( 'WPSEO_VERSION' ) ) {
  6. header( 'Status: 403 Forbidden' );
  7. header( 'HTTP/1.1 403 Forbidden' );
  8. exit();
  9. }
  10. /**
  11. * @todo this whole thing should probably be a proper class.
  12. */
  13. /**
  14. * Convenience function to JSON encode and echo results and then die
  15. *
  16. * @param array $results Results array for encoding.
  17. */
  18. function wpseo_ajax_json_echo_die( $results ) {
  19. echo WPSEO_Utils::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', array( '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. $options = get_option( 'wpseo' );
  52. $options[ 'ignore_' . $ignore_key ] = true;
  53. update_option( 'wpseo', $options );
  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. * Function used to delete blocking files, dies on exit.
  71. */
  72. function wpseo_kill_blocking_files() {
  73. if ( ! current_user_can( 'manage_options' ) ) {
  74. die( '-1' );
  75. }
  76. check_ajax_referer( 'wpseo-blocking-files' );
  77. $message = 'success';
  78. $errors = array();
  79. // Todo: Use WP_Filesystem, but not so easy to use in AJAX with credentials form still internal.
  80. $options = get_option( 'wpseo' );
  81. if ( is_array( $options['blocking_files'] ) && $options['blocking_files'] !== array() ) {
  82. foreach ( $options['blocking_files'] as $file ) {
  83. if ( is_file( $file ) ) {
  84. if ( ! @unlink( $file ) ) {
  85. $errors[] = __(
  86. sprintf( 'The file "%s" could not be removed. Please remove it via FTP.', $file ),
  87. 'wordpress-seo'
  88. );
  89. }
  90. }
  91. if ( is_dir( $file ) ) {
  92. if ( ! @ rmdir( $file ) ) {
  93. $errors[] = __(
  94. sprintf( 'The directory "%s" could not be removed. Please remove it via FTP.', $file ),
  95. 'wordpress-seo'
  96. );
  97. }
  98. }
  99. }
  100. }
  101. if ( $errors ) {
  102. $message = implode( '<br />', $errors );
  103. }
  104. die( $message );
  105. }
  106. add_action( 'wp_ajax_wpseo_kill_blocking_files', 'wpseo_kill_blocking_files' );
  107. /**
  108. * Used in the editor to replace vars for the snippet preview
  109. */
  110. function wpseo_ajax_replace_vars() {
  111. global $post;
  112. check_ajax_referer( 'wpseo-replace-vars' );
  113. $post = get_post( intval( filter_input( INPUT_POST, 'post_id' ) ) );
  114. global $wp_query;
  115. $wp_query->queried_object = $post;
  116. $wp_query->queried_object_id = $post->ID;
  117. $omit = array( 'excerpt', 'excerpt_only', 'title' );
  118. echo wpseo_replace_vars( stripslashes( filter_input( INPUT_POST, 'string' ) ), $post, $omit );
  119. die;
  120. }
  121. add_action( 'wp_ajax_wpseo_replace_vars', 'wpseo_ajax_replace_vars' );
  122. /**
  123. * Save an individual SEO title from the Bulk Editor.
  124. */
  125. function wpseo_save_title() {
  126. wpseo_save_what( 'title' );
  127. }
  128. add_action( 'wp_ajax_wpseo_save_title', 'wpseo_save_title' );
  129. /**
  130. * Save an individual meta description from the Bulk Editor.
  131. */
  132. function wpseo_save_description() {
  133. wpseo_save_what( 'metadesc' );
  134. }
  135. add_action( 'wp_ajax_wpseo_save_metadesc', 'wpseo_save_description' );
  136. /**
  137. * Save titles & descriptions
  138. *
  139. * @param string $what Type of item to save (title, description).
  140. */
  141. function wpseo_save_what( $what ) {
  142. check_ajax_referer( 'wpseo-bulk-editor' );
  143. $new = filter_input( INPUT_POST, 'new_value' );
  144. $post_id = intval( filter_input( INPUT_POST, 'wpseo_post_id' ) );
  145. $original = filter_input( INPUT_POST, 'existing_value' );
  146. $results = wpseo_upsert_new( $what, $post_id, $new, $original );
  147. wpseo_ajax_json_echo_die( $results );
  148. }
  149. /**
  150. * Helper function to update a post's meta data, returning relevant information
  151. * about the information updated and the results or the meta update.
  152. *
  153. * @param int $post_id Post ID.
  154. * @param string $new_meta_value New meta value to record.
  155. * @param string $orig_meta_value Original meta value.
  156. * @param string $meta_key Meta key string.
  157. * @param string $return_key Return key string to use in results.
  158. *
  159. * @return string
  160. */
  161. function wpseo_upsert_meta( $post_id, $new_meta_value, $orig_meta_value, $meta_key, $return_key ) {
  162. $post_id = intval( $post_id );
  163. $sanitized_new_meta_value = wp_strip_all_tags( $new_meta_value );
  164. $orig_meta_value = wp_strip_all_tags( $orig_meta_value );
  165. $upsert_results = array(
  166. 'status' => 'success',
  167. 'post_id' => $post_id,
  168. "new_{$return_key}" => $new_meta_value,
  169. "original_{$return_key}" => $orig_meta_value,
  170. );
  171. $the_post = get_post( $post_id );
  172. if ( empty( $the_post ) ) {
  173. $upsert_results['status'] = 'failure';
  174. $upsert_results['results'] = __( 'Post doesn\'t exist.', 'wordpress-seo' );
  175. return $upsert_results;
  176. }
  177. $post_type_object = get_post_type_object( $the_post->post_type );
  178. if ( ! $post_type_object ) {
  179. $upsert_results['status'] = 'failure';
  180. $upsert_results['results'] = sprintf( __( 'Post has an invalid Post Type: %s.', 'wordpress-seo' ), $the_post->post_type );
  181. return $upsert_results;
  182. }
  183. if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) {
  184. $upsert_results['status'] = 'failure';
  185. $upsert_results['results'] = sprintf( __( 'You can\'t edit %s.', 'wordpress-seo' ), $post_type_object->label );
  186. return $upsert_results;
  187. }
  188. if ( ! current_user_can( $post_type_object->cap->edit_others_posts ) && $the_post->post_author != get_current_user_id() ) {
  189. $upsert_results['status'] = 'failure';
  190. $upsert_results['results'] = sprintf( __( 'You can\'t edit %s that aren\'t yours.', 'wordpress-seo' ), $post_type_object->label );
  191. return $upsert_results;
  192. }
  193. if ( $sanitized_new_meta_value === $orig_meta_value && $sanitized_new_meta_value !== $new_meta_value ) {
  194. $upsert_results['status'] = 'failure';
  195. $upsert_results['results'] = __( 'You have used HTML in your value which is not allowed.', 'wordpress-seo' );
  196. return $upsert_results;
  197. }
  198. $res = update_post_meta( $post_id, $meta_key, $sanitized_new_meta_value );
  199. $upsert_results['status'] = ( $res !== false ) ? 'success' : 'failure';
  200. $upsert_results['results'] = $res;
  201. return $upsert_results;
  202. }
  203. /**
  204. * Save all titles sent from the Bulk Editor.
  205. */
  206. function wpseo_save_all_titles() {
  207. wpseo_save_all( 'title' );
  208. }
  209. add_action( 'wp_ajax_wpseo_save_all_titles', 'wpseo_save_all_titles' );
  210. /**
  211. * Save all description sent from the Bulk Editor.
  212. */
  213. function wpseo_save_all_descriptions() {
  214. wpseo_save_all( 'metadesc' );
  215. }
  216. add_action( 'wp_ajax_wpseo_save_all_descriptions', 'wpseo_save_all_descriptions' );
  217. /**
  218. * Utility function to save values
  219. *
  220. * @param string $what Type of item so save.
  221. */
  222. function wpseo_save_all( $what ) {
  223. check_ajax_referer( 'wpseo-bulk-editor' );
  224. // @todo the WPSEO Utils class can't filter arrays in POST yet.
  225. $new_values = $_POST['items'];
  226. $original_values = $_POST['existing_items'];
  227. $results = array();
  228. if ( is_array( $new_values ) && $new_values !== array() ) {
  229. foreach ( $new_values as $post_id => $new_value ) {
  230. $original_value = $original_values[ $post_id ];
  231. $results[] = wpseo_upsert_new( $what, $post_id, $new_value, $original_value );
  232. }
  233. }
  234. wpseo_ajax_json_echo_die( $results );
  235. }
  236. /**
  237. * Insert a new value
  238. *
  239. * @param string $what Item type (such as title).
  240. * @param int $post_id Post ID.
  241. * @param string $new New value to record.
  242. * @param string $original Original value.
  243. *
  244. * @return string
  245. */
  246. function wpseo_upsert_new( $what, $post_id, $new, $original ) {
  247. $meta_key = WPSEO_Meta::$meta_prefix . $what;
  248. return wpseo_upsert_meta( $post_id, $new, $original, $meta_key, $what );
  249. }
  250. /**
  251. * Create an export and return the URL
  252. */
  253. function wpseo_get_export() {
  254. if ( ! current_user_can( 'manage_options' ) ) {
  255. die( '-1' );
  256. }
  257. $include_taxonomy = ( filter_input( INPUT_POST, 'include_taxonomy' ) === 'true' );
  258. $export = new WPSEO_Export( $include_taxonomy );
  259. wpseo_ajax_json_echo_die( $export->get_results() );
  260. }
  261. add_action( 'wp_ajax_wpseo_export', 'wpseo_get_export' );
  262. /**
  263. * Handles the posting of a new FB admin.
  264. */
  265. function wpseo_add_fb_admin() {
  266. check_ajax_referer( 'wpseo_fb_admin_nonce' );
  267. if ( ! current_user_can( 'manage_options' ) ) {
  268. die( '-1' );
  269. }
  270. $facebook_social = new Yoast_Social_Facebook();
  271. wp_die( $facebook_social->add_admin( filter_input( INPUT_POST, 'admin_name' ), filter_input( INPUT_POST, 'admin_id' ) ) );
  272. }
  273. add_action( 'wp_ajax_wpseo_add_fb_admin', 'wpseo_add_fb_admin' );
  274. /**
  275. * Retrieves the keyword for the keyword doubles.
  276. */
  277. function ajax_get_keyword_usage() {
  278. $post_id = filter_input( INPUT_POST, 'post_id' );
  279. $keyword = filter_input( INPUT_POST, 'keyword' );
  280. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  281. die( '-1' );
  282. }
  283. wp_die(
  284. WPSEO_Utils::json_encode( WPSEO_Meta::keyword_usage( $keyword, $post_id ) )
  285. );
  286. }
  287. add_action( 'wp_ajax_get_focus_keyword_usage', 'ajax_get_keyword_usage' );
  288. /**
  289. * Retrieves the keyword for the keyword doubles of the termpages.
  290. */
  291. function ajax_get_term_keyword_usage() {
  292. $post_id = filter_input( INPUT_POST, 'post_id' );
  293. $keyword = filter_input( INPUT_POST, 'keyword' );
  294. $taxonomy = filter_input( INPUT_POST, 'taxonomy' );
  295. if ( ! current_user_can( 'edit_terms' ) ) {
  296. die( '-1' );
  297. }
  298. $usage = WPSEO_Taxonomy_Meta::get_keyword_usage( $keyword, $post_id, $taxonomy );
  299. // Normalize the result so it it the same as the post keyword usage AJAX request.
  300. $usage = $usage[ $keyword ];
  301. wp_die(
  302. WPSEO_Utils::json_encode( $usage )
  303. );
  304. }
  305. add_action( 'wp_ajax_get_term_keyword_usage', 'ajax_get_term_keyword_usage' );
  306. /**
  307. * Removes stopword from the sample permalink that is generated in an AJAX request
  308. *
  309. * @param array $permalink The permalink generated for this post by WordPress.
  310. * @param int $post_ID The ID of the post.
  311. * @param string $title The title for the post that the user used.
  312. * @param string $name The name for the post that the user used.
  313. *
  314. * @return array
  315. */
  316. function wpseo_remove_stopwords_sample_permalink( $permalink, $post_ID, $title, $name ) {
  317. WPSEO_Options::get_instance();
  318. $options = WPSEO_Options::get_options( array( 'wpseo_permalinks' ) );
  319. if ( $options['cleanslugs'] !== true ) {
  320. return $permalink;
  321. }
  322. /*
  323. * If the name is empty and the title is not, WordPress will generate a slug. In that case we want to remove stop
  324. * words from the slug.
  325. */
  326. if ( empty( $name ) && ! empty( $title ) ) {
  327. $stop_words = new WPSEO_Admin_Stop_Words();
  328. // The second element is the slug.
  329. $permalink[1] = $stop_words->remove_in( $permalink[1] );
  330. }
  331. return $permalink;
  332. }
  333. add_action( 'get_sample_permalink', 'wpseo_remove_stopwords_sample_permalink', 10, 4 );
  334. // Crawl Issue Manager AJAX hooks.
  335. new WPSEO_GSC_Ajax;
  336. // SEO Score Recalculations.
  337. new WPSEO_Recalculate_Scores_Ajax;
  338. new Yoast_Dashboard_Widget();
  339. new Yoast_OnPage_Ajax();
  340. new WPSEO_Shortcode_Filter();
  341. new WPSEO_Taxonomy_Columns();
  342. // Setting the notice for the recalculate the posts.
  343. new Yoast_Dismissable_Notice_Ajax( 'recalculate', Yoast_Dismissable_Notice_Ajax::FOR_SITE );