Skip to content

Commit 3b6a300

Browse files
import from json does not work on some sources
1 parent 8d40a47 commit 3b6a300

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

classes/Visualizer/Source/Json.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,16 @@ public function parse() {
186186
if ( array_key_exists( $tag, $leaf ) ) {
187187
$leaf = $leaf[ $tag ];
188188
} else {
189-
// if the tag does not exist, we assume it is present in the 0th element of the current array.
190-
// TODO: we may want to change this to a filter later.
191-
$leaf = $leaf[0][ $tag ];
189+
// if the tag does not exist, we assume it is present in every element of the current array.
190+
$temp = array();
191+
for ( $depth = 0; $depth < count( $leaf ); $depth++ ) {
192+
if ( ! isset( $leaf[ $depth ][ $tag ] ) ) {
193+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Element %s not present at depth %d. Ignoring.', $tag, $depth ), 'debug', __FILE__, __LINE__ );
194+
continue;
195+
}
196+
$temp[] = $leaf[ $depth ][ $tag ];
197+
}
198+
$leaf = $temp;
192199
}
193200
}
194201

@@ -204,6 +211,7 @@ public function parse() {
204211
}
205212
$data[] = $inner_data;
206213
} else {
214+
$row_num = 0;
207215
// we will filter out all elements of this array that have array as a value.
208216
foreach ( $leaf as $datum ) {
209217
$inner_data = array();
@@ -213,13 +221,15 @@ public function parse() {
213221
}
214222
$inner_data[ $key ] = $value;
215223
}
216-
// if we want to exclude entire rows on the basis of some data/key.
217-
if ( apply_filters( 'visualizer_json_include_row', true, $inner_data, $this->_root, $this->_url ) ) {
224+
// if we want to exclude entire rows on the basis of some data/key or row index.
225+
if ( apply_filters( 'visualizer_json_include_row', true, $inner_data, $this->_root, $this->_url, ++$row_num ) ) {
218226
$data[] = $inner_data;
219227
}
220228
}
221229
}
222230

231+
$data = $this->collectCommonElements( $data );
232+
223233
$url = $this->getNextPage( $array );
224234
}
225235

@@ -228,6 +238,40 @@ public function parse() {
228238
return $data;
229239
}
230240

241+
/**
242+
* Determines which keys are common to all the data and returns an array with only those elements.
243+
* This is to ensure that the data set displayed on the table is consistent.
244+
*
245+
* @since ?
246+
*
247+
* @access private
248+
*/
249+
private function collectCommonElements( $data ) {
250+
$keys = array();
251+
foreach ( $data as $datum ) {
252+
if ( empty( $keys ) ) {
253+
$keys = array_keys( $datum );
254+
continue;
255+
}
256+
$keys = array_intersect( $keys, array_keys( $datum ) );
257+
}
258+
259+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Extracting data only for the common keys = %s', print_r( $keys, true ) ), 'debug', __FILE__, __LINE__ );
260+
261+
$rows = array();
262+
foreach ( $data as $datum ) {
263+
foreach ( $datum as $key => $value ) {
264+
if ( in_array( $key, $keys, true ) ) {
265+
$row[ $key ] = $value;
266+
}
267+
}
268+
$rows[] = $row;
269+
}
270+
271+
return $rows;
272+
273+
}
274+
231275
/**
232276
* Get the next page URL.
233277
*

0 commit comments

Comments
 (0)