From bcbf133dc2fbd8df7ac13e19f64f6e182eca60d1 Mon Sep 17 00:00:00 2001 From: Alt-Charlie Date: Mon, 9 Nov 2026 10:33:55 +0000 Subject: [PATCH 1/7] Add content drafts, structured data and collections, globals and variables on /health --- altimiser.php | 1 + src/Collections.php | 84 ++++++ src/DraftPosts.php | 169 +++++++++++ src/DraftRequest.php | 61 ++++ src/Globals.php | 118 ++++++++ src/MarkdownBlocks.php | 217 +++++++++++++++ src/PostVariables.php | 83 ++++++ src/Receiver.php | 57 ++++ src/StructuredDataApplier.php | 137 +++++++++ src/StructuredDataPrinter.php | 43 +++ src/StructuredDataRenderer.php | 139 ++++++++++ src/StructuredDataStore.php | 83 ++++++ tests/DraftsTest.php | 324 ++++++++++++++++++++++ tests/HealthListsTest.php | 197 +++++++++++++ tests/HealthTest.php | 7 +- tests/MarkdownBlocksTest.php | 113 ++++++++ tests/StructuredDataGlobalsTest.php | 79 ++++++ tests/StructuredDataTest.php | 416 ++++++++++++++++++++++++++++ 18 files changed, 2326 insertions(+), 2 deletions(-) create mode 100644 src/Collections.php create mode 100644 src/DraftPosts.php create mode 100644 src/DraftRequest.php create mode 100644 src/Globals.php create mode 100644 src/MarkdownBlocks.php create mode 100644 src/PostVariables.php create mode 100644 src/StructuredDataApplier.php create mode 100644 src/StructuredDataPrinter.php create mode 100644 src/StructuredDataRenderer.php create mode 100644 src/StructuredDataStore.php create mode 100644 tests/DraftsTest.php create mode 100644 tests/HealthListsTest.php create mode 100644 tests/MarkdownBlocksTest.php create mode 100644 tests/StructuredDataGlobalsTest.php create mode 100644 tests/StructuredDataTest.php diff --git a/altimiser.php b/altimiser.php index 2ff79a0..a23bb02 100644 --- a/altimiser.php +++ b/altimiser.php @@ -54,3 +54,4 @@ }); (new Updater(__FILE__))->register(); +(new AltDesign\Altimiser\StructuredDataPrinter())->register(); diff --git a/src/Collections.php b/src/Collections.php new file mode 100644 index 0000000..e30cbfa --- /dev/null +++ b/src/Collections.php @@ -0,0 +1,84 @@ + + */ + public function all(): array + { + $collections = []; + + foreach (get_post_types(['public' => true], 'objects') as $type) { + $route = $this->route($type); + + if ($type->name === 'attachment' || $route === '') { + continue; + } + + $collections[] = [ + 'handle' => $type->name, + 'title' => (string) ($type->labels->name ?? $type->label), + 'route' => $route, + 'loopable' => is_post_type_viewable($type), + ]; + } + + return $collections; + } + + public function exists(string $handle): bool + { + return in_array($handle, array_column($this->all(), 'handle'), true); + } + + public function loopable(string $handle): bool + { + foreach ($this->all() as $collection) { + if ($collection['handle'] === $handle) { + return $collection['loopable']; + } + } + + return false; + } + + /** + * Empty where the type has no address of its own, which includes every type + * while the site uses plain permalinks. + */ + private function route(WP_Post_Type $type): string + { + global $wp_rewrite; + + if (! is_post_type_viewable($type)) { + return ''; + } + + $structure = match ($type->name) { + 'post' => (string) $wp_rewrite->permalink_structure, + 'page' => (string) $wp_rewrite->get_page_permastruct(), + default => (string) $wp_rewrite->get_extra_permastruct($type->name), + }; + + if ($structure === '') { + return ''; + } + + $named = str_replace(["%{$type->name}%", '%postname%', '%pagename%'], '%slug%', $structure); + $variables = (string) preg_replace('/%([a-z_]+)%/', '{ $1 }', $named); + + return '/'.trim($variables, '/'); + } +} diff --git a/src/DraftPosts.php b/src/DraftPosts.php new file mode 100644 index 0000000..9266e89 --- /dev/null +++ b/src/DraftPosts.php @@ -0,0 +1,169 @@ + + * + * @throws RuntimeException when WordPress cannot save the post + */ + public function store(DraftRequest $draft): array + { + $beside = null; + + if ($draft->expands !== null) { + $beside = $this->content->find($draft->expands); + + if (! $beside instanceof WP_Post) { + return $this->refused('page_not_found', "Nothing lives at {$draft->expands}, so there is nothing to put the draft beside."); + } + } + + $postType = $beside?->post_type ?? (string) $draft->collection; + + if ($beside === null && ! $this->collections->exists($postType)) { + return $this->refused('collection_not_found', "There is no collection called \"{$postType}\" on this site."); + } + + $existing = $this->existing($draft->reference); + + if ($existing !== null && ! $this->isUnpublished($existing)) { + return $this->refused('already_published', "The draft for this idea was published as \"{$existing->post_title}\", so it is left alone."); + } + + if (! post_type_supports($postType, 'editor')) { + return $this->refused('no_body_field', "The {$postType} post type has no editor to write the draft into."); + } + + $id = $existing === null + ? $this->create($draft, $postType, $beside) + : $this->rewrite($draft, $existing); + + $this->storeDescription($id, $draft); + + return [ + 'status' => $existing === null ? 'created' : 'updated', + 'id' => (string) $id, + 'collection' => $postType, + 'edit_url' => admin_url("post.php?post={$id}&action=edit"), + ]; + } + + private function existing(string $reference): ?WP_Post + { + $found = get_posts([ + 'post_type' => array_values(get_post_types()), + 'post_status' => 'any', + 'meta_key' => $this->referenceKey, + 'meta_value' => $reference, + 'numberposts' => 1, + 'orderby' => 'ID', + 'order' => 'ASC', + ]); + + return $found[0] ?? null; + } + + private function isUnpublished(WP_Post $post): bool + { + return in_array($post->post_status, ['draft', 'pending', 'auto-draft'], true); + } + + private function create(DraftRequest $draft, string $postType, ?WP_Post $beside): int + { + return $this->save('wp_insert_post', [ + 'post_type' => $postType, + 'post_status' => 'draft', + 'post_title' => $this->title($draft), + 'post_content' => $this->markdown->convert($draft->body), + 'post_name' => $beside === null ? sanitize_title($draft->title) : "{$beside->post_name}-draft", + 'post_parent' => $beside?->post_parent ?? 0, + 'meta_input' => [$this->referenceKey => $draft->reference], + ]); + } + + private function rewrite(DraftRequest $draft, WP_Post $existing): int + { + return $this->save('wp_update_post', [ + 'ID' => $existing->ID, + 'post_title' => $this->title($draft), + 'post_content' => $this->markdown->convert($draft->body), + ]); + } + + private function title(DraftRequest $draft): string + { + return trim(wp_strip_all_tags($draft->title)); + } + + /** + * Kses is taken off for the write because a signed request has no user, so + * WordPress would filter the content as it would a contributor's. It goes back + * on afterwards only if it was on. + * + * @param array $post + */ + private function save(callable $write, array $post): int + { + $filtered = has_filter('content_save_pre', 'wp_filter_post_kses') !== false; + + kses_remove_filters(); + + try { + $id = $write(wp_slash($post), true); + } finally { + if ($filtered) { + kses_init_filters(); + } + } + + if (is_wp_error($id)) { + throw new RuntimeException($id->get_error_message()); + } + + return (int) $id; + } + + private function storeDescription(int $id, DraftRequest $draft): void + { + $post = get_post($id); + + if ($this->yoast === null || $draft->metaDescription === null || ! $post instanceof WP_Post) { + return; + } + + $this->yoast->write($post, 'metadesc', sanitize_text_field($draft->metaDescription)); + } + + /** + * @return array{status: string, reason: string, message: string} + */ + private function refused(string $reason, string $message): array + { + return ['status' => 'refused', 'reason' => $reason, 'message' => $message]; + } +} diff --git a/src/DraftRequest.php b/src/DraftRequest.php new file mode 100644 index 0000000..02b5e69 --- /dev/null +++ b/src/DraftRequest.php @@ -0,0 +1,61 @@ + $payload + */ + private static function text(array $payload, string $key, int $limit, bool $required = false): ?string + { + $value = $payload[$key] ?? null; + + if ($value === null || (is_string($value) && trim($value) === '')) { + return $required ? throw new InvalidArgumentException("The {$key} is required.") : null; + } + + if (! is_string($value)) { + throw new InvalidArgumentException("The {$key} has to be text."); + } + + if (mb_strlen($value) > $limit) { + throw new InvalidArgumentException("The {$key} can be at most {$limit} characters."); + } + + return $value; + } +} diff --git a/src/Globals.php b/src/Globals.php new file mode 100644 index 0000000..1e2bff8 --- /dev/null +++ b/src/Globals.php @@ -0,0 +1,118 @@ +}> + */ + private ?array $sets = null; + + /** + * @return array}> + */ + public function all(): array + { + if ($this->sets !== null) { + return $this->sets; + } + + if (! $this->acfIsActive()) { + return $this->sets = []; + } + + $sets = []; + + foreach ((array) acf_get_options_pages() as $page) { + $handle = (string) ($page['menu_slug'] ?? ''); + $fields = $this->fields($handle, $page['post_id'] ?? 'options'); + + if ($handle === '' || $fields === []) { + continue; + } + + $sets[] = ['handle' => $handle, 'title' => (string) ($page['page_title'] ?? $handle), 'fields' => $fields]; + } + + return $this->sets = $sets; + } + + /** + * Null where the options page or the field is not one of those reported, so + * that a template naming something that is not there can be told from an empty value. + */ + public function value(string $handle, string $field): ?string + { + foreach ($this->all() as $set) { + if ($set['handle'] !== $handle) { + continue; + } + + foreach ($set['fields'] as $candidate) { + if ($candidate['handle'] === $field) { + return $candidate['value']; + } + } + } + + return null; + } + + private function acfIsActive(): bool + { + foreach (['acf_get_options_pages', 'acf_get_field_groups', 'acf_get_fields', 'get_field'] as $function) { + if (! function_exists($function)) { + return false; + } + } + + return true; + } + + /** + * @return array + */ + private function fields(string $optionsPage, mixed $postId): array + { + $fields = []; + + foreach ((array) acf_get_field_groups(['options_page' => $optionsPage]) as $group) { + foreach ((array) acf_get_fields($group) as $field) { + $value = $this->singleLine($field, $postId); + + if ($value !== null) { + $fields[] = ['handle' => (string) $field['name'], 'display' => (string) ($field['label'] ?? $field['name']), 'value' => $value]; + } + } + } + + return $fields; + } + + /** + * @param array $field + */ + private function singleLine(array $field, mixed $postId): ?string + { + if (($field['type'] ?? null) !== 'text' || ! is_string($field['name'] ?? null) || $field['name'] === '') { + return null; + } + + $value = get_field($field['name'], $postId); + + if (! is_string($value) || trim($value) === '' || preg_match('/[\r\n]/', trim($value)) === 1) { + return null; + } + + return trim($value); + } +} diff --git a/src/MarkdownBlocks.php b/src/MarkdownBlocks.php new file mode 100644 index 0000000..04ce603 --- /dev/null +++ b/src/MarkdownBlocks.php @@ -0,0 +1,217 @@ +blocks($lines)); + } + + /** + * @param array $lines + * @return array + */ + private function blocks(array $lines): array + { + $blocks = []; + $paragraph = []; + $position = 0; + + while ($position < count($lines)) { + $line = $lines[$position]; + + if (trim($line) === '') { + $this->flush($paragraph, $blocks); + $position++; + + continue; + } + + $block = $this->structural($lines, $position); + + if ($block === null) { + $paragraph[] = trim($line); + $position++; + + continue; + } + + $this->flush($paragraph, $blocks); + $blocks[] = $block; + } + + $this->flush($paragraph, $blocks); + + return $blocks; + } + + /** + * Moves the position past what it consumed. + * + * @param array $lines + */ + private function structural(array $lines, int &$position): ?string + { + $line = $lines[$position]; + + if (preg_match('/^\s{0,3}(#{1,6})\s+(.*?)(?:\s+#+)?\s*$/', $line, $heading) === 1) { + $position++; + + return $this->heading(strlen($heading[1]), $heading[2]); + } + + if (preg_match('/^\s{0,3}([-*_])(\s*\1){2,}\s*$/', $line) === 1) { + $position++; + + return "\n
\n"; + } + + if (preg_match('/^\s*>/', $line) === 1) { + return $this->quote($lines, $position); + } + + if (preg_match('/^\s*(?:[-*+]|\d+[.)])\s+/', $line) === 1) { + return $this->list($lines, $position); + } + + return null; + } + + private function heading(int $level, string $text): string + { + $attributes = $level === 2 ? '' : " {\"level\":{$level}}"; + + return "\n{$this->inline($text)}\n"; + } + + /** + * @param array $lines + */ + private function quote(array $lines, int &$position): string + { + $inner = []; + + while ($position < count($lines) && preg_match('/^\s*>\s?(.*)$/', $lines[$position], $quoted) === 1) { + $inner[] = $quoted[1]; + $position++; + } + + $body = implode("\n\n", $this->blocks($inner)); + + return "\n
{$body}
\n"; + } + + /** + * @param array $lines + */ + private function list(array $lines, int &$position): string + { + $ordered = preg_match('/^\s*\d+[.)]\s+/', $lines[$position]) === 1; + $marker = $ordered ? '\d+[.)]' : '[-*+]'; + $items = ''; + + while ($position < count($lines) && preg_match("/^\s*{$marker}\s+(.*)$/", $lines[$position], $item) === 1) { + $items .= "\n
  • {$this->inline($item[1])}
  • \n"; + $position++; + } + + $tag = $ordered ? 'ol' : 'ul'; + $attributes = $ordered ? ' {"ordered":true}' : ''; + + return "\n<{$tag} class=\"wp-block-list\">{$items}\n"; + } + + /** + * @param array $paragraph + * @param array $blocks + */ + private function flush(array &$paragraph, array &$blocks): void + { + if ($paragraph === []) { + return; + } + + $blocks[] = "\n

    {$this->inline(implode("\n", $paragraph))}

    \n"; + $paragraph = []; + } + + private function inline(string $text): string + { + $stash = []; + + $withoutMarkers = (string) preg_replace_callback( + '/\[GAP:[^\]]*\]/', + function (array $marker) use (&$stash): string { + return $this->stash($this->escape($marker[0]), $stash); + }, + $text, + ); + + $converted = $this->emphasis($this->links($this->escape($withoutMarkers), $stash)); + + return (string) preg_replace_callback( + "/\u{E000}(\d+)\u{E001}/u", + fn (array $placeholder): string => $stash[(int) $placeholder[1]], + $converted, + ); + } + + /** + * @param array $stash + */ + private function stash(string $html, array &$stash): string + { + $stash[] = $html; + + return "\u{E000}".(count($stash) - 1)."\u{E001}"; + } + + /** + * The tags are set aside with the markers, so an underscore in an address is + * not taken for emphasis. + * + * @param array $stash + */ + private function links(string $escaped, array &$stash): string + { + return (string) preg_replace_callback('/\[([^\]]+)\]\(([^)\s]+)\)/', function (array $link) use (&$stash): string { + $address = esc_url(html_entity_decode($link[2], ENT_QUOTES | ENT_HTML5, 'UTF-8')); + + if ($address === '') { + return $link[1]; + } + + return $this->stash("", $stash).$link[1].$this->stash('', $stash); + }, $escaped); + } + + private function emphasis(string $html): string + { + $rules = [ + '/`([^`]+)`/' => '$1', + '/(\*\*|__)(?=\S)(.+?)(?<=\S)\1/s' => '$2', + '/(? '$1', + '/(? '$1', + ]; + + return (string) preg_replace(array_keys($rules), array_values($rules), $html); + } + + private function escape(string $text): string + { + return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); + } +} diff --git a/src/PostVariables.php b/src/PostVariables.php new file mode 100644 index 0000000..56fa2db --- /dev/null +++ b/src/PostVariables.php @@ -0,0 +1,83 @@ + + */ + public function descriptions(): array + { + return [ + 'title' => "The post's title", + 'meta_description' => "The post's meta description, from Yoast SEO where it is active, otherwise its excerpt", + 'permalink' => "The post's full address", + 'excerpt' => "The post's excerpt, or the start of its text where it has none", + 'featured_image' => "The address of the post's featured image, empty where it has none", + 'date_published' => 'When the post was published, as an ISO 8601 date and time', + 'date_modified' => 'When the post was last modified, as an ISO 8601 date and time', + 'author_name' => "The post's author's display name", + 'site_name' => "The site's name", + 'site_url' => "The site's home address", + ]; + } + + /** + * @return array + */ + public function for(WP_Post $post): array + { + return [ + 'title' => $this->plain($post->post_title), + 'meta_description' => $this->metaDescription($post), + 'permalink' => (string) get_permalink($post), + 'excerpt' => $this->excerpt($post), + 'featured_image' => (string) get_the_post_thumbnail_url($post, 'full'), + 'date_published' => (string) get_post_time('c', false, $post), + 'date_modified' => (string) get_post_modified_time('c', false, $post), + 'author_name' => (string) get_the_author_meta('display_name', (int) $post->post_author), + 'site_name' => get_bloginfo('name'), + 'site_url' => home_url(), + ]; + } + + private function metaDescription(WP_Post $post): string + { + $fromYoast = $this->yoast === null ? '' : $this->yoastDescription($this->yoast, $post); + + return $fromYoast === '' ? $this->excerpt($post) : $fromYoast; + } + + private function yoastDescription(Yoast $yoast, WP_Post $post): string + { + $stored = $yoast->stored($post, 'metadesc'); + $template = $stored === '' ? $yoast->template($post, 'metadesc') : $stored; + + return $this->plain($yoast->render($template, $post)); + } + + private function excerpt(WP_Post $post): string + { + $text = $post->post_excerpt !== '' + ? $post->post_excerpt + : wp_trim_words(strip_shortcodes(wp_strip_all_tags($post->post_content)), 55, ''); + + return $this->plain($text); + } + + private function plain(string $value): string + { + return trim(html_entity_decode(wp_strip_all_tags($value), ENT_QUOTES | ENT_HTML5, 'UTF-8')); + } +} diff --git a/src/Receiver.php b/src/Receiver.php index 00c31e5..5277db7 100644 --- a/src/Receiver.php +++ b/src/Receiver.php @@ -2,6 +2,8 @@ namespace AltDesign\Altimiser; +use InvalidArgumentException; +use RuntimeException; use Throwable; use WP_Error; use WP_REST_Request; @@ -27,6 +29,12 @@ public function registerRoutes(): void 'callback' => [$this, 'changes'], 'permission_callback' => [$this, 'verifySignature'], ]); + + register_rest_route('altimiser/v1', '/drafts', [ + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => [$this, 'drafts'], + 'permission_callback' => [$this, 'verifySignature'], + ]); } /** @@ -98,6 +106,9 @@ public function health(): WP_REST_Response 'capabilities' => $this->capabilities($checks), 'checks' => $checks, 'integrations' => ['yoast' => $this->yoastState()], + 'collections' => $this->orEmpty(fn (): array => (new Collections())->all()), + 'globals' => $this->orEmpty(fn (): array => (new Globals())->all()), + 'variables' => (new PostVariables($this->yoast))->descriptions(), 'setup' => $this->setup(), 'site_url' => home_url(), ]); @@ -132,10 +143,29 @@ private function capabilities(array $checks): array { return array_keys(array_filter([ 'content.update' => $checks !== [], + 'content.draft' => true, 'content.link' => in_array('link.opportunity', $checks, true), ])); } + /** + * A failure reading collections or globals leaves the list empty and not the + * connection refused. + * + * @param callable(): array $read + * @return array + */ + private function orEmpty(callable $read): array + { + try { + return $read(); + } catch (Throwable $exception) { + error_log("Altimiser could not read a list for /health: {$exception->getMessage()}"); + + return []; + } + } + /** * Settings that stop fixes from counting, which Altimiser reports as findings. * @@ -184,6 +214,27 @@ public function changes(WP_REST_Request $request): WP_REST_Response|WP_Error ]); } + /** + * An empty body is refused because a signature does not cover the path, so a + * signed empty body would otherwise be accepted here for five minutes. + */ + public function drafts(WP_REST_Request $request): WP_REST_Response|WP_Error + { + try { + $draft = DraftRequest::fromPayload(json_decode($request->get_body(), true)); + } catch (InvalidArgumentException $exception) { + return new WP_Error('altimiser_unreadable', $exception->getMessage(), ['status' => 422]); + } + + $posts = new DraftPosts(new ContentResolver(), new Collections(), new MarkdownBlocks(), $this->yoast); + + try { + return new WP_REST_Response($posts->store($draft)); + } catch (RuntimeException $exception) { + return new WP_Error('altimiser_write_failed', $exception->getMessage(), ['status' => 500]); + } + } + /** * @return array */ @@ -191,12 +242,18 @@ private function appliers(): array { $content = new ContentResolver(); $writer = new ContentWriter(); + $variables = new PostVariables($this->yoast); return [ $this->yoast === null ? new InactiveSeoPlugin() : new YoastApplier($this->yoast, $content), new AltTextApplier($content, $writer), new LinkApplier($content, new MentionLinker(), $writer), new RobotsApplier(), + new StructuredDataApplier( + new StructuredDataStore(), + new StructuredDataRenderer($variables, new Globals(), new Collections()), + $content, + ), ]; } diff --git a/src/StructuredDataApplier.php b/src/StructuredDataApplier.php new file mode 100644 index 0000000..1653f08 --- /dev/null +++ b/src/StructuredDataApplier.php @@ -0,0 +1,137 @@ +suggestedValue ?? ''); + + if ($block === '') { + return ChangeResult::skipped( + $change->id, + 'no_suggestion', + 'There is no replacement value for this check, and this receiver cannot work one out for itself.', + ); + } + + $record = $this->content->find($change->url); + + if ($record === null) { + return ChangeResult::skipped($change->id, 'not_found', "Nothing editable answers to {$change->url}."); + } + + if (! $record instanceof WP_Post) { + return ChangeResult::skipped($change->id, 'no_field', 'Structured data is kept on posts and pages, and this address is a term.'); + } + + if ($this->renderer->render($block, $record) === null) { + return ChangeResult::skipped( + $change->id, + 'invalid_markup', + 'This markup does not render to valid JSON for this page, so nothing was stored.', + ); + } + + return $this->store->sharesDefault($record->post_type) + ? $this->applyAsDefault($change, $record, $block, $dryRun) + : $this->applyToPost($change, $record, $block, $dryRun); + } + + /** + * @return array + */ + private function applyAsDefault(ChangeRequest $change, WP_Post $post, string $block, bool $dryRun): array + { + $current = $this->store->defaultFor($post->post_type); + $location = [ + 'type' => 'collection', + 'collection' => $post->post_type, + 'field' => $this->store->defaultsOption(), + ]; + + if ($current === $block) { + return ChangeResult::alreadyApplied($change->id, $location, 'This post type already defaults to this markup.'); + } + + if ($current !== '') { + return ChangeResult::skipped( + $change->id, + 'default_present', + "The {$post->post_type} post type already has default schema markup, which was left in place.", + ); + } + + if ($dryRun) { + return ChangeResult::applied($change->id, $location, null, $block); + } + + $this->store->writeDefault($post->post_type, $block); + + if ($this->store->defaultFor($post->post_type) !== $block) { + return ChangeResult::failed($change->id, 'write_failed', "Nothing new was stored in {$location['field']}."); + } + + return ChangeResult::applied($change->id, $location, null, $block); + } + + /** + * @return array + */ + private function applyToPost(ChangeRequest $change, WP_Post $post, string $block, bool $dryRun): array + { + $current = $this->store->own($post); + $location = [ + 'type' => 'post', + 'id' => $post->ID, + 'collection' => $post->post_type, + 'field' => $this->store->metaKey(), + ]; + + if ($current === $block) { + return ChangeResult::alreadyApplied($change->id, $location, 'This page already has this markup.'); + } + + if ($current !== '') { + return ChangeResult::skipped($change->id, 'value_changed', 'This page has structured data of its own, which was left alone.'); + } + + if ($dryRun) { + return ChangeResult::applied($change->id, $location, null, $block); + } + + $this->store->writeOwn($post, $block); + + if ($this->store->own($post) !== $block) { + return ChangeResult::failed($change->id, 'write_failed', "Nothing new was stored in {$location['field']}."); + } + + return ChangeResult::applied($change->id, $location, null, $block); + } +} diff --git a/src/StructuredDataPrinter.php b/src/StructuredDataPrinter.php new file mode 100644 index 0000000..b16cfcb --- /dev/null +++ b/src/StructuredDataPrinter.php @@ -0,0 +1,43 @@ +blockFor($post); + + if ($block === '') { + return; + } + + $variables = new PostVariables(defined('WPSEO_VERSION') ? new Yoast() : null); + $json = (new StructuredDataRenderer($variables, new Globals(), new Collections()))->render($block, $post); + + if ($json !== null) { + echo "\n"; + } + } +} diff --git a/src/StructuredDataRenderer.php b/src/StructuredDataRenderer.php new file mode 100644 index 0000000..9f5b11d --- /dev/null +++ b/src/StructuredDataRenderer.php @@ -0,0 +1,139 @@ +expand($template, $this->variables->for($post), null, $failed); + + if ($failed) { + return null; + } + + $decoded = json_decode($rendered); + + if (json_last_error() !== JSON_ERROR_NONE || (! is_object($decoded) && ! is_array($decoded))) { + return null; + } + + $json = wp_json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP); + + return is_string($json) ? $json : null; + } + + /** + * A single pass, so a value that itself holds {{ title }} is printed as text + * and not run again. + * + * @param array $values + * @param ?array{count: int, last: bool} $position + */ + private function expand(string $template, array $values, ?array $position, bool &$failed): string + { + $pattern = '/\{\{\s*collection:([\w-]+)[^}]*\}\}(.*?)\{\{\s*\/collection(?::\1)?\s*\}\}' + .'|\{\{\s*unless\s+last\s*\}\}(.*?)\{\{\s*\/unless\s*\}\}' + .'|\{\{\s*([A-Za-z_][\w-]*)(?::([\w-]+))?\s*\}\}' + .'|\{\{.*?\}\}/s'; + + return (string) preg_replace_callback($pattern, function (array $tag) use ($values, $position, &$failed): string { + if ($tag[1] !== null && $position === null) { + return $this->loop($tag[1], $tag[2], $failed); + } + + if ($tag[3] !== null && $position !== null) { + return $position['last'] ? '' : $this->expand($tag[3], $values, $position, $failed); + } + + if ($tag[4] !== null) { + return $this->value($tag[4], $tag[5] ?? '', $values, $position, $failed); + } + + $failed = true; + + return ''; + }, $template, -1, $count, PREG_UNMATCHED_AS_NULL); + } + + private function loop(string $handle, string $body, bool &$failed): string + { + if (! $this->collections->loopable($handle)) { + return ''; + } + + $posts = get_posts([ + 'post_type' => $handle, + 'post_status' => 'publish', + 'numberposts' => -1, + 'orderby' => ['menu_order' => 'ASC', 'date' => 'DESC'], + 'no_found_rows' => true, + ]); + + $rendered = ''; + + foreach (array_values($posts) as $index => $post) { + $position = ['count' => $index + 1, 'last' => $index === count($posts) - 1]; + $rendered .= $this->expand($body, $this->variables->for($post), $position, $failed); + } + + return $rendered; + } + + /** + * @param array $values + * @param ?array{count: int, last: bool} $position + */ + private function value(string $name, string $field, array $values, ?array $position, bool &$failed): string + { + if ($field !== '') { + $global = $this->globals->value($name, $field); + $failed = $failed || $global === null; + + return $this->escape($global ?? ''); + } + + if ($name === 'count' && $position !== null) { + return (string) $position['count']; + } + + if (! array_key_exists($name, $values)) { + $failed = true; + + return ''; + } + + return $this->escape($values[$name]); + } + + private function escape(string $value): string + { + $encoded = (string) wp_json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); + + return substr($encoded, 1, -1); + } +} diff --git a/src/StructuredDataStore.php b/src/StructuredDataStore.php new file mode 100644 index 0000000..09cb0d3 --- /dev/null +++ b/src/StructuredDataStore.php @@ -0,0 +1,83 @@ +metaKey; + } + + public function defaultsOption(): string + { + return $this->defaultsOption; + } + + /** + * Pages are left out because they differ from one another, unlike the posts of + * a type, so a block written for one of them says nothing true about the rest. + */ + public function sharesDefault(string $postType): bool + { + return ! in_array($postType, (array) apply_filters('altimiser_structured_data_default_exclusions', ['page']), true); + } + + public function own(WP_Post $post): string + { + $stored = get_post_meta($post->ID, $this->metaKey, true); + + return is_string($stored) ? trim($stored) : ''; + } + + public function defaultFor(string $postType): string + { + $stored = $this->defaults()[$postType] ?? ''; + + return is_string($stored) ? trim($stored) : ''; + } + + public function blockFor(WP_Post $post): string + { + $own = $this->own($post); + + return $own !== '' ? $own : $this->defaultFor($post->post_type); + } + + public function writeOwn(WP_Post $post, string $block): void + { + update_post_meta($post->ID, $this->metaKey, wp_slash($block)); + } + + /** + * Options are not unslashed on the way in, unlike post meta, so the block goes + * in as it is. + */ + public function writeDefault(string $postType, string $block): void + { + update_option($this->defaultsOption, [...$this->defaults(), $postType => $block], false); + } + + /** + * @return array + */ + private function defaults(): array + { + $stored = get_option($this->defaultsOption, []); + + return is_array($stored) ? $stored : []; + } +} diff --git a/tests/DraftsTest.php b/tests/DraftsTest.php new file mode 100644 index 0000000..71e47f2 --- /dev/null +++ b/tests/DraftsTest.php @@ -0,0 +1,324 @@ +set_permalink_structure('/%postname%/'); + + register_post_type('team', ['public' => true, 'label' => 'Team', 'rewrite' => ['slug' => 'team'], 'supports' => ['title', 'editor']]); + register_post_type('snippet', ['public' => true, 'label' => 'Snippets', 'rewrite' => ['slug' => 'snippet'], 'supports' => ['title']]); + create_initial_taxonomies(); + flush_rewrite_rules(); + } + + public function tear_down(): void + { + _unregister_post_type('team'); + _unregister_post_type('snippet'); + WPSEO_Options::clear_cache(); + + parent::tear_down(); + } + + /** + * @param array $overrides + * @return array + */ + private function draft(array $overrides = []): array + { + return [ + 'reference' => 'idea-42', + 'title' => 'Loft conversion costs', + 'meta_description' => 'What a loft conversion costs and why.', + 'body' => "Most cost between [GAP: typical range].\n\n## How long it takes\n\n[GAP: typical duration]", + 'collection' => 'post', + ...$overrides, + ]; + } + + private function signedDraft(string $body, ?int $timestamp = null): WP_REST_Request + { + $timestamp ??= time(); + $request = new WP_REST_Request('POST', '/altimiser/v1/drafts'); + + $request->set_body($body); + $request->set_header('Content-Type', 'application/json'); + $request->set_header('X-Altimiser-Timestamp', (string) $timestamp); + $request->set_header('X-Altimiser-Signature', $this->protocolSignature($body, $timestamp, self::$secret)); + + return $request; + } + + /** + * @param array $overrides + */ + private function sendDraft(array $overrides = []): WP_REST_Response + { + return $this->dispatch($this->signedDraft((string) json_encode($this->draft($overrides)))); + } + + private function postFor(string $reference): ?WP_Post + { + return get_posts([ + 'post_type' => 'any', + 'post_status' => 'any', + 'meta_key' => '_altimiser_draft_reference', + 'meta_value' => $reference, + ])[0] ?? null; + } + + public function test_saves_the_draft_unpublished_in_the_named_post_type(): void + { + $response = $this->sendDraft(['collection' => 'team']); + $post = $this->postFor('idea-42'); + + $this->assertSame(200, $response->get_status()); + $this->assertSame('created', $response->get_data()['status']); + $this->assertSame((string) $post->ID, $response->get_data()['id']); + $this->assertSame('team', $response->get_data()['collection']); + $this->assertSame('draft', $post->post_status); + $this->assertSame('team', $post->post_type); + $this->assertSame('Loft conversion costs', $post->post_title); + } + + public function test_builds_the_edit_address_from_the_admin_address(): void + { + $response = $this->sendDraft(); + $id = $response->get_data()['id']; + + $this->assertSame(admin_url("post.php?post={$id}&action=edit"), $response->get_data()['edit_url']); + } + + public function test_writes_the_body_as_blocks_with_every_gap_kept(): void + { + $this->sendDraft(); + $blocks = parse_blocks($this->postFor('idea-42')->post_content); + + $this->assertSame(['core/paragraph', 'core/heading', 'core/paragraph'], array_column(array_filter($blocks, fn (array $block): bool => $block['blockName'] !== null), 'blockName')); + $this->assertStringContainsString('[GAP: typical range]', $this->postFor('idea-42')->post_content); + $this->assertStringContainsString('[GAP: typical duration]', $this->postFor('idea-42')->post_content); + } + + public function test_puts_the_meta_description_in_yoasts_field(): void + { + $this->sendDraft(); + + $this->assertSame( + 'What a loft conversion costs and why.', + get_post_meta($this->postFor('idea-42')->ID, '_yoast_wpseo_metadesc', true), + ); + } + + public function test_keeps_backslashes_in_the_title_and_body(): void + { + $this->sendDraft(['title' => 'Files in C:\temp', 'body' => 'Saved in C:\temp\new.']); + $post = $this->postFor('idea-42'); + + $this->assertSame('Files in C:\temp', $post->post_title); + $this->assertStringContainsString('C:\temp\new.', $post->post_content); + } + + public function test_sending_the_same_reference_again_rewrites_the_same_post(): void + { + $first = $this->sendDraft()->get_data(); + $second = $this->sendDraft(['title' => 'Loft conversion prices', 'body' => 'A new body.'])->get_data(); + $post = $this->postFor('idea-42'); + + $this->assertSame('created', $first['status']); + $this->assertSame('updated', $second['status']); + $this->assertSame($first['id'], $second['id']); + $this->assertSame('Loft conversion prices', $post->post_title); + $this->assertStringContainsString('A new body.', $post->post_content); + $this->assertCount(1, get_posts(['post_type' => 'post', 'post_status' => 'any', 'meta_key' => '_altimiser_draft_reference'])); + } + + public function test_leaves_a_published_draft_alone_and_says_so(): void + { + $id = (int) $this->sendDraft()->get_data()['id']; + wp_update_post(['ID' => $id, 'post_status' => 'publish', 'post_title' => 'Edited by an editor']); + + $response = $this->sendDraft(['title' => 'Overwritten', 'body' => 'Overwritten body.']); + $post = get_post($id); + + $this->assertSame('refused', $response->get_data()['status']); + $this->assertSame('already_published', $response->get_data()['reason']); + $this->assertNotSame('', $response->get_data()['message']); + $this->assertSame('Edited by an editor', $post->post_title); + $this->assertStringNotContainsString('Overwritten', $post->post_content); + $this->assertSame('publish', $post->post_status); + } + + public function test_saves_beside_the_page_it_expands_with_a_draft_slug_and_the_same_parent(): void + { + $parent = self::factory()->post->create(['post_type' => 'page', 'post_name' => 'services']); + $page = self::factory()->post->create(['post_type' => 'page', 'post_name' => 'lofts', 'post_parent' => $parent]); + + $response = $this->sendDraft(['collection' => null, 'expands' => get_permalink($page)]); + $draft = $this->postFor('idea-42'); + + $this->assertSame('created', $response->get_data()['status']); + $this->assertSame('page', $draft->post_type); + $this->assertSame($parent, $draft->post_parent); + $this->assertSame('lofts-draft', $draft->post_name); + $this->assertSame('draft', $draft->post_status); + $this->assertSame('publish', get_post($page)->post_status); + } + + public function test_refuses_when_nothing_lives_at_the_page_to_expand(): void + { + $response = $this->sendDraft(['collection' => null, 'expands' => home_url('/nowhere/')]); + + $this->assertSame('refused', $response->get_data()['status']); + $this->assertSame('page_not_found', $response->get_data()['reason']); + $this->assertNull($this->postFor('idea-42')); + } + + public function test_refuses_a_collection_the_site_does_not_have(): void + { + $response = $this->sendDraft(['collection' => 'recipes']); + + $this->assertSame('collection_not_found', $response->get_data()['reason']); + $this->assertNull($this->postFor('idea-42')); + } + + public function test_refuses_a_post_type_that_is_not_a_destination(): void + { + $this->assertSame('collection_not_found', $this->sendDraft(['collection' => 'attachment'])->get_data()['reason']); + $this->assertSame('collection_not_found', $this->sendDraft(['collection' => 'revision'])->get_data()['reason']); + } + + public function test_refuses_a_post_type_with_no_editor_for_the_body(): void + { + $response = $this->sendDraft(['collection' => 'snippet']); + + $this->assertSame('refused', $response->get_data()['status']); + $this->assertSame('no_body_field', $response->get_data()['reason']); + $this->assertNull($this->postFor('idea-42')); + } + + public function test_refuses_an_empty_body(): void + { + $response = $this->dispatch($this->signedDraft('')); + + $this->assertSame(422, $response->get_status()); + $this->assertSame('altimiser_unreadable', $response->get_data()['code']); + } + + public function test_refuses_an_empty_object(): void + { + $this->assertSame(422, $this->dispatch($this->signedDraft('{}'))->get_status()); + } + + /** + * @dataProvider invalidDrafts + * + * @param array $overrides + */ + public function test_refuses_a_draft_outside_the_limits_of_the_protocol(array $overrides): void + { + $response = $this->sendDraft($overrides); + + $this->assertSame(422, $response->get_status()); + $this->assertNull($this->postFor('idea-42')); + } + + /** + * @return array>> + */ + public static function invalidDrafts(): array + { + return [ + 'no reference' => [['reference' => null]], + 'reference too long' => [['reference' => str_repeat('a', 65)]], + 'no title' => [['title' => '']], + 'title too long' => [['title' => str_repeat('a', 201)]], + 'meta description too long' => [['meta_description' => str_repeat('a', 501)]], + 'no body' => [['body' => null]], + 'body too long' => [['body' => str_repeat('a', 100001)]], + 'no collection and no page' => [['collection' => null]], + 'collection too long' => [['collection' => str_repeat('a', 101)]], + 'expands too long' => [['expands' => str_repeat('a', 2001)]], + 'title not text' => [['title' => ['a']]], + ]; + } + + public function test_accepts_the_limits_of_the_protocol(): void + { + $response = $this->sendDraft([ + 'reference' => str_repeat('a', 64), + 'title' => str_repeat('a', 200), + 'meta_description' => str_repeat('a', 500), + 'body' => str_repeat('a', 100000), + ]); + + $this->assertSame('created', $response->get_data()['status']); + } + + public function test_accepts_a_draft_without_a_meta_description(): void + { + $response = $this->sendDraft(['meta_description' => null]); + + $this->assertSame('created', $response->get_data()['status']); + $this->assertSame('', get_post_meta((int) $response->get_data()['id'], '_yoast_wpseo_metadesc', true)); + } + + public function test_refuses_an_unsigned_request(): void + { + $request = new WP_REST_Request('POST', '/altimiser/v1/drafts'); + $request->set_body((string) json_encode($this->draft())); + + $this->assertSame(401, $this->dispatch($request)->get_status()); + $this->assertNull($this->postFor('idea-42')); + } + + public function test_leaves_content_filtering_as_it_found_it(): void + { + kses_init_filters(); + + $this->sendDraft(); + + $this->assertNotFalse(has_filter('content_save_pre', 'wp_filter_post_kses')); + + kses_remove_filters(); + + $this->sendDraft(['reference' => 'idea-43']); + + $this->assertFalse(has_filter('content_save_pre', 'wp_filter_post_kses')); + } + + public function test_keeps_the_draft_through_content_filtering_for_a_user_without_unfiltered_html(): void + { + kses_init_filters(); + + $this->sendDraft(['body' => "[Read more](https://example.com/more?a=1&b=2) in the guide.\n\n---"]); + $content = $this->postFor('idea-42')->post_content; + + $this->assertStringContainsString('Read more', $content); + $this->assertStringContainsString('has-alpha-channel-opacity"/>', $content); + } + + public function test_advertises_the_draft_capability(): void + { + $this->assertContains('content.draft', $this->dispatch($this->signedRequest())->get_data()['capabilities']); + } + + public function test_saves_without_a_meta_description_field_when_yoast_is_not_active(): void + { + $receiver = new AltDesign\Altimiser\Receiver(dirname(__DIR__).'/altimiser.php', null); + + $response = $receiver->drafts($this->signedDraft((string) json_encode($this->draft()))); + + $this->assertSame('created', $response->get_data()['status']); + $this->assertSame('', get_post_meta((int) $response->get_data()['id'], '_yoast_wpseo_metadesc', true)); + } +} diff --git a/tests/HealthListsTest.php b/tests/HealthListsTest.php new file mode 100644 index 0000000..a437676 --- /dev/null +++ b/tests/HealthListsTest.php @@ -0,0 +1,197 @@ +dispatch($this->signedRequest())->get_data(); + } + + private function usePrettyPermalinks(): void + { + $this->set_permalink_structure('/%postname%/'); + + register_post_type('team', ['public' => true, 'label' => 'Team', 'rewrite' => ['slug' => 'team']]); + register_post_type('hidden', ['public' => false]); + flush_rewrite_rules(); + } + + private function collection(string $handle): ?array + { + foreach ($this->health()['collections'] as $collection) { + if ($collection['handle'] === $handle) { + return $collection; + } + } + + return null; + } + + public function test_lists_public_post_types_with_the_route_their_posts_live_at(): void + { + $this->usePrettyPermalinks(); + + $this->assertSame( + ['handle' => 'team', 'title' => 'Team', 'route' => '/team/{ slug }', 'loopable' => true], + $this->collection('team'), + ); + $this->assertSame('/{ slug }', $this->collection('page')['route']); + $this->assertSame('/{ slug }', $this->collection('post')['route']); + } + + public function test_shows_a_dated_permalink_structure_with_its_variables(): void + { + $this->set_permalink_structure('/news/%year%/%postname%/'); + + $this->assertSame('/news/{ year }/{ slug }', $this->collection('post')['route']); + } + + public function test_leaves_out_attachments_and_types_without_public_pages(): void + { + $this->usePrettyPermalinks(); + + $handles = array_column($this->health()['collections'], 'handle'); + + $this->assertNotContains('attachment', $handles); + $this->assertNotContains('hidden', $handles); + } + + public function test_lists_no_collections_while_the_site_uses_plain_permalinks(): void + { + $this->set_permalink_structure(''); + + $this->assertSame([], $this->health()['collections']); + } + + public function test_lists_no_globals_where_acf_is_absent(): void + { + $this->assertFalse(function_exists('acf_get_options_pages')); + $this->assertSame([], $this->health()['globals']); + } + + /** + * @runInSeparateProcess + * + * @preserveGlobalState disabled + */ + public function test_lists_single_line_text_fields_from_acf_options_pages(): void + { + $this->defineAcf(); + + $this->assertSame([ + [ + 'handle' => 'site-settings', + 'title' => 'Site settings', + 'fields' => [ + ['handle' => 'telephone', 'display' => 'Telephone', 'value' => '+44 20 7946 0000'], + ], + ], + ], $this->health()['globals']); + } + + /** + * @runInSeparateProcess + * + * @preserveGlobalState disabled + */ + public function test_answers_with_an_empty_list_when_reading_acf_fails(): void + { + $errorLog = tempnam(sys_get_temp_dir(), 'altimiser'); + ini_set('error_log', $errorLog); + + function acf_get_options_pages(): array + { + throw new RuntimeException('ACF is not ready.'); + } + + function acf_get_field_groups(): array + { + return []; + } + + function acf_get_fields(): array + { + return []; + } + + function get_field(): string + { + return ''; + } + + $response = $this->dispatch($this->signedRequest()); + + $this->assertSame(200, $response->get_status()); + $this->assertSame([], $response->get_data()['globals']); + $this->assertStringContainsString('ACF is not ready.', (string) file_get_contents($errorLog)); + unlink($errorLog); + } + + private function defineAcf(): void + { + eval(<<<'PHP' + function acf_get_options_pages(): array + { + return [ + 'site-settings' => ['menu_slug' => 'site-settings', 'page_title' => 'Site settings', 'post_id' => 'options'], + 'empty-page' => ['menu_slug' => 'empty-page', 'page_title' => 'Empty', 'post_id' => 'options'], + ]; + } + + function acf_get_field_groups(array $filter): array + { + return $filter['options_page'] === 'site-settings' ? [['key' => 'group_contact']] : []; + } + + function acf_get_fields(array $group): array + { + return [ + ['name' => 'telephone', 'label' => 'Telephone', 'type' => 'text'], + ['name' => 'address', 'label' => 'Address', 'type' => 'textarea'], + ['name' => 'fax', 'label' => 'Fax', 'type' => 'text'], + ['name' => 'tagline', 'label' => 'Tagline', 'type' => 'text'], + ]; + } + + function get_field(string $name, mixed $postId): mixed + { + return [ + 'telephone' => ' +44 20 7946 0000 ', + 'address' => "1 High Street\nLondon", + 'fax' => '', + 'tagline' => "Two\nlines", + ][$name] ?? null; + } + PHP); + } + + public function test_describes_each_variable_the_renderer_understands(): void + { + $variables = $this->health()['variables']; + + foreach (['title', 'meta_description', 'permalink', 'excerpt', 'featured_image', 'date_published', 'date_modified', 'author_name'] as $name) { + $this->assertArrayHasKey($name, $variables); + $this->assertNotSame('', $variables[$name]); + } + } + + public function test_keeps_health_uncached(): void + { + $this->assertStringContainsString('no-store', $this->dispatch($this->signedRequest())->get_headers()['Cache-Control']); + } +} diff --git a/tests/HealthTest.php b/tests/HealthTest.php index 6cde556..7404f02 100644 --- a/tests/HealthTest.php +++ b/tests/HealthTest.php @@ -49,8 +49,9 @@ public function test_advertises_what_it_can_write_through_yoast(): void 'link.opportunity', 'site.robots_missing', 'sitemap.not_declared_in_robots', + 'structured_data.missing', ], $health['checks']); - $this->assertSame(['content.update', 'content.link'], $health['capabilities']); + $this->assertSame(['content.update', 'content.draft', 'content.link'], $health['capabilities']); $this->assertSame(['installed' => true, 'version' => WPSEO_VERSION], $health['integrations']['yoast']); } @@ -66,7 +67,7 @@ public function test_withholds_social_tags_when_yoast_is_set_not_to_print_them() $this->assertContains('title.too_long', $checks); } - public function test_withholds_what_yoast_would_write_without_a_supported_seo_plugin(): void + public function test_advertises_only_what_needs_no_seo_plugin_without_one(): void { $receiver = new AltDesign\Altimiser\Receiver(dirname(__DIR__).'/altimiser.php', null); @@ -79,7 +80,9 @@ public function test_withholds_what_yoast_would_write_without_a_supported_seo_pl 'link.opportunity', 'site.robots_missing', 'sitemap.not_declared_in_robots', + 'structured_data.missing', ], $health['checks']); + $this->assertSame(['content.update', 'content.draft', 'content.link'], $health['capabilities']); $this->assertSame(['installed' => false], $health['integrations']['yoast']); } diff --git a/tests/MarkdownBlocksTest.php b/tests/MarkdownBlocksTest.php new file mode 100644 index 0000000..4aa3d7f --- /dev/null +++ b/tests/MarkdownBlocksTest.php @@ -0,0 +1,113 @@ +convert($markdown); + } + + private function paragraph(string $html): string + { + return "\n

    {$html}

    \n"; + } + + public function test_wraps_paragraphs_in_blocks(): void + { + $this->assertSame( + $this->paragraph('First.')."\n\n".$this->paragraph("Second\nline."), + $this->convert("First.\n\nSecond\nline."), + ); + } + + public function test_writes_headings_with_their_level(): void + { + $this->assertSame( + "\n

    How long

    \n", + $this->convert('## How long'), + ); + $this->assertStringContainsString( + "\n

    Costs

    ", + $this->convert('### Costs'), + ); + } + + public function test_writes_bulleted_and_numbered_lists(): void + { + $bulleted = $this->convert("- one\n- two"); + $numbered = $this->convert("1. one\n2. two"); + + $this->assertStringContainsString('', $bulleted); + $this->assertStringContainsString("
      \n
    • one
    • ", $bulleted); + $this->assertStringContainsString('
    • two
    • ', $bulleted); + $this->assertStringContainsString('', $numbered); + $this->assertStringContainsString('
        ', $numbered); + } + + public function test_writes_quotes_around_paragraph_blocks(): void + { + $this->assertSame( + "\n
        ".$this->paragraph('Said once.')."
        \n", + $this->convert('> Said once.'), + ); + } + + public function test_writes_links_bold_italic_and_code(): void + { + $this->assertSame( + $this->paragraph('a link, italic, also, code.'), + $this->convert('[a **link**](https://example.com/a_b_c), *italic*, _also_, `code`.'), + ); + } + + public function test_keeps_every_gap_marker_as_written(): void + { + $markdown = "Most cost between [GAP: typical range].\n\n## How long\n\n[GAP: duration in_weeks_*]"; + + $converted = $this->convert($markdown); + + $this->assertStringContainsString('[GAP: typical range]', $converted); + $this->assertStringContainsString('[GAP: duration in_weeks_*]', $converted); + $this->assertSame(2, substr_count($converted, '[GAP:')); + } + + public function test_escapes_markup_in_the_text(): void + { + $converted = $this->convert(' & [x](javascript:alert(1))'); + + $this->assertStringNotContainsString('#s', get_echo('wp_head'), $match); + + $this->assertSame('applied', $applied['status']); + $this->assertSame('invalid_markup', $unknown['reason']); + $this->assertSame('Call "us" on \\ 020 7946 0000', json_decode($match[1], true)['telephone']); + } +} diff --git a/tests/StructuredDataTest.php b/tests/StructuredDataTest.php new file mode 100644 index 0000000..1528d1b --- /dev/null +++ b/tests/StructuredDataTest.php @@ -0,0 +1,416 @@ +set_permalink_structure('/%postname%/'); + + register_post_type('team', ['public' => true, 'label' => 'Team', 'rewrite' => ['slug' => 'team']]); + create_initial_taxonomies(); + flush_rewrite_rules(); + } + + public function tear_down(): void + { + _unregister_post_type('team'); + WPSEO_Options::clear_cache(); + + parent::tear_down(); + } + + /** + * @param array $overrides + */ + private function createPost(array $overrides = []): int + { + return self::factory()->post->create(wp_slash(['post_status' => 'publish', ...$overrides])); + } + + /** + * @return array + */ + private function applyBlock(int $postId, ?string $block = null, bool $dryRun = false): array + { + return $this->sendOne([ + 'check' => 'structured_data.missing', + 'url' => get_permalink($postId), + 'target' => ['selector' => null, 'attribute' => null], + 'current_value' => null, + 'suggested_value' => $block ?? $this->block, + ], $dryRun); + } + + /** + * @return array + */ + private function printedBlocks(int $postId): array + { + $this->go_to(get_permalink($postId)); + + $head = get_echo('wp_head'); + preg_match_all('##s', $head, $matches); + + return $matches[1]; + } + + /** + * @return array + */ + private function printed(int $postId): array + { + $blocks = $this->printedBlocks($postId); + + $this->assertCount(1, $blocks); + + return json_decode($blocks[0], true, flags: JSON_THROW_ON_ERROR); + } + + public function test_advertises_the_check_and_stores_a_page_block_on_the_page(): void + { + $pageId = $this->createPost(['post_type' => 'page', 'post_title' => 'About']); + + $result = $this->applyBlock($pageId); + + $this->assertSame('applied', $result['status']); + $this->assertSame($this->block, $result['to']); + $this->assertSame(['type' => 'post', 'id' => $pageId, 'collection' => 'page', 'field' => '_altimiser_structured_data'], $result['location']); + $this->assertSame($this->block, get_post_meta($pageId, '_altimiser_structured_data', true)); + $this->assertContains('structured_data.missing', $this->dispatch($this->signedRequest())->get_data()['checks']); + } + + public function test_keeps_a_page_block_per_page_and_not_as_a_default(): void + { + $first = $this->createPost(['post_type' => 'page']); + $second = $this->createPost(['post_type' => 'page']); + + $this->applyBlock($first); + $result = $this->applyBlock($second); + + $this->assertSame('applied', $result['status']); + $this->assertSame($this->block, get_post_meta($second, '_altimiser_structured_data', true)); + $this->assertFalse(get_option('altimiser_structured_data_defaults')); + } + + public function test_leaves_a_page_with_markup_of_its_own_alone(): void + { + $pageId = $this->createPost(['post_type' => 'page']); + update_post_meta($pageId, '_altimiser_structured_data', '{"@type": "WebPage"}'); + + $result = $this->applyBlock($pageId); + + $this->assertSame('skipped', $result['status']); + $this->assertSame('value_changed', $result['reason']); + $this->assertSame('{"@type": "WebPage"}', get_post_meta($pageId, '_altimiser_structured_data', true)); + } + + public function test_reports_a_page_already_holding_the_block_as_already_applied(): void + { + $pageId = $this->createPost(['post_type' => 'page']); + $this->applyBlock($pageId); + + $this->assertSame('already_applied', $this->applyBlock($pageId)['status']); + } + + public function test_stores_a_block_once_as_the_default_for_a_post_type(): void + { + $postId = $this->createPost(); + + $result = $this->applyBlock($postId); + + $this->assertSame('applied', $result['status']); + $this->assertSame(['type' => 'collection', 'collection' => 'post', 'field' => 'altimiser_structured_data_defaults'], $result['location']); + $this->assertSame(['post' => $this->block], get_option('altimiser_structured_data_defaults')); + $this->assertSame('', (string) get_post_meta($postId, '_altimiser_structured_data', true)); + } + + public function test_reports_later_posts_of_the_type_as_already_applied(): void + { + $this->applyBlock($this->createPost()); + + $result = $this->applyBlock($this->createPost()); + + $this->assertSame('already_applied', $result['status']); + $this->assertSame('collection', $result['location']['type']); + } + + public function test_leaves_an_existing_default_in_place(): void + { + $this->applyBlock($this->createPost()); + + $result = $this->applyBlock($this->createPost(), '{"@type": "NewsArticle", "headline": "{{ title }}"}'); + + $this->assertSame('skipped', $result['status']); + $this->assertSame('default_present', $result['reason']); + $this->assertSame(['post' => $this->block], get_option('altimiser_structured_data_defaults')); + } + + public function test_writes_nothing_on_a_dry_run_but_reports_what_would_happen(): void + { + $postId = $this->createPost(); + $pageId = $this->createPost(['post_type' => 'page']); + + $postResult = $this->applyBlock($postId, dryRun: true); + $pageResult = $this->applyBlock($pageId, dryRun: true); + + $this->assertSame('applied', $postResult['status']); + $this->assertSame('applied', $pageResult['status']); + $this->assertFalse(get_option('altimiser_structured_data_defaults')); + $this->assertSame('', (string) get_post_meta($pageId, '_altimiser_structured_data', true)); + } + + public function test_keeps_backslashes_in_the_stored_block(): void + { + $block = '{"@type": "Article", "headline": "{{ title }}", "alternativeHeadline": "Line\\\\nbreak \\"quoted\\""}'; + $pageId = $this->createPost(['post_type' => 'page']); + + $this->applyBlock($pageId, $block); + + $this->assertSame($block, get_post_meta($pageId, '_altimiser_structured_data', true)); + } + + public function test_skips_a_block_that_does_not_render_to_json_for_the_page(): void + { + $postId = $this->createPost(); + + $conditional = $this->applyBlock($postId, '{"headline": "{{ title }}"{{ if x }}, "a": 1{{ /if }}}'); + $unknown = $this->applyBlock($postId, '{"headline": "{{ alt_seo_meta_description }}"}'); + $broken = $this->applyBlock($postId, '{"headline": "{{ title }}"'); + + foreach ([$conditional, $unknown, $broken] as $result) { + $this->assertSame('skipped', $result['status']); + $this->assertSame('invalid_markup', $result['reason']); + } + + $this->assertFalse(get_option('altimiser_structured_data_defaults')); + } + + public function test_skips_a_change_with_no_markup_or_no_page(): void + { + $this->assertSame('no_suggestion', $this->sendOne(['check' => 'structured_data.missing', 'suggested_value' => null])['reason']); + $this->assertSame('not_found', $this->sendOne([ + 'check' => 'structured_data.missing', + 'url' => home_url('/nowhere/'), + 'suggested_value' => $this->block, + ])['reason']); + } + + public function test_skips_an_unpublished_page(): void + { + $draftId = $this->createPost(['post_status' => 'draft', 'post_name' => 'hidden']); + + $this->assertSame('not_found', $this->sendOne([ + 'check' => 'structured_data.missing', + 'url' => home_url('/hidden/'), + 'suggested_value' => $this->block, + ])['reason']); + $this->assertSame('', (string) get_post_meta($draftId, '_altimiser_structured_data', true)); + } + + public function test_renders_a_default_with_each_posts_own_values_including_quotes_and_backslashes(): void + { + $first = $this->createPost(['post_title' => 'He said "hello" \\ goodbye', 'post_name' => 'first']); + $second = $this->createPost(['post_title' => 'Plain title', 'post_name' => 'second']); + $this->applyBlock($first); + + $firstPrinted = $this->printed($first); + $secondPrinted = $this->printed($second); + + $this->assertSame('He said "hello" \\ goodbye', $firstPrinted['headline']); + $this->assertSame(get_permalink($first), $firstPrinted['url']); + $this->assertSame('Plain title', $secondPrinted['headline']); + $this->assertSame(get_permalink($second), $secondPrinted['url']); + $this->assertSame('Article', $secondPrinted['@type']); + } + + public function test_a_post_with_its_own_block_keeps_it_over_the_default(): void + { + $post = $this->createPost(['post_title' => 'Own']); + $other = $this->createPost(['post_title' => 'Other']); + $this->applyBlock($other); + update_post_meta($post, '_altimiser_structured_data', '{"@type": "NewsArticle", "headline": "{{ title }} (own)"}'); + + $printed = $this->printed($post); + + $this->assertSame('NewsArticle', $printed['@type']); + $this->assertSame('Own (own)', $printed['headline']); + } + + public function test_renders_the_date_author_and_site_variables(): void + { + $author = self::factory()->user->create(['display_name' => 'Rachel Adams']); + $postId = $this->createPost(['post_author' => $author, 'post_date' => '2026-03-04 09:30:00', 'post_title' => 'Dated']); + $this->applyBlock($postId, '{"datePublished": "{{ date_published }}", "dateModified": "{{ date_modified }}", "author": "{{ author_name }}", "image": "{{ featured_image }}", "site": "{{ site_name }} {{ site_url }}", "summary": "{{ excerpt }}"}'); + + $printed = $this->printed($postId); + + $this->assertSame(get_post_time('c', false, $postId), $printed['datePublished']); + $this->assertStringStartsWith('2026-03-04T09:30:00', $printed['datePublished']); + $this->assertSame(get_post_modified_time('c', false, $postId), $printed['dateModified']); + $this->assertSame('Rachel Adams', $printed['author']); + $this->assertSame('', $printed['image']); + $this->assertSame(get_bloginfo('name').' '.home_url(), $printed['site']); + } + + public function test_takes_the_meta_description_from_yoast_and_falls_back_to_the_excerpt(): void + { + $withYoast = $this->createPost(['post_excerpt' => 'The excerpt.']); + update_post_meta($withYoast, '_yoast_wpseo_metadesc', 'Written for search engines.'); + $without = $this->createPost(['post_excerpt' => 'The excerpt.']); + $this->applyBlock($withYoast); + + $this->assertSame('Written for search engines.', $this->printed($withYoast)['description']); + $this->assertSame('The excerpt.', $this->printed($without)['description']); + } + + public function test_cannot_close_the_script_tag_it_is_printed_in(): void + { + global $wpdb; + $author = self::factory()->user->create(); + $wpdb->update($wpdb->users, ['display_name' => ' \n

        {$html}

        \n"; } + public function test_takes_out_the_characters_it_uses_to_hold_markup_in_place(): void + { + $this->assertSame($this->paragraph('Costs 5 and link more.'), $this->convert("Costs \u{E000}5\u{E001} and [link](x) more.")); + } + public function test_wraps_paragraphs_in_blocks(): void { $this->assertSame( diff --git a/tests/StructuredDataTest.php b/tests/StructuredDataTest.php index 88d3e4c..39f339f 100644 --- a/tests/StructuredDataTest.php +++ b/tests/StructuredDataTest.php @@ -427,6 +427,16 @@ public function test_a_loop_picks_up_a_post_published_after_the_block_was_stored $this->assertSame($before + 1, count($this->printed($listPage)['items'])); } + public function test_a_loop_takes_the_first_fifty_posts_of_a_large_type(): void + { + $listPage = $this->createPost(['post_type' => 'page', 'post_name' => 'blog-index']); + self::factory()->post->create_many(55); + + $this->applyBlock($listPage, '{"items": [{{ collection:post }}"{{ title }}"{{ unless last }},{{ /unless }}{{ /collection:post }}]}'); + + $this->assertCount(50, $this->printed($listPage)['items']); + } + public function test_refuses_a_loop_inside_a_loop(): void { $listPage = $this->createPost(['post_type' => 'page', 'post_name' => 'nested']); From 0eb8f5f28ddacb3ee289c5ead12c1fe77f64c8aa Mon Sep 17 00:00:00 2001 From: Alt-Charlie Date: Mon, 9 Nov 2026 12:46:41 +0000 Subject: [PATCH 4/7] Explain why canonical.missing is not handled --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b477ce1..444a41a 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,8 @@ It reports what structured data and drafts can use as well: `collections`, the p These are written to the post, page, custom post type or taxonomy term the URL belongs to, as long as its type has public pages. A shared template is never edited and a meta title is never written to `post_title`. +`canonical.missing` is not handled. Yoast SEO prints a canonical on every page it lets search engines index, and Altimiser only checks those pages, so a missing canonical means a theme or another plugin has taken Yoast's away. Writing Yoast's canonical field would not bring it back, so that one needs a developer. + | Checks | Where it is written | | --- | --- | | `image.alt_missing`, `alt_empty`, `alt_too_long` | The image's alt text in the media library, and the page's own copy of it | From 26a6f0e5a1cb811725133392c47853a75a3b431a Mon Sep 17 00:00:00 2001 From: Alt-Charlie Date: Fri, 13 Nov 2026 15:51:27 +0000 Subject: [PATCH 5/7] Work out only the variables a structured data block names A loop asks for the variables of up to 50 posts on every page view the cache does not answer, and each post's excerpt, featured image, author and Yoast description were worked out whether the block used them or not. --- src/PostVariables.php | 25 ++++++++++++++----------- src/StructuredDataRenderer.php | 6 +++--- tests/StructuredDataTest.php | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/PostVariables.php b/src/PostVariables.php index bdcd771..ebf8f11 100644 --- a/src/PostVariables.php +++ b/src/PostVariables.php @@ -34,21 +34,24 @@ public function descriptions(): array } /** - * @return array + * Each value is worked out only when a block names it, because a loop asks this for + * up to 50 posts on every page view the cache does not answer. + * + * @return array */ public function for(WP_Post $post): array { return [ - 'title' => $this->plain($post->post_title), - 'meta_description' => $this->metaDescription($post), - 'permalink' => (string) get_permalink($post), - 'excerpt' => $this->excerpt($post), - 'featured_image' => (string) get_the_post_thumbnail_url($post, 'full'), - 'date_published' => (string) get_post_time('c', false, $post), - 'date_modified' => (string) get_post_modified_time('c', false, $post), - 'author_name' => (string) get_the_author_meta('display_name', (int) $post->post_author), - 'site_name' => get_bloginfo('name'), - 'site_url' => home_url(), + 'title' => fn (): string => $this->plain($post->post_title), + 'meta_description' => fn (): string => $this->metaDescription($post), + 'permalink' => static fn (): string => (string) get_permalink($post), + 'excerpt' => fn (): string => $this->excerpt($post), + 'featured_image' => static fn (): string => (string) get_the_post_thumbnail_url($post, 'full'), + 'date_published' => static fn (): string => (string) get_post_time('c', false, $post), + 'date_modified' => static fn (): string => (string) get_post_modified_time('c', false, $post), + 'author_name' => static fn (): string => (string) get_the_author_meta('display_name', (int) $post->post_author), + 'site_name' => static fn (): string => get_bloginfo('name'), + 'site_url' => static fn (): string => home_url(), ]; } diff --git a/src/StructuredDataRenderer.php b/src/StructuredDataRenderer.php index 800a540..054c074 100644 --- a/src/StructuredDataRenderer.php +++ b/src/StructuredDataRenderer.php @@ -51,7 +51,7 @@ public function render(string $template, WP_Post $post): ?string * A single pass, so a value that itself holds {{ title }} is printed as text * and not run again. * - * @param array $values + * @param array $values * @param ?array{count: int, last: bool} $position */ private function expand(string $template, array $values, ?array $position, bool &$failed): string @@ -110,7 +110,7 @@ private function loop(string $handle, string $body, bool &$failed): string } /** - * @param array $values + * @param array $values * @param ?array{count: int, last: bool} $position */ private function value(string $name, string $field, array $values, ?array $position, bool &$failed): string @@ -132,7 +132,7 @@ private function value(string $name, string $field, array $values, ?array $posit return ''; } - return $this->escape($values[$name]); + return $this->escape($values[$name]()); } private function escape(string $value): string diff --git a/tests/StructuredDataTest.php b/tests/StructuredDataTest.php index 39f339f..a820a2a 100644 --- a/tests/StructuredDataTest.php +++ b/tests/StructuredDataTest.php @@ -437,6 +437,26 @@ public function test_a_loop_takes_the_first_fifty_posts_of_a_large_type(): void $this->assertCount(50, $this->printed($listPage)['items']); } + public function test_works_out_only_the_variables_a_block_names(): void + { + $listPage = $this->createPost(['post_type' => 'page', 'post_name' => 'blog-index']); + self::factory()->post->create_many(5, ['post_status' => 'publish', 'post_excerpt' => '']); + $this->applyBlock($listPage, '{"items": [{{ collection:post }}"{{ title }}"{{ unless last }},{{ /unless }}{{ /collection:post }}]}'); + + $trimmed = 0; + $count = static function (string $text) use (&$trimmed): string { + $trimmed++; + + return $text; + }; + + add_filter('wp_trim_words', $count); + $this->printed($listPage); + remove_filter('wp_trim_words', $count); + + $this->assertSame(0, $trimmed); + } + public function test_refuses_a_loop_inside_a_loop(): void { $listPage = $this->createPost(['post_type' => 'page', 'post_name' => 'nested']); From f26eb767e9deefa3884d95f2a1dc2bb16a26f2db Mon Sep 17 00:00:00 2001 From: Alt-Charlie Date: Fri, 13 Nov 2026 15:51:48 +0000 Subject: [PATCH 6/7] Give a new draft to the first administrator, or the user a filter names A signed request has no user, so drafts were saved with no author: no byline once published, and an empty author_name in their structured data. --- README.md | 2 +- src/DraftPosts.php | 13 +++++++++++++ tests/DraftsTest.php | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 444a41a..1a9096b 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ Each request attempts up to 10 changes within 45 seconds and hands the rest back ## Drafts -Altimiser can send a new page to `POST /drafts`, and the plugin saves it as a draft for an editor to finish and publish. The Markdown body becomes block markup, with each `[GAP: ...]` left where it is for the editor to fill. The draft goes into the post type Altimiser names, or beside the page it expands, with a slug ending in `-draft`. The meta description goes to Yoast SEO's field where it is active. Sending the same idea again rewrites its draft while it is still a draft or pending review and nobody has changed its title or body. A draft an editor has worked on is refused as `edited`, and one that has been published as `already_published`. A signed request has no user, so the draft has no author: set one before publishing, or the byline and `{{ author_name }}` stay empty. Drafts need pretty permalinks, because post types are only offered as destinations with an address. +Altimiser can send a new page to `POST /drafts`, and the plugin saves it as a draft for an editor to finish and publish. The Markdown body becomes block markup, with each `[GAP: ...]` left where it is for the editor to fill. The draft goes into the post type Altimiser names, or beside the page it expands, with a slug ending in `-draft`. The meta description goes to Yoast SEO's field where it is active. A new draft belongs to the site's first administrator, or to whichever user ID the `altimiser_draft_author` filter returns. Sending the same idea again rewrites its draft while it is still a draft or pending review and nobody has changed its title or body. A draft an editor has worked on is refused as `edited`, and one that has been published as `already_published`. A signed request has no user, so the draft has no author: set one before publishing, or the byline and `{{ author_name }}` stay empty. Drafts need pretty permalinks, because post types are only offered as destinations with an address. ## Page caches diff --git a/src/DraftPosts.php b/src/DraftPosts.php index 52d1c26..b659958 100644 --- a/src/DraftPosts.php +++ b/src/DraftPosts.php @@ -124,6 +124,7 @@ private function create(DraftRequest $draft, string $postType, ?WP_Post $beside) return $this->save('wp_insert_post', [ 'post_type' => $postType, 'post_status' => 'draft', + 'post_author' => $this->author(), 'post_title' => $this->title($draft), 'post_content' => $this->markdown->convert($draft->body), 'post_name' => $beside === null ? sanitize_title($draft->title) : "{$beside->post_name}-draft", @@ -132,6 +133,18 @@ private function create(DraftRequest $draft, string $postType, ?WP_Post $beside) ]); } + /** + * A signed request has no user, and a draft owned by nobody has no byline once it is + * published, so it goes to the first administrator unless altimiser_draft_author + * names someone else. + */ + private function author(): int + { + $administrators = get_users(['role' => 'administrator', 'orderby' => 'ID', 'number' => 1, 'fields' => 'ID']); + + return (int) apply_filters('altimiser_draft_author', (int) ($administrators[0] ?? 0)); + } + private function rewrite(DraftRequest $draft, WP_Post $existing): int { return $this->save('wp_update_post', [ diff --git a/tests/DraftsTest.php b/tests/DraftsTest.php index 688ebb3..f427ab7 100644 --- a/tests/DraftsTest.php +++ b/tests/DraftsTest.php @@ -91,6 +91,25 @@ public function test_saves_the_draft_unpublished_in_the_named_post_type(): void $this->assertSame('Loft conversion costs', $post->post_title); } + public function test_gives_the_draft_to_the_first_administrator_unless_a_filter_names_someone(): void + { + $first = self::factory()->user->create(['role' => 'administrator']); + $editor = self::factory()->user->create(['role' => 'editor']); + $firstAdministrator = (int) get_users(['role' => 'administrator', 'orderby' => 'ID', 'number' => 1, 'fields' => 'ID'])[0]; + + $this->sendDraft(); + + $this->assertSame($firstAdministrator, (int) $this->postFor('idea-42')->post_author); + $this->assertLessThanOrEqual($first, $firstAdministrator); + + $chosen = static fn (): int => $editor; + add_filter('altimiser_draft_author', $chosen); + $this->sendDraft(['reference' => 'idea-43']); + remove_filter('altimiser_draft_author', $chosen); + + $this->assertSame($editor, (int) $this->postFor('idea-43')->post_author); + } + public function test_builds_the_edit_address_from_the_admin_address(): void { $response = $this->sendDraft(); From 7b43144029950f60f5955839fccab0fac1152667 Mon Sep 17 00:00:00 2001 From: Alt-Charlie Date: Fri, 13 Nov 2026 16:29:44 +0000 Subject: [PATCH 7/7] Leave ACF options fields named like credentials out of the globals sent to Altimiser --- README.md | 2 +- src/Globals.php | 19 +++++++++++++++++-- tests/HealthListsTest.php | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index eb688e9..6d14514 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Connect again after activating Yoast SEO, so Altimiser reads what the site can n `GET /health` also reports two settings under `setup`, which Altimiser turns into findings because they stop fixes from counting: **Discourage search engines** under **Settings > Reading**, and plain permalinks. -It reports what structured data and drafts can use as well: `collections`, the public post types with the address pattern of their posts, `globals`, the single line text fields on ACF PRO options pages, and `variables`, the names a structured data block can use. Under plain permalinks `collections` is empty. +It reports what structured data and drafts can use as well: `collections`, the public post types with the address pattern of their posts, `globals`, the single line text fields on ACF PRO options pages with their values, leaving out any field whose name or label has key, token, secret or password as a word in it, and `variables`, the names a structured data block can use. Under plain permalinks `collections` is empty. ## What gets changed diff --git a/src/Globals.php b/src/Globals.php index 1e2bff8..eb64f19 100644 --- a/src/Globals.php +++ b/src/Globals.php @@ -8,7 +8,9 @@ * * Only single line text, because a line break inside a JSON string breaks the whole * block. The values travel so Altimiser can tell which of them a page actually shows. - * Without ACF, or without options pages, there are none. + * A field named or labelled like a credential is left out, because some sites keep an + * API key on an options page and the values go on to Altimiser's prompts. Without ACF, + * or without options pages, there are none. */ class Globals { @@ -103,7 +105,7 @@ private function fields(string $optionsPage, mixed $postId): array */ private function singleLine(array $field, mixed $postId): ?string { - if (($field['type'] ?? null) !== 'text' || ! is_string($field['name'] ?? null) || $field['name'] === '') { + if (($field['type'] ?? null) !== 'text' || ! is_string($field['name'] ?? null) || $field['name'] === '' || $this->looksLikeACredential($field)) { return null; } @@ -115,4 +117,17 @@ private function singleLine(array $field, mixed $postId): ?string return trim($value); } + + /** + * Matched on whole words, splitting camel case too, so `apiKey` and `maps_api_key` + * are left out and `keywords` is not. + * + * @param array $field + */ + private function looksLikeACredential(array $field): bool + { + $words = preg_split('/[^a-z0-9]+/', strtolower((string) preg_replace('/([a-z0-9])([A-Z])/', '$1 $2', $field['name'].' '.($field['label'] ?? '')))); + + return array_intersect((array) $words, ['key', 'apikey', 'token', 'secret', 'password']) !== []; + } } diff --git a/tests/HealthListsTest.php b/tests/HealthListsTest.php index a7915f0..35edd96 100644 --- a/tests/HealthListsTest.php +++ b/tests/HealthListsTest.php @@ -111,6 +111,28 @@ public function test_lists_single_line_text_fields_from_acf_options_pages(): voi ], $this->health()['globals']); } + /** + * @runInSeparateProcess + * + * @preserveGlobalState disabled + */ + public function test_leaves_out_fields_named_like_credentials(): void + { + require __DIR__.'/fixtures/acf-pro-options-pages.php'; + + altimiser_test_site_settings([ + 'maps_api_key' => 'a-maps-key', + 'recaptchaSecret' => 'a-recaptcha-secret', + 'mailchimp_token' => 'a-mailchimp-token', + 'keywords' => 'housing repairs', + ]); + + $this->assertSame( + [['handle' => 'keywords', 'display' => 'Keywords', 'value' => 'housing repairs']], + $this->health()['globals'][0]['fields'], + ); + } + /** * @runInSeparateProcess *