Completed
Push — issues/1132 ( 7a9b00...b11cf8 )
by Ravinder
17:39
created

Give_DB::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
104
105
		return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
106
	}
107
108
	/**
109
	 * Retrieve a row by a specific column / value
110
	 *
111
	 * @since  1.0
112
	 * @access public
113
	 *
114
     * @param  int $column Column ID.
115
     * @param  int $row_id Row ID.
116
     *
117
     * @return object
118
	 */
119
	public function get_by( $column, $row_id ) {
120
        /* @var WPDB $wpdb */
121
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
122
123
		$column = esc_sql( $column );
124
		return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $column = %s LIMIT 1;", $row_id ) );
125
	}
126
127
	/**
128
	 * Retrieve a specific column's value by the primary key
129
	 *
130
	 * @since  1.0
131
	 * @access public
132
     *
133
     * @param  int $column Column ID.
134
     * @param  int $row_id Row ID.
135
     *
136
	 * @return string      Column value.
137
	 */
138
	public function get_column( $column, $row_id ) {
139
        /* @var WPDB $wpdb */
140
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
141
142
		$column = esc_sql( $column );
143
		return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
144
	}
145
146
	/**
147
	 * Retrieve a specific column's value by the the specified column / value
148
	 *
149
	 * @since  1.0
150
	 * @access public
151
     *
152
     * @param  int    $column       Column ID.
153
     * @param  string $column_where Column name.
154
     * @param  string $column_value Column value.
155
     *
156
	 * @return string
157
	 */
158
	public function get_column_by( $column, $column_where, $column_value ) {
159
        /* @var WPDB $wpdb */
160
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
161
162
		$column_where = esc_sql( $column_where );
163
		$column       = esc_sql( $column );
164
		return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = %s LIMIT 1;", $column_value ) );
165
	}
166
167
	/**
168
	 * Insert a new row
169
	 *
170
	 * @since  1.0
171
	 * @access public
172
     *
173
     * @param  array  $data
174
     * @param  string $type
175
     *
176
	 * @return int
177
	 */
178
	public function insert( $data, $type = '' ) {
179
        /* @var WPDB $wpdb */
180
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
181
182
		// Set default values.
183
		$data = wp_parse_args( $data, $this->get_column_defaults() );
184
185
		/**
186
		 * Fires before inserting data to the database.
187
		 *
188
		 * @since 1.0
189
		 *
190
		 * @param array $data
191
		 */
192
		do_action( "give_pre_insert_{$type}", $data );
193
194
		// Initialise column format array
195
		$column_formats = $this->get_columns();
196
197
		// Force fields to lower case
198
		$data = array_change_key_case( $data );
199
200
		// White list columns
201
		$data = array_intersect_key( $data, $column_formats );
202
203
		// Reorder $column_formats to match the order of columns given in $data
204
		$data_keys      = array_keys( $data );
205
		$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
206
207
		$wpdb->insert( $this->table_name, $data, $column_formats );
208
209
		/**
210
		 * Fires after inserting data to the database.
211
		 *
212
		 * @since 1.0
213
		 *
214
		 * @param int   $insert_id
215
		 * @param array $data
216
		 */
217
		do_action( "give_post_insert_{$type}", $wpdb->insert_id, $data );
218
219
		return $wpdb->insert_id;
220
	}
221
222
	/**
223
	 * Update a row
224
	 *
225
	 * @since  1.0
226
	 * @access public
227
     *
228
     * @param  int    $row_id Column ID
229
     * @param  array  $data
230
     * @param  string $where  Column value
231
     *
232
	 * @return bool
233
	 */
234
	public function update( $row_id, $data = array(), $where = '' ) {
235
        /* @var WPDB $wpdb */
236
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
237
238
		// Row ID must be positive integer
239
		$row_id = absint( $row_id );
240
241
		if ( empty( $row_id ) ) {
242
			return false;
243
		}
244
245
		if ( empty( $where ) ) {
246
			$where = $this->primary_key;
247
		}
248
249
		// Initialise column format array
250
		$column_formats = $this->get_columns();
251
252
		// Force fields to lower case
253
		$data = array_change_key_case( $data );
254
255
		// White list columns
256
		$data = array_intersect_key( $data, $column_formats );
257
258
		// Reorder $column_formats to match the order of columns given in $data
259
		$data_keys      = array_keys( $data );
260
		$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
261
262
		if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) {
263
			return false;
264
		}
265
266
		return true;
267
	}
268
269
	/**
270
	 * Delete a row identified by the primary key
271
	 *
272
	 * @since  1.0
273
	 * @access public
274
     *
275
     * @param  int $row_id Column ID.
276
     *
277
	 * @return bool
278
	 */
279
	public function delete( $row_id = 0 ) {
280
        /* @var WPDB $wpdb */
281
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
282
283
		// Row ID must be positive integer
284
		$row_id = absint( $row_id );
285
286
		if ( empty( $row_id ) ) {
287
			return false;
288
		}
289
290
		if ( false === $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $this->primary_key = %d", $row_id ) ) ) {
291
			return false;
292
		}
293
294
		return true;
295
	}
296
297
	/**
298
	 * Check if the given table exists
299
	 *
300
	 * @since  1.3.2
301
	 * @access public
302
     *
303
	 * @param  string $table The table name.
304
     *
305
	 * @return bool          If the table name exists.
306
	 */
307
	public function table_exists( $table ) {
308
        /* @var WPDB $wpdb */
309
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
310
311
		$table = sanitize_text_field( $table );
312
313
		return $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $table ) ) === $table;
314
	}
315
316
	/**
317
	 * Check if the table was ever installed
318
	 *
319
	 * @since  1.6
320
	 * @access public
321
	 *
322
	 * @return bool Returns if the customers table was installed and upgrade routine run.
323
	 */
324
	public function installed() {
325
		return $this->table_exists( $this->table_name );
326
	}
327
328
	/**
329
	 * Register tables
330
	 *
331
	 * @since 1.8.9
332
	 * @access public
333
	 */
334
	public function register_table() {
335
		$current_version = get_option( $this->table_name . '_db_version' );
336
		if ( ! $current_version || version_compare( $current_version, $this->version, '<' ) ) {
337
			$this->create_table();
338
		}
339
	}
340
341
	/**
342
	 * Create table
343
	 *
344
	 * @since  1.8.9
345
	 * @access public
346
	 */
347
	public function create_table(){}
348
}
349