h_title', true ); $title = $title ? $title : Paper::get_from_options( "pt_{$post_type}_title", $post, '%title% %sep% %sitename%' ); $keywords = array_map( 'trim', explode( ',', Helper::get_post_meta( 'focus_keyword', $post_id ) ) ); $keyword = $keywords[0]; $values = [ 'title' => Helper::replace_vars( '%seo_title%', $post ), 'description' => Helper::replace_vars( '%seo_description%', $post ), 'keywords' => $keywords, 'keyword' => $keyword, 'content' => wpautop( $post->post_content ), 'url' => urldecode( get_the_permalink( $post_id ) ), 'hasContentAi' => ! empty( Helper::get_post_meta( 'contentai_score', $post_id ) ), ]; if ( has_post_thumbnail( $post_id ) ) { $thumbnail_id = get_post_thumbnail_id( $post_id ); $values['thumbnail'] = get_the_post_thumbnail_url( $post_id ); $values['thumbnailAlt'] = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true ); } /** * Filter the values sent to the analyzer to calculate the SEO score. * * @param array $values The values to be sent to the analyzer. */ $data[ $post_id ] = $this->do_filter( 'recalculate_score/data', $values, $post_id ); } return $data; } /** * Ensure only one instance is loaded or can be loaded. * * @return Update_Score */ public static function get() { static $instance; if ( is_null( $instance ) && ! ( $instance instanceof Update_Score ) ) { $instance = new Update_Score(); } return $instance; } /** * Find posts with focus keyword but no SEO score. * * @param bool $update_all Whether to update all posts or only those without a score. * @return int */ public function find( $update_all = true ) { global $wpdb; $post_types = $this->get_post_types(); $placeholder = implode( ', ', array_fill( 0, count( $post_types ), '%s' ) ); $query = "SELECT COUNT(ID) FROM {$wpdb->posts} as p LEFT JOIN {$wpdb->postmeta} as pm ON p.ID = pm.post_id AND pm.meta_key = 'rank_math_focus_keyword' WHERE p.post_type IN ({$placeholder}) AND p.post_status = 'publish' AND pm.meta_value != ''"; if ( ! $update_all ) { $query .= " AND (SELECT COUNT(*) FROM {$wpdb->postmeta} as pm2 WHERE pm2.post_id = p.ID AND pm2.meta_key = 'rank_math_seo_score' AND pm2.meta_value != '') = 0"; } $update_score_post_ids = $wpdb->get_var( $wpdb->prepare( $query, $post_types ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- It's prepared above. return (int) $update_score_post_ids; } /** * Get post types. * * @return array */ private function get_post_types() { $post_types = get_post_types( [ 'public' => true ] ); if ( isset( $post_types['attachment'] ) ) { unset( $post_types['attachment'] ); } return $this->do_filter( 'tool/post_types', array_keys( $post_types ) ); } }