PluginProbe
Packeta / 2.1
Packeta v2.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
← All changes | src/Packetery/Module/WpdbAdapter.php +136 -55 1.6.12.1 View file →
@@ -6,31 +6,83 @@
6 6 */
7 7
8 8 declare( strict_types=1 );
9 9
10 -
11 10 namespace Packetery\Module;
12 11
13 -use Packetery\Tracy\Debugger;
14 12 use WC_Logger;
15 13
16 14 /**
17 15 * Class WpdbAdapter
18 16 *
19 - * @property string $packetery_carrier
20 - * @property string $packetery_order
21 - * @property string $packetery_log
22 - * @property string $packetery_customs_declaration
23 - * @property string $packetery_customs_declaration_item
24 - * @property string $posts
25 - * @property string $wc_orders
26 - * @property string $options
27 - * @property string $postmeta
28 17 * @package Packetery
29 18 */
30 19 class WpdbAdapter {
31 20
32 21 /**
22 + * Table name.
23 + *
24 + * @var string
25 + */
26 + public $packeteryCarrier;
27 +
28 + /**
29 + * Table name.
30 + *
31 + * @var string
32 + */
33 + public $packeteryOrder;
34 +
35 + /**
36 + * Table name.
37 + *
38 + * @var string
39 + */
40 + public $packeteryLog;
41 +
42 + /**
43 + * Table name.
44 + *
45 + * @var string
46 + */
47 + public $packeteryCustomsDeclaration;
48 +
49 + /**
50 + * Table name.
51 + *
52 + * @var string
53 + */
54 + public $packeteryCustomsDeclarationItem;
55 +
56 + /**
57 + * Table name.
58 + *
59 + * @var string
60 + */
61 + public $wcOrders;
62 +
63 + /**
64 + * Table name.
65 + *
66 + * @var string
67 + */
68 + public $posts;
69 +
70 + /**
71 + * Table name.
72 + *
73 + * @var string
74 + */
75 + public $options;
76 +
77 + /**
78 + * Table name.
79 + *
80 + * @var string
81 + */
82 + public $postmeta;
83 +
84 + /**
33 85 * Wpdb.
34 86 *
35 87 * @var \wpdb
36 88 */
@@ -45,21 +97,19 @@
45 97 $this->wpdb = $wpdb;
46 98 }
47 99
48 100 /**
49 - * Gets row.
50 - *
51 101 * @param string $query SQL query.
52 102 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
53 103 * correspond to an stdClass object, an associative array, or a numeric array,
54 104 * respectively. Default OBJECT.
55 105 *
56 - * @return array|object|null Database query result or null on failure.
106 + * @return array<string, mixed>|object|null Database query result or null on failure.
57 107 */
58 108 public function get_row( string $query, string $output = OBJECT ) {
59 109 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
60 110 $result = $this->wpdb->get_row( $query, $output );
61 - if ( null === $result ) {
111 + if ( $result === null ) {
62 112 $this->handleError();
63 113 }
64 114
65 115 return $result;
@@ -75,9 +125,9 @@
75 125 */
76 126 public function prepare( string $query, ...$args ): string {
77 127 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
78 128 $result = $this->wpdb->prepare( $query, ...$args );
79 - if ( null === $result ) {
129 + if ( $result === null ) {
80 130 $this->logError( 'Query to prepare is invalid. Likely due placeholder count mismatch.' );
81 131 }
82 132
83 133 return (string) $result;
@@ -93,9 +143,9 @@
93 143 */
94 144 public function query( string $query ) {
95 145 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
96 146 $result = $this->wpdb->query( $query );
97 - if ( false === $result ) {
147 + if ( $result === false ) {
98 148 $this->handleError();
99 149 }
100 150
101 151 return $result;
@@ -103,18 +153,18 @@
103 153
104 154 /**
105 155 * Helper function for insert and replace.
106 156 *
107 - * @param string $table Table name.
108 - * @param array $data Data to insert (in column => value pairs).
109 - * @param array|null $format Optional. An array of formats to be mapped to each of the value in $data.
110 - * @param string $type Optional. Type of operation. Possible values include 'INSERT' or 'REPLACE'.
157 + * @param string $table Table name.
158 + * @param array<string, mixed> $data Data to insert (in column => value pairs).
159 + * @param string[]|null $format Optional. An array of formats to be mapped to each of the value in $data.
160 + * @param string $type Optional. Type of operation. Possible values include 'INSERT' or 'REPLACE'.
111 161 *
112 162 * @return int|false The number of rows affected, or false on error.
113 163 */
114 164 public function insertReplaceHelper( string $table, array $data, ?array $format = null, string $type = 'INSERT' ) {
115 165 $result = $this->wpdb->_insert_replace_helper( $table, $data, $format, $type );
116 - if ( false === $result ) {
166 + if ( $result === false ) {
117 167 $this->handleError();
118 168 }
119 169
120 170 return $result;
@@ -122,17 +172,17 @@
122 172
123 173 /**
124 174 * Deletes a row in the table.
125 175 *
126 - * @param string $table Table name.
127 - * @param array $where A named array of WHERE clauses (in column => value pairs).
128 - * @param string|null $whereFormat Optional. An array of formats to be mapped to each of the values in $where.
176 + * @param string $table Table name.
177 + * @param array<string, int|string> $where A named array of WHERE clauses (in column => value pairs).
178 + * @param string|null $whereFormat Optional. An array of formats to be mapped to each of the values in $where.
129 179 *
130 180 * @return int|false The number of rows updated, or false on error.
131 181 */
132 182 public function delete( string $table, array $where, ?string $whereFormat = null ) {
133 183 $result = $this->wpdb->delete( $table, $where, $whereFormat );
134 - if ( false === $result ) {
184 + if ( $result === false ) {
135 185 $this->handleError();
136 186 }
137 187
138 188 return $result;
@@ -140,16 +190,16 @@
140 190
141 191 /**
142 192 * Inserts a row into the table.
143 193 *
144 - * @param string $table Table name.
145 - * @param array $data Data to insert (in column => value pairs).
194 + * @param string $table Table name.
195 + * @param array<string, mixed> $data Data to insert (in column => value pairs).
146 196 *
147 197 * @return int|false The number of rows inserted, or false on error.
148 198 */
149 199 public function insert( string $table, array $data ) {
150 200 $result = $this->wpdb->insert( $table, $data );
151 - if ( false === $result ) {
201 + if ( $result === false ) {
152 202 $this->handleError();
153 203 }
154 204
155 205 return $result;
@@ -157,17 +207,17 @@
157 207
158 208 /**
159 209 * Updates a row in the table.
160 210 *
161 - * @param string $table Table name.
162 - * @param array $data Data to update (in column => value pairs).
163 - * @param array $where A named array of WHERE clauses (in column => value pairs).
211 + * @param string $table Table name.
212 + * @param array<string, int|float|string|null|bool> $data Data to update (in column => value pairs).
213 + * @param array<string, int|string> $where A named array of WHERE clauses (in column => value pairs).
164 214 *
165 215 * @return int|false The number of rows updated, or false on error.
166 216 */
167 217 public function update( string $table, array $data, array $where ) {
168 218 $result = $this->wpdb->update( $table, $data, $where );
169 - if ( false === $result ) {
219 + if ( $result === false ) {
170 220 $this->handleError();
171 221 }
172 222
173 223 return $result;
@@ -207,9 +257,9 @@
207 257 */
208 258 public function get_var( string $query ): ?string {
209 259 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
210 260 $result = $this->wpdb->get_var( $query );
211 - if ( null === $result ) {
261 + if ( $result === null ) {
212 262 $this->handleError();
213 263 }
214 264
215 265 return $result;
@@ -222,9 +272,9 @@
222 272 *
223 273 * @return bool
224 274 */
225 275 private function isPacketeryTableQueried( string $query ): bool {
226 - return 1 === preg_match( '~\s*(FROM|JOIN|INTO|UPDATE|TABLE)\s*`?' . preg_quote( $this->getPacketeryPrefix(), '~' ) . '~i', $query );
276 + return preg_match( '~\s*(FROM|JOIN|INTO|UPDATE|TABLE)\s*`?' . preg_quote( $this->getPacketeryPrefix(), '~' ) . '~i', $query ) === 1;
227 277 }
228 278
229 279 /**
230 280 * Gets packetery prefix.
@@ -242,9 +292,16 @@
242 292 *
243 293 * @return void
244 294 */
245 295 private function logError( string $errorMessage ): void {
246 - Debugger::log( $errorMessage, sprintf( 'wpdb-errors_%s', gmdate( 'Y-m-d' ) ) );
296 + /**
297 + * WC logger.
298 + *
299 + * @var WC_Logger $wcLogger
300 + */
301 + $wcLogger = wc_get_logger();
302 +
303 + $wcLogger->error( sprintf( 'wpdb: %s', $errorMessage ), [ 'source' => 'packeta' ] );
247 304 }
248 305
249 306 /**
250 307 * Handles wpdb error.
@@ -251,9 +308,9 @@
251 308 *
252 309 * @return void
253 310 */
254 311 private function handleError(): void {
255 - if ( '' !== $this->getLastWpdbError() && $this->isPacketeryTableQueried( (string) $this->wpdb->last_query ) ) {
312 + if ( $this->getLastWpdbError() !== '' && $this->isPacketeryTableQueried( (string) $this->wpdb->last_query ) ) {
256 313 $this->logError( $this->getLastWpdbError() );
257 314 }
258 315 }
259 316
@@ -271,9 +328,9 @@
271 328 *
272 329 * @return \Generator
273 330 */
274 331 public function getWpdbQueries(): \Generator {
275 - if ( ! empty( $this->wpdb->queries ) ) {
332 + if ( $this->wpdb->queries !== null ) {
276 333 foreach ( $this->wpdb->queries as $queryInfo ) {
277 334 yield $queryInfo;
278 335 }
279 336 }
@@ -283,16 +340,16 @@
283 340 * This method outputs a one dimensional array. If more than one column is returned by the query,
284 341 * only the specified column will be returned, but the entire result is cached for later use.
285 342 *
286 343 * @param string $query The query you wish to execute. Setting this parameter to null will return the specified column from the cached results of the previous query.
287 - * @param int $column_offset The desired column (0 being the first). Defaults to 0.
344 + * @param int $columnOffset The desired column (0 being the first). Defaults to 0.
288 345 *
289 - * @return array Returns an empty array if no result is found.
346 + * @return array<int|float|string|null|bool> Returns an empty array if no result is found.
290 347 */
291 - public function get_col( string $query, int $column_offset = 0 ): array {
348 + public function get_col( string $query, int $columnOffset = 0 ): array {
292 349 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
293 - $result = $this->wpdb->get_col( $query, $column_offset );
294 - if ( [] === $result ) {
350 + $result = $this->wpdb->get_col( $query, $columnOffset );
351 + if ( $result === [] ) {
295 352 $this->handleError();
296 353 }
297 354
298 355 return $result;
@@ -300,11 +357,11 @@
300 357
301 358 /**
302 359 * Quote array of strings.
303 360 *
304 - * @param array $input Input.
361 + * @param string[] $input Input.
305 362 *
306 - * @return array
363 + * @return string[]
307 364 */
308 365 private function quoteArrayOfStrings( array $input ): array {
309 366 return array_map(
310 367 function ( string $item ) {
@@ -316,9 +373,9 @@
316 373
317 374 /**
318 375 * Prepare IN clause from array of strings.
319 376 *
320 - * @param array $input Input array.
377 + * @param string[] $input Input array.
321 378 *
322 379 * @return string
323 380 */
324 381 public function prepareInClause( array $input ): string {
@@ -347,12 +404,11 @@
347 404 foreach ( $result1 as $tableOrColumn => $message ) {
348 405 $wcLogger->info( sprintf( 'dbDelta: %s => %s', $tableOrColumn, $message ), [ 'source' => 'packeta' ] );
349 406 }
350 407
351 - // If the first command tries to create the table and so does the second, it means it failed.
352 - // Otherwise, we assume everything is fine.
353 408 $parsedResult1 = $this->parseDbdeltaOutput( $result1 );
354 409 $parsedResult2 = $this->parseDbdeltaOutput( $result2 );
410 + // If the first command tries to create the table and so does the second, it means it failed.
355 411 if (
356 412 in_array( $tableName, $parsedResult1['created_tables'], true ) &&
357 413 in_array( $tableName, $parsedResult2['created_tables'], true )
358 414 ) {
@@ -357,9 +413,14 @@
357 413 in_array( $tableName, $parsedResult2['created_tables'], true )
358 414 ) {
359 415 return false;
360 416 }
417 + // If the first command tries to add column and so does the second, it means it failed.
418 + if ( $parsedResult1['added_columns'] !== [] && $parsedResult2['added_columns'] !== [] ) {
419 + return false;
420 + }
361 421
422 + // Otherwise, we assume everything is fine, column changes errors are not safe to catch this way.
362 423 return true;
363 424 }
364 425
365 426 /**
@@ -364,22 +425,32 @@
364 425
365 426 /**
366 427 * Parses the output given by dbDelta and returns information about it. Taken from DatabaseUtil 7.5.1.
367 428 *
368 - * @param array $dbdeltaOutput The output from the execution of dbDelta.
429 + * @param array<int|string, string> $dbdeltaOutput The output from the execution of dbDelta.
369 430 *
370 - * @return array[] An array containing a 'created_tables' key whose value is an array with the names of the tables that have been (or would have been) created.
431 + * An array containing a 'created_tables' and 'added_columns' key whose value is an array with the names of the tables or columns that have been (or would have been) created.
432 + * @return array{created_tables: array<int<0, max>, (int|string)>, added_columns: array<int<0, max>, (int|string)>}
371 433 */
372 434 private function parseDbdeltaOutput( array $dbdeltaOutput ): array {
373 435 $createdTables = [];
436 + $addedColumns = [];
374 437
375 - foreach ( $dbdeltaOutput as $tableName => $result ) {
376 - if ( "Created table $tableName" === $result ) {
377 - $createdTables[] = $tableName;
438 + foreach ( $dbdeltaOutput as $tableOrColumn => $result ) {
439 + if ( "Created table $tableOrColumn" === $result ) {
440 + $createdTables[] = $tableOrColumn;
441 +
442 + continue;
378 443 }
444 + if ( "Added column $tableOrColumn" === $result ) {
445 + $addedColumns[] = $tableOrColumn;
446 + }
379 447 }
380 448
381 - return [ 'created_tables' => $createdTables ];
449 + return [
450 + 'created_tables' => $createdTables,
451 + 'added_columns' => $addedColumns,
452 + ];
382 453 }
383 454
384 455 /**
385 456 * Gets last insert ID.
@@ -386,9 +457,9 @@
386 457 *
387 458 * @return string|null
388 459 */
389 460 public function getLastInsertId(): ?string {
390 - if ( 0 === $this->wpdb->insert_id ) {
461 + if ( $this->wpdb->insert_id === 0 ) {
391 462 return null;
392 463 }
393 464
394 465 return (string) $this->wpdb->insert_id;
@@ -393,5 +464,15 @@
393 464
394 465 return (string) $this->wpdb->insert_id;
395 466 }
396 467
468 + /**
469 + * Wpdb esc_like method proxy.
470 + *
471 + * @param string $text Text to escape.
472 + *
473 + * @return string
474 + */
475 + public function escLike( string $text ): string {
476 + return $this->wpdb->esc_like( $text );
477 + }
397 478 }