wpseek.com
Outil de recherche WordPress pour les développeurs et auteurs de thèmes



wp_json_file_decode › WordPress Function

Depuis5.9.0
Dépréciéen/a
› wp_json_file_decode ( $filename, $options = array() )
Paramètres: (2)
  • (string) $filename Path to the JSON file.
    Requis: Oui
  • (array) $options { Optional. Options to be used with `json_decode()`. @type bool $associative Optional. When `true`, JSON objects will be returned as associative arrays. When `false`, JSON objects will be returned as objects. Default false. }
    Requis: Non
    Défaut: array()
Retourne:
  • (mixed) Returns the value encoded in JSON in appropriate PHP type.
    `null` is returned if the file is not found, is not a readable file,
    or its content can't be decoded.
Défini(e) dans:
Codex:

Reads and decodes a JSON file.



Source

function wp_json_file_decode( $filename, $options = array() ) {
	$normalized_filename = wp_normalize_path( realpath( $filename ) );

	if ( ! $normalized_filename ) {
		wp_trigger_error(
			__FUNCTION__,
			sprintf(
				/* translators: %s: Path to the JSON file. */
				__( "File %s doesn't exist!" ),
				$filename
			)
		);
		return null;
	}
	if ( ! is_file( $normalized_filename ) || ! is_readable( $normalized_filename ) ) {
		wp_trigger_error(
			__FUNCTION__,
			sprintf(
				/* translators: %s: Path to the JSON file. */
				__( '%s is not a readable file.' ),
				$filename
			)
		);
		return null;
	}

	$options      = wp_parse_args( $options, array( 'associative' => false ) );
	$decoded_file = json_decode( file_get_contents( $normalized_filename ), $options['associative'] );

	if ( JSON_ERROR_NONE !== json_last_error() ) {
		wp_trigger_error(
			__FUNCTION__,
			sprintf(
				/* translators: 1: Path to the JSON file, 2: Error message. */
				__( 'Error when decoding a JSON file at path %1$s: %2$s' ),
				$filename,
				json_last_error_msg()
			)
		);
		return null;
	}

	return $decoded_file;
}