PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.4
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / woocommerce-product-category-permalink-integration.php

woocommerce-product-category-permalink-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.4, at src/integrations/woocommerce-product-category-permalink-integration.php

77 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Integrations;
4
5 use WP_Term;
6 use Yoast\WP\SEO\Conditionals\Dynamic_Product_Permalinks_Conditional;
7 use Yoast\WP\SEO\Conditionals\Woo_SEO_Inactive_Conditional;
8 use Yoast\WP\SEO\Conditionals\WooCommerce_Version_Conditional;
9
10 /**
11 * Integration for WooCommerce product category permalink handling.
12 */
13 class Woocommerce_Product_Category_Permalink_Integration implements Integration_Interface {
14
15 /**
16 * Holds the Dynamic_Product_Permalinks_Conditional.
17 *
18 * @var Dynamic_Product_Permalinks_Conditional
19 */
20 private $dynamic_product_permalinks_conditional;
21
22 /**
23 * Returns the conditionals based on which this loadable should be active.
24 *
25 * @return array<string> The array of conditionals.
26 */
27 public static function get_conditionals() {
28 return [
29 WooCommerce_Version_Conditional::class,
30 Woo_SEO_Inactive_Conditional::class,
31 ];
32 }
33
34 /**
35 * Constructs Support_Integration.
36 *
37 * @param Dynamic_Product_Permalinks_Conditional $dynamic_product_permalinks_conditional The Dynamic_Product_Permalinks_Conditional.
38 */
39 public function __construct(
40 Dynamic_Product_Permalinks_Conditional $dynamic_product_permalinks_conditional
41 ) {
42 $this->dynamic_product_permalinks_conditional = $dynamic_product_permalinks_conditional;
43 }
44
45 /**
46 * Registers the hooks for this integration.
47 *
48 * @return void
49 */
50 public function register_hooks() {
51 \add_filter( 'wc_product_post_type_link_product_cat', [ $this, 'restore_legacy_permalink_category' ], 10, 2 );
52 }
53
54 /**
55 * Handles the product category link filter. Basically restores the pre-10.5 behavior for now.
56 *
57 * @param WP_Term $category The deepest category (new default behavior).
58 * @param WP_Term[] $terms All categories assigned to the product.
59 *
60 * @return WP_Term The category to use in the permalink.
61 */
62 public function restore_legacy_permalink_category( $category, $terms ) {
63 if ( $this->dynamic_product_permalinks_conditional->is_met() ) {
64 return $category;
65 }
66
67 $sorted_terms = \wp_list_sort(
68 $terms,
69 [
70 'parent' => 'DESC',
71 'term_id' => 'ASC',
72 ],
73 );
74 return $sorted_terms[0];
75 }
76 }
77