PluginProbe
WP-Stateless – Google Cloud Storage / 2.2.0
WP-Stateless – Google Cloud Storage v2.2.0
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / cli / class-sm-cli-upgrade.php

class-sm-cli-upgrade.php in WP-Stateless – Google Cloud Storage 2.2.0, at lib/cli/class-sm-cli-upgrade.php

249 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 *
4 * @author peshkov@UD
5 */
6
7 if( !class_exists( 'SM_CLI_Scaffold' ) ) {
8 require_once( dirname( __FILE__ ) . '/class-sm-cli-scaffold.php' );
9 }
10
11 class SM_CLI_Upgrade extends SM_CLI_Scaffold {
12
13 /**
14 *
15 */
16 public $start = false;
17
18 /**
19 *
20 */
21 public $end = false;
22
23 /**
24 *
25 */
26 public $limit = false;
27
28 /**
29 *
30 */
31 public $batch = false;
32
33 /**
34 *
35 */
36 public $batches = false;
37
38 /**
39 *
40 */
41 public $total = 0;
42
43 /**
44 *
45 */
46 public $log;
47
48 /**
49 * Upgrade Meta.
50 *
51 */
52 public function meta() {
53 global $wpdb;
54
55 /** Prepare arguments */
56 $this->_prepare();
57
58 $timer = time();
59
60 /** Get Total Amount of Attachments */
61 $this->total = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type = 'attachment'" );
62
63 if( $this->batch ) {
64 $this->output( "Running Batch {$this->batch} from {$this->batches}" );
65 $range = round( $this->total / $this->batches );
66 $this->start = ( $this->batch * $range ) - $range;
67 $this->end = $this->batches == $this->batch ? $this->total : $this->batch * $range;
68 $this->output( "Starting from {$this->start} row. " );
69 $this->output( "And proceeding up to {$this->end} row." );
70 } else {
71 $this->output( "Running in default way. Starting from {$this->start} row and proceeding up to end." );
72 $this->end = $this->end ? $this->end : $this->total;
73 }
74 $media_to_proceed = $this->end - $this->start;
75
76 //** Counters */
77 $upgraded_meta = 0;
78
79 WP_CLI::line( 'Starting extract attachments.' );
80
81 for( $this->start; $this->start < $this->end; $this->start += $this->limit ) {
82
83 $limit = ( $this->end - $this->start ) < $this->limit ? ( $this->end - $this->start ) : $this->limit;
84
85 $this->output( 'Upgraded: ' . $upgraded_meta . '. Extracting from ' . ( $this->start + 1 ) . ' to ' . ( $this->start + $limit ) );
86
87 /**
88 * Get Attachments data.
89 *
90 */
91 $attachments = $wpdb->get_results( $wpdb->prepare( "
92 SELECT
93 ID
94 FROM {$wpdb->posts}
95 WHERE post_type = 'attachment'
96 LIMIT %d, %d;
97 ", $this->start, $limit ), ARRAY_A );
98
99 //print_r( $attachments ); die();
100
101 foreach( $attachments as $i => $a ) {
102
103 if( $this->_maybe_upgrade_meta( $a[ 'ID' ] ) ) {
104 $upgraded_meta++;
105 }
106
107 /** Flush data */
108 $wpdb->flush();
109 @ob_flush();
110 @flush();
111
112 }
113
114 unset( $attachments );
115
116 }
117
118 WP_CLI::success( "Stateless Media Meta is upgraded" );
119
120 WP_CLI::line( 'Media which have been checked: ' . number_format_i18n( $media_to_proceed ) );
121 WP_CLI::line( 'Upgraded stateless meta for ' . number_format_i18n( $upgraded_meta ) . ' attachments' );
122 WP_CLI::line( 'Spent Time: ' . ( time() - $timer ) . ' sec' );
123
124 }
125
126 /**
127 * Maybe Upgrade Stateless meta for specific attachment
128 */
129 private function _maybe_upgrade_meta( $id ) {
130 global $wpdb;
131
132 $bool = false;
133
134 /* Determine if attachment has legacy main meta data */
135 $name = get_post_meta( $id, 'sm_cloud:name', true );
136 $fileLink = get_post_meta( $id, 'sm_cloud:fileLink', true );
137
138 /* Let's upgrade our shit. */
139 if( !empty( $name ) && !empty( $fileLink ) ) {
140
141 /* Disable autocommit to Database to prevent broken balance transactions. */
142 $wpdb->query( 'SET autocommit = 0;' );
143 $wpdb->query( 'START TRANSACTION;' );
144
145 try {
146
147 $cloud_meta = array(
148 'name' => $name,
149 'fileLink' => $fileLink,
150 'id' => get_post_meta( $id, 'sm_cloud:id', true ),
151 'storageClass' => get_post_meta( $id, 'sm_cloud:storageClass', true ),
152 'mediaLink' => get_post_meta( $id, 'sm_cloud:mediaLink', true ),
153 'selfLink' => get_post_meta( $id, 'sm_cloud:selfLink', true ),
154 'bucket' => get_post_meta( $id, 'sm_cloud:bucket', true ),
155 'object' => get_post_meta( $id, 'sm_cloud:object', true ),
156 'sizes' => array(),
157 );
158
159 delete_post_meta( $id, 'sm_cloud:name' );
160 delete_post_meta( $id, 'sm_cloud:fileLink' );
161 delete_post_meta( $id, 'sm_cloud:id' );
162 delete_post_meta( $id, 'sm_cloud:storageClass' );
163 delete_post_meta( $id, 'sm_cloud:mediaLink' );
164 delete_post_meta( $id, 'sm_cloud:selfLink' );
165 delete_post_meta( $id, 'sm_cloud:bucket' );
166 delete_post_meta( $id, 'sm_cloud:object' );
167
168 $metadata = wp_get_attachment_metadata( $id );
169
170 if( !empty( $metadata[ 'sizes' ] ) && is_array( $metadata[ 'sizes' ] ) ) {
171
172 foreach( $metadata[ 'sizes' ] as $image_size => $data ) {
173
174 $name = get_post_meta( $id, 'sm_cloud:' . $image_size . ':name', true );
175 $fileLink = get_post_meta( $id, 'sm_cloud:' . $image_size . ':fileLink', true );
176
177 if( !empty( $name ) && !empty( $fileLink ) ) {
178 $cloud_meta[ 'sizes' ][ $image_size ] = array(
179 'id' => get_post_meta( $id, 'sm_cloud:' . $image_size . ':id', true ),
180 'name' => $name,
181 'fileLink' => $fileLink,
182 'mediaLink' => get_post_meta( $id, 'sm_cloud:' . $image_size . ':mediaLink', true ),
183 'selfLink' => get_post_meta( $id, 'sm_cloud:' . $image_size . ':selfLink', true ),
184 );
185 }
186
187 delete_post_meta( $id, 'sm_cloud:' . $image_size . ':name' );
188 delete_post_meta( $id, 'sm_cloud:' . $image_size . ':fileLink' );
189 delete_post_meta( $id, 'sm_cloud:' . $image_size . ':id' );
190 delete_post_meta( $id, 'sm_cloud:' . $image_size . ':mediaLink' );
191 delete_post_meta( $id, 'sm_cloud:' . $image_size . ':selfLink' );
192
193 }
194
195 }
196
197 if( !empty( $cloud_meta ) ) {
198 update_post_meta( $id, 'sm_cloud', $cloud_meta );
199 }
200
201 $bool = true;
202
203 } catch ( \Exception $e ) {
204
205 /* Rollback all transactions to prevent broken orders, order items, etc. */
206 $wpdb->query( 'ROLLBACK' );
207 $wpdb->query( 'SET autocommit = 1;' );
208
209 WP_CLI::warning( "SM Meta for attachment #{$id} was not upgraded due the error: " . $e->getMessage() );
210
211 return $bool;
212 }
213
214 /* Commit all transactions to Database and enable autocommit again. */
215 $wpdb->query( 'COMMIT' );
216 $wpdb->query( 'SET autocommit = 1;' );
217
218 }
219
220 return $bool;
221 }
222
223 /**
224 *
225 */
226 private function _prepare() {
227 $args = $this->assoc_args;
228 if( isset( $args[ 'b' ] ) ) {
229 WP_CLI::error( 'Invalid parameter --b. Command must not be run directly with --b parameter.' );
230 }
231 $this->start = isset( $args[ 'start' ] ) && is_numeric( $args[ 'start' ] ) ? $args[ 'start' ] : 0;
232 $this->limit = isset( $args[ 'limit' ] ) && is_numeric( $args[ 'limit' ] ) ? $args[ 'limit' ] : 100;
233 if( isset( $args[ 'batch' ] ) ) {
234 if( !is_numeric( $args[ 'batch' ] ) || $args[ 'batch' ] <= 0 ) {
235 WP_CLI::error( 'Invalid parameter --batch' );
236 }
237 $this->batch = $args[ 'batch' ];
238 $this->batches = isset( $args[ 'batches' ] ) ? $args[ 'batches' ] : 10;
239 if( !is_numeric( $args[ 'batches' ] ) || $args[ 'batches' ] <= 0 ) {
240 WP_CLI::error( 'Invalid parameter --batches' );
241 } elseif ( $this->batch > $this->batches ) {
242 WP_CLI::error( '--batch parameter must is invalid. It must not equal or less then --batches' );
243 }
244 } else {
245 $this->end = isset( $args[ 'end' ] ) && is_numeric( $args[ 'end' ] ) ? $args[ 'end' ] : false;
246 }
247 }
248
249 }