Base.php
7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* Base Custom Database Table Class.
*
* @package Index
* @subpackage Base
* @copyright Copyright (c) 2019
* @license https://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.0.0
*/
namespace SearchWP\Index\Engine;
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/**
* The base class that all other database base classes extend.
*
* This class attempts to provide some universal immutability to all other
* classes that extend it, starting with a magic getter, but likely expanding
* into a magic call handler and others.
*
* @since 1.0.0
*/
class Base {
/**
* The name of the PHP global that contains the primary database interface.
*
* For example, WordPress traditionally uses 'wpdb', but other applications
* may use something else, or you may be doing something really cool that
* requires a custom interface.
*
* @since 1.0.0
* @var string
*/
protected $db_global = 'wpdb';
/** Global Properties *****************************************************/
/**
* Global prefix used for tables/hooks/cache-groups/etc...
*
* @since 1.0.0
* @var string
*/
protected $prefix = 'searchwp';
/**
* The last database error, if any.
*
* @since 1.0.0
* @var mixed
*/
protected $last_error = false;
/** Public ****************************************************************/
/**
* Magic isset'ter for immutability.
*
* @since 1.0.0
*
* @param string $key
* @return mixed
*/
public function __isset( $key = '' ) {
// No more uppercase ID properties ever
if ( 'ID' === $key ) {
$key = 'id';
}
// Class method to try and call
$method = "get_{$key}";
// Return property if exists
if ( method_exists( $this, $method ) ) {
return true;
// Return get method results if exists
} elseif ( property_exists( $this, $key ) ) {
return true;
}
// Return false if not exists
return false;
}
/**
* Magic getter for immutability.
*
* @since 1.0.0
*
* @param string $key
* @return mixed
*/
public function __get( $key = '' ) {
// No more uppercase ID properties ever
if ( 'ID' === $key ) {
$key = 'id';
}
// Class method to try and call
$method = "get_{$key}";
// Return property if exists
if ( method_exists( $this, $method ) ) {
return call_user_func( array( $this, $method ) );
// Return get method results if exists
} elseif ( property_exists( $this, $key ) ) {
return $this->{$key};
}
// Return null if not exists
return null;
}
/**
* Converts the given object to an array.
*
* @since 1.0.0
*
* @return array Array version of the given object.
*/
public function to_array() {
return get_object_vars( $this );
}
/** Protected *************************************************************/
/**
* Maybe append the prefix to string.
*
* @since 1.0.0
*
* @param string $string
* @param string $sep
* @return string
*/
protected function apply_prefix( $string = '', $sep = '_' ) {
return ! empty( $this->prefix )
? "{$this->prefix}{$sep}{$string}"
: $string;
}
/**
* Return the first letters of a string of words with a separator.
*
* Used primarily to guess at table aliases when none is manually set.
*
* Applies the following formatting to a string:
* - Trim whitespace
* - No accents
* - No trailing underscores
*
* @since 1.0.0
*
* @param string $string
* @param string $sep
* @return string
*/
protected function first_letters( $string = '', $sep = '_' ) {
// Set empty default return value
$retval = '';
// Bail if empty or not a string
if ( empty( $string ) || ! is_string( $string ) ) {
return $retval;
}
// Trim spaces off the ends
$unspace = trim( $string );
$accents = remove_accents( $unspace );
$lower = strtolower( $accents );
$parts = explode( $sep, $lower );
// Loop through parts and concatenate the first letters together
foreach ( $parts as $part ) {
$retval .= substr( $part, 0, 1 );
}
// Return the result
return $retval;
}
/**
* Sanitize a table name string.
*
* Used to make sure that a table name value meets MySQL expectations.
*
* Applies the following formatting to a string:
* - Trim whitespace
* - No accents
* - No special characters
* - No hyphens
* - No double underscores
* - No trailing underscores
*
* @since 1.0.0
*
* @param string $name The name of the database table
*
* @return string Sanitized database table name
*/
protected function sanitize_table_name( $name = '' ) {
// Bail if empty or not a string
if ( empty( $name ) || ! is_string( $name ) ) {
return false;
}
// Trim spaces off the ends
$unspace = trim( $name );
// Only non-accented table names (avoid truncation)
$accents = remove_accents( $unspace );
// Only lowercase characters, hyphens, and dashes (avoid index corruption)
$lower = sanitize_key( $accents );
// Replace hyphens with single underscores
$under = str_replace( '-', '_', $lower );
// Single underscores only
$single = str_replace( '__', '_', $under );
// Remove trailing underscores
$clean = trim( $single, '_' );
// Bail if table name was garbaged
if ( empty( $clean ) ) {
return false;
}
// Return the cleaned table name
return $clean;
}
/**
* Set class variables from arguments.
*
* @since 1.0.0
* @param array $args
*/
protected function set_vars( $args = array() ) {
// Bail if empty or not an array
if ( empty( $args ) ) {
return;
}
// Cast to an array
if ( ! is_array( $args ) ) {
$args = (array) $args;
}
// Set all properties
foreach ( $args as $key => $value ) {
$this->{$key} = $value;
}
}
/**
* Return the global database interface.
*
* See: https://core.trac.wordpress.org/ticket/31556
*
* @since 1.0.0
*
* @return object Database interface, or False if not set
*/
protected function get_db() {
// Default database return value (might change)
$retval = false;
// Look for a commonly used global database interface
if ( isset( $GLOBALS[ $this->db_global ] ) ) {
$retval = $GLOBALS[ $this->db_global ];
}
/*
* Developer note:
*
* It should be impossible for a database table to be interacted with
* before the primary database interface it is setup.
*
* However, because applications are complicated, it is unsafe to assume
* anything, so this silently returns false instead of halting everything.
*
* If you are here because this method is returning false for you, that
* means the database table is being invoked too early in the lifecycle
* of the application.
*
* In WordPress, that means before the $wpdb global is created; in other
* environments, you will need to adjust accordingly.
*/
// Return the database interface
return $retval;
}
/**
* Check if an operation succeeded.
*
* @since 1.0.0
*
* @param mixed $result
* @return bool
*/
protected function is_success( $result = false ) {
// Bail if no row exists
if ( empty( $result ) ) {
$retval = false;
// Bail if an error occurred
} elseif ( is_wp_error( $result ) ) {
$this->last_error = $result;
$retval = false;
// No errors
} else {
$retval = true;
}
// Return the result
return (bool) $retval;
}
}