Skip to content

Commit 6a3afa8

Browse files
committed
Add mc_insert_event() to simplify insertion.
See #641
1 parent cd51604 commit 6a3afa8

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/my-calendar-event-editor.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,7 @@ function my_calendar_edit() {
417417
echo wp_kses_post( $response['message'] );
418418
}
419419
for ( $i = 0; $i < $count; $i++ ) {
420-
$mc_output = mc_check_data( $action, $post, $i );
421-
if ( 'add' === $action || 'copy' === $action ) {
422-
$response = my_calendar_save( $action, $mc_output );
423-
$action = 'add'; // After saving, reset action to default.
424-
} else {
425-
$response = my_calendar_save( $action, $mc_output, (int) $post['event_id'] );
426-
}
420+
$response = mc_insert_event( $action, $post, $i );
427421
echo wp_kses_post( $response['message'] );
428422
}
429423
if ( isset( $post['ref'] ) ) {
@@ -2123,6 +2117,36 @@ function mc_event_accessibility( $form, $data, $label ) {
21232117
return $form;
21242118
}
21252119

2120+
/**
2121+
* Insert an event. Checks data and saves event to the data, returning a response array.
2122+
*
2123+
* @param string $action Type of action being performed.
2124+
* @param array $post Post data.
2125+
* @param int $i If multiple events submitted, which index this is.
2126+
* @param bool $ignore_required Pass 'true' to ignore required fields.
2127+
*
2128+
* @return array Modified data and information about approval.
2129+
*/
2130+
function mc_insert_event( $action, $post, $i, $ignore_required = false ) {
2131+
$mc_output = mc_check_data( $action, $post, $i, $ignore_required );
2132+
if ( $mc_output[0] ) {
2133+
if ( 'add' === $action || 'copy' === $action ) {
2134+
$response = my_calendar_save( $action, $mc_output );
2135+
$action = 'add'; // After saving, reset action to default.
2136+
} else {
2137+
$response = my_calendar_save( $action, $mc_output, (int) $post['event_id'] );
2138+
}
2139+
} else {
2140+
$response = array(
2141+
'event_id' => false,
2142+
'event_post' => false,
2143+
'message' => $mc_output[3],
2144+
);
2145+
}
2146+
2147+
return $response;
2148+
}
2149+
21262150
/**
21272151
* Review data submitted and verify.
21282152
*

0 commit comments

Comments
 (0)