From b3be19fc8902cb6a591e9cd972438603ed7b7dcd Mon Sep 17 00:00:00 2001 From: Joe Dolson Date: Wed, 20 Nov 2024 17:17:40 -0600 Subject: [PATCH] Move all event save actions into a handler function --- src/my-calendar-event-editor.php | 75 ++++++++++++++++--------------- src/my-calendar-group-manager.php | 13 +----- 2 files changed, 40 insertions(+), 48 deletions(-) diff --git a/src/my-calendar-event-editor.php b/src/my-calendar-event-editor.php index 51f11ef7..a7fe86a6 100644 --- a/src/my-calendar-event-editor.php +++ b/src/my-calendar-event-editor.php @@ -24,7 +24,6 @@ * @return int|false post ID or false if post ID not available or created. */ function mc_event_post( $action, $data, $event_id, $result = false ) { - // if the event save was successful. $post_id = false; // Check if this is an ajax request. $post_post = isset( $_POST['post'] ) ? $_POST['post'] : array(); @@ -479,6 +478,33 @@ function my_calendar_edit() { insert( my_calendar_table(), $update, $formats ); - // need to get this variable into URL for form submit. + $result = $wpdb->insert( my_calendar_table(), $update, $formats ); $new_event = $wpdb->insert_id; mc_update_category_relationships( $cats, $new_event ); - $result = mc_update_instance( $event_instance, $new_event, $update ); + mc_update_instance( $event_instance, $new_event, $update ); + $event_post = mc_do_event_save_actions( $action, $update, $new_event, $result ); } else { if ( $update['event_begin'][0] === $post['prev_event_begin'] && $update['event_end'][0] === $post['prev_event_end'] ) { // There were no changes at all. } else { // Only dates were changed. - $result = mc_update_instance( $event_instance, $event_id, $update ); + mc_update_instance( $event_instance, $event_id, $update ); $message = mc_show_notice( __( 'Date/time information for this event has been updated.', 'my-calendar' ) . " $url", false, 'date-updated', 'success' ); } } @@ -718,20 +733,8 @@ function my_calendar_save( $action, $output, $event_id = false ) { mc_increment_event( $event_id, array(), false, $instances ); } } + $event_post = mc_do_event_save_actions( $action, $update, $event_id, $result ); } - $data = $update; - $event_post = mc_event_post( $action, $data, $event_id, $result ); - /** - * Run action when an event is saved. - * - * @hook mc_save_event - * - * @param {string} $action Current action: edit, copy, add. - * @param {array} $data Data updated. - * @param {int} $event_id Event ID. - * @param {int|false} $result Result of the DB update query. - */ - do_action( 'mc_save_event', $action, $data, $event_id, $result ); if ( false === $result ) { $message = mc_show_error( __( 'Your event was not updated.', 'my-calendar' ) . " $url", false ); } else { @@ -750,10 +753,10 @@ function my_calendar_save( $action, $output, $event_id = false ) { * @param {int} $prev_event_status Previous status. * @param {int} $new_event_status New status. * @param {string} $action Action being performed. - * @param {array} $data Submitted event data. + * @param {array} $update Submitted event data. * @param {int} $event_id Event ID. */ - do_action( 'mc_transition_event', (int) $post['prev_event_status'], $new_event_status, $action, $data, $event_id ); + do_action( 'mc_transition_event', (int) $post['prev_event_status'], $new_event_status, $action, $update, $event_id ); } $message = mc_show_notice( __( 'Event updated successfully', 'my-calendar' ) . ". $url", false, 'event-updated', 'success' ); } diff --git a/src/my-calendar-group-manager.php b/src/my-calendar-group-manager.php index a7c0a0d7..b523bfb1 100644 --- a/src/my-calendar-group-manager.php +++ b/src/my-calendar-group-manager.php @@ -176,18 +176,7 @@ function my_calendar_save_group( $action, $output, $event_id, $post = array() ) // Make sure POST data is available in update array. $update = array_merge( $update, $post ); // Same as action on basic save. - mc_event_post( 'edit', $update, $event_id, $result ); - /** - * Run action when an event is saved. - * - * @hook mc_save_event - * - * @param {string} $action Current action: edit, copy, add. - * @param {array} $update Data updated. - * @param {int} $event_id Event ID. - * @param {int|false} $result Result of the DB update query. - */ - do_action( 'mc_save_event', 'edit', $update, $event_id, $result ); + mc_do_event_save_actions( 'edit', $update, $event_id, $result ); /** * Run action when a group of events are saved. *