functions.php
178 lines
| 1 | <?php |
| 2 | /** MainWP Child Reports default functions. */ |
| 3 | |
| 4 | // Exit if accessed directly. |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * Gets a specific external variable by name and optionally filters it. |
| 11 | * |
| 12 | * This is a polyfill function intended to be used in place of PHP's |
| 13 | * filter_input() function, which can occasionally be unreliable. |
| 14 | * |
| 15 | * @param int $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. |
| 16 | * @param string $variable_name Name of a variable to get. |
| 17 | * @param int $filter The ID of the filter to apply. |
| 18 | * @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. |
| 19 | * |
| 20 | * @return Value of the requested variable on success, FALSE if the filter fails, or NULL if the $variable_name is not set. |
| 21 | * |
| 22 | * @uses \WP_MainWP_Stream\Filter_Input |
| 23 | */ |
| 24 | function wp_mainwp_stream_filter_input( $type, $variable_name, $filter = null, $options = array() ) { |
| 25 | return call_user_func_array( array( '\WP_MainWP_Stream\Filter_Input', 'super' ), func_get_args() ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Filters a variable with a specified filter. |
| 30 | * |
| 31 | * This is a polyfill function intended to be used in place of PHP's |
| 32 | * filter_var() function, which can occasionally be unreliable. |
| 33 | * |
| 34 | * @param string $var Value to filter. |
| 35 | * @param int $filter The ID of the filter to apply. |
| 36 | * @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. |
| 37 | * |
| 38 | * @return Returns the filtered data, or FALSE if the filter fails. |
| 39 | * |
| 40 | * @uses \WP_MainWP_Stream\Filter_Input |
| 41 | */ |
| 42 | function wp_mainwp_stream_filter_var( $var, $filter = null, $options = array() ) { |
| 43 | return call_user_func_array( array( '\WP_MainWP_Stream\Filter_Input', 'filter' ), func_get_args() ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Converts a time into an ISO 8601 extended formatted string. |
| 48 | * |
| 49 | * @param int|bool $time Seconds since unix epoc |
| 50 | * @param int $offset Hour offset |
| 51 | * @param bool $mysql_date_string Whether to use mysql date string. |
| 52 | * |
| 53 | * @return string an ISO 8601 extended formatted time |
| 54 | * @throws Exception Error message. |
| 55 | */ |
| 56 | function wp_mainwp_stream_get_iso_8601_extended_date( $time = false, $offset = 0, $mysql_date_string = false ) { |
| 57 | if ( $time ) { |
| 58 | $microtime = (float) ( $time . '.0000' ); |
| 59 | } else { |
| 60 | $microtime = microtime( true ); |
| 61 | } |
| 62 | |
| 63 | $micro_seconds = sprintf( '%06d', ( $microtime - floor( $microtime ) ) * 1000000 ); |
| 64 | $offset_string = sprintf( 'Etc/GMT%s%d', $offset < 0 ? '+' : '-', abs( $offset ) ); |
| 65 | |
| 66 | $timezone = new DateTimeZone( $offset_string ); |
| 67 | $date = new DateTime( gmdate( 'Y-m-d H:i:s.' . $micro_seconds, intval( $microtime ) ), $timezone ); |
| 68 | |
| 69 | if ( $mysql_date_string ) { |
| 70 | return $date->format( 'Y-m-d H:i:s' ); |
| 71 | } |
| 72 | |
| 73 | return $date->format( 'Y-m-d\TH:i:sO' ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Encode to JSON in a way that is also backwards compatible. |
| 78 | * |
| 79 | * @param mixed $data |
| 80 | * @param int $options (optional) |
| 81 | * @param int $depth (optional) |
| 82 | * |
| 83 | * @return string |
| 84 | */ |
| 85 | function wp_mainwp_stream_json_encode( $data, $options = 0, $depth = 512 ) { |
| 86 | if ( function_exists( 'wp_json_encode' ) ) { |
| 87 | $json = wp_json_encode( $data, $options, $depth ); |
| 88 | } else { |
| 89 | // @codingStandardsIgnoreStart |
| 90 | if ( version_compare( PHP_VERSION, '5.5', '<' ) ) { |
| 91 | $json = json_encode( $data, $options ); |
| 92 | } else { |
| 93 | $json = json_encode( $data, $options, $depth ); |
| 94 | } |
| 95 | // @codingStandardsIgnoreEnd |
| 96 | } |
| 97 | |
| 98 | return $json; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return an array of sites for a network in a way that is also backwards compatible. |
| 103 | * |
| 104 | * @param string|array $args |
| 105 | * |
| 106 | * @return array |
| 107 | */ |
| 108 | function wp_mainwp_stream_get_sites( $args = array() ) { |
| 109 | if ( function_exists( 'get_sites' ) ) { |
| 110 | $sites = get_sites( $args ); |
| 111 | } else { |
| 112 | $sites = array(); |
| 113 | foreach ( wp_get_sites( $args ) as $site ) { // @codingStandardsIgnoreLine Specifically for old version of WP first, in order to provide backward compatibility |
| 114 | $sites[] = WP_Site::get_instance( $site['blog_id'] ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return $sites; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Check if Stream is running on WordPress.com VIP |
| 123 | * |
| 124 | * @return bool |
| 125 | */ |
| 126 | function wp_mainwp_stream_is_vip() { |
| 127 | return function_exists( 'wpcom_vip_load_plugin' ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * True if it is mainwp dashboard request |
| 132 | * |
| 133 | * @return bool |
| 134 | */ |
| 135 | function wp_mainwp_stream_is_dashboard_request() { |
| 136 | return ( isset( $_POST['mainwpsignature'] ) && isset( $_POST['function'] ) ) ? true : false; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * True if native WP Cron is enabled, otherwise false |
| 141 | * |
| 142 | * @return bool |
| 143 | */ |
| 144 | function wp_mainwp_stream_is_cron_enabled() { |
| 145 | return ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ? false : true; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * True if native WP Cron is enabled, otherwise false |
| 151 | * |
| 152 | * @return bool |
| 153 | */ |
| 154 | function wp_mainwp_stream_is_cron_doing() { |
| 155 | return ( defined( 'DOING_CRON' ) && DOING_CRON ) ? true : false; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get current WordPress version. |
| 160 | * |
| 161 | * @return string $wp_version Current WordPress version. |
| 162 | */ |
| 163 | function wp_mainwp_stream_get_wordpress_version() { |
| 164 | |
| 165 | /** |
| 166 | * The installed version of WordPress. |
| 167 | * |
| 168 | * @global string $wp_version The installed version of WordPress. |
| 169 | */ |
| 170 | global $wp_version; |
| 171 | |
| 172 | if ( function_exists( '\wp_get_wp_version' ) ) { |
| 173 | return \wp_get_wp_version(); |
| 174 | } |
| 175 | |
| 176 | return $wp_version; |
| 177 | } |
| 178 |