PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / watchers / indexable-post-meta-watcher.php

indexable-post-meta-watcher.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.0, at src/integrations/watchers/indexable-post-meta-watcher.php

110 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
25 */
26 protected $post_ids_to_update = [];
27
28 /**
29 * Returns the conditionals based on which this loadable should be active.
30 *
31 * @return array
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 public function register_hooks() {
52 // Register all posts whose meta have changed.
53 \add_action( 'added_post_meta', [ $this, 'add_post_id' ], 10, 3 );
54 \add_action( 'updated_post_meta', [ $this, 'add_post_id' ], 10, 3 );
55 \add_action( 'deleted_post_meta', [ $this, 'add_post_id' ], 10, 3 );
56
57 // Remove posts that get saved as they are handled by the Indexable_Post_Watcher.
58 \add_action( 'wp_insert_post', [ $this, 'remove_post_id' ] );
59 \add_action( 'delete_post', [ $this, 'remove_post_id' ] );
60 \add_action( 'edit_attachment', [ $this, 'remove_post_id' ] );
61 \add_action( 'add_attachment', [ $this, 'remove_post_id' ] );
62 \add_action( 'delete_attachment', [ $this, 'remove_post_id' ] );
63
64 // Update indexables of all registered posts.
65 \register_shutdown_function( [ $this, 'update_indexables' ] );
66 }
67
68 /**
69 * Adds a post id to the array of posts to update.
70 *
71 * @param int|string $meta_id The meta ID.
72 * @param int|string $post_id The post ID.
73 * @param string $meta_key The meta key.
74 *
75 * @return void
76 */
77 public function add_post_id( $meta_id, $post_id, $meta_key ) {
78 // Only register changes to our own meta.
79 if ( \strpos( $meta_key, WPSEO_Meta::$meta_prefix ) !== 0 ) {
80 return;
81 }
82
83 if ( ! \in_array( $post_id, $this->post_ids_to_update, true ) ) {
84 $this->post_ids_to_update[] = (int) $post_id;
85 }
86 }
87
88 /**
89 * Removes a post id from the array of posts to update.
90 *
91 * @param int|string $post_id The post ID.
92 *
93 * @return void
94 */
95 public function remove_post_id( $post_id ) {
96 $this->post_ids_to_update = \array_diff( $this->post_ids_to_update, [ (int) $post_id ] );
97 }
98
99 /**
100 * Updates all indexables changed during the request.
101 *
102 * @return void
103 */
104 public function update_indexables() {
105 foreach ( $this->post_ids_to_update as $post_id ) {
106 $this->post_watcher->build_indexable( $post_id );
107 }
108 }
109 }
110