PluginProbe
Parse.ly / 3.16.4
Parse.ly v3.16.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
wp-parsely / src / Utils / utils.php

utils.php in Parse.ly 3.16.4, at src/Utils/utils.php

388 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Util Functions.
4 *
5 * To enforce typing on commonly used functions.
6 *
7 * @package Parsely
8 * @since 3.7.0
9 */
10
11 declare(strict_types=1);
12
13 namespace Parsely\Utils;
14
15 use WP_Post;
16 use WP_Error;
17
18 use const Parsely\PARSELY_FILE;
19
20 const DATE_UTC_FORMAT = 'Y-m-d';
21 const WP_DATE_TIME_FORMAT = 'Y-m-d H:i:s';
22
23 /**
24 * Gets UTC Date.
25 *
26 * @since 3.7.0
27 *
28 * @param int $days Number of days before or after the current date.
29 *
30 * @return string
31 */
32 function get_utc_date_format( int $days = 0 ): string {
33 if ( 0 === $days ) {
34 return gmdate( DATE_UTC_FORMAT );
35 }
36
37 return gmdate( DATE_UTC_FORMAT, (int) strtotime( "{$days} days" ) );
38 }
39
40 /**
41 * Gets default category.
42 *
43 * @since 3.7.0
44 *
45 * @return int
46 */
47 function get_default_category(): int {
48 /**
49 * Variable.
50 *
51 * @var string
52 */
53 $default_category = get_option( 'default_category' );
54
55 return (int) $default_category;
56 }
57
58 /**
59 * Gets option `page_for_posts`.
60 *
61 * @since 3.7.0
62 *
63 * @param bool $default_value Default Value.
64 *
65 * @return int|WP_Post
66 */
67 function get_page_for_posts( $default_value = false ) {
68 /**
69 * Variable.
70 *
71 * @var int|WP_Post
72 */
73 return get_option( 'page_for_posts', $default_value );
74 }
75
76 /**
77 * Gets option `page_on_front`.
78 *
79 * @since 3.7.0
80 *
81 * @return bool
82 */
83 function get_page_on_front() {
84 /**
85 * Variable.
86 *
87 * @var bool
88 */
89 return get_option( 'page_on_front' );
90 }
91
92 /**
93 * Gets 'string' query variable from WP_Query class.
94 *
95 * @since 3.7.0
96 *
97 * @param string $key Variable key to retrieve.
98 *
99 * @return string
100 */
101 function get_string_query_var( string $key ): string {
102 /**
103 * Variable.
104 *
105 * @var string
106 */
107 return get_query_var( $key );
108 }
109
110 /**
111 * Gets site date format.
112 *
113 * @since 3.7.0
114 */
115 function get_date_format(): string {
116 /**
117 * Variable.
118 *
119 * @var string
120 */
121 return get_option( 'date_format' );
122 }
123
124 /**
125 * Gets site time format.
126 *
127 * @since 3.7.0
128 */
129 function get_time_format(): string {
130 /**
131 * Variable.
132 *
133 * @var string
134 */
135 return get_option( 'time_format' );
136 }
137
138 /**
139 * Returns the current time's timestamp.
140 *
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 * Example:
159 * - Represent 10000 as 10K.
160 *
161 * @since 3.7.0
162 *
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.
166 *
167 * @return string The number formatted as an imprecise number.
168 */
169 function get_formatted_number( string $value, int $fraction_digits = 1, string $glue = '' ): string {
170 $number = (int) preg_replace( '/\D/', '', $value );
171
172 if ( $number < 1000 ) {
173 return $value;
174 } elseif ( $number < 10000 ) {
175 $fraction_digits = 1;
176 }
177
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;
215 }
216
217 /**
218 * Gets time in formatted form.
219 *
220 * Example:
221 * - Input `1000` (seconds) and Output `16:40` which represents "16 minutes, 40 seconds”
222 *
223 * @since 3.7.0
224 *
225 * @param float $seconds Time in seconds to be formatted.
226 *
227 * @return string
228 */
229 function get_formatted_time( $seconds ): string {
230 $seconds = round( $seconds );
231 $hours = floor( $seconds / 3600 );
232
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 );
241 }
242
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 );
255 }
256
257 /**
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 * Converts to associate array.
285 *
286 * @since 3.7.0
287 *
288 * @param mixed $obj Input object.
289 *
290 * @return array<string, mixed>|WP_Error
291 */
292 function convert_to_associative_array( $obj ) {
293 $encoded = wp_json_encode( $obj );
294
295 if ( false === $encoded ) {
296 return new WP_Error( 'parsely_encoding_failed', __( 'Unable to encode API response for associative array', 'wp-parsely' ) );
297 }
298
299 /**
300 * Variable.
301 *
302 * @var array<string, mixed>
303 */
304 return json_decode( $encoded, true );
305 }
306
307 /**
308 * Converts a string to a positive integer, removing any non-numeric
309 * characters.
310 *
311 * @param string $value The string to be converted to an integer.
312 * @return int The integer resulting from the conversion.
313 */
314 function convert_to_positive_integer( string $value ): int {
315 return (int) preg_replace( '/\D/', '', $value );
316 }
317
318 /**
319 * Converts endpoint to filter key by replacing `/` with `_`.
320 *
321 * @param string $endpoint Route of the endpoint.
322 *
323 * @since 3.7.0
324 *
325 * @return string
326 */
327 function convert_endpoint_to_filter_key( string $endpoint ): string {
328 return trim( str_replace( array( '-', '/' ), '_', $endpoint ), '_' );
329 }
330
331 /**
332 * Gets content of asset file.
333 *
334 * @param string $path Path of the asset file.
335 *
336 * @since 3.8.0
337 *
338 * @return Asset_Info
339 */
340 function get_asset_info( string $path ) {
341 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 }
388