Completed
Push — master ( 647547...13dcfc )
by Devin
17:56
created

Give_DB   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 64.52%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 227
rs 10
ccs 40
cts 62
cp 0.6452
wmc 16
lcom 1
cbo 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get_columns() 0 3 1
A get_column_defaults() 0 3 1
A get() 0 5 1
A get_by() 0 5 1
A get_column() 0 5 1
A get_column_by() 0 6 1
B insert() 0 27 1
B update() 0 34 4
A delete() 0 17 3
A table_exists() 0 6 1
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 19 and the first side effect is on line 13.

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 base class
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_DB
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.0
10
 */
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
} // Exit if accessed directly
15
16
/**
17
 * Class Give_DB
18
 */
19
abstract class Give_DB {
20
21
	/**
22
	 * The name of our database table
23
	 *
24
	 * @access  public
25
	 * @since   1.0
26
	 */
27
	public $table_name;
28
29
	/**
30
	 * The version of our database table
31
	 *
32
	 * @access  public
33
	 * @since   1.0
34
	 */
35
	public $version;
36
37
	/**
38
	 * The name of the primary column
39
	 *
40
	 * @access  public
41
	 * @since   1.0
42
	 */
43
	public $primary_key;
44
45
	/**
46
	 * Get things started
47
	 *
48
	 * @access  public
49
	 * @since   1.0
50
	 */
51
	public function __construct() {
52
	}
53
54
	/**
55
	 * Whitelist of columns
56
	 *
57
	 * @access  public
58
	 * @since   1.0
59
	 * @return  array
60
	 */
61
	public function get_columns() {
62
		return array();
63
	}
64
65
	/**
66
	 * Default column values
67
	 *
68
	 * @access  public
69
	 * @since   1.0
70
	 * @return  array
71
	 */
72
	public function get_column_defaults() {
73
		return array();
74
	}
75
76
	/**
77
	 * Retrieve a row by the primary key
78
	 *
79
	 * @access  public
80
	 * @since   1.0
81
	 * @return  object
82
	 */
83
	public function get( $row_id ) {
84
		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...
85
86
		return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
87
	}
88
89
	/**
90
	 * Retrieve a row by a specific column / value
91
	 *
92
	 * @access  public
93
	 * @since   1.0
94
	 * @return  object
95
	 */
96
	public function get_by( $column, $row_id ) {
97
		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...
98
		$column = esc_sql( $column );
99
		return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $this->table_name WHERE $column = %s LIMIT 1;", $row_id ) );
100
	}
101
102
	/**
103
	 * Retrieve a specific column's value by the primary key
104
	 *
105
	 * @access  public
106
	 * @since   1.0
107
	 * @return  string
108
	 */
109 52
	public function get_column( $column, $row_id ) {
110 52
		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...
111 52
		$column = esc_sql( $column );
112 52
		return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $this->primary_key = %s LIMIT 1;", $row_id ) );
113
	}
114
115
	/**
116
	 * Retrieve a specific column's value by the the specified column / value
117
	 *
118
	 * @access  public
119
	 * @since   1.0
120
	 * @return  string
121
	 */
122 2
	public function get_column_by( $column, $column_where, $column_value ) {
123 2
		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...
124 2
		$column_where = esc_sql( $column_where );
125 2
		$column       = esc_sql( $column );
126 2
		return $wpdb->get_var( $wpdb->prepare( "SELECT $column FROM $this->table_name WHERE $column_where = %s LIMIT 1;", $column_value ) );
127
	}
128
129
	/**
130
	 * Insert a new row
131
	 *
132
	 * @access  public
133
	 * @since   1.0
134
	 * @return  int
135
	 */
136 52
	public function insert( $data, $type = '' ) {
137 52
		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...
138
139
		// Set default values
140 52
		$data = wp_parse_args( $data, $this->get_column_defaults() );
141
142 52
		do_action( 'give_pre_insert_' . $type, $data );
143
144
		// Initialise column format array
145 52
		$column_formats = $this->get_columns();
146
147
		// Force fields to lower case
148 52
		$data = array_change_key_case( $data );
149
150
		// White list columns
151 52
		$data = array_intersect_key( $data, $column_formats );
152
153
		// Reorder $column_formats to match the order of columns given in $data
154 52
		$data_keys      = array_keys( $data );
155 52
		$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
156
157 52
		$wpdb->insert( $this->table_name, $data, $column_formats );
158
159 52
		do_action( 'give_post_insert_' . $type, $wpdb->insert_id, $data );
160
161 52
		return $wpdb->insert_id;
162
	}
163
164
	/**
165
	 * Update a row
166
	 *
167
	 * @access  public
168
	 * @since   1.0
169
	 * @return  bool
170
	 */
171 52
	public function update( $row_id, $data = array(), $where = '' ) {
172
173 52
		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...
174
175
		// Row ID must be positive integer
176 52
		$row_id = absint( $row_id );
177
178 52
		if ( empty( $row_id ) ) {
179 1
			return false;
180
		}
181
182 52
		if ( empty( $where ) ) {
183 52
			$where = $this->primary_key;
184 52
		}
185
186
		// Initialise column format array
187 52
		$column_formats = $this->get_columns();
188
189
		// Force fields to lower case
190 52
		$data = array_change_key_case( $data );
191
192
		// White list columns
193 52
		$data = array_intersect_key( $data, $column_formats );
194
195
		// Reorder $column_formats to match the order of columns given in $data
196 52
		$data_keys      = array_keys( $data );
197 52
		$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
198
199 52
		if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) {
200
			return false;
201
		}
202
203 52
		return true;
204
	}
205
206
	/**
207
	 * Delete a row identified by the primary key
208
	 *
209
	 * @access  public
210
	 * @since   1.0
211
	 * @return  bool
212
	 */
213
	public function delete( $row_id = 0 ) {
214
215
		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...
216
217
		// Row ID must be positive integer
218
		$row_id = absint( $row_id );
219
220
		if ( empty( $row_id ) ) {
221
			return false;
222
		}
223
224
		if ( false === $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $this->primary_key = %d", $row_id ) ) ) {
225
			return false;
226
		}
227
228
		return true;
229
	}
230
231
	/**
232
	 * Check if the given table exists
233
	 *
234
	 * @since  1.3.2
235
	 * @param  string $table The table name
236
	 * @return bool          If the table name exists
237
	 */
238 2
	public function table_exists( $table ) {
239 2
		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...
240 2
		$table = sanitize_text_field( $table );
241
242 2
		return $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $table ) ) === $table;
243
	}
244
245
}