Skip to content

Commit a8cacce

Browse files
Merge pull request #439 from contactashish13/issue-437
import from json does not work on some sources
2 parents 56117fb + 50268f8 commit a8cacce

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

classes/Visualizer/Source/Json.php

Lines changed: 50 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 in %d elements. Ignoring.', $tag, $depth, count( $leaf ) ), '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,41 @@ 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+
* Inconsistent data causes the table to not display.
245+
*
246+
* @since ?
247+
*
248+
* @access private
249+
*/
250+
private function collectCommonElements( $data ) {
251+
$keys = array();
252+
foreach ( $data as $datum ) {
253+
if ( empty( $keys ) ) {
254+
$keys = array_keys( $datum );
255+
continue;
256+
}
257+
$keys = array_intersect( $keys, array_keys( $datum ) );
258+
}
259+
260+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Extracting data only for the common keys = %s', print_r( $keys, true ) ), 'debug', __FILE__, __LINE__ );
261+
262+
$rows = array();
263+
foreach ( $data as $datum ) {
264+
foreach ( $datum as $key => $value ) {
265+
if ( in_array( $key, $keys, true ) ) {
266+
$row[ $key ] = $value;
267+
}
268+
}
269+
$rows[] = $row;
270+
}
271+
272+
return $rows;
273+
274+
}
275+
231276
/**
232277
* Get the next page URL.
233278
*

0 commit comments

Comments
 (0)