| @@ -119,8 +119,23 @@ | ||
| 119 | 119 | 'required' => false, |
| 120 | 120 | 'type' => 'string', |
| 121 | 121 | 'sanitize_callback' => 'sanitize_text_field', |
| 122 | 122 | ], |
| 123 | + // The permalink field as it stands in the editor, so an unsaved | |
| 124 | + // slug edit scores immediately instead of matching the old URL. | |
| 125 | + 'live_slug' => [ | |
| 126 | + 'required' => false, | |
| 127 | + 'type' => 'string', | |
| 128 | + // NOT sanitize_title: REST calls a sanitize_callback as | |
| 129 | + // ($value, $request, $param), and sanitize_title()'s second | |
| 130 | + // parameter is $fallback_title — so an empty slug returned | |
| 131 | + // the WP_REST_Request object, which fataled on the string | |
| 132 | + // cast below. The editor sends an empty slug whenever the | |
| 133 | + // permalink field is blank (every draft), so this fired on | |
| 134 | + // ordinary use. Sanitized with a single-argument | |
| 135 | + // sanitize_title() where it is consumed instead. | |
| 136 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 137 | + ], | |
| 123 | 138 | 'readability_score' => [ |
| 124 | 139 | 'required' => false, |
| 125 | 140 | 'type' => 'string', |
| 126 | 141 | 'sanitize_callback' => 'sanitize_text_field', |
| @@ -224,8 +239,17 @@ | ||
| 224 | 239 | // nothing about the field, which keeps the saved value. |
| 225 | 240 | $live_title = $request->get_param('live_title'); |
| 226 | 241 | $live_description = $request->get_param('live_description'); |
| 227 | 242 | |
| 243 | + // Score the slug the editor is showing. Omitted (null) keeps the | |
| 244 | + // saved slug; an empty string means the field was cleared, which | |
| 245 | + // falls back to the title-derived slug exactly as an unsaved draft | |
| 246 | + // does inside the calculator. | |
| 247 | + $live_slug = $request->get_param('live_slug'); | |
| 248 | + if ($live_slug !== null) { | |
| 249 | + $content_data['slug'] = sanitize_title((string) $live_slug); | |
| 250 | + } | |
| 251 | + | |
| 228 | 252 | $raw_title = $live_title !== null |
| 229 | 253 | ? (string) $live_title |
| 230 | 254 | : get_post_meta($post_id, '_thinkrank_seo_title', true); |
| 231 | 255 | $raw_description = $live_description !== null |
| @@ -262,13 +286,39 @@ | ||
| 262 | 286 | $metadata, |
| 263 | 287 | $score_options |
| 264 | 288 | ); |
| 265 | 289 | |
| 266 | - // Add readability_score and content_quality from frontend if provided | |
| 267 | - if (!empty($readability_score)) { | |
| 290 | + // Add readability_score and content_quality from frontend if | |
| 291 | + // provided. `!== null`, not `!empty()`: 0 is a legitimate score and | |
| 292 | + // empty() discarded it, so a post the editor scored as 0 kept | |
| 293 | + // whatever the calculator had produced instead (#394). Both params | |
| 294 | + // already default to null above, so null means "not sent". | |
| 295 | + // | |
| 296 | + // Only when the editor actually measured something, though. Inside a | |
| 297 | + // page builder there is no editor content to read, so Refresh sent an | |
| 298 | + // empty `live_content` along with the two labels derived from that | |
| 299 | + // empty string — the literal words "No content" — and they were | |
| 300 | + // applied over the correct server-side analysis this request had just | |
| 301 | + // run, then persisted by save_score(). That is what put "No content" | |
| 302 | + // on the dashboard for a page with a valid score (#778). With nothing | |
| 303 | + // measured, the analysis above is the better answer. | |
| 304 | + // | |
| 305 | + // Non-empty `live_content` is not on its own proof that the editor | |
| 306 | + // measured anything: builder markup (Divi 5) is markup the editor | |
| 307 | + // has plenty of and can read no words from, so it sent that same | |
| 308 | + // "No content" pair alongside 450 characters of block comments. | |
| 309 | + // Treat the sentinel itself as "nothing measured" whenever this | |
| 310 | + // request's own analysis did find words, so a stale bundle cannot | |
| 311 | + // overwrite a valid analysis either. | |
| 312 | + $editor_measured = '' !== trim((string) $live_content); | |
| 313 | + $server_found_content = ($content_data['word_count'] ?? 0) > 0; | |
| 314 | + | |
| 315 | + if ($editor_measured && null !== $readability_score | |
| 316 | + && !($server_found_content && self::is_no_content_label($readability_score))) { | |
| 268 | 317 | $score_data['readability_score'] = $readability_score; |
| 269 | 318 | } |
| 270 | - if (!empty($content_quality)) { | |
| 319 | + if ($editor_measured && null !== $content_quality | |
| 320 | + && !($server_found_content && self::is_no_content_label($content_quality))) { | |
| 271 | 321 | $score_data['content_quality'] = $content_quality; |
| 272 | 322 | } |
| 273 | 323 | |
| 274 | 324 | // Save score if requested |
| @@ -295,8 +345,25 @@ | ||
| 295 | 345 | } |
| 296 | 346 | } |
| 297 | 347 | |
| 298 | 348 | /** |
| 349 | + * Whether an editor-supplied label means "I could not read the content". | |
| 350 | + * | |
| 351 | + * `calculateReadabilityScore()` and `calculateContentQuality()` both return | |
| 352 | + * this exact untranslated string when handed an empty body, so it is the | |
| 353 | + * editor saying it measured nothing rather than a measurement in its own | |
| 354 | + * right. Every real label carries a level or a grade. | |
| 355 | + * | |
| 356 | + * @since 2.12.0 | |
| 357 | + * | |
| 358 | + * @param mixed $label Label sent by the editor. | |
| 359 | + * @return bool True when the label is the empty-content sentinel. | |
| 360 | + */ | |
| 361 | + private static function is_no_content_label($label): bool { | |
| 362 | + return is_string($label) && 'no content' === strtolower(trim($label)); | |
| 363 | + } | |
| 364 | + | |
| 365 | + /** | |
| 299 | 366 | * Get existing SEO score for a post |
| 300 | 367 | * |
| 301 | 368 | * @param WP_REST_Request $request Request object |
| 302 | 369 | * @return WP_REST_Response|WP_Error Response object |
| @@ -308,8 +375,10 @@ | ||
| 308 | 375 | // Get existing score data from database |
| 309 | 376 | $existing_data = $this->calculator->get_existing_score_data($post_id); |
| 310 | 377 | |
| 311 | 378 | if ($existing_data) { |
| 379 | + $existing_data['keywords'] = $this->keyword_placements((int) $post_id); | |
| 380 | + | |
| 312 | 381 | return new WP_REST_Response([ |
| 313 | 382 | 'success' => true, |
| 314 | 383 | 'data' => $existing_data, |
| 315 | 384 | 'message' => __('Existing SEO score retrieved successfully', 'thinkrank') |
| @@ -327,8 +396,27 @@ | ||
| 327 | 396 | } |
| 328 | 397 | } |
| 329 | 398 | |
| 330 | 399 | /** |
| 400 | + * Where each focus keyword sits (#729), for a stored score. A stored score | |
| 401 | + * predates the editor's current keywords and content, so this is read | |
| 402 | + * from the post as saved now — a text pass, no request, no AI. | |
| 403 | + * | |
| 404 | + * @param int $post_id Post ID. | |
| 405 | + * @return array<int, array<string, mixed>> | |
| 406 | + */ | |
| 407 | + private function keyword_placements(int $post_id): array { | |
| 408 | + return $this->calculator->keyword_placements( | |
| 409 | + $this->calculator->analyze_post_content($post_id), | |
| 410 | + [ | |
| 411 | + 'title' => \ThinkRank\SEO\Pattern_Resolver::effective_title($post_id), | |
| 412 | + 'description' => \ThinkRank\SEO\Pattern_Resolver::effective_description($post_id), | |
| 413 | + ], | |
| 414 | + \ThinkRank\SEO\Focus_Keywords::get($post_id) | |
| 415 | + ); | |
| 416 | + } | |
| 417 | + | |
| 418 | + /** | |
| 331 | 419 | * Get score history for a post |
| 332 | 420 | * |
| 333 | 421 | * @param WP_REST_Request $request Request object |
| 334 | 422 | * @return WP_REST_Response|WP_Error Response object |
| @@ -363,10 +451,14 @@ | ||
| 363 | 451 | public function get_latest_score(WP_REST_Request $request) { |
| 364 | 452 | try { |
| 365 | 453 | $post_id = $request->get_param('post_id'); |
| 366 | 454 | |
| 455 | + // The stored row only — no keyword gauges (#729). The dashboard | |
| 456 | + // asks for five posts at once and reads overall_score; a gauge | |
| 457 | + // pass would analyse and render each whole post for nothing. The | |
| 458 | + // editor reads its gauges from /seo-score/get. | |
| 367 | 459 | $latest_score = $this->calculator->get_latest_score($post_id); |
| 368 | - | |
| 460 | + | |
| 369 | 461 | return new WP_REST_Response([ |
| 370 | 462 | 'success' => true, |
| 371 | 463 | 'data' => $latest_score, |
| 372 | 464 | ], 200); |