get_var( "SELECT COUNT(DISTINCT post_id) FROM {$wpdb->postmeta} WHERE meta_key = 'rank_math_title'" ); if ( $rm > 0 ) { $plugins[] = array( 'id' => 'rankmath', 'name' => 'Rank Math SEO', 'count' => $rm, 'icon' => '📊' ); } /* Yoast SEO */ // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.NotPrepared $yoast = (int) $wpdb->get_var( "SELECT COUNT(DISTINCT post_id) FROM {$wpdb->postmeta} WHERE meta_key = '_yoast_wpseo_title'" ); if ( $yoast > 0 ) { $plugins[] = array( 'id' => 'yoast', 'name' => 'Yoast SEO', 'count' => $yoast, 'icon' => '🟣' ); } /* All in One SEO */ // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.NotPrepared $aioseo = (int) $wpdb->get_var( "SELECT COUNT(DISTINCT post_id) FROM {$wpdb->postmeta} WHERE meta_key = '_aioseo_title'" ); if ( $aioseo > 0 ) { $plugins[] = array( 'id' => 'aioseo', 'name' => 'All in One SEO', 'count' => $aioseo, 'icon' => '🔵' ); } wp_send_json_success( array( 'plugins' => $plugins ) ); } /* ── AJAX: Run import from selected plugin ── */ add_action( 'wp_ajax_agenli_wizard_import', 'agenli_ajax_wizard_import' ); function agenli_ajax_wizard_import() { check_ajax_referer( 'agenli_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error(); $source = isset( $_POST['source'] ) ? sanitize_key( $_POST['source'] ) : ''; $mode = isset( $_POST['mode'] ) ? sanitize_key( $_POST['mode'] ) : 'dry_run'; /* Meta key maps per plugin */ $maps = array( 'rankmath' => array( 'rank_math_title' => 'seo_title', 'rank_math_description' => 'seo_desc', 'rank_math_focus_keyword' => 'focus_kw', 'rank_math_canonical_url' => 'canonical', 'rank_math_facebook_title' => 'og_title', 'rank_math_facebook_description' => 'og_desc', 'rank_math_facebook_image' => 'og_image', ), 'yoast' => array( '_yoast_wpseo_title' => 'seo_title', '_yoast_wpseo_metadesc' => 'seo_desc', '_yoast_wpseo_focuskw' => 'focus_kw', '_yoast_wpseo_canonical' => 'canonical', '_yoast_wpseo_opengraph-title' => 'og_title', '_yoast_wpseo_opengraph-description'=> 'og_desc', '_yoast_wpseo_opengraph-image' => 'og_image', ), 'aioseo' => array( '_aioseo_title' => 'seo_title', '_aioseo_description' => 'seo_desc', '_aioseo_keywords' => 'focus_kw', '_aioseo_og_title' => 'og_title', '_aioseo_og_description' => 'og_desc', '_aioseo_og_image_custom_url' => 'og_image', ), ); if ( ! isset( $maps[ $source ] ) ) { wp_send_json_error( array( 'message' => 'Unknown source plugin.' ) ); } $map = $maps[ $source ]; global $wpdb; /* Get all post IDs with data from source plugin */ $source_keys = "'" . implode( "','", array_keys( $map ) ) . "'"; // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared $post_ids = $wpdb->get_col( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key IN ({$source_keys})" ); /* Handle robots separately for Rank Math and Yoast */ $robots_key = ''; if ( 'rankmath' === $source ) $robots_key = 'rank_math_robots'; if ( 'yoast' === $source ) $robots_key = '_yoast_wpseo_meta-robots-noindex'; if ( 'aioseo' === $source ) $robots_key = '_aioseo_robots_default'; if ( $robots_key ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared $robot_ids = $wpdb->get_col( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key = '{$robots_key}'" ); $post_ids = array_unique( array_merge( $post_ids, $robot_ids ) ); } $migrated = 0; $skipped = 0; $total = 0; $log = array(); foreach ( $post_ids as $pid ) { $pid = (int) $pid; $post = get_post( $pid ); if ( ! $post || ! in_array( $post->post_status, array( 'publish', 'draft' ), true ) ) continue; $total++; $changed = array(); /* Standard fields */ foreach ( $map as $src_key => $ab_key ) { $val = get_post_meta( $pid, $src_key, true ); if ( empty( $val ) ) continue; /* OG image handling */ if ( 'og_image' === $ab_key ) { if ( is_numeric( $val ) ) { $url = wp_get_attachment_url( (int) $val ); if ( $url ) $val = $url; } if ( is_array( $val ) ) { $val = ! empty( $val['url'] ) ? $val['url'] : ''; } } if ( empty( $val ) ) continue; if ( 'live' === $mode ) { agenli_save_post_meta( $pid, $ab_key, sanitize_text_field( $val ) ); } $changed[] = $ab_key; } /* Robots */ if ( $robots_key ) { $robots_val = get_post_meta( $pid, $robots_key, true ); if ( ! empty( $robots_val ) ) { $robots_final = 'index,follow'; if ( 'rankmath' === $source ) { if ( is_string( $robots_val ) ) $robots_val = maybe_unserialize( $robots_val ); if ( is_array( $robots_val ) ) { $ni = in_array( 'noindex', $robots_val, true ) ? 'noindex' : 'index'; $nf = in_array( 'nofollow', $robots_val, true ) ? 'nofollow' : 'follow'; $robots_final = $ni . ',' . $nf; } } elseif ( 'yoast' === $source ) { /* Yoast: 1 = noindex, 0 = index */ $robots_final = ( '1' === (string) $robots_val ) ? 'noindex,follow' : 'index,follow'; } elseif ( 'aioseo' === $source ) { $robots_final = ( 'noindex' === $robots_val ) ? 'noindex,follow' : 'index,follow'; } if ( 'live' === $mode ) { agenli_save_post_meta( $pid, 'seo_robots', $robots_final ); } $changed[] = 'seo_robots'; } } if ( ! empty( $changed ) ) { $migrated++; $log[] = array( 'id' => $pid, 'title' => wp_trim_words( $post->post_title, 6 ), 'fields' => $changed ); } else { $skipped++; } } /* Mark wizard complete on live run */ if ( 'live' === $mode ) { update_option( 'agenli_wizard_complete', '1' ); update_option( 'agenli_show_import_wizard', '0' ); } wp_send_json_success( array( 'mode' => $mode, 'total' => $total, 'migrated' => $migrated, 'skipped' => $skipped, 'log' => array_slice( $log, 0, 50 ), /* First 50 for display */ ) ); } /* ── Wizard page UI ── */ function agenli_page_setup_wizard() { /* Mark wizard as seen */ update_option( 'agenli_show_import_wizard', '0' ); /* Hide WP sidebar and admin bar via inline CSS */ echo ''; ?>