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

When a user creates an event, she should be attending right away #105

Merged
merged 4 commits into from
Feb 21, 2024
Merged
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
28 changes: 28 additions & 0 deletions wporg-gp-translation-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ function validate_event_dates( string $event_start, string $event_end ): bool {
return false;
}

/**
* Handle the event form submission for the creation, editing, and deletion of events. This function is called via AJAX.
*/
function submit_event_ajax() {
$event_id = null;
$response_message = '';
Expand Down Expand Up @@ -309,10 +312,35 @@ function register_translation_event_js() {
);
}

/**
* Handle the event status transition.
*
* The user who creates the event will assist to it when it's published.
*
* @param string $new_status The new post status.
* @param string $old_status The old post status.
* @param WP_Post $post The post object.
*/
function event_status_transition( string $new_status, string $old_status, WP_Post $post ): void {
if ( 'event' !== $post->post_type ) {
return;
}
if ( 'publish' === $new_status && ( 'new' === $old_status || 'draft' === $old_status ) ) {
$current_user_id = get_current_user_id();
$user_attending_events = get_user_meta( $current_user_id, Route::USER_META_KEY_ATTENDING, true ) ?: array();
$is_user_attending_event = in_array( $post->ID, $user_attending_events, true );
if ( ! $is_user_attending_event ) {
$new_user_attending_events = $user_attending_events;
$new_user_attending_events[ $post->ID ] = true;
update_user_meta( $current_user_id, Route::USER_META_KEY_ATTENDING, $new_user_attending_events, $user_attending_events );
}
}
}
add_action( 'wp_enqueue_scripts', 'Wporg\TranslationEvents\register_translation_event_js' );
add_action( 'init', 'Wporg\TranslationEvents\register_event_post_type' );
add_action( 'add_meta_boxes', 'Wporg\TranslationEvents\event_meta_boxes' );
add_action( 'save_post', 'Wporg\TranslationEvents\save_event_meta_boxes' );
add_action( 'transition_post_status', 'Wporg\TranslationEvents\event_status_transition', 10, 3 );

/**
* Add the events link to the GlotPress main menu.
Expand Down
Loading