Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 89
0.00% covered (danger)
0.00%
0 / 4
CRAP
n/a
0 / 0
wpcomsh_is_migration_in_progress
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
wpcomsh_allow_migration_option
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
aiowp_migration_logging_helper
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
6
aiowp_migration_status_helper
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * Migration helper functions.
4 *
5 * @package wpcom-migration-helpers
6 */
7
8/**
9 * Checks if there's an active site migration happening for the current site.
10 *
11 * The current site is the destination site for the migration.
12 *
13 * @return bool
14 */
15function wpcomsh_is_migration_in_progress() {
16    return get_option( 'wpcomsh_site_migration_status', false ) === '1';
17}
18
19/**
20 * Allow setting the `site_migration_status` from WPCOM to the target site through Jetpack.
21 *
22 * @param array $options List of allowed Jetpack options.
23 *
24 * @return array
25 */
26function wpcomsh_allow_migration_option( $options ) {
27    // For storing AT options.
28    $options[] = 'wpcomsh_site_migration_status';
29
30    return $options;
31}
32
33add_filter( 'jetpack_options_whitelist', 'wpcomsh_allow_migration_option' );
34
35/**
36 * Logs the start and end of an AIOWP migration import and any errors that occur during the import.
37 */
38function aiowp_migration_logging_helper() {
39    if ( ! class_exists( 'Ai1wm_Main_Controller' ) ) {
40        return;
41    }
42
43    $target_blog_id = _wpcom_get_current_blog_id();
44
45    // Filter that gets called when import starts
46    add_filter(
47        'ai1wm_import',
48        function ( $params = array() ) use ( $target_blog_id ) {
49            wpcomsh_record_tracks_event(
50                'wpcom_import_start',
51                array(
52                    'migration_tool' => 'aiowp',
53                    'target_blog_id' => $target_blog_id,
54                )
55            );
56            return $params;
57        },
58        10
59    );
60
61    // Filter that gets called when import finishes or is cancelled by the user
62    add_filter(
63        'ai1wm_import',
64        function ( $params = array() ) use ( $target_blog_id ) {
65            wpcomsh_record_tracks_event(
66                'wpcom_import_done',
67                array(
68                    'migration_tool' => 'aiowp',
69                    'target_blog_id' => $target_blog_id,
70                )
71            );
72            return $params;
73        },
74        400
75    );
76
77    // Filter that gets called when an import fails
78    add_filter(
79        'ai1wm_notification_error_toggle',
80        function ( $should_notify ) {
81            do_action(
82                'wpcomsh_log',
83                'There was an error with the AIOWP Migration.'
84            );
85            return $should_notify;
86        },
87        9
88    );
89}
90add_action( 'plugins_loaded', 'aiowp_migration_logging_helper', 10 );
91
92/**
93 * Helper function that registers a filter that listens for the AIOWP migration completed event.
94 * Once detected, it will trigger a call to an endpoint on WPCOM to run the corresponding cleanup jobs.
95 */
96function aiowp_migration_status_helper() {
97    if ( ! class_exists( 'Ai1wm_Main_Controller' ) ) {
98        return;
99    }
100
101    $wpcom_blog_id             = Jetpack_Options::get_option( 'id' );
102    $wpcom_blog_id_backup_file = WP_CONTENT_DIR . '/uploads/blog_id_backup.txt';
103    add_filter(
104        'ai1wm_import',
105        function ( $params = array() ) use ( $wpcom_blog_id, $wpcom_blog_id_backup_file ) {
106            // This filter runs at this priority at the start of the import.
107            // We store the wpcom_blog_id in a backup file so we can use it later in the import, since
108            // the blog id is removed from the db by subsequent steps in the AIOWP migration.
109            file_put_contents( $wpcom_blog_id_backup_file, $wpcom_blog_id ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
110            return $params;
111        },
112        10
113    );
114    add_filter(
115        'ai1wm_import',
116        function ( $params = array() ) use ( $wpcom_blog_id_backup_file ) {
117            if ( ! file_exists( $wpcom_blog_id_backup_file ) ) {
118                do_action( 'wpcomsh_log', 'No wpcom_blog_id_backup_file found' );
119                return $params;
120            }
121
122            // Read the wpcom_blog_id from the backup file.
123            $file_contents = file_get_contents( $wpcom_blog_id_backup_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
124
125            if ( false === $file_contents ) {
126                do_action( 'wpcomsh_log', 'Failed to read wpcom_blog_id_backup_file' );
127                return $params;
128            }
129
130            if ( ! is_numeric( $file_contents ) || (int) $file_contents === 0 ) {
131                do_action( 'wpcomsh_log', 'The content of the wpcom_blog_id_backup_file is not valid' );
132                return $params;
133            }
134
135            $wpcom_blog_id = intval( $file_contents );
136            unlink( $wpcom_blog_id_backup_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
137
138            $endpoint = sprintf( '/sites/%s/migration-aiowp-notifications', $wpcom_blog_id );
139            $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_blog(
140                $endpoint,
141                'v2',
142                array( 'method' => 'POST' ),
143                array( 'status' => 'completed' ),
144                'wpcom'
145            );
146            if ( 200 !== $response['response']['code'] || empty( $response['body'] ) ) {
147                return $params;
148            }
149            return $params;
150        },
151        400
152    );
153}
154
155add_action( 'plugins_loaded', 'aiowp_migration_status_helper', 10 );