Files
eventLabs/includes/class-admin.php
josh 6df287d530
All checks were successful
WordPress release / release (push) Successful in 5s
Hide event sub-club field until burnWorks syncs
2026-11-12 15:17:32 -05:00

532 lines
30 KiB
PHP

<?php
namespace BurnEvents;
use DateTimeImmutable;
use DateTimeZone;
use WP_Post;
use WP_Term;
defined( 'ABSPATH' ) || exit;
final class Admin {
private const OPTION = 'burn_events_settings';
public static function register(): void {
add_action( 'admin_menu', [ self::class, 'menu' ] );
add_action( 'admin_init', [ self::class, 'settings' ] );
add_action( 'add_meta_boxes', [ self::class, 'boxes' ] );
add_action( 'save_post_' . Events::EVENT, [ self::class, 'save_event' ] );
add_action( 'save_post_' . Events::VENUE, [ self::class, 'save_location' ] );
add_action( 'save_post_' . Events::ORGANIZER, [ self::class, 'save_location' ] );
add_action( 'admin_notices', [ self::class, 'errors' ] );
add_action( 'admin_enqueue_scripts', [ self::class, 'assets' ] );
add_action( Events::CATEGORY . '_edit_form_fields', [ self::class, 'category_color_field' ] );
add_action( 'edited_' . Events::CATEGORY, [ self::class, 'save_category_color' ] );
add_filter( 'manage_burn_event_posts_columns', [ self::class, 'columns' ] );
add_action( 'manage_burn_event_posts_custom_column', [ self::class, 'column' ], 10, 2 );
}
public static function assets(): void {
$screen = get_current_screen();
if ( $screen && 'post' === $screen->base && Events::EVENT === $screen->post_type ) {
wp_enqueue_style( 'burn-events-editor', plugins_url( 'assets/event-editor.css', BURN_EVENTS_FILE ), [], BURN_EVENTS_VERSION );
wp_enqueue_script( 'burn-events-editor', plugins_url( 'assets/event-editor.js', BURN_EVENTS_FILE ), [], BURN_EVENTS_VERSION, true );
}
if ( ! $screen || Events::CATEGORY !== $screen->taxonomy || 'term' !== $screen->base ) {
return;
}
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker' );
wp_add_inline_script( 'wp-color-picker', 'jQuery(function ($) { $(".be-category-color").wpColorPicker(); });' );
}
public static function category_color_field( WP_Term $term ): void {
$color = sanitize_hex_color( (string) get_term_meta( $term->term_id, Calendar_Grid::COLOR_META, true ) );
?>
<tr class="form-field">
<th scope="row"><label for="be-category-color"><?php esc_html_e( 'Calendar color', 'burn-events' ); ?></label></th>
<td>
<?php wp_nonce_field( 'burn_events_category_color', 'burn_events_category_color_nonce', false ); ?>
<input type="text" id="be-category-color" class="be-category-color" name="burn_events_category_color" value="<?php echo esc_attr( $color ?? '' ); ?>" aria-describedby="be-category-color-description">
<p class="description" id="be-category-color-description"><?php esc_html_e( 'Leave blank to use the automatic color for this category.', 'burn-events' ); ?></p>
</td>
</tr>
<?php
}
public static function save_category_color( int $term_id ): void {
$nonce = $_POST['burn_events_category_color_nonce'] ?? null;
$input = $_POST['burn_events_category_color'] ?? null;
if ( ! is_string( $nonce ) || ! is_string( $input ) || ! current_user_can( 'edit_term', $term_id )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $nonce ) ), 'burn_events_category_color' )
) {
return;
}
$input = trim( wp_unslash( $input ) );
if ( '' === $input ) {
delete_term_meta( $term_id, Calendar_Grid::COLOR_META );
} elseif ( $color = sanitize_hex_color( $input ) ) {
update_term_meta( $term_id, Calendar_Grid::COLOR_META, $color );
}
}
public static function options(): array {
return (array) get_option( self::OPTION, [] ) + [
'view' => 'list', 'per_page' => 12, 'timezone' => wp_timezone_string(),
'venue' => 0, 'organizer' => 0, 'anonymous' => false, 'community_edit' => true,
'maps' => false, 'fields' => [], 'definitions' => '',
];
}
public static function menu(): void {
add_submenu_page( 'edit.php?post_type=' . Events::EVENT, __( 'eventLabs Settings', 'burn-events' ), __( 'Settings', 'burn-events' ), 'manage_options', 'burn-events-settings', [ self::class, 'settings_page' ] );
add_submenu_page( 'edit.php?post_type=' . Events::EVENT, __( 'Import and Export Events', 'burn-events' ), __( 'Import and Export', 'burn-events' ), 'manage_options', 'burn-events-transfer', [ Transfer::class, 'admin_page' ] );
}
public static function settings(): void {
register_setting( 'burn_events', self::OPTION, [ 'sanitize_callback' => [ self::class, 'sanitize_options' ] ] );
Maps::register_setting();
}
public static function sanitize_options( $input ): array {
$input = (array) $input;
$view = sanitize_key( $input['view'] ?? 'list' );
$zone = sanitize_text_field( $input['timezone'] ?? '' );
try {
$timezone = ( new DateTimeZone( $zone ) )->getName();
} catch ( \Exception $error ) {
$timezone = wp_timezone_string();
add_settings_error( self::OPTION, 'timezone', __( 'The time zone was reset to the WordPress site time zone.', 'burn-events' ) );
}
$fields = [];
$definitions = sanitize_textarea_field( $input['definitions'] ?? '' );
foreach ( preg_split( '/\R/', $definitions, -1, PREG_SPLIT_NO_EMPTY ) as $line ) {
[ $key, $label, $type, $choices ] = array_pad( explode( '|', $line, 4 ), 4, '' );
$key = sanitize_key( trim( $key ) );
$label = sanitize_text_field( trim( $label ) );
$type = sanitize_key( trim( $type ) );
if ( $key && $label && in_array( $type, [ 'text', 'url', 'checkbox', 'select' ], true ) ) {
$fields[ $key ] = [ 'label' => $label, 'type' => $type, 'choices' => array_values( array_filter( array_map( 'sanitize_text_field', explode( ',', $choices ) ) ) ) ];
}
}
return [
'view' => in_array( $view, Calendar::VIEWS, true ) ? $view : 'list',
'per_page' => max( 1, min( 50, absint( $input['per_page'] ?? 12 ) ) ),
'timezone' => $timezone,
'venue' => absint( $input['venue'] ?? 0 ),
'organizer' => absint( $input['organizer'] ?? 0 ),
'anonymous' => ! empty( $input['anonymous'] ),
'community_edit' => ! empty( $input['community_edit'] ),
'maps' => ! empty( $input['maps'] ),
'definitions' => $definitions,
'fields' => $fields,
];
}
public static function settings_page(): void {
$options = self::options();
?>
<div class="wrap">
<h1><?php esc_html_e( 'eventLabs Settings', 'burn-events' ); ?></h1>
<p><a href="<?php echo esc_url( Calendar::archive_url() ); ?>"><?php esc_html_e( 'View calendar', 'burn-events' ); ?></a> · <a href="<?php echo esc_url( Community::url( 'submit' ) ); ?>"><?php esc_html_e( 'View submission form', 'burn-events' ); ?></a></p>
<?php settings_errors(); ?>
<form action="options.php" method="post">
<?php settings_fields( 'burn_events' ); ?>
<table class="form-table" role="presentation">
<tr><th scope="row"><label for="be-view"><?php esc_html_e( 'Default calendar view', 'burn-events' ); ?></label></th><td><select id="be-view" name="burn_events_settings[view]"><?php foreach ( Calendar::view_labels() as $id => $label ) : ?><option value="<?php echo esc_attr( $id ); ?>" <?php selected( $options['view'], $id ); ?>><?php echo esc_html( $label ); ?></option><?php endforeach; ?></select></td></tr>
<tr><th scope="row"><label for="be-per-page"><?php esc_html_e( 'Events per page', 'burn-events' ); ?></label></th><td><input id="be-per-page" name="burn_events_settings[per_page]" type="number" min="1" max="50" value="<?php echo esc_attr( (string) $options['per_page'] ); ?>"></td></tr>
<tr><th scope="row"><label for="be-timezone"><?php esc_html_e( 'Default time zone', 'burn-events' ); ?></label></th><td><input id="be-timezone" name="burn_events_settings[timezone]" class="regular-text" value="<?php echo esc_attr( $options['timezone'] ); ?>"><p class="description"><?php esc_html_e( 'Use a time zone such as America/New_York.', 'burn-events' ); ?></p></td></tr>
<?php foreach ( [ 'venue' => Events::VENUE, 'organizer' => Events::ORGANIZER ] as $name => $type ) : ?>
<tr><th scope="row"><label for="be-default-<?php echo esc_attr( $name ); ?>"><?php echo esc_html( 'venue' === $name ? __( 'Default venue', 'burn-events' ) : __( 'Default organizer', 'burn-events' ) ); ?></label></th><td><?php self::relation_select( "burn_events_settings[$name]", $type, [ $options[ $name ] ], false, 'be-default-' . $name ); ?></td></tr>
<?php endforeach; ?>
<tr><th scope="row"><?php esc_html_e( 'Community submissions', 'burn-events' ); ?></th><td>
<label><input name="burn_events_settings[anonymous]" type="checkbox" value="1" <?php checked( $options['anonymous'] ); ?>> <?php esc_html_e( 'Allow submissions from guests', 'burn-events' ); ?></label><br>
<label><input name="burn_events_settings[community_edit]" type="checkbox" value="1" <?php checked( $options['community_edit'] ); ?>> <?php esc_html_e( 'Let contributors edit their own submissions', 'burn-events' ); ?></label>
<p class="description"><?php esc_html_e( 'New submissions and edits go to Pending Review.', 'burn-events' ); ?></p>
</td></tr>
<tr>
<th scope="row"><label for="be-maps-key"><?php esc_html_e( 'Maps API key', 'burn-events' ); ?></label></th>
<td>
<input id="be-maps-key" name="<?php echo esc_attr( Maps::KEY_OPTION ); ?>" class="regular-text" type="password" autocomplete="new-password" placeholder="<?php echo Maps::has_key() ? esc_attr__( 'Key saved', 'burn-events' ) : ''; ?>">
<p class="description"><?php esc_html_e( 'Use a Geoapify key. When you save a venue or organizer, its street address is sent to Geoapify to look up coordinates.', 'burn-events' ); ?></p>
<?php if ( Maps::has_key() ) : ?><label><input name="burn_events_remove_maps_key" type="checkbox" value="1"> <?php esc_html_e( 'Remove saved key', 'burn-events' ); ?></label><br><?php endif; ?>
<label><input name="burn_events_settings[maps]" type="checkbox" value="1" <?php checked( $options['maps'] ); ?>> <?php esc_html_e( 'Show OpenStreetMap maps for venues with coordinates', 'burn-events' ); ?></label>
</td>
</tr>
<tr><th scope="row"><label for="be-fields"><?php esc_html_e( 'Custom event fields', 'burn-events' ); ?></label></th><td><textarea id="be-fields" class="large-text code" name="burn_events_settings[definitions]" rows="5"><?php echo esc_textarea( $options['definitions'] ); ?></textarea><p class="description"><?php esc_html_e( 'One field per line. Use key|Label|type. Types are text, url, checkbox, and select. Add |First,Second for select choices.', 'burn-events' ); ?></p></td></tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public static function boxes(): void {
add_meta_box( 'burn-event-details', __( 'Event Details', 'burn-events' ), [ self::class, 'event_box' ], Events::EVENT, 'normal', 'high' );
add_meta_box( 'burn-location-details', __( 'Contact and Location', 'burn-events' ), [ self::class, 'location_box' ], [ Events::VENUE, Events::ORGANIZER ], 'normal', 'high' );
}
public static function event_box( WP_Post $post ): void {
wp_nonce_field( 'burn_event_details', 'burn_event_nonce' );
$data = Events::event_data( $post->ID );
$data['imported_schedule'] = Events::uses_imported_dates( $post->ID );
if ( ! $data['schedule'] ) {
$options = self::options();
$data['venue'] = array_filter( [ $options['venue'] ] );
$data['organizer'] = array_filter( [ $options['organizer'] ] );
}
self::event_fields( $data );
$options = Clubs::editor_options();
if ( null === $options ) {
return;
}
$scope = (string) get_post_meta( $post->ID, Clubs::EVENT_META, true );
?>
<p><label for="burn-event-sub-club"><strong><?php esc_html_e( 'Sub-club', 'burn-events' ); ?></strong></label><br>
<select id="burn-event-sub-club" name="be_sub_club" aria-describedby="burn-event-sub-club-help" <?php disabled( (bool) BurnWorks::snapshot( $post->ID ) ); ?>>
<option value=""><?php esc_html_e( 'No sub-club (organization calendar)', 'burn-events' ); ?></option>
<?php if ( $scope && ! isset( $options[ $scope ] ) ) : ?>
<option value="<?php echo esc_attr( $scope ); ?>" selected><?php esc_html_e( 'Current assignment (unavailable)', 'burn-events' ); ?></option>
<?php endif; ?>
<?php foreach ( $options as $value => $label ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $scope, $value ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select></p>
<p class="description" id="burn-event-sub-club-help"><?php
if ( BurnWorks::snapshot( $post->ID ) ) {
esc_html_e( 'This assignment is managed in burnWorks.', 'burn-events' );
} elseif ( ! $options ) {
esc_html_e( 'No active sub-clubs have synced from burnWorks.', 'burn-events' );
} else {
esc_html_e( 'This determines which CRM sub-club receives the event.', 'burn-events' );
}
?></p>
<?php
}
public static function event_fields( array $data, bool $public = false ): void {
$options = self::options();
$start = new DateTimeImmutable( 'tomorrow', new DateTimeZone( $options['timezone'] ) );
$rule = ( $data['schedule'] ?? [] ) + [
'start_date' => $start->format( 'Y-m-d' ), 'end_date' => $start->format( 'Y-m-d' ), 'start_time' => '10:00', 'end_time' => '11:00',
'timezone' => $options['timezone'], 'all_day' => false, 'repeat' => 'none', 'interval' => 1,
'weekdays' => [], 'monthly' => 'day', 'until' => '', 'count' => 0, 'include' => [], 'exclude' => [],
];
if ( ! $public ) {
self::editor_fields( $data, $rule, $options['fields'] );
return;
}
?>
<div class="be-fields">
<?php self::schedule_fields( $rule ); ?>
<label><input type="checkbox" name="be[all_day]" value="1" <?php checked( $rule['all_day'] ); ?>> <?php esc_html_e( 'All day', 'burn-events' ); ?></label>
</div>
<?php self::recurrence_fields( $rule ); ?>
<div class="be-fields">
<?php
self::input_field( 'location', __( 'Location name or address', 'burn-events' ), $data['location'] ?? '' );
self::input_field( 'cost', __( 'Cost', 'burn-events' ), $data['cost'] ?? '' );
self::input_field( 'url', __( 'Event website', 'burn-events' ), $data['url'] ?? '', 'url' );
foreach ( [ 'venue' => Events::VENUE, 'organizer' => Events::ORGANIZER ] as $name => $type ) :
?>
<label><?php echo esc_html( get_post_type_object( $type )->labels->name ); ?> <?php self::relation_select( "be[$name]", $type, $data[ $name ] ?? [], true ); ?></label>
<?php endforeach; ?>
<?php self::custom_field_controls( $options['fields'], $data['fields'] ?? [] ); ?>
</div>
<?php
}
private static function editor_fields( array $data, array $rule, array $custom_fields ): void {
?>
<div class="be-event-editor" data-be-event-editor>
<section class="be-editor-section be-editor-schedule">
<header class="be-editor-heading">
<h3><?php esc_html_e( 'Schedule', 'burn-events' ); ?></h3>
<label class="be-editor-toggle"><input type="checkbox" name="be[all_day]" value="1" <?php checked( $rule['all_day'] ); ?>> <?php esc_html_e( 'All day', 'burn-events' ); ?></label>
</header>
<div class="be-editor-fields be-editor-dates"><?php self::schedule_fields( $rule ); ?></div>
<?php if ( ! empty( $data['imported_schedule'] ) ) : ?>
<p class="description"><?php esc_html_e( 'This event uses saved dates from the migration. Changing its schedule replaces those dates.', 'burn-events' ); ?></p>
<?php endif; ?>
</section>
<?php self::recurrence_fields( $rule, true ); ?>
<section class="be-editor-section">
<header class="be-editor-heading"><h3><?php esc_html_e( 'Location and people', 'burn-events' ); ?></h3></header>
<div class="be-editor-fields">
<?php foreach ( Events::RELATIONS as $name => $type ) { self::relation_picker( "be[$name]", $type, $data[ $name ] ?? [] ); } ?>
</div>
<div class="be-editor-location">
<?php self::input_field( 'location', __( 'Location name or address', 'burn-events' ), $data['location'] ?? '' ); ?>
</div>
</section>
<section class="be-editor-section">
<header class="be-editor-heading"><h3><?php esc_html_e( 'Event settings', 'burn-events' ); ?></h3></header>
<div class="be-editor-fields">
<?php self::input_field( 'cost', __( 'Cost', 'burn-events' ), $data['cost'] ?? '' ); ?>
<?php self::input_field( 'url', __( 'Event website', 'burn-events' ), $data['url'] ?? '', 'url' ); ?>
<label><?php esc_html_e( 'Event status', 'burn-events' ); ?>
<select name="be[status]">
<?php foreach ( Events::statuses() as $key => $label ) : ?>
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $data['status'] ?? 'scheduled', $key ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
</label>
</div>
<div class="be-editor-visibility">
<label class="be-editor-toggle"><input type="checkbox" name="be[featured]" value="1" <?php checked( $data['featured'] ?? false ); ?>> <?php esc_html_e( 'Featured event', 'burn-events' ); ?></label>
<label class="be-editor-toggle"><input type="checkbox" name="be[hidden]" value="1" <?php checked( $data['hidden'] ?? false ); ?>> <?php esc_html_e( 'Hide from calendar listings', 'burn-events' ); ?></label>
</div>
</section>
<?php if ( $custom_fields ) : ?>
<section class="be-editor-section">
<header class="be-editor-heading"><h3><?php esc_html_e( 'Custom fields', 'burn-events' ); ?></h3></header>
<div class="be-editor-fields"><?php self::custom_field_controls( $custom_fields, $data['fields'] ?? [] ); ?></div>
</section>
<?php endif; ?>
</div>
<?php
}
private static function input_field( string $key, string $label, string $value, string $type = 'text' ): void {
?>
<label<?php if ( 'time' === $type ) : ?> data-be-time<?php elseif ( 'timezone' === $key ) : ?> class="be-editor-zone"<?php endif; ?>>
<?php echo esc_html( $label ); ?>
<input type="<?php echo esc_attr( $type ); ?>" <?php if ( 'time' === $type ) { echo 'step="1"'; } ?> name="be[<?php echo esc_attr( $key ); ?>]" value="<?php echo esc_attr( $value ); ?>">
</label>
<?php
}
private static function schedule_fields( array $rule ): void {
self::input_field( 'start_date', __( 'Start date', 'burn-events' ), $rule['start_date'], 'date' );
self::input_field( 'start_time', __( 'Start time', 'burn-events' ), $rule['start_time'], 'time' );
self::input_field( 'end_date', __( 'End date', 'burn-events' ), $rule['end_date'], 'date' );
self::input_field( 'end_time', __( 'End time', 'burn-events' ), $rule['end_time'], 'time' );
self::input_field( 'timezone', __( 'Time zone', 'burn-events' ), $rule['timezone'] );
}
private static function recurrence_fields( array $rule, bool $editor = false ): void {
global $wp_locale;
$labels = [
'none' => __( 'Does not repeat', 'burn-events' ), 'daily' => __( 'Daily', 'burn-events' ),
'weekly' => __( 'Weekly', 'burn-events' ), 'monthly' => __( 'Monthly', 'burn-events' ), 'yearly' => __( 'Yearly', 'burn-events' ),
];
$open = 'none' !== $rule['repeat'] || ( $editor && ( $rule['include'] || $rule['exclude'] ) );
?>
<details class="be-recurrence<?php echo $editor ? ' be-editor-section be-editor-recurrence' : ''; ?>" <?php if ( $open ) { echo 'open'; } ?>>
<summary>
<span><?php esc_html_e( 'Recurrence', 'burn-events' ); ?></span>
<?php if ( $editor ) : ?><span class="be-editor-frequency" data-be-frequency><?php echo esc_html( $labels[ $rule['repeat'] ] ?? $labels['none'] ); ?></span><?php endif; ?>
</summary>
<div class="be-editor-recurrence-body">
<div class="be-fields be-editor-fields be-editor-repeat-fields">
<label><?php esc_html_e( 'Repeat', 'burn-events' ); ?>
<select name="be[repeat]">
<?php foreach ( Schedule::REPEATS as $type ) : ?>
<option value="<?php echo esc_attr( $type ); ?>" <?php selected( $type, $rule['repeat'] ); ?>><?php echo esc_html( $labels[ $type ] ?? ucfirst( $type ) ); ?></option>
<?php endforeach; ?>
</select>
</label>
<label data-be-repeat="enabled"><?php esc_html_e( 'Repeat every', 'burn-events' ); ?><input type="number" min="1" max="365" name="be[interval]" value="<?php echo esc_attr( (string) $rule['interval'] ); ?>"></label>
<label data-be-repeat="enabled"><?php esc_html_e( 'Repeat through', 'burn-events' ); ?><input type="date" name="be[until]" value="<?php echo esc_attr( $rule['until'] ); ?>"></label>
<label data-be-repeat="enabled"><?php esc_html_e( 'Occurrence limit', 'burn-events' ); ?><input type="number" min="0" max="10000" name="be[count]" value="<?php echo esc_attr( (string) $rule['count'] ); ?>"></label>
<label data-be-repeat="monthly"><?php esc_html_e( 'Monthly pattern', 'burn-events' ); ?>
<select name="be[monthly]">
<option value="day" <?php selected( $rule['monthly'], 'day' ); ?>><?php esc_html_e( 'Same day of month', 'burn-events' ); ?></option>
<option value="weekday" <?php selected( $rule['monthly'], 'weekday' ); ?>><?php esc_html_e( 'Same weekday of month', 'burn-events' ); ?></option>
</select>
</label>
</div>
<fieldset class="be-editor-weekdays" data-be-repeat="weekly">
<legend><?php esc_html_e( 'Days for weekly recurrence', 'burn-events' ); ?></legend>
<div>
<?php foreach ( array_keys( Schedule::WEEKDAYS ) as $day ) : ?>
<?php $name = $wp_locale->get_weekday( $day % 7 ); ?>
<label>
<input type="checkbox" name="be[weekdays][]" value="<?php echo esc_attr( (string) $day ); ?>" <?php checked( in_array( $day, $rule['weekdays'], true ) ); ?>>
<?php if ( $editor ) : ?>
<span aria-hidden="true"><?php echo esc_html( $wp_locale->get_weekday_abbrev( $name ) ); ?></span><span class="screen-reader-text"><?php echo esc_html( $name ); ?></span>
<?php else : ?>
<?php echo esc_html( $name ); ?>
<?php endif; ?>
</label>
<?php endforeach; ?>
</div>
</fieldset>
<div class="be-fields be-editor-fields be-editor-exceptions">
<?php foreach ( [ 'include' => __( 'Additional dates', 'burn-events' ), 'exclude' => __( 'Excluded dates', 'burn-events' ) ] as $key => $label ) : ?>
<label><?php echo esc_html( $label ); ?>
<input name="be[<?php echo esc_attr( $key ); ?>]" value="<?php echo esc_attr( implode( ', ', $rule[ $key ] ) ); ?>" placeholder="YYYY-MM-DD, YYYY-MM-DD">
</label>
<?php endforeach; ?>
</div>
<p class="description" data-be-repeat="enabled"><?php esc_html_e( 'Leave Repeat through blank and the occurrence limit at zero for no end. Weekly recurrence uses the start weekday when no days are selected.', 'burn-events' ); ?></p>
</div>
</details>
<?php
}
private static function custom_field_controls( array $fields, array $values ): void {
foreach ( $fields as $key => $field ) :
$value = $values[ $key ] ?? '';
?>
<label<?php if ( 'checkbox' === $field['type'] ) : ?> class="be-editor-toggle"<?php endif; ?>>
<?php if ( 'select' === $field['type'] ) : ?>
<?php echo esc_html( $field['label'] ); ?>
<select name="be[fields][<?php echo esc_attr( $key ); ?>]">
<option value=""><?php esc_html_e( 'Select', 'burn-events' ); ?></option>
<?php foreach ( $field['choices'] as $choice ) : ?><option <?php selected( $value, $choice ); ?>><?php echo esc_html( $choice ); ?></option><?php endforeach; ?>
</select>
<?php elseif ( 'checkbox' === $field['type'] ) : ?>
<input type="checkbox" name="be[fields][<?php echo esc_attr( $key ); ?>]" value="1" <?php checked( $value, '1' ); ?>> <?php echo esc_html( $field['label'] ); ?>
<?php else : ?>
<?php echo esc_html( $field['label'] ); ?>
<input type="<?php echo esc_attr( $field['type'] ); ?>" name="be[fields][<?php echo esc_attr( $key ); ?>]" value="<?php echo esc_attr( $value ); ?>">
<?php endif; ?>
</label>
<?php
endforeach;
}
private static function relation_posts( string $type ): array {
return get_posts( [ 'post_type' => $type, 'post_status' => 'publish', 'has_password' => false, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC' ] );
}
private static function relation_picker( string $name, string $type, array $selected ): void {
$posts = self::relation_posts( $type );
$selected = array_map( 'intval', $selected );
$choices = array_map( static fn( WP_Post $post ): string => get_the_title( $post ), $posts );
$chosen = [];
foreach ( $posts as $index => $post ) {
if ( in_array( $post->ID, $selected, true ) ) {
$chosen[] = $choices[ $index ];
}
}
$placeholder = __( 'None selected', 'burn-events' );
?>
<fieldset class="be-editor-picker" data-be-picker>
<legend><?php echo esc_html( get_post_type_object( $type )->labels->name ); ?></legend>
<?php if ( $posts ) : ?>
<details>
<summary><span data-be-picker-selection data-be-empty="<?php echo esc_attr( $placeholder ); ?>"><?php echo esc_html( $chosen ? implode( ', ', $chosen ) : $placeholder ); ?></span></summary>
<div class="be-editor-picker-body">
<?php if ( count( $posts ) > 8 ) : ?>
<label><span class="screen-reader-text"><?php esc_html_e( 'Filter options', 'burn-events' ); ?></span><input type="search" data-be-picker-search placeholder="<?php esc_attr_e( 'Filter options', 'burn-events' ); ?>"></label>
<?php endif; ?>
<div class="be-editor-picker-options">
<?php foreach ( $posts as $index => $post ) : ?>
<label data-be-picker-option>
<input type="checkbox" name="<?php echo esc_attr( $name . '[]' ); ?>" value="<?php echo esc_attr( (string) $post->ID ); ?>" <?php checked( in_array( $post->ID, $selected, true ) ); ?>>
<span><?php echo esc_html( $choices[ $index ] ); ?></span>
</label>
<?php endforeach; ?>
<p class="be-editor-no-matches" data-be-no-options hidden><?php esc_html_e( 'No matching options.', 'burn-events' ); ?></p>
</div>
</div>
</details>
<?php else : ?>
<div class="be-editor-picker-empty"><?php esc_html_e( 'None available', 'burn-events' ); ?></div>
<?php endif; ?>
</fieldset>
<?php
}
private static function relation_select( string $name, string $type, array $selected, bool $multiple, string $id = '' ): void {
$posts = self::relation_posts( $type );
?>
<select <?php if ( $id ) { echo 'id="' . esc_attr( $id ) . '"'; } ?> name="<?php echo esc_attr( $name . ( $multiple ? '[]' : '' ) ); ?>" <?php if ( $multiple ) { echo 'multiple size="3"'; } ?>>
<?php if ( ! $multiple ) : ?><option value="0"><?php esc_html_e( 'None', 'burn-events' ); ?></option><?php endif; ?>
<?php foreach ( $posts as $post ) : ?><option value="<?php echo esc_attr( (string) $post->ID ); ?>" <?php selected( in_array( $post->ID, $selected, true ) ); ?>><?php echo esc_html( get_the_title( $post ) ); ?></option><?php endforeach; ?>
</select>
<?php
}
public static function save_event( int $id ): void {
if ( ! self::valid_save( $id, 'burn_event_nonce', 'burn_event_details' ) || ! is_array( $_POST['be'] ?? null ) ) {
return;
}
try {
$current = (string) get_post_meta( $id, Clubs::EVENT_META, true );
$options = ! BurnWorks::snapshot( $id ) && is_string( $_POST['be_sub_club'] ?? null ) ? Clubs::editor_options() : null;
$scope = null !== $options ? wp_unslash( $_POST['be_sub_club'] ) : $current;
if ( $scope !== $current && '' !== $scope && ! isset( $options[ $scope ] ) ) {
throw new \InvalidArgumentException( __( 'Select an available sub-club.', 'burn-events' ) );
}
Events::save( $id, Events::validate_event( wp_unslash( $_POST['be'] ) ) );
if ( $scope !== $current ) {
Clubs::assign( $id, $scope );
}
} catch ( \Throwable $error ) {
set_transient( 'burn_events_error_' . get_current_user_id(), $error->getMessage(), MINUTE_IN_SECONDS );
}
}
public static function location_box( WP_Post $post ): void {
wp_nonce_field( 'burn_location_details', 'burn_location_nonce' );
$data = Events::location_data( $post->ID );
$labels = [
'address' => __( 'Street address', 'burn-events' ), 'city' => __( 'City', 'burn-events' ),
'region' => __( 'State or region', 'burn-events' ), 'postal' => __( 'Postal code', 'burn-events' ),
'country' => __( 'Country', 'burn-events' ), 'website' => __( 'Website', 'burn-events' ),
'email' => __( 'Email', 'burn-events' ), 'phone' => __( 'Phone', 'burn-events' ),
];
foreach ( $labels as $key => $label ) {
echo '<p><label>' . esc_html( $label ) . ' <input class="regular-text" name="be_location[' . esc_attr( $key ) . ']" value="' . esc_attr( $data[ $key ] ) . '"></label></p>';
}
echo '<p><strong>' . esc_html__( 'Address lookup', 'burn-events' ) . '</strong> ' . esc_html( Maps::lookup_message( $data ) ) . '</p>';
if ( '' !== $data['latitude'] && '' !== $data['longitude'] ) {
echo '<p>' . esc_html( $data['latitude'] . ', ' . $data['longitude'] ) . '</p>';
Maps::attribution( $data );
}
if ( Maps::has_key() && Maps::address( $data ) ) {
echo '<p><label><input type="checkbox" name="be_lookup_address" value="1"> ' . esc_html__( 'Look up this address again on save', 'burn-events' ) . '</label></p>';
}
}
public static function save_location( int $id ): void {
if ( ! self::valid_save( $id, 'burn_location_nonce', 'burn_location_details' ) || ! is_array( $_POST['be_location'] ?? null ) ) {
return;
}
try {
Events::save_location( $id, wp_unslash( $_POST['be_location'] ), ! empty( $_POST['be_lookup_address'] ) );
} catch ( \Throwable $error ) {
set_transient( 'burn_events_error_' . get_current_user_id(), $error->getMessage(), MINUTE_IN_SECONDS );
}
}
private static function valid_save( int $id, string $key, string $action ): bool {
return ! wp_is_post_autosave( $id ) && ! wp_is_post_revision( $id ) && current_user_can( 'edit_post', $id )
&& isset( $_POST[ $key ] ) && is_string( $_POST[ $key ] ) && wp_verify_nonce( wp_unslash( $_POST[ $key ] ), $action );
}
public static function errors(): void {
$key = 'burn_events_error_' . get_current_user_id();
$error = get_transient( $key );
if ( $error ) {
delete_transient( $key );
echo '<div class="notice notice-error"><p>' . esc_html( $error ) . '</p></div>';
}
}
public static function columns( array $columns ): array {
$columns['burn_event_schedule'] = __( 'Schedule', 'burn-events' );
return $columns;
}
public static function column( string $name, int $id ): void {
if ( 'burn_event_schedule' === $name ) {
$data = Events::event_data( $id );
$row = Events::occurrence( $id );
echo $row ? esc_html( Calendar::period( $row, $data ) ) : esc_html__( 'Unscheduled', 'burn-events' );
if ( $data['schedule'] && 'none' !== $data['schedule']['repeat'] ) {
echo '<br>' . esc_html__( 'Recurring', 'burn-events' );
}
}
}
}