$t ) {
$out[ $slug ] = $t;
if ( $after === $slug ) {
$out[ self::PAGE ] = array( __( 'Gatherings', 'nodebook' ), 0 );
}
}
if ( ! isset( $out[ self::PAGE ] ) ) {
$out[ self::PAGE ] = array( __( 'Gatherings', 'nodebook' ), 0 );
}
return $out;
}
public static function notices( $map ) {
return $map + array(
'gsaved' => array( 'success', __( 'Gathering saved.', 'nodebook' ) ),
'gdeleted' => array( 'success', __( 'Gathering deleted. The deals of its guests are kept.', 'nodebook' ) ),
'gadded' => array( 'success', __( 'Added to the roster.', 'nodebook' ) ),
'gwait' => array( 'warning', __( 'It is full, so they are on the waitlist. Record a payment to give them a spot anyway.', 'nodebook' ) ),
'gpaid' => array( 'success', __( 'Payment recorded.', 'nodebook' ) ),
'gupdated' => array( 'success', __( 'Roster updated.', 'nodebook' ) ),
'ginvited' => array( 'success', __( 'Past guests added as interested. Time to send them a note!', 'nodebook' ) ),
'gfail' => array( 'error', __( 'That did not work. Give the gathering a name, and pick a contact or type an email address.', 'nodebook' ) ),
);
}
private static function guard( $nonce, $cap = '' ) {
if ( ! current_user_can( $cap ? $cap : Nodebook_Admin::cap() ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'nodebook' ), 403 );
}
check_admin_referer( $nonce );
}
private static function id_from_post( $key = 'id' ) {
return isset( $_POST[ $key ] ) ? absint( $_POST[ $key ] ) : 0;
}
/* ------------------------------------------------------------------ *
* Handlers
* ------------------------------------------------------------------ */
public static function do_save() {
$id = self::id_from_post();
self::guard( 'nodebook_gathering_save_' . $id, 'manage_options' );
$in = array();
foreach ( array( 'title', 'starts_on', 'ends_on', 'location', 'capacity', 'price', 'deposit', 'currency', 'balance_days', 'page_id', 'product_id', 'match_text', 'status', 'notes' ) as $k ) {
if ( isset( $_POST[ $k ] ) ) {
$in[ $k ] = 'notes' === $k ? sanitize_textarea_field( wp_unslash( $_POST[ $k ] ) ) : sanitize_text_field( wp_unslash( $_POST[ $k ] ) );
}
}
$new = Nodebook_Gatherings::save( $in, $id );
if ( ! $new ) {
Nodebook_Admin::redirect( Nodebook_Gatherings::url( $id, $id ? array( 'edit' => 1 ) : array( 'new' => 1 ) ), 'gfail' );
}
Nodebook_Node::flush();
Nodebook_Admin::redirect( Nodebook_Gatherings::url( $new ), 'gsaved' );
}
public static function do_delete() {
$id = self::id_from_post();
self::guard( 'nodebook_gathering_delete_' . $id, 'manage_options' );
Nodebook_Gatherings::delete( $id );
Nodebook_Node::flush();
Nodebook_Admin::redirect( Nodebook_Gatherings::url(), 'gdeleted' );
}
public static function do_add() {
$id = self::id_from_post();
self::guard( 'nodebook_gathering_add_' . $id );
$back = isset( $_POST['back'] ) && 'contact' === sanitize_key( wp_unslash( $_POST['back'] ) );
$cid = isset( $_POST['contact_id'] ) ? absint( $_POST['contact_id'] ) : 0;
if ( $cid ) {
$cid = Nodebook_Contacts::get( $cid ) && Nodebook_Team::can_see( $cid ) ? $cid : -1;
} else {
$cid = Nodebook_Admin_Deals::resolve_contact( isset( $_POST['contact'] ) ? sanitize_text_field( wp_unslash( $_POST['contact'] ) ) : '' );
}
$g = Nodebook_Gatherings::get( $id );
if ( ! $g || $cid <= 0 ) {
Nodebook_Admin::redirect( $back && $cid > 0 ? Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $cid ) ) : Nodebook_Gatherings::url( $id ) . '#nb-roster', 'gfail' );
}
$status = isset( $_POST['status'] ) ? sanitize_key( wp_unslash( $_POST['status'] ) ) : 'interested';
$pid = Nodebook_Gatherings::add(
$id,
$cid,
array(
'status' => in_array( $status, array( 'interested', 'reserved', 'waitlist' ), true ) ? $status : 'interested',
'seats' => isset( $_POST['seats'] ) ? absint( $_POST['seats'] ) : 1,
'source' => __( 'Added by hand', 'nodebook' ),
)
);
$p = $pid ? Nodebook_Gatherings::person( $pid ) : null;
$notice = $p && 'reserved' === $status && 'waitlist' === $p['status'] ? 'gwait' : ( $pid ? 'gadded' : 'gfail' );
Nodebook_Admin::redirect( $back ? Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => $cid ) ) . '#nb-gatherings' : Nodebook_Gatherings::url( $id ) . '#nb-roster', $notice );
}
/** Roster row changes: status, seats, amount owed, attended, remove. */
public static function do_person() {
$pid = self::id_from_post( 'pid' );
self::guard( 'nodebook_gathering_person_' . $pid );
$p = Nodebook_Gatherings::person( $pid );
if ( ! $p || ! Nodebook_Team::can_see( (int) $p['contact_id'] ) ) {
Nodebook_Admin::redirect( Nodebook_Gatherings::url(), 'gfail' );
}
$op = isset( $_POST['op'] ) ? sanitize_key( wp_unslash( $_POST['op'] ) ) : '';
if ( 'remove' === $op ) {
Nodebook_Gatherings::remove( $pid );
} else {
$in = array();
if ( isset( $_POST['status'] ) ) {
$in['status'] = sanitize_key( wp_unslash( $_POST['status'] ) );
}
if ( isset( $_POST['seats'] ) ) {
$in['seats'] = absint( $_POST['seats'] );
}
if ( isset( $_POST['amount'] ) ) {
$v = Nodebook_Props::to_number( sanitize_text_field( wp_unslash( $_POST['amount'] ) ) );
if ( null !== $v ) {
$in['amount'] = $v;
}
}
if ( 'attended' === $op ) {
$in['attended'] = ! empty( $_POST['attended'] );
}
Nodebook_Gatherings::update( $pid, $in );
}
Nodebook_Admin::redirect( Nodebook_Gatherings::url( (int) $p['event_id'] ) . '#nb-p-' . $pid, 'gupdated' );
}
public static function do_pay() {
$pid = self::id_from_post( 'pid' );
self::guard( 'nodebook_gathering_pay_' . $pid );
$p = Nodebook_Gatherings::person( $pid );
$amount = Nodebook_Props::to_number( isset( $_POST['amount'] ) ? sanitize_text_field( wp_unslash( $_POST['amount'] ) ) : '' );
if ( ! $p || ! Nodebook_Team::can_see( (int) $p['contact_id'] ) || null === $amount || 0.0 === (float) $amount ) {
Nodebook_Admin::redirect( $p ? Nodebook_Gatherings::url( (int) $p['event_id'] ) . '#nb-roster' : Nodebook_Gatherings::url(), 'gfail' );
}
$refund = ! empty( $_POST['refund'] );
Nodebook_Gatherings::pay( $pid, $refund ? -abs( (float) $amount ) : (float) $amount, isset( $_POST['note'] ) ? sanitize_text_field( wp_unslash( $_POST['note'] ) ) : '' );
Nodebook_Admin::redirect( Nodebook_Gatherings::url( (int) $p['event_id'] ) . '#nb-p-' . $pid, 'gpaid' );
}
public static function do_invite() {
$id = self::id_from_post();
self::guard( 'nodebook_gathering_invite_' . $id );
$g = Nodebook_Gatherings::get( $id );
if ( ! $g ) {
Nodebook_Admin::redirect( Nodebook_Gatherings::url(), 'gfail' );
}
$ids = isset( $_POST['ids'] ) ? array_map( 'absint', (array) wp_unslash( $_POST['ids'] ) ) : array();
$alumni = Nodebook_Gatherings::alumni( $g );
foreach ( $ids as $cid ) {
if ( isset( $alumni[ $cid ] ) ) {
Nodebook_Gatherings::add(
$id,
$cid,
array(
'status' => 'interested',
'source' => __( 'Invited back', 'nodebook' ),
)
);
}
}
Nodebook_Admin::redirect( Nodebook_Gatherings::url( $id ) . '#nb-roster', 'ginvited' );
}
public static function do_deals() {
self::guard( 'nodebook_gathering_deals', 'manage_options' );
update_option( 'nodebook_mod_gatherings_deals', empty( $_POST['on'] ) ? '0' : '1', false );
Nodebook_Admin::redirect( Nodebook_Gatherings::url(), 'saved' );
}
/** The roster as a CSV file (for the venue, the caterer, name badges). */
public static function do_csv() {
$id = self::id_from_post();
self::guard( 'nodebook_gathering_csv_' . $id );
$g = Nodebook_Gatherings::get( $id );
if ( ! $g ) {
wp_die( esc_html__( 'That gathering does not exist.', 'nodebook' ), 404 );
}
$labels = Nodebook_Gatherings::status_labels();
$rows = array( array( __( 'First name', 'nodebook' ), __( 'Last name', 'nodebook' ), __( 'Email', 'nodebook' ), __( 'Phone', 'nodebook' ), __( 'Status', 'nodebook' ), __( 'Seats', 'nodebook' ), __( 'Owes', 'nodebook' ), __( 'Paid', 'nodebook' ), __( 'Came', 'nodebook' ) ) );
foreach ( Nodebook_Gatherings::people( $id ) as $p ) {
if ( ! Nodebook_Team::can_see( (int) $p['contact_id'] ) ) {
continue;
}
$rows[] = array( $p['first_name'], $p['last_name'], $p['email'], $p['phone'], $labels[ $p['status'] ] ?? $p['status'], (int) $p['seats'], $p['amount'], $p['paid'], (int) $p['attended'] ? __( 'Yes', 'nodebook' ) : '' );
}
nocache_headers();
header( 'Content-Type: text/csv; charset=utf-8' );
header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( sanitize_title( $g['title'] ) . '-roster.csv' ) . '"' );
$out = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- streaming the download.
fwrite( $out, "\xEF\xBB\xBF" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- UTF-8 mark so spreadsheets read accents.
foreach ( $rows as $r ) {
fputcsv( $out, array_map( array( 'Nodebook_CSV', 'cell' ), $r ) );
}
fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- streaming the download.
exit;
}
/* ------------------------------------------------------------------ *
* Screens
* ------------------------------------------------------------------ */
public static function page() {
Nodebook_Admin::require_cap();
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- navigation only.
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0;
$edit = ! empty( $_GET['edit'] );
$new = ! empty( $_GET['new'] );
// phpcs:enable
if ( $new || ( $id && $edit ) ) {
self::form( $id ? Nodebook_Gatherings::get( $id ) : null );
return;
}
if ( $id ) {
$g = Nodebook_Gatherings::get( $id );
if ( $g ) {
self::view( $g );
return;
}
}
self::index();
}
private static function index() {
$can = current_user_can( 'manage_options' );
Nodebook_Admin::header( __( 'Gatherings', 'nodebook' ), $can ? array( __( 'New gathering', 'nodebook' ) => Nodebook_Gatherings::url( 0, array( 'new' => 1 ) ) ) : array() );
$upcoming = Nodebook_Gatherings::all( array( 'when' => 'upcoming' ) );
$past = Nodebook_Gatherings::all( array( 'when' => 'past' ) );
if ( ! $upcoming && ! $past ) {
echo '
';
$said = Nodebook_Admin_Node::says( Nodebook_Node::say( __( 'Planning a retreat, a workshop or a trip? Add it here and I’ll keep an eye on spots, deposits and balances for you 🌿', 'nodebook' ), __( 'Add a retreat, workshop or trip to track its spots, deposits and balances.', 'nodebook' ) ), 'hello' );
if ( ! $said ) {
echo '
' . esc_html__( 'Plan your first gathering', 'nodebook' ) . ' ';
}
echo '
';
$cards = array(
array( 'calendar', __( 'Dates, spots and price', 'nodebook' ), __( 'Set how many people fit, the price and the deposit.', 'nodebook' ) ),
array( 'users', __( 'A roster', 'nodebook' ), __( 'Who is interested, who has a spot, who is waiting.', 'nodebook' ) ),
array( 'check', __( 'Deposits and balances', 'nodebook' ), __( 'See who paid what, and who still owes before the balance date.', 'nodebook' ) ),
);
foreach ( $cards as $c ) {
echo '
' . wp_kses( Nodebook_Icons::svg( $c[0], 18 ), Nodebook_Icons::kses() ) . ' ' . esc_html( $c[1] ) . ' ' . esc_html( $c[2] ) . ' ';
}
echo '
';
self::deals_toggle();
Nodebook_Admin::footer();
return;
}
if ( $upcoming ) {
echo '';
} else {
echo '' . esc_html__( 'Nothing coming up. Plan the next one!', 'nodebook' ) . '
';
}
if ( $past ) {
echo '' . esc_html__( 'Past gatherings', 'nodebook' ) . ' ';
foreach ( $past as $g ) {
$s = Nodebook_Gatherings::stats( $g );
echo '' . esc_html( $g['title'] ) . ' ' . esc_html( Nodebook_Gatherings::dates_label( $g ) ) . ' ' . esc_html(
sprintf(
/* translators: 1: guests, 2: came */
__( '%1$d guests · %2$d came', 'nodebook' ),
$s['taken'],
$s['attended']
)
) . ' ';
}
echo ' ';
}
self::deals_toggle();
Nodebook_Admin::footer();
}
/**
* A cover for a gathering card: a simple landscape in one of six palettes, picked from the id so every
* gathering keeps its own look. Decorative only.
*/
private static function cover( $id ) {
$pals = array(
array( '#1F5E4B', '#2E7D63', '#3B9A78', '#F6D27A' ),
array( '#3A3F8F', '#5257B5', '#6B71D1', '#F2E9D8' ),
array( '#0E5A7A', '#1B7FA6', '#4FB3D9', '#FFE8A3' ),
array( '#8A3B2E', '#B5543F', '#E08A5A', '#FFD9A8' ),
array( '#4B2E63', '#6E4594', '#9B6CC4', '#F7D6E8' ),
array( '#6B5B3E', '#8F7A52', '#C2A878', '#FFF3D6' ),
);
$k = absint( $id ) % 6;
$c = $pals[ $k ];
$sun = 0 === $k % 2 ? '320' : '90';
$back = 0 === $k % 3 ? 'M0 110 L60 64 L110 96 L170 40 L230 90 L290 52 L350 86 L400 60 L400 150 L0 150Z' : ( 1 === $k % 3 ? 'M0 112 Q100 82 200 106 T400 94 L400 150 L0 150Z' : 'M0 104 L80 78 L140 98 L210 58 L280 92 L340 70 L400 88 L400 150 L0 150Z' );
$front = 1 === $k % 3 ? 'M0 132 Q120 112 240 128 T400 120 L400 150 L0 150Z' : 'M0 130 L80 100 L150 124 L220 96 L300 122 L400 98 L400 150 L0 150Z';
return ' ';
}
/** One gathering as a card in the list: cover with the date, title, seats ring, money and who to chase. */
private static function gcard( array $g ) {
$s = Nodebook_Gatherings::stats( $g );
$phase = Nodebook_Gatherings::phase( $g );
$start = $g['starts_on'] ? strtotime( $g['starts_on'] . ' 12:00:00' ) : 0;
$cap = (int) $g['capacity'];
$days = $start ? (int) floor( ( $start - strtotime( wp_date( 'Y-m-d' ) . ' 12:00:00' ) ) / DAY_IN_SECONDS ) : null;
if ( 'running' === $phase ) {
$tag = __( 'Happening now', 'nodebook' );
} elseif ( 'closed' === $g['status'] ) {
$tag = __( 'Closed', 'nodebook' );
} elseif ( null !== $days && $days > 0 ) {
/* translators: %s: number of days */
$tag = sprintf( _n( 'In %s day', 'In %s days', $days, 'nodebook' ), number_format_i18n( $days ) );
} else {
$tag = __( 'Open', 'nodebook' );
}
$allowed = array_merge(
Nodebook_Icons::kses(),
array(
'svg' => array(
'class' => true,
'viewbox' => true,
'preserveaspectratio' => true,
'aria-hidden' => true,
'focusable' => true,
'style' => true,
),
'path' => array(
'd' => true,
'fill' => true,
),
'circle' => array(
'cx' => true,
'cy' => true,
'r' => true,
'fill' => true,
),
)
);
echo '' . wp_kses( self::cover( (int) $g['id'] ), $allowed );
echo '' . ( $start ? '' . esc_html( wp_date( 'M', $start ) ) . ' ' . esc_html( wp_date( 'j', $start ) ) . ' ' : '' . esc_html__( 'TBD', 'nodebook' ) . ' ' ) . ' ';
echo '' . esc_html( $tag ) . ' ';
echo '' . esc_html( $g['title'] ) . ' ';
echo '' . esc_html( implode( ' · ', array_filter( array( Nodebook_Gatherings::dates_label( $g ), $g['location'] ) ) ) ) . ' ';
echo '';
if ( $cap ) {
$pct = min( 100, (int) round( 100 * $s['taken'] / $cap ) );
echo '' . esc_html( $s['taken'] . '/' . $cap ) . ' ';
}
echo '';
self::spots( $g, $s );
$pay = $s['expected'] > 0 ? min( 100, (int) round( 100 * $s['paid'] / $s['expected'] ) ) : 0;
echo '' . esc_html(
$s['expected'] > 0
/* translators: 1: paid, 2: expected */
? sprintf( __( '%1$s paid of %2$s', 'nodebook' ), Nodebook_Admin::money( $s['paid'], $g['currency'] ), Nodebook_Admin::money( $s['expected'], $g['currency'] ) )
: ( $s['paid'] > 0 ? sprintf( /* translators: %s: money */ __( '%s paid', 'nodebook' ), Nodebook_Admin::money( $s['paid'], $g['currency'] ) ) : __( 'No payments yet', 'nodebook' ) )
) . ' ';
if ( $s['expected'] > 0 ) {
echo ' ';
}
echo ' ';
echo '';
if ( $s['interested'] ) {
/* translators: %d: people */
echo '' . esc_html( sprintf( _n( '%d interested', '%d interested', $s['interested'], 'nodebook' ), $s['interested'] ) ) . ' ';
}
if ( $s['waitlist'] ) {
/* translators: %d: people */
echo '' . esc_html( sprintf( _n( '%d waiting', '%d waiting', $s['waitlist'], 'nodebook' ), $s['waitlist'] ) ) . ' ';
}
if ( $s['owing'] ) {
/* translators: %d: people */
echo '' . esc_html( sprintf( _n( '%d owes', '%d owe', $s['owing'], 'nodebook' ), $s['owing'] ) ) . ' ';
}
echo ' ';
}
/** Spots bar: "8 of 12 spots taken · 4 left". */
private static function spots( array $g, array $s ) {
$cap = (int) $g['capacity'];
if ( $cap > 0 ) {
$pct = min( 100, (int) round( $s['taken'] / $cap * 100 ) );
echo '' . esc_html(
0 === $s['left']
/* translators: %d: capacity */
? sprintf( __( 'Full · %d spots', 'nodebook' ), $cap )
/* translators: 1: taken, 2: capacity, 3: left */
: sprintf( _n( '%1$d of %2$d taken · %3$d spot left', '%1$d of %2$d taken · %3$d spots left', (int) $s['left'], 'nodebook' ), $s['taken'], $cap, (int) $s['left'] )
) . ' ';
} else {
/* translators: %d: people */
echo '' . esc_html( sprintf( _n( '%d spot taken', '%d spots taken', $s['taken'], 'nodebook' ), $s['taken'] ) ) . ' ';
}
}
private static function deals_toggle() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
echo '';
}
/** One gathering: numbers, roster, invite past guests, details. */
private static function view( array $g ) {
$id = (int) $g['id'];
$people = array_values(
array_filter(
Nodebook_Gatherings::people( $id ),
static function ( $p ) {
return Nodebook_Team::can_see( (int) $p['contact_id'] );
}
)
);
$s = Nodebook_Gatherings::stats( $g, $people );
$phase = Nodebook_Gatherings::phase( $g );
$due = Nodebook_Gatherings::balance_due( $g );
$can = current_user_can( 'manage_options' );
$sub = implode( ' · ', array_filter( array( Nodebook_Gatherings::dates_label( $g ), $g['location'] ) ) );
Nodebook_Admin::header(
$g['title'],
array_filter(
array(
'← ' . __( 'Gatherings', 'nodebook' ) => Nodebook_Gatherings::url(),
__( 'Edit', 'nodebook' ) => $can ? Nodebook_Gatherings::url( $id, array( 'edit' => 1 ) ) : '',
)
),
array( 'sub' => $sub )
);
echo '';
echo '
' . esc_html__( 'Spots', 'nodebook' ) . ' ' . esc_html( (int) $g['capacity'] ? $s['taken'] . ' / ' . (int) $g['capacity'] : (string) $s['taken'] ) . ' ';
self::spots( $g, $s );
echo '
';
$pct = $s['expected'] > 0 ? min( 100, (int) round( $s['paid'] / $s['expected'] * 100 ) ) : 0;
echo '
' . esc_html__( 'Paid', 'nodebook' ) . ' ' . esc_html( Nodebook_Admin::money( $s['paid'], $g['currency'] ) ) . ' ' . esc_html(
$s['expected'] > 0
/* translators: %s: money */
? sprintf( __( 'of %s expected', 'nodebook' ), Nodebook_Admin::money( $s['expected'], $g['currency'] ) )
: __( 'Nothing expected yet', 'nodebook' )
) . '
';
echo '
' . esc_html__( 'Balance due', 'nodebook' ) . ' ' . esc_html( $due ? wp_date( 'j M', strtotime( $due . ' 12:00:00' ) ) : '—' ) . ' ' . esc_html(
$s['owing']
/* translators: 1: people, 2: money */
? sprintf( _n( '%1$d person owes %2$s', '%1$d people owe %2$s', $s['owing'], 'nodebook' ), $s['owing'], Nodebook_Admin::money( $s['owed'], $g['currency'] ) )
: ( $s['taken'] ? __( 'Everyone is paid up', 'nodebook' ) : __( 'No one to collect from yet', 'nodebook' ) )
) . '
';
echo '
' . esc_html__( 'Interested', 'nodebook' ) . ' ' . esc_html( (string) $s['interested'] ) . ' ' . esc_html(
/* translators: %d: people */
sprintf( _n( '%d on the waitlist', '%d on the waitlist', $s['waitlist'], 'nodebook' ), $s['waitlist'] )
) . '
';
echo '
';
echo '';
self::roster( $g, $people, $s, $phase );
if ( 'past' !== $phase ) {
self::invite_card( $g );
}
echo '
';
self::details_card( $g, $due );
echo ' ';
Nodebook_Admin::footer();
}
private static function roster( array $g, array $people, array $s, $phase ) {
$id = (int) $g['id'];
$labels = Nodebook_Gatherings::status_labels();
$came = in_array( $phase, array( 'running', 'past' ), true );
$counts = array_count_values( wp_list_pluck( $people, 'status' ) );
$emails = array();
foreach ( $people as $p ) {
if ( 'cancelled' !== $p['status'] && '' !== (string) $p['email'] ) {
$emails[] = $p['email'];
}
}
echo '' . esc_html__( 'Roster', 'nodebook' ) . ' ' . esc_html( number_format_i18n( $s['people'] ) ) . ' ';
if ( $emails ) {
echo '' . esc_html__( 'Copy emails', 'nodebook' ) . ' ';
}
if ( $people ) {
echo '';
}
echo '
';
// Add someone.
echo '
';
if ( ! $people ) {
echo '
' . esc_html__( 'No one yet. Add people above, or link the gathering’s page or product (Edit) so they arrive on their own.', 'nodebook' ) . '
';
return;
}
// Filters.
$owing = 0;
foreach ( $people as $p ) {
$owing += 'reserved' === $p['status'] && 'deposit' === Nodebook_Gatherings::payment( $p ) || ( 'reserved' === $p['status'] && 'none' === Nodebook_Gatherings::payment( $p ) ) ? 1 : 0;
}
echo '';
echo '' . esc_html__( 'Everyone', 'nodebook' ) . ' ' . esc_html( (string) count( $people ) ) . ' ';
foreach ( $labels as $k => $l ) {
if ( ! empty( $counts[ $k ] ) ) {
echo '' . esc_html( $l ) . ' ' . esc_html( (string) $counts[ $k ] ) . ' ';
}
}
if ( $owing ) {
echo '' . esc_html__( 'Still owes', 'nodebook' ) . ' ' . esc_html( (string) $owing ) . ' ';
}
echo '
';
echo '';
foreach ( $people as $p ) {
$pid = (int) $p['id'];
$pay = Nodebook_Gatherings::payment( $p );
$owe = max( 0, (float) $p['amount'] - (float) $p['paid'] );
$name = Nodebook_Contacts::display_name( $p );
$tags = $p['status'] . ( 'reserved' === $p['status'] && in_array( $pay, array( 'none', 'deposit' ), true ) ? ' owes' : '' );
echo '';
echo Nodebook_Admin::avatar( $name, 'c' . $p['contact_id'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- avatar() escapes.
echo '' . esc_html( $name ) . ' ';
echo '
' . esc_html( implode( ' · ', array_filter( array( $p['email'], (int) $p['seats'] > 1 ? sprintf( /* translators: %d: seats */ __( '%d seats', 'nodebook' ), (int) $p['seats'] ) : '', $p['source'] ) ) ) ) . ' ';
// Status.
echo '';
// Money.
echo '';
if ( (float) $p['amount'] > 0 || (float) $p['paid'] > 0 ) {
$pct = (float) $p['amount'] > 0 ? min( 100, (int) round( (float) $p['paid'] / (float) $p['amount'] * 100 ) ) : 100;
$pill = array(
'paid' => array( 'ok', __( 'Paid', 'nodebook' ) ),
'deposit' => array( 'due', __( 'Deposit', 'nodebook' ) ),
'none' => array( '', __( 'Not paid', 'nodebook' ) ),
'free' => array( '', __( 'Free', 'nodebook' ) ),
)[ $pay ];
echo '' . esc_html( $pill[1] ) . ' ' . esc_html(
/* translators: 1: paid, 2: amount */
sprintf( __( '%1$s of %2$s', 'nodebook' ), Nodebook_Admin::money( (float) $p['paid'], $g['currency'] ), Nodebook_Admin::money( (float) $p['amount'], $g['currency'] ) )
) . ' ';
} else {
echo '' . esc_html__( 'No price', 'nodebook' ) . ' ';
}
echo '
';
// Actions.
echo '';
if ( 'cancelled' !== $p['status'] && ( $owe > 0.009 || (float) $p['amount'] <= 0 ) ) {
$suggest = (float) $p['paid'] <= 0 && (float) $g['deposit'] > 0 ? min( $owe, (float) $g['deposit'] * max( 1, (int) $p['seats'] ) ) : $owe;
echo '';
}
if ( $came && 'reserved' === $p['status'] ) {
echo '';
}
echo '⋯ ' . esc_html__( 'More', 'nodebook' ) . '
';
}
echo ' ';
}
/** Past guests who are not on this gathering yet. */
private static function invite_card( array $g ) {
$alumni = Nodebook_Gatherings::alumni( $g );
if ( ! $alumni ) {
return;
}
$id = (int) $g['id'];
echo '
' . esc_html__( 'Invite past guests', 'nodebook' ) . ' ' . esc_html( number_format_i18n( count( $alumni ) ) ) . ' ';
echo '
' . esc_html__( 'They came to (or booked) an earlier gathering and are not on this one yet. Add them as interested, then send them a note: people who came before are the easiest to welcome back.', 'nodebook' ) . '
';
echo '
';
}
private static function details_card( array $g, $due ) {
$rows = array(
__( 'Dates', 'nodebook' ) => Nodebook_Gatherings::dates_label( $g ),
__( 'Where', 'nodebook' ) => $g['location'],
__( 'Spots', 'nodebook' ) => (int) $g['capacity'] ? number_format_i18n( (int) $g['capacity'] ) : __( 'No limit', 'nodebook' ),
__( 'Price', 'nodebook' ) => (float) $g['price'] > 0 ? Nodebook_Admin::money( (float) $g['price'], $g['currency'] ) . ' ' . __( 'per person', 'nodebook' ) : '',
__( 'Deposit', 'nodebook' ) => (float) $g['deposit'] > 0 ? Nodebook_Admin::money( (float) $g['deposit'], $g['currency'] ) : '',
__( 'Balance due', 'nodebook' ) => $due ? wp_date( 'j M Y', strtotime( $due . ' 12:00:00' ) ) : '',
__( 'Sign-ups', 'nodebook' ) => 'closed' === $g['status'] ? __( 'Closed', 'nodebook' ) : __( 'Open', 'nodebook' ),
);
echo '';
foreach ( $rows as $k => $v ) {
if ( '' !== (string) $v ) {
echo '' . esc_html( $k ) . ' ' . esc_html( (string) $v ) . ' ';
}
}
echo ' ';
$links = array();
if ( (int) $g['page_id'] && get_post( (int) $g['page_id'] ) ) {
$links[] = '
' . esc_html__( 'Forms on', 'nodebook' ) . ' ' . esc_html( get_the_title( (int) $g['page_id'] ) ) . ' ' . esc_html__( 'add people as interested', 'nodebook' ) . ' ';
}
if ( (int) $g['product_id'] && get_post( (int) $g['product_id'] ) ) {
$links[] = '
' . esc_html__( 'Orders of', 'nodebook' ) . ' ' . esc_html( get_the_title( (int) $g['product_id'] ) ) . ' ' . esc_html__( 'reserve a spot and record the payment', 'nodebook' ) . ' ';
}
if ( '' !== (string) $g['match_text'] ) {
/* translators: %s: booking name */
$links[] = '
' . esc_html( sprintf( __( 'Bookings named “%s” reserve a spot', 'nodebook' ), $g['match_text'] ) ) . ' ';
}
if ( $links ) {
echo '
' . esc_html__( 'People arrive from', 'nodebook' ) . ' ' . wp_kses_post( implode( '', $links ) ) . ' ';
} elseif ( current_user_can( 'manage_options' ) ) {
echo '
' . esc_html__( 'Tip: link the gathering’s page, product or booking (Edit) and people are added to the roster on their own.', 'nodebook' ) . '
';
}
if ( '' !== (string) $g['notes'] ) {
echo '
' . esc_html__( 'Notes', 'nodebook' ) . ' ' . nl2br( esc_html( $g['notes'] ) ) . '
';
}
echo '
';
}
/** Plan or edit a gathering. */
private static function form( $g ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'nodebook' ), 403 );
}
$id = $g ? (int) $g['id'] : 0;
$g = $g ? $g : array(
'title' => '',
'starts_on' => '',
'ends_on' => '',
'location' => '',
'capacity' => '',
'price' => '',
'deposit' => '',
'currency' => Nodebook_Conversions::currency(),
'balance_days' => 30,
'page_id' => 0,
'product_id' => 0,
'match_text' => '',
'status' => 'open',
'notes' => '',
);
$num = static function ( $v ) {
return (float) $v > 0 ? (string) ( 0.0 === fmod( (float) $v, 1.0 ) ? (int) $v : (float) $v ) : '';
};
Nodebook_Admin::header( $id ? __( 'Edit gathering', 'nodebook' ) : __( 'New gathering', 'nodebook' ), array( '← ' . ( $id ? $g['title'] : __( 'Gatherings', 'nodebook' ) ) => Nodebook_Gatherings::url( $id ) ) );
echo '';
if ( $id ) {
echo ' ';
wp_nonce_field( 'nodebook_gathering_delete_' . $id );
echo '' . esc_html__( 'Delete this gathering and its roster', 'nodebook' ) . ' ';
}
Nodebook_Admin::footer();
}
/* ------------------------------------------------------------------ *
* Contact page
* ------------------------------------------------------------------ */
/** "Gatherings" on a contact: where they are on, and add them to an upcoming one. */
public static function contact_card( $c ) {
$mine = Nodebook_Gatherings::for_contact( (int) $c['id'] );
$upcoming = Nodebook_Gatherings::all( array( 'when' => 'upcoming' ) );
$on = array_map( 'intval', wp_list_pluck( $mine, 'event_id' ) );
$open = array_filter(
$upcoming,
static function ( $g ) use ( $on ) {
return ! in_array( (int) $g['id'], $on, true );
}
);
if ( ! $mine && ! $open ) {
return;
}
$labels = Nodebook_Gatherings::status_labels();
echo '
' . esc_html__( 'Gatherings', 'nodebook' ) . ' ';
if ( $mine ) {
echo '
';
foreach ( $mine as $p ) {
$pay = Nodebook_Gatherings::payment( $p );
echo '' . esc_html( $p['title'] ) . ' ' . esc_html( Nodebook_Gatherings::dates_label( $p ) ) . ' ' . esc_html( $labels[ $p['status'] ] ?? $p['status'] ) . ' ';
if ( (float) $p['amount'] > 0 && 'cancelled' !== $p['status'] ) {
echo ' ' . esc_html(
'paid' === $pay
? __( 'paid in full', 'nodebook' )
/* translators: 1: paid, 2: amount */
: sprintf( __( '%1$s of %2$s paid', 'nodebook' ), Nodebook_Admin::money( (float) $p['paid'], $p['currency'] ), Nodebook_Admin::money( (float) $p['amount'], $p['currency'] ) )
) . ' ';
}
if ( (int) $p['attended'] ) {
echo ' ' . esc_html__( 'Came', 'nodebook' ) . ' ';
}
echo ' ';
}
echo ' ';
}
if ( $open ) {
$first = reset( $open );
$back = isset( $_SERVER['REQUEST_URI'] ) ? remove_query_arg( 'nb_notice', sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) : '';
echo '
';
// The nonce is per gathering: JS swaps it when another one is picked; without JS the first one is used.
echo '' . esc_html__( 'Gathering', 'nodebook' ) . ' ';
foreach ( $open as $g ) {
echo '' . esc_html( $g['title'] . ( $g['starts_on'] ? ' · ' . Nodebook_Gatherings::dates_label( $g ) : '' ) ) . ' ';
}
echo ' ';
echo '' . esc_html( $labels['interested'] ) . ' ' . esc_html( $labels['reserved'] ) . ' ' . esc_html__( 'Add', 'nodebook' ) . ' ';
}
echo '
';
}
}