PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.0.12
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.0.12
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 3.10.4 All 148 releases
visualizer / classes / Visualizer / Module / Frontend.php

Frontend.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.0.12, at classes/Visualizer/Module/Frontend.php

309 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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->_addAction( 'visualizer_enqueue_scripts', 'enqueueScripts' );
58 $this->_addFilter( 'visualizer_get_language', 'getLanguage' );
59 $this->_addShortcode( 'visualizer', 'renderChart' );
60
61 // add do_shortocde hook for widget_text filter
62 if ( ! has_filter( 'widget_text', 'do_shortcode' ) ) {
63 add_filter( 'widget_text', 'do_shortcode' );
64 }
65
66 // add do_shortcode hook for term_description filter
67 if ( ! has_filter( 'term_description', 'do_shortcode' ) ) {
68 add_filter( 'term_description', 'do_shortcode' );
69 }
70
71 add_action( 'rest_api_init', array( $this, 'endpoint_register' ) );
72 }
73
74 /**
75 * Returns the language/locale.
76 */
77 function getLanguage( $dummy, $only_language ) {
78 return $this->get_language();
79 }
80
81
82 /**
83 * Registers the endpoints
84 */
85 function endpoint_register() {
86 register_rest_route(
87 'visualizer/v' . VISUALIZER_REST_VERSION,
88 '/action/(?P<chart>\d+)/(?P<type>.+)/',
89 array(
90 'methods' => array( 'GET', 'POST' ),
91 'callback' => array( $this, 'perform_action' ),
92 )
93 );
94 }
95
96 /**
97 * All possible actions and their labels
98 *
99 * @access private
100 */
101 private function get_actions() {
102 return apply_filters(
103 'visualizer_action_buttons',
104 array(
105 'print' => __( 'Print', 'visualizer' ),
106 'csv' => __( 'CSV', 'visualizer' ),
107 'xls' => __( 'Excel', 'visualizer' ),
108 'copy' => __( 'Copy', 'visualizer' ),
109 )
110 );
111 }
112
113 /**
114 * The print button
115 *
116 * @since 1.0.0
117 *
118 * @access public
119 */
120 public function perform_action( WP_REST_Request $params ) {
121 $chart_id = filter_var( sanitize_text_field( $params['chart'] ), FILTER_VALIDATE_INT );
122 $type = sanitize_text_field( $params['type'] );
123 $data = null;
124
125 if ( ! $chart_id ) {
126 return new WP_REST_Response( array( 'error' => new WP_Error( __( 'Invalid chart ID', 'visualizer' ) ) ) );
127 }
128
129 if ( ! $type ) {
130 return new WP_REST_Response( array( 'error' => new WP_Error( __( 'Invalid action', 'visualizer' ) ) ) );
131 }
132
133 switch ( $type ) {
134 case 'print':
135 $data = $this->_getDataAs( $chart_id, 'print' );
136 break;
137 case 'csv':
138 $data = $this->_getDataAs( $chart_id, 'csv' );
139 break;
140 case 'xls':
141 $data = $this->_getDataAs( $chart_id, 'xls' );
142 break;
143 default:
144 $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this );
145 break;
146 }
147
148 return new WP_REST_Response( array( 'data' => $data ) );
149 }
150
151 /**
152 * Registers scripts for charts rendering.
153 *
154 * @since 1.0.0
155 * @uses wp_register_script To register javascript file.
156 *
157 * @access public
158 */
159 public function enqueueScripts() {
160 wp_register_script( 'visualizer-google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
161 wp_register_script( 'visualizer-google-jsapi-old', '//www.google.com/jsapi', array( 'visualizer-google-jsapi-new' ), null, true );
162 wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true );
163 wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi-old', 'jquery', 'visualizer-customization' ), Visualizer_Plugin::VERSION, true );
164 wp_register_script( 'visualizer-clipboardjs', VISUALIZER_ABSURL . 'js/lib/clipboardjs/clipboard.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true );
165 wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION );
166 do_action( 'visualizer_pro_frontend_load_resources' );
167 }
168
169 /**
170 * Returns placeholder for chart according to visualizer shortcode attributes.
171 *
172 * @since 1.0.0
173 * @uses shortcode_atts() To parse income shortocdes.
174 * @uses apply_filters() To filter chart's data and series arrays.
175 * @uses get_post_meta() To fetch chart's meta information.
176 * @uses wp_enqueue_script() To enqueue charts render script.
177 * @uses wp_localize_script() To add chart data to the page inline script.
178 *
179 * @access public
180 * @param array $atts The array of shortcode attributes.
181 */
182 public function renderChart( $atts ) {
183 global $wp_version;
184 $atts = shortcode_atts(
185 array(
186 'id' => false, // chart id
187 'class' => false, // chart class
188 'series' => false, // series filter hook
189 'data' => false, // data filter hook
190 'settings' => false, // data filter hook
191 ),
192 $atts
193 );
194
195 // if empty id or chart does not exists, then return empty string
196 if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) {
197 return '';
198 }
199
200 if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) {
201 return '';
202 }
203
204 // in case revisions exist.
205 if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) {
206 $chart = get_post( $chart->ID );
207 }
208
209 $id = 'visualizer-' . $atts['id'];
210 $defaultClass = 'visualizer-front';
211 $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] );
212 $class = $defaultClass . ' ' . $class . ' ' . 'visualizer-front-' . $atts['id'];
213 $class = ! empty( $class ) ? ' class="' . trim( $class ) . '"' : '';
214
215 $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
216
217 // faetch and update settings
218 $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
219 if ( empty( $settings['height'] ) ) {
220 $settings['height'] = '400';
221 }
222
223 // handle series filter hooks
224 $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type );
225 if ( ! empty( $atts['series'] ) ) {
226 $series = apply_filters( $atts['series'], $series, $chart->ID, $type );
227 }
228 // handle settings filter hooks
229 $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type );
230 if ( ! empty( $atts['settings'] ) ) {
231 $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type );
232 }
233
234 // handle data filter hooks
235 $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type );
236 if ( ! empty( $atts['data'] ) ) {
237 $data = apply_filters( $atts['data'], $data, $chart->ID, $type );
238 }
239
240 $id = $id . '-' . rand();
241 $arguments = array( '', $id, $settings );
242 $css = '';
243 apply_filters_ref_array( 'visualizer_pro_inline_css', array( &$arguments ) );
244 if ( ! empty( $arguments ) ) {
245 $css = $arguments[0];
246 $settings = $arguments[2];
247 }
248
249 // add chart to the array
250 $this->_charts[ $id ] = array(
251 'type' => $type,
252 'series' => $series,
253 'settings' => $settings,
254 'data' => $data,
255 );
256
257 // enqueue visualizer render and update render localizations
258 wp_enqueue_script( 'visualizer-render' );
259 wp_localize_script(
260 'visualizer-render',
261 'visualizer',
262 array(
263 'charts' => $this->_charts,
264 'language' => $this->get_language(),
265 'map_api_key' => get_option( 'visualizer-map-api-key' ),
266 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
267 'i10n' => array(
268 'copied' => __( 'Copied!', 'visualizer' ),
269 ),
270 )
271 );
272 wp_enqueue_style( 'visualizer-front' );
273
274 $actions_div = '';
275 $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] );
276 if ( ! empty( $actions_visible ) ) {
277 $actions = $this->get_actions();
278 $actions_div = '<div class="visualizer-actions">';
279 foreach ( $actions_visible as $action_type ) {
280 $key = $action_type;
281 $mime = '';
282 if ( strpos( $action_type, ';' ) !== false ) {
283 $array = explode( ';', $action_type );
284 $key = $array[0];
285 $mime = end( $array );
286 }
287 $label = $actions[ $key ];
288 $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 . '" ';
289
290 if ( 'copy' === $key ) {
291 $copy = $this->_getDataAs( $atts['id'], 'csv' );
292 $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"';
293 wp_enqueue_script( 'visualizer-clipboardjs' );
294 }
295
296 $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] );
297 $actions_div .= '>' . $label . '</a> &nbsp;';
298 }
299
300 $actions_div .= '</div>';
301 }
302
303 $actions_div .= $css;
304
305 // return placeholder div
306 return $actions_div . '<div id="' . $id . '"' . $class . '></div>';
307 }
308 }
309