PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 1.5.3
Visualizer – Tables & Charts Manager with Built-in AI Generator v1.5.3
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
← All changes | classes/Visualizer/Module/Frontend.php +156 -294 3.0.71.5.3 View file →
@@ -1,294 +1,156 @@
1 -<?php
2 -
3 -// +----------------------------------------------------------------------+
4 -// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) |
5 -// +----------------------------------------------------------------------+
6 -// | This program is free software; you can redistribute it and/or modify |
7 -// | it under the terms of the GNU General Public License, version 2, as |
8 -// | published by the Free Software Foundation. |
9 -// | |
10 -// | This program is distributed in the hope that it will be useful, |
11 -// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 -// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 -// | GNU General Public License for more details. |
14 -// | |
15 -// | You should have received a copy of the GNU General Public License |
16 -// | along with this program; if not, write to the Free Software |
17 -// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
18 -// | MA 02110-1301 USA |
19 -// +----------------------------------------------------------------------+
20 -// | Author: Eugene Manuilov <eugene@manuilov.org> |
21 -// +----------------------------------------------------------------------+
22 -/**
23 - * Frontend module class.
24 - *
25 - * @category Visualizer
26 - * @package Module
27 - *
28 - * @since 1.0.0
29 - */
30 -class Visualizer_Module_Frontend extends Visualizer_Module {
31 -
32 - const NAME = __CLASS__;
33 -
34 - /**
35 - * The array of charts to render.
36 - *
37 - * @since 1.0.0
38 - *
39 - * @access private
40 - * @var array
41 - */
42 - private $_charts = array();
43 -
44 - /**
45 - * Constructor.
46 - *
47 - * @since 1.0.0
48 - * @uses add_filter() To add "do_shortcode" hook for "widget_text" and "term_description" filters.
49 - *
50 - * @access public
51 - * @param Visualizer_Plugin $plugin The instance of the plugin.
52 - */
53 - public function __construct( Visualizer_Plugin $plugin ) {
54 - parent::__construct( $plugin );
55 -
56 - $this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' );
57 - $this->_addFilter( 'visualizer_get_language', 'getLanguage' );
58 - $this->_addShortcode( 'visualizer', 'renderChart' );
59 -
60 - // add do_shortocde hook for widget_text filter
61 - if ( ! has_filter( 'widget_text', 'do_shortcode' ) ) {
62 - add_filter( 'widget_text', 'do_shortcode' );
63 - }
64 -
65 - // add do_shortcode hook for term_description filter
66 - if ( ! has_filter( 'term_description', 'do_shortcode' ) ) {
67 - add_filter( 'term_description', 'do_shortcode' );
68 - }
69 -
70 - add_action( 'rest_api_init', array( $this, 'endpoint_register' ) );
71 - }
72 -
73 - /**
74 - * Returns the language/locale.
75 - */
76 - function getLanguage( $dummy, $only_language ) {
77 - return $this->get_language();
78 - }
79 -
80 -
81 - /**
82 - * Registers the endpoints
83 - */
84 - function endpoint_register() {
85 - register_rest_route(
86 - 'visualizer/v' . VISUALIZER_REST_VERSION,
87 - '/action/(?P<chart>\d+)/(?P<type>.+)/',
88 - array(
89 - 'methods' => array( 'GET', 'POST' ),
90 - 'callback' => array( $this, 'perform_action' ),
91 - )
92 - );
93 - }
94 -
95 - /**
96 - * All possible actions and their labels
97 - *
98 - * @access private
99 - */
100 - private function get_actions() {
101 - return apply_filters(
102 - 'visualizer_action_buttons', array(
103 - 'print' => __( 'Print', 'visualizer' ),
104 - 'csv' => __( 'CSV', 'visualizer' ),
105 - 'xls' => __( 'Excel', 'visualizer' ),
106 - 'copy' => __( 'Copy', 'visualizer' ),
107 - )
108 - );
109 - }
110 -
111 - /**
112 - * The print button
113 - *
114 - * @since 1.0.0
115 - *
116 - * @access public
117 - */
118 - public function perform_action( WP_REST_Request $params ) {
119 - $chart_id = filter_var( sanitize_text_field( $params['chart'] ), FILTER_VALIDATE_INT );
120 - $type = sanitize_text_field( $params['type'] );
121 - $data = null;
122 -
123 - if ( ! $chart_id ) {
124 - return new WP_REST_Response( array( 'error' => new WP_Error( __( 'Invalid chart ID', 'visualizer' ) ) ) );
125 - }
126 -
127 - if ( ! $type ) {
128 - return new WP_REST_Response( array( 'error' => new WP_Error( __( 'Invalid action', 'visualizer' ) ) ) );
129 - }
130 -
131 - switch ( $type ) {
132 - case 'print':
133 - $data = $this->_getDataAs( $chart_id, 'print' );
134 - break;
135 - case 'csv':
136 - $data = $this->_getDataAs( $chart_id, 'csv' );
137 - break;
138 - case 'xls':
139 - $data = $this->_getDataAs( $chart_id, 'xls' );
140 - break;
141 - default:
142 - $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this );
143 - break;
144 - }
145 -
146 - return new WP_REST_Response( array( 'data' => $data ) );
147 - }
148 -
149 - /**
150 - * Registers scripts for charts rendering.
151 - *
152 - * @since 1.0.0
153 - * @uses wp_register_script To register javascript file.
154 - *
155 - * @access public
156 - */
157 - public function enqueueScripts() {
158 - wp_register_script( 'visualizer-google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
159 - wp_register_script( 'visualizer-google-jsapi-old', '//www.google.com/jsapi', array( 'visualizer-google-jsapi-new' ), null, true );
160 - wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi-old', 'jquery' ), Visualizer_Plugin::VERSION, true );
161 - wp_register_script( 'visualizer-clipboardjs', VISUALIZER_ABSURL . 'js/lib/clipboardjs/clipboard.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true );
162 - wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION );
163 - do_action( 'visualizer_pro_frontend_load_resources' );
164 - }
165 -
166 - /**
167 - * Returns placeholder for chart according to visualizer shortcode attributes.
168 - *
169 - * @since 1.0.0
170 - * @uses shortcode_atts() To parse income shortocdes.
171 - * @uses apply_filters() To filter chart's data and series arrays.
172 - * @uses get_post_meta() To fetch chart's meta information.
173 - * @uses wp_enqueue_script() To enqueue charts render script.
174 - * @uses wp_localize_script() To add chart data to the page inline script.
175 - *
176 - * @access public
177 - * @param array $atts The array of shortcode attributes.
178 - */
179 - public function renderChart( $atts ) {
180 - global $wp_version;
181 - $atts = shortcode_atts(
182 - array(
183 - 'id' => false, // chart id
184 - 'class' => false, // chart class
185 - 'series' => false, // series filter hook
186 - 'data' => false, // data filter hook
187 - 'settings' => false, // data filter hook
188 - ), $atts
189 - );
190 -
191 - // if empty id or chart does not exists, then return empty string
192 - if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
193 - return '';
194 - }
195 -
196 - if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) {
197 - return '';
198 - }
199 -
200 - $id = 'visualizer-' . $atts['id'];
201 - $defaultClass = 'visualizer-front';
202 - $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] );
203 - $class = $defaultClass . ' ' . $class . ' ' . 'visualizer-front-' . $atts['id'];
204 - $class = ! empty( $class ) ? ' class="' . trim( $class ) . '"' : '';
205 -
206 - $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
207 -
208 - // faetch and update settings
209 - $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
210 - if ( empty( $settings['height'] ) ) {
211 - $settings['height'] = '400';
212 - }
213 -
214 - // handle series filter hooks
215 - $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
216 - if ( ! empty( $atts['series'] ) ) {
217 - $series = apply_filters( $atts['series'], $series, $chart->ID, $type );
218 - }
219 - // handle settings filter hooks
220 - $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
221 - if ( ! empty( $atts['settings'] ) ) {
222 - $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type );
223 - }
224 -
225 - // handle data filter hooks
226 - $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
227 - if ( ! empty( $atts['data'] ) ) {
228 - $data = apply_filters( $atts['data'], $data, $chart->ID, $type );
229 - }
230 -
231 - $id = $id . '-' . rand();
232 - $arguments = array( '', $id, $settings );
233 - apply_filters_ref_array( 'visualizer_pro_inline_css', array( &$arguments ) );
234 - $css = $arguments[0];
235 - $settings = $arguments[2];
236 -
237 - // add chart to the array
238 - $this->_charts[ $id ] = array(
239 - 'type' => $type,
240 - 'series' => $series,
241 - 'settings' => $settings,
242 - 'data' => $data,
243 - );
244 -
245 - // enqueue visualizer render and update render localizations
246 - wp_enqueue_script( 'visualizer-render' );
247 - wp_localize_script(
248 - 'visualizer-render', 'visualizer', array(
249 - 'charts' => $this->_charts,
250 - 'language' => $this->get_language(),
251 - 'map_api_key' => get_option( 'visualizer-map-api-key' ),
252 - 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
253 - 'i10n' => array(
254 - 'copied' => __( 'Copied!', 'visualizer' ),
255 - ),
256 - )
257 - );
258 - wp_enqueue_style( 'visualizer-front' );
259 -
260 - $actions_div = '';
261 - $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] );
262 - if ( ! empty( $actions_visible ) ) {
263 - $actions = $this->get_actions();
264 - $actions_div = '<div class="visualizer-actions">';
265 - foreach ( $actions_visible as $action_type ) {
266 - $key = $action_type;
267 - $mime = '';
268 - if ( strpos( $action_type, ';' ) !== false ) {
269 - $array = explode( ';', $action_type );
270 - $key = $array[0];
271 - $mime = end( $array );
272 - }
273 - $label = $actions[ $key ];
274 - $actions_div .= '<a href="#" class="visualizer-action visualizer-action-' . $key . '" data-visualizer-type="' . $key . '" data-visualizer-chart-id="' . $atts['id'] . '" data-visualizer-mime="' . $mime . '" title="' . $label . '" ';
275 -
276 - if ( 'copy' === $key ) {
277 - $copy = $this->_getDataAs( $atts['id'], 'csv' );
278 - $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"';
279 - wp_enqueue_script( 'visualizer-clipboardjs' );
280 - }
281 -
282 - $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] );
283 - $actions_div .= '>' . $label . '</a> &nbsp;';
284 - }
285 -
286 - $actions_div .= '</div>';
287 - }
288 -
289 - $actions_div .= $css;
290 -
291 - // return placeholder div
292 - return $actions_div . '<div id="' . $id . '"' . $class . '></div>';
293 - }
294 -}
1 +<?php
2 +
3 +// +----------------------------------------------------------------------+
4 +// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) |
5 +// +----------------------------------------------------------------------+
6 +// | This program is free software; you can redistribute it and/or modify |
7 +// | it under the terms of the GNU General Public License, version 2, as |
8 +// | published by the Free Software Foundation. |
9 +// | |
10 +// | This program is distributed in the hope that it will be useful, |
11 +// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 +// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 +// | GNU General Public License for more details. |
14 +// | |
15 +// | You should have received a copy of the GNU General Public License |
16 +// | along with this program; if not, write to the Free Software |
17 +// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
18 +// | MA 02110-1301 USA |
19 +// +----------------------------------------------------------------------+
20 +// | Author: Eugene Manuilov <eugene@manuilov.org> |
21 +// +----------------------------------------------------------------------+
22 +
23 +/**
24 + * Frontend module class.
25 + *
26 + * @category Visualizer
27 + * @package Module
28 + *
29 + * @since 1.0.0
30 + */
31 +class Visualizer_Module_Frontend extends Visualizer_Module {
32 +
33 + const NAME = __CLASS__;
34 +
35 + /**
36 + * The array of charts to render.
37 + *
38 + * @since 1.0.0
39 + *
40 + * @access private
41 + * @var array
42 + */
43 + private $_charts = array();
44 +
45 + /**
46 + * Constructor.
47 + *
48 + * @since 1.0.0
49 + * @uses add_filter() To add "do_shortcode" hook for "widget_text" and "term_description" filters.
50 + *
51 + * @access public
52 + * @param Visualizer_Plugin $plugin The instance of the plugin.
53 + */
54 + public function __construct( Visualizer_Plugin $plugin ) {
55 + parent::__construct( $plugin );
56 +
57 + $this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' );
58 + $this->_addShortcode( 'visualizer', 'renderChart' );
59 +
60 + // add do_shortocde hook for widget_text filter
61 + if ( !has_filter( 'widget_text', 'do_shortcode' ) ) {
62 + add_filter( 'widget_text', 'do_shortcode' );
63 + }
64 +
65 + // add do_shortcode hook for term_description filter
66 + if ( !has_filter( 'term_description', 'do_shortcode' ) ) {
67 + add_filter( 'term_description', 'do_shortcode' );
68 + }
69 + }
70 +
71 + /**
72 + * Registers scripts for charts rendering.
73 + *
74 + * @since 1.0.0
75 + * @uses wp_register_script To register javascript file.
76 + *
77 + * @access public
78 + */
79 + public function enqueueScripts() {
80 + wp_register_script( 'visualizer-google-jsapi', '//www.google.com/jsapi', array(), null, true );
81 + wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi', 'jquery' ), Visualizer_Plugin::VERSION, true );
82 + }
83 +
84 + /**
85 + * Returns placeholder for chart according to visualizer shortcode attributes.
86 + *
87 + * @since 1.0.0
88 + * @uses shortcode_atts() To parse income shortocdes.
89 + * @uses apply_filters() To filter chart's data and series arrays.
90 + * @uses get_post_meta() To fetch chart's meta information.
91 + * @uses wp_enqueue_script() To enqueue charts render script.
92 + * @uses wp_localize_script() To add chart data to the page inline script.
93 + *
94 + * @access public
95 + * @param array $atts The array of shortcode attributes.
96 + */
97 + public function renderChart( $atts ) {
98 + $atts = shortcode_atts( array(
99 + 'id' => false, // chart id
100 + 'class' => false, // chart class
101 + 'series' => false, // series filter hook
102 + 'data' => false, // data filter hook
103 + 'settings' => false, // data filter hook
104 + ), $atts );
105 +
106 + // if empty id or chart does not exists, then return empty string
107 + if ( !$atts['id'] || !( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
108 + return '';
109 + }
110 +
111 + $id = 'visualizer-' . $atts['id'];
112 + $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] );
113 + $class = !empty( $class ) ? ' class="' . $class . '"' : '';
114 +
115 + $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
116 +
117 + // faetch and update settings
118 + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
119 + if ( empty( $settings['height'] ) ) {
120 + $settings['height'] = '400';
121 + }
122 +
123 + // handle series filter hooks
124 + $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
125 + if ( !empty( $atts['series'] ) ) {
126 + $series = apply_filters( $atts['series'], $series, $chart->ID, $type );
127 + }
128 + // handle settings filter hooks
129 + $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
130 + if ( !empty( $atts['settings'] ) ) {
131 + $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type );
132 + }
133 +
134 + // handle data filter hooks
135 + $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type );
136 + if ( !empty( $atts['data'] ) ) {
137 + $data = apply_filters( $atts['data'], $data, $chart->ID, $type );
138 + }
139 +
140 + // add chart to the array
141 + $this->_charts[$id] = array(
142 + 'type' => $type,
143 + 'series' => $series,
144 + 'settings' => $settings,
145 + 'data' => $data,
146 + );
147 +
148 + // enqueue visualizer render and update render localizations
149 + wp_enqueue_script( 'visualizer-render' );
150 + wp_localize_script( 'visualizer-render', 'visualizer', array( 'charts' => $this->_charts ) );
151 +
152 + // return placeholder div
153 + return '<div id="' . $id . '"' . $class . '></div>';
154 + }
155 +
156 +}