Skip to content

Commit 927789e

Browse files
Merge pull request #952 from Codeinwp/enhancement/pro/323
Compatible Woocommerce data
2 parents 2c42a03 + efc1c2e commit 927789e

File tree

6 files changed

+185
-19
lines changed

6 files changed

+185
-19
lines changed

classes/Visualizer/Module.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ public static function can_show_feature( $feature ) {
732732
public static final function get_features_for_license( $plan ) {
733733
switch ( $plan ) {
734734
case 1:
735-
return array( 'import-wp', 'db-query' );
735+
return array( 'import-wp', 'db-query', 'import-wc-report' );
736736
case 2:
737737
return array( 'schedule-chart', 'chart-permissions' );
738738
}

classes/Visualizer/Module/Chart.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ public function setJsonSchedule() {
135135
)
136136
);
137137

138+
if ( Visualizer_Module::is_pro() ) {
139+
$is_woocommerce_report = filter_input(
140+
INPUT_POST,
141+
'is_woocommerce_report',
142+
FILTER_VALIDATE_BOOLEAN
143+
);
144+
145+
if ( $is_woocommerce_report ) {
146+
update_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true );
147+
} else {
148+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE );
149+
}
150+
}
151+
138152
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE );
139153

140154
if ( -1 < $time ) {
@@ -246,6 +260,14 @@ public function setJsonData() {
246260
add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING, $params['paging'] );
247261
}
248262

263+
if ( Visualizer_Module::is_pro() ) {
264+
if ( ! empty( $params['vz_woo_source'] ) ) {
265+
update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_WOOCOMMERCE_SOURCE, $params['vz_woo_source'] );
266+
} else {
267+
delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_WOOCOMMERCE_SOURCE );
268+
}
269+
}
270+
249271
$time = filter_input(
250272
INPUT_POST,
251273
'time',
@@ -881,6 +903,7 @@ private function _handleDataAndSettingsPage() {
881903
'json_tag_separator' => Visualizer_Source_Json::TAG_SEPARATOR,
882904
'json_tag_separator_view' => Visualizer_Source_Json::TAG_SEPARATOR_VIEW,
883905
'is_front' => false,
906+
'rest_base' => get_rest_url( null, 'wc/v3/reports/' ),
884907
)
885908
);
886909

classes/Visualizer/Module/Frontend.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ public function renderChart( $atts ) {
330330
return '';
331331
}
332332

