PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
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 trunk, at src/integrations/watchers/indexable-post-meta-watcher.php

112 lines 2.9 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<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