| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Builders\Indexable_Builder; |
| 6 |
use Yoast\WP\SEO\Conditionals\Migrations_Conditional; |
| 7 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 8 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 9 |
|
| 10 |
/** |
| 11 |
* Date archive watcher to save the meta data to an indexable. |
| 12 |
* |
| 13 |
* Watches the date archive options to save the meta information when updated. |
| 14 |
*/ |
| 15 |
class Indexable_Date_Archive_Watcher implements Integration_Interface { |
| 16 |
|
| 17 |
/** |
| 18 |
* The indexable repository. |
| 19 |
* |
| 20 |
* @var Indexable_Repository |
| 21 |
*/ |
| 22 |
protected $repository; |
| 23 |
|
| 24 |
/** |
| 25 |
* The indexable builder. |
| 26 |
* |
| 27 |
* @var Indexable_Builder |
| 28 |
*/ |
| 29 |
protected $builder; |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns the conditionals based on which this loadable should be active. |
| 33 |
* |
| 34 |
* @return array |
| 35 |
*/ |
| 36 |
public static function get_conditionals() { |
| 37 |
return [ Migrations_Conditional::class ]; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Indexable_Date_Archive_Watcher constructor. |
| 42 |
* |
| 43 |
* @param Indexable_Repository $repository The repository to use. |
| 44 |
* @param Indexable_Builder $builder The date archive builder to use. |
| 45 |
*/ |
| 46 |
public function __construct( Indexable_Repository $repository, Indexable_Builder $builder ) { |
| 47 |
$this->repository = $repository; |
| 48 |
$this->builder = $builder; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Initializes the integration. |
| 53 |
* |
| 54 |
* This is the place to register hooks and filters. |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function register_hooks() { |
| 59 |
\add_action( 'update_option_wpseo_titles', [ $this, 'check_option' ], 10, 2 ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Checks if the date archive indexable needs to be rebuild based on option values. |
| 64 |
* |
| 65 |
* @param array $old_value The old value of the option. |
| 66 |
* @param array $new_value The new value of the option. |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function check_option( $old_value, $new_value ) { |
| 71 |
$relevant_keys = [ 'title-archive-wpseo', 'breadcrumbs-archiveprefix', 'metadesc-archive-wpseo', 'noindex-archive-wpseo' ]; |
| 72 |
|
| 73 |
foreach ( $relevant_keys as $key ) { |
| 74 |
// If both values aren't set they haven't changed. |
| 75 |
if ( ! isset( $old_value[ $key ] ) && ! isset( $new_value[ $key ] ) ) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
// If the value was set but now isn't, is set but wasn't or is not the same it has changed. |
| 80 |
if ( ! isset( $old_value[ $key ] ) || ! isset( $new_value[ $key ] ) || $old_value[ $key ] !== $new_value[ $key ] ) { |
| 81 |
$this->build_indexable(); |
| 82 |
return; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Saves the date archive. |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function build_indexable() { |
| 93 |
$indexable = $this->repository->find_for_date_archive( false ); |
| 94 |
$this->builder->build_for_date_archive( $indexable ); |
| 95 |
} |
| 96 |
} |
| 97 |
|