WPSEO_Recalculate_Scores_Ajax

Class WPSEO_Recalculate_Scores.

Defined (1)

The class is defined in the following location(s).

/admin/ajax/class-recalculate-scores-ajax.php  
  1. class WPSEO_Recalculate_Scores_Ajax { 
  2.  
  3. /** 
  4. * Initialize the AJAX hooks 
  5. */ 
  6. public function __construct() { 
  7. add_action( 'wp_ajax_wpseo_recalculate_scores', array( $this, 'recalculate_scores' ) ); 
  8. add_action( 'wp_ajax_wpseo_update_score', array( $this, 'save_score' ) ); 
  9. add_action( 'wp_ajax_wpseo_recalculate_total', array( $this, 'get_total' ) ); 
  10.  
  11. /** 
  12. * Get the totals for the posts and the terms. 
  13. */ 
  14. public function get_total() { 
  15. check_ajax_referer( 'wpseo_recalculate', 'nonce' ); 
  16.  
  17. wp_die( 
  18. wp_json_encode( 
  19. array( 
  20. 'posts' => $this->calculate_posts(),  
  21. 'terms' => $this->calculate_terms(),  
  22. ); 
  23.  
  24. /** 
  25. * Start recalculation 
  26. */ 
  27. public function recalculate_scores() { 
  28. check_ajax_referer( 'wpseo_recalculate', 'nonce' ); 
  29.  
  30. $fetch_object = $this->get_fetch_object(); 
  31. if ( ! empty( $fetch_object ) ) { 
  32. $paged = filter_input( INPUT_POST, 'paged', FILTER_VALIDATE_INT ); 
  33. $response = $fetch_object->get_items_to_recalculate( $paged ); 
  34.  
  35. if ( ! empty( $response ) ) { 
  36. wp_die( wp_json_encode( $response ) ); 
  37.  
  38. wp_die( '' ); 
  39.  
  40. /** 
  41. * Saves the new linkdex score for given post 
  42. */ 
  43. public function save_score() { 
  44. check_ajax_referer( 'wpseo_recalculate', 'nonce' ); 
  45.  
  46. $fetch_object = $this->get_fetch_object(); 
  47. if ( ! empty( $fetch_object ) ) { 
  48. $scores = filter_input( INPUT_POST, 'scores', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); 
  49. $fetch_object->save_scores( $scores ); 
  50.  
  51. wp_die(); 
  52.  
  53. /** 
  54. * Returns the needed object for recalculating scores. 
  55. * @return WPSEO_Recalculate_Posts|WPSEO_Recalculate_Terms 
  56. */ 
  57. private function get_fetch_object() { 
  58. switch ( filter_input( INPUT_POST, 'type' ) ) { 
  59. case 'post': 
  60. return new WPSEO_Recalculate_Posts(); 
  61. case 'term': 
  62. return new WPSEO_Recalculate_Terms(); 
  63.  
  64. return null; 
  65.  
  66. /** 
  67. * Gets the total number of posts 
  68. * @return int 
  69. */ 
  70. private function calculate_posts() { 
  71. $count_posts_query = new WP_Query( 
  72. array( 
  73. 'post_type' => 'any',  
  74. 'meta_key' => '_yoast_wpseo_focuskw',  
  75. 'posts_per_page' => 1,  
  76. 'fields' => 'ids',  
  77. ); 
  78.  
  79. return $count_posts_query->found_posts; 
  80.  
  81. /** 
  82. * Get the total number of terms 
  83. * @return int 
  84. */ 
  85. private function calculate_terms() { 
  86. $total = 0; 
  87. foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) { 
  88. $total += wp_count_terms( $taxonomy->name, array( 'hide_empty' => false ) ); 
  89.  
  90. return $total;