|null */ private static ?array $cache = null; /** * @return array */ public static function all(): array { if ( null !== self::$cache ) { return self::$cache; } $adapters = array( new Adapters\Yoast(), new Adapters\RankMath(), new Adapters\Aioseo(), new Adapters\SeoPress(), new Adapters\SeoFramework(), new Adapters\SlimSeo(), new Adapters\Squirrly(), new Adapters\SmartCrawl(), new Adapters\WpMetaSeo(), new Adapters\SeoUltimate(), new Adapters\Genesis(), new Adapters\Jetpack(), new Adapters\WooCommerce(), new Adapters\Redirection(), ); /** * Filter the import adapters. * * @param array $adapters */ $adapters = (array) apply_filters( 'airanksuite/import/adapters', $adapters ); $out = array(); foreach ( $adapters as $adapter ) { if ( $adapter instanceof Adapters\Adapter ) { $out[ $adapter->id() ] = $adapter; } } self::$cache = $out; return $out; } public static function get( string $id ): ?Adapters\Adapter { return self::all()[ $id ] ?? null; } /** Test seam: forget the instances (adapters hold no state, but tests swap them). */ public static function reset(): void { self::$cache = null; } /** * What every source holds on this site, for the import screen. * * @return array> */ public static function detect(): array { $out = array(); // Two grouped queries tell every adapter whether its keys hold data, // so the twelve empty ones stop paying two COUNTs each. $post_keys = array(); $term_keys = array(); foreach ( self::all() as $adapter ) { $map = $adapter->key_map(); $post_keys = array_merge( $post_keys, $map['post'] ); $term_keys = array_merge( $term_keys, $map['term'] ); } \AiRankSuite\Modules\ImportExport\Adapters\Adapter::prime_key_data( array_values( array_unique( $post_keys ) ), array_values( array_unique( $term_keys ) ) ); foreach ( self::all() as $id => $adapter ) { $counts = $adapter->counts(); $total = (int) ( $counts['posts'] ?? 0 ) + (int) ( $counts['terms'] ?? 0 ) + (int) ( $counts['redirects'] ?? 0 ); // Every supported source gets a row, including the ones with // nothing on this site. The list is also the answer to "can this // plugin read my old data", which is a question asked before the // old plugin's data is anywhere near this install — and a grid // that hides them reads as an import that has gone missing. $out[] = array( 'id' => $id, 'label' => $adapter->label(), 'active' => $adapter->active(), 'posts' => (int) ( $counts['posts'] ?? 0 ), 'terms' => (int) ( $counts['terms'] ?? 0 ), 'redirects' => (int) ( $counts['redirects'] ?? 0 ), 'settings' => ! empty( $counts['settings'] ), 'has_data' => $total > 0 || ! empty( $counts['settings'] ), ); } // Most to least: the source somebody is actually migrating from should // not be below nine they have never installed. usort( $out, static function ( array $a, array $b ): int { $weight = static fn( array $row ): int => ( $row['has_data'] ? 1000000 : 0 ) + ( $row['active'] ? 100000 : 0 ) + (int) $row['posts'] + (int) $row['terms'] + (int) $row['redirects']; return $weight( $b ) <=> $weight( $a ); } ); return $out; } }