ID ); $after = self::permalink_as( $post, self::TARGET ); $rows[] = array( 'id' => (int) $post->ID, 'title' => (string) $post->post_title, 'slug' => (string) $post->post_name, 'from' => (string) $post->post_type, 'status' => (string) $post->post_status, 'before' => $before, 'after' => $after, 'moves' => ( untrailingslashit( $before ) !== untrailingslashit( $after ) ), 'located' => (int) get_post_meta( $post->ID, self::META_LOCATION, true ), ); } foreach ( $rows as $r ) { if ( $r['moves'] ) { $blockers[] = sprintf( __( '"%1$s" would change URL: %2$s becomes %3$s. Set the store URL base to match before migrating.', 'ai-eshop-optimizer' ), $r['title'], $r['before'], $r['after'] ); } } return array( 'rows' => $rows, 'safe' => empty( $blockers ), 'blockers' => array_values( array_unique( $blockers ) ), ); } private static function permalink_as( $post, $type ) { $obj = get_post_type_object( $type ); if ( ! $obj ) { return ''; } $base = is_array( $obj->rewrite ) && ! empty( $obj->rewrite['slug'] ) ? $obj->rewrite['slug'] : $type; return user_trailingslashit( home_url( '/' . trim( (string) $base, '/' ) . '/' . $post->post_name ), 'single' ); } private static function source_posts() { $types = array_values( array_filter( self::SOURCES, 'post_type_exists' ) ); if ( ! $types ) { return array(); } return get_posts( array( 'post_type' => $types, 'post_status' => 'any', 'numberposts' => -1, 'orderby' => 'ID', 'order' => 'ASC', 'suppress_filters' => true, ) ); } public static function run( $force = false ) { $plan = self::plan(); if ( ! $plan['safe'] && ! $force ) { return array( 'moved' => 0, 'skipped' => count( $plan['rows'] ), 'blockers' => $plan['blockers'] ); } $moved = 0; foreach ( $plan['rows'] as $row ) { update_post_meta( $row['id'], self::META_FROM, $row['from'] ); set_post_type( $row['id'], self::TARGET ); $moved++; } if ( $moved ) { flush_rewrite_rules( false ); } return array( 'moved' => $moved, 'skipped' => 0, 'blockers' => $plan['blockers'] ); } public static function reverse() { $posts = get_posts( array( 'post_type' => self::TARGET, 'post_status' => 'any', 'numberposts' => -1, 'meta_key' => self::META_FROM, 'suppress_filters' => true, ) ); $restored = 0; foreach ( $posts as $post ) { $from = (string) get_post_meta( $post->ID, self::META_FROM, true ); if ( '' === $from || ! post_type_exists( $from ) ) { continue; } set_post_type( $post->ID, $from ); delete_post_meta( $post->ID, self::META_FROM ); $restored++; } if ( $restored ) { flush_rewrite_rules( false ); } return array( 'restored' => $restored ); } public static function create_missing( $publish = false ) { if ( ! post_type_exists( self::TARGET ) || ! class_exists( 'AIEO_Google_Reviews_Locations' ) ) { return array( 'created' => 0, 'skipped' => 0, 'ids' => array() ); } $locations = AIEO_Google_Reviews_Locations::list_all( true ); $created = 0; $skipped = 0; $ids = array(); foreach ( (array) $locations as $loc ) { $loc_id = (int) ( is_array( $loc ) ? ( $loc['id'] ?? 0 ) : 0 ); if ( $loc_id <= 0 || self::page_for_location( $loc_id ) ) { $skipped++; continue; } $title = trim( (string) ( $loc['label'] ?? '' ) ); if ( '' === $title ) { $title = trim( (string) ( $loc['google_name'] ?? '' ) ); } if ( '' === $title ) { $title = sprintf( __( 'Store %d', 'ai-eshop-optimizer' ), $loc_id ); } $slug = sanitize_title( (string) ( $loc['slug'] ?? '' ) ); $args = array( 'post_type' => self::TARGET, 'post_status' => $publish ? 'publish' : 'draft', 'post_title' => $title, 'post_content' => '', 'meta_input' => array( self::META_LOCATION => $loc_id ), ); if ( '' !== $slug ) { $args['post_name'] = $slug; } $id = wp_insert_post( $args, true ); if ( is_wp_error( $id ) ) { $skipped++; continue; } $created++; $ids[] = (int) $id; } return array( 'created' => $created, 'skipped' => $skipped, 'ids' => $ids ); } public static function page_for_location( $location_id ) { $types = array_values( array_filter( array_merge( array( self::TARGET ), self::SOURCES ), 'post_type_exists' ) ); if ( ! $types ) { return 0; } $ids = get_posts( array( 'post_type' => $types, 'post_status' => 'any', 'numberposts' => 1, 'fields' => 'ids', 'meta_key' => self::META_LOCATION, 'meta_value' => (string) (int) $location_id, 'suppress_filters' => true, ) ); return $ids ? (int) $ids[0] : 0; } } } if ( defined( 'WP_CLI' ) && WP_CLI && ! class_exists( 'AIEO_CLI_Stores' ) ) { class AIEO_CLI_Stores { public function plan() { $plan = AIEO_Store_Migration::plan(); if ( ! $plan['rows'] ) { WP_CLI::success( 'Nothing to migrate: no pages on a legacy store type.' ); return; } WP_CLI\Utils\format_items( 'table', $plan['rows'], array( 'id', 'title', 'from', 'status', 'before', 'after', 'moves', 'located' ) ); foreach ( $plan['blockers'] as $b ) { WP_CLI::warning( $b ); } WP_CLI::log( $plan['safe'] ? 'SAFE: every URL is unchanged.' : 'NOT SAFE: resolve the warnings above first.' ); } public function migrate( $args, $assoc ) { $r = AIEO_Store_Migration::run( ! empty( $assoc['force'] ) ); foreach ( $r['blockers'] as $b ) { WP_CLI::warning( $b ); } if ( ! $r['moved'] ) { WP_CLI::error( 'Nothing moved. Run `wp aieo stores plan` to see why.' ); } WP_CLI::success( sprintf( 'Moved %d page(s) to %s.', $r['moved'], AIEO_Store_Migration::TARGET ) ); } public function reverse() { $r = AIEO_Store_Migration::reverse(); WP_CLI::success( sprintf( 'Restored %d page(s).', $r['restored'] ) ); } public function create( $args, $assoc ) { $r = AIEO_Store_Migration::create_missing( ! empty( $assoc['publish'] ) ); WP_CLI::success( sprintf( 'Created %d, skipped %d (already had a page).', $r['created'], $r['skipped'] ) ); } } WP_CLI::add_command( 'aieo stores', 'AIEO_CLI_Stores' ); }