Completed
Pull Request — master (#613)
by Devin
30:57 queued 27:10
created

Give_DB::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 2
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( "SELECT * FROM $this->table_name WHERE $this->primary_key = $row_id LIMIT 1;" );
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
99
		return $wpdb->get_row( "SELECT * FROM $this->table_name WHERE $column = '$row_id' LIMIT 1;" );
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 34
	public function get_column( $column, $row_id ) {
110 34
		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
112 34
		return $wpdb->get_var( "SELECT $column FROM $this->table_name WHERE $this->primary_key = $row_id LIMIT 1;" );
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
125 2
		return $wpdb->get_var( "SELECT $column FROM $this->table_name WHERE $column_where = '$column_value' LIMIT 1;" );
126
	}
127
128
	/**
129
	 * Insert a new row
130
	 *
131
	 * @access  public
132
	 * @since   1.0
133
	 * @return  int
134
	 */
135 34
	public function insert( $data, $type = '' ) {
136 34
		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...
137
138
		// Set default values
139 34
		$data = wp_parse_args( $data, $this->get_column_defaults() );
140
141 34
		do_action( 'give_pre_insert_' . $type, $data );
142
143
		// Initialise column format array
144 34
		$column_formats = $this->get_columns();
145
146
		// Force fields to lower case
147 34
		$data = array_change_key_case( $data );
148
149
		// White list columns
150 34
		$data = array_intersect_key( $data, $column_formats );
151
152
		// Reorder $column_formats to match the order of columns given in $data
153 34
		$data_keys      = array_keys( $data );
154 34
		$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
155
156 34
		$wpdb->insert( $this->table_name, $data, $column_formats );
157
158 34
		do_action( 'give_post_insert_' . $type, $wpdb->insert_id, $data );
159
160 34
		return $wpdb->insert_id;
161
	}
162
163
	/**
164
	 * Update a row
165
	 *
166
	 * @access  public
167
	 * @since   1.0
168
	 * @return  bool
169
	 */
170 34
	public function update( $row_id, $data = array(), $where = '' ) {
171
172 34
		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...
173
174
		// Row ID must be positive integer
175 34
		$row_id = absint( $row_id );
176
177 34
		if ( empty( $row_id ) ) {
178
			return false;
179
		}
180
181 34
		if ( empty( $where ) ) {
182 34
			$where = $this->primary_key;
183 34
		}
184
185
		// Initialise column format array
186 34
		$column_formats = $this->get_columns();
187
188
		// Force fields to lower case
189 34
		$data = array_change_key_case( $data );
190
191
		// White list columns
192 34
		$data = array_intersect_key( $data, $column_formats );
193
194
		// Reorder $column_formats to match the order of columns given in $data
195 34
		$data_keys      = array_keys( $data );
196 34
		$column_formats = array_merge( array_flip( $data_keys ), $column_formats );
197
198 34
		if ( false === $wpdb->update( $this->table_name, $data, array( $where => $row_id ), $column_formats ) ) {
199
			return false;
200
		}
201
202 34
		return true;
203
	}
204
205
	/**
206
	 * Delete a row identified by the primary key
207
	 *
208
	 * @access  public
209
	 * @since   1.0
210
	 * @return  bool
211
	 */
212
	public function delete( $row_id = 0 ) {
213
214
		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...
215
216
		// Row ID must be positive integer
217
		$row_id = absint( $row_id );
218
219
		if ( empty( $row_id ) ) {
220
			return false;
221
		}
222
223
		if ( false === $wpdb->query( $wpdb->prepare( "DELETE FROM $this->table_name WHERE $this->primary_key = %d", $row_id ) ) ) {
224
			return false;
225
		}
226
227
		return true;
228
	}
229
	
230
	/**
231
	 * Check if the given table exists
232
	 *
233
	 * @since  1.3.2
234
	 * @param  string $table The table name
235
	 * @return bool          If the table name exists
236
	 */
237 2
	public function table_exists( $table ) {
238 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...
239 2
		$table = sanitize_text_field( $table );
240
		
241 2
		return $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $table ) ) === $table;
242
	}
243
244
}
245