Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Migration
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 9
992
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 migration_redirect
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
42
 get_brands_to_urls
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 on_update_nfd_migrate_site
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 on_update_instawp_last_migration_details
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
72
 register_wp_migration_tool
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 wordpress_migration_tool
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 set_import_tools
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
56
 load_text_domain
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace NewfoldLabs\WP\Module\Migration;
3
4use NewfoldLabs\WP\Module\Migration\Data\Constants;
5use NewfoldLabs\WP\Module\Migration\Helpers\BrandHelper;
6use NewfoldLabs\WP\Module\Migration\Helpers\Permissions;
7use NewfoldLabs\WP\ModuleLoader\Container;
8use NewfoldLabs\WP\Module\Migration\RestApi\RestApi;
9use NewfoldLabs\WP\Module\Migration\Services\InstaMigrateService;
10use NewfoldLabs\WP\Module\Migration\Reports\MigrationReport;
11use NewfoldLabs\WP\Module\Migration\Listeners\InstaWpOptionsUpdatesListener;
12use NewfoldLabs\WP\Module\Migration\Services\UtilityService;
13/**
14 * Class Migration
15 *
16 * @package NewfoldLabs\WP\Module\Migration
17 */
18class Migration {
19
20    /**
21     * Container loaded from the brand plugin.
22     *
23     * @var Container
24     */
25    protected $container;
26
27    /**
28     * To create insta service instance
29     *
30     * @var insta_service
31     */
32    protected $insta_service;
33
34    /**
35     * Identifier for script handle.
36     *
37     * @var string
38     */
39    public static $handle = 'nfd-migration';
40
41    /**
42     * Migration constructor.
43     *
44     * @param Container $container Container loaded from the brand plugin.
45     */
46    public function __construct( Container $container ) {
47        $this->container = $container;
48
49        new Constants( $container );
50        new InstaWpOptionsUpdatesListener();
51
52        if ( Permissions::rest_is_authorized_admin() ) {
53            new RestApi();
54        }
55
56        if ( Permissions::is_authorized_admin() ) {
57            new MigrationReport();
58            add_action( 'init', array( $this, 'load_text_domain' ), 100 );
59            if ( BrandHelper::is_whitelisted( $container->plugin()->id ) ) {
60                add_action( 'load-import.php', array( $this, 'register_wp_migration_tool' ) );
61                add_action( 'admin_enqueue_scripts', array( $this, 'set_import_tools' ) );
62            }
63        }
64
65        add_action( 'pre_update_option_nfd_migrate_site', array( $this, 'on_update_nfd_migrate_site' ) );
66        add_action( 'pre_update_option_instawp_last_migration_details', array( $this, 'on_update_instawp_last_migration_details' ), 10, 1 );
67        add_filter( 'newfold_sso_success_url', array( $this, 'migration_redirect' ) );
68    }
69
70    /**
71     * Redirects to on boarding migration page dynamically by brand if nfd_migrate_site is set.
72     *
73     * @param string $url The URL to redirect to.
74     * @return string $url The URL to redirect to.
75     */
76    public function migration_redirect( $url ) {
77        $sso_to_migration = isset( $_GET['nfd-action'] ) && 'nfd-migration' === sanitize_key( $_GET['nfd-action'] ) ? true : false; // phpcs:ignore WordPress.Security.NonceVerification
78
79        $brands_to_onboarding_urls = $this->get_brands_to_urls();
80
81        if ( $sso_to_migration || get_option( 'nfd_migrate_site', false ) ) {
82            if ( isset( $brands_to_onboarding_urls[ BRAND_PLUGIN ] ) ) {
83                $url = $brands_to_onboarding_urls[ BRAND_PLUGIN ];
84            }
85        }
86        return $url;
87    }
88
89    /**
90     * Returns the brand specific URLs for onboarding migration.
91     *
92     * @return array
93     */
94    public function get_brands_to_urls() {
95        return array(
96            'bluehost'  => admin_url( 'index.php?page=nfd-onboarding#/migration' ),
97            'hostgator' => admin_url( 'index.php?page=nfd-onboarding#/sitegen/step/migration' ),
98        );
99    }
100
101    /**
102     * Triggers on instawp connect installation
103     *
104     * @param boolean $option status of migration.
105     */
106    public function on_update_nfd_migrate_site( $option ) {
107        $this->insta_service = new InstaMigrateService();
108        $this->insta_service->run();
109        return $option;
110    }
111
112    /**
113     * Updates nfd_show_migration_steps option based on instawp_last_migration_details
114     *
115     * @param array $new_option status of migration.
116     */
117    public function on_update_instawp_last_migration_details( $new_option ) {
118        $migrate_group_uuid = isset( $new_option['migrate_group_uuid'] ) ? $new_option['migrate_group_uuid'] : '';
119        if ( ! empty( $migrate_group_uuid ) ) {
120            $data = UtilityService::get_migration_data( $migrate_group_uuid );
121            if ( $data && is_array( $data ) && isset( $data['status'] ) && $data['status'] ) {
122                $migration_status = $data['data']['status'];
123                if ( 'completed' === $migration_status ) {
124                    update_option( 'nfd_show_migration_steps', true );
125                }
126            }
127        }
128        return $new_option;
129    }
130
131    /**
132     * Register WordPress Migration Tool to imports.
133     */
134    public function register_wp_migration_tool() {
135        register_importer(
136            'site_migration_wordpress_importer',
137            __( 'WordPress Migration Tool', 'wp-module-migration' ),
138            __( 'Migrate an existing WordPress site to this WordPress instance. This tool will make a copy of an existing site and automatically import it into this WordPress instance <strong>This will overwrite all the content.</strong>', 'wp-module-migration' ),
139            array( $this, 'wordpress_migration_tool' )
140        );
141    }
142
143    /**
144     * Initiates the Migration service redirects it the instawp screen
145     */
146    public function wordpress_migration_tool() {
147        $this->insta_service = new InstaMigrateService();
148        $response            = $this->insta_service->run();
149        if ( ! is_wp_error( $response ) ) {
150            wp_safe_redirect( apply_filters( 'nfd_build_url', $response['redirect_url'] ) );
151        } else {
152            wp_safe_redirect( apply_filters( 'nfd_build_url', admin_url( 'import.php' ) ) );
153        }
154        die();
155    }
156
157    /**
158     * Changes the text WordPress to WordPress content in import page
159     */
160    public function set_import_tools() {
161        global $pagenow;
162
163        $script_subpath = 'includes/import-tools-changes.js';
164        $style_subpath  = 'includes/styles.css';
165        $script_path    = NFD_MIGRATION_DIR . $script_subpath;
166        $style_path     = NFD_MIGRATION_DIR . $style_subpath;
167        $script_version = file_exists( $script_path ) ? filemtime( $script_path ) : '1.0';
168        $style_version  = file_exists( $style_path ) ? filemtime( $style_path ) : '1.0';
169
170        wp_register_script(
171            'nfd_migration_tool',
172            NFD_MIGRATION_PLUGIN_URL . 'vendor/newfold-labs/wp-module-migration/' . $script_subpath,
173            array( 'jquery' ),
174            $script_version,
175            true
176        );
177
178        wp_register_style(
179            'nfd_migration_tool',
180            NFD_MIGRATION_PLUGIN_URL . 'vendor/newfold-labs/wp-module-migration/' . $style_subpath,
181            array(),
182            $style_version,
183            'all'
184        );
185
186        $is_ui_tracking_page = isset( $_GET['page'] ) && 'nfd-migration' === $_GET['page'] && 'admin.php' === $pagenow; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
187        if ( $is_ui_tracking_page ) {
188            wp_enqueue_style( 'nfd_migration_tool' );
189        }
190        if ( 'import.php' === $pagenow ) {
191            wp_enqueue_script( 'nfd_migration_tool' );
192            wp_enqueue_style( 'nfd_migration_tool' );
193
194            $migration_data = array(
195                'migration_title'       => __( 'Preparing your site', 'wp-module-migration' ),
196                'migration_description' => __( 'Please wait a few seconds while we get your new account ready to import your existing WordPress site.', 'wp-module-migration' ),
197                'wordpress_title'       => __( 'WordPress Content', 'wp-module-migration' ),
198                'restApiUrl'            => \get_home_url( null, '/index.php?rest_route=' ),
199                'restApiNonce'          => \wp_create_nonce( 'wp_rest' ),
200            );
201            wp_localize_script( 'nfd_migration_tool', 'migration', $migration_data );
202        }
203    }
204
205    /**
206     * Load text domain for Module
207     *
208     * @return void
209     */
210    public function load_text_domain() {
211        load_plugin_textdomain(
212            'wp-module-migration',
213            false,
214            dirname( $this->container->plugin()->basename ) . '/vendor/newfold-labs/wp-module-migration/languages'
215        );
216    }
217}