Changeset 51599 for trunk/src/wp-includes/functions.php
- Timestamp:
- 08/11/2021 09:06:31 AM (5 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/functions.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/functions.php
r51557 r51599 4269 4269 4270 4270 /** 4271 * Reads and decodes a JSON file. 4272 * 4273 * @since 5.9.0 4274 * 4275 * @param string $filename Path to the JSON file. 4276 * @param array $options { 4277 * Optional. Options to be used with `json_decode()`. 4278 * 4279 * @type bool associative Optional. When `true`, JSON objects will be returned as associative arrays. 4280 * When `false`, JSON objects will be returned as objects. 4281 * } 4282 * 4283 * @return mixed Returns the value encoded in JSON in appropriate PHP type. 4284 * `null` is returned if the file is not found, or its content can't be decoded. 4285 */ 4286 function wp_json_file_decode( $filename, $options = array() ) { 4287 $result = null; 4288 $filename = wp_normalize_path( realpath( $filename ) ); 4289 if ( ! file_exists( $filename ) ) { 4290 trigger_error( 4291 sprintf( 4292 /* translators: %s: Path to the JSON file. */ 4293 __( "File %s doesn't exist!" ), 4294 $filename 4295 ) 4296 ); 4297 return $result; 4298 } 4299 4300 $options = wp_parse_args( $options, array( 'associative' => false ) ); 4301 $decoded_file = json_decode( file_get_contents( $filename ), $options['associative'] ); 4302 4303 if ( JSON_ERROR_NONE !== json_last_error() ) { 4304 trigger_error( 4305 sprintf( 4306 /* translators: 1: Path to the JSON file, 2: Error message. */ 4307 __( 'Error when decoding a JSON file at path %1$s: %2$s' ), 4308 $filename, 4309 json_last_error_msg() 4310 ) 4311 ); 4312 return $result; 4313 } 4314 4315 return $decoded_file; 4316 } 4317 4318 /** 4271 4319 * Retrieve the WordPress home page URL. 4272 4320 *
Note: See TracChangeset
for help on using the changeset viewer.