PluginProbe
Parse.ly / 3.8.4
Parse.ly v3.8.4
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
← All changes | src/Utils/utils.php +26 -169 3.16.03.8.4 View file →
@@ -11,8 +11,9 @@
11 11 declare(strict_types=1);
12 12
13 13 namespace Parsely\Utils;
14 14
15 +use NumberFormatter;
15 16 use WP_Post;
16 17 use WP_Error;
17 18
18 19 use const Parsely\PARSELY_FILE;
@@ -59,19 +60,19 @@
59 60 * Gets option `page_for_posts`.
60 61 *
61 62 * @since 3.7.0
62 63 *
63 - * @param bool $default_value Default Value.
64 + * @param bool $default Default Value.
64 65 *
65 66 * @return int|WP_Post
66 67 */
67 -function get_page_for_posts( $default_value = false ) {
68 +function get_page_for_posts( $default = false ) {
68 69 /**
69 70 * Variable.
70 71 *
71 72 * @var int|WP_Post
72 73 */
73 - return get_option( 'page_for_posts', $default_value );
74 + return get_option( 'page_for_posts', $default );
74 75 }
75 76
76 77 /**
77 78 * Gets option `page_on_front`.
@@ -93,19 +94,19 @@
93 94 * Gets 'string' query variable from WP_Query class.
94 95 *
95 96 * @since 3.7.0
96 97 *
97 - * @param string $key Variable key to retrieve.
98 + * @param string $var Variable key to retrieve.
98 99 *
99 100 * @return string
100 101 */
101 -function get_string_query_var( string $key ): string {
102 +function get_string_query_var( $var ): string {
102 103 /**
103 104 * Variable.
104 105 *
105 106 * @var string
106 107 */
107 - return get_query_var( $key );
108 + return get_query_var( $var );
108 109 }
109 110
110 111 /**
111 112 * Gets site date format.
@@ -135,84 +136,28 @@
135 136 return get_option( 'time_format' );
136 137 }
137 138
138 139 /**
139 - * Returns the current time's timestamp.
140 + * Gets number in formatted form i.e. express bigger numbers in form of thousands (K), millions (M), billions (B).
140 141 *
141 - * @since 3.12.0
142 - *
143 - * @return string
144 - */
145 -function get_timestamp(): string {
146 - $timestamp = round( microtime( true ) * 1000 );
147 -
148 - return number_format( $timestamp, 0, '', '' );
149 -}
150 -
151 -/**
152 - * Gets number in formatted form i.e. express bigger numbers in form of
153 - * thousands (k), millions (M), billions (B).
154 - *
155 - * Note: This function is not made to process float numbers, and it is a PHP
156 - * port of our formatToImpreciseNumber() TypeScript function.
157 - *
158 142 * Example:
159 143 * - Represent 10000 as 10K.
160 144 *
161 145 * @since 3.7.0
162 146 *
163 - * @param string $value The number to process. It can be formatted.
164 - * @param int $fraction_digits The number of desired fraction digits.
165 - * @param string $glue A string to put between the number and unit.
147 + * @param int|float $number Number that we have to format.
166 148 *
167 - * @return string The number formatted as an imprecise number.
149 + * @return string
168 150 */
169 -function get_formatted_number( string $value, int $fraction_digits = 1, string $glue = '' ): string {
170 - $number = (int) preg_replace( '/\D/', '', $value );
151 +function get_formatted_number( $number ): string {
152 + $number_formatter = new NumberFormatter( 'en', NumberFormatter::PADDING_POSITION );
153 + $formatted_number = $number_formatter->format( $number );
171 154
172 - if ( $number < 1000 ) {
173 - return $value;
174 - } elseif ( $number < 10000 ) {
175 - $fraction_digits = 1;
155 + if ( false === $formatted_number ) {
156 + return '';
176 157 }
177 158
178 - $unit_names = array(
179 - '1000' => 'k',
180 - '1000000' => 'M',
181 - '1000000000' => 'B',
182 - '1000000000000' => 'T',
183 - '1000000000000000' => 'Q',
184 - );
185 - $current_number = $number;
186 - $current_number_as_string = (string) $number;
187 - $unit = '';
188 - $previous_number = 0;
189 -
190 - foreach ( $unit_names as $thousands => $suffix ) {
191 - $thousands_int = (int) preg_replace( '/\D/', '', (string) $thousands );
192 -
193 - if ( $number >= $thousands_int ) {
194 - $current_number = $number / $thousands_int;
195 - $precision = $fraction_digits;
196 -
197 - // For over 10 units, we reduce the precision to 1 fraction digit.
198 - $modulo = (int) fmod( $current_number, 1 );
199 - if ( 0 !== $previous_number && $modulo > 1 / $previous_number ) {
200 - $precision = $current_number > 10 ? 1 : 2;
201 - }
202 -
203 - // Precision override, where we want to show 2 fraction digits.
204 - $zeroes = floatval( number_format( $current_number, 2 ) ) ===
205 - floatval( number_format( $current_number, 0 ) );
206 - $precision = $zeroes ? 0 : $precision;
207 - $current_number_as_string = number_format( $current_number, $precision, '.', '' );
208 - $unit = $suffix;
209 - }
210 -
211 - $previous_number = $current_number;
212 - }
213 -
214 - return $current_number_as_string . $glue . $unit;
159 + return $formatted_number;
215 160 }
216 161
217 162 /**
218 163 * Gets time in formatted form.
@@ -221,67 +166,24 @@
221 166 * - Input `1000` (seconds) and Output `16:40` which represents "16 minutes, 40 seconds”
222 167 *
223 168 * @since 3.7.0
224 169 *
225 - * @param float $seconds Time in seconds to be formatted.
170 + * @param int|float $seconds Time in seconds that we have to format.
226 171 *
227 172 * @return string
228 173 */
229 174 function get_formatted_time( $seconds ): string {
230 - $seconds = round( $seconds );
231 - $hours = floor( $seconds / 3600 );
175 + $time_formatter = new NumberFormatter( 'en', NumberFormatter::DURATION );
176 + $formatted_time = $time_formatter->format( $seconds );
232 177
233 - if ( $hours >= 1 ) {
234 - $seconds = $seconds - ( $hours * 3600 );
235 - $minutes = floor( $seconds / 60 );
236 - $seconds = round( $seconds % 60 );
237 -
238 - return esc_html( /* translators: 1: Number of hours 2: Number of minutes 3: Number of seconds */
239 - sprintf( __( '%1$d:%2$02d:%3$02d', 'wp-parsely' ), $hours, $minutes, $seconds )
240 - );
178 + if ( false === $formatted_time ) {
179 + return '';
241 180 }
242 181
243 - $minutes = floor( $seconds / 60 );
244 - $seconds = round( $seconds % 60 );
245 -
246 - if ( $minutes >= 1 ) {
247 - return esc_html( /* translators: 1: Number of minutes 2: Number of seconds */
248 - sprintf( __( '%1$d:%2$02d', 'wp-parsely' ), $minutes, $seconds )
249 - );
250 - }
251 -
252 - return esc_html( /* translators: 1: Number of seconds */
253 - sprintf( __( '%1$d sec.', 'wp-parsely' ), round( $seconds ) )
254 - );
182 + return $formatted_time;
255 183 }
256 184
257 185 /**
258 - * Returns the passed float as a time duration in m:ss format.
259 - *
260 - * Examples:
261 - * - $time of 1.005 yields '1:00'.
262 - * - $time of 1.5 yields '1:30'.
263 - * - $time of 1.999 yields '2:00'.
264 - *
265 - * @since 3.6.0
266 - *
267 - * @param float $time The time as a float number.
268 - *
269 - * @return string The resulting formatted time duration.
270 - */
271 -function get_formatted_duration( float $time ): string {
272 - $minutes = absint( $time );
273 - $seconds = absint( round( fmod( $time, 1 ) * 60 ) );
274 -
275 - if ( 60 === $seconds ) {
276 - ++$minutes;
277 - $seconds = 0;
278 - }
279 -
280 - return sprintf( '%d:%02d', $minutes, $seconds );
281 -}
282 -
283 -/**
284 186 * Converts to associate array.
285 187 *
286 188 * @since 3.7.0
287 189 *
@@ -307,13 +209,13 @@
307 209 /**
308 210 * Converts a string to a positive integer, removing any non-numeric
309 211 * characters.
310 212 *
311 - * @param string $value The string to be converted to an integer.
213 + * @param string $string The string to be converted to an integer.
312 214 * @return int The integer resulting from the conversion.
313 215 */
314 -function convert_to_positive_integer( string $value ): int {
315 - return (int) preg_replace( '/\D/', '', $value );
216 +function convert_to_positive_integer( string $string ): int {
217 + return (int) preg_replace( '/\D/', '', $string );
316 218 }
317 219
318 220 /**
319 221 * Converts endpoint to filter key by replacing `/` with `_`.
@@ -324,9 +226,9 @@
324 226 *
325 227 * @return string
326 228 */
327 229 function convert_endpoint_to_filter_key( string $endpoint ): string {
328 - return trim( str_replace( array( '-', '/' ), '_', $endpoint ), '_' );
230 + return trim( str_replace( '/', '_', $endpoint ), '_' );
329 231 }
330 232
331 233 /**
332 234 * Gets content of asset file.
@@ -338,50 +240,5 @@
338 240 * @return Asset_Info
339 241 */
340 242 function get_asset_info( string $path ) {
341 243 return require plugin_dir_path( PARSELY_FILE ) . $path;
342 -}
343 -
344 -/**
345 - * Checks if a string starts with a specific substring.
346 - *
347 - * This function uses the built-in PHP function `str_starts_with` if it's available (PHP 8.0 and later).
348 - * If the function is not available (PHP versions prior to 8.0), it uses the `strpos` function as a fallback.
349 - *
350 - * @since 3.13.0
351 - *
352 - * @param string $haystack The string to search in.
353 - * @param string $needle The substring to search for at the start of $haystack.
354 - * @return bool Returns true if $haystack starts with $needle, false otherwise.
355 - */
356 -function str_starts_with( string $haystack, string $needle ): bool {
357 - if ( function_exists( '\str_starts_with' ) ) {
358 - return \str_starts_with( $haystack, $needle );
359 - }
360 - return 0 === strpos( $haystack, $needle );
361 -}
362 -
363 -/**
364 - * Checks if HTTPS is supported for the site.
365 - *
366 - * This function checks if the WordPress function 'wp_is_using_https' exists and uses it to determine if
367 - * HTTPS is supported.
368 - * If the function does not exist, it checks if the home URL scheme is HTTPS.
369 - * If neither of the above conditions are met, it checks if the site URL option scheme is HTTPS.
370 - *
371 - * @since 3.14.1
372 - *
373 - * @return bool Returns true if HTTPS is supported, false otherwise.
374 - */
375 -function parsely_is_https_supported(): bool {
376 - if ( function_exists( 'wp_is_using_https' ) ) {
377 - return wp_is_using_https();
378 - }
379 -
380 - if ( 'https' === wp_parse_url( home_url(), PHP_URL_SCHEME ) ) {
381 - return true;
382 - }
383 -
384 - // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
385 - $site_url = apply_filters( 'site_url', get_option( 'siteurl' ), '', null, null );
386 - return 'https' === wp_parse_url( $site_url, PHP_URL_SCHEME );
387 244 }