Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wordpres.../src/builders
File: indexable-term-builder.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Builders;
[2] Fix | Delete
[3] Fix | Delete
use Yoast\WP\SEO\Exceptions\Indexable\Invalid_Term_Exception;
[4] Fix | Delete
use Yoast\WP\SEO\Exceptions\Indexable\Term_Not_Built_Exception;
[5] Fix | Delete
use Yoast\WP\SEO\Exceptions\Indexable\Term_Not_Found_Exception;
[6] Fix | Delete
use Yoast\WP\SEO\Helpers\Post_Helper;
[7] Fix | Delete
use Yoast\WP\SEO\Helpers\Taxonomy_Helper;
[8] Fix | Delete
use Yoast\WP\SEO\Models\Indexable;
[9] Fix | Delete
use Yoast\WP\SEO\Values\Indexables\Indexable_Builder_Versions;
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Term Builder for the indexables.
[13] Fix | Delete
*
[14] Fix | Delete
* Formats the term meta to indexable format.
[15] Fix | Delete
*/
[16] Fix | Delete
class Indexable_Term_Builder {
[17] Fix | Delete
[18] Fix | Delete
use Indexable_Social_Image_Trait;
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Holds the taxonomy helper instance.
[22] Fix | Delete
*
[23] Fix | Delete
* @var Taxonomy_Helper
[24] Fix | Delete
*/
[25] Fix | Delete
protected $taxonomy_helper;
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* The latest version of the Indexable_Term_Builder.
[29] Fix | Delete
*
[30] Fix | Delete
* @var int
[31] Fix | Delete
*/
[32] Fix | Delete
protected $version;
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Holds the taxonomy helper instance.
[36] Fix | Delete
*
[37] Fix | Delete
* @var Post_Helper
[38] Fix | Delete
*/
[39] Fix | Delete
protected $post_helper;
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* Indexable_Term_Builder constructor.
[43] Fix | Delete
*
[44] Fix | Delete
* @param Taxonomy_Helper $taxonomy_helper The taxonomy helper.
[45] Fix | Delete
* @param Indexable_Builder_Versions $versions The latest version of each Indexable Builder.
[46] Fix | Delete
* @param Post_Helper $post_helper The post helper.
[47] Fix | Delete
*/
[48] Fix | Delete
public function __construct(
[49] Fix | Delete
Taxonomy_Helper $taxonomy_helper,
[50] Fix | Delete
Indexable_Builder_Versions $versions,
[51] Fix | Delete
Post_Helper $post_helper
[52] Fix | Delete
) {
[53] Fix | Delete
$this->taxonomy_helper = $taxonomy_helper;
[54] Fix | Delete
$this->version = $versions->get_latest_version_for_type( 'term' );
[55] Fix | Delete
$this->post_helper = $post_helper;
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Formats the data.
[60] Fix | Delete
*
[61] Fix | Delete
* @param int $term_id ID of the term to save data for.
[62] Fix | Delete
* @param Indexable $indexable The indexable to format.
[63] Fix | Delete
*
[64] Fix | Delete
* @return bool|Indexable The extended indexable. False when unable to build.
[65] Fix | Delete
*
[66] Fix | Delete
* @throws Invalid_Term_Exception When the term is invalid.
[67] Fix | Delete
* @throws Term_Not_Built_Exception When the term is not viewable.
[68] Fix | Delete
* @throws Term_Not_Found_Exception When the term is not found.
[69] Fix | Delete
*/
[70] Fix | Delete
public function build( $term_id, $indexable ) {
[71] Fix | Delete
$term = \get_term( $term_id );
[72] Fix | Delete
[73] Fix | Delete
if ( $term === null ) {
[74] Fix | Delete
throw new Term_Not_Found_Exception();
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
if ( \is_wp_error( $term ) ) {
[78] Fix | Delete
throw new Invalid_Term_Exception( $term->get_error_message() );
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
$indexable_taxonomies = $this->taxonomy_helper->get_indexable_taxonomies();
[82] Fix | Delete
if ( ! \in_array( $term->taxonomy, $indexable_taxonomies, true ) ) {
[83] Fix | Delete
throw Term_Not_Built_Exception::because_not_indexable( $term_id );
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
$term_link = \get_term_link( $term, $term->taxonomy );
[87] Fix | Delete
[88] Fix | Delete
if ( \is_wp_error( $term_link ) ) {
[89] Fix | Delete
throw new Invalid_Term_Exception( $term_link->get_error_message() );
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
$term_meta = $this->taxonomy_helper->get_term_meta( $term );
[93] Fix | Delete
[94] Fix | Delete
$indexable->object_id = $term_id;
[95] Fix | Delete
$indexable->object_type = 'term';
[96] Fix | Delete
$indexable->object_sub_type = $term->taxonomy;
[97] Fix | Delete
$indexable->permalink = $term_link;
[98] Fix | Delete
$indexable->blog_id = \get_current_blog_id();
[99] Fix | Delete
[100] Fix | Delete
$indexable->primary_focus_keyword_score = $this->get_keyword_score(
[101] Fix | Delete
$this->get_meta_value( 'wpseo_focuskw', $term_meta ),
[102] Fix | Delete
$this->get_meta_value( 'wpseo_linkdex', $term_meta )
[103] Fix | Delete
);
[104] Fix | Delete
[105] Fix | Delete
$indexable->is_robots_noindex = $this->get_noindex_value( $this->get_meta_value( 'wpseo_noindex', $term_meta ) );
[106] Fix | Delete
$indexable->is_public = ( $indexable->is_robots_noindex === null ) ? null : ! $indexable->is_robots_noindex;
[107] Fix | Delete
[108] Fix | Delete
$this->reset_social_images( $indexable );
[109] Fix | Delete
[110] Fix | Delete
foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) {
[111] Fix | Delete
$indexable->{$indexable_key} = $this->get_meta_value( $meta_key, $term_meta );
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
if ( empty( $indexable->breadcrumb_title ) ) {
[115] Fix | Delete
$indexable->breadcrumb_title = $term->name;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
$this->handle_social_images( $indexable );
[119] Fix | Delete
[120] Fix | Delete
$indexable->is_cornerstone = $this->get_meta_value( 'wpseo_is_cornerstone', $term_meta );
[121] Fix | Delete
[122] Fix | Delete
// Not implemented yet.
[123] Fix | Delete
$indexable->is_robots_nofollow = null;
[124] Fix | Delete
$indexable->is_robots_noarchive = null;
[125] Fix | Delete
$indexable->is_robots_noimageindex = null;
[126] Fix | Delete
$indexable->is_robots_nosnippet = null;
[127] Fix | Delete
[128] Fix | Delete
$timestamps = $this->get_object_timestamps( $term_id, $term->taxonomy );
[129] Fix | Delete
$indexable->object_published_at = $timestamps->published_at;
[130] Fix | Delete
$indexable->object_last_modified = $timestamps->last_modified;
[131] Fix | Delete
[132] Fix | Delete
$indexable->version = $this->version;
[133] Fix | Delete
[134] Fix | Delete
return $indexable;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Converts the meta noindex value to the indexable value.
[139] Fix | Delete
*
[140] Fix | Delete
* @param string $meta_value Term meta to base the value on.
[141] Fix | Delete
*
[142] Fix | Delete
* @return bool|null
[143] Fix | Delete
*/
[144] Fix | Delete
protected function get_noindex_value( $meta_value ) {
[145] Fix | Delete
if ( $meta_value === 'noindex' ) {
[146] Fix | Delete
return true;
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
if ( $meta_value === 'index' ) {
[150] Fix | Delete
return false;
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
return null;
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
/**
[157] Fix | Delete
* Determines the focus keyword score.
[158] Fix | Delete
*
[159] Fix | Delete
* @param string $keyword The focus keyword that is set.
[160] Fix | Delete
* @param int $score The score saved on the meta data.
[161] Fix | Delete
*
[162] Fix | Delete
* @return int|null Score to use.
[163] Fix | Delete
*/
[164] Fix | Delete
protected function get_keyword_score( $keyword, $score ) {
[165] Fix | Delete
if ( empty( $keyword ) ) {
[166] Fix | Delete
return null;
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
return $score;
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Retrieves the lookup table.
[174] Fix | Delete
*
[175] Fix | Delete
* @return array Lookup table for the indexable fields.
[176] Fix | Delete
*/
[177] Fix | Delete
protected function get_indexable_lookup() {
[178] Fix | Delete
return [
[179] Fix | Delete
'wpseo_canonical' => 'canonical',
[180] Fix | Delete
'wpseo_focuskw' => 'primary_focus_keyword',
[181] Fix | Delete
'wpseo_title' => 'title',
[182] Fix | Delete
'wpseo_desc' => 'description',
[183] Fix | Delete
'wpseo_content_score' => 'readability_score',
[184] Fix | Delete
'wpseo_inclusive_language_score' => 'inclusive_language_score',
[185] Fix | Delete
'wpseo_bctitle' => 'breadcrumb_title',
[186] Fix | Delete
'wpseo_opengraph-title' => 'open_graph_title',
[187] Fix | Delete
'wpseo_opengraph-description' => 'open_graph_description',
[188] Fix | Delete
'wpseo_opengraph-image' => 'open_graph_image',
[189] Fix | Delete
'wpseo_opengraph-image-id' => 'open_graph_image_id',
[190] Fix | Delete
'wpseo_twitter-title' => 'twitter_title',
[191] Fix | Delete
'wpseo_twitter-description' => 'twitter_description',
[192] Fix | Delete
'wpseo_twitter-image' => 'twitter_image',
[193] Fix | Delete
'wpseo_twitter-image-id' => 'twitter_image_id',
[194] Fix | Delete
];
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
/**
[198] Fix | Delete
* Retrieves a meta value from the given meta data.
[199] Fix | Delete
*
[200] Fix | Delete
* @param string $meta_key The key to extract.
[201] Fix | Delete
* @param array $term_meta The meta data.
[202] Fix | Delete
*
[203] Fix | Delete
* @return string|null The meta value.
[204] Fix | Delete
*/
[205] Fix | Delete
protected function get_meta_value( $meta_key, $term_meta ) {
[206] Fix | Delete
if ( ! $term_meta || ! \array_key_exists( $meta_key, $term_meta ) ) {
[207] Fix | Delete
return null;
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
$value = $term_meta[ $meta_key ];
[211] Fix | Delete
if ( \is_string( $value ) && $value === '' ) {
[212] Fix | Delete
return null;
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
return $value;
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
/**
[219] Fix | Delete
* Finds an alternative image for the social image.
[220] Fix | Delete
*
[221] Fix | Delete
* @param Indexable $indexable The indexable.
[222] Fix | Delete
*
[223] Fix | Delete
* @return array|bool False when not found, array with data when found.
[224] Fix | Delete
*/
[225] Fix | Delete
protected function find_alternative_image( Indexable $indexable ) {
[226] Fix | Delete
$content_image = $this->image->get_term_content_image( $indexable->object_id );
[227] Fix | Delete
if ( $content_image ) {
[228] Fix | Delete
return [
[229] Fix | Delete
'image' => $content_image,
[230] Fix | Delete
'source' => 'first-content-image',
[231] Fix | Delete
];
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
return false;
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
/**
[238] Fix | Delete
* Returns the timestamps for a given term.
[239] Fix | Delete
*
[240] Fix | Delete
* @param int $term_id The term ID.
[241] Fix | Delete
* @param string $taxonomy The taxonomy.
[242] Fix | Delete
*
[243] Fix | Delete
* @return object An object with last_modified and published_at timestamps.
[244] Fix | Delete
*/
[245] Fix | Delete
protected function get_object_timestamps( $term_id, $taxonomy ) {
[246] Fix | Delete
global $wpdb;
[247] Fix | Delete
$post_statuses = $this->post_helper->get_public_post_statuses();
[248] Fix | Delete
[249] Fix | Delete
$replacements = [];
[250] Fix | Delete
$replacements[] = 'post_modified_gmt';
[251] Fix | Delete
$replacements[] = 'post_date_gmt';
[252] Fix | Delete
$replacements[] = $wpdb->posts;
[253] Fix | Delete
$replacements[] = $wpdb->term_relationships;
[254] Fix | Delete
$replacements[] = 'object_id';
[255] Fix | Delete
$replacements[] = 'ID';
[256] Fix | Delete
$replacements[] = $wpdb->term_taxonomy;
[257] Fix | Delete
$replacements[] = 'term_taxonomy_id';
[258] Fix | Delete
$replacements[] = 'term_taxonomy_id';
[259] Fix | Delete
$replacements[] = 'taxonomy';
[260] Fix | Delete
$replacements[] = $taxonomy;
[261] Fix | Delete
$replacements[] = 'term_id';
[262] Fix | Delete
$replacements[] = $term_id;
[263] Fix | Delete
$replacements[] = 'post_status';
[264] Fix | Delete
$replacements = \array_merge( $replacements, $post_statuses );
[265] Fix | Delete
$replacements[] = 'post_password';
[266] Fix | Delete
[267] Fix | Delete
//phpcs:disable WordPress.DB.PreparedSQLPlaceholders -- %i placeholder is still not recognized.
[268] Fix | Delete
//phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery -- Reason: Most performant way.
[269] Fix | Delete
//phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: No relevant caches.
[270] Fix | Delete
return $wpdb->get_row(
[271] Fix | Delete
$wpdb->prepare(
[272] Fix | Delete
'
[273] Fix | Delete
SELECT MAX(p.%i) AS last_modified, MIN(p.%i) AS published_at
[274] Fix | Delete
FROM %i AS p
[275] Fix | Delete
INNER JOIN %i AS term_rel
[276] Fix | Delete
ON term_rel.%i = p.%i
[277] Fix | Delete
INNER JOIN %i AS term_tax
[278] Fix | Delete
ON term_tax.%i = term_rel.%i
[279] Fix | Delete
AND term_tax.%i = %s
[280] Fix | Delete
AND term_tax.%i = %d
[281] Fix | Delete
WHERE p.%i IN (' . \implode( ', ', \array_fill( 0, \count( $post_statuses ), '%s' ) ) . ")
[282] Fix | Delete
AND p.%i = ''
[283] Fix | Delete
",
[284] Fix | Delete
$replacements
[285] Fix | Delete
)
[286] Fix | Delete
);
[287] Fix | Delete
//phpcs:enable
[288] Fix | Delete
}
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function