Skip to content

Commit 7142b90

Browse files
Merge pull request #406 from Codeinwp/development
Add support for charts in AMP requests Add support to show charts from JSON/REST endpoints Fix loading of Google Visualization javascript files Add simple editors for editing chart data Tested up to WP 5.2
2 parents 6800d24 + 79b0917 commit 7142b90

34 files changed

+1802
-160
lines changed

classes/Visualizer/Gutenberg/Block.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Visualizer_Gutenberg_Block {
4646
* Returns an instance of this class.
4747
*/
4848
public static function get_instance() {
49-
if ( null == self::$instance ) {
49+
if ( null === self::$instance ) {
5050
self::$instance = new Visualizer_Gutenberg_Block();
5151
}
5252
return self::$instance;
@@ -297,19 +297,19 @@ public function format_chart_data( $data, $series ) {
297297
continue;
298298
}
299299

300-
if ( $row['type'] == 'number' ) {
300+
if ( $row['type'] === 'number' ) {
301301
foreach ( $data as $o => $col ) {
302302
$data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
303303
}
304304
}
305305

306-
if ( $row['type'] == 'boolean' ) {
306+
if ( $row['type'] === 'boolean' ) {
307307
foreach ( $data as $o => $col ) {
308308
$data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_validate( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
309309
}
310310
}
311311

312-
if ( $row['type'] == 'timeofday' ) {
312+
if ( $row['type'] === 'timeofday' ) {
313313
foreach ( $data as $o => $col ) {
314314
$date = new DateTime( '1984-03-16T' . $col[ $i ] );
315315
if ( $date ) {
@@ -323,7 +323,7 @@ public function format_chart_data( $data, $series ) {
323323
}
324324
}
325325

326-
if ( $row['type'] == 'string' ) {
326+
if ( $row['type'] === 'string' ) {
327327
foreach ( $data as $o => $col ) {
328328
$data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
329329
}

classes/Visualizer/Module.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function __construct( Visualizer_Plugin $plugin ) {
6666

6767
$this->_addFilter( Visualizer_Plugin::FILTER_UNDO_REVISIONS, 'undoRevisions', 10, 2 );
6868
$this->_addFilter( Visualizer_Plugin::FILTER_HANDLE_REVISIONS, 'handleExistingRevisions', 10, 2 );
69+
$this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_DATA_AS, 'getDataAs', 10, 3 );
6970

7071
}
7172

@@ -147,6 +148,15 @@ protected function _addShortcode( $tag, $method ) {
147148
return $this;
148149
}
149150

151+
/**
152+
* A wrapper around the actual function _getDataAs. This function is invoked as a filter.
153+
*
154+
* @since 3.2.0
155+
*/
156+
public function getDataAs( $final, $chart_id, $type ) {
157+
return $this->_getDataAs( $chart_id, $type );
158+
}
159+
150160
/**
151161
* Extracts the data for a chart and prepares it for the given type.
152162
*
@@ -159,7 +169,7 @@ public function _getDataAs( $chart_id, $type ) {
159169
$success = false;
160170
if ( $chart_id ) {
161171
$chart = get_post( $chart_id );
162-
$success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
172+
$success = $chart && $chart->post_type === Visualizer_Plugin::CPT_VISUALIZER;
163173
}
164174
if ( $success ) {
165175
$settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
@@ -223,14 +233,16 @@ public function _getDataAs( $chart_id, $type ) {
223233
private function _getCSV( $rows, $filename ) {
224234
$filename .= '.csv';
225235

236+
$bom = chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF );
226237
$fp = tmpfile();
227238
// support for MS Excel
228-
fprintf( $fp, $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ) );
239+
fprintf( $fp, $bom );
229240
foreach ( $rows as $row ) {
230241
fputcsv( $fp, $row );
231242
}
232243
rewind( $fp );
233244
$csv = '';
245+
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
234246
while ( ( $array = fgetcsv( $fp ) ) !== false ) {
235247
if ( strlen( $csv ) > 0 ) {
236248
$csv .= PHP_EOL;
@@ -242,6 +254,7 @@ private function _getCSV( $rows, $filename ) {
242254
return array(
243255
'csv' => $csv,
244256
'name' => $filename,
257+
'string' => str_replace( $bom, '', $csv ),
245258
);
246259
}
247260

@@ -280,6 +293,7 @@ private function _getExcel( $rows, $filename ) {
280293
return array(
281294
'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ),
282295
'name' => $filename,
296+
'raw' => base64_encode( $xlsData ),
283297
);
284298
}
285299

@@ -311,6 +325,12 @@ private function _getHTML( $rows ) {
311325
$table = '<table class="visualizer-print">';
312326
$index = 0;
313327
foreach ( $rows as $row ) {
328+
// skip the data type row.
329+
if ( 1 === $index ) {
330+
$index++;
331+
continue;
332+
}
333+
314334
$table .= '<tr>';
315335
foreach ( $row as $col ) {
316336
if ( $index === 0 ) {
@@ -432,6 +452,7 @@ protected function get_user_customization_js() {
432452
}
433453

434454
if ( ! $wp_filesystem->exists( $dir ) ) {
455+
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
435456
if ( ( $done = $wp_filesystem->mkdir( $dir ) ) === false ) {
436457
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ );
437458
return $default;
@@ -441,6 +462,7 @@ protected function get_user_customization_js() {
441462
// if file does not exist, copy.
442463
if ( ! $wp_filesystem->exists( $file ) ) {
443464
$src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' );
465+
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
444466
if ( ( $done = $wp_filesystem->copy( $src, $file ) ) === false ) {
445467
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ );
446468
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/Admin.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function restoreRevision( $post_id, $revision_id ) {
133133
* @access public
134134
*/
135135
public function init() {
136+
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
136137
if ( current_user_can( 'edit_posts' ) && current_user_can( 'edit_pages' ) && 'true' == get_user_option( 'rich_editing' ) ) {
137138
$this->_addFilter( 'mce_external_languages', 'add_tinymce_lang', 10, 1 );
138139
$this->_addFilter( 'mce_external_plugins', 'tinymce_plugin', 10, 1 );
@@ -465,7 +466,7 @@ private static function handleDeprecatedCharts( $types, $enabledOnly, $get2Darra
465466
*/
466467
public function renderTemplates() {
467468
global $pagenow;
468-
if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow ) {
469+
if ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) {
469470
return;
470471
}
471472
$render = new Visualizer_Render_Templates();
@@ -485,7 +486,7 @@ public function renderTemplates() {
485486
* @param string $suffix The current page suffix.
486487
*/
487488
public function enqueueLibraryScripts( $suffix ) {
488-
if ( $suffix == $this->_libraryPage ) {
489+
if ( $suffix === $this->_libraryPage ) {
489490
wp_enqueue_style( 'visualizer-library', VISUALIZER_ABSURL . 'css/library.css', array(), Visualizer_Plugin::VERSION );
490491
$this->_addFilter( 'media_upload_tabs', 'setupVisualizerTab' );
491492
wp_enqueue_media();
@@ -579,7 +580,7 @@ private function getQuery() {
579580
);
580581
// add chart type filter to the query arguments
581582
$filter = filter_input( INPUT_GET, 'type' );
582-
if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) {
583+
if ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) {
583584
$query_args['meta_query'] = array(
584585
array(
585586
'key' => Visualizer_Plugin::CF_CHART_TYPE,
@@ -631,7 +632,7 @@ public function renderLibraryPage() {
631632
);
632633
// add chart type filter to the query arguments
633634
$filter = filter_input( INPUT_GET, 'type' );
634-
if ( ! ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes() ) ) ) {
635+
if ( ! ( $filter && in_array( $filter, Visualizer_Plugin::getChartTypes(), true ) ) ) {
635636
$filter = 'all';
636637
}
637638

@@ -705,6 +706,7 @@ public function renderLibraryPage() {
705706
),
706707
),
707708
'page_type' => 'library',
709+
'is_front' => false,
708710
)
709711
);
710712
// render library page
@@ -738,7 +740,7 @@ public function renderLibraryPage() {
738740
* @return array Updated array of action links.
739741
*/
740742
public function getPluginActionLinks( $links, $file ) {
741-
if ( $file == plugin_basename( VISUALIZER_BASEFILE ) ) {
743+
if ( $file === plugin_basename( VISUALIZER_BASEFILE ) ) {
742744
array_unshift(
743745
$links,
744746
sprintf(
@@ -765,7 +767,7 @@ public function getPluginActionLinks( $links, $file ) {
765767
* @return array Updated array of plugin meta links.
766768
*/
767769
public function getPluginMetaLinks( $plugin_meta, $plugin_file ) {
768-
if ( $plugin_file == plugin_basename( VISUALIZER_BASEFILE ) ) {
770+
if ( $plugin_file === plugin_basename( VISUALIZER_BASEFILE ) ) {
769771
// knowledge base link
770772
$plugin_meta[] = sprintf(
771773
'<a href="https://github.com/codeinwp/visualizer/wiki" target="_blank">%s</a>',

0 commit comments

Comments
 (0)