333+
if ( ! is_admin() && ! empty( $chart_data['is_woocommerce_report'] ) ) {
334+
return '';
335+
}
336+
333337
// in case revisions exist.
334338
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
335339
if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) {
@@ -650,8 +654,9 @@ private function getChartData( $cache_key = '', $chart_id = 0 ) {
650654
// Get chart by ID.
651655
$chart = get_post( $chart_id );
652656
if ( $chart && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type ) {
653-
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
654-
$series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
657+
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
658+
$series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
659+
$is_woocommerce_report = get_post_meta( $chart->ID, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true );
655660

656661
if ( isset( $settings['series'] ) && ! ( count( $settings['series'] ) - count( $series ) > 1 ) ) {
657662
$diff_total_series = abs( count( $settings['series'] ) - count( $series ) );
@@ -662,11 +667,12 @@ private function getChartData( $cache_key = '', $chart_id = 0 ) {
662667
}
663668
}
664669
$chart_data = array(
665-
'chart' => $chart,
666-
'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ),
667-
'settings' => $settings,
668-
'series' => $series,
669-
'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ),
670+
'chart' => $chart,
671+
'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ),
672+
'settings' => $settings,
673+
'series' => $series,
674+
'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ),
675+
'is_woocommerce_report' => $is_woocommerce_report,
670676
);
671677

672678
// Put the results in a transient. Expire after 12 hours.

classes/Visualizer/Plugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ class Visualizer_Plugin {
102102
const PRO_TEASER_TITLE = 'Check PRO version ';
103103

104104
const CF_CHART_CACHE = 'visualizer-chart-cache';
105+
const CF_JSON_WOOCOMMERCE_SOURCE = 'visualizer-woocommerce-source';
106+
const CF_IS_WOOCOMMERCE_SOURCE = 'visualizer-is-woocommerce-source';
105107

106108
/**
107109
* Name of the option for WordPress DB.

classes/Visualizer/Render/Layout.php

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public static function _renderJsonScreen( $args ) {
139139
)
140140
);
141141

142+
$is_wc_source = get_post_meta( $id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true );
142143
$url = get_post_meta( $id, Visualizer_Plugin::CF_JSON_URL, true );
143144
$root = get_post_meta( $id, Visualizer_Plugin::CF_JSON_ROOT, true );
144145
$paging = get_post_meta( $id, Visualizer_Plugin::CF_JSON_PAGING, true );
@@ -171,9 +172,11 @@ public static function _renderJsonScreen( $args ) {
171172
name="url"
172173
value="<?php echo esc_url( $url ); ?>"
173174
placeholder="<?php esc_html_e( 'Please enter the URL', 'visualizer' ); ?>"
174-
class="visualizer-input json-form-element">
175+
class="visualizer-input json-form-element"
176+
<?php echo $is_wc_source ? 'readonly' : ''; ?>
177+
>
175178
<button class="button button-secondary button-small" id="visualizer-json-fetch"><?php esc_html_e( 'Fetch Endpoint', 'visualizer' ); ?></button>
176-
179+
177180
<div class="visualizer-json-subform">
178181
<h3 class="viz-substep <?php echo $headers_open ? 'open' : ''; ?>"><?php _e( 'Headers', 'visualizer' ); ?></h3>
179182
<div class="json-wizard-headers">
@@ -670,7 +673,12 @@ public static function _renderTabBasic( $args ) {
670673
);
671674

672675
// this will allow us to open the correct source tab by default.
673-
$source_of_chart = strtolower( get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true ) );
676+
$source_of_chart = strtolower( get_post_meta( $chart_id, Visualizer_Plugin::CF_SOURCE, true ) );
677+
// Import from woocommerce report.
678+
$is_woocommerce_source = strtolower( get_post_meta( $chart_id, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true ) );
679+
if ( ! empty( $is_woocommerce_source ) ) {
680+
$source_of_chart .= '_wc';
681+
}
674682
// both import from wp and import from db have the same source so we need to differentiate.
675683
$filter_config = get_post_meta( $chart_id, Visualizer_Plugin::CF_FILTER_CONFIG, true );
676684
// if filter config is present, then its import from wp.
@@ -920,6 +928,104 @@ class="dashicons dashicons-lock"></span></h2>
920928
</div>
921929
</li>
922930

931+
<!-- import from WooCommerce -->
932+
<?php
933+
if ( class_exists( 'WooCommerce', false ) ) :
934+
$wc_source = strtolower( get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_WOOCOMMERCE_SOURCE, true ) );
935+
$wc_source_list = apply_filters(
936+
'visualizer_woocommerce_report_endpoints',
937+
array(
938+
array(
939+
'name' => esc_html__( 'Sales', 'visualizer' ),
940+
'endpoint' => esc_attr( 'sales' ),
941+
),
942+
array(
943+
'name' => esc_html__( 'Top Sellers', 'visualizer' ),
944+
'endpoint' => esc_attr( 'top_sellers' ),
945+
),
946+
array(
947+
'name' => esc_html__( 'Coupons Totals', 'visualizer' ),
948+
'endpoint' => esc_attr( 'coupons/totals' ),
949+
),
950+
array(
951+
'name' => esc_html__( 'Customers Totals', 'visualizer' ),
952+
'endpoint' => esc_attr( 'customers/totals' ),
953+
),
954+
array(
955+
'name' => esc_html__( 'Orders Totals', 'visualizer' ),
956+
'endpoint' => esc_attr( 'orders/totals' ),
957+
),
958+
array(
959+
'name' => esc_html__( 'Products Totals', 'visualizer' ),
960+
'endpoint' => esc_attr( 'products/totals' ),
961+
),
962+
array(
963+
'name' => esc_html__( 'Reviews Totals', 'visualizer' ),
964+
'endpoint' => esc_attr( 'reviews/totals' ),
965+
),
966+
)
967+
);
968+
?>
969+
<li class="viz-group visualizer_woocommerce_source<?php echo 'visualizer_source_json_wc' === $source_of_chart ? ' open' : ''; ?> <?php echo apply_filters( 'visualizer_pro_upsell_class', 'only-pro-feature', 'import-wc-report' ); ?> ">
970+
<h2 class="viz-group-title viz-sub-group"><?php _e( 'Import from WooCommerce Reports', 'visualizer' ); ?><span class="dashicons dashicons-lock"></span></h2>
971+
<div class="viz-group-content edit-data-content">
972+
<div>
973+
<p class="viz-group-description"><?php _e( 'You can choose here to import/synchronize your chart data with a WooCommerce report API. For more info check <a href="https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#reports" target="_blank" >this</a> tutorial', 'visualizer' ); ?></p>
974+
<form id="vz-import-woo-report" action="<?php echo $upload_link; ?>" method="post" target="thehole" enctype="multipart/form-data">
975+
<div class="remote-file-section">
976+
<?php
977+
$bttn_label = 'visualizer_source_json_wc' === $source_of_chart ? __( 'Modify Parameters', 'visualizer' ) : __( 'Create Parameters', 'visualizer' );
978+
?>
979+
<p class="viz-group-description"><?php _e( 'How often do you want to check the URL', 'visualizer' ); ?></p>
980+
<select name="time" id="vz-woo-time" class="visualizer-select json-form-element" data-chart="<?php echo $chart_id; ?>">
981+
<?php
982+
$hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true );
983+
$schedules = apply_filters(
984+
'visualizer_chart_schedules', array(
985+
'-1' => __( 'One-time', 'visualizer' ),
986+
),
987+
'json',
988+
$chart_id
989+
);
990+
foreach ( $schedules as $num => $name ) {
991+
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
992+
$extra = $num == $hours ? 'selected' : '';
993+
?>
994+
<option value="<?php echo $num; ?>" <?php echo $extra; ?>><?php echo $name; ?></option>
995+
<?php
996+
}
997+
do_action( 'visualizer_chart_schedules_spl', 'json', $chart_id, 1 );
998+
?>
999+
</select>
1000+
<p class="viz-group-description"><?php _e( 'Select report endpoint', 'visualizer' ); ?></p>
1001+
<select name="vz_woo_source" id="vz-woo-source" class="visualizer-select json-form-element" data-chart="<?php echo $chart_id; ?>">
1002+
<option value=""></option>
1003+
<?php
1004+
if ( ! empty( $wc_source_list ) ) {
1005+
foreach ( $wc_source_list as $api ) {
1006+
if ( isset( $api['name'] ) && $api['endpoint'] ) {
1007+
echo sprintf( '<option value="%2$s" %3$s>%1$s</option>', $api['name'], $api['endpoint'], selected( $wc_source, $api['endpoint'], false ) ); // phpcs:ignore
1008+
}
1009+
}
1010+
}
1011+
?>
1012+
</select>
1013+
</div>
1014+
1015+
<input type="button" id="woo-chart-button" class="button button-secondary show-chart-toggle"
1016+
value="<?php echo $bttn_label; ?>" data-current="chart"
1017+
data-t-filter="<?php _e( 'Show Chart', 'visualizer' ); ?>"
1018+
data-t-chart="<?php echo $bttn_label; ?>"
1019+
<?php empty( $wc_source ) ? 'disabled' : ''; ?>
1020+
>
1021+
<input type="button" id="woo-chart-save-button" class="button button-primary "
1022+
value="<?php _e( 'Save Schedule', 'visualizer' ); ?>" <?php empty( $wc_source ) ? 'disabled' : ''; ?>>
1023+
</form>
1024+
<?php echo apply_filters( 'visualizer_pro_upsell', '', 'import-wc-report' ); ?>
1025+
</div>
1026+
</div>
1027+
</li>
1028+
<?php endif; ?>
9231029
<?php
9241030
$save_query = esc_url(
9251031
add_query_arg(

js/frame.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,17 +383,23 @@
383383
});
384384

385385
// toggle between chart and create/modify parameters
386-
$( '#json-chart-button' ).on( 'click', function(){
386+
$( '#json-chart-button, #woo-chart-button' ).on( 'click', function(){
387387

388388
$('body').off('visualizer:change:action').on('visualizer:change:action', function(e){
389-
var filter_button = $( '#json-chart-button' );
389+
var filter_button = $( '#json-chart-button, #woo-chart-button' );
390390
$( '#visualizer-json-screen' ).css("z-index", "-1").hide();
391391
filter_button.val( filter_button.attr( 'data-t-chart' ) );
392392
filter_button.html( filter_button.attr( 'data-t-chart' ) );
393393
filter_button.attr( 'data-current', 'chart' );
394394
$( '#canvas' ).css("z-index", "1").show();
395+
$( '#vz-import-woo-report' ).find( 'select, input' ).removeAttr( 'disabled' );
395396
});
396397

398+
if ( 'json-chart-button' === $( this ).attr( 'id' ) ) {
399+
$( '#vz-import-woo-report' ).find( 'select, input' ).attr( 'disabled', true );
400+
} else {
401+
$( '#vz-import-woo-report' ).find( 'select, input' ).removeAttr( 'disabled' );
402+
}
397403
$('#content').css('width', 'calc(100% - 100px)');
398404
if( $(this).attr( 'data-current' ) === 'chart'){
399405
// toggle from chart to LHS form
@@ -402,6 +408,15 @@
402408
$(this).attr( 'data-current', 'filter' );
403409
$( '.visualizer-editor-lhs' ).hide();
404410
$( '#visualizer-json-screen' ).css("z-index", "9999").show();
411+
if ( 'woo-chart-button' === $( this ).attr( 'id' ) && '' !== $( '#vz-woo-source:visible' ).val() ) {
412+
$( '#vz-import-json-url' ).val( visualizer.rest_base + $( '#vz-woo-source' ).val() ).attr( 'readonly', true );
413+
} else {
414+
if ( 'undefined' !== $( '#vz-import-json-url' ).attr( 'readonly' ) ) {
415+
$( '#vz-import-json-url' ).val( '' ).removeAttr( 'readonly' ).removeAttr( 'value' );
416+
$('#json-endpoint-form')[0].reset();
417+
$('.visualizer-json-form h3.viz-step:not(.step1)').addClass('ui-state-disabled');
418+
}
419+
}
405420
$( '#canvas' ).hide();
406421
}else{
407422
// toggle from LHS form to chart
@@ -410,14 +425,14 @@
410425
} );
411426

412427
$('body').on('visualizer:json:form:submit', function() {
413-
var filter_button = $( '#json-chart-button' );
428+
var filter_button = $( '#json-chart-button, #woo-chart-button' );
414429
$( '#visualizer-json-screen' ).css("z-index", "-1").hide();
415430
$('#canvas').lock();
416431
filter_button.val( filter_button.attr( 'data-t-chart' ) );
417432
filter_button.html( filter_button.attr( 'data-t-chart' ) );
418433
filter_button.attr( 'data-current', 'chart' );
419434
end_ajax( $( '#visualizer-json-screen' ) );
420-
$( '#canvas' ).css("z-index", "1").show();
435+
$( '#canvas' ).removeClass('visualizer-chart-loaded').css("z-index", "1").show();
421436
});
422437

423438

@@ -512,25 +527,27 @@
512527
}
513528

514529
// populate the form elements that are in the other tabs.
515-
$('#json-conclude-form-helper .json-form-element, #json-endpoint-form .json-form-element, #json-root-form .json-form-element, #vz-import-json .json-form-element').each(function(x, y){
530+
$('#json-conclude-form-helper .json-form-element, #json-endpoint-form .json-form-element, #json-root-form .json-form-element, #vz-import-json .json-form-element, #vz-import-woo-report .json-form-element:not(:disabled)').each(function(x, y){
516531
$('#json-conclude-form').append('<input type="hidden" name="' + y.name + '" value="' + y.value + '">');
517532
});
518533

519534
$('body').trigger('visualizer:json:form:submit');
520535
});
521536

522537
// update the schedule
523-
$('#json-chart-save-button').on('click', function(e){
538+
$('#json-chart-save-button, #woo-chart-save-button').on('click', function(e){
524539
e.preventDefault();
525540
$('#canvas').lock();
541+
var btnID = jQuery( this ).attr( 'id' );
526542
$.ajax({
527543
url : ajaxurl,
528544
method : 'post',
529545
data : {
530546
'action' : visualizer.ajax['actions']['json_set_schedule'],
531547
'security' : visualizer.ajax['nonces']['json_set_schedule'],
532-
'chart' : $('#vz-json-time').attr('data-chart'),
533-
'time' : $('#vz-json-time').val()
548+
'chart' : $('#vz-json-time, #vz-woo-time').attr('data-chart'),
549+
'time' : $('#vz-json-time, #vz-woo-time').val(),
550+
'is_woocommerce_report': 'woo-chart-save-button' === btnID,
534551
},
535552
success : function(data){
536553
// do nothing.
@@ -541,6 +558,18 @@
541558
});
542559
});
543560

561+
// Select WooCommerce report endpoint.
562+
$( '#vz-woo-source' ).on( 'click', function( e ) {
563+
// Trigger change action.
564+
$( 'body' ).trigger( 'visualizer:change:action' );
565+
566+
if ( '' !== $( this ).val() ) {
567+
$( '#woo-chart-button, #woo-chart-save-button' ).removeAttr( 'disabled' );
568+
} else {
569+
$( '#woo-chart-button, #woo-chart-save-button' ).attr( 'disabled', true );
570+
}
571+
} );
572+
544573
}
545574

546575
function init_editor_table() {

0 commit comments

Comments
 (0)