| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gets a specific external variable by name and optionally filters it. |
| 4 |
* |
| 5 |
* This is a polyfill function intended to be used in place of PHP's |
| 6 |
* filter_input() function, which can occasionally be unreliable. |
| 7 |
* |
| 8 |
* @param int $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. |
| 9 |
* @param string $variable_name Name of a variable to get. |
| 10 |
* @param int $filter The ID of the filter to apply. |
| 11 |
* @param mixed $options Associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in "flags" field of array. |
| 12 |
* |
| 13 |
* @return Value of the requested variable on success, FALSE if the filter fails, or NULL if the $variable_name is not set. |
| 14 |
*/ |
| 15 |
function wp_stream_filter_input( $type, $variable_name, $filter = null, $options = array() ) { |
| 16 |
return call_user_func_array( array( '\WP_Stream\Filter_Input', 'super' ), func_get_args() ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Filters a variable with a specified filter. |
| 21 |
* |
| 22 |
* This is a polyfill function intended to be used in place of PHP's |
| 23 |
* filter_var() function, which can occasionally be unreliable. |
| 24 |
* |
| 25 |
* @param string $var Value to filter. |
| 26 |
* @param int $filter The ID of the filter to apply. |
| 27 |
* @param mixed $options Associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in "flags" field of array. For the "callback" filter, callable type should be passed. The callback must accept one argument, the value to be filtered, and return the value after filtering/sanitizing it. |
| 28 |
* |
| 29 |
* @return Returns the filtered data, or FALSE if the filter fails. |
| 30 |
*/ |
| 31 |
function wp_stream_filter_var( $var, $filter = null, $options = array() ) { |
| 32 |
return call_user_func_array( array( '\WP_Stream\Filter_Input', 'filter' ), func_get_args() ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Converts a time into an ISO 8601 extended formatted string. |
| 37 |
* |
| 38 |
* @param int|bool $time Seconds since unix epoc |
| 39 |
* @param int $offset Hour offset |
| 40 |
* |
| 41 |
* @return string an ISO 8601 extended formatted time |
| 42 |
*/ |
| 43 |
function wp_stream_get_iso_8601_extended_date( $time = false, $offset = 0 ) { |
| 44 |
if ( $time ) { |
| 45 |
$microtime = (float) $time . '.0000'; |
| 46 |
} else { |
| 47 |
$microtime = microtime( true ); |
| 48 |
} |
| 49 |
|
| 50 |
$micro_seconds = sprintf( '%06d', ( $microtime - floor( $microtime ) ) * 1000000 ); |
| 51 |
$offset_string = sprintf( 'Etc/GMT%s%s', $offset < 0 ? '+' : '-', abs( $offset ) ); |
| 52 |
|
| 53 |
$timezone = new DateTimeZone( $offset_string ); |
| 54 |
$date = new DateTime( date( 'Y-m-d H:i:s.' . $micro_seconds, $microtime ), $timezone ); |
| 55 |
|
| 56 |
return sprintf( |
| 57 |
'%s%03d%s', |
| 58 |
$date->format( 'Y-m-d\TH:i:s.' ), |
| 59 |
floor( $date->format( 'u' ) / 1000 ), |
| 60 |
$date->format( 'O' ) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Encode to JSON in a way that is also backwards compatible |
| 66 |
* |
| 67 |
* @param mixed $data |
| 68 |
* @param int $options (optional) |
| 69 |
* @param int $depth (optional) |
| 70 |
* |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
function wp_stream_json_encode( $data, $options = 0, $depth = 512 ) { |
| 74 |
if ( function_exists( 'wp_json_encode' ) ) { |
| 75 |
$json = wp_json_encode( $data, $options, $depth ); |
| 76 |
} else { |
| 77 |
// @codingStandardsIgnoreStart |
| 78 |
if ( version_compare( PHP_VERSION, '5.5', '<' ) ) { |
| 79 |
$json = json_encode( $data, $options ); |
| 80 |
} else { |
| 81 |
$json = json_encode( $data, $options, $depth ); |
| 82 |
} |
| 83 |
// @codingStandardsIgnoreEnd |
| 84 |
} |
| 85 |
|
| 86 |
return $json; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Return an array of sites for a network in a way that is also backwards compatible |
| 91 |
* |
| 92 |
* @return array |
| 93 |
*/ |
| 94 |
function wp_stream_get_sites() { |
| 95 |
if ( function_exists( 'get_sites' ) ) { |
| 96 |
$sites = get_sites(); |
| 97 |
} else { |
| 98 |
$sites = array(); |
| 99 |
foreach ( wp_get_sites() as $site ) { |
| 100 |
$sites[] = WP_Site::get_instance( $site['blog_id'] ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return $sites; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Check if Stream is running on WordPress.com VIP |
| 109 |
* |
| 110 |
* @return bool |
| 111 |
*/ |
| 112 |
function wp_stream_is_vip() { |
| 113 |
return function_exists( 'wpcom_vip_load_plugin' ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* True if native WP Cron is enabled, otherwise false |
| 118 |
* |
| 119 |
* @return bool |
| 120 |
*/ |
| 121 |
function wp_stream_is_cron_enabled() { |
| 122 |
return ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ? false : true; |
| 123 |
} |
| 124 |
|