| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 |
|
| 5 |
use WPSEO_Utils; |
| 6 |
use Yoast\WP\SEO\Config\Indexing_Reasons; |
| 7 |
|
| 8 |
/** |
| 9 |
* Watches the stripcategorybase key in wpseo_titles, in order to clear the permalink of the category indexables. |
| 10 |
*/ |
| 11 |
class Indexable_Category_Permalink_Watcher extends Indexable_Permalink_Watcher { |
| 12 |
|
| 13 |
/** |
| 14 |
* Initializes the integration. |
| 15 |
* |
| 16 |
* This is the place to register hooks and filters. |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public function register_hooks() { |
| 21 |
\add_action( 'update_option_wpseo_titles', [ $this, 'check_option' ], 10, 2 ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Checks if the stripcategorybase key in wpseo_titles has a change in value, and if so, |
| 26 |
* clears the permalink for category indexables. |
| 27 |
* |
| 28 |
* @param array $old_value The old value of the wpseo_titles option. |
| 29 |
* @param array $new_value The new value of the wpseo_titles option. |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public function check_option( $old_value, $new_value ) { |
| 34 |
// If this is the first time saving the option, in which case its value would be false. |
| 35 |
if ( $old_value === false ) { |
| 36 |
$old_value = []; |
| 37 |
} |
| 38 |
|
| 39 |
// If either value is not an array, return. |
| 40 |
if ( ! \is_array( $old_value ) || ! \is_array( $new_value ) ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// If both values aren't set, they haven't changed. |
| 45 |
if ( ! isset( $old_value['stripcategorybase'] ) && ! isset( $new_value['stripcategorybase'] ) ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
// If a new value has been set for 'stripcategorybase', clear the category permalinks. |
| 50 |
if ( $old_value['stripcategorybase'] !== $new_value['stripcategorybase'] ) { |
| 51 |
$this->indexable_helper->reset_permalink_indexables( 'term', 'category', Indexing_Reasons::REASON_CATEGORY_BASE_PREFIX ); |
| 52 |
// Clear the rewrites, so the new permalink structure is used. |
| 53 |
WPSEO_Utils::clear_rewrites(); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|