| @@ -1,6 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | + * Defines common functionality used throughout the plugin. | |
| 4 | + * | |
| 5 | + * @package WP_Stream | |
| 6 | + */ | |
| 7 | + | |
| 8 | +/** | |
| 3 | 9 | * Gets a specific external variable by name and optionally filters it. |
| 4 | 10 | * |
| 5 | 11 | * This is a polyfill function intended to be used in place of PHP's |
| 6 | 12 | * filter_input() function, which can occasionally be unreliable. |
| @@ -34,10 +40,10 @@ | ||
| 34 | 40 | |
| 35 | 41 | /** |
| 36 | 42 | * Converts a time into an ISO 8601 extended formatted string. |
| 37 | 43 | * |
| 38 | - * @param int|bool $time Seconds since unix epoc | |
| 39 | - * @param int $offset Hour offset | |
| 44 | + * @param int|bool $time Seconds since unix epoch. | |
| 45 | + * @param int $offset Timezone offset. | |
| 40 | 46 | * |
| 41 | 47 | * @return string an ISO 8601 extended formatted time |
| 42 | 48 | */ |
| 43 | 49 | function wp_stream_get_iso_8601_extended_date( $time = false, $offset = 0 ) { |
| @@ -47,27 +53,22 @@ | ||
| 47 | 53 | $microtime = microtime( true ); |
| 48 | 54 | } |
| 49 | 55 | |
| 50 | 56 | $micro_seconds = sprintf( '%06d', ( $microtime - floor( $microtime ) ) * 1000000 ); |
| 51 | - $offset_string = sprintf( 'Etc/GMT%s%s', $offset < 0 ? '+' : '-', abs( $offset ) ); | |
| 57 | + $offset_string = sprintf( 'Etc/GMT%s%d', $offset < 0 ? '+' : '-', abs( $offset ) ); | |
| 52 | 58 | |
| 53 | 59 | $timezone = new DateTimeZone( $offset_string ); |
| 54 | - $date = new DateTime( date( 'Y-m-d H:i:s.' . $micro_seconds, $microtime ), $timezone ); | |
| 60 | + $date = new DateTime( gmdate( 'Y-m-d H:i:s.' . $micro_seconds, $microtime ), $timezone ); | |
| 55 | 61 | |
| 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 | + return $date->format( 'Y-m-d\TH:i:sO' ); | |
| 62 | 63 | } |
| 63 | 64 | |
| 64 | 65 | /** |
| 65 | 66 | * Encode to JSON in a way that is also backwards compatible |
| 66 | 67 | * |
| 67 | - * @param mixed $data | |
| 68 | - * @param int $options (optional) | |
| 69 | - * @param int $depth (optional) | |
| 68 | + * @param mixed $data Data to be encoded. | |
| 69 | + * @param int $options Compression options (optional). | |
| 70 | + * @param int $depth Tree depth limit (optional). | |
| 70 | 71 | * |
| 71 | 72 | * @return string |
| 72 | 73 | */ |
| 73 | 74 | function wp_stream_json_encode( $data, $options = 0, $depth = 512 ) { |
| @@ -88,18 +89,25 @@ | ||
| 88 | 89 | |
| 89 | 90 | /** |
| 90 | 91 | * Return an array of sites for a network in a way that is also backwards compatible |
| 91 | 92 | * |
| 92 | - * @param string|array $args | |
| 93 | + * @param string|array $args Argument to filter results by. | |
| 93 | 94 | * |
| 94 | 95 | * @return array |
| 95 | 96 | */ |
| 96 | 97 | function wp_stream_get_sites( $args = array() ) { |
| 98 | + if ( empty( $args['limit'] ) ) { | |
| 99 | + $args['limit'] = 0; | |
| 100 | + } | |
| 101 | + | |
| 97 | 102 | if ( function_exists( 'get_sites' ) ) { |
| 103 | + // Account for get_sites() which uses 'number' while wp_get_sites() uses 'limit'. | |
| 104 | + $args['number'] = $args['limit']; | |
| 105 | + | |
| 98 | 106 | $sites = get_sites( $args ); |
| 99 | 107 | } else { |
| 100 | 108 | $sites = array(); |
| 101 | - foreach ( wp_get_sites( $args ) as $site ) { | |
| 109 | + foreach ( wp_get_sites( $args ) as $site ) { // @codingStandardsIgnoreLine Specifically for old version of WP first, in order to provide backward compatibility | |
| 102 | 110 | $sites[] = WP_Site::get_instance( $site['blog_id'] ); |
| 103 | 111 | } |
| 104 | 112 | } |
| 105 | 113 | |
| @@ -115,11 +123,28 @@ | ||
| 115 | 123 | return function_exists( 'wpcom_vip_load_plugin' ); |
| 116 | 124 | } |
| 117 | 125 | |
| 118 | 126 | /** |
| 119 | - * True if native WP Cron is enabled, otherwise false | |
| 127 | + * Check if the default front-end WP Cron is enabled. It doesn't | |
| 128 | + * mean that the Cron is disabled in general. | |
| 120 | 129 | * |
| 121 | 130 | * @return bool |
| 122 | 131 | */ |
| 123 | 132 | function wp_stream_is_cron_enabled() { |
| 124 | 133 | return ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ? false : true; |
| 134 | +} | |
| 135 | + | |
| 136 | +/** | |
| 137 | + * Get the asset min suffix if defined. | |
| 138 | + * | |
| 139 | + * @return string | |
| 140 | + */ | |
| 141 | +function wp_stream_min_suffix() { | |
| 142 | + $min = ''; | |
| 143 | + $is_script_debugging = ! defined( 'SCRIPT_DEBUG' ) || false === SCRIPT_DEBUG; | |
| 144 | + | |
| 145 | + if ( apply_filters( 'wp_stream_load_min_assets', $is_script_debugging ) ) { | |
| 146 | + $min = 'min.'; | |
| 147 | + } | |
| 148 | + | |
| 149 | + return $min; | |
| 125 | 150 | } |