Test Failed
Pull Request — master (#2551)
by Devin
04:51
created

Give_DB::update()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 34
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 5
nop 3
dl 0
loc 34
rs 8.5806
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 20
1
<?php
2
/**
3
 * Give DB
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_DB
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Give_DB Class
19
 *
20
 * This class is for interacting with the database table.
21
 *
22
 * @since 1.0
23
 */
24
abstract class Give_DB {
25
26
	/**
27
	 * The name of our database table
28
	 *
29
	 * @since  1.0
30
	 * @access public
31
	 *
32
	 * @var    string
33
	 */
34
	public $table_name;
35
36
	/**
37
	 * The version of our database table
38
	 *
39
	 * @since  1.0
40
	 * @access public
41
	 *
42
	 * @var    string
43
	 */
44
	public $version;
45
46
	/**
47
	 * The name of the primary column
48
	 *
49
	 * @since  1.0
50
	 * @access public
51
	 *
52
	 * @var    string
53
	 */
54
	public $primary_key;
55
56
	/**
57
	 * Class Constructor
58
	 *
59
	 * Set up the Give DB Class.
60
	 *
61
	 * @since  1.0
62
	 * @access public
63
	 */
64
	public function __construct() {
65
	}
66
67
	/**
68
	 * Whitelist of columns
69
	 *
70
	 * @since  1.0
71
	 * @access public
72
	 *
73
	 * @return array  Columns and formats.
74
	 */
75
	public function get_columns() {
76
		return array();
77
	}
78
79
	/**
80
	 * Default column values
81
	 *
82
	 * @since  1.0
83
	 * @access public
84
	 *
85
	 * @return array  Default column values.
86
	 */
87
	public function get_column_defaults() {
88
		return array();
89
	}
90
91
	/**
92
	 * Retrieve a row by the primary key
93
	 *
94
	 * @since  1.0
95
	 * @access public
96
	 *
97
	 * @param  int $row_id Row ID.
98
	 *
99
	 * @return object
100
	 */
101
	public function get( $row_id ) {
102
		/* @var WPDB $wpdb */
103
		global $wpdb;
104
105
		// Bailout.
106
		if ( empty( $row_id ) ) {
107
			return null;
108
		}
109 52
110 52
		return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
111 52
	}
112 52
113
	/**
114
	 * Retrieve a row by a specific column / value
115
	 *
116
	 * @since  1.0
117
	 * @access public
118
	 *
119
	 * @param  int $column Column ID.
120
	 * @param  int $row_id Row ID.
121
	 *
122 2
	 * @return object
123 2
	 */
124 2 View Code Duplication
	public function get_by( $column, $row_id ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125 2
		/* @var WPDB $wpdb */
126 2
		global $wpdb;
127
128
		// Bailout.
129
		if ( empty( $column ) || empty( $row_id ) ) {
130
			return null;
131
		}
132
133
		$column = esc_sql( $column );
134
135
		return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $column = %s LIMIT 1;", $row_id ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
136 52
	}
137 52
138
	/**
139
	 * Retrieve a specific column's value by the primary key
140 52
	 *
141
	 * @since  1.0
142 52
	 * @access public
143
	 *
144
	 * @param  int $column Column ID.
145 52
	 * @param  int $row_id Row ID.
146
	 *
147
	 * @return string      Column value.
148 52
	 */
149 View Code Duplication
	public function get_column( $column, $row_id ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
		/* @var WPDB $wpdb */
151 52
		global $wpdb;
152
153
		// Bailout.
154 52
		if ( empty( $column ) || empty( $row_id ) ) {
155 52
			return null;
156
		}
157 52
158
		$column = esc_sql( $column );
159 52
160
		return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
161 52
	}
162
163
	/**
164
	 * Retrieve a specific column's value by the the specified column / value
165
	 *
166
	 * @since  1.0
167
	 * @access public
168
     *
169
     * @param  int    $column       Column ID.
170
     * @param  string $column_where Column name.
171 52
     * @param  string $column_value Column value.
172
     *
173 52
	 * @return string
174
	 */
175
	public function get_column_by( $column, $column_where, $column_value ) {
176 52
        /* @var WPDB $wpdb */
177
        global $wpdb;
178 52
179 1
		// Bailout.
180
		if ( empty( $column ) || empty( $column_where ) || empty( $column_value ) ) {
181
			return null;
182 52
		}
183 52
184 52
		$column_where = esc_sql( $column_where );
185
		$column       = esc_sql( $column );
186
		return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = %s LIMIT 1;", $column_value ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
187 52
	}
188
189
	/**
190 52
	 * Insert a new row
191
	 *
192
	 * @since  1.0
193 52
	 * @access public
194
     *
195
     * @param  array  $data
196 52
     * @param  string $type
197 52
     *
198
	 * @return int
199 52
	 */
200
	public function insert( $data, $type = '' ) {
201
        /* @var WPDB $wpdb */
202
        global $wpdb;
203 52
204
		// Set default values.
205
		$data = wp_parse_args( $data, $this->get_column_defaults() );
206
207
		/**
208
		 * Fires before inserting data to the database.
209
		 *
210
		 * @since 1.0
211
		 *
212
		 * @param array $data
213
		 */
214
		do_action( "give_pre_insert_{$type}", $data );
215
216
		// Initialise column format array
217
		$column_formats = $this->get_columns();
218
219
		// Force fields to lower case
220
		$data = array_change_key_case( $data );
221
222
		// White list columns
223
		$data = array_intersect_key( $data, $column_formats );
224
225
		// Reorder $column_formats to match the order of columns given in $data
226
		$data_keys      = array_keys( $data );
227
		$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
228
229
		$wpdb->insert( $this->table_name, $data, $column_formats );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
230
231
		/**
232
		 * Fires after inserting data to the database.
233
		 *
234
		 * @since 1.0
235
		 *
236
		 * @param int   $insert_id
237
		 * @param array $data
238 2
		 */
239 2
		do_action( "give_post_insert_{$type}", $wpdb->insert_id, $data );
240 2
241
		return $wpdb->insert_id;
242 2
	}
243
244
	/**
245
	 * Update a row
246
	 *
247
	 * @since  1.0
248
	 * @access public
249
     *
250
     * @param  int    $row_id Column ID
251
     * @param  array  $data
252
     * @param  string $where  Column value
253
     *
254
	 * @return bool
255
	 */
256
	public function update( $row_id, $data = array(), $where = '' ) {
257
        /* @var WPDB $wpdb */
258
        global $wpdb;
259
260
		// Row ID must be positive integer
261
		$row_id = absint( $row_id );
262
263
		if ( empty( $row_id ) ) {
264
			return false;
265
		}
266
267
		if ( empty( $where ) ) {
268
			$where = $this->primary_key;
269
		}
270
271
		// Initialise column format array
272
		$column_formats = $this->get_columns();
273
274
		// Force fields to lower case
275
		$data = array_change_key_case( $data );
276
277
		// White list columns
278
		$data = array_intersect_key( $data, $column_formats );
279
280
		// Reorder $column_formats to match the order of columns given in $data
281
		$data_keys      = array_keys( $data );
282
		$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
283
284
		if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
285
			return false;
286
		}
287
288
		return true;
289
	}
290
291
	/**
292
	 * Delete a row identified by the primary key
293
	 *
294
	 * @since  1.0
295
	 * @access public
296
     *
297
     * @param  int $row_id Column ID.
298
     *
299
	 * @return bool
300
	 */
301
	public function delete( $row_id = 0 ) {
302
        /* @var WPDB $wpdb */
303
        global $wpdb;
304
305
		// Row ID must be positive integer
306
		$row_id = absint( $row_id );
307
308
		if ( empty( $row_id ) ) {
309
			return false;
310
		}
311
312
		if ( false === $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $this->primary_key = %d", $row_id ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
313
			return false;
314
		}
315
316
		return true;
317
	}
318
319
	/**
320
	 * Check if the given table exists
321
	 *
322
	 * @since  1.3.2
323
	 * @access public
324
     *
325
	 * @param  string $table The table name.
326
     *
327
	 * @return bool          If the table name exists.
328
	 */
329
	public function table_exists( $table ) {
330
        /* @var WPDB $wpdb */
331
		global $wpdb;
332
333
		$table = sanitize_text_field( $table );
334
335
		return $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $table ) ) === $table;
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
336
	}
