AsyncPluginsInstallLogger.php
252 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Admin\PluginsInstallLoggers; |
| 4 | |
| 5 | /** |
| 6 | * A logger to log plugin installation progress in real time to an option. |
| 7 | */ |
| 8 | class AsyncPluginsInstallLogger implements PluginsInstallLogger { |
| 9 | |
| 10 | /** |
| 11 | * Variable to store logs. |
| 12 | * |
| 13 | * @var string $option_name option name to store logs. |
| 14 | */ |
| 15 | private $option_name; |
| 16 | |
| 17 | /** |
| 18 | * Constructor. |
| 19 | * |
| 20 | * @param string $option_name option name. |
| 21 | */ |
| 22 | public function __construct( string $option_name ) { |
| 23 | $this->option_name = $option_name; |
| 24 | add_option( |
| 25 | $this->option_name, |
| 26 | array( |
| 27 | 'created_time' => time(), |
| 28 | 'status' => 'pending', |
| 29 | 'plugins' => array(), |
| 30 | ), |
| 31 | '', |
| 32 | 'no' |
| 33 | ); |
| 34 | |
| 35 | // Set status as failed in case we run out of execution time. |
| 36 | register_shutdown_function( |
| 37 | function () { |
| 38 | $error = error_get_last(); |
| 39 | if ( isset( $error['type'] ) && E_ERROR === $error['type'] ) { |
| 40 | $option = $this->get(); |
| 41 | $option['status'] = 'failed'; |
| 42 | $this->update( $option ); |
| 43 | } |
| 44 | } |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Update the option. |
| 50 | * |
| 51 | * @param array $data New data. |
| 52 | * |
| 53 | * @return bool |
| 54 | */ |
| 55 | private function update( array $data ) { |
| 56 | return update_option( $this->option_name, $data ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Retrieve the option. |
| 61 | * |
| 62 | * @return false|mixed|void |
| 63 | */ |
| 64 | private function get() { |
| 65 | return get_option( $this->option_name ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add requested plugin. |
| 70 | * |
| 71 | * @param string $plugin_name plugin name. |
| 72 | * |
| 73 | * @return void |
| 74 | */ |
| 75 | public function install_requested( string $plugin_name ) { |
| 76 | $option = $this->get(); |
| 77 | if ( ! isset( $option['plugins'][ $plugin_name ] ) ) { |
| 78 | $option['plugins'][ $plugin_name ] = array( |
| 79 | 'status' => 'installing', |
| 80 | 'errors' => array(), |
| 81 | 'install_duration' => 0, |
| 82 | ); |
| 83 | } |
| 84 | $this->update( $option ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Add installed plugin. |
| 89 | * |
| 90 | * @param string $plugin_name plugin name. |
| 91 | * @param int $duration time took to install plugin. |
| 92 | * |
| 93 | * @return void |
| 94 | */ |
| 95 | public function installed( string $plugin_name, int $duration ) { |
| 96 | $option = $this->get(); |
| 97 | |
| 98 | $option['plugins'][ $plugin_name ]['status'] = 'installed'; |
| 99 | $option['plugins'][ $plugin_name ]['install_duration'] = $duration; |
| 100 | $this->update( $option ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Change status to activated. |
| 105 | * |
| 106 | * @param string $plugin_name plugin name. |
| 107 | * |
| 108 | * @return void |
| 109 | */ |
| 110 | public function activated( string $plugin_name ) { |
| 111 | $option = $this->get(); |
| 112 | |
| 113 | $option['plugins'][ $plugin_name ]['status'] = 'activated'; |
| 114 | $this->update( $option ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Add an error. |
| 119 | * |
| 120 | * @param string $plugin_name plugin name. |
| 121 | * @param string|null $error_message error message. |
| 122 | * |
| 123 | * @return void |
| 124 | */ |
| 125 | public function add_error( string $plugin_name, ?string $error_message = null ) { |
| 126 | $option = $this->get(); |
| 127 | |
| 128 | $option['plugins'][ $plugin_name ]['errors'][] = $error_message; |
| 129 | $option['plugins'][ $plugin_name ]['status'] = 'failed'; |
| 130 | $option['status'] = 'failed'; |
| 131 | |
| 132 | wc_admin_record_tracks_event( |
| 133 | 'coreprofiler_store_extension_installed_and_activated', |
| 134 | array( |
| 135 | 'success' => false, |
| 136 | 'extension' => $this->get_plugin_track_key( $plugin_name ), |
| 137 | 'error_message' => $error_message, |
| 138 | ) |
| 139 | ); |
| 140 | |
| 141 | $this->update( $option ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Record completed_time. |
| 146 | * |
| 147 | * @param array $data return data from install_plugins(). |
| 148 | * @return void |
| 149 | */ |
| 150 | public function complete( $data = array() ) { |
| 151 | $option = $this->get(); |
| 152 | |
| 153 | $option['complete_time'] = time(); |
| 154 | $option['status'] = 'complete'; |
| 155 | |
| 156 | $this->track( $data ); |
| 157 | $this->update( $option ); |
| 158 | } |
| 159 | |
| 160 | private function get_plugin_track_key( $id ) { |
| 161 | $slug = explode( ':', $id )[0]; |
| 162 | $key = preg_match( '/^woocommerce(-|_)payments$/', $slug ) |
| 163 | ? 'wcpay' |
| 164 | : explode( ':', str_replace( '-', '_', $slug ) )[0]; |
| 165 | return $key; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns time frame for a given time in milliseconds. |
| 170 | * |
| 171 | * @param int $timeInMs - time in milliseconds |
| 172 | * |
| 173 | * @return string - Time frame. |
| 174 | */ |
| 175 | function get_timeframe( $timeInMs ) { |
| 176 | $time_frames = array( |
| 177 | array( |
| 178 | 'name' => '0-2s', |
| 179 | 'max' => 2, |
| 180 | ), |
| 181 | array( |
| 182 | 'name' => '2-5s', |
| 183 | 'max' => 5, |
| 184 | ), |
| 185 | array( |
| 186 | 'name' => '5-10s', |
| 187 | 'max' => 10, |
| 188 | ), |
| 189 | array( |
| 190 | 'name' => '10-15s', |
| 191 | 'max' => 15, |
| 192 | ), |
| 193 | array( |
| 194 | 'name' => '15-20s', |
| 195 | 'max' => 20, |
| 196 | ), |
| 197 | array( |
| 198 | 'name' => '20-30s', |
| 199 | 'max' => 30, |
| 200 | ), |
| 201 | array( |
| 202 | 'name' => '30-60s', |
| 203 | 'max' => 60, |
| 204 | ), |
| 205 | array( 'name' => '>60s' ), |
| 206 | ); |
| 207 | |
| 208 | foreach ( $time_frames as $time_frame ) { |
| 209 | if ( ! isset( $time_frame['max'] ) ) { |
| 210 | return $time_frame['name']; |
| 211 | } |
| 212 | if ( $timeInMs < $time_frame['max'] * 1000 ) { |
| 213 | return $time_frame['name']; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | private function track( $data ) { |
| 219 | $track_data = array( |
| 220 | 'success' => true, |
| 221 | 'installed_extensions' => array_map( |
| 222 | function ( $extension ) { |
| 223 | return $this->get_plugin_track_key( $extension ); |
| 224 | }, |
| 225 | $data['installed'] |
| 226 | ), |
| 227 | 'total_time' => $this->get_timeframe( ( time() - $data['start_time'] ) * 1000 ), |
| 228 | ); |
| 229 | |
| 230 | foreach ( $data['installed'] as $plugin ) { |
| 231 | if ( ! isset( $data['time'][ $plugin ] ) ) { |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | $plugin_track_key = $this->get_plugin_track_key( $plugin ); |
| 236 | $install_time = $this->get_timeframe( $data['time'][ $plugin ] ); |
| 237 | $track_data[ 'install_time_' . $plugin_track_key ] = $install_time; |
| 238 | |
| 239 | wc_admin_record_tracks_event( |
| 240 | 'coreprofiler_store_extension_installed_and_activated', |
| 241 | array( |
| 242 | 'success' => true, |
| 243 | 'extension' => $plugin_track_key, |
| 244 | 'install_time' => $install_time, |
| 245 | ) |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | wc_admin_record_tracks_event( 'coreprofiler_store_extensions_installed_and_activated', $track_data ); |
| 250 | } |
| 251 | } |
| 252 |