namespace, '/events/submit', [ [ 'methods' => 'POST', 'callback' => [ $this, 'submit_event' ], 'permission_callback' => [ $this, 'submit_event_permissions_check' ], 'args' => [ 'title' => $this->rest_arg_string( 200, true ), 'start' => $this->rest_arg_iso_date( true ), 'description' => $this->rest_arg_rich_text(), 'location' => $this->rest_arg_string( 500, false ), 'duration' => [ 'required' => false, 'type' => 'integer', 'default' => 60, 'sanitize_callback' => 'absint', 'validate_callback' => static fn( $v ): bool => ( is_int( $v ) || ( is_string( $v ) && ctype_digit( $v ) ) ) && (int) $v >= 0, ], 'allDay' => $this->rest_arg_bool( false ), 'category_id' => $this->rest_arg_positive_int( false ), ], ], ] ); register_rest_route( $this->namespace, '/submissions', [ [ 'methods' => 'GET', 'callback' => [ $this, 'get_submissions' ], 'permission_callback' => [ $this, 'manage_submissions_permissions_check' ], ], ] ); $submission_id_arg = [ 'id' => [ 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', 'validate_callback' => static fn( $v ): bool => is_numeric( $v ) && (int) $v > 0, ], ]; register_rest_route( $this->namespace, '/submissions/(?P\d+)/approve', [ [ 'methods' => 'POST', 'callback' => [ $this, 'approve_submission' ], 'permission_callback' => [ $this, 'manage_submissions_permissions_check' ], 'args' => $submission_id_arg, ], ] ); register_rest_route( $this->namespace, '/submissions/(?P\d+)/reject', [ [ 'methods' => 'POST', 'callback' => [ $this, 'reject_submission' ], 'permission_callback' => [ $this, 'manage_submissions_permissions_check' ], 'args' => array_merge( $submission_id_arg, // Optional reason is accepted by the schema so the // API surface is complete; the current handler // does not persist it (future work). [ 'reason' => $this->rest_arg_string( 500, false ) ] ), ], ] ); } /** * Check permissions for submitting events. */ public function submit_event_permissions_check( WP_REST_Request $request ): bool|\WP_Error { $rate_limit = $this->check_rate_limit( 20, 60 ); if ( is_wp_error( $rate_limit ) ) { return $rate_limit; } if ( ! webcal_feature_available( 'frontend_submission' ) ) { return new \WP_Error( 'rest_forbidden', __( 'Frontend event submission is not available on your plan.', 'agenticdaisy-calendar' ), [ 'status' => 403 ] ); } $raw_opt = get_option( 'webcal_frontend_submission', 'no' ); if ( ! is_scalar( $raw_opt ) || (string) $raw_opt !== 'yes' ) { return new \WP_Error( 'rest_forbidden', __( 'Frontend event submission is not enabled.', 'agenticdaisy-calendar' ), [ 'status' => 403 ] ); } return $this->check_permission( 'webcal_can_submit' ); } /** * Check permissions for managing submissions (approve/reject/list). */ public function manage_submissions_permissions_check( WP_REST_Request $request ): bool|\WP_Error { $rate_limit = $this->check_rate_limit(); if ( is_wp_error( $rate_limit ) ) { return $rate_limit; } return $this->check_permission( 'webcal_can_approve' ); } /** * Submit a new event for review (or direct creation if approval not required). */ public function submit_event( WP_REST_Request $request ): WP_REST_Response|WP_Error { /** @var array $params */ $params = $this->safe_params( $request ); // Schema (see register_routes()) has already run sanitize_text_field // and validate-non-empty via rest_arg_string(200, true); keep // $this->str() as the PHPStan Level 9 mixed→string coercion. $title = $this->str( $params['title'] ?? '' ); if ( $title === '' ) { return new WP_Error( 'webcal_validation', __( 'Title is required.', 'agenticdaisy-calendar' ), [ 'status' => 400 ] ); } $start_str = $this->str( $params['start'] ?? '' ); if ( $start_str === '' ) { return new WP_Error( 'webcal_validation', __( 'Start date is required.', 'agenticdaisy-calendar' ), [ 'status' => 400 ] ); } try { $start = new \DateTimeImmutable( $start_str ); } catch ( \Exception $e ) { return new WP_Error( 'webcal_validation', __( 'Invalid start date.', 'agenticdaisy-calendar' ), [ 'status' => 400 ] ); } $is_all_day = ! empty( $params['allDay'] ); $creator_login = wp_get_current_user()->user_login; // Determine status based on approval setting $raw_approval = get_option( 'webcal_submission_requires_approval', 'yes' ); $requires_approval = is_scalar( $raw_approval ) && (string) $raw_approval === 'yes'; $status = $requires_approval ? 'TENTATIVE' : null; try { // description/location are sanitized by the schema // (wp_kses_post and sanitize_text_field respectively via // rest_arg_rich_text / rest_arg_string). $this->str/int stay // for PHPStan Level 9 mixed→narrow coercion. $event = new Event( id: new EventId( 0 ), uid: wp_generate_uuid4(), name: $title, description: $this->str( $params['description'] ?? '' ), location: $this->str( $params['location'] ?? '' ), start: $start, duration: $is_all_day ? 1440 : $this->int( $params['duration'] ?? 60 ), createdBy: $creator_login, type: EventType::EVENT, access: AccessLevel::PUBLIC, recurrence: new Recurrence(), status: $status, allDay: $is_all_day, ); $event_service = $this->container->get_event_service(); $event_service->createEvent( $event, $this->get_current_actor() ); // Look up created event by UID to get the real ID $event_repo = $this->container->get_event_repository(); $saved_event = $event_repo->findByUid( $event->uid() ); $event_id = $saved_event ? $saved_event->id()->value() : 0; // Handle category association if ( $event_id && ! empty( $params['category_id'] ) ) { $cat_id = $this->int( $params['category_id'] ); if ( $cat_id > 0 ) { $category_repo = $this->container->get_category_repository(); $category_repo->assignToEvent( new EventId( $event_id ), $creator_login, [ $cat_id ] ); } } // Notify admin of new submission (if email_notifications available and approval required) if ( $requires_approval && webcal_feature_available( 'email_notifications' ) ) { $this->notify_admin_of_submission( $title, $creator_login, $start ); } $message = $requires_approval ? __( 'Your event has been submitted for review.', 'agenticdaisy-calendar' ) : __( 'Your event has been created.', 'agenticdaisy-calendar' ); return new WP_REST_Response( [ 'id' => $event_id, 'status' => $requires_approval ? 'pending' : 'approved', 'message' => $message, ], 201 ); } catch ( \Exception $e ) { $this->get_logger()->error( 'REST Error in SubmissionsController (submit_event): ' . $e->getMessage() ); return new WP_Error( 'webcal_error', $e->getMessage(), [ 'status' => 500 ] ); } } /** * List all pending (TENTATIVE) submissions. */ public function get_submissions( WP_REST_Request $request ): WP_REST_Response|WP_Error { try { $event_repo = $this->container->get_event_repository(); $transformer = $this->container->get_event_transformer(); $events = $event_repo->findByStatus( 'TENTATIVE' ); $data = []; foreach ( $events as $event ) { $transformed = $transformer->transform_event( $event ); $transformed['submitted_by_display'] = $this->get_display_name( $event->createdBy() ); $data[] = $transformed; } return new WP_REST_Response( $data, 200 ); } catch ( \Exception $e ) { $this->get_logger()->error( 'REST Error in SubmissionsController (get_submissions): ' . $e->getMessage() ); return new WP_Error( 'webcal_error', $e->getMessage(), [ 'status' => 500 ] ); } } /** * Approve a pending submission. */ public function approve_submission( WP_REST_Request $request ): WP_REST_Response|WP_Error { $id = $this->int( $request->get_param( 'id' ) ); try { $event_service = $this->container->get_event_service(); $event = $event_service->getEventById( new EventId( $id ) ); if ( ! $event ) { return new WP_Error( 'webcal_not_found', __( 'Submission not found.', 'agenticdaisy-calendar' ), [ 'status' => 404 ] ); } if ( $event->status() !== 'TENTATIVE' ) { return new WP_Error( 'webcal_invalid', __( 'This event is not pending approval.', 'agenticdaisy-calendar' ), [ 'status' => 400 ] ); } // Re-create event with status = null (confirmed) $approved = new Event( id: $event->id(), uid: $event->uid(), name: $event->name(), description: $event->description(), location: $event->location(), start: $event->start(), duration: $event->duration(), createdBy: $event->createdBy(), type: $event->type(), access: $event->access(), recurrence: $event->recurrence(), status: null, allDay: $event->isAllDay(), ); $event_service->updateEvent( $approved, $this->get_current_actor() ); // Notify submitter if ( webcal_feature_available( 'email_notifications' ) ) { $this->notify_submitter( $event->createdBy(), $event->name(), 'approved' ); } return new WP_REST_Response( [ 'id' => $id, 'status' => 'approved', ], 200 ); } catch ( \Exception $e ) { $this->get_logger()->error( 'REST Error in SubmissionsController (approve_submission): ' . $e->getMessage() ); return new WP_Error( 'webcal_error', $e->getMessage(), [ 'status' => 500 ] ); } } /** * Reject a pending submission (deletes the event). */ public function reject_submission( WP_REST_Request $request ): WP_REST_Response|WP_Error { $id = $this->int( $request->get_param( 'id' ) ); try { $event_service = $this->container->get_event_service(); $event = $event_service->getEventById( new EventId( $id ) ); if ( ! $event ) { return new WP_Error( 'webcal_not_found', __( 'Submission not found.', 'agenticdaisy-calendar' ), [ 'status' => 404 ] ); } if ( $event->status() !== 'TENTATIVE' ) { return new WP_Error( 'webcal_invalid', __( 'This event is not pending approval.', 'agenticdaisy-calendar' ), [ 'status' => 400 ] ); } $submitter_login = $event->createdBy(); $event_name = $event->name(); // Delete the event $event_service->deleteEvent( new EventId( $id ), $this->get_current_actor() ); // Notify submitter if ( webcal_feature_available( 'email_notifications' ) ) { $this->notify_submitter( $submitter_login, $event_name, 'rejected' ); } return new WP_REST_Response( null, 204 ); } catch ( \Exception $e ) { $this->get_logger()->error( 'REST Error in SubmissionsController (reject_submission): ' . $e->getMessage() ); return new WP_Error( 'webcal_error', $e->getMessage(), [ 'status' => 500 ] ); } } /** * Notify admin users of a new submission. */ private function notify_admin_of_submission( string $title, string $submitter, \DateTimeImmutable $start ): void { $admins = \get_users( [ 'role' => 'administrator', 'number' => 10, ] ); $subject = sprintf( /* translators: %s: event title */ __( '[WebCalendar] New event submission: %s', 'agenticdaisy-calendar' ), $title ); $message = sprintf( /* translators: 1: submitter login, 2: event title, 3: event date */ __( "A new event has been submitted for review.\n\nSubmitted by: %1\$s\nTitle: %2\$s\nDate: %3\$s\n\nReview pending submissions in the WordPress admin.", 'agenticdaisy-calendar' ), $submitter, $title, $start->format( 'Y-m-d H:i' ) ); foreach ( $admins as $admin ) { \wp_mail( $admin->user_email, $subject, $message ); } } /** * Notify the event submitter about approval/rejection. */ private function notify_submitter( string $login, string $event_name, string $action ): void { $user = \get_user_by( 'login', $login ); if ( ! $user || ! $user->user_email ) { return; } if ( $action === 'approved' ) { $subject = sprintf( /* translators: %s: event title */ __( '[WebCalendar] Your event has been approved: %s', 'agenticdaisy-calendar' ), $event_name ); $message = sprintf( /* translators: %s: event title */ __( 'Your submitted event "%s" has been approved and is now visible on the calendar.', 'agenticdaisy-calendar' ), $event_name ); } else { $subject = sprintf( /* translators: %s: event title */ __( '[WebCalendar] Your event submission was not approved: %s', 'agenticdaisy-calendar' ), $event_name ); $message = sprintf( /* translators: %s: event title */ __( 'Your submitted event "%s" was reviewed and not approved.', 'agenticdaisy-calendar' ), $event_name ); } \wp_mail( $user->user_email, $subject, $message ); } /** * Get the display name for a user login. */ private function get_display_name( string $login ): string { $user = \get_user_by( 'login', $login ); return $user ? $user->display_name : $login; } /** * Get the count of pending (TENTATIVE) submissions. * * Static helper for use by Settings.php admin badge. * * @return int */ public static function get_pending_count(): int { try { $container = \WebCalendar\WP\Container::get_instance(); return $container->get_event_repository()->countByStatus( 'TENTATIVE' ); } catch ( \Exception $e ) { return 0; } } }