| @@ -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 | * |
| @@ -5,19 +10,17 @@ | ||
| 5 | 10 | * |
| 6 | 11 | * This is a polyfill function intended to be used in place of PHP's |
| 7 | 12 | * filter_input() function, which can occasionally be unreliable. |
| 8 | 13 | * |
| 9 | - * @since 1.2.5 | |
| 10 | - * | |
| 11 | 14 | * @param int $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. |
| 12 | 15 | * @param string $variable_name Name of a variable to get. |
| 13 | 16 | * @param int $filter The ID of the filter to apply. |
| 14 | 17 | * @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 | 18 | * |
| 16 | - * @return Value of the requested variable on success, FALSE if the filter fails, or NULL if the $variable_name is not set. | |
| 19 | + * @return mixed|false|null Value of the requested variable on success, FALSE if the filter fails, or NULL if the $variable_name is not set. | |
| 17 | 20 | */ |
| 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() ); | |
| 21 | +function wp_stream_filter_input( $type, $variable_name, $filter = null, $options = array() ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed | |
| 22 | + return call_user_func_array( array( '\WP_Stream\Filter_Input', 'super' ), func_get_args() ); | |
| 20 | 23 | } |
| 21 | 24 | |
| 22 | 25 | /** |
| 23 | 26 | * Filters a variable with a specified filter. |
| @@ -24,47 +27,27 @@ | ||
| 24 | 27 | * |
| 25 | 28 | * This is a polyfill function intended to be used in place of PHP's |
| 26 | 29 | * filter_var() function, which can occasionally be unreliable. |
| 27 | 30 | * |
| 28 | - * @since 1.2.5 | |
| 31 | + * @param string $value Value to filter. | |
| 32 | + * @param int $filter The ID of the filter to apply. | |
| 33 | + * @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. | |
| 29 | 34 | * |
| 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 | 35 | * @return Returns the filtered data, or FALSE if the filter fails. |
| 35 | 36 | */ |
| 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() ); | |
| 37 | +function wp_stream_filter_var( $value, $filter = null, $options = array() ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed | |
| 38 | + return call_user_func_array( array( '\WP_Stream\Filter_Input', 'filter' ), func_get_args() ); | |
| 38 | 39 | } |
| 39 | 40 | |
| 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 | 41 | /** |
| 59 | 42 | * Converts a time into an ISO 8601 extended formatted string. |
| 60 | 43 | * |
| 61 | - * @param int Seconds since unix epoc | |
| 62 | - * @param int Hour offset | |
| 44 | + * @param int|bool $time Seconds since unix epoch. | |
| 45 | + * @param int $offset Timezone offset. | |
| 63 | 46 | * |
| 64 | 47 | * @return string an ISO 8601 extended formatted time |
| 65 | 48 | */ |
| 66 | -function wp_stream_get_iso_8601_extended_date( $time = false, $offset = 0 ) { | |
| 49 | +function wp_stream_get_iso_8601_extended_date( $time = false, $offset = 0 ) { | |
| 67 | 50 | if ( $time ) { |
| 68 | 51 | $microtime = (float) $time . '.0000'; |
| 69 | 52 | } else { |
| 70 | 53 | $microtime = microtime( true ); |
| @@ -70,61 +53,73 @@ | ||
| 70 | 53 | $microtime = microtime( true ); |
| 71 | 54 | } |
| 72 | 55 | |
| 73 | 56 | $micro_seconds = sprintf( '%06d', ( $microtime - floor( $microtime ) ) * 1000000 ); |
| 74 | - $offset_string = sprintf( 'Etc/GMT%s%s', $offset < 0 ? '+' : '-', abs( $offset ) ); | |
| 57 | + $offset_string = sprintf( 'Etc/GMT%s%d', $offset < 0 ? '+' : '-', abs( $offset ) ); | |
| 75 | 58 | |
| 76 | 59 | $timezone = new DateTimeZone( $offset_string ); |
| 77 | - $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 ); | |
| 78 | 61 | |
| 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 | - ); | |
| 62 | + return $date->format( 'Y-m-d\TH:i:sO' ); | |
| 85 | 63 | } |
| 86 | 64 | |
| 87 | 65 | /** |
| 88 | - * Returns array of existing values for requested field. | |
| 89 | - * Used to fill search filters with only used items, instead of all items. | |
| 66 | + * Return an array of sites for a network in a way that is also backwards compatible | |
| 90 | 67 | * |
| 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 | |
| 68 | + * @param string|array $args Argument to filter results by. | |
| 69 | + * | |
| 70 | + * @return array | |
| 95 | 71 | */ |
| 96 | -function wp_stream_existing_records( $field ) { | |
| 97 | - $values = WP_Stream::$db->get_distinct_field_values( $field ); | |
| 72 | +function wp_stream_get_sites( $args = array() ) { | |
| 73 | + if ( empty( $args['limit'] ) ) { | |
| 74 | + $args['limit'] = 0; | |
| 75 | + } | |
| 98 | 76 | |
| 99 | - if ( is_array( $values ) && ! empty( $values ) ) { | |
| 100 | - return array_combine( $values, $values ); | |
| 77 | + if ( function_exists( 'get_sites' ) ) { | |
| 78 | + // Account for get_sites() which uses 'number' while wp_get_sites() uses 'limit'. | |
| 79 | + $args['number'] = $args['limit']; | |
| 80 | + | |
| 81 | + $sites = get_sites( $args ); | |
| 101 | 82 | } else { |
| 102 | - $field = sprintf( 'stream_%s', $field ); | |
| 103 | - return isset( WP_Stream_Connectors::$term_labels[ $field ] ) ? WP_Stream_Connectors::$term_labels[ $field ] : array(); | |
| 83 | + $sites = array(); | |
| 84 | + foreach ( wp_get_sites( $args ) as $site ) { // @codingStandardsIgnoreLine Specifically for old version of WP first, in order to provide backward compatibility | |
| 85 | + $sites[] = WP_Site::get_instance( $site['blog_id'] ); | |
| 86 | + } | |
| 104 | 87 | } |
| 88 | + | |
| 89 | + return $sites; | |
| 105 | 90 | } |
| 106 | 91 | |
| 107 | 92 | /** |
| 108 | - * Determine the title of an object that a record is for. | |
| 93 | + * Check if Stream is running on WordPress.com VIP | |
| 109 | 94 | * |
| 110 | - * @since 2.1.0 | |
| 111 | - * @param object Record object | |
| 112 | - * @return mixed The title of the object as a string, otherwise false | |
| 95 | + * @return bool | |
| 113 | 96 | */ |
| 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 | - } | |
| 97 | +function wp_stream_is_vip() { | |
| 98 | + return function_exists( 'wpcom_vip_load_plugin' ); | |
| 99 | +} | |
| 118 | 100 | |
| 119 | - $output = false; | |
| 101 | +/** | |
| 102 | + * Check if the default front-end WP Cron is enabled. It doesn't | |
| 103 | + * mean that the Cron is disabled in general. | |
| 104 | + * | |
| 105 | + * @return bool | |
| 106 | + */ | |
| 107 | +function wp_stream_is_cron_enabled() { | |
| 108 | + return ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ? false : true; | |
| 109 | +} | |
| 120 | 110 | |
| 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; | |
| 111 | +/** | |
| 112 | + * Get the asset min suffix if defined. | |
| 113 | + * | |
| 114 | + * @return string | |
| 115 | + */ | |
| 116 | +function wp_stream_min_suffix() { | |
| 117 | + $min = ''; | |
| 118 | + $is_script_debugging = ! defined( 'SCRIPT_DEBUG' ) || false === SCRIPT_DEBUG; | |
| 119 | + | |
| 120 | + if ( apply_filters( 'wp_stream_load_min_assets', $is_script_debugging ) ) { | |
| 121 | + $min = 'min.'; | |
| 127 | 122 | } |
| 128 | 123 | |
| 129 | - return $output; | |
| 124 | + return $min; | |
| 130 | 125 | } |