Skip to content

Commit

Permalink
Move all event save actions into a handler function
Browse files Browse the repository at this point in the history
  • Loading branch information
joedolson committed Nov 20, 2024
1 parent e61eeea commit b3be19f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 48 deletions.
75 changes: 39 additions & 36 deletions src/my-calendar-event-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -479,6 +478,33 @@ function my_calendar_edit() {
<?php
}

/**
* Do event save actions.
*
* @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.
*
* @return int Event post ID.
*/
function mc_do_event_save_actions( $action, $data, $event_id, $result ) {
$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 );

return $event_post;
}

/**
* Save an event to the database
*
Expand All @@ -497,9 +523,9 @@ function my_calendar_save( $action, $output, $event_id = false ) {
$formats = array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d' );

if ( ( 'add' === $action || 'copy' === $action ) && true === $proceed ) {
$add = $output[2]; // add format here.
$data = $output[2];
$cats = $add['event_categories'];
$add = $output[2]; // add format here.
$update = $output[2];
$cats = $add['event_categories'];

unset( $add['event_categories'] );
// My Calendar no longer saves location data to the events table, and it'll cause problems if it's in the array.
Expand Down Expand Up @@ -540,19 +566,8 @@ function my_calendar_save( $action, $output, $event_id = false ) {
$event_link = '';
$edit_link = '';

$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 );
$event = mc_get_first_event( $event_id );
$event_post = mc_do_event_save_actions( $action, $update, $event_id, $result );
$event = mc_get_first_event( $event_id );

if ( 'true' === mc_get_option( 'event_mail' ) ) {
// insert_id is last occurrence inserted in the db.
Expand Down Expand Up @@ -672,17 +687,17 @@ function my_calendar_save( $action, $output, $event_id = false ) {
$location = absint( $post['preset_location'] );
$update['event_location'] = $location;
}
$wpdb->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' );
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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' );
}
Expand Down
13 changes: 1 addition & 12 deletions src/my-calendar-group-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit b3be19f

Please sign in to comment.