| @@ -1,75 +1,65 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | /** |
| 3 | - * | |
| 4 | - * @author palant@UD | |
| 4 | + * CLI Sync Implementation | |
| 5 | + * | |
| 6 | + * @author palant@UDX | |
| 7 | + * @author korotkov@UDX | |
| 5 | 8 | */ |
| 9 | + | |
| 10 | +use wpCloud\StatelessMedia\FatalException; | |
| 11 | +use wpCloud\StatelessMedia\UnprocessableException; | |
| 6 | 12 | use wpCloud\StatelessMedia\Utility; |
| 7 | 13 | |
| 8 | -if( !class_exists( 'SM_CLI_Scaffold' ) ) { | |
| 9 | - require_once( dirname( __FILE__ ) . '/class-sm-cli-scaffold.php' ); | |
| 14 | +if (!class_exists('SM_CLI_Scaffold')) { | |
| 15 | + require_once(dirname(__FILE__) . '/class-sm-cli-scaffold.php'); | |
| 10 | 16 | } |
| 11 | 17 | |
| 12 | 18 | class SM_CLI_Sync extends SM_CLI_Scaffold { |
| 13 | 19 | |
| 14 | 20 | /** |
| 15 | - * | |
| 21 | + * Order | |
| 16 | 22 | */ |
| 17 | - public $force = false; | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * | |
| 21 | - */ | |
| 22 | - public $continue = false; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * | |
| 26 | - */ | |
| 27 | - public $fix = false; | |
| 28 | - | |
| 29 | - /** | |
| 30 | - * | |
| 31 | - */ | |
| 32 | 23 | public $order = ""; |
| 33 | 24 | |
| 34 | 25 | /** |
| 35 | - * | |
| 26 | + * Start | |
| 36 | 27 | */ |
| 37 | 28 | public $start = false; |
| 38 | 29 | |
| 39 | 30 | /** |
| 40 | - * | |
| 31 | + * End | |
| 41 | 32 | */ |
| 42 | 33 | public $end = false; |
| 43 | 34 | |
| 44 | 35 | /** |
| 45 | - * | |
| 36 | + * Limit | |
| 46 | 37 | */ |
| 47 | 38 | public $limit = false; |
| 48 | 39 | |
| 49 | 40 | /** |
| 50 | - * | |
| 41 | + * Batch | |
| 51 | 42 | */ |
| 52 | 43 | public $batch = false; |
| 53 | 44 | |
| 54 | 45 | /** |
| 55 | - * | |
| 46 | + * Batches | |
| 56 | 47 | */ |
| 57 | 48 | public $batches = false; |
| 58 | 49 | |
| 59 | 50 | /** |
| 60 | - * | |
| 51 | + * Total | |
| 61 | 52 | */ |
| 62 | 53 | public $total = 0; |
| 63 | 54 | |
| 64 | 55 | /** |
| 65 | - * | |
| 56 | + * Log | |
| 66 | 57 | */ |
| 67 | 58 | public $log; |
| 68 | 59 | |
| 69 | 60 | /** |
| 70 | 61 | * Sync images |
| 71 | - * | |
| 72 | 62 | */ |
| 73 | 63 | public function images() { |
| 74 | 64 | global $wpdb; |
| 75 | 65 | |
| @@ -77,52 +67,25 @@ | ||
| 77 | 67 | $this->_prepare(); |
| 78 | 68 | |
| 79 | 69 | $timer = time(); |
| 80 | 70 | |
| 81 | - $where = ''; | |
| 82 | - | |
| 83 | - if ( $this->fix ) { | |
| 84 | - $failed_attachments = Utility::sync_get_fails( 'cli_images' ); | |
| 85 | - | |
| 86 | - if ( !empty($failed_attachments) ) { | |
| 87 | - $where .= " AND ID IN (" . implode(',', $failed_attachments) . ") "; | |
| 88 | - } else { | |
| 89 | - WP_CLI::success( "Stateless Media is synced. No failed attachments." ); | |
| 90 | - exit; | |
| 91 | - } | |
| 92 | - } | |
| 93 | - | |
| 94 | - if ( $this->continue ) { | |
| 95 | - $current_progress = Utility::sync_retrieve_current_progress( 'cli_images' ); | |
| 96 | - | |
| 97 | - if ( isset($current_progress[1]) && !empty( $current_progress[1] )) { | |
| 98 | - $where .= " AND ID " . ( $this->order === 'DESC' ? ' < ' : ' > ' ) . $current_progress[1]; | |
| 99 | - } | |
| 100 | - } | |
| 101 | - | |
| 102 | - if ( ! $this->force ) { | |
| 103 | - $where .= " AND meta_id IS NULL " ; | |
| 104 | - } | |
| 105 | - | |
| 106 | 71 | /** Get Total Amount of Attachments */ |
| 107 | 72 | $this->total = $wpdb->get_var(" |
| 108 | - SELECT | |
| 109 | - COUNT(ID) | |
| 110 | - FROM {$wpdb->posts} | |
| 111 | - LEFT JOIN {$wpdb->postmeta} ON ID = post_id AND meta_key = 'sm_cloud' | |
| 112 | - WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' " . $where . " | |
| 113 | - "); | |
| 73 | + SELECT | |
| 74 | + COUNT(ID) | |
| 75 | + FROM {$wpdb->posts} | |
| 76 | + WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' | |
| 77 | + "); | |
| 114 | 78 | |
| 115 | - | |
| 116 | - if( $this->batch ) { | |
| 117 | - $this->output( "Running Batch {$this->batch} from {$this->batches}" ); | |
| 118 | - $range = round( $this->total / $this->batches ); | |
| 119 | - $this->start = ( $this->batch * $range ) - $range; | |
| 79 | + if ($this->batch) { | |
| 80 | + $this->output("Running Batch {$this->batch} from {$this->batches}"); | |
| 81 | + $range = round($this->total / $this->batches); | |
| 82 | + $this->start = ($this->batch * $range) - $range; | |
| 120 | 83 | $this->end = $this->batches == $this->batch ? $this->total : $this->batch * $range; |
| 121 | - $this->output( "Starting from {$this->start} row. " ); | |
| 122 | - $this->output( "And proceeding up to {$this->end} row." ); | |
| 84 | + $this->output("Starting from {$this->start} row. "); | |
| 85 | + $this->output("And proceeding up to {$this->end} row."); | |
| 123 | 86 | } else { |
| 124 | - $this->output( "Running in default way. Starting from {$this->start} row and proceeding up to end." ); | |
| 87 | + $this->output("Running in default way. Starting from {$this->start} row and proceeding up to end."); | |
| 125 | 88 | $this->end = $this->end ? $this->end : $this->total; |
| 126 | 89 | } |
| 127 | 90 | $media_to_proceed = $this->end - $this->start; |
| 128 | 91 | |
| @@ -128,40 +91,45 @@ | ||
| 128 | 91 | |
| 129 | 92 | //** Counters */ |
| 130 | 93 | $synced_images = 0; |
| 131 | 94 | |
| 132 | - WP_CLI::line( 'Starting extract attachments.' ); | |
| 95 | + WP_CLI::line('Starting extract attachments.'); | |
| 133 | 96 | |
| 134 | - for( $this->start; $this->start < $this->end; $this->start += $this->limit ) { | |
| 97 | + set_time_limit(0); | |
| 135 | 98 | |
| 136 | - $limit = ( $this->end - $this->start ) < $this->limit ? ( $this->end - $this->start ) : $this->limit; | |
| 99 | + for ($this->start; $this->start < $this->end; $this->start += $this->limit) { | |
| 137 | 100 | |
| 138 | - $this->output( 'Synced: ' . $synced_images . '. Extracting from ' . ( $this->start + 1 ) . ' to ' . ( $this->start + $limit ) ); | |
| 101 | + $limit = ($this->end - $this->start) < $this->limit ? ($this->end - $this->start) : $this->limit; | |
| 139 | 102 | |
| 103 | + $this->output('Synced: ' . $synced_images . '. Extracting from ' . ($this->start + 1) . ' to ' . ($this->start + $limit)); | |
| 104 | + | |
| 140 | 105 | /** |
| 141 | 106 | * Get Attachments data. |
| 142 | - * | |
| 143 | 107 | */ |
| 144 | - $attachments = $wpdb->get_results( $wpdb->prepare( " | |
| 145 | - SELECT | |
| 146 | - ID | |
| 147 | - FROM {$wpdb->posts} | |
| 148 | - LEFT JOIN {$wpdb->postmeta} ON ID = post_id AND meta_key = 'sm_cloud' | |
| 149 | - WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' " . $where . " ORDER BY ID " . $this->order . " | |
| 150 | - LIMIT %d, %d; | |
| 151 | - ", $this->start, $limit ), ARRAY_A ); | |
| 108 | + $attachments = $wpdb->get_results($wpdb->prepare(" | |
| 109 | + SELECT ID | |
| 110 | + FROM {$wpdb->posts} | |
| 111 | + WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID {$this->order} | |
| 112 | + LIMIT %d, %d; | |
| 113 | + ", $this->start, $limit), ARRAY_A); | |
| 152 | 114 | |
| 153 | - if ( ! empty( $attachments ) ) { | |
| 154 | - foreach ($attachments as $i => $a) { | |
| 155 | - | |
| 115 | + if (!empty($attachments)) { | |
| 116 | + foreach ($attachments as $a) { | |
| 156 | 117 | try { |
| 157 | - $response = $this->stateless_process_image($a['ID']); | |
| 118 | + timer_start(); | |
| 119 | + $response = Utility::process_image_by_id(intval($a['ID'])); | |
| 158 | 120 | if (!empty($response)) { |
| 159 | 121 | $synced_images++; |
| 160 | - $this->output($response); | |
| 122 | + $this->output(sprintf(__('%1$s (ID %2$s) was successfully synced in %3$s seconds.', ud_get_stateless_media()->domain), esc_html(get_the_title($response->ID)), $response->ID, timer_stop())); | |
| 161 | 123 | } |
| 162 | - } catch( \Exception $e ) { | |
| 124 | + } catch (FatalException $e) { | |
| 163 | 125 | $this->output($e->getMessage()); |
| 126 | + break; | |
| 127 | + } catch (UnprocessableException $e) { | |
| 128 | + $this->output($e->getMessage()); | |
| 129 | + } catch (\Throwable $e) { | |
| 130 | + $this->output($e->getMessage()); | |
| 131 | + break; | |
| 164 | 132 | } |
| 165 | 133 | |
| 166 | 134 | /** Flush data */ |
| 167 | 135 | $wpdb->flush(); |
| @@ -166,27 +134,22 @@ | ||
| 166 | 134 | /** Flush data */ |
| 167 | 135 | $wpdb->flush(); |
| 168 | 136 | @ob_flush(); |
| 169 | 137 | @flush(); |
| 170 | - | |
| 171 | 138 | } |
| 172 | 139 | |
| 173 | 140 | unset($attachments); |
| 174 | 141 | } |
| 175 | - | |
| 176 | 142 | } |
| 177 | - WP_CLI::success( "Stateless Media is synced" ); | |
| 178 | - | |
| 179 | - WP_CLI::line( 'Media which have been checked: ' . number_format_i18n( $media_to_proceed ) ); | |
| 180 | - WP_CLI::line( 'Synced stateless for ' . number_format_i18n( $synced_images ) . ' attachments' ); | |
| 181 | - WP_CLI::line( 'Spent Time: ' . ( time() - $timer ) . ' sec' ); | |
| 182 | - | |
| 143 | + WP_CLI::success("Stateless Media is synced"); | |
| 144 | + WP_CLI::line('Media which have been checked: ' . number_format_i18n($media_to_proceed)); | |
| 145 | + WP_CLI::line('Synced stateless for ' . number_format_i18n($synced_images) . ' attachments'); | |
| 146 | + WP_CLI::line('Spent Time: ' . (time() - $timer) . ' sec'); | |
| 183 | 147 | } |
| 184 | 148 | |
| 185 | 149 | |
| 186 | 150 | /** |
| 187 | 151 | * Sync files |
| 188 | - * | |
| 189 | 152 | */ |
| 190 | 153 | public function files() { |
| 191 | 154 | global $wpdb; |
| 192 | 155 | |
| @@ -194,52 +157,25 @@ | ||
| 194 | 157 | $this->_prepare(); |
| 195 | 158 | |
| 196 | 159 | $timer = time(); |
| 197 | 160 | |
| 198 | - $where = ''; | |
| 199 | - | |
| 200 | - if ( $this->fix ) { | |
| 201 | - $failed_attachments = Utility::sync_get_fails( 'cli_images' ); | |
| 202 | - | |
| 203 | - if ( !empty( $failed_attachments ) ) { | |
| 204 | - $where .= " AND ID IN (" . implode(',', $failed_attachments) . ") "; | |
| 205 | - } else { | |
| 206 | - WP_CLI::success( "Stateless Media is synced. No failed attachments." ); | |
| 207 | - exit; | |
| 208 | - } | |
| 209 | - } | |
| 210 | - | |
| 211 | - if ( $this->continue ) { | |
| 212 | - $current_progress = Utility::sync_retrieve_current_progress( 'cli_other' ); | |
| 213 | - | |
| 214 | - if ( isset( $current_progress[1] ) && !empty( $current_progress[1] )) { | |
| 215 | - $where .= " AND ID " . ( $this->order === 'DESC' ? ' < ' : ' > ' ) . $current_progress[1]; | |
| 216 | - } | |
| 217 | - } | |
| 218 | - | |
| 219 | - if ( ! $this->force ) { | |
| 220 | - $where .= " AND meta_id IS NULL " ; | |
| 221 | - } | |
| 222 | - | |
| 223 | 161 | /** Get Total Amount of Attachments */ |
| 224 | 162 | $this->total = $wpdb->get_var(" |
| 225 | - SELECT | |
| 226 | - COUNT(ID) | |
| 227 | - FROM {$wpdb->posts} | |
| 228 | - LEFT JOIN {$wpdb->postmeta} ON ID = post_id AND meta_key = 'sm_cloud' | |
| 229 | - WHERE post_type = 'attachment' AND post_mime_type NOT LIKE 'image/%' " . $where . " | |
| 230 | - "); | |
| 163 | + SELECT | |
| 164 | + COUNT(ID) | |
| 165 | + FROM {$wpdb->posts} | |
| 166 | + WHERE post_type = 'attachment' AND post_mime_type NOT LIKE 'image/%' | |
| 167 | + "); | |
| 231 | 168 | |
| 232 | - | |
| 233 | - if( $this->batch ) { | |
| 234 | - $this->output( "Running Batch {$this->batch} from {$this->batches}" ); | |
| 235 | - $range = round( $this->total / $this->batches ); | |
| 236 | - $this->start = ( $this->batch * $range ) - $range; | |
| 169 | + if ($this->batch) { | |
| 170 | + $this->output("Running Batch {$this->batch} from {$this->batches}"); | |
| 171 | + $range = round($this->total / $this->batches); | |
| 172 | + $this->start = ($this->batch * $range) - $range; | |
| 237 | 173 | $this->end = $this->batches == $this->batch ? $this->total : $this->batch * $range; |
| 238 | - $this->output( "Starting from {$this->start} row. " ); | |
| 239 | - $this->output( "And proceeding up to {$this->end} row." ); | |
| 174 | + $this->output("Starting from {$this->start} row. "); | |
| 175 | + $this->output("And proceeding up to {$this->end} row."); | |
| 240 | 176 | } else { |
| 241 | - $this->output( "Running in default way. Starting from {$this->start} row and proceeding up to end." ); | |
| 177 | + $this->output("Running in default way. Starting from {$this->start} row and proceeding up to end."); | |
| 242 | 178 | $this->end = $this->end ? $this->end : $this->total; |
| 243 | 179 | } |
| 244 | 180 | $media_to_proceed = $this->end - $this->start; |
| 245 | 181 | |
| @@ -245,40 +181,45 @@ | ||
| 245 | 181 | |
| 246 | 182 | //** Counters */ |
| 247 | 183 | $synced_files = 0; |
| 248 | 184 | |
| 249 | - WP_CLI::line( 'Starting extract attachments.' ); | |
| 185 | + WP_CLI::line('Starting extract attachments.'); | |
| 250 | 186 | |
| 251 | - for( $this->start; $this->start < $this->end; $this->start += $this->limit ) { | |
| 187 | + set_time_limit(0); | |
| 252 | 188 | |
| 253 | - $limit = ( $this->end - $this->start ) < $this->limit ? ( $this->end - $this->start ) : $this->limit; | |
| 189 | + for ($this->start; $this->start < $this->end; $this->start += $this->limit) { | |
| 254 | 190 | |
| 255 | - $this->output( 'Synced: ' . $synced_files . '. Extracting from ' . ( $this->start + 1 ) . ' to ' . ( $this->start + $limit ) ); | |
| 191 | + $limit = ($this->end - $this->start) < $this->limit ? ($this->end - $this->start) : $this->limit; | |
| 256 | 192 | |
| 193 | + $this->output('Synced: ' . $synced_files . '. Extracting from ' . ($this->start + 1) . ' to ' . ($this->start + $limit)); | |
| 194 | + | |
| 257 | 195 | /** |
| 258 | 196 | * Get Attachments data. |
| 259 | - * | |
| 260 | 197 | */ |
| 261 | - $attachments = $wpdb->get_results( $wpdb->prepare( " | |
| 262 | - SELECT | |
| 263 | - ID | |
| 264 | - FROM {$wpdb->posts} | |
| 265 | - LEFT JOIN {$wpdb->postmeta} ON ID = post_id AND meta_key = 'sm_cloud' | |
| 266 | - WHERE post_type = 'attachment' AND post_mime_type NOT LIKE 'image/%' " . $where . " ORDER BY ID " . $this->order . " | |
| 267 | - LIMIT %d, %d; | |
| 268 | - ", $this->start, $limit ), ARRAY_A ); | |
| 198 | + $attachments = $wpdb->get_results($wpdb->prepare(" | |
| 199 | + SELECT ID | |
| 200 | + FROM {$wpdb->posts} | |
| 201 | + WHERE post_type = 'attachment' AND post_mime_type NOT LIKE 'image/%' ORDER BY ID {$this->order} | |
| 202 | + LIMIT %d, %d; | |
| 203 | + ", $this->start, $limit), ARRAY_A); | |
| 269 | 204 | |
| 270 | - if ( ! empty( $attachments ) ) { | |
| 271 | - foreach ($attachments as $i => $a) { | |
| 272 | - | |
| 205 | + if (!empty($attachments)) { | |
| 206 | + foreach ($attachments as $a) { | |
| 273 | 207 | try { |
| 274 | - $response = $this->stateless_process_file($a['ID']); | |
| 208 | + timer_start(); | |
| 209 | + $response = Utility::process_file_by_id(intval($a['ID'])); | |
| 275 | 210 | if (!empty($response)) { |
| 276 | 211 | $synced_files++; |
| 277 | - $this->output($response); | |
| 212 | + $this->output(sprintf(__('%1$s (ID %2$s) was successfully synchronised in %3$s seconds.', ud_get_stateless_media()->domain), esc_html(get_the_title($response->ID)), $response->ID, timer_stop())); | |
| 278 | 213 | } |
| 279 | - } catch( \Exception $e ) { | |
| 214 | + } catch (FatalException $e) { | |
| 280 | 215 | $this->output($e->getMessage()); |
| 216 | + break; | |
| 217 | + } catch (UnprocessableException $e) { | |
| 218 | + $this->output($e->getMessage()); | |
| 219 | + } catch (\Throwable $e) { | |
| 220 | + $this->output($e->getMessage()); | |
| 221 | + break; | |
| 281 | 222 | } |
| 282 | 223 | |
| 283 | 224 | /** Flush data */ |
| 284 | 225 | $wpdb->flush(); |
| @@ -283,194 +224,45 @@ | ||
| 283 | 224 | /** Flush data */ |
| 284 | 225 | $wpdb->flush(); |
| 285 | 226 | @ob_flush(); |
| 286 | 227 | @flush(); |
| 287 | - | |
| 288 | 228 | } |
| 289 | 229 | |
| 290 | 230 | unset($attachments); |
| 291 | 231 | } |
| 292 | - | |
| 293 | 232 | } |
| 294 | - WP_CLI::success( "Stateless Media is synced" ); | |
| 295 | - | |
| 296 | - WP_CLI::line( 'Media which have been checked: ' . number_format_i18n( $media_to_proceed ) ); | |
| 297 | - WP_CLI::line( 'Synced stateless for ' . number_format_i18n( $synced_files ) . ' attachments' ); | |
| 298 | - WP_CLI::line( 'Spent Time: ' . ( time() - $timer ) . ' sec' ); | |
| 299 | - | |
| 233 | + WP_CLI::success("Stateless Media is synced"); | |
| 234 | + WP_CLI::line('Media which have been checked: ' . number_format_i18n($media_to_proceed)); | |
| 235 | + WP_CLI::line('Synced stateless for ' . number_format_i18n($synced_files) . ' attachments'); | |
| 236 | + WP_CLI::line('Spent Time: ' . (time() - $timer) . ' sec'); | |
| 300 | 237 | } |
| 301 | 238 | |
| 302 | - | |
| 303 | 239 | /** |
| 304 | - * @param $id | |
| 305 | - * @return string | |
| 306 | - * @throws Exception | |
| 307 | - * Regenerate image sizes. | |
| 240 | + * Prepare | |
| 308 | 241 | */ |
| 309 | - public function stateless_process_image( $id ) { | |
| 310 | - | |
| 311 | - if(ud_get_stateless_media()->is_connected_to_gs() !== true){ | |
| 312 | - //WP_CLI::line( 'Starting extract attachments.' ); | |
| 313 | - throw new \Exception( __( 'Not connected to GCS', ud_get_stateless_media()->domain) ); | |
| 314 | - } | |
| 315 | - | |
| 316 | - @error_reporting( 0 ); | |
| 317 | - timer_start(); | |
| 318 | - | |
| 319 | - $image = get_post( $id ); | |
| 320 | - | |
| 321 | - if ( ! $image || 'attachment' != $image->post_type || 'image/' != substr( $image->post_mime_type, 0, 6 ) ) | |
| 322 | - throw new \Exception( sprintf( __( 'Failed resize: %s is an invalid image ID.', ud_get_stateless_media()->domain ), esc_html( $id ) ) ); | |
| 323 | - | |
| 324 | - $fullsizepath = get_attached_file( $image->ID ); | |
| 325 | - $upload_dir = wp_upload_dir(); | |
| 326 | - | |
| 327 | - // If no file found | |
| 328 | - if ( false === $fullsizepath || ! file_exists( $fullsizepath ) ) { | |
| 329 | - | |
| 330 | - // Try get it and save | |
| 331 | - $result_code = ud_get_stateless_media()->get_client()->get_media( apply_filters( 'wp_stateless_file_name', str_replace( trailingslashit( $upload_dir[ 'basedir' ] ), '', $fullsizepath ), true, $id, "", $this->use_wildcards), true, $fullsizepath ); | |
| 332 | - | |
| 333 | - if ( $result_code !== 200 ) { | |
| 334 | - if(!Utility::sync_get_attachment_if_exist($image->ID, $fullsizepath)){ // Save file to local from proxy. | |
| 335 | - Utility::sync_store_failed_attachment( $image->ID, 'cli_images' ); | |
| 336 | - throw new \Exception(sprintf(__('Both local and remote files are missing. Unable to resize. (%s)', ud_get_stateless_media()->domain), $image->guid)); | |
| 337 | - } | |
| 338 | - } | |
| 339 | - } | |
| 340 | - | |
| 341 | - @set_time_limit( -1 ); | |
| 342 | - | |
| 343 | - // | |
| 344 | - do_action( 'sm:pre::synced::image', $image->ID); | |
| 345 | - $metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath ); | |
| 346 | - | |
| 347 | - if(get_post_mime_type($image->ID) !== 'image/svg+xml'){ | |
| 348 | - if ( is_wp_error( $metadata ) ) { | |
| 349 | - Utility::sync_store_failed_attachment( $image->ID, 'cli_images' ); | |
| 350 | - throw new \Exception($metadata->get_error_message()); | |
| 351 | - } | |
| 352 | - | |
| 353 | - if ( empty( $metadata ) ) { | |
| 354 | - Utility::sync_store_failed_attachment( $image->ID, 'cli_images' ); | |
| 355 | - throw new \Exception(sprintf( __('No metadata generated for %1$s (ID %2$s).', ud_get_stateless_media()->domain), esc_html( get_the_title( $image->ID ) ), $image->ID)); | |
| 356 | - } | |
| 357 | - } | |
| 358 | - | |
| 359 | - // If this fails, then it just means that nothing was changed (old value == new value) | |
| 360 | - wp_update_attachment_metadata( $image->ID, $metadata ); | |
| 361 | - Utility::sync_store_current_progress( 'cli_images', $image->ID, true ); | |
| 362 | - Utility::sync_maybe_fix_failed_attachment( 'cli_images', $image->ID ); | |
| 363 | - do_action( 'sm:synced::image', $image->ID, $metadata); | |
| 364 | - | |
| 365 | - return sprintf( __( '%1$s (ID %2$s) was successfully synced in %3$s seconds.', ud_get_stateless_media()->domain ), esc_html( get_the_title( $image->ID ) ), $image->ID, timer_stop() ); | |
| 366 | - } | |
| 367 | - | |
| 368 | - /** | |
| 369 | - * @param $id | |
| 370 | - * @return string | |
| 371 | - * @throws \Exception | |
| 372 | - */ | |
| 373 | - public function stateless_process_file( $id ) { | |
| 374 | - | |
| 375 | - @error_reporting( 0 ); | |
| 376 | - timer_start(); | |
| 377 | - | |
| 378 | - if(ud_get_stateless_media()->is_connected_to_gs() !== true){ | |
| 379 | - throw new \Exception( __( 'Not connected to GCS', ud_get_stateless_media()->domain) ); | |
| 380 | - } | |
| 381 | - | |
| 382 | - $file = get_post( $id ); | |
| 383 | - | |
| 384 | - if ( ! $file || 'attachment' != $file->post_type ) | |
| 385 | - throw new \Exception( sprintf( __( 'Attachment not found: %s is an invalid file ID.', ud_get_stateless_media()->domain ), esc_html( $id ) ) ); | |
| 386 | - | |
| 387 | - $fullsizepath = get_attached_file( $file->ID ); | |
| 388 | - $local_file_exists = file_exists( $fullsizepath ); | |
| 389 | - | |
| 390 | - $upload_dir = wp_upload_dir(); | |
| 391 | - | |
| 392 | - if ( false === $fullsizepath || ! $local_file_exists ) { | |
| 393 | - | |
| 394 | - // Try get it and save | |
| 395 | - $result_code = ud_get_stateless_media()->get_client()->get_media( str_replace( trailingslashit( $upload_dir[ 'basedir' ] ), '', $fullsizepath ), true, $fullsizepath ); | |
| 396 | - | |
| 397 | - if ( $result_code !== 200 ) { | |
| 398 | - if(!Utility::sync_get_attachment_if_exist($file->ID, $fullsizepath)){ // Save file to local from proxy. | |
| 399 | - Utility::sync_store_failed_attachment( $file->ID, 'cli_other' ); | |
| 400 | - throw new \Exception(sprintf(__('File not found (%s)', ud_get_stateless_media()->domain), $file->guid)); | |
| 401 | - } | |
| 402 | - else{ | |
| 403 | - $local_file_exists = true; | |
| 404 | - } | |
| 405 | - } | |
| 406 | - else{ | |
| 407 | - $local_file_exists = true; | |
| 408 | - } | |
| 409 | - } | |
| 410 | - | |
| 411 | - if($local_file_exists){ | |
| 412 | - | |
| 413 | - if ( !ud_get_stateless_media()->get_client()->media_exists( str_replace( trailingslashit( $upload_dir[ 'basedir' ] ), '', $fullsizepath ) ) ) { | |
| 414 | - | |
| 415 | - @set_time_limit( -1 ); | |
| 416 | - | |
| 417 | - $metadata = wp_generate_attachment_metadata( $file->ID, $fullsizepath ); | |
| 418 | - | |
| 419 | - if ( is_wp_error( $metadata ) ) { | |
| 420 | - Utility::sync_store_failed_attachment( $file->ID, 'cli_other' ); | |
| 421 | - throw new \Exception($metadata->get_error_message()); | |
| 422 | - } | |
| 423 | - | |
| 424 | - wp_update_attachment_metadata( $file->ID, $metadata ); | |
| 425 | - do_action( 'sm:synced::nonImage', $file->ID, $metadata); | |
| 426 | - | |
| 427 | - } | |
| 428 | - else{ | |
| 429 | - // Stateless mode: we don't need the local version. | |
| 430 | - if(ud_get_stateless_media()->get( 'sm.mode' ) === 'ephemeral'){ | |
| 431 | - unlink($fullsizepath); | |
| 432 | - } | |
| 433 | - } | |
| 434 | - | |
| 435 | - } | |
| 436 | - | |
| 437 | - Utility::sync_store_current_progress( 'cli_other', $file->ID, true ); | |
| 438 | - Utility::sync_maybe_fix_failed_attachment( 'cli_other', $file->ID ); | |
| 439 | - | |
| 440 | - return sprintf( __( '%1$s (ID %2$s) was successfully synchronised in %3$s seconds.', ud_get_stateless_media()->domain ), esc_html( get_the_title( $file->ID ) ), $file->ID, timer_stop() ); | |
| 441 | - } | |
| 442 | - | |
| 443 | - /** | |
| 444 | - * | |
| 445 | - */ | |
| 446 | 242 | private function _prepare() { |
| 447 | 243 | $args = $this->assoc_args; |
| 448 | - if( isset( $args[ 'b' ] ) ) { | |
| 449 | - WP_CLI::error( 'Invalid parameter --b. Command must not be run directly with --b parameter.' ); | |
| 244 | + if (isset($args['b'])) { | |
| 245 | + WP_CLI::error('Invalid parameter --b. Command must not be run directly with --b parameter.'); | |
| 450 | 246 | } |
| 451 | - $this->start = isset( $args[ 'start' ] ) && is_numeric( $args[ 'start' ] ) ? $args[ 'start' ] : 0; | |
| 452 | - $this->limit = isset( $args[ 'limit' ] ) && is_numeric( $args[ 'limit' ] ) ? $args[ 'limit' ] : 100; | |
| 453 | - $this->force = isset( $args[ 'force' ] ) ? true : false; | |
| 454 | - $this->continue = isset( $args[ 'continue' ] ) ? true : false; | |
| 455 | - $this->fix = isset( $args[ 'fix' ] ) ? true : false; | |
| 456 | - $this->use_wildcards = isset( $args[ 'use_wildcards' ] ) ? true : false; | |
| 457 | - $_REQUEST['use_wildcards'] = $this->use_wildcards; | |
| 458 | - $this->order = isset( $args[ 'order' ]) && $args[ 'order' ] === 'ASC' ? 'ASC' : 'DESC'; | |
| 459 | - if( isset( $args[ 'batch' ] ) ) { | |
| 460 | - if( !is_numeric( $args[ 'batch' ] ) || $args[ 'batch' ] <= 0 ) { | |
| 461 | - WP_CLI::error( 'Invalid parameter --batch' ); | |
| 247 | + $this->start = isset($args['start']) && is_numeric($args['start']) ? $args['start'] : 0; | |
| 248 | + $this->limit = isset($args['limit']) && is_numeric($args['limit']) ? $args['limit'] : 100; | |
| 249 | + $this->force = isset($args['force']) ? true : false; | |
| 250 | + $this->continue = isset($args['continue']) ? true : false; | |
| 251 | + $this->fix = isset($args['fix']) ? true : false; | |
| 252 | + $this->order = isset($args['order']) && strtoupper($args['order']) === 'ASC' ? 'ASC' : 'DESC'; | |
| 253 | + if (isset($args['batch'])) { | |
| 254 | + if (!is_numeric($args['batch']) || $args['batch'] <= 0) { | |
| 255 | + WP_CLI::error('Invalid parameter --batch'); | |
| 462 | 256 | } |
| 463 | - $this->batch = $args[ 'batch' ]; | |
| 464 | - $this->batches = isset( $args[ 'batches' ] ) ? $args[ 'batches' ] : 10; | |
| 465 | - if( !is_numeric( $this->batches ) || $this->batches <= 0 ) { | |
| 466 | - WP_CLI::error( 'Invalid parameter --batches' ); | |
| 467 | - } elseif ( $this->batch > $this->batches ) { | |
| 468 | - WP_CLI::error( '--batch parameter must is invalid. It must not equal or less then --batches' ); | |
| 257 | + $this->batch = $args['batch']; | |
| 258 | + $this->batches = isset($args['batches']) ? $args['batches'] : 10; | |
| 259 | + if (!is_numeric($this->batches) || $this->batches <= 0) { | |
| 260 | + WP_CLI::error('Invalid parameter --batches'); | |
| 261 | + } elseif ($this->batch > $this->batches) { | |
| 262 | + WP_CLI::error('--batch parameter must is invalid. It must not equal or less then --batches'); | |
| 469 | 263 | } |
| 470 | 264 | } else { |
| 471 | - $this->end = isset( $args[ 'end' ] ) && is_numeric( $args[ 'end' ] ) ? $args[ 'end' ] : false; | |
| 265 | + $this->end = isset($args['end']) && is_numeric($args['end']) ? $args['end'] : false; | |
| 472 | 266 | } |
| 473 | 267 | } |
| 474 | - | |
| 475 | - | |
| 476 | -} | |
| 268 | +} | |