PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / modules / wpemerge-app-core / src / Concerns / ReadsJsonTrait.php

ReadsJsonTrait.php in Depicter — Popup & Slider Builder 1.2.0, at modules/wpemerge-app-core/src/Concerns/ReadsJsonTrait.php

83 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package WPEmergeAppCore
4 * @author Atanas Angelov <hi@atanas.dev>
5 * @copyright 2017-2020 Atanas Angelov
6 * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 * @link https://wpemerge.com/
8 */
9
10 namespace WPEmergeAppCore\Concerns;
11
12 use WPEmerge\Support\Arr;
13
14 trait ReadsJsonTrait {
15 /**
16 * Cache.
17 *
18 * @var array|null
19 */
20 protected $cache = null;
21
22 /**
23 * Get the path to the JSON that should be read.
24 *
25 * @return string
26 */
27 abstract protected function getJsonPath();
28
29 /**
30 * Load the json file.
31 *
32 * @param string $file
33 *
34 * @return array
35 */
36 protected function load( $file ) {
37 /** @var \WP_Filesystem_Base $wp_filesystem */
38 global $wp_filesystem;
39
40 require_once ABSPATH . '/wp-admin/includes/file.php';
41
42 WP_Filesystem();
43
44 if ( ! $wp_filesystem->exists( $file ) ) {
45 throw new JsonFileNotFoundException( 'The required ' . basename( $file ) . ' file is missing.' );
46 }
47
48 $contents = $wp_filesystem->get_contents( $file );
49 $json = json_decode( $contents, true );
50 $json_error = json_last_error();
51
52 if ( $json_error !== JSON_ERROR_NONE ) {
53 throw new JsonFileInvalidException( 'The required ' . basename( $file ) . ' file is not valid JSON (error code ' . $json_error . ').' );
54 }
55
56 return $json;
57 }
58
59 /**
60 * Get the entire json array.
61 *
62 * @return array
63 */
64 protected function getAll() {
65 if ($this->cache === null) {
66 $this->cache = $this->load( $this->getJsonPath() );
67 }
68
69 return $this->cache;
70 }
71
72 /**
73 * Get a json value.
74 *
75 * @param string $key
76 * @param mixed $default
77 * @return mixed
78 */
79 public function get( $key, $default = null ) {
80 return Arr::get( $this->getAll(), $key, $default );
81 }
82 }
83