*/ public function get_keywords_for( int $post_id, string $post_type = 'post' ): array { global $wpdb; if ( $post_id <= 0 || 'post' !== $post_type || ! isset( $wpdb ) ) { return array(); } $table = $wpdb->prefix . 'aioseo_posts'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- AIOSEO has no public API for keyphrases; direct query is the only option. $row = $wpdb->get_row( $wpdb->prepare( 'SELECT keyphrases FROM %i WHERE post_id = %d', $table, $post_id ) ); if ( ! is_object( $row ) || ! isset( $row->keyphrases ) || '' === $row->keyphrases ) { return array(); } $data = json_decode( (string) $row->keyphrases, true ); if ( ! is_array( $data ) ) { return array(); } $out = array(); // Primary focus keyphrase. $focus = $data['focus']['keyphrase'] ?? ''; if ( is_string( $focus ) && '' !== trim( $focus ) ) { $out[] = trim( $focus ); } // Additional keyphrases. $additional = $data['additional'] ?? array(); if ( is_array( $additional ) ) { foreach ( $additional as $item ) { if ( ! is_array( $item ) ) { continue; } $kw = trim( (string) ( $item['keyphrase'] ?? '' ) ); if ( '' !== $kw && ! in_array( $kw, $out, true ) ) { $out[] = $kw; } } } return $out; } }