PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.7.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.7.0
2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 All 48 releases
thinkrank / includes / abilities / settings / class-settings-key-map.php

class-settings-key-map.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 2.7.0, at includes/abilities/settings/class-settings-key-map.php

460 lines 21.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shared setting-key declarations for the settings abilities.
4 *
5 * @package ThinkRank\Abilities\Settings
6 * @since 2.1.1
7 */
8
9 declare(strict_types=1);
10
11 namespace ThinkRank\Abilities\Settings;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * The keys each settings category exposes over the abilities layer, and their
19 * JSON types.
20 *
21 * Declared once and shared by the get/update pair for each category, because
22 * two hand-maintained allowlists per category is how the gap opened in the
23 * first place: site identity exposed 13 of its 54 keys and sitemap 8 of its 20,
24 * so an MCP agent could neither read nor set any title template, the whole
25 * business/Local SEO block, or the sitemap index toggle — and, since the read
26 * ability omitted them silently, had no way to tell its picture was incomplete
27 * (#518).
28 *
29 * AbilitySettingsCoverageTest walks each manager's get_known_setting_keys()
30 * against these maps and fails when a category gains a key that is neither
31 * exposed here nor listed as deliberately excluded, so the drift cannot recur
32 * unnoticed.
33 *
34 * @since 2.1.1
35 */
36 final class Settings_Key_Map {
37
38 /**
39 * `site_identity` keys that belong to another ability.
40 *
41 * Not gaps: each is already reachable, and exposing it twice would give an
42 * agent two ways to write the same row.
43 *
44 * @var array<string,string> Key => the ability that owns it.
45 */
46 public const SITE_IDENTITY_ELSEWHERE = [
47 'robots_txt_content' => 'thinkrank/update-robots-txt',
48 'custom_robots_rules' => 'thinkrank/update-robots-txt',
49 'knowledge_graph' => 'thinkrank/update-schema-settings',
50 'organization_schema' => 'thinkrank/update-schema-settings',
51 'post_title' => 'thinkrank/update-global-settings',
52 'page_title' => 'thinkrank/update-global-settings',
53 ];
54
55 /**
56 * `sitemap` keys that are derived state rather than settings.
57 *
58 * @var array<string,string> Key => why it is not exposed.
59 */
60 public const SITEMAP_ELSEWHERE = [
61 'last_generated' => 'written by the generator, not a setting',
62 'sitemap_urls' => 'written by the generator, not a setting',
63 'selected_preset' => 'UI marker the settings screen maintains for itself',
64 ];
65
66 /**
67 * JSON schema properties for every exposed `site_identity` key.
68 *
69 * @return array<string, array<string, mixed>>
70 */
71 public static function site_identity(): array {
72 return [
73 // Core.
74 'enabled' => self::boolean( __( 'Whether site identity management is active.', 'thinkrank' ) ),
75 'site_name' => self::string( __( 'Official name of the site, used in titles and schema.', 'thinkrank' ) ),
76 'site_description' => self::string( __( 'Short description of the site.', 'thinkrank' ) ),
77 'tagline' => self::string( __( 'Site tagline.', 'thinkrank' ) ),
78 'alternate_name' => self::alternate_name(),
79 'identity_type' => self::string( __( 'What the site is, e.g. "blog", "business", "portfolio".', 'thinkrank' ) ),
80 'represents' => self::string( __( 'Whether the site represents a "person" or an "organization".', 'thinkrank' ) ),
81 'default_meta_description' => self::string( __( 'Meta description used where no more specific one is set.', 'thinkrank' ) ),
82 'default_social_image' => self::string( __( 'URL of the fallback social sharing image.', 'thinkrank' ) ),
83 'social_media_accounts' => [
84 'type' => 'array',
85 'description' => __( 'Profile URLs for the site\'s social accounts.', 'thinkrank' ),
86 'items' => [ 'type' => 'string' ],
87 ],
88
89 // Titles. These are %token% templates; the manager sanitizes them
90 // without stripping their tokens.
91 'title_template' => self::string( __( 'Named title layout, e.g. "default", "reverse", "category".', 'thinkrank' ) ),
92 'title_separator' => self::string( __( 'Separator between title parts, e.g. "pipe", "dash".', 'thinkrank' ) ),
93 'homepage_title' => self::template( __( 'the homepage', 'thinkrank' ), '' ),
94 'category_title' => self::template( __( 'category archives', 'thinkrank' ), '%category_title%, %category%' ),
95 'tag_title' => self::template( __( 'tag archives', 'thinkrank' ), '%tag_title%' ),
96 'author_title' => self::template( __( 'author archives', 'thinkrank' ), '%author_name%' ),
97 'search_title' => self::template( __( 'search results pages', 'thinkrank' ), '%search_term%' ),
98 'archive_title' => self::template( __( 'date and other archives', 'thinkrank' ), '%archive_title%' ),
99
100 // Breadcrumbs.
101 'breadcrumbs_enabled' => self::boolean( __( 'Whether breadcrumb markup is generated.', 'thinkrank' ) ),
102 'breadcrumb_type' => self::string( __( 'Breadcrumb structure, e.g. "hierarchical".', 'thinkrank' ) ),
103 'breadcrumb_home_text' => self::string( __( 'Label for the home link in breadcrumbs.', 'thinkrank' ) ),
104 'breadcrumb_separator' => self::string( __( 'Separator drawn between breadcrumb items.', 'thinkrank' ) ),
105 'breadcrumb_prefix' => self::string( __( 'Text shown before the breadcrumb trail.', 'thinkrank' ) ),
106 'show_current_page' => self::boolean( __( 'Whether the current page appears in its own breadcrumb trail.', 'thinkrank' ) ),
107 'breadcrumb_use_seo_title' => self::boolean( __( 'Whether breadcrumb labels use the SEO title of a post or term when one is set, instead of its raw title.', 'thinkrank' ) ),
108
109 // Brand imagery.
110 'logo_url' => self::string( __( 'URL of the site logo.', 'thinkrank' ) ),
111 'favicon_url' => self::string( __( 'URL of the site favicon.', 'thinkrank' ) ),
112 'apple_touch_icon_url' => self::string( __( 'URL of the Apple touch icon.', 'thinkrank' ) ),
113
114 // Homepage hero.
115 'hero_title' => self::string( __( 'Homepage hero heading.', 'thinkrank' ) ),
116 'hero_subtitle' => self::string( __( 'Homepage hero subheading.', 'thinkrank' ) ),
117 'hero_cta_text' => self::string( __( 'Label on the homepage hero call to action.', 'thinkrank' ) ),
118 'hero_cta_url' => self::string( __( 'URL the homepage hero call to action points at.', 'thinkrank' ) ),
119 'hero_background_image' => self::string( __( 'URL of the homepage hero background image.', 'thinkrank' ) ),
120
121 // Business / Local SEO. Feeds LocalBusiness schema.
122 'local_seo_enabled' => self::boolean( __( 'Whether Local SEO output and LocalBusiness schema are enabled.', 'thinkrank' ) ),
123 'business_name' => self::string( __( 'Registered business name.', 'thinkrank' ) ),
124 'business_type' => self::string( __( 'schema.org business type, e.g. "Restaurant", "Store".', 'thinkrank' ) ),
125 'business_email' => self::string( __( 'Public contact email address.', 'thinkrank' ) ),
126 'business_phone' => self::string( __( 'Public contact telephone number.', 'thinkrank' ) ),
127 'business_address' => self::string( __( 'Street address.', 'thinkrank' ) ),
128 'business_city' => self::string( __( 'City or locality.', 'thinkrank' ) ),
129 'business_state' => self::string( __( 'State, province or region.', 'thinkrank' ) ),
130 'business_country' => self::string( __( 'Country.', 'thinkrank' ) ),
131 'business_postal_code' => self::string( __( 'Postal or ZIP code.', 'thinkrank' ) ),
132 'business_latitude' => self::string( __( 'Latitude of the business location, in decimal degrees.', 'thinkrank' ) ),
133 'business_longitude' => self::string( __( 'Longitude of the business location, in decimal degrees.', 'thinkrank' ) ),
134 'business_price_range' => self::string( __( 'Price range indicator, e.g. "$$".', 'thinkrank' ) ),
135 'business_hours' => self::business_hours(),
136
137 // Indexing.
138 'allow_search_engines' => self::boolean( __( 'Whether search engines are allowed to index the site.', 'thinkrank' ) ),
139 'query_protection' => self::boolean( __( 'Whether a URL whose content selector resolved to nothing (for example ?post_type=nosuchtype) answers 404 instead of serving the blog listing at 200.', 'thinkrank' ) ),
140 'canonical_scheme' => [
141 'type' => 'string',
142 'description' => __( 'Scheme for canonical URLs, og:url, schema @ids and sitemap entries. "automatic" follows WordPress; the other two override it, for a site behind a proxy that terminates TLS and leaves WordPress reporting the wrong scheme.', 'thinkrank' ),
143 'enum' => \ThinkRank\SEO\Url_Scheme::MODES,
144 ],
145 'robots_txt_enabled' => self::boolean( __( 'Whether ThinkRank manages robots.txt. Its contents are set with thinkrank/update-robots-txt.', 'thinkrank' ) ),
146
147 // RSS feeds.
148 'feed_excerpt_only' => self::boolean( __( 'Whether feed entries are shortened to an excerpt instead of carrying the full post.', 'thinkrank' ) ),
149 'feed_source_link' => self::boolean( __( 'Whether each feed entry is signed with a link back to the original post and the site.', 'thinkrank' ) ),
150 'feed_noindex' => self::boolean( __( 'Whether feeds are served with X-Robots-Tag: noindex. Leave off for a podcast feed, which needs to be indexable.', 'thinkrank' ) ),
151 'ai_crawler_rules' => self::ai_crawler_rules(),
152 ];
153 }
154
155 /**
156 * JSON schema properties for every exposed `sitemap` key.
157 *
158 * @return array<string, array<string, mixed>>
159 */
160 public static function sitemap(): array {
161 return [
162 'enabled' => self::boolean( __( 'Whether XML sitemap generation is active.', 'thinkrank' ) ),
163 'auto_generate' => self::boolean( __( 'Whether the sitemap regenerates automatically when content changes.', 'thinkrank' ) ),
164 'use_sitemap_index' => self::boolean( __( 'Whether to publish a sitemap index that links per-type sitemaps, rather than one flat file.', 'thinkrank' ) ),
165 'links_per_sitemap' => [
166 'type' => 'integer',
167 'description' => __( 'Maximum URLs per sitemap file before it is split.', 'thinkrank' ),
168 'minimum' => 1,
169 'maximum' => 50000,
170 ],
171 'custom_url_pattern' => self::string( __( 'Filename pattern for generated sitemaps, e.g. "sitemap-{type}.xml".', 'thinkrank' ) ),
172 'enable_styling' => self::boolean( __( 'Whether an XSL stylesheet is attached so the sitemap is readable in a browser.', 'thinkrank' ) ),
173 'ping_search_engines' => self::boolean( __( 'Whether search engines are notified after the sitemap is regenerated.', 'thinkrank' ) ),
174
175 // How the styled sitemap looks. All four need enable_styling on.
176 'styling_logo' => self::boolean( __( 'Whether a logo is shown above the sitemap heading. Requires enable_styling.', 'thinkrank' ) ),
177 'styling_logo_url' => self::string( __( 'URL of the sitemap logo image. Empty falls back to the site icon.', 'thinkrank' ) ),
178 'styling_color_main' => self::string( __( 'Hex colour ("#rrggbb") for the sitemap header, links and table head. Empty keeps the stock palette.', 'thinkrank' ) ),
179 'styling_color_accent' => self::string( __( 'Hex colour ("#rrggbb") for the header gradient end and link hovers. Empty keeps the stock palette.', 'thinkrank' ) ),
180
181 // What goes in.
182 'include_posts' => self::boolean( __( 'Whether posts are included.', 'thinkrank' ) ),
183 'include_pages' => self::boolean( __( 'Whether pages are included.', 'thinkrank' ) ),
184 'include_categories' => self::boolean( __( 'Whether category archives are included.', 'thinkrank' ) ),
185 'include_tags' => self::boolean( __( 'Whether tag archives are included.', 'thinkrank' ) ),
186 'include_images' => self::boolean( __( 'Whether image entries are included for each URL.', 'thinkrank' ) ),
187 'include_featured_images' => self::boolean( __( 'Whether featured images are included as image entries.', 'thinkrank' ) ),
188
189 // What stays out.
190 'exclude_posts' => self::string( __( 'Comma-separated post IDs to leave out.', 'thinkrank' ) ),
191 'exclude_terms' => self::string( __( 'Comma-separated term IDs to leave out.', 'thinkrank' ) ),
192 'exclude_private_posts' => self::boolean( __( 'Whether privately published posts are left out.', 'thinkrank' ) ),
193 'exclude_password_protected' => self::boolean( __( 'Whether password-protected posts are left out.', 'thinkrank' ) ),
194 ];
195 }
196
197 /**
198 * Read the exposed keys out of a manager's stored settings.
199 *
200 * The store keeps everything as strings ("1"/""), so each value is coerced
201 * back to the type the schema advertises before it reaches an agent.
202 *
203 * @param array<string, array<string, mixed>> $properties Schema properties for the category.
204 * @param array<string, mixed> $stored Settings as the manager returns them.
205 * @return array<string, mixed> Exposed settings, one entry per declared key.
206 */
207 public static function read( array $properties, array $stored ): array {
208 $out = [];
209
210 foreach ( $properties as $key => $property ) {
211 $value = $stored[ $key ] ?? null;
212
213 $type = $property['type'] ?? 'string';
214
215 // A union type ('string' or a list of them) keeps whichever shape
216 // it arrived in: casting to string would flatten a list, and
217 // casting to array would replace a name with [] (#692).
218 if ( is_array( $type ) ) {
219 $out[ $key ] = is_array( $value )
220 ? array_values( array_map( 'strval', $value ) )
221 : (string) ( $value ?? '' );
222 continue;
223 }
224
225 switch ( $type ) {
226 case 'boolean':
227 $out[ $key ] = (bool) $value;
228 break;
229 case 'integer':
230 $out[ $key ] = (int) $value;
231 break;
232 case 'array':
233 case 'object':
234 $out[ $key ] = is_array( $value ) ? $value : [];
235 break;
236 default:
237 $out[ $key ] = (string) ( $value ?? '' );
238 }
239 }
240
241 return $out;
242 }
243
244 /**
245 * Coerce an incoming patch to the declared types, dropping unknown keys.
246 *
247 * Strings are passed through rather than sanitized here: the manager's own
248 * save path already picks the right sanitizer per key, and running
249 * sanitize_text_field() first would strip %date% and %category% out of the
250 * title templates as percent-encoding before it ever got the chance (#521).
251 *
252 * @param array<string, array<string, mixed>> $properties Schema properties for the category.
253 * @param array<string, mixed> $incoming Caller-supplied settings.
254 * @return array<string, mixed> Recognized keys only, coerced to type.
255 */
256 public static function coerce( array $properties, array $incoming ): array {
257 $out = [];
258
259 foreach ( $properties as $key => $property ) {
260 if ( ! array_key_exists( $key, $incoming ) ) {
261 continue;
262 }
263
264 $value = $incoming[ $key ];
265
266 $type = $property['type'] ?? 'string';
267
268 // A union type ('string' or a list of them) keeps whichever shape
269 // it arrived in: casting to string would flatten a list, and
270 // casting to array would replace a name with [] (#692).
271 if ( is_array( $type ) ) {
272 $out[ $key ] = is_array( $value )
273 ? array_values( array_map( 'strval', $value ) )
274 : (string) ( $value ?? '' );
275 continue;
276 }
277
278 switch ( $type ) {
279 case 'boolean':
280 $out[ $key ] = (bool) $value;
281 break;
282 case 'integer':
283 $number = (int) $value;
284 if ( isset( $property['minimum'] ) ) {
285 $number = max( (int) $property['minimum'], $number );
286 }
287 if ( isset( $property['maximum'] ) ) {
288 $number = min( (int) $property['maximum'], $number );
289 }
290 $out[ $key ] = $number;
291 break;
292 case 'array':
293 case 'object':
294 if ( ! is_array( $value ) ) {
295 // A structured key given a scalar is a caller error, not
296 // something to flatten into a string and store.
297 continue 2;
298 }
299 $out[ $key ] = $value;
300 break;
301 default:
302 if ( is_array( $value ) ) {
303 continue 2;
304 }
305 $out[ $key ] = (string) $value;
306 }
307 }
308
309 return $out;
310 }
311
312 /**
313 * The opening hours object, one entry per day.
314 *
315 * A real sub-schema rather than string coercion: the value is a map of day
316 * name to {open, close, closed}, and flattening it to a string would make
317 * it unwritable over MCP.
318 *
319 * @return array<string, mixed>
320 */
321 private static function business_hours(): array {
322 $day = [
323 'type' => 'object',
324 'additionalProperties' => false,
325 'properties' => [
326 'open' => [
327 'type' => 'string',
328 'description' => __( 'Opening time as 24-hour HH:MM, or "" when closed.', 'thinkrank' ),
329 ],
330 'close' => [
331 'type' => 'string',
332 'description' => __( 'Closing time as 24-hour HH:MM, or "" when closed.', 'thinkrank' ),
333 ],
334 'closed' => [
335 'type' => 'boolean',
336 'description' => __( 'Whether the business is closed all day.', 'thinkrank' ),
337 ],
338 ],
339 ];
340
341 $properties = [];
342 foreach ( [ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' ] as $name ) {
343 $properties[ $name ] = $day;
344 }
345
346 return [
347 'type' => 'object',
348 'description' => __( 'Opening hours per day of the week.', 'thinkrank' ),
349 'additionalProperties' => false,
350 'properties' => $properties,
351 ];
352 }
353
354 /**
355 * The per-agent AI crawler allow/block map.
356 *
357 * Enumerating the known slugs rather than accepting a free-form object is
358 * what makes this usable by an agent: the alternative is a caller guessing
359 * `chatgpt` for a crawler registered as `chatgpt-user`, having the key
360 * dropped as unknown, and being told the save succeeded — because it did,
361 * with nothing in it.
362 *
363 * @return array<string, mixed>
364 */
365 private static function ai_crawler_rules(): array {
366 $properties = [];
367
368 foreach ( \ThinkRank\SEO\AI_Crawlers::for_display() as $agent ) {
369 $properties[ $agent['slug'] ] = [
370 'type' => 'string',
371 'enum' => [ 'allow', 'block' ],
372 /* translators: 1: crawler user-agent token, e.g. GPTBot. 2: what that crawler is for. */
373 'description' => sprintf( __( '%1$s — %2$s', 'thinkrank' ), $agent['token'], $agent['purpose'] ),
374 ];
375 }
376
377 return [
378 'type' => 'object',
379 'description' => __( 'Per-agent AI crawler rules. "block" writes a User-agent/Disallow pair for that crawler into robots.txt; "allow" (the default for any crawler not listed) writes nothing. Blocking Google-Extended does not affect normal Google Search indexing.', 'thinkrank' ),
380 'additionalProperties' => false,
381 'properties' => $properties,
382 ];
383 }
384
385 /**
386 * A boolean property.
387 *
388 * @param string $description Translated text describing what the toggle does.
389 * @return array<string, mixed>
390 */
391 /**
392 * The site's alternate name, as one value or several.
393 *
394 * schema.org and Google both allow `alternateName` to carry a list, and the
395 * store already round-trips either shape, so the schema says so rather than
396 * forcing a caller to pick one name and drop the rest (#692).
397 *
398 * @since 2.7.0
399 * @return array<string, mixed>
400 */
401 private static function alternate_name(): array {
402 return [
403 // A union rather than anyOf: read() and coerce() switch on `type`,
404 // and an entry without one is dropped as unrecognised. WordPress's
405 // schema validator accepts a type list, so this is both valid JSON
406 // Schema and legible to this file's own consumers.
407 'type' => [ 'string', 'array' ],
408 'items' => [ 'type' => 'string' ],
409 'description' => __( 'Alternate or former name of the site, published as schema alternateName on the homepage WebSite node. Send a string for one name, or an array for several. Blank entries and duplicates are dropped, and a single surviving name is published as a string.', 'thinkrank' ),
410 ];
411 }
412
413 private static function boolean( string $description ): array {
414 return [
415 'type' => 'boolean',
416 'description' => $description,
417 ];
418 }
419
420 /**
421 * A string property.
422 *
423 * @param string $description Translated text describing the value.
424 * @return array<string, mixed>
425 */
426 private static function string( string $description ): array {
427 return [
428 'type' => 'string',
429 'description' => $description,
430 ];
431 }
432
433 /**
434 * A %token% title template property.
435 *
436 * The vocabulary here is Site Identity's, which is not the one the Global
437 * SEO per-post-type templates use — %site_title% rather than %title%,
438 * %site_name% rather than %sitename% — so the description spells it out
439 * instead of leaving an agent to guess between the two.
440 *
441 * @param string $what Translated name of the pages the template titles.
442 * @param string $extra Comma-separated tags available only on those pages, or ''.
443 * @return array<string, mixed>
444 */
445 private static function template( string $what, string $extra ): array {
446 $shared = '%site_title%, %site_name%, %site_description%, %tagline%, %sep%, %separator%, %date%';
447
448 $description = '' === $extra
449 /* translators: 1: the pages a title template applies to. 2: the list of available variable tags. */
450 ? sprintf( __( 'Title template for %1$s. Available tags: %2$s.', 'thinkrank' ), $what, $shared )
451 /* translators: 1: the pages a title template applies to. 2: tags available everywhere. 3: tags available only on those pages. */
452 : sprintf( __( 'Title template for %1$s. Available tags: %2$s, and on these pages also %3$s.', 'thinkrank' ), $what, $shared, $extra );
453
454 return [
455 'type' => 'string',
456 'description' => $description,
457 ];
458 }
459 }
460