Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 104 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| InstaWpOptionsUpdatesListener | |
0.00% |
0 / 104 |
|
0.00% |
0 / 7 |
1332 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| register_hooks | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| on_update_instawp_last_migration_details | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
420 | |||
| on_update_instawp_migration_details | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
42 | |||
| source_hosting_info | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| page_speed_source | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| page_speed_destination | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | namespace NewfoldLabs\WP\Module\Migration\Listeners; |
| 3 | |
| 4 | use NewfoldLabs\WP\Module\Migration\Data\Events; |
| 5 | use NewfoldLabs\WP\Module\Migration\Services\EventService; |
| 6 | use NewfoldLabs\WP\Module\Migration\Services\UtilityService; |
| 7 | use NewfoldLabs\WP\Module\Migration\Services\Tracker; |
| 8 | use NewfoldLabs\WP\Module\Migration\Steps\Push; |
| 9 | use NewfoldLabs\WP\Module\Migration\Steps\PageSpeed; |
| 10 | use NewfoldLabs\WP\Module\Migration\Steps\LastStep; |
| 11 | use NewfoldLabs\WP\Module\Migration\Steps\SourceHostingInfo; |
| 12 | |
| 13 | /** |
| 14 | * Monitors InstaWp options update |
| 15 | */ |
| 16 | class InstaWpOptionsUpdatesListener { |
| 17 | /** |
| 18 | * Tracker class instance. |
| 19 | * |
| 20 | * @var Tracker $tracker |
| 21 | */ |
| 22 | public $tracker; |
| 23 | |
| 24 | /** |
| 25 | * InstaWpOptionsUpdatesListener constructor. |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | $this->register_hooks(); |
| 29 | } |
| 30 | /** |
| 31 | * Register the hooks for the listener |
| 32 | * |
| 33 | * @return void |
| 34 | */ |
| 35 | public function register_hooks() { |
| 36 | $this->tracker = new Tracker(); |
| 37 | add_filter( 'pre_update_option_instawp_last_migration_details', array( $this, 'on_update_instawp_last_migration_details' ), 10, 2 ); |
| 38 | add_filter( 'pre_update_option_instawp_migration_details', array( $this, 'on_update_instawp_migration_details' ), 10, 2 ); |
| 39 | add_action( 'nfd_migration_page_speed_source', array( $this, 'page_speed_source' ), 10 ); |
| 40 | add_action( 'nfd_migration_page_speed_destination', array( $this, 'page_speed_destination' ), 10, 3 ); |
| 41 | add_action( 'nfd_migration_source_hosting_info', array( $this, 'source_hosting_info' ), 10 ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Triggers events |
| 46 | * |
| 47 | * @param array $new_value status of migration |
| 48 | * @param array $old_value previous status of migration |
| 49 | */ |
| 50 | public function on_update_instawp_last_migration_details( $new_value, $old_value ) { |
| 51 | if ( $old_value !== $new_value && ! get_option( 'nfd_migration_status_sent', false ) ) { |
| 52 | $migrate_group_uuid = isset( $new_value['migrate_group_uuid'] ) ? $new_value['migrate_group_uuid'] : ''; |
| 53 | if ( ! empty( $migrate_group_uuid ) ) { |
| 54 | $response = UtilityService::get_migration_data( $migrate_group_uuid ); |
| 55 | |
| 56 | if ( $response && is_array( $response ) && isset( $response['status'] ) && $response['status'] ) { |
| 57 | $migration_status = $response['data']['status']; |
| 58 | |
| 59 | if ( 'completed' === $migration_status || 'failed' === $migration_status || 'aborted' === $migration_status ) { |
| 60 | $push = new Push(); |
| 61 | $push->set_status( $push->statuses[ $migration_status ] ); |
| 62 | $this->tracker->update_track( $push ); |
| 63 | |
| 64 | if ( isset( $response['data']['source_site_url'] ) ) { |
| 65 | $source_site_url = $response['data']['source_site_url']; |
| 66 | if ( ! wp_next_scheduled( 'nfd_migration_source_hosting_info' ) ) { |
| 67 | wp_schedule_single_event( time() + 60, 'nfd_migration_source_hosting_info', array( 'source_site_url' => $source_site_url ) ); |
| 68 | } |
| 69 | if ( 'completed' === $migration_status ) { |
| 70 | if ( ! wp_next_scheduled( 'nfd_migration_page_speed_source' ) ) { |
| 71 | wp_schedule_single_event( time() + 90, 'nfd_migration_page_speed_source', array( 'source_site_url' => $source_site_url ) ); |
| 72 | } |
| 73 | if ( ! wp_next_scheduled( 'nfd_migration_page_speed_destination' ) ) { |
| 74 | wp_schedule_single_event( |
| 75 | time() + 120, |
| 76 | 'nfd_migration_page_speed_destination', |
| 77 | array( |
| 78 | 'source_site_url' => $source_site_url, |
| 79 | 'migrate_group_uuid' => $migrate_group_uuid, |
| 80 | 'status' => $migration_status, |
| 81 | ), |
| 82 | ); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if ( 'completed' === $migration_status ) { |
| 89 | $migration_complete = new LastStep(); |
| 90 | $migration_complete->set_status( $migration_complete->statuses['completed'] ); |
| 91 | $this->tracker->update_track( $migration_complete ); |
| 92 | } elseif ( 'failed' === $migration_status ) { |
| 93 | $migration_complete = new LastStep(); |
| 94 | $migration_complete->set_status( $migration_complete->statuses['failed'] ); |
| 95 | $this->tracker->update_track( $migration_complete ); |
| 96 | EventService::send_application_event( |
| 97 | 'migration_failed', |
| 98 | $this->tracker->get_track_content() |
| 99 | ); |
| 100 | } elseif ( 'aborted' === $migration_status ) { |
| 101 | $migration_complete = new LastStep(); |
| 102 | $migration_complete->set_status( $migration_complete->statuses['aborted'] ); |
| 103 | $this->tracker->update_track( $migration_complete ); |
| 104 | EventService::send_application_event( |
| 105 | 'migration_aborted', |
| 106 | $this->tracker->get_track_content() |
| 107 | ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return $new_value; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Listen instaWp option update to intercept the Push step and track it |
| 118 | * |
| 119 | * @param array $new_value status of migration |
| 120 | * @param array $old_value previous status of migration |
| 121 | * @return array |
| 122 | */ |
| 123 | public function on_update_instawp_migration_details( $new_value, $old_value ) { |
| 124 | if ( $old_value !== $new_value ) { |
| 125 | $mode = isset( $new_value['mode'] ) ? $new_value['mode'] : ''; |
| 126 | $status = isset( $new_value['status'] ) ? $new_value['status'] : ''; |
| 127 | if ( 'push' === $mode && 'initiated' === $status ) { |
| 128 | $push = new Push(); |
| 129 | $this->tracker->update_track( $push ); |
| 130 | } |
| 131 | } |
| 132 | return $new_value; |
| 133 | } |
| 134 | /** |
| 135 | * Get source site hosting informations. |
| 136 | * |
| 137 | * @param string $source_site_url source site url. |
| 138 | * @return void |
| 139 | */ |
| 140 | public function source_hosting_info( $source_site_url ) { |
| 141 | $source_hosting_info = new SourceHostingInfo( $source_site_url ); |
| 142 | $this->tracker->update_track( $source_hosting_info ); |
| 143 | |
| 144 | if ( ! $source_hosting_info->failed() ) { |
| 145 | $source_hosting_info->set_status( $source_hosting_info->statuses['completed'] ); |
| 146 | } |
| 147 | |
| 148 | $this->tracker->update_track( $source_hosting_info ); |
| 149 | } |
| 150 | /** |
| 151 | * Track page speed for source site. |
| 152 | * |
| 153 | * @param string $source_site_url source site url. |
| 154 | * @return void |
| 155 | */ |
| 156 | public function page_speed_source( $source_site_url ) { |
| 157 | $source_url_pagespeed = new PageSpeed( $source_site_url, 'source' ); |
| 158 | if ( ! $source_url_pagespeed->failed() ) { |
| 159 | $source_url_pagespeed->set_status( $source_url_pagespeed->statuses['completed'] ); |
| 160 | } |
| 161 | |
| 162 | $this->tracker->update_track( $source_url_pagespeed ); |
| 163 | } |
| 164 | /** |
| 165 | * Track page speed for source site. |
| 166 | * |
| 167 | * @param string $source_site_url source site url. |
| 168 | * @param string $migrate_group_uuid migrate group uuid. |
| 169 | * @param string $status status of migration. |
| 170 | * @return void |
| 171 | */ |
| 172 | public function page_speed_destination( $source_site_url, $migrate_group_uuid, $status ) { |
| 173 | try { |
| 174 | $source_url_pagespeed = new PageSpeed( site_url(), 'destination' ); |
| 175 | if ( ! $source_url_pagespeed->failed() ) { |
| 176 | $source_url_pagespeed->set_status( $source_url_pagespeed->statuses['completed'] ); |
| 177 | } |
| 178 | |
| 179 | $this->tracker->update_track( $source_url_pagespeed ); |
| 180 | } finally { |
| 181 | if ( ! get_option( 'nfd_migration_status_sent', false ) ) { |
| 182 | EventService::send_application_event( |
| 183 | 'migration_completed', |
| 184 | array_merge( |
| 185 | array( |
| 186 | 'migration_uuid' => $migrate_group_uuid, |
| 187 | ), |
| 188 | $this->tracker->get_track_content() |
| 189 | ), |
| 190 | ); |
| 191 | |
| 192 | // send specific data to the Migration Table Event |
| 193 | $tracked_datas = $this->tracker->get_track_content(); |
| 194 | $isp = $tracked_datas['SourceHostingInfo']['data']['SourceHostingData']['isp'] ?? 'N/A'; |
| 195 | $as = $tracked_datas['SourceHostingInfo']['data']['SourceHostingData']['as'] ?? 'N/A'; |
| 196 | $source_speed_index = $tracked_datas['PageSpeed_source']['data']['speedIndex'] ?? '0'; |
| 197 | $source_speed_index = str_replace( ' s', '', $source_speed_index ); |
| 198 | $destination_speed_index = $tracked_datas['PageSpeed_destination']['data']['speedIndex'] ?? 0; |
| 199 | $destination_speed_index = str_replace( ' s', '', $destination_speed_index ); |
| 200 | $status = 'completed' === $status ? 'successful' : $status; |
| 201 | $migration_infos = array( |
| 202 | 'migration_uuid' => $migrate_group_uuid, |
| 203 | 'status' => $status, |
| 204 | 'origin_url' => $source_site_url, |
| 205 | 'origin_isp' => $isp, |
| 206 | 'origin_as' => $as, |
| 207 | 'origin_page_speed' => $source_speed_index, |
| 208 | 'destination_page_speed' => $destination_speed_index, |
| 209 | ); |
| 210 | |
| 211 | EventService::send_application_event( "migration_$status", $migration_infos ); |
| 212 | update_option( 'nfd_migration_status_sent', true ); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |