Publish eventLabs 0.5.0
This commit is contained in:
146
includes/class-burnworks.php
Normal file
146
includes/class-burnworks.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace BurnEvents;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use WP_REST_Request;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class BurnWorks {
|
||||
public const META = '_eventlabs_burnworks';
|
||||
public const UUID = '[0-9a-fA-F]{8}(?:-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}';
|
||||
|
||||
public static function register(): void {
|
||||
$path = '/burnworks/workspaces/(?P<workspace_id>' . self::UUID . ')/events/(?P<event_id>' . self::UUID . ')';
|
||||
register_rest_route( Api::NAMESPACE, $path, [
|
||||
[
|
||||
'methods' => 'GET', 'callback' => [ self::class, 'read' ],
|
||||
'permission_callback' => [ Api::class, 'permission' ],
|
||||
],
|
||||
[
|
||||
'methods' => 'PUT', 'callback' => [ self::class, 'publish' ],
|
||||
'permission_callback' => [ Api::class, 'permission' ],
|
||||
],
|
||||
] );
|
||||
}
|
||||
|
||||
public static function schema(): array {
|
||||
$string = static fn( int $max ): array => [ 'type' => 'string', 'maxLength' => $max ];
|
||||
$uuid = [ 'type' => 'string', 'pattern' => '^' . self::UUID . '$' ];
|
||||
$date = [ 'type' => 'string', 'format' => 'date-time' ];
|
||||
$boolean = [ 'type' => 'boolean' ];
|
||||
return [
|
||||
'type' => 'object',
|
||||
'required' => [ 'club_id', 'title', 'description', 'starts_at', 'venue', 'status', 'audience', 'capacity', 'kind' ],
|
||||
'additionalProperties' => false,
|
||||
'properties' => [
|
||||
'workspace_id' => $uuid, 'id' => $uuid, 'club_id' => $uuid,
|
||||
'title' => [ 'type' => 'string', 'minLength' => 2, 'maxLength' => 160 ],
|
||||
'description' => $string( 4000 ), 'venue' => $string( 160 ), 'starts_at' => $date,
|
||||
'status' => [ 'type' => 'string', 'enum' => [ 'draft', 'published', 'cancelled' ] ],
|
||||
'audience' => [ 'type' => 'string', 'enum' => [ 'public', 'members', 'private' ] ],
|
||||
'capacity' => [ 'type' => 'integer', 'minimum' => 0, 'maximum' => 100000 ],
|
||||
'kind' => [ 'type' => 'string', 'enum' => [ 'event', 'volunteer', 'meeting' ] ],
|
||||
'created_by' => $string( 254 ), 'created_at' => $date,
|
||||
'ends_at' => $date, 'website_enabled' => $boolean, 'public_events_enabled' => $boolean,
|
||||
'publish_to_wordpress' => $boolean,
|
||||
'source_version' => [ 'type' => 'integer', 'minimum' => 1, 'maximum' => 9007199254740991 ],
|
||||
'custom' => [ 'type' => 'object', 'additionalProperties' => true ],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function snapshot( int $id ): array {
|
||||
return self::decode_snapshot( get_post_meta( $id, self::META, true ) );
|
||||
}
|
||||
|
||||
private static function decode_snapshot( $value ): array {
|
||||
if ( is_array( $value ) ) {
|
||||
return $value;
|
||||
}
|
||||
$record = is_string( $value ) ? json_decode( $value ) : null;
|
||||
return $record instanceof \stdClass ? (array) $record : [];
|
||||
}
|
||||
|
||||
private static function identity( WP_REST_Request $request ): array {
|
||||
$params = $request->get_url_params();
|
||||
return array_map( 'strtolower', [ $params['workspace_id'], $params['event_id'] ] );
|
||||
}
|
||||
|
||||
public static function read( WP_REST_Request $request ) {
|
||||
return Api::external( 'events', 'burnworks', implode( ':', self::identity( $request ) ) );
|
||||
}
|
||||
|
||||
public static function publish( WP_REST_Request $request ) {
|
||||
$raw = json_decode( $request->get_body() );
|
||||
if ( ! $raw instanceof \stdClass || ! $request->is_json_content_type() ) {
|
||||
return Api::error( 'invalid_json', __( 'Send a JSON object with Content-Type: application/json.', 'burn-events' ), 400 );
|
||||
}
|
||||
$input = $request->get_json_params();
|
||||
$schema = self::schema();
|
||||
$valid = rest_validate_value_from_schema( $input, $schema, 'body' );
|
||||
if ( is_wp_error( $valid ) ) {
|
||||
$valid->add_data( [ 'status' => 400 ] );
|
||||
return $valid;
|
||||
}
|
||||
[ $workspace, $id ] = self::identity( $request );
|
||||
if ( strtolower( $input['workspace_id'] ?? $workspace ) !== $workspace || strtolower( $input['id'] ?? $id ) !== $id ) {
|
||||
return Api::error( 'identity_mismatch', __( 'The burnWorks IDs must match the route.', 'burn-events' ), 400 );
|
||||
}
|
||||
$record = rest_sanitize_value_from_schema( $input, $schema, 'body' );
|
||||
$record += [ 'workspace_id' => $workspace, 'id' => $id ];
|
||||
if ( isset( $raw->custom ) ) {
|
||||
if ( ! $raw->custom instanceof \stdClass ) {
|
||||
return Api::error( 'invalid_custom', __( 'custom must be a JSON object.', 'burn-events' ), 400 );
|
||||
}
|
||||
$record['custom'] = $raw->custom;
|
||||
}
|
||||
|
||||
$start = new DateTimeImmutable( $record['starts_at'] );
|
||||
$end = isset( $record['ends_at'] ) ? new DateTimeImmutable( $record['ends_at'] ) : $start->modify( '+1 hour' );
|
||||
if ( $end->getTimestamp() <= $start->getTimestamp() ) {
|
||||
return Api::error( 'invalid_time_range', __( 'The burnWorks event end must be after its start.', 'burn-events' ), 422 );
|
||||
}
|
||||
$utc = new DateTimeZone( 'UTC' );
|
||||
$start = $start->setTimezone( $utc );
|
||||
$end = $end->setTimezone( $utc );
|
||||
return Api::write( 'events', [
|
||||
'title' => $record['title'],
|
||||
'description' => esc_html( $record['description'] ),
|
||||
'location' => $record['venue'],
|
||||
'post_status' => self::publishable( $record ) ? 'publish' : 'draft',
|
||||
'status' => 'cancelled' === $record['status'] ? 'cancelled' : 'scheduled',
|
||||
'schedule' => [
|
||||
'start_date' => $start->format( 'Y-m-d' ), 'start_time' => $start->format( 'H:i:s' ),
|
||||
'end_date' => $end->format( 'Y-m-d' ), 'end_time' => $end->format( 'H:i:s' ),
|
||||
'timezone' => 'UTC', 'all_day' => false, 'repeat' => 'none',
|
||||
],
|
||||
], $request, 0, [ 'burnworks', $workspace . ':' . $id ], $record );
|
||||
}
|
||||
|
||||
private static function publishable( array $record ): bool {
|
||||
return 'published' === $record['status'] && 'public' === $record['audience']
|
||||
&& ! empty( $record['website_enabled'] ) && ! empty( $record['public_events_enabled'] )
|
||||
&& ( $record['publish_to_wordpress'] ?? true );
|
||||
}
|
||||
|
||||
public static function accepts_version( array $incoming, array $stored ): bool {
|
||||
$previous = $stored['source_version'] ?? 0;
|
||||
$version = $incoming['source_version'] ?? 0;
|
||||
return ! $previous || $version > $previous || ( $version === $previous && maybe_serialize( $incoming ) === maybe_serialize( $stored ) );
|
||||
}
|
||||
|
||||
public static function protect_publication( array $data, array $post ): array {
|
||||
if ( Events::EVENT !== $data['post_type'] || ! in_array( $data['post_status'], [ 'publish', 'future' ], true ) ) {
|
||||
return $data;
|
||||
}
|
||||
$id = (int) ( $post['ID'] ?? 0 );
|
||||
$record = $id ? self::snapshot( $id ) : self::decode_snapshot( wp_unslash( $post['meta_input'][ self::META ] ?? '' ) );
|
||||
if ( $record && ! self::publishable( $record ) ) {
|
||||
$data['post_status'] = 'draft';
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user