Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/243 term user meta watching #272

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions includes/watchers/class-algolia-post-changes-watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,14 @@ public function delete_item( $post_id ) {
* @author WebDevStudios <contact@webdevstudios.com>
* @since 1.0.0
*
* @param string|array $meta_id The meta ID.
* @param int $object_id The post ID.
* @param string $meta_key The meta key.
* @param string|array $meta_id The meta ID.
* @param int $object_id The post ID.
* @param string $meta_key The meta key.
* @param mixed $meta_value The meta value.
*
* @return void
*/
public function on_meta_change( $meta_id, $object_id, $meta_key ) {
public function on_meta_change( $meta_id, $object_id, $meta_key, $meta_value ) {
$keys = array( '_thumbnail_id' );
$keys = (array) apply_filters( 'algolia_watch_post_meta_keys', $keys, $object_id );

Expand Down
30 changes: 30 additions & 0 deletions includes/watchers/class-algolia-term-changes-watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function watch() {
// Fires after an object's terms have been set.
add_action( 'set_object_terms', array( $this, 'handle_changes' ), 10, 6 );

// Handle meta changes after the change occurred.
add_action( 'added_term_meta', [ $this, 'on_meta_change' ], 10, 4 );
add_action( 'updated_term_meta', [ $this, 'on_meta_change' ], 10, 4 );
add_action( 'deleted_term_meta', [ $this, 'on_meta_change' ], 10, 4 );

// Fires after a term is deleted from the database and the cache is cleaned.
add_action( 'delete_term', array( $this, 'on_delete_term' ), 10, 4 );
}
Expand Down Expand Up @@ -130,4 +135,29 @@ public function on_delete_term( $term, $tt_id, $taxonomy, $deleted_term ) {
error_log( $exception->getMessage() ); // phpcs:ignore -- Legacy.
}
}

/**
* Watch meta changes for item.
*
* @param string|array $meta_id The meta ID.
* @param int $object_id The post ID.
* @param string $meta_key The meta key.
* @param mixed $meta_value The meta value.
*
* @return void
* @author WebDevStudios <contact@webdevstudios.com>
* @since 2.5.0
*/
public function on_meta_change( $meta_id, $object_id, $meta_key, $meta_value ) {

// We will not listen for any specific key by default.
$keys = [];
$keys = (array) apply_filters( 'algolia_watch_term_meta_keys', $keys, $object_id );

if ( empty( $keys ) || ! in_array( $meta_key, $keys, true ) ) {
return;
}

$this->sync_item( $object_id );
}
}
30 changes: 30 additions & 0 deletions includes/watchers/class-algolia-user-changes-watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function watch() {
// Fires immediately after a new user is registered.
add_action( 'user_register', array( $this, 'sync_item' ) );

// Handle meta changes after the change occurred.
add_action( 'added_user_meta', [ $this, 'on_meta_change' ], 10, 4 );
add_action( 'updated_user_meta', [ $this, 'on_meta_change' ], 10, 4 );
add_action( 'deleted_user_meta', [ $this, 'on_meta_change' ], 10, 4 );

// Fires immediately before a user is deleted.
add_action( 'delete_user', array( $this, 'delete_item' ) );

Expand Down Expand Up @@ -159,4 +164,29 @@ function() use ( $watcher, $author_id ) {
}
);
}

/**
* Watch meta changes for item.
*
* @param string|array $meta_id The meta ID.
* @param int $object_id The post ID.
* @param string $meta_key The meta key.
* @param mixed $meta_value The meta value.
*
* @return void
* @author WebDevStudios <contact@webdevstudios.com>
* @since 2.5.0
*/
public function on_meta_change( $meta_id, $object_id, $meta_key, $meta_value ) {

// We will not listen for any specific key by default.
$keys = [];
$keys = (array) apply_filters( 'algolia_watch_user_meta_keys', $keys, $object_id );

if ( empty( $keys ) || ! in_array( $meta_key, $keys, true ) ) {
return;
}

$this->sync_item( $object_id );
}
}