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 +148 -51 1.5.42.1 View file →
@@ -6,28 +6,83 @@
6 6 */
7 7
8 8 declare( strict_types=1 );
9 9
10 -
11 10 namespace Packetery\Module;
12 11
13 -use PacketeryTracy\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 $posts
23 - * @property string $options
24 - * @property string $postmeta
25 17 * @package Packetery
26 18 */
27 19 class WpdbAdapter {
28 20
29 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 + /**
30 85 * Wpdb.
31 86 *
32 87 * @var \wpdb
33 88 */
@@ -42,21 +97,19 @@
42 97 $this->wpdb = $wpdb;
43 98 }
44 99
45 100 /**
46 - * Gets row.
47 - *
48 101 * @param string $query SQL query.
49 102 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
50 103 * correspond to an stdClass object, an associative array, or a numeric array,
51 104 * respectively. Default OBJECT.
52 105 *
53 - * @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.
54 107 */
55 108 public function get_row( string $query, string $output = OBJECT ) {
56 109 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
57 110 $result = $this->wpdb->get_row( $query, $output );
58 - if ( null === $result ) {
111 + if ( $result === null ) {
59 112 $this->handleError();
60 113 }
61 114
62 115 return $result;
@@ -72,9 +125,9 @@
72 125 */
73 126 public function prepare( string $query, ...$args ): string {
74 127 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
75 128 $result = $this->wpdb->prepare( $query, ...$args );
76 - if ( null === $result ) {
129 + if ( $result === null ) {
77 130 $this->logError( 'Query to prepare is invalid. Likely due placeholder count mismatch.' );
78 131 }
79 132
80 133 return (string) $result;
@@ -90,9 +143,9 @@
90 143 */
91 144 public function query( string $query ) {
92 145 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
93 146 $result = $this->wpdb->query( $query );
94 - if ( false === $result ) {
147 + if ( $result === false ) {
95 148 $this->handleError();
96 149 }
97 150
98 151 return $result;
@@ -100,18 +153,18 @@
100 153
101 154 /**
102 155 * Helper function for insert and replace.
103 156 *
104 - * @param string $table Table name.
105 - * @param array $data Data to insert (in column => value pairs).
106 - * @param array|null $format Optional. An array of formats to be mapped to each of the value in $data.
107 - * @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'.
108 161 *
109 162 * @return int|false The number of rows affected, or false on error.
110 163 */
111 164 public function insertReplaceHelper( string $table, array $data, ?array $format = null, string $type = 'INSERT' ) {
112 165 $result = $this->wpdb->_insert_replace_helper( $table, $data, $format, $type );
113 - if ( false === $result ) {
166 + if ( $result === false ) {
114 167 $this->handleError();
115 168 }
116 169
117 170 return $result;
@@ -119,17 +172,17 @@
119 172
120 173 /**
121 174 * Deletes a row in the table.
122 175 *
123 - * @param string $table Table name.
124 - * @param array $where A named array of WHERE clauses (in column => value pairs).
125 - * @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.
126 179 *
127 180 * @return int|false The number of rows updated, or false on error.
128 181 */
129 182 public function delete( string $table, array $where, ?string $whereFormat = null ) {
130 183 $result = $this->wpdb->delete( $table, $where, $whereFormat );
131 - if ( false === $result ) {
184 + if ( $result === false ) {
132 185 $this->handleError();
133 186 }
134 187
135 188 return $result;
@@ -137,16 +190,16 @@
137 190
138 191 /**
139 192 * Inserts a row into the table.
140 193 *
141 - * @param string $table Table name.
142 - * @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).
143 196 *
144 197 * @return int|false The number of rows inserted, or false on error.
145 198 */
146 199 public function insert( string $table, array $data ) {
147 200 $result = $this->wpdb->insert( $table, $data );
148 - if ( false === $result ) {
201 + if ( $result === false ) {
149 202 $this->handleError();
150 203 }
151 204
152 205 return $result;
@@ -154,17 +207,17 @@
154 207
155 208 /**
156 209 * Updates a row in the table.
157 210 *
158 - * @param string $table Table name.
159 - * @param array $data Data to update (in column => value pairs).
160 - * @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).
161 214 *
162 215 * @return int|false The number of rows updated, or false on error.
163 216 */
164 217 public function update( string $table, array $data, array $where ) {
165 218 $result = $this->wpdb->update( $table, $data, $where );
166 - if ( false === $result ) {
219 + if ( $result === false ) {
167 220 $this->handleError();
168 221 }
169 222
170 223 return $result;
@@ -204,9 +257,9 @@
204 257 */
205 258 public function get_var( string $query ): ?string {
206 259 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
207 260 $result = $this->wpdb->get_var( $query );
208 - if ( null === $result ) {
261 + if ( $result === null ) {
209 262 $this->handleError();
210 263 }
211 264
212 265 return $result;
@@ -219,9 +272,9 @@
219 272 *
220 273 * @return bool
221 274 */
222 275 private function isPacketeryTableQueried( string $query ): bool {
223 - 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;
224 277 }
225 278
226 279 /**
227 280 * Gets packetery prefix.
@@ -239,9 +292,16 @@
239 292 *
240 293 * @return void
241 294 */
242 295 private function logError( string $errorMessage ): void {
243 - 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' ] );
244 304 }
245 305
246 306 /**
247 307 * Handles wpdb error.
@@ -248,9 +308,9 @@
248 308 *
249 309 * @return void
250 310 */
251 311 private function handleError(): void {
252 - if ( '' !== $this->getLastWpdbError() && $this->isPacketeryTableQueried( (string) $this->wpdb->last_query ) ) {
312 + if ( $this->getLastWpdbError() !== '' && $this->isPacketeryTableQueried( (string) $this->wpdb->last_query ) ) {
253 313 $this->logError( $this->getLastWpdbError() );
254 314 }
255 315 }
256 316
@@ -268,9 +328,9 @@
268 328 *
269 329 * @return \Generator
270 330 */
271 331 public function getWpdbQueries(): \Generator {
272 - if ( ! empty( $this->wpdb->queries ) ) {
332 + if ( $this->wpdb->queries !== null ) {
273 333 foreach ( $this->wpdb->queries as $queryInfo ) {
274 334 yield $queryInfo;
275 335 }
276 336 }
@@ -280,16 +340,16 @@
280 340 * This method outputs a one dimensional array. If more than one column is returned by the query,
281 341 * only the specified column will be returned, but the entire result is cached for later use.
282 342 *
283 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.
284 - * @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.
285 345 *
286 - * @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.
287 347 */
288 - public function get_col( string $query, int $column_offset = 0 ): array {
348 + public function get_col( string $query, int $columnOffset = 0 ): array {
289 349 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
290 - $result = $this->wpdb->get_col( $query, $column_offset );
291 - if ( [] === $result ) {
350 + $result = $this->wpdb->get_col( $query, $columnOffset );
351 + if ( $result === [] ) {
292 352 $this->handleError();
293 353 }
294 354
295 355 return $result;
@@ -297,11 +357,11 @@
297 357
298 358 /**
299 359 * Quote array of strings.
300 360 *
301 - * @param array $input Input.
361 + * @param string[] $input Input.
302 362 *
303 - * @return array
363 + * @return string[]
304 364 */
305 365 private function quoteArrayOfStrings( array $input ): array {
306 366 return array_map(
307 367 function ( string $item ) {
@@ -313,9 +373,9 @@
313 373
314 374 /**
315 375 * Prepare IN clause from array of strings.
316 376 *
317 - * @param array $input Input array.
377 + * @param string[] $input Input array.
318 378 *
319 379 * @return string
320 380 */
321 381 public function prepareInClause( array $input ): string {
@@ -344,12 +404,11 @@
344 404 foreach ( $result1 as $tableOrColumn => $message ) {
345 405 $wcLogger->info( sprintf( 'dbDelta: %s => %s', $tableOrColumn, $message ), [ 'source' => 'packeta' ] );
346 406 }
347 407
348 - // If the first command tries to create the table and so does the second, it means it failed.
349 - // Otherwise, we assume everything is fine.
350 408 $parsedResult1 = $this->parseDbdeltaOutput( $result1 );
351 409 $parsedResult2 = $this->parseDbdeltaOutput( $result2 );
410 + // If the first command tries to create the table and so does the second, it means it failed.
352 411 if (
353 412 in_array( $tableName, $parsedResult1['created_tables'], true ) &&
354 413 in_array( $tableName, $parsedResult2['created_tables'], true )
355 414 ) {
@@ -354,9 +413,14 @@
354 413 in_array( $tableName, $parsedResult2['created_tables'], true )
355 414 ) {
356 415 return false;
357 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 + }
358 421
422 + // Otherwise, we assume everything is fine, column changes errors are not safe to catch this way.
359 423 return true;
360 424 }
361 425
362 426 /**
@@ -361,21 +425,54 @@
361 425
362 426 /**
363 427 * Parses the output given by dbDelta and returns information about it. Taken from DatabaseUtil 7.5.1.
364 428 *
365 - * @param array $dbdeltaOutput The output from the execution of dbDelta.
429 + * @param array<int|string, string> $dbdeltaOutput The output from the execution of dbDelta.
366 430 *
367 - * @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)>}
368 433 */
369 434 private function parseDbdeltaOutput( array $dbdeltaOutput ): array {
370 435 $createdTables = [];
436 + $addedColumns = [];
371 437
372 - foreach ( $dbdeltaOutput as $tableName => $result ) {
373 - if ( "Created table $tableName" === $result ) {
374 - $createdTables[] = $tableName;
438 + foreach ( $dbdeltaOutput as $tableOrColumn => $result ) {
439 + if ( "Created table $tableOrColumn" === $result ) {
440 + $createdTables[] = $tableOrColumn;
441 +
442 + continue;
375 443 }
444 + if ( "Added column $tableOrColumn" === $result ) {
445 + $addedColumns[] = $tableOrColumn;
446 + }
376 447 }
377 448
378 - return [ 'created_tables' => $createdTables ];
449 + return [
450 + 'created_tables' => $createdTables,
451 + 'added_columns' => $addedColumns,
452 + ];
379 453 }
380 454
455 + /**
456 + * Gets last insert ID.
457 + *
458 + * @return string|null
459 + */
460 + public function getLastInsertId(): ?string {
461 + if ( $this->wpdb->insert_id === 0 ) {
462 + return null;
463 + }
464 +
465 + return (string) $this->wpdb->insert_id;
466 + }
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 + }
381 478 }