counts(); if ( $c['posts'] > 0 || $c['terms'] > 0 ) { return true; } $titles = get_option( 'rank-math-options-titles' ); return is_array( $titles ) && ! empty( $titles ); } public function counts() { global $wpdb; static $memo = null; if ( null !== $memo ) { return $memo; } $out = array( 'posts' => 0, 'terms' => 0 ); $like = $this->like_prefix( self::META_PREFIX ); $out['posts'] = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT pm.post_id) FROM {$wpdb->postmeta} pm INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE pm.meta_key LIKE %s AND p.post_type NOT IN ('revision')", $like ) ); $out['terms'] = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT tm.term_id) FROM {$wpdb->termmeta} tm INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_id = tm.term_id WHERE tm.meta_key LIKE %s", $like ) ); $memo = $out; return $out; } public function import_posts_chunk( $after_id, $limit ) { global $wpdb; $out = $this->empty_result( $after_id ); $like = $this->like_prefix( self::META_PREFIX ); $posts = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT p.ID, p.post_type FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID AND pm.meta_key LIKE %s WHERE p.ID > %d AND p.post_type NOT IN ('revision') ORDER BY p.ID ASC LIMIT %d", $like, (int) $after_id, (int) $limit ), ARRAY_A ); $count = is_array( $posts ) ? count( $posts ) : 0; if ( ! $count ) { return $out; } $ids = array(); $types = array(); foreach ( $posts as $p ) { $id = (int) $p['ID']; $ids[] = $id; $types[ $id ] = (string) $p['post_type']; } $out['last_id'] = (int) end( $ids ); $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $args = $ids; $args[] = $like; $meta_rows = $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id IN ({$placeholders}) AND meta_key LIKE %s", $args ), ARRAY_A ); $by_post = array(); foreach ( (array) $meta_rows as $r ) { $by_post[ (int) $r['post_id'] ][ (string) $r['meta_key'] ] = $r['meta_value']; } foreach ( $ids as $id ) { if ( empty( $by_post[ $id ] ) || '' === $types[ $id ] ) { $out['skipped']++; continue; } list( $fields, $stripped ) = $this->map_meta( $by_post[ $id ], 'post' ); if ( ! $fields ) { $out['skipped']++; continue; } AIEO_SEO_Store::save( $types[ $id ], $id, $fields ); if ( $stripped ) { $out['stripped']++; } $out['imported']++; } $out['done'] = ( $count < (int) $limit ); return $out; } public function import_terms_chunk( $after_id, $limit ) { global $wpdb; $out = $this->empty_result( $after_id ); $like = $this->like_prefix( self::META_PREFIX ); $terms = $wpdb->get_results( $wpdb->prepare( "SELECT tm.term_id, MIN(tt.taxonomy) AS taxonomy FROM {$wpdb->termmeta} tm INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_id = tm.term_id WHERE tm.meta_key LIKE %s AND tm.term_id > %d GROUP BY tm.term_id ORDER BY tm.term_id ASC LIMIT %d", $like, (int) $after_id, (int) $limit ), ARRAY_A ); $count = is_array( $terms ) ? count( $terms ) : 0; if ( ! $count ) { return $out; } $ids = array(); $taxo = array(); foreach ( $terms as $t ) { $id = (int) $t['term_id']; $ids[] = $id; $taxo[ $id ] = (string) $t['taxonomy']; } $out['last_id'] = (int) end( $ids ); $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $args = $ids; $args[] = $like; $meta_rows = $wpdb->get_results( $wpdb->prepare( "SELECT term_id, meta_key, meta_value FROM {$wpdb->termmeta} WHERE term_id IN ({$placeholders}) AND meta_key LIKE %s", $args ), ARRAY_A ); $by_term = array(); foreach ( (array) $meta_rows as $r ) { $by_term[ (int) $r['term_id'] ][ (string) $r['meta_key'] ] = $r['meta_value']; } foreach ( $ids as $id ) { if ( empty( $by_term[ $id ] ) || '' === $taxo[ $id ] ) { $out['skipped']++; continue; } list( $fields, $stripped ) = $this->map_meta( $by_term[ $id ], 'term' ); if ( ! $fields ) { $out['skipped']++; continue; } AIEO_SEO_Store::save( $taxo[ $id ], $id, $fields ); if ( $stripped ) { $out['stripped']++; } $out['imported']++; } $out['done'] = ( $count < (int) $limit ); return $out; } private function map_meta( $meta, $page_type ) { $fields = array(); $stripped = false; $title = $this->tags( $this->val( $meta, 'rank_math_title' ), $page_type ); if ( '' !== $title ) { $fields['title'] = $title; } $desc = $this->tags( $this->val( $meta, 'rank_math_description' ), $page_type ); if ( '' !== $desc ) { $fields['description'] = $desc; } $canonical = trim( $this->val( $meta, 'rank_math_canonical_url' ) ); if ( '' !== $canonical ) { $fields['canonical_override'] = esc_url_raw( $canonical ); } $kw = trim( $this->val( $meta, 'rank_math_focus_keyword' ) ); if ( '' !== $kw ) { $parts = array_values( array_filter( array_map( 'trim', explode( ',', $kw ) ) ) ); $focus = array_shift( $parts ); $fields += $this->keyphrase_fields( (string) $focus, $parts ); } foreach ( array( 'rank_math_facebook_title' => 'og_title', 'rank_math_facebook_description' => 'og_description', 'rank_math_twitter_title' => 'twitter_title', 'rank_math_twitter_description' => 'twitter_description' ) as $key => $col ) { $v = $this->tags( $this->val( $meta, $key ), $page_type ); if ( '' !== $v ) { $fields[ $col ] = $v; } } $og_image = trim( $this->val( $meta, 'rank_math_facebook_image' ) ); if ( '' !== $og_image ) { $fields['og_image_custom_url'] = esc_url_raw( $og_image ); $fields['og_image_type'] = 'custom_image'; } $og_image_id = (int) $this->val( $meta, 'rank_math_facebook_image_id' ); if ( $og_image_id > 0 ) { $fields['og_image_id'] = $og_image_id; } $tw_image = trim( $this->val( $meta, 'rank_math_twitter_image' ) ); if ( '' !== $tw_image ) { $fields['twitter_image_custom_url'] = esc_url_raw( $tw_image ); $fields['twitter_image_type'] = 'custom_image'; } $use_fb = ( 'on' === $this->val( $meta, 'rank_math_twitter_use_facebook' ) ); if ( isset( $fields['twitter_title'] ) || isset( $fields['twitter_description'] ) || isset( $fields['twitter_image_custom_url'] ) ) { $use_fb = false; } $fields['twitter_use_og'] = $use_fb ? 1 : 0; $card = $this->val( $meta, 'rank_math_twitter_card_type' ); if ( '' !== $card ) { $fields['twitter_card'] = ( false !== stripos( $card, 'large' ) ) ? 'summary_large_image' : 'summary'; } $fields += $this->robots_from_meta( $meta ); $fields['pillar_content'] = ( 'on' === $this->val( $meta, 'rank_math_pillar_content' ) ) ? 1 : 0; $snippet = $this->val( $meta, 'rank_math_rich_snippet' ); if ( '' === $snippet ) { foreach ( array_keys( $meta ) as $key ) { if ( preg_match( '#^rank_math_schema_([A-Za-z]+)$#', (string) $key, $m ) ) { $snippet = $m[1]; break; } } } list( $our_type, $stripped ) = $this->normalise_schema_type( $snippet ); if ( '' !== $our_type ) { $fields['schema_type'] = $our_type; } if ( 'post' === $page_type ) { $primary = array(); foreach ( $meta as $key => $value ) { if ( preg_match( '#^rank_math_primary_([a-zA-Z0-9_\-]+)$#', (string) $key, $m ) ) { $primary[ $m[1] ] = (int) $value; } } $fields += $this->primary_term_fields( $primary ); } if ( ! $this->has_authored_meta( $meta ) ) { return array( array(), false ); } return array( $fields, $stripped ); } private function has_authored_meta( $meta ) { $authored = array( 'rank_math_title', 'rank_math_description', 'rank_math_canonical_url', 'rank_math_focus_keyword', 'rank_math_robots', 'rank_math_advanced_robots', 'rank_math_facebook_title', 'rank_math_facebook_description', 'rank_math_facebook_image', 'rank_math_twitter_title', 'rank_math_twitter_description', 'rank_math_twitter_image', 'rank_math_rich_snippet', ); foreach ( $authored as $k ) { if ( isset( $meta[ $k ] ) && '' !== trim( (string) $meta[ $k ] ) ) { return true; } } if ( 'on' === ( $meta['rank_math_pillar_content'] ?? '' ) ) { return true; } foreach ( array_keys( $meta ) as $k ) { if ( 0 === strpos( (string) $k, 'rank_math_primary_' ) && (int) $meta[ $k ] > 0 ) { return true; } if ( 0 === strpos( (string) $k, 'rank_math_schema_' ) ) { return true; } } return false; } private function robots_from_meta( $meta ) { $flags = array(); $explicit = false; $robots = maybe_unserialize( $this->val( $meta, 'rank_math_robots' ) ); if ( is_array( $robots ) && $robots ) { $explicit = true; $allowed = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex', 'noodp', 'notranslate' ); foreach ( $robots as $token ) { $token = strtolower( trim( (string) $token ) ); if ( in_array( $token, $allowed, true ) ) { $flags[ $token ] = true; } } } $out = $this->robots_fields( $explicit, $flags ); $adv = maybe_unserialize( $this->val( $meta, 'rank_math_advanced_robots' ) ); if ( is_array( $adv ) ) { if ( isset( $adv['max-snippet'] ) && is_numeric( $adv['max-snippet'] ) ) { $out['robots_max_snippet'] = (int) $adv['max-snippet']; $out['robots_default'] = 0; } if ( isset( $adv['max-video-preview'] ) && is_numeric( $adv['max-video-preview'] ) ) { $out['robots_max_videopreview'] = (int) $adv['max-video-preview']; $out['robots_default'] = 0; } if ( ! empty( $adv['max-image-preview'] ) ) { $v = strtolower( trim( (string) $adv['max-image-preview'] ) ); if ( in_array( $v, array( 'none', 'standard', 'large' ), true ) ) { $out['robots_max_imagepreview'] = $v; $out['robots_default'] = 0; } } } return $out; } private function val( $meta, $key ) { return isset( $meta[ $key ] ) ? (string) $meta[ $key ] : ''; } private function tags( $string, $page_type = 'post' ) { if ( '' === (string) $string ) { return ''; } $macros = array( '%sitename%' => '#site_title', '%blog_title%' => '#site_title', '%blog_description%' => '#tagline', '%sitedesc%' => '#tagline', '%sep%' => '#separator_sa', '%post_title%' => '#post_title', '%page_title%' => '#post_title', '%postname%' => '#post_title', '%seo_title%' => '#post_title', '%excerpt%' => '#post_excerpt', '%wc_shortdesc%' => '#post_excerpt', '%category%' => '#taxonomy_title', '%primary_category%' => '#taxonomy_title', '%term%' => '#taxonomy_title', '%term_description%' => '#taxonomy_description', '%category_description%' => '#taxonomy_description', '%tag_description%' => '#taxonomy_description', '%currentyear%' => '#current_year', '%year%' => '#current_year', '%search_query%' => '#search_term', ); $macros['%title%'] = ( 'term' === $page_type ) ? '#taxonomy_title' : '#post_title'; return $this->macros_to_tags( $string, $macros, '%[a-zA-Z_][a-zA-Z0-9_]*(\([^)]*\))?%' ); } public function import_options() { if ( ! class_exists( 'AIEO_DMM_Settings' ) ) { return array( 'imported' => false, 'post_types' => 0, 'taxonomies' => 0 ); } $titles = get_option( 'rank-math-options-titles' ); if ( ! is_array( $titles ) || ! $titles ) { return array( 'imported' => false, 'post_types' => 0, 'taxonomies' => 0 ); } $seo = (array) AIEO_DMM_Settings::get( 'seo', array() ); $sep = trim( (string) ( $titles['title_separator'] ?? '' ) ); if ( '' !== $sep ) { $d = html_entity_decode( $sep, ENT_QUOTES, 'UTF-8' ); $seo['separator'] = '' !== trim( $d ) ? $d : '-'; } $kg_type = (string) ( $titles['knowledgegraph_type'] ?? '' ); if ( '' !== $kg_type ) { $seo['org_represents'] = ( 'person' === $kg_type ) ? 'person' : 'organization'; } $kg_name = trim( (string) ( $titles['knowledgegraph_name'] ?? '' ) ); if ( '' !== $kg_name ) { $seo['org_name'] = $this->tags( $kg_name ); } $kg_logo = trim( (string) ( $titles['knowledgegraph_logo'] ?? '' ) ); if ( '' !== $kg_logo ) { $seo['org_logo'] = esc_url_raw( $kg_logo ); } $phone = trim( (string) ( $titles['phone'] ?? ( $titles['phone_number'] ?? '' ) ) ); if ( '' !== $phone ) { $seo['org_phone'] = $phone; } $same = array(); foreach ( array( 'social_url_facebook', 'social_url_instagram', 'social_url_linkedin', 'social_url_youtube', 'social_url_pinterest', 'social_url_tiktok' ) as $k ) { $u = trim( (string) ( $titles[ $k ] ?? '' ) ); if ( '' !== $u ) { $same[] = esc_url_raw( $u ); } } $handle = trim( (string) ( $titles['twitter_author_names'] ?? '' ) ); if ( '' !== $handle ) { $same[] = ( 0 === strpos( $handle, 'http' ) ) ? esc_url_raw( $handle ) : 'https://x.com/' . ltrim( $handle, '@' ); } $extra = (string) ( $titles['additional_profiles'] ?? '' ); if ( '' !== trim( $extra ) ) { foreach ( preg_split( '/\s+/', $extra ) as $u ) { $u = trim( (string) $u ); if ( '' !== $u ) { $same[] = esc_url_raw( $u ); } } } if ( $same ) { $seo['social_urls'] = array_values( array_unique( array_filter( $same ) ) ); } $og_img = trim( (string) ( $titles['open_graph_image'] ?? '' ) ); if ( '' !== $og_img ) { $seo['og_default_image'] = esc_url_raw( $og_img ); } $existing = AIEO_DMM_Settings::get( 'seo.robots_global', array() ); $existing = is_array( $existing ) ? $existing : array(); $global_robots = maybe_unserialize( $titles['robots_global'] ?? '' ); $global_custom = ( 'on' === (string) ( $titles['robots_global_custom'] ?? '' ) ); $seo['robots_global'] = array_merge( $existing, array( 'noindex' => $global_custom && is_array( $global_robots ) && in_array( 'noindex', array_map( 'strval', $global_robots ), true ), 'noindex_paginated' => ( 'on' === (string) ( $titles['noindex_paginated_pages'] ?? '' ) ), ) ); $seo['post_types'] = array(); foreach ( get_post_types( array( 'public' => true ), 'names' ) as $type ) { $cfg = $this->type_cfg( $titles, 'pt_' . $type . '_', 'post' ); if ( $cfg ) { $seo['post_types'][ $type ] = $cfg; } } $seo['taxonomies'] = array(); foreach ( get_taxonomies( array( 'public' => true ), 'names' ) as $tax ) { $cfg = $this->type_cfg( $titles, 'tax_' . $tax . '_', 'term' ); if ( $cfg ) { $seo['taxonomies'][ $tax ] = $cfg; } } $all = AIEO_DMM_Settings::get_all(); $all['seo'] = $seo; AIEO_DMM_Settings::replace( $all ); return array( 'imported' => true, 'post_types' => count( $seo['post_types'] ), 'taxonomies' => count( $seo['taxonomies'] ), ); } private function type_cfg( $titles, $prefix, $page_type ) { $title = $prefix . 'title'; $desc = $prefix . 'description'; $custom = $prefix . 'custom_robots'; $robots = $prefix . 'robots'; if ( ! array_key_exists( $title, $titles ) && ! array_key_exists( $desc, $titles ) && ! array_key_exists( $robots, $titles ) ) { return null; } $noindex = false; if ( 'on' === (string) ( $titles[ $custom ] ?? '' ) ) { $list = maybe_unserialize( $titles[ $robots ] ?? '' ); if ( is_array( $list ) ) { $noindex = in_array( 'noindex', array_map( 'strval', $list ), true ); } } $schema = ''; $snippet = (string) ( $titles[ $prefix . 'default_rich_snippet' ] ?? ( $titles[ $prefix . 'default_snippet' ] ?? '' ) ); if ( '' !== $snippet ) { list( $schema ) = $this->normalise_schema_type( $snippet ); } return array( 'title' => $this->tags( (string) ( $titles[ $title ] ?? '' ), $page_type ), 'description' => $this->tags( (string) ( $titles[ $desc ] ?? '' ), $page_type ), 'schema_type' => $schema, 'noindex' => (bool) $noindex, ); } public function has_redirects() { global $wpdb; $t = $wpdb->prefix . 'rank_math_redirections'; return (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) === $t; } public function import_redirects() { if ( ! class_exists( 'AIEO_SEO_Redirects' ) ) { return parent::import_redirects(); } return AIEO_SEO_Redirects::import_rankmath(); } public function mapping_report() { return array( 'rank_math_title / _description' => array( 'status' => 'transformed', 'detail' => 'Rank Math %macros% converted to our #smart-tags; macros with no equivalent (%page%, %customfield(x)%, %modified%) are REMOVED rather than carried' ), 'rank_math_canonical_url' => array( 'status' => 'transformed', 'detail' => '→ canonical_override (our canonical_url is the materialised path)' ), 'rank_math_focus_keyword' => array( 'status' => 'transformed', 'detail' => 'comma list; first = focus keyphrase, rest → keyphrases JSON' ), 'rank_math_robots' => array( 'status' => 'transformed', 'detail' => 'serialized token list → our seven flags, WHITELISTED (an unknown token is ignored, never used as a column name); the "index" token is the absence of noindex and stores nothing' ), 'rank_math_advanced_robots' => array( 'status' => 'mapped', 'detail' => 'max-snippet / max-video-preview / max-image-preview' ), 'rank_math_primary_{taxonomy}' => array( 'status' => 'transformed', 'detail' => '→ primary_term JSON + flat primary_cat_id (every taxonomy, not just category)' ), 'rank_math_facebook_* / twitter_*' => array( 'status' => 'transformed', 'detail' => 'custom URL + id copied; card type normalised to summary / summary_large_image; "use Facebook" is overridden by any Twitter-specific value the operator typed' ), 'rank_math_rich_snippet / _schema_*' => array( 'status' => 'transformed', 'detail' => 'both storage generations read; mapped to Article / Product / WebPage' ), 'FAQ / HowTo schema' => array( 'status' => 'dropped', 'detail' => 'STRIPPED to WebPage — no longer rich-result-eligible (FAQ ended 2026-05-07)' ), 'rank_math_pillar_content' => array( 'status' => 'mapped', 'detail' => "'on' → pillar_content (AIEO also auto-derives pillars from performance)" ), 'term meta (rank_math_* on terms)' => array( 'status' => 'mapped', 'detail' => 'same keys, same mapper; terms whose taxonomy no longer exists are skipped' ), 'rank_math_seo_score / _internal_links_processed' => array( 'status' => 'dropped', 'detail' => 'recomputed by our own demand-aware score' ), 'rank_math_breadcrumb_title' => array( 'status' => 'dropped', 'detail' => 'our breadcrumbs use the object title' ), 'rank-math-options-titles' => array( 'status' => 'mapped', 'detail' => 'separator, knowledge graph (name/logo/type), social profiles → sameAs, default OG image, per-type + per-taxonomy formats, schema type and the EFFECTIVE noindex (only honoured when that type\'s custom_robots is on)' ), 'rank_math_redirections' => array( 'status' => 'deferred', 'detail' => 'imported by the Redirects tool — validated (target must resolve, no loops) before entering' ), '404 monitor / Analytics / Instant Indexing' => array( 'status' => 'dropped', 'detail' => 'not migrated (out of scope; AIEO has its own 404 log)' ), ); } }