PageRenderTime 31ms CodeModel.GetById 21ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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