PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
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 +20 -43 2.3.22.0.9 View file →
@@ -8,10 +8,10 @@
8 8 declare( strict_types=1 );
9 9
10 10 namespace Packetery\Module;
11 11
12 -use Packetery\Module\Exception\DeleteErrorException;
13 -use Packetery\Module\Framework\WcAdapter;
12 +use Packetery\Tracy\Debugger;
13 +use WC_Logger;
14 14
15 15 /**
16 16 * Class WpdbAdapter
17 17 *
@@ -89,20 +89,14 @@
89 89 */
90 90 private $wpdb;
91 91
92 92 /**
93 - * @var WcAdapter
94 - */
95 - private $wcAdapter;
96 -
97 - /**
98 93 * Constructor.
99 94 *
100 95 * @param \wpdb $wpdb Wpdb.
101 96 */
102 - public function __construct( \wpdb $wpdb, WcAdapter $wcAdapter ) {
103 - $this->wpdb = $wpdb;
104 - $this->wcAdapter = $wcAdapter;
97 + public function __construct( \wpdb $wpdb ) {
98 + $this->wpdb = $wpdb;
105 99 }
106 100
107 101 /**
108 102 * @param string $query SQL query.
@@ -183,17 +177,14 @@
183 177 * @param string $table Table name.
184 178 * @param array<string, int|string> $where A named array of WHERE clauses (in column => value pairs).
185 179 * @param string|null $whereFormat Optional. An array of formats to be mapped to each of the values in $where.
186 180 *
187 - * @return int The number of rows deleted, throws DeleteErrorException on error.
188 - * @throws DeleteErrorException
181 + * @return int|false The number of rows updated, or false on error.
189 182 */
190 - public function delete( string $table, array $where, ?string $whereFormat = null ): int {
183 + public function delete( string $table, array $where, ?string $whereFormat = null ) {
191 184 $result = $this->wpdb->delete( $table, $where, $whereFormat );
192 185 if ( $result === false ) {
193 186 $this->handleError();
194 -
195 - throw new DeleteErrorException( "Could not delete from table `{$table}`." );
196 187 }
197 188
198 189 return $result;
199 190 }
@@ -302,10 +293,9 @@
302 293 *
303 294 * @return void
304 295 */
305 296 private function logError( string $errorMessage ): void {
306 - $wcLogger = $this->wcAdapter->getLogger();
307 - $wcLogger->error( sprintf( 'wpdb: %s', $errorMessage ), [ 'source' => 'packeta' ] );
297 + Debugger::log( $errorMessage, sprintf( 'wpdb-errors_%s', gmdate( 'Y-m-d' ) ) );
308 298 }
309 299
310 300 /**
311 301 * Handles wpdb error.
@@ -398,16 +388,22 @@
398 388 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
399 389 $result1 = dbDelta( $createTableQuery );
400 390 $result2 = dbDelta( $createTableQuery );
401 391
402 - $wcLogger = $this->wcAdapter->getLogger();
392 + /**
393 + * WC logger.
394 + *
395 + * @var WC_Logger $wcLogger
396 + */
397 + $wcLogger = wc_get_logger();
403 398 foreach ( $result1 as $tableOrColumn => $message ) {
404 399 $wcLogger->info( sprintf( 'dbDelta: %s => %s', $tableOrColumn, $message ), [ 'source' => 'packeta' ] );
405 400 }
406 401
402 + // If the first command tries to create the table and so does the second, it means it failed.
403 + // Otherwise, we assume everything is fine.
407 404 $parsedResult1 = $this->parseDbdeltaOutput( $result1 );
408 405 $parsedResult2 = $this->parseDbdeltaOutput( $result2 );
409 - // If the first command tries to create the table and so does the second, it means it failed.
410 406 if (
411 407 in_array( $tableName, $parsedResult1['created_tables'], true ) &&
412 408 in_array( $tableName, $parsedResult2['created_tables'], true )
413 409 ) {
@@ -412,14 +408,9 @@
412 408 in_array( $tableName, $parsedResult2['created_tables'], true )
413 409 ) {
414 410 return false;
415 411 }
416 - // If the first command tries to add column and so does the second, it means it failed.
417 - if ( $parsedResult1['added_columns'] !== [] && $parsedResult2['added_columns'] !== [] ) {
418 - return false;
419 - }
420 412
421 - // Otherwise, we assume everything is fine, column changes errors are not safe to catch this way.
422 413 return true;
423 414 }
424 415
425 416 /**
@@ -426,30 +417,20 @@
426 417 * Parses the output given by dbDelta and returns information about it. Taken from DatabaseUtil 7.5.1.
427 418 *
428 419 * @param array<int|string, string> $dbdeltaOutput The output from the execution of dbDelta.
429 420 *
430 - * 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.
431 - * @return array{created_tables: array<int<0, max>, (int|string)>, added_columns: array<int<0, max>, (int|string)>}
421 + * @return array{created_tables: array<int<0, max>, (int|string)>} 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.
432 422 */
433 423 private function parseDbdeltaOutput( array $dbdeltaOutput ): array {
434 424 $createdTables = [];
435 - $addedColumns = [];
436 425
437 - foreach ( $dbdeltaOutput as $tableOrColumn => $result ) {
438 - if ( "Created table $tableOrColumn" === $result ) {
439 - $createdTables[] = $tableOrColumn;
440 -
441 - continue;
426 + foreach ( $dbdeltaOutput as $tableName => $result ) {
427 + if ( "Created table $tableName" === $result ) {
428 + $createdTables[] = $tableName;
442 429 }
443 - if ( "Added column $tableOrColumn" === $result ) {
444 - $addedColumns[] = $tableOrColumn;
445 - }
446 430 }
447 431
448 - return [
449 - 'created_tables' => $createdTables,
450 - 'added_columns' => $addedColumns,
451 - ];
432 + return [ 'created_tables' => $createdTables ];
452 433 }
453 434
454 435 /**
455 436 * Gets last insert ID.
@@ -472,10 +453,6 @@
472 453 * @return string
473 454 */
474 455 public function escLike( string $text ): string {
475 456 return $this->wpdb->esc_like( $text );
476 - }
477 -
478 - public function dbServerInfo(): string {
479 - return $this->wpdb->db_server_info();
480 457 }
481 458 }