diff --git a/README.md b/README.md index 6d8f13f..6d14514 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ Connects a WordPress site to Altimiser. It implements Altimiser's receiver protocol, which is kept with Altimiser itself, and it is held to that document rather than to the [Statamic receiver](https://github.com/alt-design/altimiser-statamic), with which it shares no code. -It applies the changes Altimiser sends to titles, meta descriptions and Open Graph text, by writing to Yoast SEO's own fields. It also writes image alt text, links a page to another it already names, and fixes robots.txt. Without Yoast SEO active a site can still connect and take those three, but `GET /health` advertises none of the Yoast checks, Altimiser reports the gap as `receiver.no_seo_plugin`, and no title, description or social text is changed. +It applies the changes Altimiser sends to titles, meta descriptions and Open Graph text, by writing to Yoast SEO's own fields. It also writes image alt text, links a page to another it already names, fixes robots.txt, adds structured data, and saves drafts of new pages. Without Yoast SEO active a site can still connect and take all of those, but `GET /health` advertises none of the Yoast checks, Altimiser reports the gap as `receiver.no_seo_plugin`, and no title, description or social text is changed. ## Requirements -WordPress 6.8 or newer, PHP 8.1 or newer, and Yoast SEO 24.0 or newer for titles, descriptions and Open Graph text. The tests run against Yoast SEO 24.0 as well as the latest release. Advanced Custom Fields is optional: where it is active, links also go into its editor fields. The plugin has no runtime Composer dependencies, because vendor classes in a plugin can collide with another plugin's copies. +WordPress 6.8 or newer, PHP 8.1 or newer, and Yoast SEO 24.0 or newer for titles, descriptions and Open Graph text. The tests run against Yoast SEO 24.0 as well as the latest release. Advanced Custom Fields is optional: where it is active, links also go into its editor fields, and structured data can use text fields from ACF PRO's options pages. The plugin has no runtime Composer dependencies, because vendor classes in a plugin can collide with another plugin's copies. ## Installing @@ -51,6 +51,8 @@ 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 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 | Checks | Yoast SEO field | @@ -61,12 +63,15 @@ Connect again after activating Yoast SEO, so Altimiser reads what the site can n 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 | | `link.opportunity` | The first mention in the page's content, or in an ACF editor field | | `site.robots_missing` | A new `robots.txt` at the root of the site | | `sitemap.not_declared_in_robots` | The `robots.txt` file, or what WordPress prints at `/robots.txt` when there is no file | +| `structured_data.missing` | A JSON-LD block the plugin prints in the page's head, kept once for a whole post type or on the page itself | ### Alt text @@ -84,6 +89,12 @@ A revert takes the link off again and restores the words as they were. A missing `robots.txt` is created with no `Disallow` rules and the site's sitemap, Yoast SEO's where it makes one. A missing sitemap line is added to the file where there is one, and otherwise to what WordPress prints, through the `robots_txt` filter, which keeps the rules WordPress and other plugins print there. Neither is reverted. A site not at the root of its domain has no `robots.txt` of its own, so both are refused there. On a multisite network every site is served from one folder, so a file there would answer for all of them: no file is created or edited, and a sitemap goes only into what WordPress prints for that site. A site set to discourage search engines is not given a file that lets them in. The sitemap address has to be a single http or https address on the site's own host. +### Structured data + +The block arrives as a JSON template and is printed in the head of single posts and pages, beside Yoast SEO's own graph. It can use `{{ title }}`, `{{ meta_description }}`, `{{ permalink }}`, `{{ excerpt }}`, `{{ featured_image }}`, `{{ date_published }}`, `{{ date_modified }}`, `{{ author_name }}`, `{{ site_name }}` and `{{ site_url }}`, a text field from an ACF PRO options page as `{{ options-page:field }}`, and one loop over a post type, `{{ collection:handle }}` to `{{ /collection:handle }}`, with `{{ count }}` and `{{ unless last }}` inside it. A loop takes the first 50 posts, by menu order and then newest first, because the block is rendered on every uncached view. Values are escaped for JSON when printed, and a password protected post gives no excerpt or description, since the block is printed for everyone. A post hidden by a membership plugin is still published as far as WordPress is concerned, so on a membership site leave `{{ excerpt }}` and `{{ meta_description }}` out of blocks for members' content. + +A block for any post type but pages is kept once, as that type's default, and every post of the type uses it unless it has its own. Later posts of the type are reported as already done, and an existing default is left in place as `default_present`. Pages keep a block each. The `altimiser_structured_data_default_exclusions` filter lists the post types that keep a block per post. A block that does not render to valid JSON for the page prints nothing and is refused as `invalid_markup`. + `canonical.missing` is left to a developer. Yoast SEO prints a canonical itself except on noindex pages, so a missing one points to something else on the site. A change is refused rather than guessed at, and the finding goes to a person: @@ -101,6 +112,8 @@ A change is refused rather than guessed at, and the finding goes to a person: | `mention_not_found` | The words to link are not in the page's content, usually because the theme prints them | | `page_builder` | The page is built with Elementor, Beaver Builder, Divi, WPBakery, Avada, Oxygen, Bricks or SiteOrigin, or its content holds another builder's shortcodes. These keep the page in their own data or in shortcode settings, where an edit would be lost or break the layout. Alt text still goes into the media library, which they print | | `invalid_change` | A link change arrived without the words to link or where to link them, or pointing off the site, or a sitemap address that is not on the site | +| `invalid_markup` | A structured data block does not render to valid JSON for the page, usually because it names a variable or global the site does not have | +| `default_present` | The post type already has a structured data default, which was left in place | If Yoast SEO is not active when one of its changes arrives, nothing is attempted and the change comes back as `failed` with the reason `seo_plugin_inactive`. That keeps its approval, so it goes out again on the next apply. @@ -108,9 +121,13 @@ A page that inherited from a template goes back to inheriting when the change is Each request attempts up to 10 changes within 45 seconds and hands the rest back as `deferred`, for Altimiser to send again. The `altimiser_batch_size` and `altimiser_time_budget` filters change those numbers. The time budget is also held to half of `max_execution_time`. +## 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. 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 -A request that applies something, or finds it already in place, purges the pages it changed, once, during the request, after Yoast has rebuilt what the page prints. Alt text written to the media library shows wherever the image does, so that clears the whole cache instead. A dry run never purges anything. +A request that applies something, or finds it already in place, purges the pages it changed, once, during the request, after Yoast has rebuilt what the page prints. Storing a post type's structured data default, or alt text in the media library, purges the whole cache instead, since every page of that type or showing that image changes. A dry run never purges anything. | Plugin | What is purged | How it was checked | | --- | --- | --- | @@ -173,7 +190,9 @@ To publish a release, change `Version` in `altimiser.php` on `main`, then push a ## Uninstalling -Deleting the plugin removes its settings, the counts it keeps and every editor's connection, on every site of a network. Changes it applied are the site's content by then, and stay, except a sitemap declared in the `robots.txt` WordPress prints, which only the plugin printed. +Deleting the plugin removes its settings, the counts it keeps and every editor's connection, on every site of a network. Changes it applied are the site's content by then, and stay, except a sitemap declared in the `robots.txt` WordPress prints and stored structured data, which only the plugin printed. Drafts stay, and keep the reference that ties them to Altimiser. + +Deactivating the plugin stops it printing structured data and the sitemap line in WordPress's `robots.txt` as well, though both come back on reactivation. Until then Altimiser's next scan finds them missing again. ## When the connection fails diff --git a/altimiser.php b/altimiser.php index 14eb11b..a675d52 100644 --- a/altimiser.php +++ b/altimiser.php @@ -56,3 +56,4 @@ }); (new Updater(__FILE__))->register(); +(new AltDesign\Altimiser\StructuredDataPrinter())->register(); diff --git a/src/ChangeDispatcher.php b/src/ChangeDispatcher.php index 7f763df..849779f 100644 --- a/src/ChangeDispatcher.php +++ b/src/ChangeDispatcher.php @@ -57,9 +57,9 @@ public function dispatch(array $changes, bool $dryRun): array } /** - * A change stored as a default shows on every page of its kind, and alt text in the - * media library on every page that prints the image, so either clears the whole - * cache. Anything else clears the page it was made for. + * A change stored as a collection's default shows on every page of that type, and + * alt text in the media library on every page that prints the image, so either + * clears the whole cache. Anything else clears the page it was made for. * * @param array}> $landed */ @@ -70,7 +70,7 @@ private function purge(array $landed): void } foreach ($landed as [, $location]) { - if (($location['default'] ?? false) === true || ($location['type'] ?? null) === 'asset') { + if (in_array($location['type'] ?? null, ['collection', 'asset'], true)) { $this->pageCache->purgeEverything(); return; 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..b659958 --- /dev/null +++ b/src/DraftPosts.php @@ -0,0 +1,208 @@ + + * + * @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 ($existing !== null && $this->editedSinceWritten($existing)) { + return $this->refused('edited', "Somebody has edited the draft \"{$existing->post_title}\" since it was written, so it was not rewritten over their work."); + } + + 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); + + update_post_meta($id, $this->writtenKey, $this->fingerprint($id)); + $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 editedSinceWritten(WP_Post $post): bool + { + $written = (string) get_post_meta($post->ID, $this->writtenKey, true); + + return $written !== '' && $written !== $this->fingerprint($post->ID); + } + + /** + * Taken from the post as saved, so filters that change content on save do not make + * every draft look edited. + */ + private function fingerprint(int $postId): string + { + $post = get_post($postId); + + return $post instanceof WP_Post ? md5("{$post->post_title}\n{$post->post_content}") : ''; + } + + 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_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", + 'post_parent' => $beside?->post_parent ?? 0, + 'meta_input' => [$this->referenceKey => $draft->reference], + ]); + } + + /** + * 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', [ + '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..eb64f19 --- /dev/null +++ b/src/Globals.php @@ -0,0 +1,133 @@ +}> + */ + 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'] === '' || $this->looksLikeACredential($field)) { + 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); + } + + /** + * 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/src/MarkdownBlocks.php b/src/MarkdownBlocks.php new file mode 100644 index 0000000..6ac2655 --- /dev/null +++ b/src/MarkdownBlocks.php @@ -0,0 +1,222 @@ +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 = []; + } + + /** + * Stashed markup is held in place by private use characters, so any in the text + * itself are taken out first. + */ + private function inline(string $text): string + { + $stash = []; + $text = str_replace(["\u{E000}", "\u{E001}"], '', $text); + + $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..ebf8f11 --- /dev/null +++ b/src/PostVariables.php @@ -0,0 +1,94 @@ + + */ + 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", + ]; + } + + /** + * 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' => 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(), + ]; + } + + 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)); + } + + /** + * Empty for a password protected post, whose text is only for those given the + * password, while structured data is printed for everyone. + */ + private function excerpt(WP_Post $post): string + { + if ($post->post_password !== '') { + return ''; + } + + $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..212301b 100644 --- a/src/Receiver.php +++ b/src/Receiver.php @@ -2,6 +2,7 @@ namespace AltDesign\Altimiser; +use InvalidArgumentException; use Throwable; use WP_Error; use WP_REST_Request; @@ -27,6 +28,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 +105,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 +142,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 +213,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 (Throwable $exception) { + return new WP_Error('altimiser_write_failed', $exception->getMessage(), ['status' => 500]); + } + } + /** * @return array */ @@ -191,12 +241,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..054c074 --- /dev/null +++ b/src/StructuredDataRenderer.php @@ -0,0 +1,144 @@ +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); + } + + /** + * The block is rendered on every uncached view of the page, so a loop takes the + * first 50 posts rather than every one a large post type holds. + */ + 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' => 50, + 'orderby' => ['menu_order' => 'ASC', 'date' => 'DESC'], + 'no_found_rows' => true, + 'update_post_term_cache' => false, + ]); + + $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..f427ab7 --- /dev/null +++ b/tests/DraftsTest.php @@ -0,0 +1,355 @@ +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_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(); + $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_draft_an_editor_has_worked_on_alone_and_says_so(): void + { + $id = (int) $this->sendDraft()->get_data()['id']; + wp_update_post(['ID' => $id, 'post_content' => 'Most cost between 30,000 and 60,000 pounds.']); + + $response = $this->sendDraft(['body' => 'Overwritten body.']); + + $this->assertSame('refused', $response->get_data()['status']); + $this->assertSame('edited', $response->get_data()['reason']); + $this->assertSame('Most cost between 30,000 and 60,000 pounds.', get_post($id)->post_content); + } + + 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..35edd96 --- /dev/null +++ b/tests/HealthListsTest.php @@ -0,0 +1,174 @@ +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_without_acf_options_pages(): 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 + { + require __DIR__.'/fixtures/acf-pro-options-pages.php'; + + altimiser_test_site_settings([ + 'telephone' => ' +44 20 7946 0000 ', + 'address' => "1 High Street\nLondon", + 'fax' => '', + 'tagline' => "Two\nlines", + ]); + + $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_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 + * + * @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.'); + } + + $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); + } + + + 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..4a07d50 --- /dev/null +++ b/tests/MarkdownBlocksTest.php @@ -0,0 +1,118 @@ +convert($markdown); + } + + private function paragraph(string $html): string + { + return "\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( + $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..a820a2a --- /dev/null +++ b/tests/StructuredDataTest.php @@ -0,0 +1,469 @@ +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_clears_the_whole_page_cache_for_a_default_shared_by_every_post_of_a_type(): void + { + $purged = []; + $record = static function (array $urls, bool $everything) use (&$purged): void { + $purged[] = $everything; + }; + $postId = $this->createPost(); + + add_action('altimiser_purge_urls', $record, 10, 2); + $this->applyBlock($postId); + remove_action('altimiser_purge_urls', $record); + + $this->assertSame([true], $purged); + } + + 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_keeps_the_text_of_a_password_protected_post_out_of_its_markup(): void + { + $postId = $this->createPost(['post_password' => 'members', 'post_excerpt' => 'Only for members.', 'post_content' => 'The terms we agreed.']); + $this->applyBlock($postId, '{"description": "{{ meta_description }}", "excerpt": "{{ excerpt }}"}'); + + $this->assertSame(['description' => '', 'excerpt' => ''], $this->printed($postId)); + } + + 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' => '