| 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 |
|