← All changes
|
includes/admin/importers/class-abstract-plugin-exporter.php
+105
-0
2.9.0
→
2.13.0
View file →
| @@ -254,8 +254,113 @@ | ||
| 254 | 254 | return ''; |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | /** |
| 258 | + * Placeholder a mapped token travels as while the rest of a template is | |
| 259 | + * resolved literally. A control character no source vocabulary uses, so | |
| 260 | + * no literal resolver matches, strips or splits on it. | |
| 261 | + */ | |
| 262 | + private const TOKEN_MARK = "\x1A"; | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * Convert a per-object (post or term) SEO value into ThinkRank variable | |
| 266 | + * tags wherever ThinkRank resolves the same thing per request, and into | |
| 267 | + * literal text everywhere else. | |
| 268 | + * | |
| 269 | + * A source title such as "%%title%% %%sep%% %%sitename%%" used to be | |
| 270 | + * resolved to literal text at import, so the site name, separator and | |
| 271 | + * post title were frozen as they stood that day: renaming the site or | |
| 272 | + * switching the separator left every imported title behind, and every | |
| 273 | + * exporter wrote " - " whatever separator the source used. Mapped tokens | |
| 274 | + * now become `%title%`, `%sep%`, `%sitename%`… — the tags the metabox | |
| 275 | + * inserts and Pattern_Resolver resolves on every output path — and only | |
| 276 | + * tokens ThinkRank has no per-object equivalent for are resolved here. | |
| 277 | + * | |
| 278 | + * Mapped tokens are masked before `$resolve_rest` runs, so a literal | |
| 279 | + * resolver that knows them (Rank Math's shares ThinkRank's syntax) never | |
| 280 | + * sees them, and unmasked afterwards. A value with no mapped token is | |
| 281 | + * returned exactly as `$resolve_rest` produces it. | |
| 282 | + * | |
| 283 | + * @param string $value Raw source value (already stringified) | |
| 284 | + * @param array $token_map Regex matching a source token => ThinkRank tag | |
| 285 | + * @param callable $resolve_rest fn(string): string — the exporter's literal resolver | |
| 286 | + * @return string ThinkRank value | |
| 287 | + */ | |
| 288 | + final protected function tokenize_object_template(string $value, array $token_map, callable $resolve_rest): string { | |
| 289 | + $tags = []; | |
| 290 | + $masked = $value; | |
| 291 | + | |
| 292 | + if ($value !== '') { | |
| 293 | + foreach ($token_map as $pattern => $tag) { | |
| 294 | + $masked = (string) preg_replace_callback( | |
| 295 | + $pattern, | |
| 296 | + static function () use (&$tags, $tag): string { | |
| 297 | + $index = array_search($tag, $tags, true); | |
| 298 | + if ($index === false) { | |
| 299 | + $tags[] = $tag; | |
| 300 | + $index = count($tags) - 1; | |
| 301 | + } | |
| 302 | + | |
| 303 | + return self::TOKEN_MARK . $index . self::TOKEN_MARK; | |
| 304 | + }, | |
| 305 | + $masked | |
| 306 | + ); | |
| 307 | + } | |
| 308 | + } | |
| 309 | + | |
| 310 | + if (empty($tags)) { | |
| 311 | + return (string) $resolve_rest($value); | |
| 312 | + } | |
| 313 | + | |
| 314 | + $resolved = (string) preg_replace_callback( | |
| 315 | + '/' . self::TOKEN_MARK . '(\d+)' . self::TOKEN_MARK . '/', | |
| 316 | + static fn(array $m): string => $tags[(int) $m[1]] ?? '', | |
| 317 | + (string) $resolve_rest($masked) | |
| 318 | + ); | |
| 319 | + | |
| 320 | + // Tokens resolved to '' (e.g. %%page%%) leave separators with nothing | |
| 321 | + // between or around them; Pattern_Resolver tidies those too, but the | |
| 322 | + // stored value should read cleanly in the metabox. | |
| 323 | + $resolved = trim((string) preg_replace('/\s+/', ' ', $resolved)); | |
| 324 | + $resolved = (string) preg_replace('/%sep%(?:\s*%sep%)+/', '%sep%', $resolved); | |
| 325 | + $resolved = (string) preg_replace('/^(?:%sep%\s*)+|(?:\s*%sep%)+$/', '', $resolved); | |
| 326 | + | |
| 327 | + return trim($resolved); | |
| 328 | + } | |
| 329 | + | |
| 330 | + /** | |
| 331 | + * Token map for tokenize_object_template() from literal source tokens | |
| 332 | + * (`%%title%%`, `%title%`), matched case-insensitively. | |
| 333 | + * | |
| 334 | + * @param array<string,string> $map Source token => ThinkRank tag | |
| 335 | + * @return array<string,string> Regex => ThinkRank tag | |
| 336 | + */ | |
| 337 | + final protected static function literal_token_patterns(array $map): array { | |
| 338 | + $patterns = []; | |
| 339 | + foreach ($map as $token => $tag) { | |
| 340 | + $patterns['/' . preg_quote((string) $token, '/') . '/i'] = $tag; | |
| 341 | + } | |
| 342 | + | |
| 343 | + return $patterns; | |
| 344 | + } | |
| 345 | + | |
| 346 | + /** | |
| 347 | + * Token map for tokenize_object_template() from brace-style variable | |
| 348 | + * names (`title` matches `{{title}}` and `{{ title }}`). | |
| 349 | + * | |
| 350 | + * @param array<string,string> $map Variable name => ThinkRank tag | |
| 351 | + * @return array<string,string> Regex => ThinkRank tag | |
| 352 | + */ | |
| 353 | + final protected static function brace_token_patterns(array $map): array { | |
| 354 | + $patterns = []; | |
| 355 | + foreach ($map as $name => $tag) { | |
| 356 | + $patterns['/\{\{\s*' . preg_quote((string) $name, '/') . '\s*\}\}/i'] = $tag; | |
| 357 | + } | |
| 358 | + | |
| 359 | + return $patterns; | |
| 360 | + } | |
| 361 | + | |
| 362 | + /** | |
| 258 | 363 | * Export a chunk of data and write to snapshot |
| 259 | 364 | * |
| 260 | 365 | * This is the main orchestration method. It calls the appropriate |
| 261 | 366 | * export_*_page() method, writes the chunk via Snapshot_Store, and |