[WordPress General] wp_remote_get() を利用してローカルの json file を読み込む

PHP Code Sniffer に `file_get_contents() is discouraged.` といわれてしまいました。

WordPress の HTTP API を使うようにとのこと。
WP_Error クラスを利用したエラー処理が簡単にかけるようになる。

> * [HTTP API – WordPress Codex 日本語版](https://wpdocs.osdn.jp/HTTP_API)
> * [クラスリファレンス/WP Error – WordPress Codex 日本語版](https://wpdocs.osdn.jp/%E3%82%AF%E3%83%A9%E3%82%B9%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/WP_Error)

“`prettyprinted
FILE: /var/www/html/wp-content/themes/foo/functions.php
———————————————————————-
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
———————————————————————-
427 | WARNING | file_get_contents() is discouraged. Use wp_remote_get()
| | for remote URLs instead.
| | (WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents)
———————————————————————-
“`

“`php:functions.php
/**
* Get the JSON data.
*/
function get_json_data( $file_name ) {
$file = get_template_directory_uri() . “/data/{$file_name}”;
$request = wp_remote_get( esc_url_raw( $file ) );

if ( is_wp_error( $request ) ) {
return false;
}

$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body, true );

return $data;
}
“`