Skip to content

Commit cd5736e

Browse files
Merge pull request #550 from contactashish13/issue-481
JSON import: Provide some support for authentication out of the box
2 parents 02e90a5 + 8d4652a commit cd5736e

File tree

10 files changed

+295
-98
lines changed

10 files changed

+295
-98
lines changed

classes/Visualizer/Module.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,24 @@ public function __construct( Visualizer_Plugin $plugin ) {
6767
$this->_addFilter( Visualizer_Plugin::FILTER_UNDO_REVISIONS, 'undoRevisions', 10, 2 );
6868
$this->_addFilter( Visualizer_Plugin::FILTER_HANDLE_REVISIONS, 'handleExistingRevisions', 10, 2 );
6969
$this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_DATA_AS, 'getDataAs', 10, 3 );
70+
register_shutdown_function( array($this, 'onShutdown') );
7071

7172
}
7273

74+
/**
75+
* Register a shutdown hook to catch fatal errors.
76+
*
77+
* @since ?
78+
*
79+
* @access public
80+
*/
81+
public function onShutdown() {
82+
$error = error_get_last();
83+
if ( $error['type'] === E_ERROR && false !== strpos( $error['file'], 'Visualizer/' ) ) {
84+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Critical error %s', print_r( $error, true ) ), 'error', __FILE__, __LINE__ );
85+
}
86+
}
87+
7388
/**
7489
* Registers an action hook.
7590
*

classes/Visualizer/Module/Admin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ public function renderLibraryPage() {
787787
if ( $settings ) {
788788
unset( $settings['height'], $settings['width'], $settings['chartArea'] );
789789
}
790+
790791
$series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
791792
$data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
792793

classes/Visualizer/Module/Chart.php

Lines changed: 16 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;
@@ -196,6 +197,17 @@ public function setJsonData() {
196197
update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
197198
update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, $params['url'] );
198199
update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] );
200+
201+
delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS );
202+
$headers = array( 'method' => $params['method'] );
203+
if ( ! empty( $params['auth'] ) ) {
204+
$headers['auth'] = $params['auth'];
205+
} elseif ( ! empty( $params['username'] ) && ! empty( $params['password'] ) ) {
206+
$headers['auth'] = array( 'username' => $params['username'], 'password' => $params['password'] );
207+
}
208+
209+
add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_HEADERS, $headers );
210+
199211
delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING );
200212
if ( ! empty( $params['paging'] ) ) {
201213
add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING, $params['paging'] );
@@ -679,6 +691,7 @@ private function _handleDataAndSettingsPage() {
679691
'invalid_source' => esc_html__( 'You have entered an invalid URL. Please provide a valid URL.', 'visualizer' ),
680692
'loading' => esc_html__( 'Loading...', 'visualizer' ),
681693
'json_error' => esc_html__( 'An error occured in fetching data.', 'visualizer' ),
694+
'select_columns' => esc_html__( 'Please select a few columns to include in the chart.', 'visualizer' ),
682695
),
683696
'charts' => array(
684697
'canvas' => $data,
@@ -955,6 +968,7 @@ public function uploadData() {
955968
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL );
956969
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT );
957970
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING );
971+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_HEADERS );
958972

959973
// delete last error
960974
delete_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR );

classes/Visualizer/Plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class Visualizer_Plugin {
7373
const CF_JSON_ROOT = 'visualizer-json-root';
7474
const CF_JSON_SCHEDULE = 'visualizer-json-schedule';
7575
const CF_JSON_PAGING = 'visualizer-json-paging';
76+
const CF_JSON_HEADERS = 'visualizer-json-headers';
7677

7778
const ACTION_SAVE_FILTER_QUERY = 'visualizer-save-filter-query';
7879

classes/Visualizer/Render/Layout.php

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ public static function _renderJsonScreen( $args ) {
137137
$url = get_post_meta( $id, Visualizer_Plugin::CF_JSON_URL, true );
138138
$root = get_post_meta( $id, Visualizer_Plugin::CF_JSON_ROOT, true );
139139
$paging = get_post_meta( $id, Visualizer_Plugin::CF_JSON_PAGING, true );
140-
140+
$headers = get_post_meta( $id, Visualizer_Plugin::CF_JSON_HEADERS, true );
141+
if ( empty( $headers['method'] ) ) {
142+
$headers['method'] = 'get';
143+
}
144+
$methods = apply_filters( 'visualizer_json_request_methods', array( 'GET', 'POST' ) );
141145
?>
142146
<div id="visualizer-json-screen" style="display: none">
143147
<div class="visualizer-json-form">
@@ -146,7 +150,7 @@ public static function _renderJsonScreen( $args ) {
146150
<form id="json-endpoint-form">
147151
<div class="json-wizard-hints">
148152
<ul class="info">
149-
<li><?php echo sprintf( __( 'If you want to add authentication, add headers to the endpoint or change the request in any way, please refer to our document %1$shere%2$s.', 'visualizer' ), '<a href="https://docs.themeisle.com/article/1043-visualizer-how-to-extend-rest-endpoints-with-json-response" target="_blank">', '</a>' ); ?></li>
153+
<li><?php echo sprintf( __( 'If you want to add authentication or headers to the endpoint or change the request in any way, please refer to our document %1$shere%2$s.', 'visualizer' ), '<a href="https://docs.themeisle.com/article/1043-visualizer-how-to-extend-rest-endpoints-with-json-response" target="_blank">', '</a>' ); ?></li>
150154
</ul>
151155
</div>
152156

@@ -156,15 +160,67 @@ public static function _renderJsonScreen( $args ) {
156160
name="url"
157161
value="<?php echo esc_url( $url ); ?>"
158162
placeholder="<?php esc_html_e( 'Please enter the URL', 'visualizer' ); ?>"
159-
class="visualizer-input">
163+
class="visualizer-input json-form-element">
160164
<button class="button button-secondary button-small" id="visualizer-json-fetch"><?php esc_html_e( 'Fetch Endpoint', 'visualizer' ); ?></button>
165+
166+
<div class="visualizer-json-subform">
167+
<h3 class="viz-substep"><?php _e( 'Headers', 'visualizer' ); ?></h3>
168+
<div class="json-wizard-headers">
169+
<div class="json-wizard-header">
170+
<div><?php _e( 'Request Type', 'visualizer' ); ?></div>
171+
<div>
172+
<select name="method" class="json-form-element">
173+
<?php foreach ( $methods as $method ) { ?>
174+
<option value="<?php echo $method; ?>" <?php selected( $headers['method'], $method ); ?>><?php echo $method; ?></option>
175+
<?php } ?>
176+
</select>
177+
</div>
178+
</div>
179+
<div class="json-wizard-header">
180+
<div><?php _e( 'Credentials', 'visualizer' ); ?></div>
181+
<div>
182+
<input
183+
type="text"
184+
id="vz-import-json-username"
185+
name="username"
186+
value="<?php echo ( array_key_exists( 'auth', $headers ) && array_key_exists( 'username', $headers['auth'] ) ? $headers['auth']['username'] : '' ); ?>"
187+
placeholder="<?php esc_html_e( 'Username/Access Key', 'visualizer' ); ?>"
188+
class="json-form-element">
189+
&
190+
<input
191+
type="password"
192+
id="vz-import-json-password"
193+
name="password"
194+
value="<?php echo ( array_key_exists( 'auth', $headers ) && array_key_exists( 'password', $headers['auth'] ) ? $headers['auth']['password'] : '' ); ?>"
195+
placeholder="<?php esc_html_e( 'Password/Secret Key', 'visualizer' ); ?>"
196+
class="json-form-element">
197+
</div>
198+
</div>
199+
<div class="json-wizard-header">
200+
<div></div>
201+
<div><?php esc_html_e( 'OR', 'visualizer' ); ?></div>
202+
</div>
203+
<div class="json-wizard-header">
204+
<div><?php esc_html_e( 'Authorization', 'visualizer' ); ?></div>
205+
<div>
206+
<input
207+
type="text"
208+
id="vz-import-json-auth"
209+
name="auth"
210+
value="<?php echo ( ! empty( $headers ) && array_key_exists( 'auth', $headers ) ? $headers['auth']['auth'] : '' ); ?>"
211+
placeholder="<?php esc_html_e( 'e.g. SharedKey <AccountName>:<Signature>', 'visualizer' ); ?>"
212+
class="visualizer-input json-form-element">
213+
</div>
214+
</div>
215+
</div>
216+
</div>
161217
</form>
162218
</div>
163219
<h3 class="viz-step step2 <?php echo empty( $url ) ? 'ui-state-disabled' : ''; ?> json-root-form"><?php _e( 'STEP 2: Choose the JSON root', 'visualizer' ); ?></h3>
164220
<div>
165221
<form id="json-root-form">
166222
<input type="hidden" name="chart" value="<?php echo $id; ?>">
167-
<select name="root" id="vz-import-json-root">
223+
<select name="root" id="vz-import-json-root" class="json-form-element">
168224
<?php
169225
if ( ! empty( $root ) ) {
170226
?>
@@ -173,7 +229,6 @@ class="visualizer-input">
173229
}
174230
?>
175231
</select>
176-
<input type="hidden" name="url" value="<?php echo $url; ?>">
177232
<button class="button button-secondary button-small" id="visualizer-json-parse"><?php esc_html_e( 'Parse Endpoint', 'visualizer' ); ?></button>
178233
</form>
179234
</div>
@@ -182,12 +237,12 @@ class="visualizer-input">
182237
<form id="json-conclude-form-helper">
183238
<div class="<?php echo apply_filters( 'visualizer_pro_upsell_class', 'only-pro-feature' ); ?>">
184239
<div class="json-pagination">
185-
<select name="paging" id="vz-import-json-paging" class="json-form-element" data-template='<?php echo sprintf( 'Get first %d pages using %s', apply_filters( 'visualizer_json_fetch_pages', 5, $url ), '?' ); ?>'>
186-
<option value="0" class="static"><?php _e( 'Don\'t use pagination', 'visualizer' ); ?></option>
240+
<select name="paging" id="vz-import-json-paging" class="json-form-element" data-template='<?php echo sprintf( 'Get results from the first %d pages using %s', apply_filters( 'visualizer_json_fetch_pages', 5, $url ), '?' ); ?>'>
241+
<option value="0" class="static"><?php _e( 'Get results from the first page only', 'visualizer' ); ?></option>
187242
<?php
188243
if ( ! empty( $paging ) ) {
189244
?>
190-
<option value="<?php echo esc_attr( $paging ); ?>"><?php echo sprintf( 'Get first %d pages using %s', apply_filters( 'visualizer_json_fetch_pages', 5, $url ), str_replace( Visualizer_Source_Json::TAG_SEPARATOR, Visualizer_Source_Json::TAG_SEPARATOR_VIEW, $paging ) ); ?></option>
245+
<option value="<?php echo esc_attr( $paging ); ?>"><?php echo sprintf( 'Get results from the first %d pages using %s', apply_filters( 'visualizer_json_fetch_pages', 5, $url ), str_replace( Visualizer_Source_Json::TAG_SEPARATOR, Visualizer_Source_Json::TAG_SEPARATOR_VIEW, $paging ) ); ?></option>
191246
<?php
192247
}
193248
?>
@@ -210,10 +265,6 @@ class="visualizer-input">
210265
<h3 class="viz-step step4 ui-state-disabled"><?php _e( 'STEP 4: Select the data to display in the chart', 'visualizer' ); ?></h3>
211266
<div>
212267
<form id="json-conclude-form" action="<?php echo $action; ?>" method="post" target="thehole">
213-
<input type="hidden" name="url">
214-
<input type="hidden" name="root">
215-
<input type="hidden" name="chart" value="<?php echo $id; ?>">
216-
217268
<div class="json-wizard-hints html-table-editor-hints">
218269
<ul class="info">
219270
<li><?php _e( 'Select whether to include the data in the chart. Each column selected will form one series.', 'visualizer' ); ?></li>
@@ -357,7 +408,7 @@ public static function _renderEditorTable( $args ) {
357408
echo '<select name="type[]">';
358409
} else {
359410
echo '<input name="header[]" type="hidden" value="' . $header . '">';
360-
echo '<select name="type[' . $header . ']">';
411+
echo '<select name="type[' . $header . ']" class="viz-select-data-type">';
361412
}
362413
echo '<option value="" title="' . __( 'Exclude from chart', 'visualizer' ) . '">' . __( 'Exclude', 'visualizer' ) . '</option>';
363414
echo '<option value="0" disabled title="' . __( 'Include in chart and select data type', 'visualizer' ) . '">--' . __( 'OR', 'visualizer' ) . '--</option>';
@@ -376,10 +427,15 @@ public static function _renderEditorTable( $args ) {
376427
if ( array_key_exists( 'data', $data ) ) {
377428
$data = $data['data'];
378429
}
430+
379431
foreach ( $data as $row ) {
380432
echo '<tr>';
381433
echo '<th>' . __( 'Value', 'visualizer' ) . '</th>';
382434
$index = 0;
435+
if ( empty( $row ) ) {
436+
echo '<td></td>';
437+
continue;
438+
}
383439
foreach ( array_values( $row ) as $value ) {
384440
if ( $editable_data ) {
385441
echo '<td><input type="text" name="data' . $index++ . '[]" value="' . esc_attr( $value ) . '"></td>';

classes/Visualizer/Render/Page/Data.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ protected function _renderSidebarContent() {
200200
?>
201201
</div>
202202

203+
<p class="viz-group-description viz-info-msg json-chart-msg" style="display: none"><?php echo sprintf( __( 'Please make sure you click \'Save & Show Chart\' on the left before you click \'Show Chart\' below.', 'visualizer' ) ); ?></p>
203204
<input type="button" id="json-chart-button" class="button button-secondary show-chart-toggle"
204205
value="<?php echo $bttn_label; ?>" data-current="chart"
205206
data-t-filter="<?php _e( 'Show Chart', 'visualizer' ); ?>"

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
}

0 commit comments

Comments
 (0)