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-link-builder.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Builders;
[2] Fix | Delete
[3] Fix | Delete
use DOMDocument;
[4] Fix | Delete
use WP_HTML_Tag_Processor;
[5] Fix | Delete
use WPSEO_Image_Utils;
[6] Fix | Delete
use Yoast\WP\SEO\Helpers\Image_Helper;
[7] Fix | Delete
use Yoast\WP\SEO\Helpers\Indexable_Helper;
[8] Fix | Delete
use Yoast\WP\SEO\Helpers\Options_Helper;
[9] Fix | Delete
use Yoast\WP\SEO\Helpers\Post_Helper;
[10] Fix | Delete
use Yoast\WP\SEO\Helpers\Url_Helper;
[11] Fix | Delete
use Yoast\WP\SEO\Models\Indexable;
[12] Fix | Delete
use Yoast\WP\SEO\Models\SEO_Links;
[13] Fix | Delete
use Yoast\WP\SEO\Repositories\Indexable_Repository;
[14] Fix | Delete
use Yoast\WP\SEO\Repositories\SEO_Links_Repository;
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* Indexable link builder.
[18] Fix | Delete
*/
[19] Fix | Delete
class Indexable_Link_Builder {
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* The SEO links repository.
[23] Fix | Delete
*
[24] Fix | Delete
* @var SEO_Links_Repository
[25] Fix | Delete
*/
[26] Fix | Delete
protected $seo_links_repository;
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* The url helper.
[30] Fix | Delete
*
[31] Fix | Delete
* @var Url_Helper
[32] Fix | Delete
*/
[33] Fix | Delete
protected $url_helper;
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* The image helper.
[37] Fix | Delete
*
[38] Fix | Delete
* @var Image_Helper
[39] Fix | Delete
*/
[40] Fix | Delete
protected $image_helper;
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* The indexable helper.
[44] Fix | Delete
*
[45] Fix | Delete
* @var Indexable_Helper
[46] Fix | Delete
*/
[47] Fix | Delete
protected $indexable_helper;
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* The post helper.
[51] Fix | Delete
*
[52] Fix | Delete
* @var Post_Helper
[53] Fix | Delete
*/
[54] Fix | Delete
protected $post_helper;
[55] Fix | Delete
[56] Fix | Delete
/**
[57] Fix | Delete
* The options helper.
[58] Fix | Delete
*
[59] Fix | Delete
* @var Options_Helper
[60] Fix | Delete
*/
[61] Fix | Delete
protected $options_helper;
[62] Fix | Delete
[63] Fix | Delete
/**
[64] Fix | Delete
* The indexable repository.
[65] Fix | Delete
*
[66] Fix | Delete
* @var Indexable_Repository
[67] Fix | Delete
*/
[68] Fix | Delete
protected $indexable_repository;
[69] Fix | Delete
[70] Fix | Delete
/**
[71] Fix | Delete
* Indexable_Link_Builder constructor.
[72] Fix | Delete
*
[73] Fix | Delete
* @param SEO_Links_Repository $seo_links_repository The SEO links repository.
[74] Fix | Delete
* @param Url_Helper $url_helper The URL helper.
[75] Fix | Delete
* @param Post_Helper $post_helper The post helper.
[76] Fix | Delete
* @param Options_Helper $options_helper The options helper.
[77] Fix | Delete
* @param Indexable_Helper $indexable_helper The indexable helper.
[78] Fix | Delete
*/
[79] Fix | Delete
public function __construct(
[80] Fix | Delete
SEO_Links_Repository $seo_links_repository,
[81] Fix | Delete
Url_Helper $url_helper,
[82] Fix | Delete
Post_Helper $post_helper,
[83] Fix | Delete
Options_Helper $options_helper,
[84] Fix | Delete
Indexable_Helper $indexable_helper
[85] Fix | Delete
) {
[86] Fix | Delete
$this->seo_links_repository = $seo_links_repository;
[87] Fix | Delete
$this->url_helper = $url_helper;
[88] Fix | Delete
$this->post_helper = $post_helper;
[89] Fix | Delete
$this->options_helper = $options_helper;
[90] Fix | Delete
$this->indexable_helper = $indexable_helper;
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
/**
[94] Fix | Delete
* Sets the indexable repository.
[95] Fix | Delete
*
[96] Fix | Delete
* @required
[97] Fix | Delete
*
[98] Fix | Delete
* @param Indexable_Repository $indexable_repository The indexable repository.
[99] Fix | Delete
* @param Image_Helper $image_helper The image helper.
[100] Fix | Delete
*
[101] Fix | Delete
* @return void
[102] Fix | Delete
*/
[103] Fix | Delete
public function set_dependencies(
[104] Fix | Delete
Indexable_Repository $indexable_repository,
[105] Fix | Delete
Image_Helper $image_helper
[106] Fix | Delete
) {
[107] Fix | Delete
$this->indexable_repository = $indexable_repository;
[108] Fix | Delete
$this->image_helper = $image_helper;
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Builds the links for a post.
[113] Fix | Delete
*
[114] Fix | Delete
* @param Indexable $indexable The indexable.
[115] Fix | Delete
* @param string $content The content. Expected to be unfiltered.
[116] Fix | Delete
*
[117] Fix | Delete
* @return SEO_Links[] The created SEO links.
[118] Fix | Delete
*/
[119] Fix | Delete
public function build( $indexable, $content ) {
[120] Fix | Delete
if ( ! $this->indexable_helper->should_index_indexable( $indexable ) ) {
[121] Fix | Delete
return [];
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
global $post;
[125] Fix | Delete
if ( $indexable->object_type === 'post' ) {
[126] Fix | Delete
$post_backup = $post;
[127] Fix | Delete
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
[128] Fix | Delete
$post = $this->post_helper->get_post( $indexable->object_id );
[129] Fix | Delete
\setup_postdata( $post );
[130] Fix | Delete
$content = \apply_filters( 'the_content', $content );
[131] Fix | Delete
\wp_reset_postdata();
[132] Fix | Delete
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- To setup the post we need to do this explicitly.
[133] Fix | Delete
$post = $post_backup;
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
$content = \str_replace( ']]>', ']]&gt;', $content );
[137] Fix | Delete
$links = $this->gather_links( $content );
[138] Fix | Delete
$images = $this->gather_images( $content );
[139] Fix | Delete
[140] Fix | Delete
if ( empty( $links ) && empty( $images ) ) {
[141] Fix | Delete
$indexable->link_count = 0;
[142] Fix | Delete
$this->update_related_indexables( $indexable, [] );
[143] Fix | Delete
[144] Fix | Delete
return [];
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
$links = $this->create_links( $indexable, $links, $images );
[148] Fix | Delete
[149] Fix | Delete
$this->update_related_indexables( $indexable, $links );
[150] Fix | Delete
[151] Fix | Delete
$indexable->link_count = $this->get_internal_link_count( $links );
[152] Fix | Delete
[153] Fix | Delete
return $links;
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
/**
[157] Fix | Delete
* Deletes all SEO links for an indexable.
[158] Fix | Delete
*
[159] Fix | Delete
* @param Indexable $indexable The indexable.
[160] Fix | Delete
*
[161] Fix | Delete
* @return void
[162] Fix | Delete
*/
[163] Fix | Delete
public function delete( $indexable ) {
[164] Fix | Delete
$links = ( $this->seo_links_repository->find_all_by_indexable_id( $indexable->id ) );
[165] Fix | Delete
$this->seo_links_repository->delete_all_by_indexable_id( $indexable->id );
[166] Fix | Delete
[167] Fix | Delete
$linked_indexable_ids = [];
[168] Fix | Delete
foreach ( $links as $link ) {
[169] Fix | Delete
if ( $link->target_indexable_id ) {
[170] Fix | Delete
$linked_indexable_ids[] = $link->target_indexable_id;
[171] Fix | Delete
}
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
$this->update_incoming_links_for_related_indexables( $linked_indexable_ids );
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
/**
[178] Fix | Delete
* Fixes existing SEO links that are supposed to have a target indexable but don't, because of prior indexable
[179] Fix | Delete
* cleanup.
[180] Fix | Delete
*
[181] Fix | Delete
* @param Indexable $indexable The indexable to be the target of SEO Links.
[182] Fix | Delete
*
[183] Fix | Delete
* @return void
[184] Fix | Delete
*/
[185] Fix | Delete
public function patch_seo_links( Indexable $indexable ) {
[186] Fix | Delete
if ( ! empty( $indexable->id ) && ! empty( $indexable->object_id ) ) {
[187] Fix | Delete
$links = $this->seo_links_repository->find_all_by_target_post_id( $indexable->object_id );
[188] Fix | Delete
[189] Fix | Delete
$updated_indexable = false;
[190] Fix | Delete
foreach ( $links as $link ) {
[191] Fix | Delete
if ( \is_a( $link, SEO_Links::class ) && empty( $link->target_indexable_id ) ) {
[192] Fix | Delete
// Since that post ID exists in an SEO link but has no target_indexable_id, it's probably because of prior indexable cleanup.
[193] Fix | Delete
$this->seo_links_repository->update_target_indexable_id( $link->id, $indexable->id );
[194] Fix | Delete
$updated_indexable = true;
[195] Fix | Delete
}
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
if ( $updated_indexable ) {
[199] Fix | Delete
$updated_indexable_id = [ $indexable->id ];
[200] Fix | Delete
$this->update_incoming_links_for_related_indexables( $updated_indexable_id );
[201] Fix | Delete
}
[202] Fix | Delete
}
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
/**
[206] Fix | Delete
* Gathers all links from content.
[207] Fix | Delete
*
[208] Fix | Delete
* @param string $content The content.
[209] Fix | Delete
*
[210] Fix | Delete
* @return string[] An array of urls.
[211] Fix | Delete
*/
[212] Fix | Delete
protected function gather_links( $content ) {
[213] Fix | Delete
if ( \strpos( $content, 'href' ) === false ) {
[214] Fix | Delete
// Nothing to do.
[215] Fix | Delete
return [];
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
$links = [];
[219] Fix | Delete
$regexp = '<a\s[^>]*href=("??)([^" >]*?)\1[^>]*>';
[220] Fix | Delete
// Used modifiers iU to match case insensitive and make greedy quantifiers lazy.
[221] Fix | Delete
if ( \preg_match_all( "/$regexp/iU", $content, $matches, \PREG_SET_ORDER ) ) {
[222] Fix | Delete
foreach ( $matches as $match ) {
[223] Fix | Delete
$links[] = \trim( $match[2], "'" );
[224] Fix | Delete
}
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
return $links;
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
/**
[231] Fix | Delete
* Gathers all images from content with WP's WP_HTML_Tag_Processor() and returns them along with their IDs, if
[232] Fix | Delete
* possible.
[233] Fix | Delete
*
[234] Fix | Delete
* @param string $content The content.
[235] Fix | Delete
*
[236] Fix | Delete
* @return int[] An associated array of image IDs, keyed by their URL.
[237] Fix | Delete
*/
[238] Fix | Delete
protected function gather_images_wp( $content ) {
[239] Fix | Delete
$processor = new WP_HTML_Tag_Processor( $content );
[240] Fix | Delete
$images = [];
[241] Fix | Delete
[242] Fix | Delete
$query = [
[243] Fix | Delete
'tag_name' => 'img',
[244] Fix | Delete
];
[245] Fix | Delete
[246] Fix | Delete
/**
[247] Fix | Delete
* Filter 'wpseo_image_attribute_containing_id' - Allows filtering what attribute will be used to extract image IDs from.
[248] Fix | Delete
*
[249] Fix | Delete
* Defaults to "class", which is where WP natively stores the image IDs, in a `wp-image-<ID>` format.
[250] Fix | Delete
*
[251] Fix | Delete
* @api string The attribute to be used to extract image IDs from.
[252] Fix | Delete
*/
[253] Fix | Delete
$attribute = \apply_filters( 'wpseo_image_attribute_containing_id', 'class' );
[254] Fix | Delete
[255] Fix | Delete
while ( $processor->next_tag( $query ) ) {
[256] Fix | Delete
$src = \htmlentities( $processor->get_attribute( 'src' ), ( \ENT_QUOTES | \ENT_SUBSTITUTE | \ENT_HTML401 ), \get_bloginfo( 'charset' ) );
[257] Fix | Delete
$classes = $processor->get_attribute( $attribute );
[258] Fix | Delete
$id = $this->extract_id_of_classes( $classes );
[259] Fix | Delete
[260] Fix | Delete
$images[ $src ] = $id;
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
return $images;
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
/**
[267] Fix | Delete
* Gathers all images from content with DOMDocument() and returns them along with their IDs, if possible.
[268] Fix | Delete
*
[269] Fix | Delete
* @param string $content The content.
[270] Fix | Delete
*
[271] Fix | Delete
* @return int[] An associated array of image IDs, keyed by their URL.
[272] Fix | Delete
*/
[273] Fix | Delete
protected function gather_images_domdocument( $content ) {
[274] Fix | Delete
$images = [];
[275] Fix | Delete
$charset = \get_bloginfo( 'charset' );
[276] Fix | Delete
[277] Fix | Delete
/**
[278] Fix | Delete
* Filter 'wpseo_image_attribute_containing_id' - Allows filtering what attribute will be used to extract image IDs from.
[279] Fix | Delete
*
[280] Fix | Delete
* Defaults to "class", which is where WP natively stores the image IDs, in a `wp-image-<ID>` format.
[281] Fix | Delete
*
[282] Fix | Delete
* @api string The attribute to be used to extract image IDs from.
[283] Fix | Delete
*/
[284] Fix | Delete
$attribute = \apply_filters( 'wpseo_image_attribute_containing_id', 'class' );
[285] Fix | Delete
[286] Fix | Delete
\libxml_use_internal_errors( true );
[287] Fix | Delete
$post_dom = new DOMDocument();
[288] Fix | Delete
$post_dom->loadHTML( '<?xml encoding="' . $charset . '">' . $content );
[289] Fix | Delete
\libxml_clear_errors();
[290] Fix | Delete
[291] Fix | Delete
foreach ( $post_dom->getElementsByTagName( 'img' ) as $img ) {
[292] Fix | Delete
$src = \htmlentities( $img->getAttribute( 'src' ), ( \ENT_QUOTES | \ENT_SUBSTITUTE | \ENT_HTML401 ), $charset );
[293] Fix | Delete
$classes = $img->getAttribute( $attribute );
[294] Fix | Delete
$id = $this->extract_id_of_classes( $classes );
[295] Fix | Delete
[296] Fix | Delete
$images[ $src ] = $id;
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
return $images;
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
/**
[303] Fix | Delete
* Extracts image ID out of the image's classes.
[304] Fix | Delete
*
[305] Fix | Delete
* @param string $classes The classes assigned to the image.
[306] Fix | Delete
*
[307] Fix | Delete
* @return int The ID that's extracted from the classes.
[308] Fix | Delete
*/
[309] Fix | Delete
protected function extract_id_of_classes( $classes ) {
[310] Fix | Delete
if ( ! $classes ) {
[311] Fix | Delete
return 0;
[312] Fix | Delete
}
[313] Fix | Delete
[314] Fix | Delete
/**
[315] Fix | Delete
* Filter 'wpseo_extract_id_pattern' - Allows filtering the regex patern to be used to extract image IDs from class/attribute names.
[316] Fix | Delete
*
[317] Fix | Delete
* Defaults to the pattern that extracts image IDs from core's `wp-image-<ID>` native format in image classes.
[318] Fix | Delete
*
[319] Fix | Delete
* @api string The regex pattern to be used to extract image IDs from class names. Empty string if the whole class/attribute should be returned.
[320] Fix | Delete
*/
[321] Fix | Delete
$pattern = \apply_filters( 'wpseo_extract_id_pattern', '/(?<!\S)wp-image-(\d+)(?!\S)/i' );
[322] Fix | Delete
[323] Fix | Delete
if ( $pattern === '' ) {
[324] Fix | Delete
return (int) $classes;
[325] Fix | Delete
}
[326] Fix | Delete
[327] Fix | Delete
$matches = [];
[328] Fix | Delete
[329] Fix | Delete
if ( \preg_match( $pattern, $classes, $matches ) ) {
[330] Fix | Delete
return (int) $matches[1];
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
return 0;
[334] Fix | Delete
}
[335] Fix | Delete
[336] Fix | Delete
/**
[337] Fix | Delete
* Gathers all images from content.
[338] Fix | Delete
*
[339] Fix | Delete
* @param string $content The content.
[340] Fix | Delete
*
[341] Fix | Delete
* @return int[] An associated array of image IDs, keyed by their URLs.
[342] Fix | Delete
*/
[343] Fix | Delete
protected function gather_images( $content ) {
[344] Fix | Delete
[345] Fix | Delete
/**
[346] Fix | Delete
* Filter 'wpseo_force_creating_and_using_attachment_indexables' - Filters if we should use attachment indexables to find all content images. Instead of scanning the content.
[347] Fix | Delete
*
[348] Fix | Delete
* The default value is false.
[349] Fix | Delete
*
[350] Fix | Delete
* @since 21.1
[351] Fix | Delete
*/
[352] Fix | Delete
$should_not_parse_content = \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false );
[353] Fix | Delete
[354] Fix | Delete
/**
[355] Fix | Delete
* Filter 'wpseo_force_skip_image_content_parsing' - Filters if we should force skip scanning the content to parse images.
[356] Fix | Delete
* This filter can be used if the regex gives a faster result than scanning the code.
[357] Fix | Delete
*
[358] Fix | Delete
* The default value is false.
[359] Fix | Delete
*
[360] Fix | Delete
* @since 21.1
[361] Fix | Delete
*/
[362] Fix | Delete
$should_not_parse_content = \apply_filters( 'wpseo_force_skip_image_content_parsing', $should_not_parse_content );
[363] Fix | Delete
if ( ! $should_not_parse_content && \class_exists( WP_HTML_Tag_Processor::class ) ) {
[364] Fix | Delete
return $this->gather_images_wp( $content );
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
if ( ! $should_not_parse_content && \class_exists( DOMDocument::class ) ) {
[368] Fix | Delete
return $this->gather_images_DOMDocument( $content );
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
if ( \strpos( $content, 'src' ) === false ) {
[372] Fix | Delete
// Nothing to do.
[373] Fix | Delete
return [];
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
$images = [];
[377] Fix | Delete
$regexp = '<img\s[^>]*src=("??)([^" >]*?)\\1[^>]*>';
[378] Fix | Delete
// Used modifiers iU to match case insensitive and make greedy quantifiers lazy.
[379] Fix | Delete
if ( \preg_match_all( "/$regexp/iU", $content, $matches, \PREG_SET_ORDER ) ) {
[380] Fix | Delete
foreach ( $matches as $match ) {
[381] Fix | Delete
$images[ $match[2] ] = 0;
[382] Fix | Delete
}
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
return $images;
[386] Fix | Delete
}
[387] Fix | Delete
[388] Fix | Delete
/**
[389] Fix | Delete
* Creates link models from lists of URLs and image sources.
[390] Fix | Delete
*
[391] Fix | Delete
* @param Indexable $indexable The indexable.
[392] Fix | Delete
* @param string[] $links The link URLs.
[393] Fix | Delete
* @param int[] $images The image sources.
[394] Fix | Delete
*
[395] Fix | Delete
* @return SEO_Links[] The link models.
[396] Fix | Delete
*/
[397] Fix | Delete
protected function create_links( $indexable, $links, $images ) {
[398] Fix | Delete
$home_url = \wp_parse_url( \home_url() );
[399] Fix | Delete
$current_url = \wp_parse_url( $indexable->permalink );
[400] Fix | Delete
$links = \array_map(
[401] Fix | Delete
function ( $link ) use ( $home_url, $indexable ) {
[402] Fix | Delete
return $this->create_internal_link( $link, $home_url, $indexable );
[403] Fix | Delete
},
[404] Fix | Delete
$links
[405] Fix | Delete
);
[406] Fix | Delete
// Filter out links to the same page with a fragment or query.
[407] Fix | Delete
$links = \array_filter(
[408] Fix | Delete
$links,
[409] Fix | Delete
function ( $link ) use ( $current_url ) {
[410] Fix | Delete
return $this->filter_link( $link, $current_url );
[411] Fix | Delete
}
[412] Fix | Delete
);
[413] Fix | Delete
[414] Fix | Delete
$image_links = [];
[415] Fix | Delete
foreach ( $images as $image_url => $image_id ) {
[416] Fix | Delete
$image_links[] = $this->create_internal_link( $image_url, $home_url, $indexable, true, $image_id );
[417] Fix | Delete
}
[418] Fix | Delete
[419] Fix | Delete
return \array_merge( $links, $image_links );
[420] Fix | Delete
}
[421] Fix | Delete
[422] Fix | Delete
/**
[423] Fix | Delete
* Get the post ID based on the link's type and its target's permalink.
[424] Fix | Delete
*
[425] Fix | Delete
* @param string $type The type of link (either SEO_Links::TYPE_INTERNAL or SEO_Links::TYPE_INTERNAL_IMAGE).
[426] Fix | Delete
* @param string $permalink The permalink of the link's target.
[427] Fix | Delete
*
[428] Fix | Delete
* @return int The post ID.
[429] Fix | Delete
*/
[430] Fix | Delete
protected function get_post_id( $type, $permalink ) {
[431] Fix | Delete
if ( $type === SEO_Links::TYPE_INTERNAL ) {
[432] Fix | Delete
return \url_to_postid( $permalink );
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
return $this->image_helper->get_attachment_by_url( $permalink );
[436] Fix | Delete
}
[437] Fix | Delete
[438] Fix | Delete
/**
[439] Fix | Delete
* Creates an internal link.
[440] Fix | Delete
*
[441] Fix | Delete
* @param string $url The url of the link.
[442] Fix | Delete
* @param array $home_url The home url, as parsed by wp_parse_url.
[443] Fix | Delete
* @param Indexable $indexable The indexable of the post containing the link.
[444] Fix | Delete
* @param bool $is_image Whether or not the link is an image.
[445] Fix | Delete
* @param int $image_id The ID of the internal image.
[446] Fix | Delete
*
[447] Fix | Delete
* @return SEO_Links The created link.
[448] Fix | Delete
*/
[449] Fix | Delete
protected function create_internal_link( $url, $home_url, $indexable, $is_image = false, $image_id = 0 ) {
[450] Fix | Delete
$parsed_url = \wp_parse_url( $url );
[451] Fix | Delete
$link_type = $this->url_helper->get_link_type( $parsed_url, $home_url, $is_image );
[452] Fix | Delete
[453] Fix | Delete
/**
[454] Fix | Delete
* ORM representing a link in the SEO Links table.
[455] Fix | Delete
*
[456] Fix | Delete
* @var SEO_Links $model
[457] Fix | Delete
*/
[458] Fix | Delete
$model = $this->seo_links_repository->query()->create(
[459] Fix | Delete
[
[460] Fix | Delete
'url' => $url,
[461] Fix | Delete
'type' => $link_type,
[462] Fix | Delete
'indexable_id' => $indexable->id,
[463] Fix | Delete
'post_id' => $indexable->object_id,
[464] Fix | Delete
]
[465] Fix | Delete
);
[466] Fix | Delete
[467] Fix | Delete
$model->parsed_url = $parsed_url;
[468] Fix | Delete
[469] Fix | Delete
if ( $model->type === SEO_Links::TYPE_INTERNAL ) {
[470] Fix | Delete
$permalink = $this->build_permalink( $url, $home_url );
[471] Fix | Delete
[472] Fix | Delete
return $this->enhance_link_from_indexable( $model, $permalink );
[473] Fix | Delete
}
[474] Fix | Delete
[475] Fix | Delete
if ( $model->type === SEO_Links::TYPE_INTERNAL_IMAGE ) {
[476] Fix | Delete
$permalink = $this->build_permalink( $url, $home_url );
[477] Fix | Delete
[478] Fix | Delete
/** The `wpseo_force_creating_and_using_attachment_indexables` filter is documented in indexable-link-builder.php */
[479] Fix | Delete
if ( ! $this->options_helper->get( 'disable-attachment' ) || \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false ) ) {
[480] Fix | Delete
$model = $this->enhance_link_from_indexable( $model, $permalink );
[481] Fix | Delete
}
[482] Fix | Delete
else {
[483] Fix | Delete
$target_post_id = ( $image_id !== 0 ) ? $image_id : WPSEO_Image_Utils::get_attachment_by_url( $permalink );
[484] Fix | Delete
[485] Fix | Delete
if ( ! empty( $target_post_id ) ) {
[486] Fix | Delete
$model->target_post_id = $target_post_id;
[487] Fix | Delete
}
[488] Fix | Delete
}
[489] Fix | Delete
[490] Fix | Delete
if ( $model->target_post_id ) {
[491] Fix | Delete
$file = \get_attached_file( $model->target_post_id );
[492] Fix | Delete
[493] Fix | Delete
if ( $file ) {
[494] Fix | Delete
if ( \file_exists( $file ) ) {
[495] Fix | Delete
$model->size = \filesize( $file );
[496] Fix | Delete
}
[497] Fix | Delete
else {
[498] Fix | Delete
$model->size = null;
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function