Skip to content

Commit

Permalink
hook into post publish instead of meta update to send activity and ba…
Browse files Browse the repository at this point in the history
…dge notifications
  • Loading branch information
timiwahalahti committed Jul 20, 2023
1 parent 45cc50e commit a5ded30
Showing 1 changed file with 31 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function __construct() {

// Sync with the wp.org username changes.
add_filter( 'update_post_meta', array( $this, 'username_meta_update' ), 10, 4 );
add_filter( 'add_post_meta', array( $this, 'username_meta_add' ), 10, 3 );
add_filter( 'delete_post_meta', array( $this, 'username_meta_delete' ), 10, 3 );
add_filter( 'delete_post_meta', array( $this, 'username_meta_delete' ), 10, 3 );
add_action( 'wp_after_insert_post', array( $this, 'maybe_notify_on_post_save' ) );

add_action( 'camptix_rl_buyer_completed_registration', array( $this, 'primary_attendee_registered' ), 10, 2 );
add_action( 'camptix_rl_registration_confirmed', array( $this, 'additional_attendee_confirmed_registration' ), 10, 2 );
Expand Down Expand Up @@ -98,26 +98,7 @@ protected function is_post_notifiable( $post ) {
}

/**
* Add the organizer or speaker badge when the User ID meta is added
*
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Serialized if non-scalar.
*/
public function username_meta_add( $object_id, $meta_key, $meta_value ) {
if ( '_wcpt_user_id' === $meta_key && $meta_value ) {
$post = get_post( $object_id );
if ( 'publish' !== $post->post_status ) {
return;
}

$this->add_activity( $post, $meta_value );
$this->add_badge( $post, $meta_value );
}
}

/**
* Maybe add or remove the organizer or speaker badge when the User ID meta is updated
* Maybe add mentor activity when the User ID meta is updated.
*
* @param int $meta_id ID of the metadata entry to update.
* @param int $object_id ID of the object metadata is for.
Expand All @@ -128,22 +109,6 @@ public function username_meta_update( $meta_id, $object_id, $meta_key, $meta_val
$post = get_post( $object_id );
$prev_value = get_post_meta( $object_id, $meta_key, true );

if ( '_wcpt_user_id' === $meta_key && $meta_value ) {
if ( 'publish' !== $post->post_status ) {
return;
}

$meta_value = absint( $meta_value );
$prev_value = absint( $prev_value );

if ( $prev_value && $prev_value !== $meta_value ) {
$this->maybe_remove_badge( $post, $prev_value );
}

$this->add_activity( $post, $meta_value );
$this->add_badge( $post, $meta_value );
}

if ( 'Mentor WordPress.org User Name' === $meta_key && $meta_value && $prev_value !== $meta_value ) {
// Username has already been validated by `Event_Admin::metabox_save()`.
$this->add_activity( $post, $meta_value, 'mentor_assign' );
Expand All @@ -165,6 +130,34 @@ public function username_meta_delete( $meta_ids, $object_id, $meta_key ) {
}
}

/**
* Add the organizer or speaker badge when notifiable post types are saved and User ID meta exists.
*
* @param WP_Post $post The post object.
*/
public function maybe_notify_on_post_save( $post ) {
if ( wp_is_post_revision( $post ) ) {
return;
}

if ( 'publish' !== get_post_status( $post ) ) {
return;
}

if ( ! $this->is_post_notifiable( $post ) ) {
return;
}

$user_id = get_post_meta( $post_id, '_wcpt_user_id', true );

if ( empty( $user_id ) ) {
return;
}

$this->add_activity( $post, $user_id );
$this->add_badge( $post, $user_id );
}

/**
* Makes request to Profile URL to add the speaker or organizer entry to the profile's activity section
*
Expand Down

0 comments on commit a5ded30

Please sign in to comment.