DB.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Database; |
| 4 | |
| 5 | use Give\Framework\Database\Exceptions\DatabaseQueryException; |
| 6 | use WP_Error; |
| 7 | |
| 8 | class DB { |
| 9 | /** |
| 10 | * Runs the dbDelta function and returns a WP_Error with any errors that occurred during the process |
| 11 | * |
| 12 | * @see dbDelta() for parameter and return details |
| 13 | * |
| 14 | * @since 2.9.2 |
| 15 | * |
| 16 | * @param $delta |
| 17 | * |
| 18 | * @return array |
| 19 | * @throws DatabaseQueryException |
| 20 | */ |
| 21 | public static function delta( $delta ) { |
| 22 | return self::runQueryWithErrorChecking( |
| 23 | function () use ( $delta ) { |
| 24 | return dbDelta( $delta ); |
| 25 | } |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Runs the $wpdb::delete method with SQL error checking |
| 31 | * |
| 32 | * @see wpdb::delete() for parameter and return details |
| 33 | * |
| 34 | * @since 2.9.2 |
| 35 | * |
| 36 | * @param string $tableName |
| 37 | * @param array $data |
| 38 | * @param array $formats |
| 39 | * |
| 40 | * @return int|false |
| 41 | * @throws DatabaseQueryException |
| 42 | */ |
| 43 | public static function delete( $tableName, $data, $formats ) { |
| 44 | return self::runQueryWithErrorChecking( |
| 45 | function () use ( $tableName, $data, $formats ) { |
| 46 | global $wpdb; |
| 47 | |
| 48 | return $wpdb->delete( $tableName, $data, $formats = null ); |
| 49 | } |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Runs a query callable and checks to see if any unique SQL errors occurred when it was run |
| 55 | * |
| 56 | * @since 2.9.2 |
| 57 | * |
| 58 | * @param Callable $queryCaller |
| 59 | * |
| 60 | * @return mixed |
| 61 | * @throws DatabaseQueryException |
| 62 | */ |
| 63 | private static function runQueryWithErrorChecking( $queryCaller ) { |
| 64 | global $wpdb, $EZSQL_ERROR; |
| 65 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 66 | |
| 67 | $errorCount = is_array( $EZSQL_ERROR ) ? count( $EZSQL_ERROR ) : 0; |
| 68 | $hasShowErrors = $wpdb->hide_errors(); |
| 69 | |
| 70 | $output = $queryCaller(); |
| 71 | |
| 72 | if ( $hasShowErrors ) { |
| 73 | $wpdb->show_errors(); |
| 74 | } |
| 75 | |
| 76 | $errors = self::getQueryErrors( $errorCount ); |
| 77 | |
| 78 | if ( $errors->has_errors() ) { |
| 79 | throw DatabaseQueryException::create( $errors->get_error_messages() ); |
| 80 | } |
| 81 | |
| 82 | return $output; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Retrieves the SQL errors stored by WordPress |
| 87 | * |
| 88 | * @since 2.9.2 |
| 89 | * |
| 90 | * @param int $initialCount |
| 91 | * |
| 92 | * @return WP_Error |
| 93 | */ |
| 94 | private static function getQueryErrors( $initialCount = 0 ) { |
| 95 | global $EZSQL_ERROR; |
| 96 | |
| 97 | $wpError = new WP_Error(); |
| 98 | |
| 99 | if ( is_array( $EZSQL_ERROR ) ) { |
| 100 | for ( $index = $initialCount, $indexMax = count( $EZSQL_ERROR ); $index < $indexMax; $index ++ ) { |
| 101 | $error = $EZSQL_ERROR[ $index ]; |
| 102 | |
| 103 | if ( empty( $error['error_str'] ) || empty( $error['query'] ) || 0 === strpos( $error['query'], 'DESCRIBE ' ) ) { |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | $wpError->add( 'db_delta_error', $error['error_str'] ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return $wpError; |
| 112 | } |
| 113 | } |
| 114 |