PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 2.2.0
Visualizer – Tables & Charts Manager with Built-in AI Generator v2.2.0
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 2.2.0, at classes/Visualizer/Module/Frontend.php

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