| 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::string( __( 'Alternate or former name of the site, published as schema alternateName.', 'thinkrank' ) ), |
| 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 |
'robots_txt_enabled' => self::boolean( __( 'Whether ThinkRank manages robots.txt. Its contents are set with thinkrank/update-robots-txt.', 'thinkrank' ) ), |
| 140 |
'ai_crawler_rules' => self::ai_crawler_rules(), |
| 141 |
]; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* JSON schema properties for every exposed `sitemap` key. |
| 146 |
* |
| 147 |
* @return array<string, array<string, mixed>> |
| 148 |
*/ |
| 149 |
public static function sitemap(): array { |
| 150 |
return [ |
| 151 |
'enabled' => self::boolean( __( 'Whether XML sitemap generation is active.', 'thinkrank' ) ), |
| 152 |
'auto_generate' => self::boolean( __( 'Whether the sitemap regenerates automatically when content changes.', 'thinkrank' ) ), |
| 153 |
'use_sitemap_index' => self::boolean( __( 'Whether to publish a sitemap index that links per-type sitemaps, rather than one flat file.', 'thinkrank' ) ), |
| 154 |
'links_per_sitemap' => [ |
| 155 |
'type' => 'integer', |
| 156 |
'description' => __( 'Maximum URLs per sitemap file before it is split.', 'thinkrank' ), |
| 157 |
'minimum' => 1, |
| 158 |
'maximum' => 50000, |
| 159 |
], |
| 160 |
'custom_url_pattern' => self::string( __( 'Filename pattern for generated sitemaps, e.g. "sitemap-{type}.xml".', 'thinkrank' ) ), |
| 161 |
'enable_styling' => self::boolean( __( 'Whether an XSL stylesheet is attached so the sitemap is readable in a browser.', 'thinkrank' ) ), |
| 162 |
'ping_search_engines' => self::boolean( __( 'Whether search engines are notified after the sitemap is regenerated.', 'thinkrank' ) ), |
| 163 |
|
| 164 |
// What goes in. |
| 165 |
'include_posts' => self::boolean( __( 'Whether posts are included.', 'thinkrank' ) ), |
| 166 |
'include_pages' => self::boolean( __( 'Whether pages are included.', 'thinkrank' ) ), |
| 167 |
'include_categories' => self::boolean( __( 'Whether category archives are included.', 'thinkrank' ) ), |
| 168 |
'include_tags' => self::boolean( __( 'Whether tag archives are included.', 'thinkrank' ) ), |
| 169 |
'include_images' => self::boolean( __( 'Whether image entries are included for each URL.', 'thinkrank' ) ), |
| 170 |
'include_featured_images' => self::boolean( __( 'Whether featured images are included as image entries.', 'thinkrank' ) ), |
| 171 |
|
| 172 |
// What stays out. |
| 173 |
'exclude_posts' => self::string( __( 'Comma-separated post IDs to leave out.', 'thinkrank' ) ), |
| 174 |
'exclude_terms' => self::string( __( 'Comma-separated term IDs to leave out.', 'thinkrank' ) ), |
| 175 |
'exclude_private_posts' => self::boolean( __( 'Whether privately published posts are left out.', 'thinkrank' ) ), |
| 176 |
'exclude_password_protected' => self::boolean( __( 'Whether password-protected posts are left out.', 'thinkrank' ) ), |
| 177 |
]; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Read the exposed keys out of a manager's stored settings. |
| 182 |
* |
| 183 |
* The store keeps everything as strings ("1"/""), so each value is coerced |
| 184 |
* back to the type the schema advertises before it reaches an agent. |
| 185 |
* |
| 186 |
* @param array<string, array<string, mixed>> $properties Schema properties for the category. |
| 187 |
* @param array<string, mixed> $stored Settings as the manager returns them. |
| 188 |
* @return array<string, mixed> Exposed settings, one entry per declared key. |
| 189 |
*/ |
| 190 |
public static function read( array $properties, array $stored ): array { |
| 191 |
$out = []; |
| 192 |
|
| 193 |
foreach ( $properties as $key => $property ) { |
| 194 |
$value = $stored[ $key ] ?? null; |
| 195 |
|
| 196 |
switch ( $property['type'] ) { |
| 197 |
case 'boolean': |
| 198 |
$out[ $key ] = (bool) $value; |
| 199 |
break; |
| 200 |
case 'integer': |
| 201 |
$out[ $key ] = (int) $value; |
| 202 |
break; |
| 203 |
case 'array': |
| 204 |
case 'object': |
| 205 |
$out[ $key ] = is_array( $value ) ? $value : []; |
| 206 |
break; |
| 207 |
default: |
| 208 |
$out[ $key ] = (string) ( $value ?? '' ); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
return $out; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Coerce an incoming patch to the declared types, dropping unknown keys. |
| 217 |
* |
| 218 |
* Strings are passed through rather than sanitized here: the manager's own |
| 219 |
* save path already picks the right sanitizer per key, and running |
| 220 |
* sanitize_text_field() first would strip %date% and %category% out of the |
| 221 |
* title templates as percent-encoding before it ever got the chance (#521). |
| 222 |
* |
| 223 |
* @param array<string, array<string, mixed>> $properties Schema properties for the category. |
| 224 |
* @param array<string, mixed> $incoming Caller-supplied settings. |
| 225 |
* @return array<string, mixed> Recognized keys only, coerced to type. |
| 226 |
*/ |
| 227 |
public static function coerce( array $properties, array $incoming ): array { |
| 228 |
$out = []; |
| 229 |
|
| 230 |
foreach ( $properties as $key => $property ) { |
| 231 |
if ( ! array_key_exists( $key, $incoming ) ) { |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
$value = $incoming[ $key ]; |
| 236 |
|
| 237 |
switch ( $property['type'] ) { |
| 238 |
case 'boolean': |
| 239 |
$out[ $key ] = (bool) $value; |
| 240 |
break; |
| 241 |
case 'integer': |
| 242 |
$number = (int) $value; |
| 243 |
if ( isset( $property['minimum'] ) ) { |
| 244 |
$number = max( (int) $property['minimum'], $number ); |
| 245 |
} |
| 246 |
if ( isset( $property['maximum'] ) ) { |
| 247 |
$number = min( (int) $property['maximum'], $number ); |
| 248 |
} |
| 249 |
$out[ $key ] = $number; |
| 250 |
break; |
| 251 |
case 'array': |
| 252 |
case 'object': |
| 253 |
if ( ! is_array( $value ) ) { |
| 254 |
// A structured key given a scalar is a caller error, not |
| 255 |
// something to flatten into a string and store. |
| 256 |
continue 2; |
| 257 |
} |
| 258 |
$out[ $key ] = $value; |
| 259 |
break; |
| 260 |
default: |
| 261 |
if ( is_array( $value ) ) { |
| 262 |
continue 2; |
| 263 |
} |
| 264 |
$out[ $key ] = (string) $value; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
return $out; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* The opening hours object, one entry per day. |
| 273 |
* |
| 274 |
* A real sub-schema rather than string coercion: the value is a map of day |
| 275 |
* name to {open, close, closed}, and flattening it to a string would make |
| 276 |
* it unwritable over MCP. |
| 277 |
* |
| 278 |
* @return array<string, mixed> |
| 279 |
*/ |
| 280 |
private static function business_hours(): array { |
| 281 |
$day = [ |
| 282 |
'type' => 'object', |
| 283 |
'additionalProperties' => false, |
| 284 |
'properties' => [ |
| 285 |
'open' => [ |
| 286 |
'type' => 'string', |
| 287 |
'description' => __( 'Opening time as 24-hour HH:MM, or "" when closed.', 'thinkrank' ), |
| 288 |
], |
| 289 |
'close' => [ |
| 290 |
'type' => 'string', |
| 291 |
'description' => __( 'Closing time as 24-hour HH:MM, or "" when closed.', 'thinkrank' ), |
| 292 |
], |
| 293 |
'closed' => [ |
| 294 |
'type' => 'boolean', |
| 295 |
'description' => __( 'Whether the business is closed all day.', 'thinkrank' ), |
| 296 |
], |
| 297 |
], |
| 298 |
]; |
| 299 |
|
| 300 |
$properties = []; |
| 301 |
foreach ( [ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' ] as $name ) { |
| 302 |
$properties[ $name ] = $day; |
| 303 |
} |
| 304 |
|
| 305 |
return [ |
| 306 |
'type' => 'object', |
| 307 |
'description' => __( 'Opening hours per day of the week.', 'thinkrank' ), |
| 308 |
'additionalProperties' => false, |
| 309 |
'properties' => $properties, |
| 310 |
]; |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* The per-agent AI crawler allow/block map. |
| 315 |
* |
| 316 |
* Enumerating the known slugs rather than accepting a free-form object is |
| 317 |
* what makes this usable by an agent: the alternative is a caller guessing |
| 318 |
* `chatgpt` for a crawler registered as `chatgpt-user`, having the key |
| 319 |
* dropped as unknown, and being told the save succeeded — because it did, |
| 320 |
* with nothing in it. |
| 321 |
* |
| 322 |
* @return array<string, mixed> |
| 323 |
*/ |
| 324 |
private static function ai_crawler_rules(): array { |
| 325 |
$properties = []; |
| 326 |
|
| 327 |
foreach ( \ThinkRank\SEO\AI_Crawlers::for_display() as $agent ) { |
| 328 |
$properties[ $agent['slug'] ] = [ |
| 329 |
'type' => 'string', |
| 330 |
'enum' => [ 'allow', 'block' ], |
| 331 |
/* translators: 1: crawler user-agent token, e.g. GPTBot. 2: what that crawler is for. */ |
| 332 |
'description' => sprintf( __( '%1$s — %2$s', 'thinkrank' ), $agent['token'], $agent['purpose'] ), |
| 333 |
]; |
| 334 |
} |
| 335 |
|
| 336 |
return [ |
| 337 |
'type' => 'object', |
| 338 |
'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' ), |
| 339 |
'additionalProperties' => false, |
| 340 |
'properties' => $properties, |
| 341 |
]; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* A boolean property. |
| 346 |
* |
| 347 |
* @param string $description Translated text describing what the toggle does. |
| 348 |
* @return array<string, mixed> |
| 349 |
*/ |
| 350 |
private static function boolean( string $description ): array { |
| 351 |
return [ |
| 352 |
'type' => 'boolean', |
| 353 |
'description' => $description, |
| 354 |
]; |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* A string property. |
| 359 |
* |
| 360 |
* @param string $description Translated text describing the value. |
| 361 |
* @return array<string, mixed> |
| 362 |
*/ |
| 363 |
private static function string( string $description ): array { |
| 364 |
return [ |
| 365 |
'type' => 'string', |
| 366 |
'description' => $description, |
| 367 |
]; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* A %token% title template property. |
| 372 |
* |
| 373 |
* The vocabulary here is Site Identity's, which is not the one the Global |
| 374 |
* SEO per-post-type templates use — %site_title% rather than %title%, |
| 375 |
* %site_name% rather than %sitename% — so the description spells it out |
| 376 |
* instead of leaving an agent to guess between the two. |
| 377 |
* |
| 378 |
* @param string $what Translated name of the pages the template titles. |
| 379 |
* @param string $extra Comma-separated tags available only on those pages, or ''. |
| 380 |
* @return array<string, mixed> |
| 381 |
*/ |
| 382 |
private static function template( string $what, string $extra ): array { |
| 383 |
$shared = '%site_title%, %site_name%, %site_description%, %tagline%, %sep%, %separator%, %date%'; |
| 384 |
|
| 385 |
$description = '' === $extra |
| 386 |
/* translators: 1: the pages a title template applies to. 2: the list of available variable tags. */ |
| 387 |
? sprintf( __( 'Title template for %1$s. Available tags: %2$s.', 'thinkrank' ), $what, $shared ) |
| 388 |
/* translators: 1: the pages a title template applies to. 2: tags available everywhere. 3: tags available only on those pages. */ |
| 389 |
: sprintf( __( 'Title template for %1$s. Available tags: %2$s, and on these pages also %3$s.', 'thinkrank' ), $what, $shared, $extra ); |
| 390 |
|
| 391 |
return [ |
| 392 |
'type' => 'string', |
| 393 |
'description' => $description, |
| 394 |
]; |
| 395 |
} |
| 396 |
} |
| 397 |
|