DB.php
157 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Database; |
| 4 | |
| 5 | use Give\Framework\Database\Exceptions\DatabaseQueryException; |
| 6 | use WP_Error; |
| 7 | |
| 8 | /** |
| 9 | * Class DB |
| 10 | * |
| 11 | * A static decorator for the $wpdb class and decorator function which does SQL error checking when performing queries. |
| 12 | * If a SQL error occurs a DatabaseQueryException is thrown. |
| 13 | * |
| 14 | * @method static int|bool query( string $query ) |
| 15 | * @method static int|false insert( string $table, array $data, array|string $format ) |
| 16 | * @method static int|false delete( string $table, array $where, array|string $where_format ) |
| 17 | * @method static int|false update( string $table, array $where, array|string $where_format ) |
| 18 | * @method static int|false replace( string $table, array $data, array|string $format ) |
| 19 | * @method static null|string get_var( string $query = null, int $x = 0, int $y = 0 ) |
| 20 | * @method static array|object|null|void get_row( string $query = null, string $output = OBJECT, int $y = 0 ) |
| 21 | * @method static array get_col( string $query = null, int $x = 0 ) |
| 22 | * @method static array|object|null get_results( string $query = null, string $output = OBJECT ) |
| 23 | * @method static string get_charset_collate() |
| 24 | */ |
| 25 | class DB { |
| 26 | /** |
| 27 | * Runs the dbDelta function and returns a WP_Error with any errors that occurred during the process |
| 28 | * |
| 29 | * @see dbDelta() for parameter and return details |
| 30 | * |
| 31 | * @since 2.9.2 |
| 32 | * |
| 33 | * @param $delta |
| 34 | * |
| 35 | * @return array |
| 36 | * @throws DatabaseQueryException |
| 37 | */ |
| 38 | public static function delta( $delta ) { |
| 39 | return self::runQueryWithErrorChecking( |
| 40 | function () use ( $delta ) { |
| 41 | return dbDelta( $delta ); |
| 42 | } |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * A convenience method for the $wpdb->prepare method |
| 48 | * |
| 49 | * @see WPDB::prepare() for usage details |
| 50 | * |
| 51 | * @since 2.9.6 |
| 52 | * |
| 53 | * @param string $query |
| 54 | * @param mixed ...$args |
| 55 | * |
| 56 | * @return false|mixed |
| 57 | */ |
| 58 | public static function prepare( $query, ...$args ) { |
| 59 | global $wpdb; |
| 60 | |
| 61 | return $wpdb->prepare( $query, ...$args ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Magic method which calls the static method on the $wpdb while performing error checking |
| 66 | * |
| 67 | * @since 2.9.6 |
| 68 | * |
| 69 | * @param $name |
| 70 | * @param $arguments |
| 71 | * |
| 72 | * @return mixed |
| 73 | * @throws DatabaseQueryException |
| 74 | */ |
| 75 | public static function __callStatic( $name, $arguments ) { |
| 76 | return self::runQueryWithErrorChecking( |
| 77 | function () use ( $name, $arguments ) { |
| 78 | global $wpdb; |
| 79 | |
| 80 | return call_user_func_array( [ $wpdb, $name ], $arguments ); |
| 81 | } |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get last insert ID |
| 87 | * |
| 88 | * @since 2.10.0 |
| 89 | * @return int |
| 90 | */ |
| 91 | public static function last_insert_id() { |
| 92 | global $wpdb; |
| 93 | return $wpdb->insert_id; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Runs a query callable and checks to see if any unique SQL errors occurred when it was run |
| 98 | * |
| 99 | * @since 2.9.2 |
| 100 | * |
| 101 | * @param Callable $queryCaller |
| 102 | * |
| 103 | * @return mixed |
| 104 | * @throws DatabaseQueryException |
| 105 | */ |
| 106 | private static function runQueryWithErrorChecking( $queryCaller ) { |
| 107 | global $wpdb, $EZSQL_ERROR; |
| 108 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 109 | |
| 110 | $errorCount = is_array( $EZSQL_ERROR ) ? count( $EZSQL_ERROR ) : 0; |
| 111 | $hasShowErrors = $wpdb->hide_errors(); |
| 112 | |
| 113 | $output = $queryCaller(); |
| 114 | |
| 115 | if ( $hasShowErrors ) { |
| 116 | $wpdb->show_errors(); |
| 117 | } |
| 118 | |
| 119 | $wpError = self::getQueryErrors( $errorCount ); |
| 120 | |
| 121 | if ( ! empty( $wpError->errors ) ) { |
| 122 | throw DatabaseQueryException::create( $wpError->get_error_messages() ); |
| 123 | } |
| 124 | |
| 125 | return $output; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Retrieves the SQL errors stored by WordPress |
| 130 | * |
| 131 | * @since 2.9.2 |
| 132 | * |
| 133 | * @param int $initialCount |
| 134 | * |
| 135 | * @return WP_Error |
| 136 | */ |
| 137 | private static function getQueryErrors( $initialCount = 0 ) { |
| 138 | global $EZSQL_ERROR; |
| 139 | |
| 140 | $wpError = new WP_Error(); |
| 141 | |
| 142 | if ( is_array( $EZSQL_ERROR ) ) { |
| 143 | for ( $index = $initialCount, $indexMax = count( $EZSQL_ERROR ); $index < $indexMax; $index ++ ) { |
| 144 | $error = $EZSQL_ERROR[ $index ]; |
| 145 | |
| 146 | if ( empty( $error['error_str'] ) || empty( $error['query'] ) || 0 === strpos( $error['query'], 'DESCRIBE ' ) ) { |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | $wpError->add( 'db_delta_error', $error['error_str'] ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return $wpError; |
| 155 | } |
| 156 | } |
| 157 |