Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../src/integrat.../third-pa...
File: woocommerce.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Integrations\Third_Party;
[2] Fix | Delete
[3] Fix | Delete
use WPSEO_Replace_Vars;
[4] Fix | Delete
use Yoast\WP\SEO\Conditionals\Front_End_Conditional;
[5] Fix | Delete
use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
[6] Fix | Delete
use Yoast\WP\SEO\Helpers\Options_Helper;
[7] Fix | Delete
use Yoast\WP\SEO\Helpers\Pagination_Helper;
[8] Fix | Delete
use Yoast\WP\SEO\Integrations\Integration_Interface;
[9] Fix | Delete
use Yoast\WP\SEO\Memoizers\Meta_Tags_Context_Memoizer;
[10] Fix | Delete
use Yoast\WP\SEO\Models\Indexable;
[11] Fix | Delete
use Yoast\WP\SEO\Presentations\Indexable_Presentation;
[12] Fix | Delete
use Yoast\WP\SEO\Repositories\Indexable_Repository;
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* WooCommerce integration.
[16] Fix | Delete
*/
[17] Fix | Delete
class WooCommerce implements Integration_Interface {
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* The options helper.
[21] Fix | Delete
*
[22] Fix | Delete
* @var Options_Helper
[23] Fix | Delete
*/
[24] Fix | Delete
private $options;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* The WPSEO Replace Vars object.
[28] Fix | Delete
*
[29] Fix | Delete
* @var WPSEO_Replace_Vars
[30] Fix | Delete
*/
[31] Fix | Delete
private $replace_vars;
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* The memoizer for the meta tags context.
[35] Fix | Delete
*
[36] Fix | Delete
* @var Meta_Tags_Context_Memoizer
[37] Fix | Delete
*/
[38] Fix | Delete
protected $context_memoizer;
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* The indexable repository.
[42] Fix | Delete
*
[43] Fix | Delete
* @var Indexable_Repository
[44] Fix | Delete
*/
[45] Fix | Delete
private $repository;
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* The pagination helper.
[49] Fix | Delete
*
[50] Fix | Delete
* @var Pagination_Helper
[51] Fix | Delete
*/
[52] Fix | Delete
protected $pagination_helper;
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* Returns the conditionals based in which this loadable should be active.
[56] Fix | Delete
*
[57] Fix | Delete
* @return array
[58] Fix | Delete
*/
[59] Fix | Delete
public static function get_conditionals() {
[60] Fix | Delete
return [ WooCommerce_Conditional::class, Front_End_Conditional::class ];
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
/**
[64] Fix | Delete
* WooCommerce constructor.
[65] Fix | Delete
*
[66] Fix | Delete
* @param Options_Helper $options The options helper.
[67] Fix | Delete
* @param WPSEO_Replace_Vars $replace_vars The replace vars helper.
[68] Fix | Delete
* @param Meta_Tags_Context_Memoizer $context_memoizer The meta tags context memoizer.
[69] Fix | Delete
* @param Indexable_Repository $repository The indexable repository.
[70] Fix | Delete
* @param Pagination_Helper $pagination_helper The paginataion helper.
[71] Fix | Delete
*/
[72] Fix | Delete
public function __construct(
[73] Fix | Delete
Options_Helper $options,
[74] Fix | Delete
WPSEO_Replace_Vars $replace_vars,
[75] Fix | Delete
Meta_Tags_Context_Memoizer $context_memoizer,
[76] Fix | Delete
Indexable_Repository $repository,
[77] Fix | Delete
Pagination_Helper $pagination_helper
[78] Fix | Delete
) {
[79] Fix | Delete
$this->options = $options;
[80] Fix | Delete
$this->replace_vars = $replace_vars;
[81] Fix | Delete
$this->context_memoizer = $context_memoizer;
[82] Fix | Delete
$this->repository = $repository;
[83] Fix | Delete
$this->pagination_helper = $pagination_helper;
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Initializes the integration.
[88] Fix | Delete
*
[89] Fix | Delete
* This is the place to register hooks and filters.
[90] Fix | Delete
*
[91] Fix | Delete
* @return void
[92] Fix | Delete
*/
[93] Fix | Delete
public function register_hooks() {
[94] Fix | Delete
\add_filter( 'wpseo_frontend_page_type_simple_page_id', [ $this, 'get_page_id' ] );
[95] Fix | Delete
\add_filter( 'wpseo_breadcrumb_indexables', [ $this, 'add_shop_to_breadcrumbs' ] );
[96] Fix | Delete
[97] Fix | Delete
\add_filter( 'wpseo_title', [ $this, 'title' ], 10, 2 );
[98] Fix | Delete
\add_filter( 'wpseo_metadesc', [ $this, 'description' ], 10, 2 );
[99] Fix | Delete
\add_filter( 'wpseo_canonical', [ $this, 'canonical' ], 10, 2 );
[100] Fix | Delete
\add_filter( 'wpseo_adjacent_rel_url', [ $this, 'adjacent_rel_url' ], 10, 3 );
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* Returns the correct canonical when WooCommerce is enabled.
[105] Fix | Delete
*
[106] Fix | Delete
* @param string $canonical The current canonical.
[107] Fix | Delete
* @param Indexable_Presentation|null $presentation The indexable presentation.
[108] Fix | Delete
*
[109] Fix | Delete
* @return string The correct canonical.
[110] Fix | Delete
*/
[111] Fix | Delete
public function canonical( $canonical, $presentation = null ) {
[112] Fix | Delete
if ( ! $this->is_shop_page() ) {
[113] Fix | Delete
return $canonical;
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
$url = $this->get_shop_paginated_link( 'curr', $presentation );
[117] Fix | Delete
[118] Fix | Delete
if ( $url ) {
[119] Fix | Delete
return $url;
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
return $canonical;
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
/**
[126] Fix | Delete
* Returns correct adjacent pages when WooCommerce is enabled.
[127] Fix | Delete
*
[128] Fix | Delete
* @param string $link The current link.
[129] Fix | Delete
* @param string $rel Link relationship, prev or next.
[130] Fix | Delete
* @param Indexable_Presentation|null $presentation The indexable presentation.
[131] Fix | Delete
*
[132] Fix | Delete
* @return string The correct link.
[133] Fix | Delete
*/
[134] Fix | Delete
public function adjacent_rel_url( $link, $rel, $presentation = null ) {
[135] Fix | Delete
if ( ! $this->is_shop_page() ) {
[136] Fix | Delete
return $link;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
if ( $rel !== 'next' && $rel !== 'prev' ) {
[140] Fix | Delete
return $link;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
$url = $this->get_shop_paginated_link( $rel, $presentation );
[144] Fix | Delete
[145] Fix | Delete
if ( $url ) {
[146] Fix | Delete
return $url;
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
return $link;
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
/**
[153] Fix | Delete
* Adds a breadcrumb for the shop page.
[154] Fix | Delete
*
[155] Fix | Delete
* @param Indexable[] $indexables The array with indexables.
[156] Fix | Delete
*
[157] Fix | Delete
* @return Indexable[] The indexables to be shown in the breadcrumbs, with the shop page added.
[158] Fix | Delete
*/
[159] Fix | Delete
public function add_shop_to_breadcrumbs( $indexables ) {
[160] Fix | Delete
$shop_page_id = $this->get_shop_page_id();
[161] Fix | Delete
[162] Fix | Delete
if ( $shop_page_id < 1 ) {
[163] Fix | Delete
return $indexables;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
foreach ( $indexables as $index => $indexable ) {
[167] Fix | Delete
if ( $indexable->object_type === 'post-type-archive' && $indexable->object_sub_type === 'product' ) {
[168] Fix | Delete
$indexables[ $index ] = $this->repository->find_by_id_and_type( $shop_page_id, 'post' );
[169] Fix | Delete
}
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
return $indexables;
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
/**
[176] Fix | Delete
* Returns the ID of the WooCommerce shop page when the currently opened page is the shop page.
[177] Fix | Delete
*
[178] Fix | Delete
* @param int $page_id The page id.
[179] Fix | Delete
*
[180] Fix | Delete
* @return int The Page ID of the shop.
[181] Fix | Delete
*/
[182] Fix | Delete
public function get_page_id( $page_id ) {
[183] Fix | Delete
if ( ! $this->is_shop_page() ) {
[184] Fix | Delete
return $page_id;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
return $this->get_shop_page_id();
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Handles the title.
[192] Fix | Delete
*
[193] Fix | Delete
* @param string $title The title.
[194] Fix | Delete
* @param Indexable_Presentation|null $presentation The indexable presentation.
[195] Fix | Delete
*
[196] Fix | Delete
* @return string The title to use.
[197] Fix | Delete
*/
[198] Fix | Delete
public function title( $title, $presentation = null ) {
[199] Fix | Delete
$presentation = $this->ensure_presentation( $presentation );
[200] Fix | Delete
[201] Fix | Delete
if ( $presentation->model->title ) {
[202] Fix | Delete
return $title;
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
if ( ! $this->is_shop_page() ) {
[206] Fix | Delete
return $title;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
if ( ! \is_archive() ) {
[210] Fix | Delete
return $title;
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
$shop_page_id = $this->get_shop_page_id();
[214] Fix | Delete
if ( $shop_page_id < 1 ) {
[215] Fix | Delete
return $title;
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
$product_template_title = $this->get_product_template( 'title-product', $shop_page_id );
[219] Fix | Delete
if ( $product_template_title ) {
[220] Fix | Delete
return $product_template_title;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
return $title;
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
/**
[227] Fix | Delete
* Handles the meta description.
[228] Fix | Delete
*
[229] Fix | Delete
* @param string $description The title.
[230] Fix | Delete
* @param Indexable_Presentation|null $presentation The indexable presentation.
[231] Fix | Delete
*
[232] Fix | Delete
* @return string The description to use.
[233] Fix | Delete
*/
[234] Fix | Delete
public function description( $description, $presentation = null ) {
[235] Fix | Delete
$presentation = $this->ensure_presentation( $presentation );
[236] Fix | Delete
[237] Fix | Delete
if ( $presentation->model->description ) {
[238] Fix | Delete
return $description;
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
if ( ! $this->is_shop_page() ) {
[242] Fix | Delete
return $description;
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
if ( ! \is_archive() ) {
[246] Fix | Delete
return $description;
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
$shop_page_id = $this->get_shop_page_id();
[250] Fix | Delete
if ( $shop_page_id < 1 ) {
[251] Fix | Delete
return $description;
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
$product_template_description = $this->get_product_template( 'metadesc-product', $shop_page_id );
[255] Fix | Delete
if ( $product_template_description ) {
[256] Fix | Delete
return $product_template_description;
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
return $description;
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
/**
[263] Fix | Delete
* Checks if the current page is a WooCommerce shop page.
[264] Fix | Delete
*
[265] Fix | Delete
* @return bool True when the page is a shop page.
[266] Fix | Delete
*/
[267] Fix | Delete
protected function is_shop_page() {
[268] Fix | Delete
if ( ! \is_shop() ) {
[269] Fix | Delete
return false;
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
if ( \is_search() ) {
[273] Fix | Delete
return false;
[274] Fix | Delete
}
[275] Fix | Delete
[276] Fix | Delete
return true;
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
/**
[280] Fix | Delete
* Uses template for the given option name and replace the replacement variables on it.
[281] Fix | Delete
*
[282] Fix | Delete
* @param string $option_name The option name to get the template for.
[283] Fix | Delete
* @param string $shop_page_id The page id to retrieve template for.
[284] Fix | Delete
*
[285] Fix | Delete
* @return string The rendered value.
[286] Fix | Delete
*/
[287] Fix | Delete
protected function get_product_template( $option_name, $shop_page_id ) {
[288] Fix | Delete
$template = $this->options->get( $option_name );
[289] Fix | Delete
$page = \get_post( $shop_page_id );
[290] Fix | Delete
[291] Fix | Delete
return $this->replace_vars->replace( $template, $page );
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
/**
[295] Fix | Delete
* Returns the id of the set WooCommerce shop page.
[296] Fix | Delete
*
[297] Fix | Delete
* @return int The ID of the set page.
[298] Fix | Delete
*/
[299] Fix | Delete
protected function get_shop_page_id() {
[300] Fix | Delete
if ( ! \function_exists( 'wc_get_page_id' ) ) {
[301] Fix | Delete
return -1;
[302] Fix | Delete
}
[303] Fix | Delete
[304] Fix | Delete
return \wc_get_page_id( 'shop' );
[305] Fix | Delete
}
[306] Fix | Delete
[307] Fix | Delete
/**
[308] Fix | Delete
* Get paginated link for shop page.
[309] Fix | Delete
*
[310] Fix | Delete
* @param string $rel Link relationship, prev or next or curr.
[311] Fix | Delete
* @param Indexable_Presentation|null $presentation The indexable presentation.
[312] Fix | Delete
*
[313] Fix | Delete
* @return string|null The link.
[314] Fix | Delete
*/
[315] Fix | Delete
protected function get_shop_paginated_link( $rel, $presentation = null ) {
[316] Fix | Delete
$presentation = $this->ensure_presentation( $presentation );
[317] Fix | Delete
[318] Fix | Delete
$permalink = $presentation->get_permalink();
[319] Fix | Delete
if ( ! $permalink ) {
[320] Fix | Delete
return null;
[321] Fix | Delete
}
[322] Fix | Delete
[323] Fix | Delete
$current_page = \max( 1, $this->pagination_helper->get_current_archive_page_number() );
[324] Fix | Delete
[325] Fix | Delete
if ( $rel === 'curr' && $current_page === 1 ) {
[326] Fix | Delete
return $permalink;
[327] Fix | Delete
}
[328] Fix | Delete
[329] Fix | Delete
if ( $rel === 'curr' && $current_page > 1 ) {
[330] Fix | Delete
return $this->pagination_helper->get_paginated_url( $permalink, $current_page );
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
if ( $rel === 'prev' && $current_page === 2 ) {
[334] Fix | Delete
return $permalink;
[335] Fix | Delete
}
[336] Fix | Delete
[337] Fix | Delete
if ( $rel === 'prev' && $current_page > 2 ) {
[338] Fix | Delete
return $this->pagination_helper->get_paginated_url( $permalink, ( $current_page - 1 ) );
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
if ( $rel === 'next' && $current_page < $this->pagination_helper->get_number_of_archive_pages() ) {
[342] Fix | Delete
return $this->pagination_helper->get_paginated_url( $permalink, ( $current_page + 1 ) );
[343] Fix | Delete
}
[344] Fix | Delete
[345] Fix | Delete
return null;
[346] Fix | Delete
}
[347] Fix | Delete
[348] Fix | Delete
/**
[349] Fix | Delete
* Ensures a presentation is available.
[350] Fix | Delete
*
[351] Fix | Delete
* @param Indexable_Presentation $presentation The indexable presentation.
[352] Fix | Delete
*
[353] Fix | Delete
* @return Indexable_Presentation The presentation, taken from the current page if the input was invalid.
[354] Fix | Delete
*/
[355] Fix | Delete
protected function ensure_presentation( $presentation ) {
[356] Fix | Delete
if ( \is_a( $presentation, Indexable_Presentation::class ) ) {
[357] Fix | Delete
return $presentation;
[358] Fix | Delete
}
[359] Fix | Delete
[360] Fix | Delete
$context = $this->context_memoizer->for_current_page();
[361] Fix | Delete
[362] Fix | Delete
return $context->presentation;
[363] Fix | Delete
}
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function