| @@ -96,30 +96,47 @@ | ||
| 96 | 96 | * @return array Generated metadata |
| 97 | 97 | * @throws \Exception If generation fails |
| 98 | 98 | */ |
| 99 | 99 | public function generate_for_post(int $post_id, array $options = []): array { |
| 100 | + $metadata = $this->suggest_for_post($post_id, $options); | |
| 101 | + | |
| 102 | + // Save metadata to post meta | |
| 103 | + $this->save_post_metadata($post_id, $metadata); | |
| 104 | + | |
| 105 | + return $metadata; | |
| 106 | + } | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * Generate metadata for a post without writing anything. | |
| 110 | + * | |
| 111 | + * The same generation as {@see self::generate_for_post()}, minus the save. | |
| 112 | + * Bulk Snippets shows the result for review and writes only when the user | |
| 113 | + * clicks Save, so it must never persist as a side effect (#727). | |
| 114 | + * | |
| 115 | + * @since 2.8.0 | |
| 116 | + * | |
| 117 | + * @param int $post_id Post ID. | |
| 118 | + * @param array $options Generation options. | |
| 119 | + * @return array Generated metadata. | |
| 120 | + * @throws \Exception If the post is missing or generation fails. | |
| 121 | + */ | |
| 122 | + public function suggest_for_post(int $post_id, array $options = []): array { | |
| 100 | 123 | $post = get_post($post_id); |
| 101 | - | |
| 124 | + | |
| 102 | 125 | if (!$post) { |
| 103 | 126 | throw new \Exception('Post not found'); |
| 104 | 127 | } |
| 105 | - | |
| 128 | + | |
| 106 | 129 | // Extract content from post |
| 107 | 130 | $content = $this->extract_post_content($post); |
| 108 | - | |
| 131 | + | |
| 109 | 132 | // Add post-specific options |
| 110 | 133 | $options = array_merge($options, [ |
| 111 | 134 | 'content_type' => $this->determine_content_type($post), |
| 112 | 135 | 'post_id' => $post_id, |
| 113 | 136 | ]); |
| 114 | - | |
| 115 | - // Generate metadata | |
| 116 | - $metadata = $this->generate_for_content($content, $options); | |
| 117 | - | |
| 118 | - // Save metadata to post meta | |
| 119 | - $this->save_post_metadata($post_id, $metadata); | |
| 120 | - | |
| 121 | - return $metadata; | |
| 137 | + | |
| 138 | + return $this->generate_for_content($content, $options); | |
| 122 | 139 | } |
| 123 | 140 | |
| 124 | 141 | /** |
| 125 | 142 | * Generate multiple variations of metadata |
| @@ -202,11 +219,12 @@ | ||
| 202 | 219 | // Trim and limit length for AI processing |
| 203 | 220 | $content = trim($content); |
| 204 | 221 | $max_length = 4000; // Reasonable limit for AI processing |
| 205 | 222 | |
| 206 | - if (strlen($content) > $max_length) { | |
| 207 | - $content = substr($content, 0, $max_length) . '...'; | |
| 208 | - } | |
| 223 | + // strlen()/substr() count BYTES: on Thai or CJK this handed the model a | |
| 224 | + // third of the intended content, and cut the last character in half so | |
| 225 | + // the payload carried an invalid UTF-8 sequence (#687). | |
| 226 | + $content = \ThinkRank\Core\Seo_Text::trim_to_length($content, $max_length); | |
| 209 | 227 | |
| 210 | 228 | return $content; |
| 211 | 229 | } |
| 212 | 230 | |
| @@ -358,9 +376,27 @@ | ||
| 358 | 376 | $words = str_word_count($text, 1); |
| 359 | 377 | $syllables = 0; |
| 360 | 378 | |
| 361 | 379 | foreach ($words as $word) { |
| 362 | - $syllables += max(1, preg_match_all('/[aeiouy]+/i', $word)); | |
| 380 | + $word = preg_replace('/[^a-z]/', '', strtolower($word)); | |
| 381 | + if ($word === '') { | |
| 382 | + continue; | |
| 383 | + } | |
| 384 | + | |
| 385 | + $groups = preg_match_all('/[aeiouy]+/', $word); | |
| 386 | + | |
| 387 | + // Standard Flesch heuristic: a trailing silent e does not form a | |
| 388 | + // syllable ("make", "time", "these") — but only when a consonant | |
| 389 | + // precedes it (a vowel+e ending like "movie" already shares its | |
| 390 | + // group) and never for consonant-le ("table"), which does count. | |
| 391 | + // Without this the counter inflated syllables/word by ~0.2-0.3 on | |
| 392 | + // ordinary prose, driving raw Flesch negative and the UI to a | |
| 393 | + // clamped "Very Difficult (0)" (#407). | |
| 394 | + if ($groups > 1 && preg_match('/[^aeiouy]e$/', $word) && !str_ends_with($word, 'le')) { | |
| 395 | + $groups--; | |
| 396 | + } | |
| 397 | + | |
| 398 | + $syllables += max(1, $groups); | |
| 363 | 399 | } |
| 364 | 400 | |
| 365 | 401 | return $syllables; |
| 366 | 402 | } |