$request * @return Context[] */ public function collect(int $userId, array $request): array { $out = []; $post = $this->post($userId, $request['post_id'] ?? null); if ($post !== null) { $out[] = $post; } $screen = self::screenFrom($request['screen'] ?? null); if ($screen !== null) { // No text: the screen is a place, not a document, and the label is the whole of it. $out[] = new Context("screen:{$screen['id']}", "On: {$screen['title']}", '', ['screen' => $screen['id']]); } return $out; } /** * The screen the chat was opened on, as `{id, title}` cleaned, or null when the value is not * one: anything but an array whose `id` and `title` are strings (a bare string, which is what * `screen` was in 0.5, a list, a missing or non-string member), or one that cleans to an * empty id or an empty title. * * The title is untrusted text: a page title any plugin can register, arriving through the REST * request because the turn is not made on the screen. It goes through line(), the rule the post * title goes through, and is then cut to TITLE_CHARS characters with the ellipsis inside the * cap and no space before it. A title that is not valid UTF-8 cleans to empty, and so is no screen. The id is reduced * to lowercase letters, digits, `_` and `-`, at most 64 of them, so it can be written into a * Context id and an attribute as it stands. * * Public because `GET /view/panel` cleans the two strings with it before rendering the chip * whose fields the turn sends back. A cleaned screen cleans to itself, so the two ends agree. * * @return array{id: string, title: string}|null */ public static function screenFrom(mixed $value): ?array { if (!is_array($value) || !is_string($value['id'] ?? null) || !is_string($value['title'] ?? null)) { return null; } $id = substr((string) preg_replace('/[^a-z0-9_-]/', '', strtolower($value['id'])), 0, 64); $title = self::line($value['title']); if ($id === '' || $title === '') { return null; } if (mb_strlen($title) > self::TITLE_CHARS) { // Trimmed first, so a cut that lands after a space does not end "… …". $title = rtrim(mb_substr($title, 0, self::TITLE_CHARS - 1)) . '…'; } return ['id' => $id, 'title' => $title]; } /** The post being edited, as a context, when the class docblock's gate lets it through; else null. */ private function post(int $userId, mixed $value): ?Context { $postId = self::postId($value); if ($postId === 0 || !user_can($userId, 'edit_post', $postId)) { return null; } $post = get_post($postId); if ($post === null || $post->post_status === self::UNSTARTED) { return null; } $text = trim(wp_strip_all_tags($post->post_content)); if (mb_strlen($text) > self::MAX_CHARS) { $text = mb_substr($text, 0, self::MAX_CHARS) . '…'; } return new Context( "current-screen:{$post->post_type}:{$postId}", sprintf('Editing: %s (%s, %s)', self::line($post->post_title), $post->post_type, $post->post_status), $text, ['post_id' => $postId], ); } /** * A title as one line of text for a label: tags stripped, every run of whitespace made one * space, and the ends trimmed. The `u` flag is what makes `\s` match Unicode's line breaks * (NEL, the line separator, the paragraph separator) as well as ASCII's; without it they pass * through. It also makes the pattern refuse a string that is not valid UTF-8 whole, which * comes out as ''. */ private static function line(string $title): string { return trim((string) preg_replace('/\s+/u', ' ', wp_strip_all_tags($title))); } /** * 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; } }