PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.7
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.7
5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / DataTable / Renderer / Json.php
matomo / app / core / DataTable / Renderer Last commit date
Console.php 2 years ago Csv.php 2 years ago Html.php 2 years ago Json.php 2 years ago Rss.php 2 years ago Tsv.php 2 years ago Xml.php 2 years ago
Json.php
107 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 *
9 */
10 namespace Piwik\DataTable\Renderer;
11
12 use Piwik\Common;
13 use Piwik\DataTable\Renderer;
14 use Piwik\DataTable;
15 /**
16 * JSON export.
17 * Works with recursive DataTable (when a row can be associated with a subDataTable).
18 *
19 */
20 class Json extends Renderer
21 {
22 /**
23 * Computes the dataTable output and returns the string/binary
24 *
25 * @return string
26 */
27 public function render()
28 {
29 return $this->renderTable($this->table);
30 }
31 /**
32 * Computes the output for the given data table
33 *
34 * @param DataTable $table
35 * @return string
36 */
37 protected function renderTable($table)
38 {
39 if (is_array($table)) {
40 $array = $table;
41 if (self::shouldWrapArrayBeforeRendering($array, $wrapSingleValues = true)) {
42 $array = array($array);
43 }
44 foreach ($array as $key => $tab) {
45 if ($tab instanceof DataTable\Map || $tab instanceof DataTable || $tab instanceof DataTable\Simple) {
46 $array[$key] = $this->convertDataTableToArray($tab);
47 if (!is_array($array[$key])) {
48 $array[$key] = array('value' => $array[$key]);
49 }
50 }
51 }
52 } else {
53 $array = $this->convertDataTableToArray($table);
54 }
55 if (!is_array($array)) {
56 $array = array('value' => $array);
57 }
58 // convert datatable column/metadata values
59 $this->convertDataTableColumnMetadataValues($array);
60 // decode all entities
61 $callback = function (&$value, $key) {
62 if (is_string($value)) {
63 $value = html_entity_decode($value, ENT_QUOTES, "UTF-8");
64 }
65 };
66 array_walk_recursive($array, $callback);
67 // silence "Warning: json_encode(): Invalid UTF-8 sequence in argument"
68 $str = @json_encode($array);
69 if ($str === false && json_last_error() === JSON_ERROR_UTF8 && $this->canMakeArrayUtf8()) {
70 $array = $this->makeArrayUtf8($array);
71 $str = json_encode($array);
72 }
73 return $str;
74 }
75 private function canMakeArrayUtf8()
76 {
77 return function_exists('mb_convert_encoding');
78 }
79 private function makeArrayUtf8($array)
80 {
81 if (is_array($array)) {
82 foreach ($array as $key => $value) {
83 $array[$key] = self::makeArrayUtf8($value);
84 }
85 } elseif (is_string($array)) {
86 return mb_convert_encoding($array, 'UTF-8', 'auto');
87 }
88 return $array;
89 }
90 public static function sendHeaderJSON()
91 {
92 Common::sendHeader('Content-Type: application/json; charset=utf-8');
93 }
94 private function convertDataTableColumnMetadataValues(&$table)
95 {
96 if (empty($table)) {
97 return;
98 }
99 array_walk_recursive($table, function (&$value, $key) {
100 if ($value instanceof DataTable) {
101 $value = $this->convertDataTableToArray($value);
102 $this->convertDataTableColumnMetadataValues($value);
103 }
104 });
105 }
106 }
107