PluginProbe
Chartify – WordPress Chart Plugin / 3.0.3
Chartify – WordPress Chart Plugin v3.0.3
3.8.0 3.7.9 3.7.8 3.7.7 3.7.6 3.7.5 trunk 1.0.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 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.1.4 3.1.5 3.1.6 All 83 releases
chart-builder / public / class-chart-builder-public.php

class-chart-builder-public.php in Chartify – WordPress Chart Plugin 3.0.3, at public/class-chart-builder-public.php

402 lines 16.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The public-facing functionality of the plugin.
5 *
6 * @link https://ays-pro.com/
7 * @since 1.0.0
8 *
9 * @package Chart_Builder
10 * @subpackage Chart_Builder/public
11 */
12
13 /**
14 * The public-facing functionality of the plugin.
15 *
16 * Defines the plugin name, version, and two examples hooks for how to
17 * enqueue the public-facing stylesheet and JavaScript.
18 *
19 * @package Chart_Builder
20 * @subpackage Chart_Builder/public
21 * @author Chart Builder Team <info@ays-pro.com>
22 */
23 class Chart_Builder_Public {
24
25 /**
26 * The ID of this plugin.
27 *
28 * @since 1.0.0
29 * @access private
30 * @var string $plugin_name The ID of this plugin.
31 */
32 private $plugin_name;
33
34 /**
35 * The version of this plugin.
36 *
37 * @since 1.0.0
38 * @access private
39 * @var string $version The current version of this plugin.
40 */
41 private $version;
42
43 /**
44 * @var string
45 */
46 private $html_class_prefix = 'ays-chart-';
47
48 /**
49 * @var string
50 */
51 private $html_name_prefix = 'ays_chart_';
52
53 /**
54 * @var string
55 */
56 private $name_prefix = 'chart_';
57
58 /**
59 * @var
60 */
61 private $unique_id;
62
63 /**
64 * @var Chart_Builder_DB_Actions
65 */
66 private $db_object;
67
68 /**
69 * @var array
70 */
71 private $data;
72
73
74 /**
75 * Initialize the class and set its properties.
76 *
77 * @since 1.0.0
78 * @param string $plugin_name The name of the plugin.
79 * @param string $version The version of this plugin.
80 */
81 public function __construct( $plugin_name, $version ) {
82
83 $this->plugin_name = $plugin_name;
84 $this->version = $version;
85 $this->db_object = new Chart_Builder_DB_Actions( $this->plugin_name );
86
87 add_shortcode( 'ays_chart', array( $this, 'ays_generate_chart_method' ) );
88 }
89
90 /**
91 * Register the stylesheets for the public-facing side of the site.
92 *
93 * @since 1.0.0
94 */
95 public function enqueue_styles() {
96
97 /**
98 * This function is provided for demonstration purposes only.
99 *
100 * An instance of this class should be passed to the run() function
101 * defined in Chart_Builder_Loader as all of the hooks are defined
102 * in that particular class.
103 *
104 * The Chart_Builder_Loader will then create the relationship
105 * between the defined hooks and the functions defined in this
106 * class.
107 */
108
109 wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/chart-builder-public.css', array(), $this->version, 'all' );
110
111 }
112
113 /**
114 * Register the JavaScript for the public-facing side of the site.
115 *
116 * @since 1.0.0
117 */
118 public function enqueue_scripts($type) {
119
120 /**
121 * This function is provided for demonstration purposes only.
122 *
123 * An instance of this class should be passed to the run() function
124 * defined in Chart_Builder_Loader as all of the hooks are defined
125 * in that particular class.
126 *
127 * The Chart_Builder_Loader will then create the relationship
128 * between the defined hooks and the functions defined in this
129 * class.
130 */
131
132 $is_elementor_exists = Chart_Builder_DB_Actions::ays_chart_is_elementor();
133 if( !$is_elementor_exists ) {
134 if ($type === 'chart-js') {
135 wp_enqueue_script( $this->plugin_name . '-chart-js', plugin_dir_url(__FILE__) . 'js/chart-js.js', array('jquery'), $this->version, true);
136 wp_enqueue_script( $this->plugin_name . '-plugin-chartjs', plugin_dir_url( __FILE__ ) . 'js/chart-builder-public-chartjs.js', array( 'jquery' ), $this->version, false );
137 } else {
138 wp_enqueue_script( $this->plugin_name . '-charts-google', plugin_dir_url(__FILE__) . 'js/google-chart.js', array('jquery'), $this->version, true);
139 wp_enqueue_script( $this->plugin_name . '-plugin-google', plugin_dir_url( __FILE__ ) . 'js/chart-builder-public-google.js', array( 'jquery' ), $this->version, false );
140 }
141 wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/chart-builder-public.js', array( 'jquery' ), $this->version, false );
142 }
143
144 $this->get_encoded_options($type);
145 }
146
147 public function ays_generate_chart_method( $attr ) {
148 $id = (isset($attr['id'])) ? absint(intval($attr['id'])) : null;
149
150 if (is_null($id)) {
151 return "<p class='wrong_shortcode_text' style='color:red;'>" . __( 'Wrong shortcode initialized', "chart-builder" ) . "</p>";
152 }
153
154 $is_elementor = Chart_Builder_DB_Actions::ays_chart_is_elementor();
155 if ($is_elementor) {
156 return "<div style='width:100%;padding:6px 8px;font-size:13px;border:1px solid #757575;border-radius:2px;background-color:#f0f0f1;color:#2c3338;'>
157 [ays_chart id=".$id."]
158 </div>
159 <p style='margin:4px 0 0 3px;font-size:12px;font-style:italic;'>
160 " . __( 'Note: The chart will be visible on the front end of your website.', "chart-builder" ) . "
161 </p>";
162 }
163
164 $chartData = CBActions()->get_chart_data( $id );
165
166 if ( is_null( $chartData ) ) {
167 return "<p class='wrong_shortcode_text' style='color:red;'>" . __('Wrong shortcode initialized', "chart-builder") . "</p>";
168 }
169
170 $this->enqueue_styles();
171 $content = $this->show_chart( $id, $chartData, $attr );
172 $this->enqueue_scripts($chartData['chart']['type']);
173
174 return str_replace( array( "\r\n", "\n", "\r" ), '', $content );
175 }
176
177 public function show_chart( $id, $chartData, $attr ) {
178 $chart = $chartData['chart'];
179 $settings = $chartData['settings'];
180
181 $unique_id = uniqid();
182 $this->unique_id = $unique_id;
183
184 $data = array();
185
186 if ( is_null( $chart ) ) {
187 return "<p class='wrong_shortcode_text' style='color:red;'>" . __('Wrong shortcode initialized', "chart-builder") . "</p>";
188 }
189
190 $source_type = $chartData['source_type'];
191 $chart_source_type = $chartData['chart']['type'];
192 $user_id = get_current_user_id();
193
194 if ($source_type == 'quiz_maker' && $user_id == 0 && $chart['quiz_query'] != 'q1') {
195 return "<p class='ays_chart_not_logged_text'>" . __('You are not logged in. Please log in to view this chart.', $this->plugin_name) . "</p>";
196 }
197
198 $status = isset( $chart['status'] ) && $chart['status'] != '' ? $chart['status'] : '';
199
200 if ( $status != 'published' ) {
201 return "";
202 }
203
204 if ($chart_source_type === "chart-js") {
205 $settings = CBFunctions()->get_chart_settings_chartjs_public($settings);
206 } else {
207 $settings = CBFunctions()->get_chart_settings_google_public($settings, $chartData['source']);
208 }
209
210 $data['chart_type'] = $chartData['source_chart_type'];
211 $data['source'] = $chartData['source'];
212
213
214 $data['options'] = $settings;
215
216 if ($chart_source_type === "chart-js") {
217 $content = $this->chartJsContent($chart, $settings, $chartData, $id);
218 } else {
219 $content = $this->googleChartsContent($chart, $settings, $chartData, $id);
220 }
221
222 $this->data = $data;
223
224 return implode( '', $content );
225 }
226
227 public function get_encoded_options ($chart_source_type) {
228 $data = $this->data;
229 $encoded_data = base64_encode(json_encode($data));
230 $handle = $chart_source_type === 'chart-js' ? $this->plugin_name . '-chart-js' : $this->plugin_name . '-charts-google';
231
232 wp_localize_script($handle, 'aysChartOptions'.$this->unique_id, array('aysChartOptions' => $encoded_data));
233 }
234
235 public function googleChartsContent ($chart, $settings, $chartData, $id) {
236 $chart_title = (isset($chart['title']) && $chart['title'] != '') ? stripslashes ( sanitize_text_field( $chart['title'] ) ) : '';
237 $chart_description = (isset($chart['description']) && $chart['description'] != '') ? stripslashes ( sanitize_text_field( $chart['description'] ) ) : '';
238
239 $content = array();
240
241 $content[] = "<div class='" . $this->html_class_prefix . "container-google " . $this->html_class_prefix . "container-" . $id . "' id='" . $this->html_class_prefix . "container" . $this->unique_id . "' data-id='" . $this->unique_id . "'>";
242
243 $content[] = "<div class='" . $this->html_class_prefix . "header-container'>";
244
245 if ($settings['show_title'] == 'on') {
246 $content[] = "<div class='" . $this->html_class_prefix . "charts-title " . $this->html_class_prefix . "charts-title" . $this->unique_id . "'>";
247 $content[] = $chart_title;
248 $content[] = "</div>";
249 }
250
251 if ($settings['show_description'] == 'on') {
252 $content[] = "<div class='" . $this->html_class_prefix . "charts-description " . $this->html_class_prefix . "charts-description" . $this->unique_id . "'>";
253 $content[] = $chart_description;
254 $content[] = "</div>";
255 }
256
257 $content[] = "</div>";
258
259 $content[] = "<div class='" . $this->html_class_prefix . "charts-main-container " . $this->html_class_prefix . "charts-main-container" . $this->unique_id . "' id=" . $this->html_class_prefix . $chartData['source_chart_type'] . $this->unique_id . " data-type='". $chartData['source_chart_type'] ."'></div>";
260
261 $content[] = "<div class='" . $this->html_class_prefix . "actions-container'>";
262
263 $content[] = "<div class='" . $this->html_class_prefix . "export-buttons' data-id='". $id . "'>";
264 if (isset($settings['enable_img']) && $settings['enable_img'] == 'on'){
265 $content[] = "<button class='" . $this->html_class_prefix . "export-button-" . $this->unique_id . "' title='Download as a PNG' data-type='image' value='image'>Image</button>";
266
267 $content[] = "<div style='display:none'>";
268 $content[] = "<iframe src='' style='width:100%;height:600px;'></iframe>";
269 $content[] = "</div>";
270 }
271 $content[] = "</div>";
272
273 $content[] = "</div>";
274
275 $content[] = "</div>";
276
277 $custom_css = "
278 #" . $this->html_class_prefix . "container" . $this->unique_id . " div." . $this->html_class_prefix . "charts-main-container" . $this->unique_id . " {
279 width: " . $settings['chart_width'] . ";
280 height: " . $settings['chart_height'] . ";
281 " . $settings['position'] . ";
282 border-radius: " . $settings['border_radius'] . "px;
283 overflow: hidden;
284 box-shadow: " . ($settings['box_shadow'] === 'checked' ? '2px 2px 10px 2px '.$settings['box_shadow_color'] : '') . ";
285 }
286 #" . $this->html_class_prefix . "container" . $this->unique_id . " div." . $this->html_class_prefix . "charts-main-container" . $this->unique_id . "[data-type='org_chart'] {
287 overflow: auto;
288 }
289
290 #" . $this->html_class_prefix . "container" . $this->unique_id . " div." . $this->html_class_prefix . "charts-main-container" . $this->unique_id . "[data-type='org_chart'] table.google-visualization-orgchart-table tbody td {
291 padding: initial;
292 }
293
294 #" . $this->html_class_prefix . "container" . $this->unique_id . " div." . $this->html_class_prefix . "header-container {
295 margin-bottom: " . $settings['title_gap'] . "px !important;
296 }
297
298 #" . $this->html_class_prefix . "container" . $this->unique_id . " div." . $this->html_class_prefix . "header-container>." . $this->html_class_prefix . "charts-title" . $this->unique_id . " {
299 color: " . $settings['title_color'] . ";
300 font-size: " . $settings['title_font_size'] . "px;
301 font-weight: " . $settings['title_bold'] . ";
302 text-shadow: " . ($settings['title_text_shadow'] === 'checked' ? '2px 2px 5px '.$settings['title_shadow_color'] : '') . ";
303 font-style: " . $settings['title_italic'] . ";
304 text-align: " . $settings['title_position'] . ";
305 text-transform: " . $settings['title_text_transform'] . ";
306 text-decoration: " . $settings['title_text_decoration'] . ";
307 letter-spacing: " . $settings['title_letter_spacing'] . "px;
308 margin-bottom: " . $settings['title_gap_description'] . "px;
309 }
310
311 #" . $this->html_class_prefix . "container" . $this->unique_id . " div." . $this->html_class_prefix . "header-container>." . $this->html_class_prefix . "charts-description" . $this->unique_id . " {
312 color: " . $settings['description_color'] . ";
313 font-size: " . $settings['description_font_size'] . "px;
314 font-weight: " . $settings['description_bold'] . ";
315 text-shadow: " . ($settings['description_text_shadow'] === 'checked' ? '2px 2px 5px '.$settings['description_shadow_color'] : '') . ";
316 font-style: " . $settings['description_italic'] . ";
317 text-align: " . $settings['description_position'] . ";
318 text-transform: " . $settings['description_text_transform'] . ";
319 text-decoration: " . $settings['description_text_decoration'] . ";
320 letter-spacing: " . $settings['description_letter_spacing'] . "px;
321 }
322
323 #" . $this->html_class_prefix . "container" . $this->unique_id . " div." . $this->html_class_prefix . "actions-container>div." . $this->html_class_prefix . "export-buttons .ays-chart-export-button-" . $this->unique_id . " {
324 color: " . $settings['description_color'] . "B3 !important;
325 font-size: " . $settings['description_font_size'] . "px !important;
326 }
327
328 #" . $this->html_class_prefix . "container" . $this->unique_id . " div." . $this->html_class_prefix . "actions-container>div." . $this->html_class_prefix . "export-buttons .ays-chart-export-button-" . $this->unique_id . ":hover {
329 color: " . $settings['description_color'] . " !important;
330 }
331 ";
332 wp_add_inline_style($this->plugin_name, $custom_css);
333
334 return $content;
335 }
336
337 public function chartJsContent($chart, $settings, $chartData, $id) {
338 $chart_title = (isset($chart['title']) && $chart['title'] != '') ? stripslashes ( sanitize_text_field( $chart['title'] ) ) : '';
339 $chart_description = (isset($chart['description']) && $chart['description'] != '') ? stripslashes ( sanitize_text_field( $chart['description'] ) ) : '';
340
341 $content = array();
342
343 $content[] = "<div class='" . $this->html_class_prefix . "container-chartjs " . $this->html_class_prefix . "container-" . $id . "' id='" . $this->html_class_prefix . "container" . $this->unique_id . "' data-id='" . $this->unique_id . "'>";
344
345 $content[] = "<div class='" . $this->html_class_prefix . "header-container'>";
346
347 if ($settings['show_title'] == 'on') {
348 $content[] = "<div class='" . $this->html_class_prefix . "charts-title " . $this->html_class_prefix . "charts-title" . $this->unique_id . "'>";
349 $content[] = $chart_title;
350 $content[] = "</div>";
351 }
352
353 if ($settings['show_description'] == 'on') {
354 $content[] = "<div class='" . $this->html_class_prefix . "charts-description " . $this->html_class_prefix . "charts-description" . $this->unique_id . "'>";
355 $content[] = $chart_description;
356 $content[] = "</div>";
357 }
358
359 $content[] = "</div>";
360
361 $content[] = "<div class='" . $this->html_class_prefix . "charts-main-container " . $this->html_class_prefix . "charts-main-container" . $this->unique_id . "' id=" . $this->html_class_prefix . $chartData['source_chart_type'] . "-" . $this->unique_id . " data-type='". $chartData['source_chart_type'] ."'>";
362 $content[] = "<canvas id='" . $this->html_class_prefix . $chartData['source_chart_type'] . "-" . $this->unique_id . "-canvas'>";
363 $content[] = "</div>";
364
365 $content[] = "</div>";
366
367 $custom_css = "
368 #" . $this->html_class_prefix . "container" . $this->unique_id . " div." . $this->html_class_prefix . "header-container {
369 margin-bottom: " . $settings['title_gap'] . "px !important;
370 }
371
372 #" . $this->html_class_prefix . "container" . $this->unique_id . " div." . $this->html_class_prefix . "header-container>." . $this->html_class_prefix . "charts-title" . $this->unique_id . " {
373 color: " . $settings['title_color'] . ";
374 font-size: " . $settings['title_font_size'] . "px;
375 font-weight: " . $settings['title_bold'] . ";
376 text-shadow: " . ($settings['title_text_shadow'] === 'checked' ? '2px 2px 5px '.$settings['title_shadow_color'] : '') . ";
377 font-style: " . $settings['title_italic'] . ";
378 text-align: " . $settings['title_position'] . ";
379 text-transform: " . $settings['title_text_transform'] . ";
380 text-decoration: " . $settings['title_text_decoration'] . ";
381 letter-spacing: " . $settings['title_letter_spacing'] . "px;
382 margin-bottom: " . $settings['title_gap_description'] . "px;
383 }
384
385 #" . $this->html_class_prefix . "container" . $this->unique_id . " div." . $this->html_class_prefix . "header-container>." . $this->html_class_prefix . "charts-description" . $this->unique_id . " {
386 color: " . $settings['description_color'] . ";
387 font-size: " . $settings['description_font_size'] . "px;
388 font-weight: " . $settings['description_bold'] . ";
389 text-shadow: " . ($settings['description_text_shadow'] === 'checked' ? '2px 2px 5px '.$settings['description_shadow_color'] : '') . ";
390 font-style: " . $settings['description_italic'] . ";
391 text-align: " . $settings['description_position'] . ";
392 text-transform: " . $settings['description_text_transform'] . ";
393 text-decoration: " . $settings['description_text_decoration'] . ";
394 letter-spacing: " . $settings['description_letter_spacing'] . "px;
395 }
396 ";
397 wp_add_inline_style($this->plugin_name, $custom_css);
398
399 return $content;
400 }
401 }
402