| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
defined( 'ABSPATH' ) || exit; |
| 7 |
|
| 8 |
/** |
| 9 |
* Small set of tools to work with the database. |
| 10 |
* |
| 11 |
* @since 3.2 |
| 12 |
*/ |
| 13 |
class PLL_Db_Tools { |
| 14 |
|
| 15 |
/** |
| 16 |
* Changes an array of values into a comma separated list, ready to be used in a `IN ()` clause. |
| 17 |
* Only string and integers and supported for now. |
| 18 |
* |
| 19 |
* @since 3.2 |
| 20 |
* |
| 21 |
* @param (int|string)[] $values An array of values. |
| 22 |
* @return string A comma separated list of values. |
| 23 |
*/ |
| 24 |
public static function prepare_values_list( $values ) { |
| 25 |
$values = array_map( array( __CLASS__, 'prepare_value' ), (array) $values ); |
| 26 |
|
| 27 |
return implode( ',', $values ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Wraps a value in escaped double quotes or casts as an integer. |
| 32 |
* Only string and integers and supported for now. |
| 33 |
* |
| 34 |
* @since 3.2 |
| 35 |
* |
| 36 |
* @global wpdb $wpdb |
| 37 |
* |
| 38 |
* @param int|string $value A value. |
| 39 |
* @return int|string |
| 40 |
*/ |
| 41 |
public static function prepare_value( $value ) { |
| 42 |
if ( ! is_numeric( $value ) ) { |
| 43 |
return $GLOBALS['wpdb']->prepare( '%s', $value ); |
| 44 |
} |
| 45 |
|
| 46 |
return (int) $value; |
| 47 |
} |
| 48 |
} |
| 49 |
|