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

Add the delete button in the edit form of an event #69

Merged
merged 14 commits into from
Feb 16, 2024
Merged
32 changes: 27 additions & 5 deletions assets/js/translation-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,29 @@ jQuery(document).ready(function($) {
});
});

function validateEventDates() {
$('.delete-event').on('click', function(e) {
e.preventDefault();
if ( ! confirm( 'Are you sure you want to delete this event?' ) ) {
return;
}
var $form = $('.translation-event-form');
$('#form-name').val('delete_event');
$('#event-form-action').val( 'delete' );
$.ajax({
type: 'POST',
url: $translation_event.url,
data:$form.serialize(),
success: function(response) {
if ( response.data.eventId ) {
history.back();
amieiro marked this conversation as resolved.
Show resolved Hide resolved
}
},
error: function(error) {
$gp.notices.error(response.data.message);
},
});
});
function validateEventDates() {
var startDateTimeInput = $('#event-start');
var endDateTimeInput = $('#event-end');
if ( ! startDateTimeInput.length || ! endDateTimeInput.length ) {
Expand All @@ -65,7 +87,7 @@ jQuery(document).ready(function($) {
});
}
function selectUserTimezone() {
document.querySelector(`#event-timezone option[value="${Intl.DateTimeFormat().resolvedOptions().timeZone}"]`).selected = true
document.querySelector(`#event-timezone option[value="${Intl.DateTimeFormat().resolvedOptions().timeZone}"]`).selected = true
}

function convertToUserLocalTime() {
Expand All @@ -80,11 +102,11 @@ jQuery(document).ready(function($) {
var userLocalDateTime = new Date(eventDateObj.getTime() - userTimezoneOffsetMs);

var options = { weekday: 'short', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', hour12: true, timeZoneName: 'short' };

timeEl.textContent = userLocalDateTime.toLocaleString('en-US', options);
});
}

});
}( jQuery, $gp )
);
);
19 changes: 19 additions & 0 deletions includes/class-wporg-gp-translation-events-route.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public function events_edit( $event_id ) {
if ( ! $event || 'event' !== $event->post_type || ! ( current_user_can( 'edit_post', $event->ID ) || intval( $event->post_author ) === get_current_user_id() ) ) {
$this->die_with_error( 'Event does not exist, or you do not have permission to edit it.', 403 );
}
if ( 'trash' === $event->post_status ) {
$this->die_with_error( 'You cannot edit a trashed event', 403 );
}

include ABSPATH . 'wp-admin/includes/post.php';
$event_form_title = 'Edit Event';
Expand All @@ -123,6 +126,22 @@ public function events_edit( $event_id ) {
$event_status = $event->post_status;
list( $permalink, $post_name ) = get_sample_permalink( $event_id );
$permalink = str_replace( '%pagename%', $post_name, $permalink );
$show_delete_button = false;
if ('Edit Event' === $event_form_title) {
$event = get_post( $event_id );
amieiro marked this conversation as resolved.
Show resolved Hide resolved
$stats_calculator = new WPORG_GP_Translation_Events_Stats_Calculator();
try {
$event_stats = $stats_calculator->for_event($event);
} catch ( Exception $e ) {
$this->die_with_error( 'Failed to calculate event stats' );
}
if ( empty( $event_stats->rows() ) ) {
amieiro marked this conversation as resolved.
Show resolved Hide resolved
$current_user = wp_get_current_user();
if ( $current_user->ID === $event->post_author || current_user_can( 'manage_options' ) ) {
$show_delete_button = true;
}
}
}
$this->tmpl( 'events-form', get_defined_vars() );
}

Expand Down
5 changes: 4 additions & 1 deletion templates/events-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
gp_title( __( 'Translation Events' ) . ' - ' . esc_html( $event_form_title . ' - ' . $event_title ) );
gp_tmpl_header();
gp_tmpl_load( 'events-header', get_defined_vars(), dirname( __FILE__ ) );

?>

<h2 class="event-page-title"><?php echo esc_html( $event_form_title ); ?></h2>
Expand Down Expand Up @@ -62,5 +61,9 @@
<button class="button is-primary save-draft submit-event" type="submit" data-event-status="draft">Save Draft</button>
<button class="button is-primary submit-event" type="submit" data-event-status="publish">Publish Event</button>
<?php endif; ?>
<?php if ( isset( $show_delete_button ) && $show_delete_button ) : ?>
<button class="button is-destructive delete-event" type="submit" name="submit" value="Delete" id="submit">Delete Event</button>
<?php endif; ?>
</div>
</form>

43 changes: 32 additions & 11 deletions wporg-gp-translation-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function validate_event_dates( string $event_start, string $event_end ): bool {
function submit_event_ajax() {
$event_id = null;
$response_message = '';
$form_actions = array( 'draft', 'publish' );
$form_actions = array( 'draft', 'publish', 'delete' );

$is_nonce_valid = false;
$nonce_name = '_event_nonce';
Expand Down Expand Up @@ -179,7 +179,7 @@ function submit_event_ajax() {
wp_send_json_error( 'Form name must be set' );
}
$action = sanitize_text_field( wp_unslash( $_POST['form_name'] ) );
if ( ! in_array( $action, array( 'create_event', 'edit_event' ), true ) ) {
if ( ! in_array( $action, array( 'create_event', 'edit_event', 'delete_event' ), true ) ) {
wp_send_json_error( 'Invalid form name' );
}

Expand Down Expand Up @@ -213,18 +213,40 @@ function submit_event_ajax() {
);
$response_message = 'Event updated successfully!';
}
if ( 'delete_event' === $_POST['form_name'] ) {
amieiro marked this conversation as resolved.
Show resolved Hide resolved
$event_id = sanitize_text_field( wp_unslash( $_POST['event_id'] ) );
$event = get_post( $event_id );
if ( ! $event || 'event' !== $event->post_type ) {
wp_send_json_error( 'Event does not exist' );
}
if ( ! ( current_user_can( 'delete_post', $event->ID ) || get_current_user_id() === $event->post_author ) ) {
wp_send_json_error( 'You do not have permission to delete this event' );
}
$stats_calculator = new WPORG_GP_Translation_Events_Stats_Calculator();
try {
$event_stats = $stats_calculator->for_event( $event );
} catch ( Exception $e ) {
wp_send_json_error( 'Failed to calculate event stats' );
}
if ( ! empty( $event_stats->rows() ) ) {
wp_send_json_error( 'Event has translations and cannot be deleted' );
}
wp_trash_post( $event_id );
$response_message = 'Event deleted successfully!';
}
if ( ! $event_id ) {
wp_send_json_error( 'Event could not be created or updated' );
}
try {
update_post_meta( $event_id, '_event_start', convert_to_utc( $event_start, $event_timezone ) );
update_post_meta( $event_id, '_event_end', convert_to_utc( $event_end, $event_timezone ) );
} catch ( Exception $e ) {
wp_send_json_error( 'Invalid start or end' );
}

update_post_meta( $event_id, '_event_timezone', $event_timezone );
if ( 'delete_event' !== $_POST['form_name'] ) {
try {
update_post_meta( $event_id, '_event_start', convert_to_utc( $event_start, $event_timezone ) );
update_post_meta( $event_id, '_event_end', convert_to_utc( $event_end, $event_timezone ) );
} catch ( Exception $e ) {
wp_send_json_error( 'Invalid start or end' );
}

update_post_meta( $event_id, '_event_timezone', $event_timezone );
}
try {
WPORG_GP_Translation_Events_Active_Events_Cache::invalidate();
} catch ( Exception $e ) {
Expand All @@ -234,7 +256,6 @@ function submit_event_ajax() {

list( $permalink, $post_name ) = get_sample_permalink( $event_id );
$permalink = str_replace( '%pagename%', $post_name, $permalink );

wp_send_json_success(
array(
'message' => $response_message,
Expand Down
Loading