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