Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 3 |
CRAP | n/a |
0 / 0 |
|
| wpcomsh_check_for_migrate_guru_request_params | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
110 | |||
| wpcomsh_migrate_guru_log | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| wpcomsh_migrate_guru_migration_started | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * This code is used to signal WPCOM that a migration using Migrate Guru has started. |
| 4 | * |
| 5 | * @package wpcomsh |
| 6 | */ |
| 7 | |
| 8 | add_action( 'muplugins_loaded', 'wpcomsh_check_for_migrate_guru_request_params', 0, 0 ); |
| 9 | |
| 10 | /** |
| 11 | * Check for Migrate Guru request parameters and enqueue a function on |
| 12 | * shutdown to signal WPCOM that a migration has started. It's enqueued on shutdown |
| 13 | * because when Migrate Guru starts a migration it might exit the process early, and |
| 14 | * we can't call it directly from this function since we still need some functions |
| 15 | * to be loaded at this point. |
| 16 | */ |
| 17 | function wpcomsh_check_for_migrate_guru_request_params() { |
| 18 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 19 | $is_migrate_guru_migration_start_request = |
| 20 | isset( $_SERVER['REQUEST_METHOD'] ) && |
| 21 | $_SERVER['REQUEST_METHOD'] === 'POST' && |
| 22 | isset( $_REQUEST['bvplugname'] ) && |
| 23 | $_REQUEST['bvplugname'] === 'migrateguru' && |
| 24 | isset( $_REQUEST['wing'] ) && |
| 25 | $_REQUEST['wing'] === 'fswrt' && |
| 26 | isset( $_REQUEST['bvMethod'] ) && |
| 27 | $_REQUEST['bvMethod'] === 'wrtfle'; |
| 28 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 29 | |
| 30 | if ( ! $is_migrate_guru_migration_start_request ) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | $active_migration_start_time = get_transient( 'wpcomsh_migrate_guru_migration_start_time' ); |
| 35 | if ( is_int( $active_migration_start_time ) ) { |
| 36 | // We already have an active migration, so nothing further needed. |
| 37 | wpcomsh_migrate_guru_log( "Migration detected, not calling WPCOM since there's already an ongoing request" ); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | // Set the transient for 60 seconds so we don't trigger another request for the next minute. |
| 42 | set_transient( 'wpcomsh_migrate_guru_migration_start_time', time(), 60 ); |
| 43 | |
| 44 | wpcomsh_migrate_guru_log( 'Migration detected, registering shutdown function' ); |
| 45 | |
| 46 | register_shutdown_function( 'wpcomsh_migrate_guru_migration_started' ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Logs to error_log if filter wpcomsh_migrate_guru_logging_enabled is true. |
| 51 | * |
| 52 | * @param string $message The message to log. |
| 53 | */ |
| 54 | function wpcomsh_migrate_guru_log( $message ) { |
| 55 | $logging_enabled = apply_filters( 'wpcomsh_migrate_guru_logging_enabled', false ); |
| 56 | if ( $logging_enabled ) { |
| 57 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 58 | error_log( 'wpcomsh_migrate_guru_migration: ' . $message ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Signal WPCOM that a migration using Migrate Guru has started. This function is enqueued on |
| 64 | * shutdown. |
| 65 | */ |
| 66 | function wpcomsh_migrate_guru_migration_started() { |
| 67 | wpcomsh_migrate_guru_log( 'Calling WPCOM to signal a migration has started' ); |
| 68 | |
| 69 | include_once WP_CONTENT_DIR . '/../__wp__/wp-includes/pluggable.php'; |
| 70 | include_once WP_PLUGIN_DIR . '/jetpack/jetpack.php'; |
| 71 | |
| 72 | $wpcom_blog_id = _wpcom_get_current_blog_id(); |
| 73 | $endpoint = sprintf( '/sites/%s/atomic-migration-status', $wpcom_blog_id ); |
| 74 | $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_blog( |
| 75 | $endpoint, |
| 76 | 'v2', |
| 77 | array( 'method' => 'POST' ), |
| 78 | array( |
| 79 | 'status' => 'started', |
| 80 | 'migration_type' => 'migrate-guru', |
| 81 | ), |
| 82 | 'wpcom' |
| 83 | ); |
| 84 | |
| 85 | if ( 200 !== $response['response']['code'] || empty( $response['body'] ) ) { |
| 86 | wpcomsh_migrate_guru_log( 'WPCOM call failed' ); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | wpcomsh_migrate_guru_log( 'WPCOM call successful' ); |
| 91 | } |