Files
eventLabs/includes/class-community.php
2026-11-11 21:30:20 -05:00

214 lines
12 KiB
PHP

<?php
namespace BurnEvents;
use WP_Post;
defined( 'ABSPATH' ) || exit;
final class Community {
public static function register(): void {
add_shortcode( 'burn_events_submit', [ self::class, 'form' ] );
add_shortcode( 'burn_events_mine', [ self::class, 'mine' ] );
add_rewrite_rule( '^events/(submit|mine)/?$', 'index.php?be_community=$matches[1]', 'top' );
add_filter( 'redirect_canonical', static fn( $url ) => get_query_var( 'be_community' ) ? false : $url );
add_filter( 'document_title_parts', static function ( array $parts ): array {
$view = get_query_var( 'be_community' );
if ( in_array( $view, [ 'submit', 'mine' ], true ) ) {
$parts['title'] = 'mine' === $view ? __( 'My events', 'burn-events' ) : __( 'Submit an event', 'burn-events' );
}
return $parts;
} );
add_action( 'template_redirect', static function (): void {
if ( get_query_var( 'be_community' ) ) {
nocache_headers();
}
} );
add_action( 'admin_post_burn_event_submit', [ self::class, 'submit' ] );
add_action( 'admin_post_nopriv_burn_event_submit', [ self::class, 'submit' ] );
add_action( 'admin_post_burn_event_withdraw', [ self::class, 'withdraw' ] );
}
public static function url( string $view ): string {
return get_option( 'permalink_structure' )
? home_url( user_trailingslashit( 'events/' . $view ) )
: add_query_arg( 'be_community', $view, home_url( '/' ) );
}
private static function owned( int $id ): ?WP_Post {
$post = $id && is_user_logged_in() ? get_post( $id ) : null;
return $post && Events::EVENT === $post->post_type && 'trash' !== $post->post_status
&& (int) $post->post_author === get_current_user_id() && get_post_meta( $post->ID, '_burn_community', true ) ? $post : null;
}
public static function form(): string {
$options = Admin::options();
$id = absint( Calendar::request( 'be_edit' ) );
$post = $id && $options['community_edit'] ? self::owned( $id ) : null;
ob_start();
?>
<section class="be-community">
<header class="be-calendar-header">
<h1><?php echo esc_html( $post ? __( 'Edit your event', 'burn-events' ) : __( 'Submit an event', 'burn-events' ) ); ?></h1>
<?php if ( is_user_logged_in() ) : ?><a href="<?php echo esc_url( self::url( 'mine' ) ); ?>"><?php esc_html_e( 'My events', 'burn-events' ); ?></a><?php endif; ?>
</header>
<?php if ( '1' === Calendar::request( 'be_sent' ) ) : ?>
<p class="be-confirmation" role="status"><?php esc_html_e( 'Your event was submitted for review.', 'burn-events' ); ?></p>
<?php endif; ?>
<?php if ( ! is_user_logged_in() && ! $options['anonymous'] ) : ?>
<p><a class="be-button" href="<?php echo esc_url( wp_login_url( self::url( 'submit' ) ) ); ?>"><?php esc_html_e( 'Log in to submit an event', 'burn-events' ); ?></a></p>
<?php elseif ( $id && ! $post ) : ?>
<p><?php esc_html_e( 'This submission is unavailable for editing.', 'burn-events' ); ?></p>
<?php else : ?>
<form class="be-submission" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" enctype="multipart/form-data">
<input type="hidden" name="action" value="burn_event_submit">
<input type="hidden" name="event_id" value="<?php echo esc_attr( (string) $id ); ?>">
<?php wp_nonce_field( 'burn_event_submit_' . $id ); ?>
<p><?php esc_html_e( 'Submissions are reviewed before they appear on the calendar.', 'burn-events' ); ?></p>
<label><?php esc_html_e( 'Event title', 'burn-events' ); ?><input name="event_title" required maxlength="180" value="<?php echo esc_attr( $post->post_title ?? '' ); ?>"></label>
<label><?php esc_html_e( 'Description', 'burn-events' ); ?><textarea name="event_description" rows="6" maxlength="12000" required><?php echo esc_textarea( $post->post_content ?? '' ); ?></textarea></label>
<?php
$defaults = [ 'venue' => array_filter( [ $options['venue'] ] ), 'organizer' => array_filter( [ $options['organizer'] ] ) ];
Admin::event_fields( $post ? Events::event_data( $id ) : $defaults, true );
$terms = get_terms( [ 'taxonomy' => Events::CATEGORY, 'hide_empty' => false ] );
$selected = $post ? wp_get_post_terms( $id, Events::CATEGORY, [ 'fields' => 'ids' ] ) : [];
?>
<?php if ( $terms && ! is_wp_error( $terms ) ) : ?>
<label><?php esc_html_e( 'Event categories', 'burn-events' ); ?><select name="event_categories[]" multiple size="3"><?php foreach ( $terms as $term ) : ?><option value="<?php echo esc_attr( (string) $term->term_id ); ?>" <?php selected( is_array( $selected ) && in_array( $term->term_id, $selected, true ) ); ?>><?php echo esc_html( $term->name ); ?></option><?php endforeach; ?></select></label>
<?php endif; ?>
<?php if ( is_user_logged_in() ) : ?><label><?php esc_html_e( 'Featured image', 'burn-events' ); ?><input name="event_image" type="file" accept="image/jpeg,image/png,image/webp"></label><?php endif; ?>
<label class="be-honeypot" aria-hidden="true"><?php esc_html_e( 'Leave this blank', 'burn-events' ); ?><input name="be_website" tabindex="-1" autocomplete="off"></label>
<button class="be-button" type="submit"><?php esc_html_e( 'Send for review', 'burn-events' ); ?></button>
</form>
<?php endif; ?>
</section>
<?php
return ob_get_clean();
}
public static function submit(): void {
$id = isset( $_POST['event_id'] ) && is_scalar( $_POST['event_id'] ) ? absint( $_POST['event_id'] ) : 0;
check_admin_referer( 'burn_event_submit_' . $id );
$options = Admin::options();
$post = $id && $options['community_edit'] ? self::owned( $id ) : null;
if ( ( ! is_user_logged_in() && ! $options['anonymous'] ) || ( $id && ! $post ) || ! empty( $_POST['be_website'] ) ) {
self::fail( __( 'This submission is unavailable.', 'burn-events' ), 403 );
}
$rate_key = 'be_submit_' . hash_hmac( 'sha256', (string) ( $_SERVER['REMOTE_ADDR'] ?? '' ) . '/' . get_current_user_id(), wp_salt() );
if ( get_transient( $rate_key ) ) {
self::fail( __( 'Please wait a moment before submitting again.', 'burn-events' ), 429 );
}
$title = isset( $_POST['event_title'] ) && is_string( $_POST['event_title'] ) ? sanitize_text_field( wp_unslash( $_POST['event_title'] ) ) : '';
$body = isset( $_POST['event_description'] ) && is_string( $_POST['event_description'] ) ? wp_kses_post( wp_unslash( $_POST['event_description'] ) ) : '';
// Public submissions contain formatted text. Shortcodes are added by editors during review.
$body = str_replace( '[', '&#91;', $body );
if ( ! $title || ! $body || strlen( $title ) > 720 || strlen( $body ) > 48000 || ! is_array( $_POST['be'] ?? null ) ) {
self::fail( __( 'Enter a title, description, and event schedule.', 'burn-events' ) );
}
$event = null;
try {
$input = wp_unslash( $_POST['be'] );
unset( $input['status'], $input['series'], $input['featured'], $input['hidden'] );
$data = Events::validate_event( $input );
if ( $post ) {
$old = Events::event_data( $id );
foreach ( [ 'status', 'series', 'featured', 'hidden' ] as $field ) {
$data[ $field ] = $old[ $field ];
}
}
$attributes = [
'post_type' => Events::EVENT, 'post_title' => $title, 'post_content' => $body,
'post_status' => 'pending', 'comment_status' => 'closed',
];
$event = $id ? wp_update_post( wp_slash( $attributes + [ 'ID' => $id ] ), true ) : wp_insert_post( wp_slash( $attributes + [ 'post_author' => get_current_user_id() ] ), true );
if ( is_wp_error( $event ) ) {
self::fail( $event->get_error_message() );
}
Events::save( $event, $data );
update_post_meta( $event, '_burn_community', '1' );
$categories = array_values( array_filter( array_map( 'absint', (array) ( $_POST['event_categories'] ?? [] ) ) ) );
wp_set_object_terms( $event, $categories, Events::CATEGORY );
if ( is_user_logged_in() && UPLOAD_ERR_OK === ( $_FILES['event_image']['error'] ?? UPLOAD_ERR_NO_FILE ) ) {
self::image( $event );
}
} catch ( \Throwable $error ) {
if ( ! $id && is_int( $event ) ) {
wp_trash_post( $event );
}
self::fail( $error->getMessage() );
}
set_transient( $rate_key, '1', 10 );
wp_safe_redirect( add_query_arg( 'be_sent', '1', self::url( 'submit' ) ), 303 );
exit;
}
private static function image( int $event ): void {
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$file = media_handle_upload( 'event_image', $event, [], [
'test_form' => false,
'mimes' => [ 'jpg|jpeg' => 'image/jpeg', 'png' => 'image/png', 'webp' => 'image/webp' ],
] );
if ( ! is_wp_error( $file ) ) {
set_post_thumbnail( $event, $file );
}
}
public static function mine(): string {
$page = max( 1, min( 1000, absint( Calendar::request( 'be_page' ) ) ) );
$list = is_user_logged_in() ? new \WP_Query( [
'post_type' => Events::EVENT, 'post_status' => [ 'publish', 'pending', 'draft', 'future' ],
'author' => get_current_user_id(), 'posts_per_page' => 20, 'paged' => $page,
'meta_key' => '_burn_community', 'meta_value' => '1',
] ) : null;
ob_start();
?>
<section class="be-community">
<header class="be-calendar-header"><h1><?php esc_html_e( 'My events', 'burn-events' ); ?></h1><a href="<?php echo esc_url( self::url( 'submit' ) ); ?>"><?php esc_html_e( 'Submit an event', 'burn-events' ); ?></a></header>
<?php if ( ! $list ) : ?>
<p><a class="be-button" href="<?php echo esc_url( wp_login_url( self::url( 'mine' ) ) ); ?>"><?php esc_html_e( 'Log in to see your submissions', 'burn-events' ); ?></a></p>
<?php elseif ( ! $list->posts ) : ?>
<p class="be-empty"><?php esc_html_e( 'You have no submissions on this page.', 'burn-events' ); ?></p>
<?php else : ?>
<ul class="be-submission-list">
<?php foreach ( $list->posts as $post ) : ?>
<li>
<strong><?php echo esc_html( get_the_title( $post ) ); ?></strong>
<span><?php echo esc_html( get_post_status_object( $post->post_status )->label ); ?></span>
<?php if ( 'publish' === $post->post_status ) : ?><a href="<?php echo esc_url( get_permalink( $post ) ); ?>"><?php esc_html_e( 'View', 'burn-events' ); ?></a><?php endif; ?>
<?php if ( Admin::options()['community_edit'] ) : ?>
<a href="<?php echo esc_url( add_query_arg( 'be_edit', $post->ID, self::url( 'submit' ) ) ); ?>"><?php esc_html_e( 'Edit', 'burn-events' ); ?></a>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
<input type="hidden" name="action" value="burn_event_withdraw">
<input type="hidden" name="event_id" value="<?php echo esc_attr( (string) $post->ID ); ?>">
<?php wp_nonce_field( 'burn_event_withdraw_' . $post->ID ); ?>
<button type="submit"><?php esc_html_e( 'Withdraw', 'burn-events' ); ?></button>
</form>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php echo wp_kses_post( paginate_links( [ 'base' => add_query_arg( 'be_page', '%#%', self::url( 'mine' ) ), 'format' => '', 'total' => $list->max_num_pages, 'current' => $page ] ) ); ?>
<?php endif; ?>
</section>
<?php
return ob_get_clean();
}
public static function withdraw(): void {
$id = isset( $_POST['event_id'] ) && is_scalar( $_POST['event_id'] ) ? absint( $_POST['event_id'] ) : 0;
check_admin_referer( 'burn_event_withdraw_' . $id );
if ( ! Admin::options()['community_edit'] || ! self::owned( $id ) ) {
self::fail( __( 'You cannot withdraw this event.', 'burn-events' ), 403 );
}
wp_trash_post( $id );
wp_safe_redirect( self::url( 'mine' ), 303 );
exit;
}
private static function fail( string $message, int $status = 400 ): void {
wp_die( esc_html( $message ), esc_html__( 'Event submission', 'burn-events' ), [ 'response' => $status, 'back_link' => true ] );
}
}