v4_table_exists() && $this->v4_row_count() > 0 ) { return true; } return array() !== $this->v3_post_ids(); } protected function run( bool $dry, bool $overwrite ): ImportReport { $report = new ImportReport(); if ( $this->v4_table_exists() && $this->v4_row_count() > 0 ) { $this->run_v4( $report, $dry, $overwrite ); } else { $this->run_v3( $report, $dry, $overwrite ); $report->add_note( __( 'Data in the legacy AIOSEO v3 format (post meta) was detected — title/description/canonical imported; v3 robots settings skipped (unverified value format).', 'acez-seo' ) ); } if ( $this->terms_table_exists() ) { $report->add_note( __( 'The aioseo_terms table (an AIOSEO Pro feature) was detected — importing term data from AIOSEO is not supported.', 'acez-seo' ) ); } return $report; } private function run_v4( ImportReport $report, bool $dry, bool $overwrite ): void { global $wpdb; $table = $this->v4_table_name(); $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT post_id, title, description, canonical_url, robots_default, robots_noindex, robots_nofollow, og_image_custom_url FROM %i', $table ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read-only SELECT against another plugin's own custom table (WP_Query cannot reach it); one-shot read during import/conflict detection, so caching would only serve stale data. Table name goes through prepare()'s %i identifier placeholder. foreach ( $rows as $row ) { $post_id = (int) ( $row['post_id'] ?? 0 ); if ( $post_id <= 0 ) { continue; } ++$report->posts; $this->import_post_field( $report, $dry, $overwrite, $post_id, MetaFields::TITLE, $this->convert_variables( (string) ( $row['title'] ?? '' ) ) ); $this->import_post_field( $report, $dry, $overwrite, $post_id, MetaFields::DESCRIPTION, $this->convert_variables( (string) ( $row['description'] ?? '' ) ) ); $this->import_post_field( $report, $dry, $overwrite, $post_id, MetaFields::CANONICAL, (string) ( $row['canonical_url'] ?? '' ) ); // robots_default = 1 means "use the post type's defaults" — nothing // post-specific to migrate in that case. if ( empty( $row['robots_default'] ) ) { if ( ! empty( $row['robots_noindex'] ) ) { $this->import_post_flag( $report, $dry, $overwrite, $post_id, MetaFields::ROBOTS_INDEX, false ); } if ( ! empty( $row['robots_nofollow'] ) ) { $this->import_post_flag( $report, $dry, $overwrite, $post_id, MetaFields::ROBOTS_FOLLOW, false ); } } $this->import_post_og_image( $report, $dry, $overwrite, $post_id, $this->attachment_id_from_url( (string) ( $row['og_image_custom_url'] ?? '' ) ) ); } } private function run_v3( ImportReport $report, bool $dry, bool $overwrite ): void { foreach ( $this->v3_post_ids() as $post_id ) { ++$report->posts; $this->import_post_field( $report, $dry, $overwrite, $post_id, MetaFields::TITLE, (string) get_post_meta( $post_id, '_aioseop_title', true ) ); $this->import_post_field( $report, $dry, $overwrite, $post_id, MetaFields::DESCRIPTION, (string) get_post_meta( $post_id, '_aioseop_description', true ) ); $this->import_post_field( $report, $dry, $overwrite, $post_id, MetaFields::CANONICAL, (string) get_post_meta( $post_id, '_aioseop_custom_link', true ) ); } } /** * @return int[] */ private function v3_post_ids(): array { global $wpdb; return array_map( 'intval', (array) $wpdb->get_col( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key IN ('_aioseop_title', '_aioseop_description', '_aioseop_custom_link')" ) // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read-only query against another plugin's own custom table (import dry-run / foreign-plugin conflict detection) — not our schema, WP_Query/get_posts can't reach it. / one-shot read during import/conflict-detection, not a repeated query in a request loop — caching would serve stale data across saves. ); } private function v4_table_name(): string { global $wpdb; return $wpdb->prefix . 'aioseo_posts'; } private function v4_table_exists(): bool { global $wpdb; return (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $this->v4_table_name() ) ) === $this->v4_table_name(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read-only query against another plugin's own custom table (import dry-run / foreign-plugin conflict detection) — not our schema, WP_Query/get_posts can't reach it. / one-shot read during import/conflict-detection, not a repeated query in a request loop — caching would serve stale data across saves. } private function v4_row_count(): int { global $wpdb; return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $this->v4_table_name() ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read-only SELECT against another plugin's own custom table (WP_Query cannot reach it); one-shot read during import/conflict detection, so caching would only serve stale data. Table name goes through prepare()'s %i identifier placeholder. } private function terms_table_exists(): bool { global $wpdb; $table = $wpdb->prefix . 'aioseo_terms'; return (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) === $table; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read-only query against another plugin's own custom table (import dry-run / foreign-plugin conflict detection) — not our schema, WP_Query/get_posts can't reach it. / one-shot read during import/conflict-detection, not a repeated query in a request loop — caching would serve stale data across saves. } /** * Converts AIOSEO's `#tag` smart-tag syntax to ours. Only known tags are * converted — a bare `#` is legitimate in real titles (hashtags), so no * blanket stripping of unknown tokens. */ private function convert_variables( string $value ): string { return str_replace( array( '#post_title', '#site_title', '#separator_sa', '#post_excerpt', '#taxonomy_title', '#current_year' ), array( '%post_title%', '%site_name%', '%sep%', '%post_excerpt%', '%term_title%', '%current_year%' ), $value ); } }