| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 |
|
| 5 |
use WPSEO_Meta; |
| 6 |
use Yoast\WP\SEO\Conditionals\Migrations_Conditional; |
| 7 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 8 |
|
| 9 |
/** |
| 10 |
* WordPress post meta watcher. |
| 11 |
*/ |
| 12 |
class Indexable_Post_Meta_Watcher implements Integration_Interface { |
| 13 |
|
| 14 |
/** |
| 15 |
* The post watcher. |
| 16 |
* |
| 17 |
* @var Indexable_Post_Watcher |
| 18 |
*/ |
| 19 |
protected $post_watcher; |
| 20 |
|
| 21 |
/** |
| 22 |
* An array of post IDs that need to be updated. |
| 23 |
* |
| 24 |
* @var array<int> |
| 25 |
*/ |
| 26 |
protected $post_ids_to_update = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* Returns the conditionals based on which this loadable should be active. |
| 30 |
* |
| 31 |
* @return string[] |
| 32 |
*/ |
| 33 |
public static function get_conditionals() { |
| 34 |
return [ Migrations_Conditional::class ]; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Indexable_Postmeta_Watcher constructor. |
| 39 |
* |
| 40 |
* @param Indexable_Post_Watcher $post_watcher The post watcher. |
| 41 |
*/ |
| 42 |
public function __construct( Indexable_Post_Watcher $post_watcher ) { |
| 43 |
$this->post_watcher = $post_watcher; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Initializes the integration. |
| 48 |
* |
| 49 |
* This is the place to register hooks and filters. |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function register_hooks() { |
| 54 |
// Register all posts whose meta have changed. |
| 55 |
\add_action( 'added_post_meta', [ $this, 'add_post_id' ], 10, 3 ); |
| 56 |
\add_action( 'updated_post_meta', [ $this, 'add_post_id' ], 10, 3 ); |
| 57 |
\add_action( 'deleted_post_meta', [ $this, 'add_post_id' ], 10, 3 ); |
| 58 |
|
| 59 |
// Remove posts that get saved as they are handled by the Indexable_Post_Watcher. |
| 60 |
\add_action( 'wp_insert_post', [ $this, 'remove_post_id' ] ); |
| 61 |
\add_action( 'delete_post', [ $this, 'remove_post_id' ] ); |
| 62 |
\add_action( 'edit_attachment', [ $this, 'remove_post_id' ] ); |
| 63 |
\add_action( 'add_attachment', [ $this, 'remove_post_id' ] ); |
| 64 |
\add_action( 'delete_attachment', [ $this, 'remove_post_id' ] ); |
| 65 |
|
| 66 |
// Update indexables of all registered posts. |
| 67 |
\register_shutdown_function( [ $this, 'update_indexables' ] ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Adds a post id to the array of posts to update. |
| 72 |
* |
| 73 |
* @param int|string $meta_id The meta ID. |
| 74 |
* @param int|string $post_id The post ID. |
| 75 |
* @param string $meta_key The meta key. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function add_post_id( $meta_id, $post_id, $meta_key ) { |
| 80 |
// Only register changes to our own meta. |
| 81 |
if ( \is_string( $meta_key ) && \strpos( $meta_key, WPSEO_Meta::$meta_prefix ) !== 0 ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! \in_array( $post_id, $this->post_ids_to_update, true ) ) { |
| 86 |
$this->post_ids_to_update[] = (int) $post_id; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Removes a post id from the array of posts to update. |
| 92 |
* |
| 93 |
* @param int|string $post_id The post ID. |
| 94 |
* |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function remove_post_id( $post_id ) { |
| 98 |
$this->post_ids_to_update = \array_diff( $this->post_ids_to_update, [ (int) $post_id ] ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Updates all indexables changed during the request. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function update_indexables() { |
| 107 |
foreach ( $this->post_ids_to_update as $post_id ) { |
| 108 |
$this->post_watcher->build_indexable( $post_id ); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|