get_method(), ['GET', 'HEAD'], true) && preg_match('~^/wp/v2/pcwp_event/(\d+)$~', $request->get_route(), $match)) { $id = (int) $match[1]; if (! self::isPublic($id) && ! current_user_can('edit_post', $id)) { return new \WP_Error('pcwp_event_not_found', __('Event not found.', 'proeventswp'), ['status' => 404]); } } return $response; }, 10, 3); } public static function passwordRequired(int $id): bool { $parent = absint(get_post_meta($id, '_pcwp_series_id', true)); return post_password_required($id) || ($parent > 0 && get_post($parent) !== null && post_password_required($parent)); } public static function isPublic(int $id): bool { $parent = absint(get_post_meta($id, '_pcwp_series_id', true)); return get_post_type($id) === 'pcwp_event' && get_post_status($id) === 'publish' && ! self::passwordRequired($id) && ($parent === 0 || get_post($parent) === null || get_post_status($parent) === 'publish'); } /** * @param list<\WP_Post> $posts * @return list<\WP_Post> */ public function primeParents(array $posts): array { $ids = []; foreach ($posts as $post) { if ($post->post_type === 'pcwp_event') { $ids[] = $post->ID; } } if ($ids !== []) { update_meta_cache('post', $ids); $parents = array_filter(array_unique(array_map(static fn (int $id): int => absint(get_post_meta($id, '_pcwp_series_id', true)), $ids))); if ($parents !== []) { _prime_post_caches($parents, false, false); } } return $posts; } public function protectPage(): void { if (! is_singular('pcwp_event')) { return; } $id = get_queried_object_id(); $parentId = absint(get_post_meta($id, '_pcwp_series_id', true)); if ($parentId === 0) { return; } $parent = get_post($parentId); if (! $parent instanceof \WP_Post) { return; } if ($parent->post_status !== 'publish' && ! current_user_can('read_post', $parent->ID)) { global $wp_query; $wp_query->set_404(); status_header(404); nocache_headers(); return; } if ($parent->post_password === '' || ! post_password_required($parent)) { return; } nocache_headers(); header('X-Robots-Tag: noindex, nofollow'); wp_enqueue_style('pcwp-calendar-blocks'); echo ''; wp_head(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Core generates the escaped password form, including its input fields. echo '

' . esc_html(get_the_title($id)) . '

' . get_the_password_form($parent) . '
'; wp_footer(); echo ''; exit; } }