10, 'h1' => 9, 'first_100_words' => 7, 'subheadings' => 6, 'meta_description' => 6, 'slug' => 3, 'image_alt' => 5, 'links' => 8, 'content_length' => 6, ); const DENSITY_WEIGHT = 20; const READABILITY_WEIGHT = 20; /** * Keyword frequency/placement helper. * * @var ABDS_SEO_Keyword_Extractor */ protected ABDS_SEO_Keyword_Extractor $keywords; /** * Readability scoring helper. * * @var ABDS_SEO_Readability_Checker */ protected ABDS_SEO_Readability_Checker $readability; /** * Per-block recommendation engine (heading/paragraph/image/list). * * @var ABDS_SEO_Block_Recommendations */ protected ABDS_SEO_Block_Recommendations $block_recommendations; /** * Constructor. */ public function __construct() { $this->keywords = new ABDS_SEO_Keyword_Extractor(); $this->readability = new ABDS_SEO_Readability_Checker(); $this->block_recommendations = new ABDS_SEO_Block_Recommendations( $this->keywords ); } /** * Analyzes a post's content against a focus keyword and persists the * resulting content score to wp_abds_seo_page_scores. * * @param int $post_id Post ID to analyze. * @param string $focus_keyword Focus keyword (or phrase). * @return array{post_id:int, focus_keyword:string, content_score:int, keyword_density:float, checks:array, readability:array, recommendations:array, block_recommendations:array} */ public function analyze( int $post_id, string $focus_keyword ): array { $post = get_post( $post_id ); if ( ! $post ) { return $this->empty_result( $post_id, $focus_keyword ); } $focus_keyword = trim( $focus_keyword ); if ( '' === $focus_keyword ) { return $this->empty_result( $post_id, $focus_keyword ); } $title = get_post_meta( $post_id, '_abds_seo_title', true ); $title = '' !== $title ? $title : get_the_title( $post_id ); $result = $this->compute( $post_id, $focus_keyword, array( 'title' => (string) $title, 'post_title' => (string) $post->post_title, 'description' => (string) get_post_meta( $post_id, '_abds_seo_description', true ), 'slug' => (string) $post->post_name, 'content' => (string) $post->post_content, ) ); $this->persist_result( $post_id, $result['content_score'], $result['recommendations'] ); return $result; } /** * Same analysis `analyze()` runs, but against draft/in-progress field * values instead of the saved post row, and — deliberately — never * persists to `wp_abds_seo_page_scores`. For live, debounced * recalculation while the user is still typing (Gutenberg sidebar, * Classic Editor meta box): nothing about "what you're about to save" * should overwrite the real score for what's actually published. * * Every `$overrides` key is optional; anything not passed falls back to * the post's current saved value (or, for `content`, requires either an * override or a real post — an unsaved-content-only preview for a new, * never-saved post has no baseline to fall back to and returns the * empty result, same as `analyze()` does for a missing post). * * @param int $post_id Post ID (source of any field not overridden). * @param string $focus_keyword Focus keyword (or phrase) — always the live/draft value, no post-meta fallback. * @param array{title?:string, post_title?:string, description?:string, slug?:string, content?:string} $overrides Draft field values. * @return array{post_id:int, focus_keyword:string, content_score:int, keyword_density:float, checks:array, readability:array, recommendations:array, block_recommendations:array} */ public function preview( int $post_id, string $focus_keyword, array $overrides = array() ): array { $post = get_post( $post_id ); $focus_keyword = trim( $focus_keyword ); if ( '' === $focus_keyword ) { return $this->empty_result( $post_id, $focus_keyword ); } $title = $overrides['title'] ?? ( $post ? get_post_meta( $post_id, '_abds_seo_title', true ) : '' ); $title = '' !== $title ? $title : ( $post ? get_the_title( $post_id ) : '' ); $data = array( 'title' => (string) $title, 'post_title' => (string) ( $overrides['post_title'] ?? ( $post ? $post->post_title : '' ) ), 'description' => (string) ( $overrides['description'] ?? ( $post ? get_post_meta( $post_id, '_abds_seo_description', true ) : '' ) ), 'slug' => (string) ( $overrides['slug'] ?? ( $post ? $post->post_name : '' ) ), 'content' => (string) ( $overrides['content'] ?? ( $post ? $post->post_content : '' ) ), ); return $this->compute( $post_id, $focus_keyword, $data ); } /** * The actual scoring logic, shared by `analyze()` (saved post, persists) * and `preview()` (draft fields, never persists). Pulled out so both * entry points run the exact same checks — no separate "preview logic" * to drift out of sync with the real one. * * @param int $post_id Post ID (only used to label the result and for block-recommendation client-id lookups). * @param string $focus_keyword Focus keyword (already trimmed/validated non-empty by the caller). * @param array{title:string, post_title:string, description:string, slug:string, content:string} $data Field values to score. * @return array{post_id:int, focus_keyword:string, content_score:int, keyword_density:float, checks:array, readability:array, recommendations:array, block_recommendations:array} */ protected function compute( int $post_id, string $focus_keyword, array $data ): array { $title = $data['title']; $post_title = $data['post_title'] ?? $title; $description = $data['description']; $slug = $data['slug']; $content = $data['content']; $plain = wp_strip_all_tags( $content ); $first_100 = $this->keywords->first_words( $plain, 100 ); $checks = array( 'title' => $this->keywords->contains( $title, $focus_keyword ), 'h1' => $this->check_h1( $post_title, $content, $focus_keyword ), 'first_100_words' => $this->keywords->contains( $first_100, $focus_keyword ), 'subheadings' => $this->check_heading_group( $content, $focus_keyword, array( 'h2', 'h3', 'h4', 'h5', 'h6' ) ), 'slug' => $this->check_slug( $slug, $focus_keyword ), 'meta_description' => $this->keywords->contains( $description, $focus_keyword ), 'image_alt' => $this->check_image_alt( $content, $focus_keyword ), 'links' => $this->check_links( $content ), 'content_length' => $this->keywords->word_count( $plain ) >= self::MIN_CONTENT_WORDS, ); $density = $this->keywords->density( $plain, $focus_keyword ); $readability = $this->readability->analyze( $content ); $recommendations = $this->build_recommendations( $checks, $density, $content ); $score = $this->calculate_score( $checks, $density, $readability['score'] ); $score_breakdown = $this->build_score_breakdown( $checks, $density, $readability['score'] ); $block_recommendations = array_map( array( $this, 'translate_block_recommendation' ), $this->block_recommendations->analyze( parse_blocks( $content ), $focus_keyword ) ); return array( 'post_id' => $post_id, 'focus_keyword' => $focus_keyword, 'content_score' => $score, 'keyword_density' => $density, 'checks' => $checks, 'score_breakdown' => $score_breakdown, 'readability' => $readability, 'recommendations' => $recommendations, 'block_recommendations' => $block_recommendations, ); } /** * Builds the zero-value result returned when the post doesn't exist or * no focus keyword was supplied. * * @param int $post_id Post ID. * @param string $focus_keyword Focus keyword as passed in (possibly empty). * @return array{post_id:int, focus_keyword:string, content_score:int, keyword_density:float, checks:array, readability:array, recommendations:array, block_recommendations:array} */ protected function empty_result( int $post_id, string $focus_keyword ): array { return array( 'post_id' => $post_id, 'focus_keyword' => $focus_keyword, 'content_score' => 0, 'keyword_density' => 0.0, 'checks' => array(), 'score_breakdown' => array(), 'readability' => array(), 'recommendations' => array(), 'block_recommendations' => array(), ); } /** * Whether the keyword appears inside any heading matching the given tags. * * @param string $content Post content HTML. * @param string $keyword Focus keyword (or phrase). * @param string[] $tags Heading tag names to check, e.g. ['h1'] or ['h2'..'h6']. * @return bool True if found in at least one matching heading. */ protected function check_heading_group( string $content, string $keyword, array $tags ): bool { $pattern = '/<(?:' . implode( '|', $tags ) . ')[^>]*>(.*?)<\/(?:' . implode( '|', $tags ) . ')>/is'; if ( ! preg_match_all( $pattern, $content, $matches ) ) { return false; } foreach ( $matches[1] as $heading_html ) { if ( $this->keywords->contains( wp_strip_all_tags( $heading_html ), $keyword ) ) { return true; } } return false; } /** * Whether the focus keyword is in the page's real, rendered H1. In * WordPress that's `post_title` — themes render it as the page/post H1 * from the template, not from a block inside post_content — so this * checks $post_title first. A literal `

` inside the content itself * is checked too (an OR, not instead-of): rare in standard themes, but * some page builders / custom templates do render one, and if an author * put the keyword there it should still count. * * Replaces the pre-A5 version, which only ever looked for a literal * `

` in post_content — a check that could never pass on a normal * WordPress theme regardless of content quality (see * ABDS-SEO-TZ-SCORE-WOW-093.md Task A, finding 01). * * @param string $post_title The post's actual title (not the overridable SEO title). * @param string $content Post content HTML. * @param string $keyword Focus keyword (or phrase). * @return bool True if the keyword is in post_title or an explicit content

. */ protected function check_h1( string $post_title, string $content, string $keyword ): bool { if ( $this->keywords->contains( $post_title, $keyword ) ) { return true; } return $this->check_heading_group( $content, $keyword, array( 'h1' ) ); } /** * Whether the focus keyword appears in the URL slug. For Cyrillic (and * other non-ASCII) keywords this needs two extra comparisons beyond a * literal match, because of how WordPress actually stores non-Latin * slugs — NOT by transliterating to Latin (a wrong assumption corrected * during Task A5's implementation; sanitize_title() percent-encodes the * UTF-8 bytes instead, e.g. "стиснення" becomes "%d1%81%d1%82..."): * * 1. $post->post_name in the database is itself percent-encoded for a * Cyrillic title, so it needs urldecode() before a literal keyword * match can work at all. * 2. As a defensive fallback (a manually-typed Latin slug, a filter * that changes the encoding), sanitize_title($keyword) is compared * against the raw, still-encoded slug too — both sides go through * the same percent-encoding, so they stay comparable. * * Real-world confirmation: this was ABDS-SEO-TZ-SCORE-WOW-093.md Task A * finding 02, originally diagnosed as "Yoast/Rank Math share this gap * too" — true only for a hand-typed Latin slug; WordPress's actual * default Cyrillic-slug behavior is fixable outright, and doing so is * a real advantage this plugin now has that neither competitor does. * * sanitize_title() is a WordPress function, so this method lives here * rather than in ABDS_SEO_Keyword_Extractor, which stays free of WP * calls for unit testing without bootstrapping WP. * * @param string $slug URL slug to check. * @param string $keyword Focus keyword (or phrase). * @return bool True if the slug contains the keyword in any of the forms above. */ protected function check_slug( string $slug, string $keyword ): bool { if ( $this->keywords->slug_contains_keyword( $slug, $keyword ) ) { return true; } $decoded_slug = urldecode( $slug ); if ( $decoded_slug !== $slug && $this->keywords->slug_contains_keyword( $decoded_slug, $keyword ) ) { return true; } return $this->keywords->slug_contains_keyword( $slug, sanitize_title( $keyword ) ); } /** * Whether at least one in the content has alt text containing the * focus keyword. Matches Yoast's imageKeyphrase / Rank Math's * keywordInImageAlt checks (ABDS-SEO-TZ-SCORE-WOW-093.md Task A5 item * 4) — a page with zero images fails this the same way a page with * images-but-no-matching-alt does, consistent with both competitors. * * @param string $content Post content HTML. * @param string $keyword Focus keyword (or phrase). * @return bool True if a matching alt text is found. */ protected function check_image_alt( string $content, string $keyword ): bool { if ( ! preg_match_all( '/]*\balt=(["\'])(.*?)\1/is', $content, $matches ) ) { return false; } foreach ( $matches[2] as $alt_text ) { if ( $this->keywords->contains( html_entity_decode( $alt_text ), $keyword ) ) { return true; } } return false; } /** * Whether the content has at least one internal link (same host, or a * relative URL) AND at least one external link (a different host). * Kept as a single combined check per Task A5 item 4's explicit "3 * checks" scope (image alt / links / content length) rather than * splitting into two separately-weighted checks the way Yoast/Rank Math * do — a cheaper first cut; splitting these into their own weights is * open for R3, where the checks are being refactored onto * AuditCheckInterface anyway. * * @param string $content Post content HTML. * @return bool True if both an internal and an external link are present. */ protected function check_links( string $content ): bool { if ( ! preg_match_all( '/]*\bhref=(["\'])(.*?)\1/is', $content, $matches ) ) { return false; } $site_host = (string) wp_parse_url( home_url(), PHP_URL_HOST ); $has_internal = false; $has_external = false; foreach ( $matches[2] as $href ) { $href = html_entity_decode( $href ); if ( '' === $href || '#' === $href[0] ) { continue; } $link_host = wp_parse_url( $href, PHP_URL_HOST ); if ( empty( $link_host ) || $link_host === $site_host ) { $has_internal = true; } else { $has_external = true; } } return $has_internal && $has_external; } /** * Builds the add/adapt/remove recommendation list from the check * results and keyword density, then ties each to a Gutenberg block. * * @param array $checks Placement check results, keyed by check name. * @param float $density Keyword density percentage. * @param string $content Post content HTML. * @return array */ protected function build_recommendations( array $checks, float $density, string $content ): array { $recommendations = array(); if ( ! $checks['title'] ) { $recommendations[] = $this->recommendation( self::ACTION_ADAPT, 'title', __( 'Add the focus keyword to the page title.', 'abds-seo' ) ); } if ( ! $checks['h1'] ) { $recommendations[] = $this->recommendation( self::ACTION_ADD, 'h1', __( 'Add the focus keyword to the H1 heading.', 'abds-seo' ) ); } if ( ! $checks['first_100_words'] ) { $recommendations[] = $this->recommendation( self::ACTION_ADAPT, 'first_paragraph', __( 'Mention the keyword in the first 100 words of the text.', 'abds-seo' ) ); } if ( ! $checks['subheadings'] ) { $recommendations[] = $this->recommendation( self::ACTION_ADD, 'subheading', __( 'Add a subheading (H2–H6) with the keyword or a variation of it.', 'abds-seo' ) ); } if ( ! $checks['meta_description'] ) { $recommendations[] = $this->recommendation( self::ACTION_ADD, 'meta_description', __( 'Add a meta description containing the keyword.', 'abds-seo' ) ); } if ( ! $checks['slug'] ) { $recommendations[] = $this->recommendation( self::ACTION_ADAPT, 'slug', __( 'Consider adding the keyword to the URL slug.', 'abds-seo' ) ); } if ( ! $checks['image_alt'] ) { $recommendations[] = $this->recommendation( self::ACTION_ADD, 'image_alt', __( 'Add an image with the focus keyword in its alt text.', 'abds-seo' ) ); } if ( ! $checks['links'] ) { $recommendations[] = $this->recommendation( self::ACTION_ADD, 'links', __( 'Add at least one internal link and one link to an external source.', 'abds-seo' ) ); } if ( ! $checks['content_length'] ) { $recommendations[] = $this->recommendation( self::ACTION_ADAPT, 'content_length', sprintf( /* translators: %d: minimum recommended word count. */ __( 'This page is under %d words — consider expanding it.', 'abds-seo' ), self::MIN_CONTENT_WORDS ) ); } if ( $density > self::DENSITY_MAX ) { $recommendations[] = $this->recommendation( self::ACTION_REMOVE, 'content', sprintf( /* translators: %s: keyword density percentage. */ __( 'Keyword over-stuffing (density %s%%) — remove some repetitions.', 'abds-seo' ), $density ) ); } elseif ( $density < self::DENSITY_MIN ) { $recommendations[] = $this->recommendation( self::ACTION_ADAPT, 'content', sprintf( /* translators: %s: keyword density percentage. */ __( 'Keyword density is too low (%s%%) — add a few natural mentions in the text.', 'abds-seo' ), $density ) ); } return $this->attach_block_references( $recommendations, $content ); } /** * Builds a single recommendation entry. * * @param string $action One of the ACTION_* constants. * @param string $target Recommendation target, e.g. "title", "h1", "content". * @param string $message Human-readable, translated message. * @return array{action:string, target:string, message:string, block_client_id:?string, block_position:?int} */ protected function recommendation( string $action, string $target, string $message ): array { return array( 'action' => $action, 'target' => $target, 'message' => $message, 'block_client_id' => null, 'block_position' => null, ); } /** * Ties a recommendation to the Gutenberg block it applies to. Server-side * parse_blocks() does not expose the editor's runtime clientId, so the * block's position in the post is used as the fallback identifier — the * REST layer can be called from the editor with the live clientId when * a richer match is available (see includes/api/class-abds-seo-rest-controller.php). * * @param array $recommendations Recommendations to annotate. * @param string $content Post content HTML. * @return array */ protected function attach_block_references( array $recommendations, string $content ): array { if ( ! function_exists( 'parse_blocks' ) ) { return $recommendations; } $blocks = parse_blocks( $content ); $block_map = array( 'h1' => 'core/heading', 'subheading' => 'core/heading', 'first_paragraph' => 'core/paragraph', 'image_alt' => 'core/image', ); foreach ( $recommendations as &$recommendation ) { $wanted_block_name = $block_map[ $recommendation['target'] ] ?? null; if ( ! $wanted_block_name ) { continue; } foreach ( $blocks as $position => $block ) { if ( ( $block['blockName'] ?? null ) === $wanted_block_name ) { $recommendation['block_position'] = $position; break; } } } unset( $recommendation ); return $recommendations; } /** * Maps a single ABDS_SEO_Block_Recommendations entry's machine-readable * "issue" code to a translated "suggestion" message. Kept here, in the * WP-dependent orchestrator, rather than in ABDS_SEO_Block_Recommendations * itself, because that class calls zero WordPress functions (including * __()) so it stays unit testable without bootstrapping WP. * * @param array{block_path:int[], block_type:string, content_snippet:string, issue:string, severity:string} $recommendation Entry from ABDS_SEO_Block_Recommendations::analyze(). * @return array{block_path:int[], block_type:string, content_snippet:string, issue:string, suggestion:string, severity:string} */ protected function translate_block_recommendation( array $recommendation ): array { $messages = array( 'heading_missing_keyword' => __( 'Add the focus keyword to this subheading.', 'abds-seo' ), 'first_paragraph_missing_keyword' => __( 'The first paragraph does not contain the focus keyword.', 'abds-seo' ), 'paragraph_too_long' => __( 'This paragraph is too long — split it for better readability.', 'abds-seo' ), 'image_missing_alt' => __( 'This image has no alt text.', 'abds-seo' ), 'list_empty' => __( 'This list is empty.', 'abds-seo' ), ); $recommendation['suggestion'] = $messages[ $recommendation['issue'] ] ?? ''; return $recommendation; } /** * Categorizes a content_score into good/ok/poor/not_analyzed — the one * place this plugin decides what those words mean, reused everywhere * a score needs a traffic-light-style label (currently: reports, see * ABDS_SEO_Reports) rather than each caller re-deriving its own * boundaries. Deliberately reuses abds_seo_prepublish_threshold (see * ABDS_SEO_Rest_Controller::get_content_analysis()) as the poor/ok * boundary instead of a second constant, so "below the pre-publish * warning threshold" and "poor" always mean the same score, not two * numbers that could drift apart. * * @param ?int $score Content score, 0-100, or null if never analyzed. * @return string 'good', 'ok', 'poor', or 'not_analyzed'. */ public static function categorize_score( ?int $score ): string { if ( null === $score ) { return 'not_analyzed'; } /** This filter is documented in includes/api/class-abds-seo-rest-controller.php. */ $poor_threshold = (int) apply_filters( 'abds_seo_prepublish_threshold', 60 ); /** * Filters the content_score at/above which a page is categorized * "good" rather than "ok". Like abds_seo_prepublish_threshold, a * starting heuristic meant to be recalibrated once real usage data * exists. * * @param int $threshold Minimum content_score (0-100) for "good". Default 80. */ $good_threshold = (int) apply_filters( 'abds_seo_good_score_threshold', 80 ); if ( $score >= $good_threshold ) { return 'good'; } if ( $score >= $poor_threshold ) { return 'ok'; } return 'poor'; } /** * Combines weighted check results, density fit, and readability into a * 0-100 content score. Each check contributes its own CHECK_WEIGHTS * point value when passed (replacing the pre-A5 equal 1/6 split, see * CHECK_WEIGHTS's docblock) rather than the old flat percentage-of- * passed-checks model. * * @param array $checks Placement check results, keyed by check name. * @param float $density Keyword density percentage. * @param int $readability_score Readability sub-score, 0-100. * @return int Content score, 0-100. */ protected function calculate_score( array $checks, float $density, int $readability_score ): int { $checks_score = 0; foreach ( $checks as $key => $passed ) { if ( $passed ) { $checks_score += self::CHECK_WEIGHTS[ $key ] ?? 0; } } $density_score = ( $density >= self::DENSITY_MIN && $density <= self::DENSITY_MAX ) ? self::DENSITY_WEIGHT : self::DENSITY_WEIGHT / 2; $readability_weighted = ( $readability_score / 100 ) * self::READABILITY_WEIGHT; return (int) round( $checks_score + $density_score + $readability_weighted ); } /** * Builds the "what the score is made of" breakdown shown next to the * score in both editors (Task A6, ABDS-SEO-TZ-SCORE-WOW-093.md) — one * row per scoring input with its earned/possible points, in the exact * same units calculate_score() adds together, so the UI can never * silently drift out of sync with the actual formula. A bridge to the * R3 severity system: same shape (item, verdict, contribution) as the * planned audit-check breakdown, just scoped to content-score inputs * for now. * * Density and readability aren't boolean pass/fail like the CHECK_WEIGHTS * items — density is half-credit outside its ideal range rather than * zero, and readability scales smoothly — so both are represented the * same way as the boolean checks (points earned out of max) rather than * forcing an artificial pass/fail flag onto them; the UI derives a * checkmark/partial/empty state from points === max / points > 0 / 0. * * @param array $checks Placement check results, keyed by check name. * @param float $density Keyword density percentage. * @param int $readability_score Readability sub-score, 0-100. * @return array */ protected function build_score_breakdown( array $checks, float $density, int $readability_score ): array { $labels = array( 'title' => __( 'Keyword in the SEO title', 'abds-seo' ), 'h1' => __( 'Keyword in the H1 (page title)', 'abds-seo' ), 'first_100_words' => __( 'Keyword in the first 100 words', 'abds-seo' ), 'subheadings' => __( 'Keyword in a subheading', 'abds-seo' ), 'meta_description' => __( 'Keyword in the meta description', 'abds-seo' ), 'slug' => __( 'Keyword in the URL slug', 'abds-seo' ), 'image_alt' => __( 'Keyword in an image alt text', 'abds-seo' ), 'links' => __( 'Internal and external links', 'abds-seo' ), 'content_length' => __( 'Content length', 'abds-seo' ), ); $breakdown = array(); foreach ( $checks as $key => $passed ) { $max = self::CHECK_WEIGHTS[ $key ] ?? 0; $breakdown[] = array( 'key' => $key, 'label' => $labels[ $key ] ?? $key, 'points' => $passed ? $max : 0, 'max_points' => $max, ); } $density_in_range = ( $density >= self::DENSITY_MIN && $density <= self::DENSITY_MAX ); $breakdown[] = array( 'key' => 'density', 'label' => __( 'Keyword density in the ideal range', 'abds-seo' ), 'points' => $density_in_range ? self::DENSITY_WEIGHT : (int) ( self::DENSITY_WEIGHT / 2 ), 'max_points' => self::DENSITY_WEIGHT, ); $breakdown[] = array( 'key' => 'readability', 'label' => __( 'Readability', 'abds-seo' ), 'points' => (int) round( ( $readability_score / 100 ) * self::READABILITY_WEIGHT ), 'max_points' => self::READABILITY_WEIGHT, ); return $breakdown; } /** * Upserts the content score and recommendations into wp_abds_seo_page_scores. * * @param int $post_id Post ID. * @param int $score Content score, 0-100. * @param array $recommendations Recommendations to store. */ protected function persist_result( int $post_id, int $score, array $recommendations ): void { global $wpdb; $table = $wpdb->prefix . 'abds_seo_page_scores'; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $table is built only from $wpdb->prefix plus a hardcoded literal, never user input; $post_id is passed through $wpdb->prepare(). Runs once per editor keystroke-debounced analysis request, not a cacheable read path. $existing_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table} WHERE post_id = %d", $post_id ) ); // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $table is built only from $wpdb->prefix plus a hardcoded literal, never user input; $wpdb->update()/insert() safely escape their values via the format array. Runs once per editor keystroke-debounced analysis request, not a cacheable read path. if ( $existing_id ) { $wpdb->update( $table, array( 'content_score' => $score, 'last_scanned' => current_time( 'mysql' ), ), array( 'id' => $existing_id ), array( '%f', '%s' ), array( '%d' ) ); } else { $wpdb->insert( $table, array( 'post_id' => $post_id, 'content_score' => $score, 'issues_json' => wp_json_encode( array( 'content_recommendations' => $recommendations ) ), 'last_scanned' => current_time( 'mysql' ), ), array( '%d', '%f', '%s', '%s' ) ); } // phpcs:enable } }