*/ protected $row_cache = array(); /** * Whether AIOSEO is currently installed and active. * * @return bool */ public function is_active(): bool { return defined( 'AIOSEO_VERSION' ) || class_exists( '\\AIOSEO\\Plugin\\AIOSEO' ); } /** * Human-readable name of the source plugin. * * @return string */ public function get_name(): string { return 'All in One SEO'; } /** * Machine-readable slug used as `source_plugin` in the migration log. * * @return string */ public function get_slug(): string { return 'aioseo'; } /** * Fetches (and caches) a post's row from wp_aioseo_posts. * * @param int $post_id Post ID. * @return object|null The row, or null if AIOSEO has no data for this post. */ protected function get_row( int $post_id ) { if ( array_key_exists( $post_id, $this->row_cache ) ) { return $this->row_cache[ $post_id ]; } global $wpdb; $table = $wpdb->prefix . 'aioseo_posts'; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $table is built only from $wpdb->prefix plus a hardcoded literal, never user input; $post_id is passed through $wpdb->prepare(). Results are cached in $this->row_cache for the request's lifetime; this only runs during an explicit, admin-triggered migration. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE post_id = %d", $post_id ) ); $this->row_cache[ $post_id ] = $row ? $row : null; return $this->row_cache[ $post_id ]; } /** * Reads AIOSEO's meta title for a post. * * @param int $post_id Post ID. * @return string */ public function get_meta_title( int $post_id ): string { $row = $this->get_row( $post_id ); return $row ? (string) $row->title : ''; } /** * Reads AIOSEO's meta description for a post. * * @param int $post_id Post ID. * @return string */ public function get_meta_description( int $post_id ): string { $row = $this->get_row( $post_id ); return $row ? (string) $row->description : ''; } /** * Reads AIOSEO's canonical URL for a post. * * @param int $post_id Post ID. * @return string */ public function get_canonical( int $post_id ): string { $row = $this->get_row( $post_id ); return $row ? (string) $row->canonical_url : ''; } /** * Reads AIOSEO's robots meta (noindex/nofollow) for a post. * * @param int $post_id Post ID. * @return array{noindex: bool, nofollow: bool} */ public function get_robots( int $post_id ): array { $row = $this->get_row( $post_id ); return array( 'noindex' => (bool) ( $row->robots_noindex ?? false ), 'nofollow' => (bool) ( $row->robots_nofollow ?? false ), ); } /** * Reads AIOSEO's Open Graph title for a post. * * @param int $post_id Post ID. * @return string */ public function get_og_title( int $post_id ): string { $row = $this->get_row( $post_id ); return $row ? (string) $row->og_title : ''; } /** * Reads AIOSEO's Open Graph description for a post. * * @param int $post_id Post ID. * @return string */ public function get_og_description( int $post_id ): string { $row = $this->get_row( $post_id ); return $row ? (string) $row->og_description : ''; } /** * Reads AIOSEO's primary focus keyphrase for a post from its JSON-encoded keyphrases column. * * @param int $post_id Post ID. * @return string */ public function get_focus_keyword( int $post_id ): string { $row = $this->get_row( $post_id ); if ( ! $row || empty( $row->keyphrases ) ) { return ''; } $decoded = json_decode( (string) $row->keyphrases, true ); return (string) ( $decoded['focus']['keyphrase'] ?? '' ); } /** * Counts posts that have AIOSEO title or description data set. * * @return int */ public function count_migratable_pages(): int { global $wpdb; $table = $wpdb->prefix . 'aioseo_posts'; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $table is built only from $wpdb->prefix plus a hardcoded literal, never user input. Only runs when previewing a migration from the admin UI. return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table} WHERE title != '' OR description != ''" ); } /** * Deactivates AIOSEO if it's currently active. * * @return bool True if it was active and is now deactivated. */ public function deactivate_self(): bool { if ( ! function_exists( 'deactivate_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plugin_file = $this->find_active_plugin_file( 'all_in_one_seo_pack.php' ); if ( ! $plugin_file ) { return false; } deactivate_plugins( $plugin_file ); return ! is_plugin_active( $plugin_file ); } }