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 |