| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Conditionals\Migrations_Conditional; |
| 6 |
use Yoast\WP\SEO\Helpers\Indexable_Helper; |
| 7 |
use Yoast\WP\SEO\Integrations\Cleanup_Integration; |
| 8 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 9 |
|
| 10 |
/** |
| 11 |
* Watches the `wpseo_titles` option for changes to the author archive settings. |
| 12 |
*/ |
| 13 |
class Indexable_Author_Archive_Watcher implements Integration_Interface { |
| 14 |
|
| 15 |
/** |
| 16 |
* The indexable helper. |
| 17 |
* |
| 18 |
* @var Indexable_Helper |
| 19 |
*/ |
| 20 |
protected $indexable_helper; |
| 21 |
|
| 22 |
/** |
| 23 |
* Indexable_Author_Archive_Watcher constructor. |
| 24 |
* |
| 25 |
* @param Indexable_Helper $indexable_helper The indexable helper. |
| 26 |
*/ |
| 27 |
public function __construct( Indexable_Helper $indexable_helper ) { |
| 28 |
$this->indexable_helper = $indexable_helper; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Check if the author archives are disabled whenever the `wpseo_titles` option |
| 33 |
* changes. |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function register_hooks() { |
| 38 |
\add_action( |
| 39 |
'update_option_wpseo_titles', |
| 40 |
[ $this, 'reschedule_indexable_cleanup_when_author_archives_are_disabled' ], |
| 41 |
10, |
| 42 |
2, |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* This watcher should only be run when the migrations have been run. |
| 48 |
* (Otherwise there may not be an indexable table to clean). |
| 49 |
* |
| 50 |
* @return string[] The conditionals. |
| 51 |
*/ |
| 52 |
public static function get_conditionals() { |
| 53 |
return [ Migrations_Conditional::class ]; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Reschedule the indexable cleanup routine if the author archives are disabled. |
| 58 |
* to make sure that all authors are removed from the indexables table. |
| 59 |
* |
| 60 |
* When author archives are disabled, they can never be indexed. |
| 61 |
* |
| 62 |
* @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification |
| 63 |
* |
| 64 |
* @param array $old_value The old `wpseo_titles` option value. |
| 65 |
* @param array $new_value The new `wpseo_titles` option value. |
| 66 |
* |
| 67 |
* @phpcs:enable |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function reschedule_indexable_cleanup_when_author_archives_are_disabled( $old_value, $new_value ) { |
| 71 |
if ( $old_value['disable-author'] !== true && $new_value['disable-author'] === true && $this->indexable_helper->should_index_indexables() ) { |
| 72 |
$cleanup_not_yet_scheduled = ! \wp_next_scheduled( Cleanup_Integration::START_HOOK ); |
| 73 |
if ( $cleanup_not_yet_scheduled ) { |
| 74 |
\wp_schedule_single_event( ( \time() + ( \MINUTE_IN_SECONDS * 5 ) ), Cleanup_Integration::START_HOOK ); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|