PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.5
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 / third-party / woocommerce-permalinks.php

woocommerce-permalinks.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.5, at src/integrations/third-party/woocommerce-permalinks.php

109 lines 2.9 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\Third_Party;
4
5 use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
6 use Yoast\WP\SEO\Conditionals\WooCommerce_Conditional;
7 use Yoast\WP\SEO\Helpers\Indexable_Helper;
8 use Yoast\WP\SEO\Integrations\Integration_Interface;
9
10 /**
11 * The permalink watcher.
12 */
13 class Woocommerce_Permalinks implements Integration_Interface {
14
15 /**
16 * Represents the indexable helper.
17 *
18 * @var Indexable_Helper
19 */
20 protected $indexable_helper;
21
22 /**
23 * Returns the conditionals based in which this loadable should be active.
24 *
25 * @return array
26 */
27 public static function get_conditionals() {
28 return [ WooCommerce_Conditional::class, Migrations_Conditional::class ];
29 }
30
31 /**
32 * Constructor.
33 *
34 * @param Indexable_Helper $indexable_helper Indexable Helper.
35 */
36 public function __construct( Indexable_Helper $indexable_helper ) {
37 $this->indexable_helper = $indexable_helper;
38 }
39
40 /**
41 * Registers the hooks.
42 *
43 * @codeCoverageIgnore
44 */
45 public function register_hooks() {
46 \add_filter( 'wpseo_post_types_reset_permalinks', [ $this, 'filter_product_from_post_types' ] );
47 \add_action( 'update_option_woocommerce_permalinks', [ $this, 'reset_woocommerce_permalinks' ], 10, 2 );
48 }
49
50 /**
51 * Filters the product post type from the post type.
52 *
53 * @param array $post_types The post types to filter.
54 *
55 * @return array The filtered post types.
56 */
57 public function filter_product_from_post_types( $post_types ) {
58 unset( $post_types['product'] );
59
60 return $post_types;
61 }
62
63 /**
64 * Resets the indexables for WooCommerce based on the changed permalink fields.
65 *
66 * @param array $old_value The old value.
67 * @param array $new_value The new value.
68 */
69 public function reset_woocommerce_permalinks( $old_value, $new_value ) {
70 $changed_options = \array_diff( $old_value, $new_value );
71
72 if ( \array_key_exists( 'product_base', $changed_options ) ) {
73 $this->indexable_helper->reset_permalink_indexables( 'post', 'product' );
74 }
75
76 if ( \array_key_exists( 'attribute_base', $changed_options ) ) {
77 $attribute_taxonomies = $this->get_attribute_taxonomies();
78
79 foreach ( $attribute_taxonomies as $attribute_name ) {
80 $this->indexable_helper->reset_permalink_indexables( 'term', $attribute_name );
81 }
82 }
83
84 if ( \array_key_exists( 'category_base', $changed_options ) ) {
85 $this->indexable_helper->reset_permalink_indexables( 'term', 'product_cat' );
86 }
87
88 if ( \array_key_exists( 'tag_base', $changed_options ) ) {
89 $this->indexable_helper->reset_permalink_indexables( 'term', 'product_tag' );
90 }
91 }
92
93 /**
94 * Retrieves the taxonomies based on the attributes.
95 *
96 * @return array The taxonomies.
97 */
98 protected function get_attribute_taxonomies() {
99 $taxonomies = [];
100 foreach ( \wc_get_attribute_taxonomies() as $attribute_taxonomy ) {
101 $taxonomies[] = \wc_attribute_taxonomy_name( $attribute_taxonomy->attribute_name );
102 }
103
104 $taxonomies = \array_filter( $taxonomies );
105
106 return $taxonomies;
107 }
108 }
109