/wp-content/plugins/wordpress-seo/admin/ajax/class-recalculate-scores-ajax.php

https://bitbucket.org/carloskikea/helpet · PHP · 121 lines · 67 code · 17 blank · 37 comment · 4 complexity · 1fe900f89f5d569322622f86137f7c49 MD5 · raw file

  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', array( $this, 'recalculate_scores' ) );
  18. add_action( 'wp_ajax_wpseo_update_score', array( $this, 'save_score' ) );
  19. add_action( 'wp_ajax_wpseo_recalculate_total', array( $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. wp_json_encode(
  28. array(
  29. 'posts' => $this->calculate_posts(),
  30. 'terms' => $this->calculate_terms(),
  31. )
  32. )
  33. );
  34. }
  35. /**
  36. * Start recalculation
  37. */
  38. public function recalculate_scores() {
  39. check_ajax_referer( 'wpseo_recalculate', 'nonce' );
  40. $fetch_object = $this->get_fetch_object();
  41. if ( ! empty( $fetch_object ) ) {
  42. $paged = filter_input( INPUT_POST, 'paged', FILTER_VALIDATE_INT );
  43. $response = $fetch_object->get_items_to_recalculate( $paged );
  44. if ( ! empty( $response ) ) {
  45. wp_die( wp_json_encode( $response ) );
  46. }
  47. }
  48. wp_die( '' );
  49. }
  50. /**
  51. * Saves the new linkdex score for given post
  52. */
  53. public function save_score() {
  54. check_ajax_referer( 'wpseo_recalculate', 'nonce' );
  55. $fetch_object = $this->get_fetch_object();
  56. if ( ! empty( $fetch_object ) ) {
  57. $scores = filter_input( INPUT_POST, 'scores', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
  58. $fetch_object->save_scores( $scores );
  59. }
  60. wp_die();
  61. }
  62. /**
  63. * Returns the needed object for recalculating scores.
  64. *
  65. * @return WPSEO_Recalculate_Posts|WPSEO_Recalculate_Terms
  66. */
  67. private function get_fetch_object() {
  68. switch ( filter_input( INPUT_POST, 'type' ) ) {
  69. case 'post':
  70. return new WPSEO_Recalculate_Posts();
  71. case 'term':
  72. return new WPSEO_Recalculate_Terms();
  73. }
  74. return null;
  75. }
  76. /**
  77. * Gets the total number of posts
  78. *
  79. * @return int
  80. */
  81. private function calculate_posts() {
  82. $count_posts_query = new WP_Query(
  83. array(
  84. 'post_type' => 'any',
  85. 'meta_key' => '_yoast_wpseo_focuskw',
  86. 'posts_per_page' => 1,
  87. 'fields' => 'ids',
  88. )
  89. );
  90. return $count_posts_query->found_posts;
  91. }
  92. /**
  93. * Get the total number of terms
  94. *
  95. * @return int
  96. */
  97. private function calculate_terms() {
  98. $total = 0;
  99. foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
  100. $total += wp_count_terms( $taxonomy->name, array( 'hide_empty' => false ) );
  101. }
  102. return $total;
  103. }
  104. }