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-post-builder.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Builders;
[2] Fix | Delete
[3] Fix | Delete
use WP_Error;
[4] Fix | Delete
use WP_Post;
[5] Fix | Delete
use Yoast\WP\SEO\Exceptions\Indexable\Post_Not_Built_Exception;
[6] Fix | Delete
use Yoast\WP\SEO\Exceptions\Indexable\Post_Not_Found_Exception;
[7] Fix | Delete
use Yoast\WP\SEO\Helpers\Meta_Helper;
[8] Fix | Delete
use Yoast\WP\SEO\Helpers\Post_Helper;
[9] Fix | Delete
use Yoast\WP\SEO\Helpers\Post_Type_Helper;
[10] Fix | Delete
use Yoast\WP\SEO\Models\Indexable;
[11] Fix | Delete
use Yoast\WP\SEO\Repositories\Indexable_Repository;
[12] Fix | Delete
use Yoast\WP\SEO\Values\Indexables\Indexable_Builder_Versions;
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* Post Builder for the indexables.
[16] Fix | Delete
*
[17] Fix | Delete
* Formats the post meta to indexable format.
[18] Fix | Delete
*/
[19] Fix | Delete
class Indexable_Post_Builder {
[20] Fix | Delete
[21] Fix | Delete
use Indexable_Social_Image_Trait;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* The indexable repository.
[25] Fix | Delete
*
[26] Fix | Delete
* @var Indexable_Repository
[27] Fix | Delete
*/
[28] Fix | Delete
protected $indexable_repository;
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Holds the Post_Helper instance.
[32] Fix | Delete
*
[33] Fix | Delete
* @var Post_Helper
[34] Fix | Delete
*/
[35] Fix | Delete
protected $post_helper;
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* The post type helper.
[39] Fix | Delete
*
[40] Fix | Delete
* @var Post_Type_Helper
[41] Fix | Delete
*/
[42] Fix | Delete
protected $post_type_helper;
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Knows the latest version of the Indexable post builder type.
[46] Fix | Delete
*
[47] Fix | Delete
* @var int
[48] Fix | Delete
*/
[49] Fix | Delete
protected $version;
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* The meta helper.
[53] Fix | Delete
*
[54] Fix | Delete
* @var Meta_Helper
[55] Fix | Delete
*/
[56] Fix | Delete
protected $meta;
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Indexable_Post_Builder constructor.
[60] Fix | Delete
*
[61] Fix | Delete
* @param Post_Helper $post_helper The post helper.
[62] Fix | Delete
* @param Post_Type_Helper $post_type_helper The post type helper.
[63] Fix | Delete
* @param Indexable_Builder_Versions $versions The indexable builder versions.
[64] Fix | Delete
* @param Meta_Helper $meta The meta helper.
[65] Fix | Delete
*/
[66] Fix | Delete
public function __construct(
[67] Fix | Delete
Post_Helper $post_helper,
[68] Fix | Delete
Post_Type_Helper $post_type_helper,
[69] Fix | Delete
Indexable_Builder_Versions $versions,
[70] Fix | Delete
Meta_Helper $meta
[71] Fix | Delete
) {
[72] Fix | Delete
$this->post_helper = $post_helper;
[73] Fix | Delete
$this->post_type_helper = $post_type_helper;
[74] Fix | Delete
$this->version = $versions->get_latest_version_for_type( 'post' );
[75] Fix | Delete
$this->meta = $meta;
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Sets the indexable repository. Done to avoid circular dependencies.
[80] Fix | Delete
*
[81] Fix | Delete
* @required
[82] Fix | Delete
*
[83] Fix | Delete
* @param Indexable_Repository $indexable_repository The indexable repository.
[84] Fix | Delete
*
[85] Fix | Delete
* @return void
[86] Fix | Delete
*/
[87] Fix | Delete
public function set_indexable_repository( Indexable_Repository $indexable_repository ) {
[88] Fix | Delete
$this->indexable_repository = $indexable_repository;
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* Formats the data.
[93] Fix | Delete
*
[94] Fix | Delete
* @param int $post_id The post ID to use.
[95] Fix | Delete
* @param Indexable $indexable The indexable to format.
[96] Fix | Delete
*
[97] Fix | Delete
* @return bool|Indexable The extended indexable. False when unable to build.
[98] Fix | Delete
*
[99] Fix | Delete
* @throws Post_Not_Found_Exception When the post could not be found.
[100] Fix | Delete
* @throws Post_Not_Built_Exception When the post should not be indexed.
[101] Fix | Delete
*/
[102] Fix | Delete
public function build( $post_id, $indexable ) {
[103] Fix | Delete
if ( ! $this->post_helper->is_post_indexable( $post_id ) ) {
[104] Fix | Delete
throw Post_Not_Built_Exception::because_not_indexable( $post_id );
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
$post = $this->post_helper->get_post( $post_id );
[108] Fix | Delete
[109] Fix | Delete
if ( $post === null ) {
[110] Fix | Delete
throw new Post_Not_Found_Exception();
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
if ( $this->should_exclude_post( $post ) ) {
[114] Fix | Delete
throw Post_Not_Built_Exception::because_post_type_excluded( $post_id );
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
$indexable->object_id = $post_id;
[118] Fix | Delete
$indexable->object_type = 'post';
[119] Fix | Delete
$indexable->object_sub_type = $post->post_type;
[120] Fix | Delete
$indexable->permalink = $this->get_permalink( $post->post_type, $post_id );
[121] Fix | Delete
[122] Fix | Delete
$indexable->primary_focus_keyword_score = $this->get_keyword_score(
[123] Fix | Delete
$this->meta->get_value( 'focuskw', $post_id ),
[124] Fix | Delete
(int) $this->meta->get_value( 'linkdex', $post_id )
[125] Fix | Delete
);
[126] Fix | Delete
[127] Fix | Delete
$indexable->readability_score = (int) $this->meta->get_value( 'content_score', $post_id );
[128] Fix | Delete
[129] Fix | Delete
$indexable->inclusive_language_score = (int) $this->meta->get_value( 'inclusive_language_score', $post_id );
[130] Fix | Delete
[131] Fix | Delete
$indexable->is_cornerstone = ( $this->meta->get_value( 'is_cornerstone', $post_id ) === '1' );
[132] Fix | Delete
$indexable->is_robots_noindex = $this->get_robots_noindex(
[133] Fix | Delete
(int) $this->meta->get_value( 'meta-robots-noindex', $post_id )
[134] Fix | Delete
);
[135] Fix | Delete
[136] Fix | Delete
// Set additional meta-robots values.
[137] Fix | Delete
$indexable->is_robots_nofollow = ( $this->meta->get_value( 'meta-robots-nofollow', $post_id ) === '1' );
[138] Fix | Delete
$noindex_advanced = $this->meta->get_value( 'meta-robots-adv', $post_id );
[139] Fix | Delete
$meta_robots = \explode( ',', $noindex_advanced );
[140] Fix | Delete
[141] Fix | Delete
foreach ( $this->get_robots_options() as $meta_robots_option ) {
[142] Fix | Delete
$indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null;
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
$this->reset_social_images( $indexable );
[146] Fix | Delete
[147] Fix | Delete
foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) {
[148] Fix | Delete
$indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) );
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
if ( empty( $indexable->breadcrumb_title ) ) {
[152] Fix | Delete
$indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true );
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
$this->handle_social_images( $indexable );
[156] Fix | Delete
[157] Fix | Delete
$indexable->author_id = $post->post_author;
[158] Fix | Delete
$indexable->post_parent = $post->post_parent;
[159] Fix | Delete
[160] Fix | Delete
$indexable->number_of_pages = $this->get_number_of_pages_for_post( $post );
[161] Fix | Delete
$indexable->post_status = $post->post_status;
[162] Fix | Delete
$indexable->is_protected = $post->post_password !== '';
[163] Fix | Delete
$indexable->is_public = $this->is_public( $indexable );
[164] Fix | Delete
$indexable->has_public_posts = $this->has_public_posts( $indexable );
[165] Fix | Delete
$indexable->blog_id = \get_current_blog_id();
[166] Fix | Delete
[167] Fix | Delete
$indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) );
[168] Fix | Delete
$indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) );
[169] Fix | Delete
[170] Fix | Delete
$indexable->object_last_modified = $post->post_modified_gmt;
[171] Fix | Delete
$indexable->object_published_at = $post->post_date_gmt;
[172] Fix | Delete
[173] Fix | Delete
$indexable->version = $this->version;
[174] Fix | Delete
[175] Fix | Delete
return $indexable;
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
/**
[179] Fix | Delete
* Retrieves the permalink for a post with the given post type and ID.
[180] Fix | Delete
*
[181] Fix | Delete
* @param string $post_type The post type.
[182] Fix | Delete
* @param int $post_id The post ID.
[183] Fix | Delete
*
[184] Fix | Delete
* @return false|string|WP_Error The permalink.
[185] Fix | Delete
*/
[186] Fix | Delete
protected function get_permalink( $post_type, $post_id ) {
[187] Fix | Delete
if ( $post_type !== 'attachment' ) {
[188] Fix | Delete
return \get_permalink( $post_id );
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
return \wp_get_attachment_url( $post_id );
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
/**
[195] Fix | Delete
* Determines the value of is_public.
[196] Fix | Delete
*
[197] Fix | Delete
* @param Indexable $indexable The indexable.
[198] Fix | Delete
*
[199] Fix | Delete
* @return bool|null Whether or not the post type is public. Null if no override is set.
[200] Fix | Delete
*/
[201] Fix | Delete
protected function is_public( $indexable ) {
[202] Fix | Delete
if ( $indexable->is_protected === true ) {
[203] Fix | Delete
return false;
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
if ( $indexable->is_robots_noindex === true ) {
[207] Fix | Delete
return false;
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
// Attachments behave differently than the other post types, since they inherit from their parent.
[211] Fix | Delete
if ( $indexable->object_sub_type === 'attachment' ) {
[212] Fix | Delete
return $this->is_public_attachment( $indexable );
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) {
[216] Fix | Delete
return false;
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
if ( $indexable->is_robots_noindex === false ) {
[220] Fix | Delete
return true;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
return null;
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
/**
[227] Fix | Delete
* Determines the value of is_public for attachments.
[228] Fix | Delete
*
[229] Fix | Delete
* @param Indexable $indexable The indexable.
[230] Fix | Delete
*
[231] Fix | Delete
* @return bool|null False when it has no parent. Null when it has a parent.
[232] Fix | Delete
*/
[233] Fix | Delete
protected function is_public_attachment( $indexable ) {
[234] Fix | Delete
// If the attachment has no parent, it should not be public.
[235] Fix | Delete
if ( empty( $indexable->post_parent ) ) {
[236] Fix | Delete
return false;
[237] Fix | Delete
}
[238] Fix | Delete
[239] Fix | Delete
// If the attachment has a parent, the is_public should be NULL.
[240] Fix | Delete
return null;
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
/**
[244] Fix | Delete
* Determines the value of has_public_posts.
[245] Fix | Delete
*
[246] Fix | Delete
* @param Indexable $indexable The indexable.
[247] Fix | Delete
*
[248] Fix | Delete
* @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment.
[249] Fix | Delete
*/
[250] Fix | Delete
protected function has_public_posts( $indexable ) {
[251] Fix | Delete
// Only attachments (and authors) have this value.
[252] Fix | Delete
if ( $indexable->object_sub_type !== 'attachment' ) {
[253] Fix | Delete
return null;
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
// The attachment should have a post parent.
[257] Fix | Delete
if ( empty( $indexable->post_parent ) ) {
[258] Fix | Delete
return false;
[259] Fix | Delete
}
[260] Fix | Delete
[261] Fix | Delete
// The attachment should inherit the post status.
[262] Fix | Delete
if ( $indexable->post_status !== 'inherit' ) {
[263] Fix | Delete
return false;
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
// The post parent should be public.
[267] Fix | Delete
$post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' );
[268] Fix | Delete
if ( $post_parent_indexable !== false ) {
[269] Fix | Delete
return $post_parent_indexable->is_public;
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
return false;
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* Converts the meta robots noindex value to the indexable value.
[277] Fix | Delete
*
[278] Fix | Delete
* @param int $value Meta value to convert.
[279] Fix | Delete
*
[280] Fix | Delete
* @return bool|null True for noindex, false for index, null for default of parent/type.
[281] Fix | Delete
*/
[282] Fix | Delete
protected function get_robots_noindex( $value ) {
[283] Fix | Delete
$value = (int) $value;
[284] Fix | Delete
[285] Fix | Delete
switch ( $value ) {
[286] Fix | Delete
case 1:
[287] Fix | Delete
return true;
[288] Fix | Delete
case 2:
[289] Fix | Delete
return false;
[290] Fix | Delete
}
[291] Fix | Delete
[292] Fix | Delete
return null;
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
/**
[296] Fix | Delete
* Retrieves the robot options to search for.
[297] Fix | Delete
*
[298] Fix | Delete
* @return array List of robots values.
[299] Fix | Delete
*/
[300] Fix | Delete
protected function get_robots_options() {
[301] Fix | Delete
return [ 'noimageindex', 'noarchive', 'nosnippet' ];
[302] Fix | Delete
}
[303] Fix | Delete
[304] Fix | Delete
/**
[305] Fix | Delete
* Determines the focus keyword score.
[306] Fix | Delete
*
[307] Fix | Delete
* @param string $keyword The focus keyword that is set.
[308] Fix | Delete
* @param int $score The score saved on the meta data.
[309] Fix | Delete
*
[310] Fix | Delete
* @return int|null Score to use.
[311] Fix | Delete
*/
[312] Fix | Delete
protected function get_keyword_score( $keyword, $score ) {
[313] Fix | Delete
if ( empty( $keyword ) ) {
[314] Fix | Delete
return null;
[315] Fix | Delete
}
[316] Fix | Delete
[317] Fix | Delete
return $score;
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
/**
[321] Fix | Delete
* Retrieves the lookup table.
[322] Fix | Delete
*
[323] Fix | Delete
* @return array Lookup table for the indexable fields.
[324] Fix | Delete
*/
[325] Fix | Delete
protected function get_indexable_lookup() {
[326] Fix | Delete
return [
[327] Fix | Delete
'focuskw' => 'primary_focus_keyword',
[328] Fix | Delete
'canonical' => 'canonical',
[329] Fix | Delete
'title' => 'title',
[330] Fix | Delete
'metadesc' => 'description',
[331] Fix | Delete
'bctitle' => 'breadcrumb_title',
[332] Fix | Delete
'opengraph-title' => 'open_graph_title',
[333] Fix | Delete
'opengraph-image' => 'open_graph_image',
[334] Fix | Delete
'opengraph-image-id' => 'open_graph_image_id',
[335] Fix | Delete
'opengraph-description' => 'open_graph_description',
[336] Fix | Delete
'twitter-title' => 'twitter_title',
[337] Fix | Delete
'twitter-image' => 'twitter_image',
[338] Fix | Delete
'twitter-image-id' => 'twitter_image_id',
[339] Fix | Delete
'twitter-description' => 'twitter_description',
[340] Fix | Delete
'estimated-reading-time-minutes' => 'estimated_reading_time_minutes',
[341] Fix | Delete
];
[342] Fix | Delete
}
[343] Fix | Delete
[344] Fix | Delete
/**
[345] Fix | Delete
* Finds an alternative image for the social image.
[346] Fix | Delete
*
[347] Fix | Delete
* @param Indexable $indexable The indexable.
[348] Fix | Delete
*
[349] Fix | Delete
* @return array|bool False when not found, array with data when found.
[350] Fix | Delete
*/
[351] Fix | Delete
protected function find_alternative_image( Indexable $indexable ) {
[352] Fix | Delete
if (
[353] Fix | Delete
$indexable->object_sub_type === 'attachment'
[354] Fix | Delete
&& $this->image->is_valid_attachment( $indexable->object_id )
[355] Fix | Delete
) {
[356] Fix | Delete
return [
[357] Fix | Delete
'image_id' => $indexable->object_id,
[358] Fix | Delete
'source' => 'attachment-image',
[359] Fix | Delete
];
[360] Fix | Delete
}
[361] Fix | Delete
[362] Fix | Delete
$featured_image_id = $this->image->get_featured_image_id( $indexable->object_id );
[363] Fix | Delete
if ( $featured_image_id ) {
[364] Fix | Delete
return [
[365] Fix | Delete
'image_id' => $featured_image_id,
[366] Fix | Delete
'source' => 'featured-image',
[367] Fix | Delete
];
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
$gallery_image = $this->image->get_gallery_image( $indexable->object_id );
[371] Fix | Delete
if ( $gallery_image ) {
[372] Fix | Delete
return [
[373] Fix | Delete
'image' => $gallery_image,
[374] Fix | Delete
'source' => 'gallery-image',
[375] Fix | Delete
];
[376] Fix | Delete
}
[377] Fix | Delete
[378] Fix | Delete
$content_image = $this->image->get_post_content_image( $indexable->object_id );
[379] Fix | Delete
if ( $content_image ) {
[380] Fix | Delete
return [
[381] Fix | Delete
'image' => $content_image,
[382] Fix | Delete
'source' => 'first-content-image',
[383] Fix | Delete
];
[384] Fix | Delete
}
[385] Fix | Delete
[386] Fix | Delete
return false;
[387] Fix | Delete
}
[388] Fix | Delete
[389] Fix | Delete
/**
[390] Fix | Delete
* Gets the number of pages for a post.
[391] Fix | Delete
*
[392] Fix | Delete
* @param object $post The post object.
[393] Fix | Delete
*
[394] Fix | Delete
* @return int|null The number of pages or null if the post isn't paginated.
[395] Fix | Delete
*/
[396] Fix | Delete
protected function get_number_of_pages_for_post( $post ) {
[397] Fix | Delete
$number_of_pages = ( \substr_count( $post->post_content, '<!--nextpage-->' ) + 1 );
[398] Fix | Delete
[399] Fix | Delete
if ( $number_of_pages <= 1 ) {
[400] Fix | Delete
return null;
[401] Fix | Delete
}
[402] Fix | Delete
[403] Fix | Delete
return $number_of_pages;
[404] Fix | Delete
}
[405] Fix | Delete
[406] Fix | Delete
/**
[407] Fix | Delete
* Checks whether an indexable should be built for this post.
[408] Fix | Delete
*
[409] Fix | Delete
* @param WP_Post $post The post for which an indexable should be built.
[410] Fix | Delete
*
[411] Fix | Delete
* @return bool `true` if the post should be excluded from building, `false` if not.
[412] Fix | Delete
*/
[413] Fix | Delete
protected function should_exclude_post( $post ) {
[414] Fix | Delete
return $this->post_type_helper->is_excluded( $post->post_type );
[415] Fix | Delete
}
[416] Fix | Delete
[417] Fix | Delete
/**
[418] Fix | Delete
* Transforms an empty string into null. Leaves non-empty strings intact.
[419] Fix | Delete
*
[420] Fix | Delete
* @param string $text The string.
[421] Fix | Delete
*
[422] Fix | Delete
* @return string|null The input string or null.
[423] Fix | Delete
*/
[424] Fix | Delete
protected function empty_string_to_null( $text ) {
[425] Fix | Delete
if ( ! \is_string( $text ) || $text === '' ) {
[426] Fix | Delete
return null;
[427] Fix | Delete
}
[428] Fix | Delete
[429] Fix | Delete
return $text;
[430] Fix | Delete
}
[431] Fix | Delete
}
[432] Fix | Delete
[433] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function