@@ -186,9 +186,16 @@ public function parse() {
186
186
if ( array_key_exists ( $ tag , $ leaf ) ) {
187
187
$ leaf = $ leaf [ $ tag ];
188
188
} 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 ;
192
199
}
193
200
}
194
201
@@ -204,6 +211,7 @@ public function parse() {
204
211
}
205
212
$ data [] = $ inner_data ;
206
213
} else {
214
+ $ row_num = 0 ;
207
215
// we will filter out all elements of this array that have array as a value.
208
216
foreach ( $ leaf as $ datum ) {
209
217
$ inner_data = array ();
@@ -213,13 +221,15 @@ public function parse() {
213
221
}
214
222
$ inner_data [ $ key ] = $ value ;
215
223
}
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 ) ) {
218
226
$ data [] = $ inner_data ;
219
227
}
220
228
}
221
229
}
222
230
231
+ $ data = $ this ->collectCommonElements ( $ data );
232
+
223
233
$ url = $ this ->getNextPage ( $ array );
224
234
}
225
235
@@ -228,6 +238,41 @@ public function parse() {
228
238
return $ data ;
229
239
}
230
240
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
+
231
276
/**
232
277
* Get the next page URL.
233
278
*
0 commit comments