class-clauses.php
2 years ago
class-database.php
2 years ago
class-escape.php
2 years ago
class-groupby.php
2 years ago
class-joins.php
2 years ago
class-orderby.php
2 years ago
class-query-builder.php
2 years ago
class-select.php
2 years ago
class-translate.php
2 years ago
class-where.php
2 years ago
index.php
2 years ago
class-escape.php
66 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The escape functions. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package MyThemeShop |
| 7 | * @subpackage MyThemeShop\Database |
| 8 | * @author MyThemeShop <admin@mythemeshop.com> |
| 9 | */ |
| 10 | |
| 11 | namespace MyThemeShop\Database; |
| 12 | |
| 13 | /** |
| 14 | * Escape class. |
| 15 | */ |
| 16 | trait Escape { |
| 17 | |
| 18 | /** |
| 19 | * Escape array values for sql |
| 20 | * |
| 21 | * @param array $arr Array to escape. |
| 22 | * |
| 23 | * @return array |
| 24 | */ |
| 25 | public function esc_array( $arr ) { |
| 26 | return array_map( array( $this, 'esc_value' ), $arr ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Escape value for sql |
| 31 | * |
| 32 | * @param mixed $value Value to escape. |
| 33 | * |
| 34 | * @return mixed |
| 35 | */ |
| 36 | public function esc_value( $value ) { |
| 37 | global $wpdb; |
| 38 | |
| 39 | if ( is_int( $value ) ) { |
| 40 | return $wpdb->prepare( '%d', $value ); |
| 41 | } |
| 42 | |
| 43 | if ( is_float( $value ) ) { |
| 44 | return $wpdb->prepare( '%f', $value ); |
| 45 | } |
| 46 | |
| 47 | return 'NULL' === $value ? $value : $wpdb->prepare( '%s', $value ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Escape value for like statement |
| 52 | * |
| 53 | * @codeCoverageIgnore |
| 54 | * |
| 55 | * @param string $value Value for like statement. |
| 56 | * @param string $start (Optional) The start of like query. |
| 57 | * @param string $end (Optional) The end of like query. |
| 58 | * |
| 59 | * @return string |
| 60 | */ |
| 61 | public function esc_like( $value, $start = '%', $end = '%' ) { |
| 62 | global $wpdb; |
| 63 | return $start . $wpdb->esc_like( $value ) . $end; |
| 64 | } |
| 65 | } |
| 66 |