| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Gets a specific external variable by name and optionally filters it. |
| 5 |
* |
| 6 |
* This is a polyfill function intended to be used in place of PHP's |
| 7 |
* filter_input() function, which can occasionally be unreliable. |
| 8 |
* |
| 9 |
* @since 1.2.5 |
| 10 |
* |
| 11 |
* @param int $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. |
| 12 |
* @param string $variable_name Name of a variable to get. |
| 13 |
* @param int $filter The ID of the filter to apply. |
| 14 |
* @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. |
| 15 |
* |
| 16 |
* @return Value of the requested variable on success, FALSE if the filter fails, or NULL if the $variable_name is not set. |
| 17 |
*/ |
| 18 |
function wp_stream_filter_input( $type, $variable_name, $filter = null, $options = array() ) { |
| 19 |
return call_user_func_array( array( 'WP_Stream_Filter_Input', 'super' ), func_get_args() ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Filters a variable with a specified filter. |
| 24 |
* |
| 25 |
* This is a polyfill function intended to be used in place of PHP's |
| 26 |
* filter_var() function, which can occasionally be unreliable. |
| 27 |
* |
| 28 |
* @since 1.2.5 |
| 29 |
* |
| 30 |
* @param string $var Value to filter. |
| 31 |
* @param int $filter The ID of the filter to apply. |
| 32 |
* @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. |
| 33 |
* |
| 34 |
* @return Returns the filtered data, or FALSE if the filter fails. |
| 35 |
*/ |
| 36 |
function wp_stream_filter_var( $var, $filter = null, $options = array() ) { |
| 37 |
return call_user_func_array( array( 'WP_Stream_Filter_Input', 'filter' ), func_get_args() ); |
| 38 |
} |
| 39 |
|
| 40 |
function wp_stream_query( $args = array() ) { |
| 41 |
return WP_Stream_Query::instance()->query( $args ); |
| 42 |
} |
| 43 |
|
| 44 |
function wp_stream_get_meta( $record, $meta_key = '', $single = false ) { |
| 45 |
if ( isset( $record->stream_meta->$meta_key ) ) { |
| 46 |
$record_meta = $record->stream_meta->$meta_key; |
| 47 |
} else { |
| 48 |
return ''; |
| 49 |
} |
| 50 |
|
| 51 |
if ( $single ) { |
| 52 |
return $record_meta; |
| 53 |
} else { |
| 54 |
return array( $record_meta ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Converts a time into an ISO 8601 extended formatted string. |
| 60 |
* |
| 61 |
* @param int Seconds since unix epoc |
| 62 |
* @param int Hour offset |
| 63 |
* |
| 64 |
* @return string an ISO 8601 extended formatted time |
| 65 |
*/ |
| 66 |
function wp_stream_get_iso_8601_extended_date( $time = false, $offset = 0 ) { |
| 67 |
if ( $time ) { |
| 68 |
$microtime = (float) $time . '.0000'; |
| 69 |
} else { |
| 70 |
$microtime = microtime( true ); |
| 71 |
} |
| 72 |
|
| 73 |
$micro_seconds = sprintf( '%06d', ( $microtime - floor( $microtime ) ) * 1000000 ); |
| 74 |
$offset_string = sprintf( 'Etc/GMT%s%s', $offset < 0 ? '+' : '-', abs( $offset ) ); |
| 75 |
|
| 76 |
$timezone = new DateTimeZone( $offset_string ); |
| 77 |
$date = new DateTime( date( 'Y-m-d H:i:s.' . $micro_seconds, $microtime ), $timezone ); |
| 78 |
|
| 79 |
return sprintf( |
| 80 |
'%s%03d%s', |
| 81 |
$date->format( 'Y-m-d\TH:i:s.' ), |
| 82 |
floor( $date->format( 'u' ) / 1000 ), |
| 83 |
$date->format( 'O' ) |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns array of existing values for requested field. |
| 89 |
* Used to fill search filters with only used items, instead of all items. |
| 90 |
* |
| 91 |
* @see assemble_records |
| 92 |
* @since 1.0.4 |
| 93 |
* @param string Requested field (i.e., 'context') |
| 94 |
* @return array Array of items to be output to select dropdowns |
| 95 |
*/ |
| 96 |
function wp_stream_existing_records( $field ) { |
| 97 |
$values = WP_Stream::$db->get_distinct_field_values( $field ); |
| 98 |
|
| 99 |
if ( is_array( $values ) && ! empty( $values ) ) { |
| 100 |
return array_combine( $values, $values ); |
| 101 |
} else { |
| 102 |
$field = sprintf( 'stream_%s', $field ); |
| 103 |
return isset( WP_Stream_Connectors::$term_labels[ $field ] ) ? WP_Stream_Connectors::$term_labels[ $field ] : array(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Determine the title of an object that a record is for. |
| 109 |
* |
| 110 |
* @since 2.1.0 |
| 111 |
* @param object Record object |
| 112 |
* @return mixed The title of the object as a string, otherwise false |
| 113 |
*/ |
| 114 |
function wp_stream_get_object_title( $record ) { |
| 115 |
if ( ! is_object( $record ) || ! isset( $record->object_id ) || empty( $record->object_id ) ) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
$output = false; |
| 120 |
|
| 121 |
if ( isset( $record->stream_meta->post_title ) && ! empty( $record->stream_meta->post_title ) ) { |
| 122 |
$output = (string) $record->stream_meta->post_title; |
| 123 |
} elseif ( isset( $record->stream_meta->display_name ) && ! empty( $record->stream_meta->display_name ) ) { |
| 124 |
$output = (string) $record->stream_meta->display_name; |
| 125 |
} elseif ( isset( $record->stream_meta->name ) && ! empty( $record->stream_meta->name ) ) { |
| 126 |
$output = (string) $record->stream_meta->name; |
| 127 |
} |
| 128 |
|
| 129 |
return $output; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* True if native WP Cron is enabled, otherwise false |
| 134 |
* |
| 135 |
* @return bool |
| 136 |
*/ |
| 137 |
function wp_stream_is_wp_cron_enabled() { |
| 138 |
return ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ? false : true; |
| 139 |
} |
| 140 |
|