| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Builders\Indexable_Builder; |
| 6 |
use Yoast\WP\SEO\Builders\Indexable_System_Page_Builder; |
| 7 |
use Yoast\WP\SEO\Conditionals\Migrations_Conditional; |
| 8 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 9 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 10 |
|
| 11 |
/** |
| 12 |
* Search result watcher to save the meta data to an Indexable. |
| 13 |
* |
| 14 |
* Watches the search result options to save the meta information when updated. |
| 15 |
*/ |
| 16 |
class Indexable_System_Page_Watcher implements Integration_Interface { |
| 17 |
|
| 18 |
/** |
| 19 |
* The indexable repository. |
| 20 |
* |
| 21 |
* @var Indexable_Repository |
| 22 |
*/ |
| 23 |
protected $repository; |
| 24 |
|
| 25 |
/** |
| 26 |
* The indexable builder. |
| 27 |
* |
| 28 |
* @var Indexable_Builder |
| 29 |
*/ |
| 30 |
protected $builder; |
| 31 |
|
| 32 |
/** |
| 33 |
* Returns the conditionals based on which this loadable should be active. |
| 34 |
* |
| 35 |
* @return array |
| 36 |
*/ |
| 37 |
public static function get_conditionals() { |
| 38 |
return [ Migrations_Conditional::class ]; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Indexable_System_Page_Watcher constructor. |
| 43 |
* |
| 44 |
* @param Indexable_Repository $repository The repository to use. |
| 45 |
* @param Indexable_Builder $builder The post builder to use. |
| 46 |
*/ |
| 47 |
public function __construct( Indexable_Repository $repository, Indexable_Builder $builder ) { |
| 48 |
$this->repository = $repository; |
| 49 |
$this->builder = $builder; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Initializes the integration. |
| 54 |
* |
| 55 |
* This is the place to register hooks and filters. |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function register_hooks() { |
| 60 |
\add_action( 'update_option_wpseo_titles', [ $this, 'check_option' ], 10, 2 ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Checks if the home page indexable needs to be rebuild based on option values. |
| 65 |
* |
| 66 |
* @param array $old_value The old value of the option. |
| 67 |
* @param array $new_value The new value of the option. |
| 68 |
* |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function check_option( $old_value, $new_value ) { |
| 72 |
foreach ( Indexable_System_Page_Builder::OPTION_MAPPING as $type => $options ) { |
| 73 |
foreach ( $options as $option ) { |
| 74 |
// If both values aren't set they haven't changed. |
| 75 |
if ( ! isset( $old_value[ $option ] ) && ! isset( $new_value[ $option ] ) ) { |
| 76 |
return; |
| 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 ( |
| 81 |
! isset( $old_value[ $option ] ) |
| 82 |
|| ! isset( $new_value[ $option ] ) |
| 83 |
|| $old_value[ $option ] !== $new_value[ $option ] |
| 84 |
) { |
| 85 |
$this->build_indexable( $type ); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Saves the search result. |
| 93 |
* |
| 94 |
* @param string $type The type of no index page. |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function build_indexable( $type ) { |
| 99 |
$indexable = $this->repository->find_for_system_page( $type, false ); |
| 100 |
$this->builder->build_for_system_page( $type, $indexable ); |
| 101 |
} |
| 102 |
} |
| 103 |
|