get_seo_plugin_name(); $data = [ 'product_id' => $product_id, 'seo_plugin' => $plugin, 'seo_title' => '', 'seo_description' => '', 'focus_keyword' => '', 'seo_score' => null, 'readability_score' => null, ]; switch ($plugin) { case 'yoast': $data['seo_title'] = get_post_meta($product_id, '_yoast_wpseo_title', true) ?: ''; $data['seo_description'] = get_post_meta($product_id, '_yoast_wpseo_metadesc', true) ?: ''; $data['focus_keyword'] = get_post_meta($product_id, '_yoast_wpseo_focuskw', true) ?: ''; $seo_score = get_post_meta($product_id, '_yoast_wpseo_linkdex', true); $data['seo_score'] = $seo_score !== '' ? intval($seo_score) : null; $readability = get_post_meta($product_id, '_yoast_wpseo_content_score', true); $data['readability_score'] = $readability !== '' ? intval($readability) : null; break; case 'rankmath': $data['seo_title'] = get_post_meta($product_id, 'rank_math_title', true) ?: ''; $data['seo_description'] = get_post_meta($product_id, 'rank_math_description', true) ?: ''; $data['focus_keyword'] = get_post_meta($product_id, 'rank_math_focus_keyword', true) ?: ''; $seo_score = get_post_meta($product_id, 'rank_math_seo_score', true); $data['seo_score'] = $seo_score !== '' ? intval($seo_score) : null; break; case 'seopress': $data['seo_title'] = get_post_meta($product_id, '_seopress_titles_title', true) ?: ''; $data['seo_description'] = get_post_meta($product_id, '_seopress_titles_desc', true) ?: ''; $data['focus_keyword'] = get_post_meta($product_id, '_seopress_analysis_target_kw', true) ?: ''; break; default: // No SEO plugin — fall back to post title and excerpt $post = get_post($product_id); if ($post) { $data['seo_title'] = $post->post_title ?: ''; $data['seo_description'] = wp_trim_words(wp_strip_all_tags($post->post_excerpt ?: $post->post_content), 30, '...'); } break; } // If SEO title is empty, fall back to post title if (empty($data['seo_title'])) { $post = get_post($product_id); if ($post) { $data['seo_title'] = $post->post_title; } } return $data; } /** * Get SEO data for all WooCommerce products. * @return array */ public function get_all_products_seo(): array { if (!class_exists('WooCommerce')) { return []; } $products = wc_get_products([ 'status' => 'publish', 'limit' => -1, 'return' => 'ids', ]); $seo_data = []; foreach ($products as $product_id) { $seo_data[] = $this->get_product_seo_data($product_id); } return $seo_data; } }