| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Defines common functionality used throughout the plugin. | |
| 4 | + * | |
| 5 | + * @package WP_Stream | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | /** |
| 4 | 9 | * Gets a specific external variable by name and optionally filters it. |
| 5 | 10 | * |
| @@ -35,10 +40,10 @@ | ||
| 35 | 40 | |
| 36 | 41 | /** |
| 37 | 42 | * Converts a time into an ISO 8601 extended formatted string. |
| 38 | 43 | * |
| 39 | - * @param int|bool $time Seconds since unix epoc | |
| 40 | - * @param int $offset Hour offset | |
| 44 | + * @param int|bool $time Seconds since unix epoch. | |
| 45 | + * @param int $offset Timezone offset. | |
| 41 | 46 | * |
| 42 | 47 | * @return string an ISO 8601 extended formatted time |
| 43 | 48 | */ |
| 44 | 49 | function wp_stream_get_iso_8601_extended_date( $time = false, $offset = 0 ) { |
| @@ -48,27 +53,22 @@ | ||
| 48 | 53 | $microtime = microtime( true ); |
| 49 | 54 | } |
| 50 | 55 | |
| 51 | 56 | $micro_seconds = sprintf( '%06d', ( $microtime - floor( $microtime ) ) * 1000000 ); |
| 52 | - $offset_string = sprintf( 'Etc/GMT%s%s', $offset < 0 ? '+' : '-', abs( $offset ) ); | |
| 57 | + $offset_string = sprintf( 'Etc/GMT%s%d', $offset < 0 ? '+' : '-', abs( $offset ) ); | |
| 53 | 58 | |
| 54 | 59 | $timezone = new DateTimeZone( $offset_string ); |
| 55 | - $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 ); | |
| 56 | 61 | |
| 57 | - return sprintf( | |
| 58 | - '%s%03d%s', | |
| 59 | - $date->format( 'Y-m-d\TH:i:s.' ), | |
| 60 | - floor( $date->format( 'u' ) / 1000 ), | |
| 61 | - $date->format( 'O' ) | |
| 62 | - ); | |
| 62 | + return $date->format( 'Y-m-d\TH:i:sO' ); | |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | 66 | * Encode to JSON in a way that is also backwards compatible |
| 67 | 67 | * |
| 68 | - * @param mixed $data | |
| 69 | - * @param int $options (optional) | |
| 70 | - * @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). | |
| 71 | 71 | * |
| 72 | 72 | * @return string |
| 73 | 73 | */ |
| 74 | 74 | function wp_stream_json_encode( $data, $options = 0, $depth = 512 ) { |
| @@ -87,8 +87,35 @@ | ||
| 87 | 87 | return $json; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | /** |
| 91 | + * Return an array of sites for a network in a way that is also backwards compatible | |
| 92 | + * | |
| 93 | + * @param string|array $args Argument to filter results by. | |
| 94 | + * | |
| 95 | + * @return array | |
| 96 | + */ | |
| 97 | +function wp_stream_get_sites( $args = array() ) { | |
| 98 | + if ( empty( $args['limit'] ) ) { | |
| 99 | + $args['limit'] = 0; | |
| 100 | + } | |
| 101 | + | |
| 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 | + | |
| 106 | + $sites = get_sites( $args ); | |
| 107 | + } else { | |
| 108 | + $sites = array(); | |
| 109 | + foreach ( wp_get_sites( $args ) as $site ) { // @codingStandardsIgnoreLine Specifically for old version of WP first, in order to provide backward compatibility | |
| 110 | + $sites[] = WP_Site::get_instance( $site['blog_id'] ); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + return $sites; | |
| 115 | +} | |
| 116 | + | |
| 117 | +/** | |
| 91 | 118 | * Check if Stream is running on WordPress.com VIP |
| 92 | 119 | * |
| 93 | 120 | * @return bool |
| 94 | 121 | */ |
| @@ -96,11 +123,28 @@ | ||
| 96 | 123 | return function_exists( 'wpcom_vip_load_plugin' ); |
| 97 | 124 | } |
| 98 | 125 | |
| 99 | 126 | /** |
| 100 | - * 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. | |
| 101 | 129 | * |
| 102 | 130 | * @return bool |
| 103 | 131 | */ |
| 104 | 132 | function wp_stream_is_cron_enabled() { |
| 105 | 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; | |
| 106 | 150 | } |