$request * @return Context[] */ public function collect(int $userId, array $request): array { $postId = self::postId($request['post_id'] ?? null); if ($postId === 0 || !user_can($userId, 'edit_post', $postId)) { return []; } $post = get_post($postId); if ($post === null) { return []; } $text = trim(wp_strip_all_tags($post->post_content)); if (mb_strlen($text) > self::MAX_CHARS) { $text = mb_substr($text, 0, self::MAX_CHARS) . '…'; } $title = trim((string) preg_replace('/\s+/', ' ', wp_strip_all_tags($post->post_title))); return [new Context( "current-screen:{$post->post_type}:{$postId}", sprintf('Editing: %s (%s, %s)', $title, $post->post_type, $post->post_status), $text, ['post_id' => $postId], )]; } /** * The post id as the client sent it, reduced to a positive int or 0. * * Only an int or a string of digits counts. A plain `(int)` cast would turn '12abc' into * 12, an array into 1 and true into 1 — each a post the user never named. */ private static function postId(mixed $value): int { if (is_string($value) && preg_match('/^[1-9][0-9]{0,17}$/', $value) === 1) { $value = (int) $value; } return is_int($value) && $value > 0 ? $value : 0; } }