| 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 NumberFormatter; |
| 16 |
use WP_Post; |
| 17 |
use WP_Error; |
| 18 |
|
| 19 |
use const Parsely\PARSELY_FILE; |
| 20 |
|
| 21 |
const DATE_UTC_FORMAT = 'Y-m-d'; |
| 22 |
const WP_DATE_TIME_FORMAT = 'Y-m-d H:i:s'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Gets UTC Date. |
| 26 |
* |
| 27 |
* @since 3.7.0 |
| 28 |
* |
| 29 |
* @param int $days Number of days before or after the current date. |
| 30 |
* |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
function get_utc_date_format( int $days = 0 ): string { |
| 34 |
if ( 0 === $days ) { |
| 35 |
return gmdate( DATE_UTC_FORMAT ); |
| 36 |
} |
| 37 |
|
| 38 |
return gmdate( DATE_UTC_FORMAT, (int) strtotime( "{$days} days" ) ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Gets default category. |
| 43 |
* |
| 44 |
* @since 3.7.0 |
| 45 |
* |
| 46 |
* @return int |
| 47 |
*/ |
| 48 |
function get_default_category(): int { |
| 49 |
/** |
| 50 |
* Variable. |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
$default_category = get_option( 'default_category' ); |
| 55 |
|
| 56 |
return (int) $default_category; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Gets option `page_for_posts`. |
| 61 |
* |
| 62 |
* @since 3.7.0 |
| 63 |
* |
| 64 |
* @param bool $default Default Value. |
| 65 |
* |
| 66 |
* @return int|WP_Post |
| 67 |
*/ |
| 68 |
function get_page_for_posts( $default = false ) { |
| 69 |
/** |
| 70 |
* Variable. |
| 71 |
* |
| 72 |
* @var int|WP_Post |
| 73 |
*/ |
| 74 |
return get_option( 'page_for_posts', $default ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Gets option `page_on_front`. |
| 79 |
* |
| 80 |
* @since 3.7.0 |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
function get_page_on_front() { |
| 85 |
/** |
| 86 |
* Variable. |
| 87 |
* |
| 88 |
* @var bool |
| 89 |
*/ |
| 90 |
return get_option( 'page_on_front' ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Gets 'string' query variable from WP_Query class. |
| 95 |
* |
| 96 |
* @since 3.7.0 |
| 97 |
* |
| 98 |
* @param string $var Variable key to retrieve. |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
function get_string_query_var( $var ): string { |
| 103 |
/** |
| 104 |
* Variable. |
| 105 |
* |
| 106 |
* @var string |
| 107 |
*/ |
| 108 |
return get_query_var( $var ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Gets site date format. |
| 113 |
* |
| 114 |
* @since 3.7.0 |
| 115 |
*/ |
| 116 |
function get_date_format(): string { |
| 117 |
/** |
| 118 |
* Variable. |
| 119 |
* |
| 120 |
* @var string |
| 121 |
*/ |
| 122 |
return get_option( 'date_format' ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Gets site time format. |
| 127 |
* |
| 128 |
* @since 3.7.0 |
| 129 |
*/ |
| 130 |
function get_time_format(): string { |
| 131 |
/** |
| 132 |
* Variable. |
| 133 |
* |
| 134 |
* @var string |
| 135 |
*/ |
| 136 |
return get_option( 'time_format' ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Gets number in formatted form i.e. express bigger numbers in form of thousands (K), millions (M), billions (B). |
| 141 |
* |
| 142 |
* Example: |
| 143 |
* - Represent 10000 as 10K. |
| 144 |
* |
| 145 |
* @since 3.7.0 |
| 146 |
* |
| 147 |
* @param int|float $number Number that we have to format. |
| 148 |
* |
| 149 |
* @return string |
| 150 |
*/ |
| 151 |
function get_formatted_number( $number ): string { |
| 152 |
$number_formatter = new NumberFormatter( 'en', NumberFormatter::PADDING_POSITION ); |
| 153 |
$formatted_number = $number_formatter->format( $number ); |
| 154 |
|
| 155 |
if ( false === $formatted_number ) { |
| 156 |
return ''; |
| 157 |
} |
| 158 |
|
| 159 |
return $formatted_number; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Gets time in formatted form. |
| 164 |
* |
| 165 |
* Example: |
| 166 |
* - Input `1000` (seconds) and Output `16:40` which represents "16 minutes, 40 seconds” |
| 167 |
* |
| 168 |
* @since 3.7.0 |
| 169 |
* |
| 170 |
* @param int|float $seconds Time in seconds that we have to format. |
| 171 |
* |
| 172 |
* @return string |
| 173 |
*/ |
| 174 |
function get_formatted_time( $seconds ): string { |
| 175 |
$time_formatter = new NumberFormatter( 'en', NumberFormatter::DURATION ); |
| 176 |
$formatted_time = $time_formatter->format( $seconds ); |
| 177 |
|
| 178 |
if ( false === $formatted_time ) { |
| 179 |
return ''; |
| 180 |
} |
| 181 |
|
| 182 |
return $formatted_time; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Converts to associate array. |
| 187 |
* |
| 188 |
* @since 3.7.0 |
| 189 |
* |
| 190 |
* @param mixed $obj Input object. |
| 191 |
* |
| 192 |
* @return array<string, mixed>|WP_Error |
| 193 |
*/ |
| 194 |
function convert_to_associative_array( $obj ) { |
| 195 |
$encoded = wp_json_encode( $obj ); |
| 196 |
|
| 197 |
if ( false === $encoded ) { |
| 198 |
return new WP_Error( 'parsely_encoding_failed', __( 'Unable to encode API response for associative array', 'wp-parsely' ) ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Variable. |
| 203 |
* |
| 204 |
* @var array<string, mixed> |
| 205 |
*/ |
| 206 |
return json_decode( $encoded, true ); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Converts a string to a positive integer, removing any non-numeric |
| 211 |
* characters. |
| 212 |
* |
| 213 |
* @param string $string The string to be converted to an integer. |
| 214 |
* @return int The integer resulting from the conversion. |
| 215 |
*/ |
| 216 |
function convert_to_positive_integer( string $string ): int { |
| 217 |
return (int) preg_replace( '/\D/', '', $string ); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Converts endpoint to filter key by replacing `/` with `_`. |
| 222 |
* |
| 223 |
* @param string $endpoint Route of the endpoint. |
| 224 |
* |
| 225 |
* @since 3.7.0 |
| 226 |
* |
| 227 |
* @return string |
| 228 |
*/ |
| 229 |
function convert_endpoint_to_filter_key( string $endpoint ): string { |
| 230 |
return trim( str_replace( '/', '_', $endpoint ), '_' ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Gets content of asset file. |
| 235 |
* |
| 236 |
* @param string $path Path of the asset file. |
| 237 |
* |
| 238 |
* @since 3.8.0 |
| 239 |
* |
| 240 |
* @return Asset_Info |
| 241 |
*/ |
| 242 |
function get_asset_info( string $path ) { |
| 243 |
return require plugin_dir_path( PARSELY_FILE ) . $path; |
| 244 |
} |
| 245 |
|