1 | <?php |
||||||
0 ignored issues
–
show
introduced
by
![]() |
|||||||
2 | namespace PodloveSubscribeButton\Model; |
||||||
3 | |||||||
4 | abstract class Base { |
||||||
0 ignored issues
–
show
|
|||||||
5 | /** |
||||||
6 | * Property dictionary for all tables |
||||||
7 | */ |
||||||
0 ignored issues
–
show
|
|||||||
8 | private static $properties = array(); |
||||||
9 | |||||||
10 | private $is_new = true; |
||||||
0 ignored issues
–
show
|
|||||||
11 | |||||||
12 | /** |
||||||
13 | * Contains property values |
||||||
14 | */ |
||||||
0 ignored issues
–
show
|
|||||||
15 | private $data = array(); |
||||||
16 | |||||||
17 | public function __set( $name, $value ) { |
||||||
0 ignored issues
–
show
|
|||||||
18 | if ( static::has_property( $name ) ) { |
||||||
19 | $this->set_property( $name, $value ); |
||||||
20 | } else { |
||||||
21 | $this->$name = $value; |
||||||
22 | } |
||||||
23 | } |
||||||
0 ignored issues
–
show
|
|||||||
24 | |||||||
25 | private function set_property( $name, $value ) { |
||||||
0 ignored issues
–
show
|
|||||||
26 | $this->data[ $name ] = $value; |
||||||
27 | } |
||||||
0 ignored issues
–
show
|
|||||||
28 | |||||||
29 | public function __get( $name ) { |
||||||
0 ignored issues
–
show
|
|||||||
30 | if ( static::has_property( $name ) ) { |
||||||
31 | return $this->get_property( $name ); |
||||||
32 | } elseif ( property_exists( $this, $name ) ) { |
||||||
33 | return $this->$name; |
||||||
34 | } else { |
||||||
35 | return null; |
||||||
36 | } |
||||||
37 | } |
||||||
0 ignored issues
–
show
|
|||||||
38 | |||||||
39 | private function get_property( $name ) { |
||||||
0 ignored issues
–
show
|
|||||||
40 | if ( isset( $this->data[ $name ] ) ) { |
||||||
41 | return $this->data[ $name ]; |
||||||
42 | } else { |
||||||
43 | return null; |
||||||
44 | } |
||||||
45 | } |
||||||
0 ignored issues
–
show
|
|||||||
46 | |||||||
47 | private static function unserialize_property($property) { |
||||||
0 ignored issues
–
show
|
|||||||
48 | if ( ! isset($property) ) |
||||||
0 ignored issues
–
show
|
|||||||
49 | return; |
||||||
50 | |||||||
51 | if ( $unserialized_string = is_serialized($property) ) |
||||||
0 ignored issues
–
show
|
|||||||
52 | return unserialize($property); |
||||||
0 ignored issues
–
show
|
|||||||
53 | |||||||
54 | return $property; |
||||||
55 | } |
||||||
0 ignored issues
–
show
|
|||||||
56 | |||||||
57 | /** |
||||||
58 | * Retrieves the database table name. |
||||||
59 | * |
||||||
60 | * The name is derived from the namespace an class name. Additionally, it |
||||||
61 | * is prefixed with the global WordPress database table prefix. |
||||||
62 | * @todo cache |
||||||
0 ignored issues
–
show
|
|||||||
63 | * |
||||||
64 | * @return string database table name |
||||||
65 | */ |
||||||
66 | public static function table_name() { |
||||||
0 ignored issues
–
show
|
|||||||
67 | global $wpdb; |
||||||
68 | |||||||
69 | // prefix with $wpdb prefix |
||||||
0 ignored issues
–
show
|
|||||||
70 | return $wpdb->prefix . static::name(); |
||||||
71 | } |
||||||
0 ignored issues
–
show
|
|||||||
72 | |||||||
73 | /** |
||||||
0 ignored issues
–
show
|
|||||||
74 | * Define a property with name and type. |
||||||
75 | * |
||||||
76 | * Currently only supports basics. |
||||||
77 | * @todo enable additional options like NOT NULL, DEFAULT etc. |
||||||
0 ignored issues
–
show
|
|||||||
78 | * |
||||||
79 | * @param string $name Name of the property / column |
||||||
0 ignored issues
–
show
|
|||||||
80 | * @param string $type mySQL column type |
||||||
0 ignored issues
–
show
|
|||||||
81 | */ |
||||||
82 | public static function property( $name, $type, $args = array() ) { |
||||||
83 | $class = get_called_class(); |
||||||
84 | |||||||
85 | if ( ! isset( static::$properties[ $class ] ) ) { |
||||||
0 ignored issues
–
show
|
|||||||
86 | static::$properties[ $class ] = array(); |
||||||
87 | } |
||||||
88 | |||||||
89 | // "id" columns and those ending on "_id" get an index by default |
||||||
90 | $index = $name == 'id' || stripos( $name, '_id' ); |
||||||
0 ignored issues
–
show
|
|||||||
91 | // but if the argument is set, it overrides the default |
||||||
0 ignored issues
–
show
|
|||||||
92 | if (isset($args['index'])) { |
||||||
0 ignored issues
–
show
|
|||||||
93 | $index = $args['index']; |
||||||
94 | } |
||||||
95 | |||||||
96 | static::$properties[ $class ][] = array( |
||||||
97 | 'name' => $name, |
||||||
0 ignored issues
–
show
|
|||||||
98 | 'type' => $type, |
||||||
0 ignored issues
–
show
|
|||||||
99 | 'index' => $index, |
||||||
0 ignored issues
–
show
|
|||||||
100 | 'index_length' => isset($args['index_length']) ? $args['index_length'] : null, |
||||||
0 ignored issues
–
show
|
|||||||
101 | 'unique' => isset($args['unique']) ? $args['unique'] : null |
||||||
0 ignored issues
–
show
|
|||||||
102 | ); |
||||||
103 | } |
||||||
0 ignored issues
–
show
|
|||||||
104 | |||||||
105 | /** |
||||||
106 | * Return a list of property dictionaries. |
||||||
107 | * |
||||||
108 | * @return array property list |
||||||
109 | */ |
||||||
110 | private static function properties() { |
||||||
111 | $class = get_called_class(); |
||||||
112 | |||||||
113 | if ( ! isset( static::$properties[ $class ] ) ) { |
||||||
0 ignored issues
–
show
|
|||||||
114 | static::$properties[ $class ] = array(); |
||||||
115 | } |
||||||
116 | |||||||
117 | return static::$properties[ $class ]; |
||||||
118 | } |
||||||
0 ignored issues
–
show
|
|||||||
119 | |||||||
120 | /** |
||||||
121 | * Does the given property exist? |
||||||
122 | * |
||||||
123 | * @param string $name name of the property to test |
||||||
0 ignored issues
–
show
|
|||||||
124 | * @return bool True if the property exists, else false. |
||||||
125 | */ |
||||||
126 | public static function has_property( $name ) { |
||||||
0 ignored issues
–
show
|
|||||||
127 | return in_array( $name, static::property_names() ); |
||||||
0 ignored issues
–
show
|
|||||||
128 | } |
||||||
0 ignored issues
–
show
|
|||||||
129 | |||||||
130 | /** |
||||||
131 | * Return a list of property names. |
||||||
132 | * |
||||||
133 | * @return array property names |
||||||
134 | */ |
||||||
135 | public static function property_names() { |
||||||
0 ignored issues
–
show
|
|||||||
136 | return array_map( function ( $p ) { return $p['name']; } , static::properties() ); |
||||||
0 ignored issues
–
show
|
|||||||
137 | } |
||||||
0 ignored issues
–
show
|
|||||||
138 | |||||||
139 | /** |
||||||
140 | * Does the table have any entries? |
||||||
141 | * |
||||||
142 | * @return bool True if there is at least one entry, else false. |
||||||
143 | */ |
||||||
144 | public static function has_entries() { |
||||||
0 ignored issues
–
show
|
|||||||
145 | return static::count() > 0; |
||||||
146 | } |
||||||
0 ignored issues
–
show
|
|||||||
147 | |||||||
148 | /** |
||||||
149 | * Return number of rows in the table. |
||||||
150 | * |
||||||
151 | * @return int number of rows |
||||||
152 | */ |
||||||
153 | public static function count() { |
||||||
154 | global $wpdb; |
||||||
155 | |||||||
156 | $sql = 'SELECT COUNT(*) FROM ' . static::table_name(); |
||||||
157 | return (int) $wpdb->get_var( $sql ); |
||||||
0 ignored issues
–
show
|
|||||||
158 | } |
||||||
0 ignored issues
–
show
|
|||||||
159 | |||||||
160 | public static function find_by_id( $id ) { |
||||||
0 ignored issues
–
show
|
|||||||
161 | global $wpdb; |
||||||
162 | |||||||
163 | $class = get_called_class(); |
||||||
164 | $model = new $class(); |
||||||
165 | $model->flag_as_not_new(); |
||||||
166 | |||||||
167 | $row = $wpdb->get_row( 'SELECT * FROM ' . static::table_name() . ' WHERE id = ' . (int) $id ); |
||||||
0 ignored issues
–
show
|
|||||||
168 | |||||||
169 | if ( ! $row ) { |
||||||
170 | return null; |
||||||
171 | } |
||||||
172 | |||||||
173 | foreach ( $row as $property => $value ) { |
||||||
174 | $model->$property = static::unserialize_property($value); |
||||||
0 ignored issues
–
show
|
|||||||
175 | } |
||||||
176 | |||||||
177 | return $model; |
||||||
178 | } |
||||||
0 ignored issues
–
show
|
|||||||
179 | |||||||
180 | public static function find_one_by_property( $property, $value ) { |
||||||
0 ignored issues
–
show
|
|||||||
181 | global $wpdb; |
||||||
182 | |||||||
183 | $class = get_called_class(); |
||||||
184 | $model = new $class(); |
||||||
185 | $model->flag_as_not_new(); |
||||||
186 | |||||||
187 | $query = $wpdb->prepare('SELECT * FROM ' . static::table_name() . ' WHERE ' . $property . ' = \'%s\' LIMIT 0,1', $value); |
||||||
0 ignored issues
–
show
|
|||||||
188 | $row = $wpdb->get_row($query); |
||||||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||||||
189 | |||||||
190 | if ( ! $row ) { |
||||||
191 | return null; |
||||||
192 | } |
||||||
193 | |||||||
194 | foreach ( $row as $property => $value ) { |
||||||
0 ignored issues
–
show
|
|||||||
195 | $model->$property = static::unserialize_property($value); |
||||||
0 ignored issues
–
show
|
|||||||
196 | } |
||||||
197 | |||||||
198 | return $model; |
||||||
199 | } |
||||||
0 ignored issues
–
show
|
|||||||
200 | |||||||
201 | /** |
||||||
202 | * Retrieve all entries from the table. |
||||||
203 | * |
||||||
204 | * @return array list of model objects |
||||||
205 | */ |
||||||
206 | public static function all() { |
||||||
207 | global $wpdb; |
||||||
208 | |||||||
209 | $class = get_called_class(); |
||||||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||||||
210 | $models = array(); |
||||||
211 | |||||||
212 | $rows = $wpdb->get_results( 'SELECT * FROM ' . static::table_name() ); |
||||||
0 ignored issues
–
show
|
|||||||
213 | |||||||
214 | foreach ( $rows as $row ) { |
||||||
215 | $model = new $class(); |
||||||
216 | $model->flag_as_not_new(); |
||||||
217 | foreach ( $row as $property => $value ) { |
||||||
218 | $model->$property = static::unserialize_property($value); |
||||||
0 ignored issues
–
show
|
|||||||
219 | } |
||||||
220 | $models[] = $model; |
||||||
221 | } |
||||||
222 | |||||||
223 | return $models; |
||||||
224 | } |
||||||
0 ignored issues
–
show
|
|||||||
225 | |||||||
226 | /** |
||||||
227 | * True if not yet saved to database. Else false. |
||||||
228 | */ |
||||||
229 | public function is_new() { |
||||||
0 ignored issues
–
show
|
|||||||
230 | return $this->is_new; |
||||||
231 | } |
||||||
0 ignored issues
–
show
|
|||||||
232 | |||||||
233 | public function flag_as_not_new() { |
||||||
0 ignored issues
–
show
|
|||||||
234 | $this->is_new = false; |
||||||
235 | } |
||||||
0 ignored issues
–
show
|
|||||||
236 | |||||||
237 | /** |
||||||
238 | * Rails-ish update_attributes for easy form handling. |
||||||
239 | * |
||||||
240 | * Takes an array of form values and takes care of serializing it. |
||||||
241 | * |
||||||
242 | * @param array $attributes |
||||||
0 ignored issues
–
show
|
|||||||
243 | * @return bool |
||||||
244 | */ |
||||||
245 | public function update_attributes( $attributes ) { |
||||||
0 ignored issues
–
show
|
|||||||
246 | |||||||
247 | if ( ! is_array( $attributes ) ) |
||||||
0 ignored issues
–
show
|
|||||||
248 | return false; |
||||||
249 | |||||||
250 | $request = filter_input_array(INPUT_POST); // Do this for security reasons |
||||||
0 ignored issues
–
show
|
|||||||
251 | |||||||
252 | foreach ( $attributes as $key => $value ) { |
||||||
253 | if ( is_array($value) ) { |
||||||
0 ignored issues
–
show
|
|||||||
254 | $this->{$key} = serialize($value); |
||||||
0 ignored issues
–
show
|
|||||||
255 | } else { |
||||||
256 | $this->{$key} = esc_sql($value); |
||||||
0 ignored issues
–
show
|
|||||||
257 | } |
||||||
258 | } |
||||||
259 | |||||||
260 | if ( isset( $request['checkboxes'] ) && is_array( $request['checkboxes'] ) ) { |
||||||
261 | foreach ( $request['checkboxes'] as $checkbox ) { |
||||||
262 | if ( isset( $attributes[ $checkbox ] ) && $attributes[ $checkbox ] === 'on' ) { |
||||||
0 ignored issues
–
show
|
|||||||
263 | $this->$checkbox = 1; |
||||||
264 | } else { |
||||||
265 | $this->$checkbox = 0; |
||||||
266 | } |
||||||
267 | } |
||||||
268 | } |
||||||
269 | |||||||
270 | // @todo this is the wrong place to do this! |
||||||
271 | // The feed password is the only "passphrase" which is saved. It is not encrypted! |
||||||
272 | // However, we keep this function for later use |
||||||
273 | if ( isset( $request['passwords'] ) && is_array( $request['passwords'] ) ) { |
||||||
274 | foreach ( $request['passwords'] as $password ) { |
||||||
275 | $this->$password = $attributes[ $password ]; |
||||||
276 | } |
||||||
277 | } |
||||||
278 | return $this->save(); |
||||||
279 | } |
||||||
0 ignored issues
–
show
|
|||||||
280 | |||||||
281 | /** |
||||||
282 | * Update and save a single attribute. |
||||||
283 | * |
||||||
0 ignored issues
–
show
|
|||||||
284 | * @param string $attribute attribute name |
||||||
0 ignored issues
–
show
|
|||||||
285 | * @param mixed $value |
||||||
0 ignored issues
–
show
|
|||||||
286 | * @return (bool) query success |
||||||
287 | */ |
||||||
288 | public function update_attribute($attribute, $value) { |
||||||
0 ignored issues
–
show
|
|||||||
289 | global $wpdb; |
||||||
290 | |||||||
291 | $this->$attribute = $value; |
||||||
292 | |||||||
293 | $sql = sprintf( |
||||||
294 | "UPDATE %s SET %s = '%s' WHERE id = %s", |
||||||
295 | static::table_name(), |
||||||
296 | $attribute, |
||||||
297 | mysqli_real_escape_string($value), |
||||||
0 ignored issues
–
show
The call to
mysqli_real_escape_string() has too few arguments starting with string .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
298 | $this->id |
||||||
299 | ); |
||||||
300 | |||||||
301 | return $wpdb->query( $sql ); |
||||||
0 ignored issues
–
show
|
|||||||
302 | } |
||||||
0 ignored issues
–
show
|
|||||||
303 | |||||||
304 | /** |
||||||
305 | * Saves changes to database. |
||||||
306 | * |
||||||
307 | * @todo use wpdb::insert() |
||||||
308 | */ |
||||||
309 | public function save() { |
||||||
310 | global $wpdb; |
||||||
311 | |||||||
312 | if ( $this->is_new() ) { |
||||||
313 | |||||||
314 | $this->set_defaults(); |
||||||
315 | |||||||
316 | $sql = 'INSERT INTO ' |
||||||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||||||
317 | . static::table_name() |
||||||
0 ignored issues
–
show
|
|||||||
318 | . ' ( ' |
||||||
0 ignored issues
–
show
|
|||||||
319 | . implode( ',', static::property_names() ) |
||||||
0 ignored issues
–
show
|
|||||||
320 | . ' ) ' |
||||||
0 ignored issues
–
show
|
|||||||
321 | . 'VALUES' |
||||||
0 ignored issues
–
show
|
|||||||
322 | . ' ( ' |
||||||
0 ignored issues
–
show
|
|||||||
323 | . implode( ',', array_map( array( $this, 'property_name_to_sql_value' ), static::property_names() ) ) |
||||||
0 ignored issues
–
show
|
|||||||
324 | . ' );' |
||||||
0 ignored issues
–
show
|
|||||||
325 | ; |
||||||
0 ignored issues
–
show
|
|||||||
326 | $success = $wpdb->query( $sql ); |
||||||
0 ignored issues
–
show
|
|||||||
327 | if ( $success ) { |
||||||
328 | $this->id = $wpdb->insert_id; |
||||||
0 ignored issues
–
show
|
|||||||
329 | } |
||||||
330 | } else { |
||||||
331 | $sql = 'UPDATE ' . static::table_name() |
||||||
332 | . ' SET ' |
||||||
0 ignored issues
–
show
|
|||||||
333 | . implode( ',', array_map( array( $this, 'property_name_to_sql_update_statement' ), static::property_names() ) ) |
||||||
0 ignored issues
–
show
|
|||||||
334 | . ' WHERE id = ' . $this->id |
||||||
0 ignored issues
–
show
|
|||||||
335 | ; |
||||||
0 ignored issues
–
show
|
|||||||
336 | |||||||
337 | $success = $wpdb->query( $sql ); |
||||||
0 ignored issues
–
show
|
|||||||
338 | } |
||||||
339 | |||||||
340 | $this->is_new = false; |
||||||
341 | |||||||
342 | do_action('podlove_model_save', $this); |
||||||
0 ignored issues
–
show
|
|||||||
343 | do_action('podlove_model_change', $this); |
||||||
0 ignored issues
–
show
|
|||||||
344 | |||||||
345 | return $success; |
||||||
346 | } |
||||||
0 ignored issues
–
show
|
|||||||
347 | |||||||
348 | /** |
||||||
349 | * Sets default values. |
||||||
350 | * |
||||||
351 | * @return array |
||||||
352 | */ |
||||||
353 | private function set_defaults() { |
||||||
0 ignored issues
–
show
|
|||||||
354 | |||||||
355 | $defaults = $this->default_values(); |
||||||
356 | |||||||
357 | if ( ! is_array( $defaults ) || empty( $defaults ) ) |
||||||
0 ignored issues
–
show
|
|||||||
358 | return; |
||||||
359 | |||||||
360 | foreach ( $defaults as $property => $value ) { |
||||||
361 | if ( $this->$property === null ) |
||||||
0 ignored issues
–
show
|
|||||||
362 | $this->$property = $value; |
||||||
363 | } |
||||||
364 | |||||||
365 | } |
||||||
366 | |||||||
367 | /** |
||||||
368 | * Return default values for properties. |
||||||
369 | * |
||||||
370 | * Can be overridden by inheriting model classes. |
||||||
371 | * |
||||||
372 | * @return array |
||||||
373 | */ |
||||||
374 | public function default_values() { |
||||||
0 ignored issues
–
show
|
|||||||
375 | return array(); |
||||||
376 | } |
||||||
0 ignored issues
–
show
|
|||||||
377 | |||||||
378 | public function delete() { |
||||||
0 ignored issues
–
show
|
|||||||
379 | global $wpdb; |
||||||
380 | |||||||
381 | $sql = 'DELETE FROM ' |
||||||
382 | . static::table_name() |
||||||
0 ignored issues
–
show
|
|||||||
383 | . ' WHERE id = ' . $this->id; |
||||||
0 ignored issues
–
show
|
|||||||
384 | |||||||
385 | $rows_affected = $wpdb->query( $sql ); |
||||||
0 ignored issues
–
show
|
|||||||
386 | |||||||
387 | do_action('podlove_model_delete', $this); |
||||||
0 ignored issues
–
show
|
|||||||
388 | do_action('podlove_model_change', $this); |
||||||
0 ignored issues
–
show
|
|||||||
389 | |||||||
390 | return $rows_affected !== false; |
||||||
0 ignored issues
–
show
|
|||||||
391 | } |
||||||
0 ignored issues
–
show
|
|||||||
392 | |||||||
393 | private function property_name_to_sql_update_statement( $p ) { |
||||||
0 ignored issues
–
show
|
|||||||
394 | global $wpdb; |
||||||
395 | |||||||
396 | if ( $this->$p !== null && $this->$p !== '' ) { |
||||||
0 ignored issues
–
show
|
|||||||
397 | return sprintf( "%s = '%s'", $p, ( is_array($this->$p) ? serialize($this->$p) : $this->$p ) ); |
||||||
0 ignored issues
–
show
|
|||||||
398 | } else { |
||||||
399 | return "$p = NULL"; |
||||||
400 | } |
||||||
401 | } |
||||||
0 ignored issues
–
show
|
|||||||
402 | |||||||
403 | private function property_name_to_sql_value( $p ) { |
||||||
0 ignored issues
–
show
|
|||||||
404 | global $wpdb; |
||||||
405 | |||||||
406 | if ( $this->$p !== null && $this->$p !== '' ) { |
||||||
0 ignored issues
–
show
|
|||||||
407 | return sprintf( "'%s'", $this->$p ); |
||||||
408 | } else { |
||||||
409 | return 'NULL'; |
||||||
410 | } |
||||||
411 | } |
||||||
0 ignored issues
–
show
|
|||||||
412 | |||||||
413 | /** |
||||||
414 | * Create database table based on defined properties. |
||||||
415 | * |
||||||
416 | * Automatically includes an id column as auto incrementing primary key. |
||||||
417 | * @todo allow model changes |
||||||
0 ignored issues
–
show
|
|||||||
418 | */ |
||||||
419 | public static function build() { |
||||||
420 | global $wpdb; |
||||||
421 | |||||||
422 | $property_sql = array(); |
||||||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||||||
423 | foreach ( static::properties() as $property ) |
||||||
0 ignored issues
–
show
|
|||||||
424 | $property_sql[] = "`{$property['name']}` {$property['type']}"; |
||||||
425 | |||||||
426 | $sql = 'CREATE TABLE IF NOT EXISTS ' |
||||||
427 | . static::table_name() |
||||||
0 ignored issues
–
show
|
|||||||
428 | . ' (' |
||||||
0 ignored issues
–
show
|
|||||||
429 | . implode( ',', $property_sql ) |
||||||
0 ignored issues
–
show
|
|||||||
430 | . ' ) CHARACTER SET utf8;' |
||||||
0 ignored issues
–
show
|
|||||||
431 | ; |
||||||
0 ignored issues
–
show
|
|||||||
432 | |||||||
433 | $wpdb->query( $sql ); |
||||||
0 ignored issues
–
show
|
|||||||
434 | |||||||
435 | static::build_indices(); |
||||||
436 | } |
||||||
0 ignored issues
–
show
|
|||||||
437 | |||||||
438 | /** |
||||||
439 | * Convention based index generation. |
||||||
440 | * |
||||||
441 | * Creates default indices for all columns matching both: |
||||||
442 | * - equals "id" or contains "_id" |
||||||
443 | * - doesn't have an index yet |
||||||
444 | */ |
||||||
445 | public static function build_indices() { |
||||||
0 ignored issues
–
show
|
|||||||
446 | global $wpdb; |
||||||
447 | |||||||
448 | $indices_sql = 'SHOW INDEX FROM `' . static::table_name() . '`'; |
||||||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||||||
449 | $indices = $wpdb->get_results( $indices_sql ); |
||||||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||||||
450 | $index_columns = array_map( function($index){ return $index->Column_name; }, $indices ); |
||||||
0 ignored issues
–
show
|
|||||||
451 | |||||||
452 | foreach ( static::properties() as $property ) { |
||||||
453 | |||||||
454 | if ( $property['index'] && ! in_array( $property['name'], $index_columns ) ) { |
||||||
0 ignored issues
–
show
|
|||||||
455 | $length = isset($property['index_length']) ? '(' . (int) $property['index_length'] . ')' : ''; |
||||||
0 ignored issues
–
show
|
|||||||
456 | $unique = isset($property['unique']) && $property['unique'] ? 'UNIQUE' : ''; |
||||||
0 ignored issues
–
show
|
|||||||
457 | $sql = 'ALTER TABLE `' . static::table_name() . '` ADD ' . $unique . ' INDEX `' . $property['name'] . '` (' . $property['name'] . $length . ')'; |
||||||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||||||
458 | $wpdb->query( $sql ); |
||||||
0 ignored issues
–
show
|
|||||||
459 | } |
||||||
460 | } |
||||||
461 | } |
||||||
0 ignored issues
–
show
|
|||||||
462 | |||||||
463 | /** |
||||||
464 | * Model identifier. |
||||||
465 | */ |
||||||
466 | public static function name() { |
||||||
467 | // get name of implementing class |
||||||
0 ignored issues
–
show
|
|||||||
468 | $table_name = get_called_class(); |
||||||
469 | // replace backslashes from namespace by underscores |
||||||
0 ignored issues
–
show
|
|||||||
470 | $table_name = str_replace( '\\', '_', $table_name ); |
||||||
471 | // remove Models subnamespace from name |
||||||
0 ignored issues
–
show
|
|||||||
472 | $table_name = str_replace( 'Model_', '', $table_name ); |
||||||
473 | // all lowercase |
||||||
0 ignored issues
–
show
|
|||||||
474 | $table_name = strtolower( $table_name ); |
||||||
475 | |||||||
476 | return $table_name; |
||||||
477 | } |
||||||
0 ignored issues
–
show
|
|||||||
478 | } |