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

Run WordCamp Participation Notifier when post is published #964

Open
wants to merge 3 commits into
base: production
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public function __construct() {
require_once WP_CONTENT_DIR . '/mu-plugins-private/wporg-mu-plugins/pub/profile-helpers.php';

// 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( 'update_post_meta', array( $this, 'username_meta_update' ), 10, 4 );
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' ), 10, 2 );

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,32 @@ 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.
*/
public function maybe_notify_on_post_save( int $post_id, WP_Post $post ): void {
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
Loading