337
338
	/**
339
	 * Checks whether column exists in a table or not.
340
	 *
341
	 * @param string $column_name Name of the Column in Database Table.
342
	 *
343
	 * @since 1.8.18
344
	 *
345
	 * @see https://gist.github.com/datafeedr/54e89e07f87232fb055121bb766743fe
346
	 *
347
	 * @return bool
348
	 */
349
	public function does_column_exist( $column_name ) {
350
351
		global $wpdb;
352
353
		$column = $wpdb->get_results( $wpdb->prepare(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
354
			"SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND COLUMN_NAME = %s ",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal SELECT * FROM INFORMATIO...s AND COLUMN_NAME = %s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
355
			DB_NAME, $this->table_name, $column_name
356
		) );
357
358
		if ( ! empty( $column ) || get_option( 'give_version' ) ) {
359
			return true;
360
		}
361
362
		return false;
363
	}
364
365
	/**
366
	 * Check if the table was ever installed
367
	 *
368
	 * @since  1.6
369
	 * @access public
370
	 *
371
	 * @return bool Returns if the customers table was installed and upgrade routine run.
372
	 */
373
	public function installed() {
374
		return $this->table_exists( $this->table_name );
375
	}
376
377
	/**
378
	 * Register tables
379
	 *
380
	 * @since 1.8.9
381
	 * @access public
382
	 */
383
	public function register_table() {
384
		$current_version = get_option( $this->table_name . '_db_version' );
385
		if ( ! $current_version || version_compare( $current_version, $this->version, '<' ) ) {
386
			$this->create_table();
387
		}
388
	}
389
390
	/**
391
	 * Create table
392
	 *
393
	 * @since  1.8.9
394
	 * @access public
395
	 */
396
	public function create_table(){}
397
}
398