Skip to content

Commit

Permalink
CampTix: Add UI for changing attendee status (#390)
Browse files Browse the repository at this point in the history
Adds a status dropdown to the edit attendee screen, mirroring how core's post status works. This adds "publish", "refund", and "cancel" as possible statuses. It also updates bulk and quick edit on the list table to allow marking attendees cancelled or refunded in batches.
  • Loading branch information
ryelle committed Mar 18, 2020
1 parent 7d706e2 commit 73c9aaf
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 6 deletions.
Expand Up @@ -25,7 +25,7 @@
// Attendees
add_filter( 'camptix_name_order', __NAMESPACE__ . '\set_name_order' );
add_action( 'camptix_form_edit_attendee_custom_error_flags', __NAMESPACE__ . '\disable_attendee_edits' );
add_action( 'transition_post_status', __NAMESPACE__ . '\log_publish_to_cancel', 10, 3 );
add_action( 'publish_to_cancel', __NAMESPACE__ . '\log_publish_to_cancel', 10, 3 );
add_filter( 'camptix_privacy_erase_attendee', __NAMESPACE__ . '\retain_attendee_data', 10, 2 );
add_action( 'admin_notices', __NAMESPACE__ . '\admin_notice_attendee_privacy' );
add_filter( 'wp_privacy_personal_data_erasers', __NAMESPACE__ . '\modify_erasers', 99 );
Expand Down Expand Up @@ -497,19 +497,18 @@ function disable_attendee_edits( $attendee ) {
/**
* Log when published attendees are cancelled.
*
* @param string $to
* @param string $from
* @param WP_Post $post
*/
function log_publish_to_cancel( $to, $from, $post ) {
function log_publish_to_cancel( $post ) {
/** @var $camptix CampTix_Plugin */
global $camptix;

if ( 'tix_attendee' !== $post->post_type || $to === $from ) {
if ( 'tix_attendee' !== $post->post_type ) {
return;
}

if ( 'publish' === $from && 'cancel' === $to ) {
// Not a manual change, should be logged.
if ( 0 === get_current_user_id() ) {
$camptix->log( 'Publish to cancel transition, possible bug.', $post->ID );
}
}
Expand Down
37 changes: 37 additions & 0 deletions public_html/wp-content/plugins/camptix/admin.js
Expand Up @@ -538,6 +538,43 @@ window.camptix = window.camptix || { models: {}, views: {}, collections: {} };
return false;
});

// Edit post status on attendees.
if ( $( '#tix_attendee_submitdiv' ).length ) {
var $postStatusSelect = $('#post-status-select');
function updateText() {
// Update "Status:" to currently selected status.
$('#post-status-display').text(
// Remove any potential tags from post status text.
wp.sanitize.stripTagsAndEncodeText( $('option:selected', $postStatusSelect).text() )
);
}

// Post Status edit click.
$postStatusSelect.siblings('a.edit-post-status').click( function( event ) {
if ( $postStatusSelect.is( ':hidden' ) ) {
$postStatusSelect.slideDown( 'fast', function() {
$postStatusSelect.find('select').focus();
} );
$(this).hide();
}
event.preventDefault();
});

// Save the Post Status changes and hide the options.
$postStatusSelect.find('.save-post-status').click( function( event ) {
$postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().focus();
updateText();
event.preventDefault();
});

// Cancel Post Status editing and hide the options.
$postStatusSelect.find('.cancel-post-status').click( function( event ) {
$postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().focus();
$('#post_status').val( $('#hidden_post_status').val() );
updateText();
event.preventDefault();
});
}

/*
* Track Attendance addon
Expand Down
85 changes: 85 additions & 0 deletions public_html/wp-content/plugins/camptix/camptix.php
Expand Up @@ -130,6 +130,9 @@ function init() {
add_action( 'save_post', array( $this, 'save_attendee_post' ) );
add_action( 'save_post', array( $this, 'save_coupon_post' ) );

// Log attendee status changes.
add_action( 'transition_post_status', array( $this, 'log_attendee_status_change' ), 10, 3 );

// Handle query extras for attendees, tickets, etc.
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );

Expand Down Expand Up @@ -164,6 +167,9 @@ function init() {

// Change updated messages
add_filter( 'post_updated_messages', array( $this, 'ticket_updated_messages' ) );

// Add post statuses to bulk & quick edit.
add_action( 'admin_footer-edit.php', array( $this, 'append_post_status_bulk_edit' ) );

do_action( 'camptix_init' );
}
Expand Down Expand Up @@ -3921,6 +3927,31 @@ function metabox_attendee_submitdiv() {
<?php _e( 'Unknown status', 'wordcamporg' ); ?>
<?php endif; ?>
</span>
<?php if ( current_user_can( 'manage_sites' ) ) : ?>
<a href="#post_status" class="edit-post-status hide-if-no-js" role="button" style="display: inline;">
<span aria-hidden="true"><?php esc_html_e( 'Edit', 'wordcamporg' ); ?></span>
<span class="screen-reader-text"><?php esc_html_e( 'Edit status', 'wordcamporg' ); ?></span>
</a>
<div id="post-status-select" class="hide-if-js" style="display: none;">
<input type="hidden" name="hidden_post_status" id="hidden_post_status" value="<?php echo esc_attr( $post->post_status ); ?>">
<label for="post_status" class="screen-reader-text"><?php esc_html_e( 'Set status', 'wordcamporg' ); ?></label>
<select name="post_status" id="post_status">
<?php if ( ! in_array( $post_status_object->name, array( 'publish', 'refund', 'cancel' ) ) ) : ?>
<option
<?php selected( $post->post_status, $post_status_object->name ); ?>
value="<?php echo esc_attr( $post_status_object->name ); ?>"
>
<?php echo esc_html( $post_status_object->label ); ?>
</option>
<?php endif; ?>
<option <?php selected( $post->post_status, 'publish' ); ?> value="publish"><?php _e( 'Published', 'wordcamporg' ); ?></option>
<option <?php selected( $post->post_status, 'refund' ); ?> value="refund"><?php _e( 'Refunded', 'wordcamporg' ); ?></option>
<option <?php selected( $post->post_status, 'cancel' ); ?> value="cancel"><?php _e( 'Cancelled', 'wordcamporg' ); ?></option>
</select>
<a href="#post_status" class="save-post-status hide-if-no-js button"><?php esc_html_e( 'OK', 'wordcamporg' ); ?></a>
<a href="#post_status" class="cancel-post-status hide-if-no-js button-cancel"><?php esc_html_e( 'Cancel', 'wordcamporg' ); ?></a>
</div>
<?php endif; ?>
</div>

<?php
Expand Down Expand Up @@ -3983,6 +4014,38 @@ function metabox_attendee_submitdiv() {
<?php
}

/**
* Adding custom post status to status dropdown in Bulk and Quick Edit.
*/
function append_post_status_bulk_edit() {
$screen = get_current_screen();
if ( $screen && 'edit-tix_attendee' !== $screen->id ) {
return;
}
$statuses = array(
'publish' => _x( 'Published', 'post', 'wordcamporg' ),
'refund' => _x( 'Refunded', 'post', 'wordcamporg' ),
'cancel' => _x( 'Cancelled', 'post', 'wordcamporg' ),
);

?>
<script>
jQuery( document ).ready( function($) {
<?php if ( ! current_user_can( 'manage_sites' ) ) : ?>
$( '.inline-edit-status' ).remove();
<?php else: ?>
$( '.inline-edit-status select' ).empty();
<?php foreach ( $statuses as $slug => $label ) : ?>
$( '.inline-edit-status select' ).append( "<?php printf(
'<option value=\"%s\">%s</option>', esc_attr( $slug ), esc_html( $label )
); ?>" );
<?php endforeach; ?>
<?php endif; ?>
});
</script>
<?php
}

/**
* Metabox callback for ticket options.
*/
Expand Down Expand Up @@ -4964,6 +5027,28 @@ function save_coupon_post( $post_id ) {
$this->log( 'Saved coupon post with form data.', $post_id, $_POST );
}

/**
* Log status changes in Attendees.
*/
function log_attendee_status_change( $new_status, $old_status, $post ) {
if ( $old_status === $new_status || 'tix_attendee' !== $post->post_type ) {
return;
}

$current_user = wp_get_current_user();
if ( 0 !== $current_user->ID ) {
$this->log(
sprintf(
'Attendee manually changed from %1$s to %2$s by %3$s.',
$old_status,
$new_status,
$current_user->user_login
),
$post->ID
);
}
}

/**
* A bunch of magic is happening here.
*/
Expand Down

0 comments on commit 73c9aaf

Please sign in to comment.