| 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 |
* @return void |
| 46 |
*/ |
| 47 |
public function register_hooks() { |
| 48 |
\add_filter( 'wpseo_post_types_reset_permalinks', [ $this, 'filter_product_from_post_types' ] ); |
| 49 |
\add_action( 'update_option_woocommerce_permalinks', [ $this, 'reset_woocommerce_permalinks' ], 10, 2 ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Filters the product post type from the post type. |
| 54 |
* |
| 55 |
* @param array $post_types The post types to filter. |
| 56 |
* |
| 57 |
* @return array The filtered post types. |
| 58 |
*/ |
| 59 |
public function filter_product_from_post_types( $post_types ) { |
| 60 |
unset( $post_types['product'] ); |
| 61 |
|
| 62 |
return $post_types; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Resets the indexables for WooCommerce based on the changed permalink fields. |
| 67 |
* |
| 68 |
* @param array $old_value The old value. |
| 69 |
* @param array $new_value The new value. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function reset_woocommerce_permalinks( $old_value, $new_value ) { |
| 74 |
$changed_options = \array_diff( $old_value, $new_value ); |
| 75 |
|
| 76 |
if ( \array_key_exists( 'product_base', $changed_options ) ) { |
| 77 |
$this->indexable_helper->reset_permalink_indexables( 'post', 'product' ); |
| 78 |
} |
| 79 |
|
| 80 |
if ( \array_key_exists( 'attribute_base', $changed_options ) ) { |
| 81 |
$attribute_taxonomies = $this->get_attribute_taxonomies(); |
| 82 |
|
| 83 |
foreach ( $attribute_taxonomies as $attribute_name ) { |
| 84 |
$this->indexable_helper->reset_permalink_indexables( 'term', $attribute_name ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if ( \array_key_exists( 'category_base', $changed_options ) ) { |
| 89 |
$this->indexable_helper->reset_permalink_indexables( 'term', 'product_cat' ); |
| 90 |
} |
| 91 |
|
| 92 |
if ( \array_key_exists( 'tag_base', $changed_options ) ) { |
| 93 |
$this->indexable_helper->reset_permalink_indexables( 'term', 'product_tag' ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Retrieves the taxonomies based on the attributes. |
| 99 |
* |
| 100 |
* @return array The taxonomies. |
| 101 |
*/ |
| 102 |
protected function get_attribute_taxonomies() { |
| 103 |
$taxonomies = []; |
| 104 |
foreach ( \wc_get_attribute_taxonomies() as $attribute_taxonomy ) { |
| 105 |
$taxonomies[] = \wc_attribute_taxonomy_name( $attribute_taxonomy->attribute_name ); |
| 106 |
} |
| 107 |
|
| 108 |
$taxonomies = \array_filter( $taxonomies ); |
| 109 |
|
| 110 |
return $taxonomies; |
| 111 |
} |
| 112 |
} |
| 113 |
|