$events Events straight from core. * @param array $shared_logins Logins of shared calendars the * caller explicitly requested. * @return list */ public static function filter( iterable $events, array $shared_logins = [] ): array { $visible = []; foreach ( $events as $event ) { if ( self::can_view( $event, $shared_logins ) ) { $visible[] = $event; } } return $visible; } /** * Whether the current viewer may see one event. * * @param array $shared_logins Logins of shared calendars the * caller explicitly requested. */ public static function can_view( Event $event, array $shared_logins = [] ): bool { if ( ! self::passes_moderation_check( $event ) ) { return false; } if ( current_user_can( 'webcal_can_view_all_events' ) ) { return true; } $login = self::current_login(); if ( $login !== null && $event->createdBy() === $login ) { return true; } if ( isset( $shared_logins[ $event->createdBy() ] ) ) { return true; } return $event->access() === AccessLevel::PUBLIC; } /** * Pending frontend submissions stay off the calendar until approved. * * Visible to whoever submitted them, and to anyone who can approve. This * is what holds an unmoderated submission back, so it is checked before * the access-level rules rather than alongside them. */ private static function passes_moderation_check( Event $event ): bool { if ( $event->status() !== 'TENTATIVE' ) { return true; } $login = self::current_login(); if ( $login !== null && $event->createdBy() === $login ) { return true; } return current_user_can( 'webcal_can_approve' ); } /** * The current viewer's login, or null when there is no usable one. * * An anonymous request carries an empty WP_User whose `user_login` reads * back as `false` (WP_User::__get() falls through to get_user_meta( 0 )), * so this deliberately returns null rather than a falsy string: comparing * `'' === $event->createdBy()` would hand ownership of every * blank-creator event to every logged-out visitor. * * @psalm-suppress RedundantCastGivenDocblockType -- The cast is not * redundant at runtime. wordpress-stubs types WP_User::$user_login as * string, but an empty user returns false from the magic getter; both * Psalm and PHPStan believe the stub. Dropping the cast to satisfy Psalm * makes PHPStan demand the is_string() check back, and dropping both * reintroduces the false-vs-'' bug this guards. */ private static function current_login(): ?string { if ( ! is_user_logged_in() ) { return null; } // The cast is load-bearing: WP_User types user_login as string, but an // empty user reports it as false at runtime, and `false === (string)` // comparisons behave differently from `'' === (string)`. $login = (string) wp_get_current_user()->user_login; return $login === '' ? null : $login; } }