khaihihi
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
/**
|
||||
* WPSEO plugin file.
|
||||
*
|
||||
* @package WPSEO\Admin
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class handles the calculation of the SEO score for all posts with a filled focus keyword.
|
||||
*/
|
||||
class WPSEO_Recalculate_Posts extends WPSEO_Recalculate {
|
||||
|
||||
/**
|
||||
* Save the scores.
|
||||
*
|
||||
* @param array $scores The scores for the posts.
|
||||
*/
|
||||
public function save_scores( array $scores ) {
|
||||
foreach ( $scores as $score ) {
|
||||
$this->save_score( $score );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the score.
|
||||
*
|
||||
* @param array $score The score to save.
|
||||
*/
|
||||
protected function save_score( array $score ) {
|
||||
WPSEO_Meta::set_value( 'linkdex', $score['score'], $score['item_id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the posts from the database by doing a WP_Query.
|
||||
*
|
||||
* @param integer $paged The page.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_items( $paged ) {
|
||||
$items_per_page = max( 1, $this->items_per_page );
|
||||
$post_query = new WP_Query(
|
||||
[
|
||||
'post_type' => 'any',
|
||||
'meta_key' => '_yoast_wpseo_focuskw',
|
||||
'posts_per_page' => $items_per_page,
|
||||
'paged' => $paged,
|
||||
]
|
||||
);
|
||||
|
||||
return $post_query->get_posts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the posts to a response array.
|
||||
*
|
||||
* @param WP_Post $item The post for which to build the analyzer data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function item_to_response( $item ) {
|
||||
$focus_keyword = WPSEO_Meta::get_value( 'focuskw', $item->ID );
|
||||
|
||||
$content = $item->post_content;
|
||||
|
||||
// Check if there's a featured image.
|
||||
$content .= $this->add_featured_image( $item );
|
||||
|
||||
/**
|
||||
* Filter the post content for use in the SEO score recalculation.
|
||||
*
|
||||
* @param string $content Content of the post. Modify to reflect front-end content.
|
||||
* @param WP_Post $item The Post object the content comes from.
|
||||
*/
|
||||
$content = apply_filters( 'wpseo_post_content_for_recalculation', $content, $item );
|
||||
|
||||
// Apply shortcodes.
|
||||
$content = do_shortcode( $content );
|
||||
|
||||
return [
|
||||
'post_id' => $item->ID,
|
||||
'text' => $content,
|
||||
'keyword' => $focus_keyword,
|
||||
'url' => urldecode( $item->post_name ),
|
||||
'pageTitle' => apply_filters( 'wpseo_title', wpseo_replace_vars( $this->get_title( $item->ID, $item->post_type ), $item ) ),
|
||||
'meta' => apply_filters( 'wpseo_metadesc', wpseo_replace_vars( $this->get_meta_description( $item->ID, $item->post_type ), $item ) ),
|
||||
'keyword_usage' => [
|
||||
$focus_keyword => WPSEO_Meta::keyword_usage( $focus_keyword, $item->ID ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the title for given post.
|
||||
*
|
||||
* @param integer $post_id The ID of the post for which to get the title.
|
||||
* @param string $post_type The post type.
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
private function get_title( $post_id, $post_type ) {
|
||||
$title = WPSEO_Meta::get_value( 'title', $post_id );
|
||||
if ( $title !== '' ) {
|
||||
return $title;
|
||||
}
|
||||
|
||||
$default_from_options = $this->default_from_options( 'title-tax', $post_type );
|
||||
if ( $default_from_options !== false ) {
|
||||
return str_replace( ' %%page%% ', ' ', $default_from_options );
|
||||
}
|
||||
|
||||
return '%%title%%';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the meta description for given post.
|
||||
*
|
||||
* @param integer $post_id The ID of the post for which to get the meta description.
|
||||
* @param string $post_type The post type.
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
private function get_meta_description( $post_id, $post_type ) {
|
||||
$meta_description = WPSEO_Meta::get_value( 'metadesc', $post_id );
|
||||
if ( $meta_description !== '' ) {
|
||||
return $meta_description;
|
||||
}
|
||||
|
||||
$default_from_options = $this->default_from_options( 'metadesc', $post_type );
|
||||
if ( $default_from_options !== false ) {
|
||||
return $default_from_options;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the associated featured image if there is one present.
|
||||
*
|
||||
* @param WP_Post $item The post item to check for a featured image.
|
||||
*
|
||||
* @return string The image string.
|
||||
*/
|
||||
private function add_featured_image( $item ) {
|
||||
if ( ! has_post_thumbnail( $item->ID ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return ' ' . get_the_post_thumbnail( $item->ID );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
/**
|
||||
* WPSEO plugin file.
|
||||
*
|
||||
* @package WPSEO\Admin
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class handles the calculation of the SEO score for all terms.
|
||||
*/
|
||||
class WPSEO_Recalculate_Terms extends WPSEO_Recalculate {
|
||||
|
||||
/**
|
||||
* Save the scores.
|
||||
*
|
||||
* @param array $scores The scores to save.
|
||||
*/
|
||||
public function save_scores( array $scores ) {
|
||||
|
||||
$tax_meta = get_option( 'wpseo_taxonomy_meta' );
|
||||
|
||||
foreach ( $scores as $score ) {
|
||||
$tax_meta[ $score['taxonomy'] ][ $score['item_id'] ]['wpseo_linkdex'] = $score['score'];
|
||||
}
|
||||
|
||||
update_option( 'wpseo_taxonomy_meta', $tax_meta );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the score.
|
||||
*
|
||||
* @param array $score The score to save.
|
||||
*/
|
||||
protected function save_score( array $score ) {
|
||||
WPSEO_Meta::set_value( 'linkdex', $score['score'], $score['item_id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the terms from the database by doing a WP_Query.
|
||||
*
|
||||
* @param integer $paged The page.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_items( $paged ) {
|
||||
$items_per_page = max( 1, $this->items_per_page );
|
||||
|
||||
return get_terms(
|
||||
get_taxonomies(),
|
||||
[
|
||||
'hide_empty' => false,
|
||||
'number' => $items_per_page,
|
||||
'offset' => ( $items_per_page * abs( $paged - 1 ) ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given term into a analyzable object.
|
||||
*
|
||||
* @param mixed $item The term for which to build the analyzer data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function item_to_response( $item ) {
|
||||
$focus_keyword = $this->get_focus_keyword( $item );
|
||||
$title = str_replace( ' %%page%% ', ' ', $this->get_title( $item ) );
|
||||
$meta = $this->get_meta_description( $item );
|
||||
|
||||
$description = $item->description;
|
||||
|
||||
/**
|
||||
* Filter the term description for recalculation.
|
||||
*
|
||||
* @param string $description Content of the term. Modify to reflect front-end content.
|
||||
* @oaram WP_Term $item The term the description comes from.
|
||||
*/
|
||||
$description = apply_filters( 'wpseo_term_description_for_recalculation', $description, $item );
|
||||
|
||||
return [
|
||||
'term_id' => $item->term_id,
|
||||
'taxonomy' => $item->taxonomy,
|
||||
'text' => $description,
|
||||
'keyword' => $focus_keyword,
|
||||
'url' => urldecode( $item->slug ),
|
||||
'pageTitle' => apply_filters( 'wpseo_title', wpseo_replace_vars( $title, $item, [ 'page' ] ) ),
|
||||
'meta' => apply_filters( 'wpseo_metadesc', wpseo_replace_vars( $meta, $item ) ),
|
||||
'keyword_usage' => [
|
||||
$focus_keyword => WPSEO_Taxonomy_Meta::get_keyword_usage( $focus_keyword, $item->term_id, $item->taxonomy ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the focus keyword for the term.
|
||||
*
|
||||
* @param stdClass|WP_Term $term Term to determine the keyword for.
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
private function get_focus_keyword( $term ) {
|
||||
$focus_keyword = WPSEO_Taxonomy_Meta::get_term_meta( 'focuskw', $term->term_id, $term->taxonomy );
|
||||
if ( ! empty( $focus_keyword ) ) {
|
||||
return $focus_keyword;
|
||||
}
|
||||
|
||||
return $term->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the title for given term.
|
||||
*
|
||||
* @param stdClass|WP_Term $term The term object.
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
private function get_title( $term ) {
|
||||
$title = WPSEO_Taxonomy_Meta::get_term_meta( $term->term_id, $term->taxonomy, 'title' );
|
||||
if ( $title !== '' ) {
|
||||
return $title;
|
||||
}
|
||||
|
||||
$default_from_options = $this->default_from_options( 'title-tax', $term->taxonomy );
|
||||
if ( $default_from_options !== false ) {
|
||||
return $default_from_options;
|
||||
}
|
||||
|
||||
return '%%title%%';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the meta description for given post.
|
||||
*
|
||||
* @param stdClass|WP_Term $term The term object.
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
private function get_meta_description( $term ) {
|
||||
$meta_description = WPSEO_Taxonomy_Meta::get_term_meta( $term->term_id, $term->taxonomy, 'desc' );
|
||||
if ( $meta_description !== '' ) {
|
||||
return $meta_description;
|
||||
}
|
||||
|
||||
$default_from_options = $this->default_from_options( 'metadesc-tax', $term->taxonomy );
|
||||
if ( $default_from_options !== false ) {
|
||||
return $default_from_options;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* WPSEO plugin file.
|
||||
*
|
||||
* @package WPSEO\Admin
|
||||
*/
|
||||
|
||||
/**
|
||||
* Abstract class to force methods in recalculate classes.
|
||||
*/
|
||||
abstract class WPSEO_Recalculate {
|
||||
|
||||
/**
|
||||
* Recalculations per page.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $items_per_page = 20;
|
||||
|
||||
/**
|
||||
* Saves the array with scores to the database.
|
||||
*
|
||||
* @param array $scores Array with the score for each item.
|
||||
*/
|
||||
abstract public function save_scores( array $scores );
|
||||
|
||||
/**
|
||||
* Gets the items and parses it to an response.
|
||||
*
|
||||
* @param integer $paged The current page number.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function get_items( $paged );
|
||||
|
||||
/**
|
||||
* Maps the items to an array for the response.
|
||||
*
|
||||
* @param mixed $item Object with data to parse.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function item_to_response( $item );
|
||||
|
||||
/**
|
||||
* Gets the items to recalculate.
|
||||
*
|
||||
* @param int $paged The current page number.
|
||||
*
|
||||
* @return array Items that can be recalculated.
|
||||
*/
|
||||
public function get_items_to_recalculate( $paged ) {
|
||||
$return = [];
|
||||
|
||||
$paged = abs( $paged );
|
||||
|
||||
$items = $this->get_items( $paged );
|
||||
|
||||
$return['items'] = $this->parse_items( $items );
|
||||
$return['total_items'] = count( $items );
|
||||
|
||||
if ( $return['total_items'] >= $this->items_per_page ) {
|
||||
$return['next_page'] = ( $paged + 1 );
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the posts|terms with the value we need.
|
||||
*
|
||||
* @param array $items The items to parse.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function parse_items( array $items ) {
|
||||
$return = [];
|
||||
foreach ( $items as $item ) {
|
||||
$response = $this->item_to_response( $item );
|
||||
if ( ! empty( $response ) ) {
|
||||
$return[] = $response;
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default from the options for given field.
|
||||
*
|
||||
* @param string $field The field for which to get the default options.
|
||||
* @param string $suffix The post type.
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
protected function default_from_options( $field, $suffix ) {
|
||||
$target_option_field = $field . '-' . $suffix;
|
||||
if ( WPSEO_Options::get( $target_option_field, '' ) !== '' ) {
|
||||
return WPSEO_Options::get( $target_option_field );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user