> */ public static function sources(): array { return array( 'yoast' => array( 'label' => 'Yoast SEO', 'title' => '_yoast_wpseo_title', 'desc' => '_yoast_wpseo_metadesc', 'index' => '_yoast_wpseo_meta-robots-noindex', 'keyword' => '_yoast_wpseo_focuskw', 'canonical' => '_yoast_wpseo_canonical', 'og_title' => '_yoast_wpseo_opengraph-title', 'og_desc' => '_yoast_wpseo_opengraph-description', 'og_image' => '_yoast_wpseo_opengraph-image', 'tw_title' => '_yoast_wpseo_twitter-title', 'tw_desc' => '_yoast_wpseo_twitter-description', 'tw_image' => '_yoast_wpseo_twitter-image', ), 'rankmath' => array( 'label' => 'Rank Math', 'title' => 'rank_math_title', 'desc' => 'rank_math_description', 'index' => 'rank_math_robots', 'keyword' => 'rank_math_focus_keyword', 'canonical' => 'rank_math_canonical_url', 'og_title' => 'rank_math_facebook_title', 'og_desc' => 'rank_math_facebook_description', 'og_image' => 'rank_math_facebook_image', 'tw_title' => 'rank_math_twitter_title', 'tw_desc' => 'rank_math_twitter_description', 'tw_image' => 'rank_math_twitter_image', ), 'aioseo' => array( 'label' => 'All in One SEO', 'title' => '_aioseo_title', 'desc' => '_aioseo_description', 'index' => '', ), 'seopress' => array( 'label' => 'SEOPress', 'title' => '_seopress_titles_title', 'desc' => '_seopress_titles_desc', 'index' => '_seopress_robots_index', 'keyword' => '_seopress_analysis_target_kw', 'canonical' => '_seopress_robots_canonical', 'og_title' => '_seopress_social_fb_title', 'og_desc' => '_seopress_social_fb_desc', 'og_image' => '_seopress_social_fb_img', 'tw_title' => '_seopress_social_twitter_title', 'tw_desc' => '_seopress_social_twitter_desc', 'tw_image' => '_seopress_social_twitter_img', ), 'seoframework' => array( 'label' => 'The SEO Framework', 'title' => '_genesis_title', 'desc' => '_genesis_description', 'index' => '_genesis_noindex', ), 'seoultimate' => array( 'label' => 'SEO Ultimate', 'title' => '_su_title', 'desc' => '_su_description', 'index' => '_su_meta_robots_noindex', ), // Slim SEO keeps title+description together in one array meta // ('slim_seo'); source_value() unpacks it. 'slimseo' => array( 'label' => 'Slim SEO', 'title' => '', 'desc' => '', 'index' => '', ), 'smartcrawl' => array( 'label' => 'SmartCrawl', 'title' => '_wds_title', 'desc' => '_wds_metadesc', 'index' => '_wds_meta-robots-noindex', ), 'wpmetaseo' => array( 'label' => 'WP Meta SEO', 'title' => '_metaseo_metatitle', 'desc' => '_metaseo_metadesc', 'index' => '', ), ); } /** * How much data each source holds on this site. * * @return array> */ public function detect(): array { global $wpdb; $out = array(); foreach ( self::sources() as $id => $source ) { $posts = 0; foreach ( array( $source['title'], $source['desc'] ) as $key ) { if ( '' === $key ) { continue; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT post_id) FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value <> ''", $key ) ); $posts = max( $posts, $count ); } // Slim SEO keeps everything in one array meta. if ( 'slimseo' === $id ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $posts = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT post_id) FROM %i WHERE meta_key = 'slim_seo' AND meta_value <> '' AND meta_value <> 'a:0:{}'", $wpdb->postmeta ) ); } // AIOSEO v4 keeps its data in a custom table. if ( 'aioseo' === $id ) { $table = $wpdb->prefix . 'aioseo_posts'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $posts = max( $posts, (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE title <> '' OR description <> ''", $table ) ) ); } } $out[] = array( 'id' => $id, 'label' => $source['label'], 'posts' => $posts, 'redirects' => $this->count_redirects( $id ), 'active' => $this->is_active( $id ), ); } return $out; } private function is_active( string $id ): bool { return match ( $id ) { 'yoast' => defined( 'WPSEO_VERSION' ), 'rankmath' => class_exists( 'RankMath' ), 'aioseo' => defined( 'AIOSEO_VERSION' ), 'seopress' => defined( 'SEOPRESS_VERSION' ), 'seoframework' => defined( 'THE_SEO_FRAMEWORK_VERSION' ), 'seoultimate' => class_exists( 'SEO_Ultimate' ), 'slimseo' => defined( 'SLIM_SEO_VER' ), 'smartcrawl' => defined( 'SMARTCRAWL_VERSION' ) || class_exists( 'Smartcrawl_Loader' ), 'wpmetaseo' => defined( 'WPMSEO_VERSION' ), default => false, }; } /** * Redirect rows available for import. */ private function count_redirects( string $id ): int { global $wpdb; $table = match ( $id ) { 'rankmath' => $wpdb->prefix . 'rank_math_redirections', 'seopress' => '', default => '', }; if ( '' === $table ) { return 0; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) !== $table ) { return 0; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE status = 'active'", $table ) ); } /** * Import one batch of posts. * * @param string $id Source id. * @param bool $overwrite Overwrite values we already have. * @param int $offset Batch offset. * @param int $limit Batch size. * @return array{imported: int, skipped: int, processed: int} */ /** * Import one batch of posts. * * @param int $offset Kept for callers that report progress; not used to page. * @param int $limit Batch size. * @param int|null $after Keyset cursor: import posts with ID greater than this. * null means "start at the beginning" — OFFSET paging * reads and discards every earlier row on each call, * which on 50,000 posts is the whole table by the end. * @return array{imported:int, skipped:int, processed:int, fields?:array, last_id:int} */ public function import_posts( string $id, bool $overwrite, int $offset, int $limit = 50, ?int $after = null ): array { global $wpdb; $sources = self::sources(); if ( ! isset( $sources[ $id ] ) ) { return array( 'imported' => 0, 'skipped' => 0, 'processed' => 0, 'last_id' => (int) $after, ); } $source = $sources[ $id ]; unset( $offset ); $types = array_values( array_diff( get_post_types(), array( 'attachment', 'revision', 'nav_menu_item' ) ) ); $in = implode( ',', array_fill( 0, count( $types ), '%s' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE ID > %d AND post_type IN ({$in}) AND post_status NOT IN ('auto-draft','trash') ORDER BY ID ASC LIMIT %d", ...array_merge( array( (int) $after ), $types, array( max( 1, $limit ) ) ) ) ); $posts = array(); foreach ( (array) $ids as $pid ) { $p = get_post( (int) $pid ); if ( $p instanceof \WP_Post ) { $posts[] = $p; } } $last_id = (int) $after; $imported = 0; $skipped = 0; $fields = array( 'title' => 0, 'desc' => 0, 'keyword' => 0, 'canonical' => 0, 'noindex' => 0, 'social' => 0 ); foreach ( $posts as $post ) { $last_id = max( $last_id, (int) $post->ID ); $changed = false; $may = fn( string $our ) => $overwrite || '' === (string) get_post_meta( $post->ID, $our, true ); $title = $this->source_value( $id, $post->ID, $source['title'], 'title' ); if ( '' !== $title && $may( '_airanksuite_seo_title' ) ) { update_post_meta( $post->ID, '_airanksuite_seo_title', $this->translate_vars( $title ) ); $changed = true; ++$fields['title']; } $desc = $this->source_value( $id, $post->ID, $source['desc'], 'description' ); if ( '' !== $desc && $may( '_airanksuite_seo_desc' ) ) { update_post_meta( $post->ID, '_airanksuite_seo_desc', $this->translate_vars( $desc ) ); $changed = true; ++$fields['desc']; } // Keywords: Rank Math stores a comma list ("main, second, third"), // Yoast the focus keyphrase plus related ones in a JSON blob. All of // them land in our short-tail group; the first becomes the focus. $kw = $this->source_value( $id, $post->ID, (string) ( $source['keyword'] ?? '' ), 'keyword' ); $list = array_values( array_filter( array_map( 'trim', explode( ',', $kw ) ) ) ); if ( 'yoast' === $id ) { $related = get_post_meta( $post->ID, '_yoast_wpseo_focuskeywords', true ); $decoded = is_string( $related ) && '' !== $related ? json_decode( $related, true ) : null; foreach ( (array) $decoded as $r ) { if ( is_array( $r ) && ! empty( $r['keyword'] ) ) { $list[] = trim( (string) $r['keyword'] ); } } } $list = array_values( array_unique( array_filter( $list ) ) ); if ( ! empty( $list ) ) { $current = \AiRankSuite\Modules\ContentAnalyzer\Services\KeywordAnalyzer::get( $post->ID ); $have = (array) ( $current['shorttail'] ?? array() ); if ( $overwrite || empty( $have ) ) { $current['shorttail'] = $overwrite ? array_values( array_unique( array_merge( $list, $have ) ) ) : $list; \AiRankSuite\Modules\ContentAnalyzer\Services\KeywordAnalyzer::save( $post->ID, $current ); $changed = true; ++$fields['keyword']; } } $canonical = $this->source_value( $id, $post->ID, (string) ( $source['canonical'] ?? '' ), 'canonical' ); if ( '' !== $canonical && $may( \AiRankSuite\Support\PostSeo::CANONICAL ) ) { $url = esc_url_raw( $canonical ); if ( '' !== $url ) { update_post_meta( $post->ID, \AiRankSuite\Support\PostSeo::CANONICAL, $url ); $changed = true; ++$fields['canonical']; } } // The robots meta the head output and the editor read is `_airanksuite_robots` // (an array of directives) — an imported noindex goes there, so it // takes effect on the page and shows in the editor. $robots = get_post_meta( $post->ID, '_airanksuite_robots', true ); $robots = is_array( $robots ) ? array_values( array_map( 'strval', $robots ) ) : array(); if ( $this->source_noindex( $id, $post->ID, (string) ( $source['index'] ?? '' ) ) && ( $overwrite || ! in_array( 'noindex', $robots, true ) ) ) { if ( ! in_array( 'noindex', $robots, true ) ) { $robots[] = 'noindex'; update_post_meta( $post->ID, '_airanksuite_robots', $robots ); } $changed = true; ++$fields['noindex']; } // Social: only the keys the source has; the rest keeps our fallbacks. $social = array(); foreach ( array( 'og_title', 'og_desc', 'og_image', 'tw_title', 'tw_desc', 'tw_image' ) as $sk ) { $v = $this->source_value( $id, $post->ID, (string) ( $source[ $sk ] ?? '' ), $sk ); if ( '' !== $v ) { $social[ $sk ] = $v; } } if ( ! empty( $social ) ) { $current = \AiRankSuite\Support\PostSeo::social( $post->ID ); $merged = $current; $touched = false; foreach ( $social as $sk => $v ) { if ( $overwrite || '' === (string) ( $current[ $sk ] ?? '' ) ) { $merged[ $sk ] = $v; $touched = true; } } if ( $touched ) { \AiRankSuite\Support\PostSeo::save_social( $post->ID, $merged ); $changed = true; ++$fields['social']; } } if ( $changed ) { ++$imported; } else { ++$skipped; } } return array( 'imported' => $imported, 'skipped' => $skipped, 'processed' => count( $posts ), 'fields' => $fields, 'last_id' => $last_id, ); } /** * Read a value from the source plugin (meta, or AIOSEO's table). */ private function source_value( string $id, int $post_id, string $meta_key, string $column ): string { if ( 'slimseo' === $id ) { $bundle = get_post_meta( $post_id, 'slim_seo', true ); if ( is_array( $bundle ) ) { $value = (string) ( $bundle[ 'title' === $column ? 'title' : 'description' ] ?? '' ); return trim( $value ); } return ''; } if ( 'aioseo' === $id ) { global $wpdb; // The column travels as an identifier (%i), and only from this allowlist. if ( ! in_array( $column, array( 'title', 'description' ), true ) ) { return ''; } $table = $wpdb->prefix . 'aioseo_posts'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $value = $wpdb->get_var( $wpdb->prepare( 'SELECT %i FROM %i WHERE post_id = %d', $column, $table, $post_id ) ); if ( is_string( $value ) && '' !== $value ) { return $value; } } } if ( '' === $meta_key ) { return ''; } $value = get_post_meta( $post_id, $meta_key, true ); return is_string( $value ) ? trim( $value ) : ''; } /** * Whether the source marks this post noindex. Each plugin encodes it * differently: Rank Math a serialized array of directives, Yoast '1', * SEOPress 'yes', SEO Framework '1'. */ private function source_noindex( string $id, int $post_id, string $meta_key ): bool { if ( '' === $meta_key ) { return false; } $raw = get_post_meta( $post_id, $meta_key, true ); if ( is_array( $raw ) ) { return in_array( 'noindex', array_map( 'strval', $raw ), true ); } $v = strtolower( trim( (string) $raw ) ); if ( 'seopress' === $id ) { return 'yes' === $v; } return in_array( $v, array( '1', 'yes', 'noindex', 'true' ), true ); } /** * Site-wide settings the source keeps in options: title and description * templates per post type, the separator, and the home page title. This * is the part that "only three pages imported" hides — a Rank Math site * has one template for thousands of posts and stores nothing per post. * * @return array{applied: int, templates: array} */ public function import_site_settings( string $id, bool $overwrite ): array { $applied = 0; $templates = array(); $options = \AiRankSuite\Kernel::service( \AiRankSuite\Support\Options::class ); $seo = $options->get( 'seo', \AiRankSuite\Modules\Seo\SeoModule::defaults() ); $types = \AiRankSuite\Support\Inventory::eligible_types(); $title_tpl = ''; $sep = ''; $home = ''; if ( 'rankmath' === $id ) { $rm = get_option( 'rank-math-options-titles', array() ); $rm = is_array( $rm ) ? $rm : array(); $sep_map = array( 'sep-hyphen' => '-', 'sep-dash' => '–', 'sep-pipe' => '|', 'sep-bullet' => '•', 'sep-raquo' => '»', 'sep-mid' => '·', 'sep-colon' => ':', 'sep-tilde' => '~' ); $sep = (string) ( $sep_map[ (string) ( $rm['title_separator'] ?? '' ) ] ?? ( $rm['title_separator'] ?? '' ) ); $home = (string) ( $rm['homepage_title'] ?? '' ); foreach ( $types as $t ) { $tt = (string) ( $rm[ 'pt_' . $t . '_title' ] ?? '' ); $td = (string) ( $rm[ 'pt_' . $t . '_description' ] ?? '' ); if ( '' !== $tt || '' !== $td ) { $templates[ $t ] = array( 'title' => $this->translate_vars( $tt ), 'desc' => $this->translate_vars( $td ) ); } } $title_tpl = (string) ( $rm['pt_post_title'] ?? '' ); } elseif ( 'yoast' === $id ) { $y = get_option( 'wpseo_titles', array() ); $y = is_array( $y ) ? $y : array(); $sep_map = array( 'sc-dash' => '-', 'sc-ndash' => '–', 'sc-mdash' => '—', 'sc-middot' => '·', 'sc-bull' => '•', 'sc-star' => '*', 'sc-smstar' => '⋆', 'sc-pipe' => '|', 'sc-tilde' => '~', 'sc-laquo' => '«', 'sc-raquo' => '»', 'sc-lt' => '<', 'sc-gt' => '>' ); $sep = (string) ( $sep_map[ (string) ( $y['separator'] ?? '' ) ] ?? '' ); $home = (string) ( $y['title-home-wpseo'] ?? '' ); foreach ( $types as $t ) { $tt = (string) ( $y[ 'title-' . $t ] ?? '' ); $td = (string) ( $y[ 'metadesc-' . $t ] ?? '' ); if ( '' !== $tt || '' !== $td ) { $templates[ $t ] = array( 'title' => $this->translate_vars( $tt ), 'desc' => $this->translate_vars( $td ) ); } } $title_tpl = (string) ( $y['title-post'] ?? '' ); } elseif ( 'seopress' === $id ) { $sp = get_option( 'seopress_titles_option_name', array() ); $sp = is_array( $sp ) ? $sp : array(); $sep = (string) ( $sp['seopress_titles_sep'] ?? '' ); $home = (string) ( $sp['seopress_titles_home_site_title'] ?? '' ); $single = (array) ( $sp['seopress_titles_single_titles'] ?? array() ); foreach ( $types as $t ) { $tt = (string) ( $single[ $t ]['title'] ?? '' ); $td = (string) ( $single[ $t ]['description'] ?? '' ); if ( '' !== $tt || '' !== $td ) { $templates[ $t ] = array( 'title' => $this->translate_vars( $tt ), 'desc' => $this->translate_vars( $td ) ); } } $title_tpl = (string) ( $single['post']['title'] ?? '' ); } $values = array(); if ( '' !== $sep && ( $overwrite || (string) ( $seo['separator'] ?? '' ) === (string) ( \AiRankSuite\Modules\Seo\SeoModule::defaults()['separator'] ?? '' ) ) ) { $values['separator'] = mb_substr( $sep, 0, 3 ); ++$applied; } if ( '' !== $home && ( $overwrite || '' === trim( (string) ( $seo['home_title'] ?? '' ) ) ) ) { $values['home_title'] = $this->translate_vars( $home ); ++$applied; } if ( '' !== $title_tpl && ( $overwrite || (string) ( $seo['title_template'] ?? '' ) === (string) ( \AiRankSuite\Modules\Seo\SeoModule::defaults()['title_template'] ?? '' ) ) ) { $tpl = $this->translate_vars( $title_tpl ); if ( '' !== $tpl ) { $values['title_template'] = $tpl; ++$applied; } } if ( ! empty( $templates ) ) { $current = (array) ( $seo['templates'] ?? array() ); foreach ( $templates as $t => $pair ) { foreach ( array( 'title', 'desc' ) as $k ) { if ( '' === $pair[ $k ] ) { continue; } if ( $overwrite || '' === trim( (string) ( $current[ $t ][ $k ] ?? '' ) ) ) { $current[ $t ][ $k ] = $pair[ $k ]; ++$applied; } } } $values['templates'] = $current; } if ( ! empty( $values ) ) { $options->set( 'seo', $values, \AiRankSuite\Modules\Seo\SeoModule::defaults() ); } return array( 'applied' => $applied, 'templates' => $templates ); } /** * Translate another plugin's template variables into this plugin's. * * The map lives in Variables now, shared with the adapters — two copies of * a 120-entry table is how one of them ends up missing %excerpt%. */ private function translate_vars( string $value ): string { return Variables::translate( $value ); } /** * @return array */ public static function known_variables(): array { return Variables::known(); } /** * Import redirects from a source plugin. * * @return array{imported: int, skipped: int} */ public function import_redirects( string $id ): array { global $wpdb; $imported = 0; $skipped = 0; if ( 'rankmath' !== $id ) { return array( 'imported' => 0, 'skipped' => 0, ); } $table = $wpdb->prefix . 'rank_math_redirections'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) !== $table ) { return array( 'imported' => 0, 'skipped' => 0, ); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $rows = $wpdb->get_results( $wpdb->prepare( "SELECT sources, url_to, header_code FROM %i WHERE status = 'active' LIMIT 500", $table ) ); $now = current_time( 'mysql', true ); foreach ( (array) $rows as $row ) { $sources = maybe_unserialize( $row->sources ); if ( ! is_array( $sources ) ) { ++$skipped; continue; } foreach ( $sources as $source ) { $pattern = is_array( $source ) ? (string) ( $source['pattern'] ?? '' ) : (string) $source; if ( '' === $pattern ) { ++$skipped; continue; } $path = \AiRankSuite\Modules\Redirects\RedirectsModule::normalize( $pattern ); if ( '/' === $path ) { ++$skipped; continue; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $result = $wpdb->replace( Database::redirects(), array( 'source' => mb_substr( $path, 0, 191 ), 'target' => (string) $row->url_to, 'type' => in_array( (int) $row->header_code, array( 301, 302, 307, 410 ), true ) ? (int) $row->header_code : 301, 'status' => 'active', 'created_at' => $now, ), array( '%s', '%s', '%d', '%s', '%s' ) ); if ( false !== $result ) { ++$imported; } else { ++$skipped; } } } return array( 'imported' => $imported, 'skipped' => $skipped, ); } /** * Total posts (for progress reporting). */ public function total_posts(): int { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE post_type NOT IN ('attachment','revision','nav_menu_item')", $wpdb->posts ) ); } }