PluginProbe
WP Maps – Google Maps,OpenStreetMap,Mapbox,Store Locator,Listing,Directory & Filters / trunk
WP Maps – Google Maps,OpenStreetMap,Mapbox,Store Locator,Listing,Directory & Filters vtrunk
4.9.9 4.9.8 4.9.7 4.9.6 4.9.5 4.8.7 4.8.8 4.8.9 4.9.0 4.9.1 4.9.2 4.9.3 version4.4.8 trunk 3.0.8 3.1.1 4.0.6 4.2.2 4.4.5 4.4.6 4.4.7 4.4.9 4.5.0 4.5.1 4.5.2 All 54 releases
wp-google-map-plugin / core / class.model.php

class.model.php in WP Maps – Google Maps,OpenStreetMap,Mapbox,Store Locator,Listing,Directory & Filters trunk, at core/class.model.php

439 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 /**
6 * Model base class
7 *
8 * @author Flipper Code <hello@flippercode.com>
9 * @version 3.0.0
10 * @package Core
11 */
12
13 if ( ! class_exists( 'FlipperCode_Model_Base' ) ) {
14
15 /**
16 * Model base class
17 *
18 * @author Flipper Code <hello@flippercode.com>
19 * @version 3.0.0
20 * @package Core
21 */
22 class FlipperCode_Model_Base {
23
24 /**
25 * Errors container.
26 *
27 * @var array
28 */
29 protected $errors = array();
30 /**
31 * Success message container.
32 *
33 * @var array
34 */
35 protected $success = array();
36 /**
37 * Hold query to be executed.
38 *
39 * @var string
40 */
41 private $query = '';
42 /**
43 * Table columns assoicated to the model class.
44 *
45 * @var string
46 */
47 public $allowed_columns = [];
48 /**
49 * Table name assoicated to the model class.
50 *
51 * @var string
52 */
53 public $table = '';
54 /**
55 * Unique field name of the model table.
56 *
57 * @var [type]
58 */
59 public $unique;
60 /**
61 * Navigations releated to the model.
62 *
63 * @var array
64 */
65 public $navigation = array( '' );
66 /**
67 * Model class constructer.
68 */
69 private function __construct() {
70
71 }
72 /**
73 * Assign value to property.
74 *
75 * @param string $property Property Name.
76 * @param string $value Property Value.
77 */
78 function set_val( $property, $value ) {
79
80 if ( is_array( $value ) ) {
81 $this->{$property} = $value; } elseif ( $this->valid( $property, $value ) ) {
82 $this->{$property} = $value;
83 }
84 }
85 /**
86 * Validate property value before assign.
87 *
88 * @param string $property Property Name.
89 * @param string $value Property Value.
90 * @return boolean True or False.
91 */
92 function valid( $property, $value ) {
93
94 if ( property_exists( $this, $property ) ) {
95
96 $validator = new FlipperCode_Validator();
97
98 if ( isset( $this->validations[ $property ] ) ) {
99
100 foreach ( $this->validations[ $property ] as $type => $message ) {
101
102 $validator->add( $property, $value, $type, $message );
103 }
104
105 $errors = $validator->validate();
106
107 if ( $errors ) {
108 $this->errors[ $property ] = $errors[ $property ];
109 return false;
110 } else {
111 return true;
112 }
113 } else {
114 return true;
115 }
116 }
117 }
118 /**
119 * Validate all property together.
120 *
121 * @param array $data Property name and value pair.
122 * @return boolean True or False.
123 */
124 function verify( $data = array() ) {
125
126 $errors = '';
127 // Call extension validation.
128 if ( isset( $data['fc_entity_type'] ) and $data['fc_entity_type'] != '' ) {
129 $this->validations = apply_filters( $data['fc_entity_type'] . '_validation', $this->validations, $data );
130 }
131
132 if ( isset( $this->validations ) ) {
133
134 foreach ( $this->validations as $field => $checkup ) {
135 $validator = new FlipperCode_Validator();
136 $dimension = explode( '::', $field );
137 foreach ( $checkup as $property => $message ) {
138 if ( count( $dimension ) == 1 ) {
139 if ( isset( $data[ $dimension[0] ] ) ) {
140 $validator->add( $field, $data[ $dimension[0] ], $property, $message );
141 }
142 } elseif ( count( $dimension ) == 2 ) {
143 if ( isset( $data[ $dimension[0] ][ $dimension[1] ] ) ) {
144 $validator->add( $field, $data[ $dimension[0] ][ $dimension[1] ], $property, $message );
145 }
146 }
147 }
148 $errors = $validator->validate();
149 if ( $errors ) {
150 $this->errors[ $field ] = $errors[ $field ];
151 }
152 }
153 }
154
155 if ( isset( $data['fc_entity_type'] ) and $data['fc_entity_type'] != '' ) {
156 $this->errors = apply_filters( $data['fc_entity_type'] . '_custom_validation', $this->errors, $data );
157 }
158
159 if ( is_array( $this->errors ) and ! empty( $this->errors ) ) {
160 return false;
161 }
162 return true;
163
164 }
165
166 /**
167 * Load and cache the list of valid database columns for the current table.
168 *
169 * This method retrieves the
170 */
171
172 protected function load_columns() {
173
174 if ( empty( $this->table ) ) {
175 return;
176 }
177
178 global $wpdb;
179
180 $columns = $wpdb->get_results( "DESCRIBE `{$this->table}`", ARRAY_A );
181
182 if ( ! empty( $columns ) ) {
183
184 $this->allowed_columns = array_column( $columns, 'Field' );
185
186 }
187 }
188 /**
189 * Retrive records from database based on conditional array.
190 *
191 * @param string $table Table name.
192 * @param array $fcv_array Conditional Array.
193 * @param string $sortBy Sort by.
194 * @param boolean $ascending Order by.
195 * @param string $limit Limit.
196 */
197 function get( $table = '', $fcv_array = array(), $sortBy = '', $ascending = true, $limit = '' ) {
198
199 $connection = FlipperCode_Database::connect();
200
201 $sqlLimit = ( '' != $limit ? "LIMIT $limit" : '' );
202 $this->query = "SELECT * FROM $this->table ";
203 $ruleList = array();
204 $objects = array();
205
206 if ( count( $fcv_array ) > 0 ) {
207
208 $this->query .= ' WHERE ';
209
210 for ( $i = 0, $c = count( $fcv_array ); $i < $c; $i++ ) {
211
212 if ( count( $fcv_array[ $i ] ) == 1 ) {
213 $this->query .= ' ' . $fcv_array[ $i ][0] . ' ';
214 continue;
215 } else {
216 if ( $i > 0 && count( $fcv_array[ $i - 1 ] ) != 1 ) {
217 $this->query .= ' AND ';
218 }
219 if ( isset( $this->pog_attribute_type[ $fcv_array[ $i ][0] ]['db_attributes'] ) && 'NUMERIC' != $this->pog_attribute_type[ $fcv_array[ $i ][0] ]['db_attributes'][0] && 'SET' != $this->pog_attribute_type[ $fcv_array[ $i ][0] ]['db_attributes'][0] ) {
220 if ( 1 == $GLOBALS['configuration']['db_encoding'] ) {
221
222 $value = $this->is_column( $fcv_array[ $i ][2] ) ? 'BASE64_DECODE(`' . trim( $fcv_array[ $i ][2], '`' ) . '`)' : "'" . $this->escape( $fcv_array[ $i ][2] ) . "'";
223 $this->query .= 'BASE64_DECODE(`' . $fcv_array[ $i ][0] . '`) ' . $fcv_array[ $i ][1] . ' ' . $value;
224 } else {
225
226 $value = $this->is_column( $fcv_array[ $i ][2] ) ? '`' . trim( $fcv_array[ $i ][2], '`' ) . '`' : "'" . $this->escape( $fcv_array[ $i ][2] ) . "'";
227 $this->query .= '`' . $fcv_array[ $i ][0] . '` ' . $fcv_array[ $i ][1] . ' ' . $value;
228 }
229 } else {
230
231 $value = $this->is_column( $fcv_array[ $i ][2] ) ? '`' . trim( $fcv_array[ $i ][2], '`' ) . '`' : "'" . $this->escape( $fcv_array[ $i ][2] ) . "'";
232 if ( 'in' == strtolower( $fcv_array[ $i ][1] ) ) {
233 $value = str_replace( "'", '', $value );
234 $value = '(' . $value . ')';
235 }
236 $this->query .= '`' . $fcv_array[ $i ][0] . '` ' . $fcv_array[ $i ][1] . ' ' . $value;
237 }
238 }
239 }
240 }
241
242 if ( ! empty( $sortBy ) ) {
243 if ( isset( $this->pog_attribute_type[ $sortBy ]['db_attributes'] ) && 'NUMERIC' != $this->pog_attribute_type[ $sortBy ]['db_attributes'][0] && 'SET' != $this->pog_attribute_type[ $sortBy ]['db_attributes'][0] ) {
244 if ( 1 == $GLOBALS['configuration']['db_encoding'] ) {
245 $sortBy = "BASE64_DECODE($sortBy) ";
246 } else {
247 $sortBy = "$sortBy ";
248 }
249 } else {
250 $sortBy = "$sortBy ";
251 }
252 } else {
253 $sortBy = $this->unique;
254 }
255
256 $this->query .= ' ORDER BY ' . $sortBy . ' ' . ( $ascending ? 'ASC' : 'DESC' ) . " $sqlLimit";
257
258 $thisObjectName = get_class( $this );
259 $cursors = FlipperCode_Database::reader( $this->query, $connection );
260
261 return $cursors;
262 }
263 /**
264 * Query to be executed.
265 *
266 * @param string $query SQL Query.
267 * @return array Records.
268 */
269 function query( $query ) {
270
271 $this->query = $query;
272 $connection = FlipperCode_Database::connect();
273 $thisObjectName = get_class( $this );
274 $cursors = FlipperCode_Database::reader( $this->query, $connection );
275
276 if ( ! empty( $cursors ) ) {
277
278 foreach ( $cursors as $row ) {
279
280 $obj = new $thisObjectName();
281 $obj->fill( $row );
282 $objects[] = $obj;
283 }
284
285 return $objects;
286 }
287 }
288 /**
289 * Validate file extension.
290 *
291 * @param string $file_name File Name.
292 * @return boolean True or False.
293 */
294 public function wpp_validate_extension( $file_name ) {
295
296 $ext_array = array( '.csv', '.xml', '.json', '.xls' );
297 $extension = strtolower( strrchr( $file_name, '.' ) );
298 $ext_count = count( $ext_array );
299
300 if ( ! $file_name ) {
301 return false;
302 } else {
303 if ( ! $ext_array ) {
304 return true;
305 } else {
306 foreach ( $ext_array as $value ) {
307 $first_char = substr( $value, 0, 1 );
308 if ( '.' <> $first_char ) {
309 $extensions[] = '.' . strtolower( $value );
310 } else {
311 $extensions[] = strtolower( $value );
312 }
313 }
314
315 foreach ( $extensions as $value ) {
316 if ( $value == $extension ) {
317 $valid_extension = 'TRUE';
318 }
319 }
320
321 if ( $valid_extension ) {
322 return true;
323 } else {
324 return false;
325 }
326 }
327 }
328 }
329 /**
330 * Throw errors in try block.
331 *
332 * @throws Exception User custom Errors.
333 */
334 protected function throw_errors() {
335
336 if ( isset( $this->errors ) and is_array( $this->errors ) ) {
337
338 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Internal error messages for display
339 throw new Exception( implode( '<br>', $this->errors ) );
340
341 }
342 }
343
344 /**
345 * This function will try to encode $text to base64, except when $text is a number. This allows us to Escape all data before they're inserted in the database, regardless of attribute type.
346 *
347 * @param string $text String.
348 * @return string encoded to base64.
349 */
350 public function escape( $text ) {
351
352 return esc_sql( $text );
353 }
354 /**
355 * Check if column name.
356 *
357 * @param string $value Column name.
358 * @return boolean True or False.
359 */
360 public function is_column( $value ) {
361 if ( ! is_string( $value ) || empty( $this->allowed_columns ) ) {
362 return false;
363 }
364
365 $clean = trim( $value, '`' );
366
367 if ( ! preg_match( '/^[a-zA-Z0-9_]+$/', $clean ) ) {
368 return false;
369 }
370
371 return in_array( $clean, $this->allowed_columns, true );
372 }
373 /**
374 * Convert XML to array.
375 *
376 * @param string $xml XML nodes.
377 * @return array Array nodes.
378 */
379 public function wpp_xml_2array( $xml ) {
380
381 $arr = array();
382
383 foreach ( $xml->children() as $r ) {
384 $t = array();
385 if ( count( $r->children() ) == 0 ) {
386 $arr[ $r->getName() ] = strval( $r );
387 } else {
388 $arr[ $r->getName() ][] = $this->wpp_xml_2array( $r );
389 }
390 }
391
392 return $arr;
393 }
394
395 /**
396 * Validate file extension.
397 *
398 * @param string $file_name File Name.
399 * @return boolean True or False.
400 */
401 public function validate_extension( $file_name ) {
402
403 $ext_array = array( '.csv' );
404 $extension = strtolower( strrchr( $file_name, '.' ) );
405 $ext_count = count( $ext_array );
406 $valid_extension = false;
407 if ( ! $file_name ) {
408 return false;
409 } else {
410 if ( ! $ext_array ) {
411 return true;
412 } else {
413 foreach ( $ext_array as $value ) {
414 $first_char = substr( $value, 0, 1 );
415 if ( '.' <> $first_char ) {
416 $extensions[] = '.' . strtolower( $value );
417 } else {
418 $extensions[] = strtolower( $value );
419 }
420 }
421
422 foreach ( $extensions as $value ) {
423 if ( $value == $extension ) {
424 $valid_extension = true;
425 }
426 }
427
428 if ( $valid_extension ) {
429 return true;
430 } else {
431 return false;
432 }
433 }
434 }
435 }
436
437 }
438 }
439