Skip to content

Commit

Permalink
code optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
liedekef committed Aug 1, 2024
1 parent af410ff commit 6f5a3fa
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 36 deletions.
15 changes: 10 additions & 5 deletions eme-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ function eme_events_page() {
return;
} else {
$new_location_id = eme_insert_location( $location );
eme_location_store_cf_answers( $new_location_id );
eme_location_store_answers( $new_location_id );
if ( ! $new_location_id ) {
echo "<div id='message' class='error '>
<p>" . __( "Could not create the new location for this event: either you don't have the right to insert locations or there's a DB problem.", 'eme', 'events-made-easy' ) . "</p>
Expand Down Expand Up @@ -637,7 +637,7 @@ function eme_events_page() {
if ( ! $event_id ) {
$feedback_message = __( 'Database insert failed!', 'events-made-easy' );
} else {
eme_event_store_cf_answers( $event_id );
eme_event_store_answers( $event_id );
eme_upload_files( $event_id, 'events' );
// the eme_insert_event_action is only executed for single events, not those part of a recurrence
if ( has_action( 'eme_insert_event_action' ) ) {
Expand Down Expand Up @@ -708,7 +708,7 @@ function eme_events_page() {
// unlink from recurrence in case it was generated by one
$event['recurrence_id'] = 0;
if ( eme_db_update_event( $event, $event_ID ) ) {
eme_event_store_cf_answers( $event_ID );
eme_event_store_answers( $event_ID );
eme_upload_files( $event_ID, 'events' );
if ( has_action( 'eme_update_event_action' ) ) {
$event = eme_get_event( $event_ID );
Expand Down Expand Up @@ -10637,7 +10637,7 @@ function eme_delete_events( $ids_arr ) {
}
}

function eme_get_event_post_cfs() {
function eme_get_event_post_answers() {
$answers = [];
foreach ( $_POST as $key => $value ) {
if ( preg_match( '/^FIELD(\d+)$/', $key, $matches ) ) {
Expand Down Expand Up @@ -10680,11 +10680,16 @@ function eme_get_event_answers( $event_id ) {
return $cf;
}

// for backwards compat
function eme_event_store_cf_answers( $event_id ) {
return eme_event_store_answers( $event_id );
}

function eme_event_store_answers( $event_id ) {
$answer_ids_seen = [];

$all_answers = eme_get_event_answers( $event_id );
$found_answers = eme_get_event_post_cfs();
$found_answers = eme_get_event_post_answers();
foreach ( $found_answers as $answer ) {
$formfield = eme_get_formfield( $answer['field_id'] );
if ( ! empty( $formfield ) && $formfield['field_purpose'] == 'events' ) {
Expand Down
4 changes: 2 additions & 2 deletions eme-fs.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ function eme_fs_process_newevent() {
if (has_filter('eme_fs_event_insert_filter')) $event_data=apply_filters('eme_fs_event_insert_filter',$event_data);
$event_id = eme_db_insert_event($event_data);
if ($event_id) {
eme_event_store_cf_answers($event_id);
eme_event_store_answers($event_id);
eme_upload_files( $event_id, 'events' );
$event = eme_get_event($event_id);
if (has_action('eme_fs_submit_event_action')) {
Expand Down Expand Up @@ -626,7 +626,7 @@ function eme_fs_processlocation($event_data, $force=0) {
$validation_result = eme_validate_location ( $location );
if ($validation_result == "OK") {
$location_id = eme_insert_location($location, $force);
eme_location_store_cf_answers($location_id);
eme_location_store_answers($location_id);
}
}
return $location_id;
Expand Down
11 changes: 7 additions & 4 deletions eme-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// we define all db-constants here, this also means the uninstall can include this file and use it
// and doesn't need to include the main file
define( 'EME_DB_VERSION', 393 ); // increase this if the db schema changes or the options change
define( 'EME_DB_VERSION', 394 ); // increase this if the db schema changes or the options change
define( 'EME_EVENTS_TBNAME', 'eme_events' );
define( 'EME_EVENTS_CF_TBNAME', 'eme_events_cf' );
define( 'EME_RECURRENCE_TBNAME', 'eme_recurrence' );
Expand Down Expand Up @@ -1103,9 +1103,6 @@ function eme_create_answers_table( $charset, $collate, $db_version, $db_prefix )
answer_id int(11) NOT NULL auto_increment,
type varchar(20) DEFAULT NULL,
related_id mediumint(9) DEFAULT 0,
booking_id mediumint(9) DEFAULT 0,
person_id mediumint(9) DEFAULT 0,
member_id mediumint(9) DEFAULT 0,
field_id int(11) DEFAULT 0,
answer text NOT NULL,
eme_grouping int(11) DEFAULT 0,
Expand Down Expand Up @@ -1179,6 +1176,12 @@ function eme_create_answers_table( $charset, $collate, $db_version, $db_prefix )
eme_drop_table( $cf_table_name );
}
}
if ( $db_version < 394 ) {
// these were still added for new versions of EME, remove them ...
eme_maybe_drop_column( $table_name, 'person_id' );
eme_maybe_drop_column( $table_name, 'member_id' );
eme_maybe_drop_column( $table_name, 'booking_id' );
}
}
}

Expand Down
13 changes: 9 additions & 4 deletions eme-locations.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function eme_locations_page() {
if ( $action == 'do_addlocation' ) {
$new_location_id = eme_insert_location( $location );
if ( $new_location_id ) {
eme_location_store_cf_answers( $new_location_id );
eme_location_store_answers( $new_location_id );
eme_upload_files( $new_location_id, 'locations' );
$new_location = eme_get_location( $new_location_id );
$message = __( 'The location has been added.', 'events-made-easy' );
Expand All @@ -178,7 +178,7 @@ function eme_locations_page() {
}
} elseif ( $action == 'do_editlocation' ) {
if ( eme_update_location( $location, $location_id ) ) {
eme_location_store_cf_answers( $location_id );
eme_location_store_answers( $location_id );
eme_upload_files( $location_id, 'locations' );
$message = __( 'The location has been updated.', 'events-made-easy' );
if ( get_option( 'eme_stay_on_edit_page' ) ) {
Expand Down Expand Up @@ -3047,7 +3047,7 @@ function eme_ajax_manage_locations() {
wp_die();
}

function eme_get_location_post_cfs() {
function eme_get_location_post_answers() {
$answers = [];
foreach ( $_POST as $key => $value ) {
if ( preg_match( '/^FIELD(\d+)$/', eme_sanitize_request( $key ), $matches ) ) {
Expand Down Expand Up @@ -3090,11 +3090,16 @@ function eme_get_location_answers( $location_id ) {
return $cf;
}

// for backwards compat
function eme_location_store_cf_answers( $location_id ) {
return eme_location_store_answers( $location_id );
}

function eme_location_store_answers( $location_id ) {
$answer_ids_seen = [];

$all_answers = eme_get_location_answers( $location_id );
$found_answers = eme_get_location_post_cfs();
$found_answers = eme_get_location_post_answers();
foreach ( $found_answers as $answer ) {
$formfield = eme_get_formfield( $answer['field_id'] );
if ( ! empty( $formfield ) && $formfield['field_purpose'] == 'locations' ) {
Expand Down
39 changes: 30 additions & 9 deletions eme-members.php
Original file line number Diff line number Diff line change
Expand Up @@ -3621,6 +3621,17 @@ function eme_get_member_post_answers( $member, $include_dynamicdata = 1 ) {
} else {
$value = eme_sanitize_request( $value );
}
// type varchar(20) DEFAULT NULL,
// related_id mediumint(9) DEFAULT 0,

if ($formfield['field_purpose'] == 'people') {
$type = 'person';
$related_id = isset($member['person_id'])?$member['person_id']:0;
} else {
$type = 'member';
$related_id = isset($member['member_id'])?$member['member_id']:0;
}
// some extra fields are added, so people can use these to check things: field_name, field_purpose, extra_charge (also used in code), grouping_id and occurence_id
$answer = [
'field_name' => $formfield['field_name'],
'field_id' => $field_id,
Expand All @@ -3629,6 +3640,10 @@ function eme_get_member_post_answers( $member, $include_dynamicdata = 1 ) {
'answer' => $value,
'grouping_id' => $group_id,
'occurence_id' => $occurence_id,
'eme_grouping' => $group_id,
'occurence' => $occurence_id,
'type' => $type,
'related_id' => $related_id,
];
$answers[] = $answer;
}
Expand Down Expand Up @@ -3661,6 +3676,14 @@ function eme_get_member_post_answers( $member, $include_dynamicdata = 1 ) {
} else {
$value = eme_sanitize_request( $value );
}
if ($formfield['field_purpose'] == 'people') {
$type = 'person';
$related_id = isset($member['person_id'])?$member['person_id']:0;
} else {
$type = 'member';
$related_id = isset($member['member_id'])?$member['member_id']:0;
}
// some extra fields are added, so people can use these to check things: field_name, field_purpose, extra_charge (also used in code), grouping_id and occurence_id
$answer = [
'field_name' => $formfield['field_name'],
'field_id' => $field_id,
Expand All @@ -3669,6 +3692,10 @@ function eme_get_member_post_answers( $member, $include_dynamicdata = 1 ) {
'answer' => $value,
'grouping_id' => 0,
'occurence_id' => 0,
'eme_grouping' => 0,
'occurence' => 0,
'type' => $type,
'related_id' => $related_id,
];
$answers[] = $answer;
}
Expand Down Expand Up @@ -3707,18 +3734,12 @@ function eme_store_member_answers( $member, $do_update = 1 ) {
$extra_charge += $answer['answer'];
}
if ( $do_update ) {
if ( $answer['field_purpose'] == 'people' ) {
$answer_id = eme_get_answerid( $all_answers, $person_id, 'person', $answer['field_id'], $answer['grouping_id'], $answer['occurence_id'] );
} else {
$answer_id = eme_get_answerid( $all_answers, $member_id, 'member', $answer['field_id'], $answer['grouping_id'], $answer['occurence_id'] );
}
$answer_id = eme_get_answerid( $all_answers, $answer['related_id'], $answer['type'], $answer['field_id'], $answer['eme_grouping'], $answer['occurence'] );
if ( $answer_id ) {
eme_update_answer( $answer_id, $answer['answer'] );
$answer_ids_seen[] = $answer_id;
} elseif ( $answer['field_purpose'] == 'people' ) {
$answer_id = eme_insert_answer( 'person', $person_id, $answer['field_id'], $answer['answer'], $answer['grouping_id'], $answer['occurence_id'] );
} else {
$answer_id = eme_insert_answer( 'member', $member_id, $answer['field_id'], $answer['answer'], $answer['grouping_id'], $answer['occurence_id'] );
} elseif ($answer['related_id']) {
$answer_id = eme_insert_answer( $answer['type'], $answer['related_id'], $answer['field_id'], $answer['answer'], $answer['grouping_id'], $answer['occurence'] );
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions eme-recurrence.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ function eme_insert_events_for_recurrence( $recurrence, $event ) {
$event['event_end'] = "$event_end_date $event_end_time";
$event_id = eme_db_insert_event( $event, 1, $day_difference );
if ( $event_id ) {
eme_event_store_cf_answers( $event_id );
eme_event_store_answers( $event_id );
}
++$count;
}
Expand Down Expand Up @@ -431,7 +431,7 @@ function eme_update_events_for_recurrence( $recurrence, $event, $only_change_rec
$event['event_end'] = "$event_end_date $event_end_time";
$res = eme_db_update_event( $event, $existing_event['event_id'], 1, $day_difference );
if ( $res ) {
eme_event_store_cf_answers( $existing_event['event_id'] );
eme_event_store_answers( $existing_event['event_id'] );
}
}
// we handled a specific day, so remove it from the array
Expand Down
51 changes: 41 additions & 10 deletions eme-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2042,14 +2042,26 @@ function eme_get_booking_post_answers( $booking, $include_dynamicdata = 1 ) {
} else {
$value = eme_sanitize_request( $value );
}
if ($formfield['field_purpose'] == 'people') {
$type = 'person';
$related_id = isset($booking['person_id'])?$booking['person_id']:0;
} else {
$type = 'booking';
$related_id = isset($booking['booking_id'])?$booking['booking_id']:0;
}
// some extra fields are added, so people can use these to check things: field_name, field_purpose, extra_charge (also used in code), grouping_id and occurence_id
$answer = [
'field_name' => $formfield['field_name'],
'field_id' => $field_id,
'field_purpose' => $formfield['field_purpose'],
'extra_charge' => $formfield['extra_charge'],
'answer' => $value,
'grouping_id' => 0,
'grouping_id ' => 0,
'occurence_id' => 0,
'eme_grouping' => 0,
'occurence' => 0,
'type' => $type,
'related_id' => $related_id,
];
$answers[] = $answer;
}
Expand Down Expand Up @@ -2083,6 +2095,14 @@ function eme_get_booking_post_answers( $booking, $include_dynamicdata = 1 ) {
} else {
$value = eme_sanitize_request( $value );
}
if ($formfield['field_purpose'] == 'people') {
$type = 'person';
$related_id = isset($booking['person_id'])?$booking['person_id']:0;
} else {
$type = 'booking';
$related_id = isset($booking['booking_id'])?$booking['booking_id']:0;
}
// some extra fields are added, so people can use these to check things: field_name, field_purpose, extra_charge (also used in code), grouping_id and occurence_id
$answer = [
'field_name' => $formfield['field_name'],
'field_id' => $field_id,
Expand All @@ -2091,6 +2111,10 @@ function eme_get_booking_post_answers( $booking, $include_dynamicdata = 1 ) {
'answer' => $value,
'grouping_id' => intval($group_id),
'occurence_id' => intval($occurence_id),
'eme_grouping' => intval($group_id),
'occurence' => intval($occurence_id),
'type' => $type,
'related_id' => $related_id,
];
$answers[] = $answer;
}
Expand Down Expand Up @@ -2122,14 +2146,26 @@ function eme_get_booking_post_answers( $booking, $include_dynamicdata = 1 ) {
} else {
$value = eme_sanitize_request( $value );
}
if ($formfield['field_purpose'] == 'people') {
$type = 'person';
$related_id = isset($booking['person_id'])?$booking['person_id']:0;
} else {
$type = 'booking';
$related_id = isset($booking['booking_id'])?$booking['booking_id']:0;
}
// some extra fields are added, so people can use these to check things: field_name, field_purpose, extra_charge (also used in code), grouping_id and occurence_id
$answer = [
'field_name' => $formfield['field_name'],
'field_id' => $field_id,
'field_purpose' => $formfield['field_purpose'],
'extra_charge' => $formfield['extra_charge'],
'answer' => $value,
'grouping_id' => 0,
'grouping_id ' => 0,
'occurence_id' => 0,
'eme_grouping' => 0,
'occurence' => 0,
'type' => $type,
'related_id' => $related_id,
];
$answers[] = $answer;
}
Expand Down Expand Up @@ -2162,6 +2198,7 @@ function eme_store_booking_answers( $booking, $do_update = 1 ) {
} else {
$booking_id = 0;
}
$booking['booking_id']=$booking_id;

$answer_ids_seen = [];
$found_answers = eme_get_booking_post_answers( $booking );
Expand All @@ -2170,18 +2207,12 @@ function eme_store_booking_answers( $booking, $do_update = 1 ) {
$extra_charge += $answer['answer'];
}
if ( $do_update ) {
if ( $answer['field_purpose'] == 'people' ) {
$answer_id = eme_get_answerid( $all_answers, $person_id, 'person', $answer['field_id'], $answer['grouping_id'], $answer['occurence_id'] );
} else {
$answer_id = eme_get_answerid( $all_answers, $booking_id, 'booking', $answer['field_id'], $answer['grouping_id'], $answer['occurence_id'] );
}
$answer_id = eme_get_answerid( $all_answers, $answer['related_id'], $answer['type'], $answer['field_id'], $answer['eme_grouping'], $answer['occurence'] );
if ( $answer_id ) {
eme_update_answer( $answer_id, $answer['answer'] );
$answer_ids_seen[] = $answer_id;
} elseif ( $answer['field_purpose'] == 'people' ) {
$answer_id = eme_insert_answer( 'person', $person_id, $answer['field_id'], $answer['answer'], $answer['grouping_id'], $answer['occurence_id'] );
} else {
$answer_id = eme_insert_answer( 'booking', $booking_id, $answer['field_id'], $answer['answer'], $answer['grouping_id'], $answer['occurence_id'] );
$answer_id = eme_insert_answer( $answer['type'], $answer['related_id'], $answer['field_id'], $answer['answer'], $answer['eme_grouping'], $answer['occurence'] );
}
}
}
Expand Down

0 comments on commit 6f5a3fa

Please sign in to comment.