class-recalculate-scores-ajax.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Ajax
  6. */
  7. /**
  8. * Class WPSEO_Recalculate_Scores.
  9. *
  10. * This class handles the SEO score recalculation for all posts with a filled focus keyword.
  11. */
  12. class WPSEO_Recalculate_Scores_Ajax {
  13. /**
  14. * Initialize the AJAX hooks.
  15. */
  16. public function __construct() {
  17. add_action( 'wp_ajax_wpseo_recalculate_scores', [ $this, 'recalculate_scores' ] );
  18. add_action( 'wp_ajax_wpseo_update_score', [ $this, 'save_score' ] );
  19. add_action( 'wp_ajax_wpseo_recalculate_total', [ $this, 'get_total' ] );
  20. }
  21. /**
  22. * Get the totals for the posts and the terms.
  23. */
  24. public function get_total() {
  25. check_ajax_referer( 'wpseo_recalculate', 'nonce' );
  26. wp_die(
  27. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: WPSEO_Utils::format_json_encode is considered safe.
  28. WPSEO_Utils::format_json_encode(
  29. [
  30. 'posts' => $this->calculate_posts(),
  31. 'terms' => $this->calculate_terms(),
  32. ]
  33. )
  34. );
  35. }
  36. /**
  37. * Start recalculation.
  38. */
  39. public function recalculate_scores() {
  40. check_ajax_referer( 'wpseo_recalculate', 'nonce' );
  41. $fetch_object = $this->get_fetch_object();
  42. if ( ! empty( $fetch_object ) ) {
  43. $paged = filter_input( INPUT_POST, 'paged', FILTER_VALIDATE_INT );
  44. $response = $fetch_object->get_items_to_recalculate( $paged );
  45. if ( ! empty( $response ) ) {
  46. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: WPSEO_Utils::format_json_encode is considered safe.
  47. wp_die( WPSEO_Utils::format_json_encode( $response ) );
  48. }
  49. }
  50. wp_die( '' );
  51. }
  52. /**
  53. * Saves the new linkdex score for given post.
  54. */
  55. public function save_score() {
  56. check_ajax_referer( 'wpseo_recalculate', 'nonce' );
  57. $fetch_object = $this->get_fetch_object();
  58. if ( ! empty( $fetch_object ) ) {
  59. $scores = filter_input( INPUT_POST, 'scores', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
  60. $fetch_object->save_scores( $scores );
  61. }
  62. wp_die();
  63. }
  64. /**
  65. * Returns the needed object for recalculating scores.
  66. *
  67. * @return WPSEO_Recalculate_Posts|WPSEO_Recalculate_Terms
  68. */
  69. private function get_fetch_object() {
  70. switch ( filter_input( INPUT_POST, 'type' ) ) {
  71. case 'post':
  72. return new WPSEO_Recalculate_Posts();
  73. case 'term':
  74. return new WPSEO_Recalculate_Terms();
  75. }
  76. return null;
  77. }
  78. /**
  79. * Gets the total number of posts.
  80. *
  81. * @return int
  82. */
  83. private function calculate_posts() {
  84. $count_posts_query = new WP_Query(
  85. [
  86. 'post_type' => 'any',
  87. 'meta_key' => '_yoast_wpseo_focuskw',
  88. 'posts_per_page' => 1,
  89. 'fields' => 'ids',
  90. ]
  91. );
  92. return $count_posts_query->found_posts;
  93. }
  94. /**
  95. * Get the total number of terms.
  96. *
  97. * @return int
  98. */
  99. private function calculate_terms() {
  100. $total = 0;
  101. foreach ( get_taxonomies( [], 'objects' ) as $taxonomy ) {
  102. $total += wp_count_terms( $taxonomy->name, [ 'hide_empty' => false ] );
  103. }
  104. return $total;
  105. }
  106. }