Skip to content

Commit cc4cee2

Browse files
Merge branch '3.2.0' of https://github.com/codeinwp/visualizer into issue-315
2 parents 12dba61 + 810da3d commit cc4cee2

23 files changed

+1303
-91
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11

2+
### v3.1.3 - 2019-02-24
3+
**Changes:**
4+
* Fix issue with changing column settings of the last column in table chart
5+
* Add support for query language to get subset of data from Google Spreadsheet
6+
* Fix conflict with jquery 3.3.x
7+
* Migrated PHPExcel to PhpSpreadsheet
8+
* Front end action 'print' should print the chart and fall back to printing the data
9+
* Fix issue with table chart not showing in IE
10+
* Fix issue with multiple instances of same chart not showing
11+
* Fix issue with date type column does not work with Combo charts
12+
* Tested with WP 5.1
13+
214
### v3.1.2 - 2018-12-06
315
**Changes:**
416
* Fix bug "Warning: A non-numeric value encountered"

classes/Visualizer/Module.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ private function _getCSV( $rows, $filename ) {
231231
}
232232
rewind( $fp );
233233
$csv = '';
234+
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
234235
while ( ( $array = fgetcsv( $fp ) ) !== false ) {
235236
if ( strlen( $csv ) > 0 ) {
236237
$csv .= PHP_EOL;
@@ -311,6 +312,12 @@ private function _getHTML( $rows ) {
311312
$table = '<table class="visualizer-print">';
312313
$index = 0;
313314
foreach ( $rows as $row ) {
315+
// skip the data type row.
316+
if ( 1 === $index ) {
317+
$index++;
318+
continue;
319+
}
320+
314321
$table .= '<tr>';
315322
foreach ( $row as $col ) {
316323
if ( $index === 0 ) {
@@ -432,6 +439,7 @@ protected function get_user_customization_js() {
432439
}
433440

434441
if ( ! $wp_filesystem->exists( $dir ) ) {
442+
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
435443
if ( ( $done = $wp_filesystem->mkdir( $dir ) ) === false ) {
436444
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ );
437445
return $default;
@@ -441,6 +449,7 @@ protected function get_user_customization_js() {
441449
// if file does not exist, copy.
442450
if ( ! $wp_filesystem->exists( $file ) ) {
443451
$src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' );
452+
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
444453
if ( ( $done = $wp_filesystem->copy( $src, $file ) ) === false ) {
445454
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ );
446455
return $default;

classes/Visualizer/Module/AMP.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
// +----------------------------------------------------------------------+
3+
// | Copyright 2013 Madpixels (email : [email protected]) |
4+
// +----------------------------------------------------------------------+
5+
// | This program is free software; you can redistribute it and/or modify |
6+
// | it under the terms of the GNU General Public License, version 2, as |
7+
// | published by the Free Software Foundation. |
8+
// | |
9+
// | This program is distributed in the hope that it will be useful, |
10+
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11+
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12+
// | GNU General Public License for more details. |
13+
// | |
14+
// | You should have received a copy of the GNU General Public License |
15+
// | along with this program; if not, write to the Free Software |
16+
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
17+
// | MA 02110-1301 USA |
18+
// +----------------------------------------------------------------------+
19+
// | Author: Eugene Manuilov <[email protected]> |
20+
// +----------------------------------------------------------------------+
21+
/**
22+
* The module for all AMP stuff.
23+
*
24+
* @category Visualizer
25+
* @package Module
26+
*
27+
* @since 1.0.0
28+
*/
29+
class Visualizer_Module_AMP extends Visualizer_Module {
30+
31+
const NAME = __CLASS__;
32+
33+
/**
34+
* Constructor.
35+
*
36+
* @since 1.0.0
37+
*
38+
* @access public
39+
*
40+
* @param Visualizer_Plugin $plugin The instance of the plugin.
41+
*/
42+
public function __construct( Visualizer_Plugin $plugin ) {
43+
$this->_addFilter( 'amp_post_template_data', 'addToHeader' );
44+
}
45+
46+
/**
47+
* Add the iframe component to the header.
48+
*/
49+
public function addToHeader( $data ) {
50+
$data['amp_component_scripts'] = array_merge(
51+
$data['amp_component_scripts'],
52+
array(
53+
'amp-iframe' => 'https://cdn.ampproject.org/v0/amp-iframe-latest.js',
54+
)
55+
);
56+
return $data;
57+
}
58+
59+
/**
60+
* Is this an AMP request?
61+
*/
62+
public static function is_amp() {
63+
return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();
64+
}
65+
66+
/**
67+
* Loads the alterview view of the chart.
68+
*/
69+
public function get_chart( $chart, $data, $series, $settings ) {
70+
$view = apply_filters( 'visualizer_amp_view', null, $chart, $data, $series, $settings );
71+
if ( ! is_null( $view ) ) {
72+
return $view;
73+
}
74+
$output = $this->_getDataAs( $chart->ID, 'print' );
75+
return $output['csv'];
76+
}
77+
78+
}

classes/Visualizer/Module/Chart.php

Lines changed: 171 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,156 @@ public function __construct( Visualizer_Plugin $plugin ) {
6161

6262
$this->_addAjaxAction( Visualizer_Plugin::ACTION_FETCH_DB_DATA, 'getQueryData' );
6363
$this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_DB_QUERY, 'saveQuery' );
64+
65+
$this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_GET_ROOTS, 'getJsonRoots' );
66+
$this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_GET_DATA, 'getJsonData' );
67+
$this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_SET_DATA, 'setJsonData' );
68+
$this->_addAjaxAction( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE, 'setJsonSchedule' );
69+
6470
$this->_addAjaxAction( Visualizer_Plugin::ACTION_SAVE_FILTER_QUERY, 'saveFilter' );
71+
72+
}
73+
74+
/**
75+
* Sets the schedule for how JSON-endpoint charts should be updated.
76+
*
77+
* @since ?
78+
*
79+
* @access public
80+
*/
81+
public function setJsonSchedule() {
82+
check_ajax_referer( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION, 'security' );
83+
84+
$chart_id = filter_input(
85+
INPUT_POST,
86+
'chart',
87+
FILTER_VALIDATE_INT,
88+
array(
89+
'options' => array(
90+
'min_range' => 1,
91+
),
92+
)
93+
);
94+
95+
if ( ! $chart_id ) {
96+
wp_send_json_error();
97+
}
98+
99+
$time = filter_input(
100+
INPUT_POST,
101+
'time',
102+
FILTER_VALIDATE_INT,
103+
array(
104+
'options' => array(
105+
'min_range' => -1,
106+
),
107+
)
108+
);
109+
110+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE );
111+
112+
if ( -1 < $time ) {
113+
add_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_SCHEDULE, $time );
114+
}
115+
wp_send_json_success();
116+
}
117+
118+
/**
119+
* Get the root elements for JSON-endpoint.
120+
*
121+
* @since ?
122+
*
123+
* @access public
124+
*/
125+
public function getJsonRoots() {
126+
check_ajax_referer( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION, 'security' );
127+
128+
$params = wp_parse_args( $_POST['params'] );
129+
130+
$source = new Visualizer_Source_Json( $params );
131+
132+
$roots = $source->fetchRoots();
133+
if ( empty( $roots ) ) {
134+
wp_send_json_error();
135+
}
136+
137+
wp_send_json_success( array( 'url' => $params['url'], 'roots' => $roots ) );
65138
}
66139

140+
/**
141+
* Get the data for the JSON-endpoint corresponding to the chosen root.
142+
*
143+
* @since ?
144+
*
145+
* @access public
146+
*/
147+
public function getJsonData() {
148+
check_ajax_referer( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION, 'security' );
149+
150+
$params = wp_parse_args( $_POST['params'] );
151+
152+
$chart_id = $params['chart'];
153+
154+
if ( empty( $chart_id ) ) {
155+
wp_die();
156+
}
157+
158+
$source = new Visualizer_Source_Json( $params );
159+
160+
$data = $source->parse();
161+
if ( empty( $data ) ) {
162+
wp_send_json_error();
163+
}
164+
165+
$data = Visualizer_Render_Layout::show( 'editor-table', $data, $chart_id, 'viz-json-table' );
166+
wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) );
167+
}
168+
169+
/**
170+
* Updates the database with the correct post parameters for JSON-endpoint charts.
171+
*
172+
* @since ?
173+
*
174+
* @access public
175+
*/
176+
public function setJsonData() {
177+
check_ajax_referer( Visualizer_Plugin::ACTION_JSON_SET_DATA . Visualizer_Plugin::VERSION, 'security' );
178+
179+
$params = $_POST;
180+
$chart_id = $_GET['chart'];
181+
182+
if ( empty( $chart_id ) ) {
183+
wp_die();
184+
}
185+
186+
$chart = get_post( $chart_id );
187+
188+
$source = new Visualizer_Source_Json( $params );
189+
$source->fetch();
190+
191+
$content = $source->getData();
192+
$chart->post_content = $content;
193+
wp_update_post( $chart->to_array() );
194+
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
195+
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
196+
update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
197+
update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_URL, $params['url'] );
198+
update_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_ROOT, $params['root'] );
199+
delete_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING );
200+
if ( ! empty( $params['paging'] ) ) {
201+
add_post_meta( $chart->ID, Visualizer_Plugin::CF_JSON_PAGING, $params['paging'] );
202+
}
203+
204+
$render = new Visualizer_Render_Page_Update();
205+
$render->id = $chart->ID;
206+
$render->data = json_encode( $source->getRawData() );
207+
$render->series = json_encode( $source->getSeries() );
208+
$render->render();
209+
210+
defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
211+
}
212+
213+
67214
/**
68215
* Fetches charts from database.
69216
*
@@ -316,7 +463,7 @@ public function renderChartPages() {
316463
wp_register_style( 'visualizer-chosen', VISUALIZER_ABSURL . 'css/lib/chosen.min.css', array(), Visualizer_Plugin::VERSION );
317464

318465
wp_register_style( 'visualizer-frame', VISUALIZER_ABSURL . 'css/frame.css', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION );
319-
wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen' ), Visualizer_Plugin::VERSION, true );
466+
wp_register_script( 'visualizer-frame', VISUALIZER_ABSURL . 'js/frame.js', array( 'visualizer-chosen', 'jquery-ui-accordion' ), Visualizer_Plugin::VERSION, true );
320467
wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
321468
wp_register_script(
322469
'visualizer-render',
@@ -381,20 +528,21 @@ public function renderChartPages() {
381528
private function loadCodeEditorAssets() {
382529
global $wp_version;
383530

531+
$wp_scripts = wp_scripts();
532+
533+
// data tables assets.
534+
wp_register_script( 'visualizer-datatables', '//cdn.datatables.net/v/dt/dt-1.10.18/b-1.5.6/b-colvis-1.5.6/cr-1.5.0/fc-3.2.5/fh-3.1.4/r-2.2.2/sc-2.0.0/sl-1.3.0/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION );
535+
wp_register_style( 'visualizer-datatables', '//cdn.datatables.net/v/dt/dt-1.10.18/b-1.5.6/b-colvis-1.5.6/cr-1.5.0/fc-3.2.5/fh-3.1.4/r-2.2.2/sc-2.0.0/sl-1.3.0/datatables.min.css', array(), Visualizer_Plugin::VERSION );
536+
wp_register_style( 'visualizer-jquery-ui', sprintf( '//ajax.googleapis.com/ajax/libs/jqueryui/%s/themes/smoothness/jquery-ui.css', $wp_scripts->registered['jquery-ui-core']->ver ), array( 'visualizer-datatables' ), Visualizer_Plugin::VERSION );
537+
wp_enqueue_script( 'visualizer-datatables' );
538+
wp_enqueue_style( 'visualizer-jquery-ui' );
539+
384540
if ( ! VISUALIZER_PRO ) {
385541
return;
386542
}
387543

388544
$table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping();
389545

390-
// data tables assets.
391-
wp_register_script( 'visualizer-datatables', '//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION );
392-
wp_register_style( 'visualizer-datatables', '//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css', array(), Visualizer_Plugin::VERSION );
393-
wp_register_style( 'visualizer-datatables-ui', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css', array( 'visualizer-datatables' ), Visualizer_Plugin::VERSION );
394-
395-
wp_enqueue_script( 'visualizer-datatables' );
396-
wp_enqueue_style( 'visualizer-datatables-ui' );
397-
398546
if ( version_compare( $wp_version, '4.9.0', '<' ) ) {
399547
// code mirror assets.
400548
wp_register_script( 'visualizer-codemirror-core', '//codemirror.net/lib/codemirror.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
@@ -485,6 +633,7 @@ private function _handleDataAndSettingsPage() {
485633
'l10n' => array(
486634
'invalid_source' => esc_html__( 'You have entered invalid URL. Please, insert proper URL.', 'visualizer' ),
487635
'loading' => esc_html__( 'Loading...', 'visualizer' ),
636+
'json_error' => esc_html__( 'An error occured in fetching data.', 'visualizer' ),
488637
),
489638
'charts' => array(
490639
'canvas' => $data,
@@ -497,17 +646,25 @@ private function _handleDataAndSettingsPage() {
497646
'nonces' => array(
498647
'permissions' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA ),
499648
'db_get_data' => wp_create_nonce( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION ),
649+
'json_get_roots' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_ROOTS . Visualizer_Plugin::VERSION ),
650+
'json_get_data' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_GET_DATA . Visualizer_Plugin::VERSION ),
651+
'json_set_schedule' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE . Visualizer_Plugin::VERSION ),
500652
),
501653
'actions' => array(
502654
'permissions' => Visualizer_Plugin::ACTION_FETCH_PERMISSIONS_DATA,
503655
'db_get_data' => Visualizer_Plugin::ACTION_FETCH_DB_DATA,
656+
'json_get_roots' => Visualizer_Plugin::ACTION_JSON_GET_ROOTS,
657+
'json_get_data' => Visualizer_Plugin::ACTION_JSON_GET_DATA,
658+
'json_set_schedule' => Visualizer_Plugin::ACTION_JSON_SET_SCHEDULE,
504659
),
505660
),
506661
'db_query' => array(
507662
'tables' => $table_col_mapping,
508663
),
509664
'is_pro' => VISUALIZER_PRO,
510665
'page_type' => 'chart',
666+
'json_tag_separator' => Visualizer_Source_Json::TAG_SEPARATOR,
667+
'json_tag_separator_view' => Visualizer_Source_Json::TAG_SEPARATOR_VIEW,
511668
'is_front' => false,
512669
)
513670
);
@@ -632,6 +789,11 @@ public function uploadData() {
632789
delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE );
633790
}
634791

792+
// delete json related data.
793+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_URL );
794+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT );
795+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING );
796+
635797
$source = null;
636798
$render = new Visualizer_Render_Page_Update();
637799
if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) {

classes/Visualizer/Module/Frontend.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public function renderChart( $atts ) {
220220
}
221221

222222
// in case revisions exist.
223+
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
223224
if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) {
224225
$chart = get_post( $chart->ID );
225226
}
@@ -268,6 +269,11 @@ public function renderChart( $atts ) {
268269

269270
$id = $id . '-' . rand();
270271

272+
$amp = Visualizer_Plugin::instance()->getModule( Visualizer_Module_AMP::NAME );
273+
if ( $amp && $amp->is_amp() ) {
274+
return '<div id="' . $id . '"' . $class . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>';
275+
}
276+
271277
// add chart to the array
272278
$this->_charts[ $id ] = array(
273279
'type' => $type,

0 commit comments

Comments
 (0)