PluginProbe ʕ •ᴥ•ʔ
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings / 1.0.255
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings v1.0.255
1.0.272 1.0.271 1.0.271.1 1.0.270 1.0.269 trunk 1.0.216 1.0.217 1.0.218 1.0.219 1.0.220 1.0.221 1.0.222 1.0.223 1.0.224 1.0.225 1.0.226 1.0.227 1.0.227.1 1.0.228 1.0.229 1.0.230 1.0.231 1.0.232 1.0.233 1.0.234 1.0.234.1 1.0.235 1.0.236 1.0.237 1.0.238 1.0.239 1.0.240 1.0.241 1.0.242 1.0.243 1.0.244 1.0.245 1.0.246 1.0.247 1.0.248 1.0.249 1.0.250 1.0.251 1.0.251.1 1.0.252 1.0.252.1 1.0.253 1.0.254 1.0.255 1.0.256 1.0.257 1.0.258 1.0.259 1.0.259.1 1.0.260 1.0.261 1.0.262 1.0.263 1.0.264 1.0.264.1 1.0.265 1.0.266 1.0.266.1 1.0.267 1.0.268
seo-by-rank-math / vendor / mythemeshop / wordpress-helpers / src / class-json-manager.php
seo-by-rank-math / vendor / mythemeshop / wordpress-helpers / src Last commit date
admin 2 years ago database 2 years ago helpers 2 years ago class-json-manager.php 2 years ago class-notification-center.php 2 years ago class-notification.php 2 years ago index.php 2 years ago
class-json-manager.php
129 lines
1 <?php
2 /**
3 * The JSON manager handles json output to admin and frontend.
4 *
5 * @since 1.0.0
6 * @package MyThemeShop
7 * @subpackage MyThemeShop
8 * @author MyThemeShop <admin@mythemeshop.com>
9 */
10
11 namespace MyThemeShop;
12
13 /**
14 * Json_Manager class.
15 */
16 class Json_Manager {
17
18 /**
19 * Data.
20 *
21 * @var array
22 */
23 private $data = [];
24
25 /**
26 * Construct
27 */
28 public function __construct() {
29 $hook = is_admin() ? 'admin_footer' : 'wp_footer';
30 add_action( $hook, [ $this, 'output' ], 0 );
31 }
32
33 /**
34 * Add something to JSON object.
35 *
36 * @param string $key Unique identifier.
37 * @param mixed $value The data itself can be either a single or an array.
38 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
39 */
40 public function add( $key, $value, $object_name ) {
41
42 if ( empty( $key ) ) {
43 return;
44 }
45
46 // If key doesn't exists.
47 if ( ! isset( $this->data[ $object_name ][ $key ] ) ) {
48 $this->data[ $object_name ][ $key ] = $value;
49 return;
50 }
51
52 // If key already exists.
53 $old_value = $this->data[ $object_name ][ $key ];
54
55 // If both array merge them.
56 if ( is_array( $old_value ) && is_array( $value ) ) {
57 $this->data[ $object_name ][ $key ] = array_merge( $old_value, $value );
58 return;
59 }
60
61 $this->data[ $object_name ][ $key ] = $value;
62 }
63
64 /**
65 * Remove something from JSON object.
66 *
67 * @param string $key Unique identifier.
68 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
69 */
70 public function remove( $key, $object_name ) {
71 if ( isset( $this->data[ $object_name ][ $key ] ) ) {
72 unset( $this->data[ $object_name ][ $key ] );
73 }
74 }
75
76 /**
77 * Print data.
78 */
79 public function output() {
80 $script = $this->encode();
81 if ( ! $script ) {
82 return;
83 }
84
85 echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5.
86 echo "/* <![CDATA[ */\n";
87 echo "$script\n";
88 echo "/* ]]> */\n";
89 echo "</script>\n";
90 }
91
92 /**
93 * Get encoded string.
94 *
95 * @return string
96 */
97 private function encode() {
98 $script = '';
99 foreach ( $this->data as $object_name => $object_data ) {
100 $script .= $this->single_object( $object_name, $object_data );
101 }
102
103 return $script;
104 }
105
106 /**
107 * Encode single object.
108 *
109 * @param string $object_name Object name to use as JS variable.
110 * @param array $object_data Object data to json encode.
111 * @return array
112 */
113 private function single_object( $object_name, $object_data ) {
114 if ( empty( $object_data ) ) {
115 return '';
116 }
117
118 foreach ( (array) $object_data as $key => $value ) {
119 if ( ! is_string( $value ) ) {
120 continue;
121 }
122
123 $object_data[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
124 }
125
126 return "var $object_name = " . wp_json_encode( $object_data ) . ';' . PHP_EOL;
127 }
128 }
129