Skip to content

Commit 77a93c9

Browse files
move functionality to make it generic
1 parent a61b462 commit 77a93c9

File tree

3 files changed

+87
-76
lines changed

3 files changed

+87
-76
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ public function getJsonData() {
156156
}
157157

158158
$source = new Visualizer_Source_Json( $params );
159+
$source->fetch();
160+
$data = $source->getRawData();
159161

160-
$data = $source->parse();
161162
if ( empty( $data ) ) {
162163
wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) );
163164
}
@@ -186,7 +187,7 @@ public function setJsonData() {
186187
$chart = get_post( $chart_id );
187188

188189
$source = new Visualizer_Source_Json( $params );
189-
$source->fetch();
190+
$source->fetchFromEditableTable();
190191

191192
$content = $source->getData();
192193
$chart->post_content = $content;

classes/Visualizer/Source.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,5 +386,83 @@ public function get_error() {
386386
return $this->_error;
387387
}
388388

389+
/**
390+
* Fetches information from the editable table and parses it to build series and data arrays.
391+
*
392+
* @since ?
393+
*
394+
* @access public
395+
* @return boolean TRUE on success, otherwise FALSE.
396+
*/
397+
public function fetchFromEditableTable() {
398+
if ( empty( $this->_args ) ) {
399+
return false;
400+
}
401+
402+
$this->_fetchSeriesFromEditableTable();
403+
$this->_fetchDataFromEditableTable();
404+
return true;
405+
}
406+
407+
/**
408+
* Fetches series information from the editable table. This is fetched only through the UI and not while refreshing the chart data.
409+
*
410+
* @since 1.0.0
411+
*
412+
* @access private
413+
*/
414+
private function _fetchSeriesFromEditableTable() {
415+
$params = $this->_args;
416+
$headers = array_filter( $params['header'] );
417+
$types = array_filter( $params['type'] );
418+
$header_row = $type_row = array();
419+
if ( $headers ) {
420+
foreach ( $headers as $header ) {
421+
if ( ! empty( $types[ $header ] ) ) {
422+
$this->_series[] = array(
423+
'label' => $header,
424+
'type' => $types[ $header ],
425+
);
426+
}
427+
}
428+
}
429+
430+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Series found for %s = %s', print_r( $this->_args, true ), print_r( $this->_series, true ) ), 'debug', __FILE__, __LINE__ );
431+
432+
return true;
433+
}
434+
435+
436+
/**
437+
* Fetches data information from the editable table.
438+
*
439+
* @since 1.0.0
440+
*
441+
* @access private
442+
*/
443+
private function _fetchDataFromEditableTable() {
444+
$params = $this->_args;
445+
$headers = wp_list_pluck( $this->_series, 'label' );
446+
$this->fetch();
447+
448+
$data = $this->_data;
449+
$this->_data = array();
450+
451+
foreach ( $data as $line ) {
452+
$data_row = array();
453+
foreach ( $line as $header => $value ) {
454+
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
455+
if ( in_array( $header, $headers ) ) {
456+
$data_row[] = $value;
457+
}
458+
}
459+
$this->_data[] = $this->_normalizeData( $data_row );
460+
}
461+
462+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Data found for %s = %s', print_r( $this->_args, true ), print_r( $this->_data, true ) ), 'debug', __FILE__, __LINE__ );
463+
464+
return true;
465+
}
466+
389467

390468
}

classes/Visualizer/Source/Json.php

Lines changed: 6 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,15 @@ public function getPaginationElements() {
195195
*
196196
* @access public
197197
*/
198-
public function parse() {
198+
public function fetch() {
199199
$url = $this->_url;
200200
$data = array();
201201
$page = 1;
202202

203203
while ( ! is_null( $url ) && $page++ < apply_filters( 'visualizer_json_fetch_pages', 5, $this->_url ) ) {
204204
$array = $this->getJSON( $url );
205205
if ( is_null( $array ) ) {
206-
return $data;
206+
break;
207207
}
208208

209209
$root = explode( self::TAG_SEPARATOR, $this->_root );
@@ -268,9 +268,11 @@ public function parse() {
268268
$url = $this->getNextPage( $array );
269269
}
270270

271+
$this->_data = $data;
272+
271273
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Parsed data endpoint %s with root %s is %s', $this->_url, $this->_root, print_r( $data, true ) ), 'debug', __FILE__, __LINE__ );
272274

273-
return $data;
275+
return true;
274276
}
275277

276278
/**
@@ -418,76 +420,6 @@ private function connect( $url ) {
418420
return wp_remote_request( $url, $args );
419421
}
420422

421-
/**
422-
* Fetches series information. This is fetched only through the UI and not while refreshing the chart data.
423-
*
424-
* @since 1.0.0
425-
*
426-
* @access private
427-
*/
428-
private function _fetchSeries() {
429-
$params = $this->_args;
430-
$headers = array_filter( $params['header'] );
431-
$types = array_filter( $params['type'] );
432-
$header_row = $type_row = array();
433-
if ( $headers ) {
434-
foreach ( $headers as $header ) {
435-
if ( ! empty( $types[ $header ] ) ) {
436-
$this->_series[] = array(
437-
'label' => $header,
438-
'type' => $types[ $header ],
439-
);
440-
}
441-
}
442-
}
443-
444-
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Series found for %s = %s', $this->_url, print_r( $this->_series, true ) ), 'debug', __FILE__, __LINE__ );
445-
446-
return true;
447-
}
448-
449-
/**
450-
* Fetches data information.
451-
*
452-
* @since 1.0.0
453-
*
454-
* @access private
455-
*/
456-
private function _fetchData() {
457-
$params = $this->_args;
458-
459-
$headers = wp_list_pluck( $this->_series, 'label' );
460-
$data = $this->parse();
461-
foreach ( $data as $line ) {
462-
$data_row = array();
463-
foreach ( $line as $header => $value ) {
464-
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
465-
if ( in_array( $header, $headers ) ) {
466-
$data_row[] = $value;
467-
}
468-
}
469-
$this->_data[] = $this->_normalizeData( $data_row );
470-
}
471-
472-
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Data found for %s = %s', $this->_url, print_r( $this->_data, true ) ), 'debug', __FILE__, __LINE__ );
473-
474-
return true;
475-
}
476-
477-
/**
478-
* Fetches information from source, parses it and builds series and data arrays.
479-
*
480-
* @since 1.0.0
481-
*
482-
* @access public
483-
* @return boolean TRUE on success, otherwise FALSE.
484-
*/
485-
public function fetch() {
486-
$this->_fetchSeries();
487-
$this->_fetchData();
488-
return true;
489-
}
490-
491423
/**
492424
* Refresh the data for the provided series.
493425
*
@@ -497,7 +429,7 @@ public function fetch() {
497429
*/
498430
public function refresh( $series ) {
499431
$this->_series = $series;
500-
$this->_fetchData();
432+
$this->fetch();
501433
return true;
502434
}
503435

0 commit comments

Comments
 (0)