'evwr_export_attendees' );
if ( $event_id ) {
$args['evwr_event'] = (int) $event_id;
}
return wp_nonce_url( add_query_arg( $args, admin_url( 'admin-post.php' ) ), 'evwr_export_attendees' );
}
/**
* Columns.
*
* @param array $cols Columns.
* @return array
*/
public static function columns( $cols ) {
return array(
'cb' => isset( $cols['cb'] ) ? $cols['cb'] : '',
'title' => __( 'Name', 'eventwright' ),
'evwr_email' => __( 'Email', 'eventwright' ),
'evwr_event' => __( 'Event', 'eventwright' ),
'evwr_guests' => __( 'People', 'eventwright' ),
'evwr_status' => __( 'Status', 'eventwright' ),
'date' => __( 'Signed up', 'eventwright' ),
);
}
/**
* Status labels.
*
* @return array
*/
public static function statuses() {
return \Eventwright\RSVP::statuses();
}
/**
* Column content.
*
* @param string $col Column.
* @param int $post_id Post.
*/
public static function column( $col, $post_id ) {
switch ( $col ) {
case 'evwr_email':
$email = get_post_meta( $post_id, '_evwr_email', true );
echo $email ? '' . esc_html( $email ) . '' : '—';
$phone = get_post_meta( $post_id, '_evwr_phone', true );
if ( $phone ) {
echo '
' . esc_html( $phone );
}
break;
case 'evwr_event':
$event_id = (int) get_post_meta( $post_id, '_evwr_event_id', true );
if ( $event_id && get_post( $event_id ) ) {
echo '' . esc_html( get_the_title( $event_id ) ) . '';
} else {
echo '—';
}
$occ = get_post_meta( $post_id, '_evwr_occurrence', true );
if ( $occ ) {
echo '
' . esc_html( eventwright_format_local( eventwright_date_format(), $occ ) ) . '';
}
break;
case 'evwr_guests':
echo esc_html( (string) max( 1, (int) get_post_meta( $post_id, '_evwr_guests', true ) ) );
break;
case 'evwr_status':
$s = get_post_meta( $post_id, '_evwr_status', true );
$labels = self::statuses();
echo '' . esc_html( isset( $labels[ $s ] ) ? $labels[ $s ] : $s ) . '';
break;
}
}
/**
* Event and status filters.
*
* @param string $post_type Post type.
*/
public static function filters( $post_type ) {
if ( Post_Types::ATTENDEE !== $post_type ) {
return;
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- List filters.
$event = isset( $_GET['evwr_event'] ) ? absint( $_GET['evwr_event'] ) : 0;
$status = isset( $_GET['evwr_status'] ) ? sanitize_key( wp_unslash( $_GET['evwr_status'] ) ) : '';
// phpcs:enable
$events = get_posts(
array(
'post_type' => Post_Types::EVENT,
'post_status' => array( 'publish', 'future', 'private' ),
'posts_per_page' => 300,
'meta_key' => '_evwr_rsvp_enabled', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => '1', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'orderby' => 'date',
'order' => 'DESC',
)
);
echo '';
echo '';
echo '';
echo '';
}
/**
* Apply the filters; search also matches email.
*
* @param \WP_Query $q Query.
*/
public static function filter_query( $q ) {
if ( ! is_admin() || ! $q->is_main_query() || Post_Types::ATTENDEE !== $q->get( 'post_type' ) ) {
return;
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- List filters.
$meta = array();
if ( ! empty( $_GET['evwr_event'] ) ) {
$meta[] = array(
'key' => '_evwr_event_id',
'value' => (string) absint( $_GET['evwr_event'] ),
);
}
if ( ! empty( $_GET['evwr_status'] ) ) {
$meta[] = array(
'key' => '_evwr_status',
'value' => sanitize_key( wp_unslash( $_GET['evwr_status'] ) ),
);
}
// phpcs:enable
if ( $meta ) {
$q->set( 'meta_query', $meta ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
}
}
/**
* Export button above the list.
*
* @param string $which Top or bottom.
*/
public static function export_button( $which ) {
$screen = get_current_screen();
if ( 'top' !== $which || ! $screen || 'edit-' . Post_Types::ATTENDEE !== $screen->id ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- List filter.
$event = isset( $_GET['evwr_event'] ) ? absint( $_GET['evwr_event'] ) : 0;
echo '
' . esc_html__( 'Event:', 'eventwright' ) . ' '; echo $event_id ? '' . esc_html( get_the_title( $event_id ) ) . '' : '—'; $occ = get_post_meta( $post->ID, '_evwr_occurrence', true ); if ( $occ ) { echo ' (' . esc_html( eventwright_format_local( eventwright_date_format(), $occ ) ) . ')'; } echo '
'; echo ''; echo ''; echo ''; echo ''; } /** * Save edit box. * * @param int $post_id Post. * @param \WP_Post $post Post. */ public static function save( $post_id, $post ) { if ( ! isset( $_POST['_evwr_attendee_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_evwr_attendee_nonce'] ) ), 'evwr_save_attendee' ) || ! current_user_can( 'edit_post', $post_id ) ) { return; } $in = isset( $_POST['evwr_att'] ) && is_array( $_POST['evwr_att'] ) ? wp_unslash( $_POST['evwr_att'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitised per key. update_post_meta( $post_id, '_evwr_email', isset( $in['email'] ) ? sanitize_email( $in['email'] ) : '' ); update_post_meta( $post_id, '_evwr_phone', isset( $in['phone'] ) ? sanitize_text_field( $in['phone'] ) : '' ); update_post_meta( $post_id, '_evwr_guests', isset( $in['guests'] ) ? max( 1, absint( $in['guests'] ) ) : 1 ); $status = isset( $in['status'] ) ? sanitize_key( $in['status'] ) : 'going'; update_post_meta( $post_id, '_evwr_status', array_key_exists( $status, self::statuses() ) ? $status : 'going' ); // Fewer people going frees places for the waitlist. $event_id = (int) get_post_meta( $post_id, '_evwr_event_id', true ); if ( $event_id ) { \Eventwright\RSVP::promote_waitlist( $event_id, (string) get_post_meta( $post_id, '_evwr_occurrence', true ) ); } } /** * Make a CSV cell safe from spreadsheet formula injection. * * @param string $value Value. * @return string */ public static function csv_safe( $value ) { $value = (string) $value; if ( '' !== $value && in_array( $value[0], array( '=', '+', '-', '@', "\t", "\r" ), true ) ) { $value = "'" . $value; } return $value; } /** * CSV rows (header first), already made safe against formula injection. * * @param int $event_id Event (0 = all). * @return array[] */ public static function csv_rows( $event_id = 0 ) { $args = array( 'post_type' => Post_Types::ATTENDEE, 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'ASC', ); if ( $event_id ) { $args['meta_key'] = '_evwr_event_id'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key $args['meta_value'] = (string) $event_id; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value } $rows = array( array( 'Name', 'Email', 'Phone', 'People', 'Status', 'Event', 'Date', 'Signed up' ) ); $statuses = self::statuses(); foreach ( get_posts( $args ) as $p ) { $eid = (int) get_post_meta( $p->ID, '_evwr_event_id', true ); $s = get_post_meta( $p->ID, '_evwr_status', true ); $occ = get_post_meta( $p->ID, '_evwr_occurrence', true ); if ( ! $occ && $eid ) { $occ = get_post_meta( $eid, '_evwr_start', true ); } $rows[] = array_map( array( __CLASS__, 'csv_safe' ), array( html_entity_decode( get_the_title( $p ), ENT_QUOTES, 'UTF-8' ), get_post_meta( $p->ID, '_evwr_email', true ), get_post_meta( $p->ID, '_evwr_phone', true ), max( 1, (int) get_post_meta( $p->ID, '_evwr_guests', true ) ), isset( $statuses[ $s ] ) ? $statuses[ $s ] : $s, html_entity_decode( get_the_title( $eid ), ENT_QUOTES, 'UTF-8' ), $occ ? substr( $occ, 0, 16 ) : '', $p->post_date, ) ); } return $rows; } /** * Download attendees as CSV. */ public static function export() { check_admin_referer( 'evwr_export_attendees' ); $event_id = isset( $_GET['evwr_event'] ) ? absint( $_GET['evwr_event'] ) : 0; if ( $event_id ? ! current_user_can( 'edit_post', $event_id ) : ! current_user_can( 'edit_others_posts' ) ) { wp_die( esc_html__( 'You are not allowed to export these attendees.', 'eventwright' ), 403 ); } $filename = 'attendees' . ( $event_id ? '-' . sanitize_title( get_the_title( $event_id ) ) : '' ) . '-' . gmdate( 'Y-m-d' ) . '.csv'; nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); $out = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- Streaming download. fwrite( $out, "\xEF\xBB\xBF" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- UTF-8 BOM so spreadsheets read accents. foreach ( self::csv_rows( $event_id ) as $row ) { fputcsv( $out, $row ); } fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose exit; } }