PluginProbe
SQL Chart Builder / 2.3.2
SQL Chart Builder v2.3.2
3.0.2 3.0.1 trunk 1.0.2 1.0.3 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.7.1 2.3.7.2 2.3.8 3.0.0
sql-chart-builder / functions.php

functions.php in SQL Chart Builder 2.3.2, at functions.php

820 lines 28.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) {
3 die;
4 }
5 //postinstall function
6 function guaven_sqlcharts_load_defaults()
7 {
8 if (get_option("guaven_sqlcharts_already_installed_2") === false) {
9 update_option("guaven_sqlcharts_already_installed_2", "1");
10 guaven_sqlcharts_install_first_data();
11
12 }
13 }
14
15 function guaven_sqlcharts_install_first_data()
16 {
17 require_once(dirname(__FILE__) . "/initial_data.php");
18 gvn_chart_sample_nonxml_data();
19 }
20
21
22 function guaven_sqlcharts_recommended(){
23 if(!class_exists('WooCommerce'))return;
24 $uid=(int)get_current_user_id();
25 if(isset($_GET["gvnsql_dismiss_recommendation"])){
26 update_option('gvnsql_dismiss_recommendation_'.$uid,1);
27 return;
28 }
29 if(get_option('gvnsql_dismiss_recommendation_'.$uid)!='')return;
30 return '<table class="gf-alert gf-alert-info" style="margin-top:20px">
31 <tbody><tr><td style="width: auto;vertical-align: top;padding: 20px;">
32 <h2>WooCommerce Search Engine – INSTANT, RELEVANT AND SMART Search Box</h2>
33 <h3>Turn your website search into Smart Search which find products by price, SKU, attributes, meta data, categorys, tags etc. </h3>
34 <p>“WooCommerce Search Engine” is a very powerful and easy to use WooCommerce Search Plugin which turns a simple search box of your WooCommerce Store to the powerful multifunctional magic box which helps you to sell more products. The plugin UI is compatible with ALL THEMES.</p>
35 <a target="_blank" style="border:0px solid #6200ee;border-radius:0px;color:white;font-weight:bold;background: #6200ee;" class="button button-secondary"
36 href="https://codecanyon.net/item/woocommerce-search-box/15685698">Get the Search Box </a>
37 </td><td style="position:relative">
38 <a href="'.admin_url().'/edit.php?post_type=gvn_schart&gvnsql_dismiss_recommendation=1'.'" style="position: absolute;right: 0;top: -15px;right: -10px;"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="11" height="14" viewBox="0 0 11 14">
39 <path d="M10.141 10.328q0 0.312-0.219 0.531l-1.062 1.062q-0.219 0.219-0.531 0.219t-0.531-0.219l-2.297-2.297-2.297 2.297q-0.219 0.219-0.531 0.219t-0.531-0.219l-1.062-1.062q-0.219-0.219-0.219-0.531t0.219-0.531l2.297-2.297-2.297-2.297q-0.219-0.219-0.219-0.531t0.219-0.531l1.062-1.062q0.219-0.219 0.531-0.219t0.531 0.219l2.297 2.297 2.297-2.297q0.219-0.219 0.531-0.219t0.531 0.219l1.062 1.062q0.219 0.219 0.219 0.531t-0.219 0.531l-2.297 2.297 2.297 2.297q0.219 0.219 0.219 0.531z"></path>
40 </svg>
41 </a>
42 <img src="'.plugin_dir_url( __FILE__ ) . 'asset/img/recommended1.jpg" style="max-width: 430px;"></td></tr>
43 </tbody></table>';
44 }
45
46
47
48 function guaven_sqlcharts_my_admin_notice()
49 {
50 global $post;
51
52
53 if(
54 (!empty($post) and $post->post_type == 'gvn_schart')
55 or
56 strpos($_SERVER["REQUEST_URI"]=='post_type=gvn_schart')
57 ){
58 echo guaven_sqlcharts_recommended();
59 }
60
61
62
63 if (!empty($post) and $post->post_type == 'gvn_schart'):
64 if (!current_user_can('manage_options')) {
65 echo '<br><br>
66 <div class="updated gf-alert gf-alert-danger">Only administrators can manage this page</div>';
67 die();
68 }
69 echo '<div class="updated gf-alert gf-alert-info">';
70 if (empty($_GET["post"]) and strpos($_SERVER["REQUEST_URI"], "post-new") === false):
71 $gf_message = 'Use <b>Add new</b> button above to create new sql report. And click on any existing rule names below
72 to manage them. ';
73 else:
74 $gf_message = '
75 1. Give any name to your report.<br>
76 2. Choose chart type, type sql query, enter field names, labels and then press to Publish/Update<br>
77 3. After update you will see needed shortcode below. You can use that shortcode anywhere in your website: in pages, posts, widgets etc. <br>
78 ';
79 endif;
80 _e('<div style="float:left">' . $gf_message . '</div>', 'guaven_sqlcharts');
81 echo '<div style="float: right;
82 margin-top: 0px;
83 padding-top: 0px;"><a target="_blank" style="border:0px solid #6200ee;border-radius:0px;color:white;font-weight:bold;background: #6200ee;"
84 class="button button-secondary" href="https://guaven.com/contact/solution-request/">Get Premium Support </a></div> </div>';
85 endif;
86 }
87 add_action('admin_notices', 'guaven_sqlcharts_my_admin_notice');
88
89 function guaven_sqlcharts_onboarding_notice(){
90 if((function_exists('wp_doing_ajax') and wp_doing_ajax()) or get_option('guaven_sqlcharts_onboarding_notice_dismissed') == 1)
91 return;
92
93 printf('
94 <div class="guaven-sqlcharts-notice notice notice-success is-dismissible" data-notice="onboarding_notice">
95 <p>Welcome aboard on MySQL Charts!</p>
96 </div>'
97 );
98 }
99 add_action('admin_notices', 'guaven_sqlcharts_onboarding_notice');
100
101 function guaven_sqlcharts_onboarding_notice_dismissed(){
102 check_ajax_referer('notice_dismissed', 'nonce');
103 switch ($_POST['type']){
104 case 'onboarding_notice':
105 update_option('guaven_sqlcharts_onboarding_notice_dismissed', 1);
106 break;
107 }
108 }
109 add_action('wp_ajax_guaven_sqlcharts_onboarding_notice_dismissed', 'guaven_sqlcharts_onboarding_notice_dismissed');
110
111 function guaven_sqlcharts_enqueue_chart()
112 {
113 wp_enqueue_script('guaven_sqlcharts_chartjs', plugins_url('asset/bundle.min.js', __FILE__),array('jquery'),GVNSQLCHARTS_VERSION);
114 wp_localize_script('guaven_sqlcharts_chartjs', 'guaven_sqlcharts_notice_dismissed', array(
115 'action' => 'guaven_sqlcharts_onboarding_notice_dismissed',
116 'nonce' => wp_create_nonce('notice_dismissed')
117 ));
118
119 }
120 add_action('wp_enqueue_scripts', 'guaven_sqlcharts_enqueue_chart');
121 add_action('admin_enqueue_scripts', 'guaven_sqlcharts_enqueue_chart');
122
123 function guaven_sqlcharts_enqueue_main_style()
124 {
125 wp_enqueue_style('guaven_sqlcharts_main_style', plugins_url('asset/guaven_sqlcharts.css', __FILE__),array(),GVNSQLCHARTS_VERSION);
126 }
127 add_action('admin_enqueue_scripts', 'guaven_sqlcharts_enqueue_main_style');
128
129
130 function guaven_sqlcharts_isJson($string)
131 {
132 json_decode($string);
133 return (json_last_error() == JSON_ERROR_NONE);
134 }
135
136 add_action('init', 'guaven_sqlcharts_register_post');
137 function guaven_sqlcharts_register_post()
138 {
139 //register_taxonomy('guaven_update_push_tag', 'termin');
140 register_post_type('gvn_schart', array(
141 'labels' => array(
142 'name' => __('My SQL Charts'),
143 'singular_name' => __('My SQL chart')
144 ),
145
146 'public' => true,
147 //'taxonomies' => array('guaven_update_push_tag'),
148 'supports' => array(
149 'title',
150 'postmeta'
151 ),
152 'register_meta_box_cb' => 'guaven_sqlcharts_metabox_area'
153 ));
154
155 guaven_sqlcharts_load_defaults();
156 }
157
158 add_action('admin_footer', 'guaven_sqlcharts_admin_front');
159
160
161 function guaven_sqlcharts_admin_front()
162 {
163 global $post;
164 if (!empty($post) and $post->post_type == 'gvn_schart') {
165 ?>
166 <style type="text/css">#normal-sortables{display: none}</style>
167 <?php
168 }
169 }
170
171 // metabox for editor
172 function guaven_sqlcharts_metabox_area()
173 {
174 add_meta_box('guaven_sqlcharts_metabox', 'Configure your graph chart', 'guaven_sqlcharts_metabox', 'gvn_schart', 'advanced', 'default');
175 }
176
177 function guaven_sqlcharts_metabox()
178 {
179 require_once(dirname(__FILE__) . "/admin_metabox.php");
180 }
181
182 function guaven_gutenberg_wrapper($atts){
183 if(isset($atts['sqlcharts_inserted_script'])){
184 global $sqlcharts_inserted_script;
185 $sqlcharts_inserted_script = $atts['sqlcharts_inserted_script'];
186 }
187
188 // if( isset($_GET["post_id"],$_GET["context"]) and $_GET["context"]=='edit'
189 // and strpos($_SERVER["REQUEST_URI"],'block-renderer/guaven-sqlcharts/gvn-chart-gutenberg')!==false
190 // ){
191 // $atts['chart_id']=$_GET["post_id"];
192 // }
193 $post = get_post($atts['chart_id']);
194 if( ! isset($atts['chart_id']) or !isset($post) or $post->post_type != 'gvn_schart'){
195
196 return "Invalid id";
197 }
198
199 return guaven_sqlcharts_local_shortcode(array('id' => $atts['chart_id'])); // temporary explicit value
200 }
201 function guaven_register_gutenberg_blocks()
202 {
203 wp_register_script(
204 'gvn_gutenberg_charts',
205 plugins_url( 'asset/guaven_gutenberg_charts.js', __FILE__ ),
206 array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-server-side-render' ),
207 GVNSQLCHARTS_VERSION.'_'.filemtime( plugin_dir_path( __FILE__ ) . 'asset/guaven_gutenberg_charts.js' )
208 );
209 wp_localize_script('gvn_gutenberg_charts', 'guaven', array(
210 'description' => 'Add My SQL Chart to your post',
211 ));
212
213 register_block_type( 'guaven-sqlcharts/gvn-chart-gutenberg', array(
214 'editor_script' => 'gvn_gutenberg_charts',
215 'render_callback' => 'guaven_gutenberg_wrapper',
216 'attributes' => array(
217 'chart_id' => array(
218 'type' => 'string',
219 'default' => null
220 ),
221 'sqlcharts_inserted_script' => array(
222 'type' => 'number',
223 'default' => null
224 )
225 )
226 ));
227 }
228 add_action('init', 'guaven_register_gutenberg_blocks');
229
230
231
232 function guaven_sqlcharts_save_metabox_area($post_id, $post)
233 {
234 if (!isset($_POST['meta_box_nonce_field']) or !wp_verify_nonce($_POST['meta_box_nonce_field'], 'meta_box_nonce_action')) {
235 return $post->ID;
236 }
237 $fields = array(
238 "guaven_sqlcharts_chartheight",
239 "guaven_sqlcharts_chartwidth",
240 "guaven_sqlcharts_graphtype",
241 "guaven_sqlcharts_xarg_s",
242 "guaven_sqlcharts_xarg_l",
243 "guaven_sqlcharts_yarg_s",
244 "guaven_sqlcharts_yarg_l",
245 "guaven_sqlcharts_tablepart",
246 "guaven_sqlcharts_variables",
247 "guaven_sqlcharts_formpartrole",
248 "guaven_sqlcharts_formpartbutton",
249 "guaven_sqlcharts_dbhost",
250 "guaven_sqlcharts_dblogin",
251 "guaven_sqlcharts_dbname",
252 "guaven_sqlcharts_colors",
253 "guaven_sqlcharts_begin_with_0_x",
254 "guaven_sqlcharts_begin_with_0_y",
255 "guaven_sqlcharts_round_y_values",
256 "guaven_sqlcharts_legend_position",
257 "guaven_sqlcharts_nostacked"
258 );
259 foreach ($fields as $key => $value) {
260 if(isset($_POST[$value]))$newval=esc_attr($_POST[$value]);
261 else $newval='';
262
263 update_post_meta($post->ID, $value, $newval);
264 }
265 if(!empty($_POST["guaven_sqlcharts_dbpass"])){
266 $encpass=guaven_sqlcharts_encrypt_decrypt('encrypt',$_POST["guaven_sqlcharts_dbpass"]);
267 update_post_meta($post->ID, 'guaven_sqlcharts_dbpass', ['encrypted',$encpass]);
268 }
269 update_post_meta($post->ID, 'guaven_sqlcharts_code', esc_attr(str_replace("'",'"',stripslashes($_POST['guaven_sqlcharts_code']))) );
270 }
271 add_action('save_post', 'guaven_sqlcharts_save_metabox_area', 1, 2);
272 // save the custom fields
273
274
275
276
277 function guaven_sqlcharts_libloads($type, $step)
278 {
279 $stty = array(
280 'bar' => array(
281 'packages' => "'corechart', 'bar'",
282 'charts' => "BarChart"
283 ),
284 'column' => array(
285 'packages' => "'corechart', 'bar'",
286 'charts' => "ColumnChart"
287 ),
288 'area' => array(
289 'packages' => "'corechart'",
290 'charts' => "AreaChart"
291 ),
292 'pie' => array(
293 'packages' => "'corechart'",
294 'charts' => "PieChart"
295 ),
296 '3dpie' => array(
297 'packages' => "'corechart'",
298 'charts' => "PieChart"
299 )
300 );
301
302 return $stty[$type][$step];
303 }
304 function gvn_chart_check_sql_query($sql)
305 {
306 $blacklister = array(
307 "delete",
308 "update",
309 "insert",
310 "drop",
311 "truncate",
312 "alter"
313 ); //add all
314 $blacklister_f = 0;
315 foreach ($blacklister as $key => $value) {
316 if (strpos($sql, $value) !== false)
317 $blacklister_f = 1;
318 }
319 return $blacklister_f;
320 }
321
322 function guaven_get_labels_and_values($id, $fvs)
323 {
324 $values = array();
325 $labels = array();
326 $xarg_s = get_post_meta($id, 'guaven_sqlcharts_xarg_s', true);
327 $xarg_l = get_post_meta($id, 'guaven_sqlcharts_xarg_l', true);
328 $yarg_s = get_post_meta($id, 'guaven_sqlcharts_yarg_s', true);
329 $yarg_l = get_post_meta($id, 'guaven_sqlcharts_yarg_l', true);
330 $chartype = array(
331 'line_l' => 'Line',
332 'pie_l' => 'Pie',
333 'donut_l' => 'Pie',
334 'bar_l' => 'Bar',
335 'horizontalbar_l' => 'Horizontal Bar',
336 'area_l' => 'Line'
337 );
338 foreach ($fvs as $key => $value) {
339 $values[] = $value->$yarg_s;
340 $labels[] = '"' . $value->$xarg_s . '"';
341 }
342 return array(
343 $labels,
344 $values,
345 explode(";", $yarg_l),
346 explode(";", $xarg_l)
347 );
348 }
349
350 function guaven_sqlcharts_print_chart_js($tip_g, $title, $labels, $values, $ylabel, $pid = null)
351 {
352 if ($tip_g == 'line_l') {
353 guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'false', $pid);
354 }
355 if ($tip_g == 'area_l') {
356 guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'true', $pid);
357 } elseif ($tip_g == 'pie_l') {
358 guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid);
359 } elseif ($tip_g == 'donut_l') {
360 guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, 'doughnut');
361 } elseif ($tip_g == 'bar_l') {
362 guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, 'bar', $pid);
363 } elseif ($tip_g == 'horizontalbar_l') {
364 guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, 'horizontalBar', $pid);
365 }
366 elseif($tip_g == 'custom'){
367 guaven_sqlcharts_custom($title, $labels, $values, $ylabel, $pid);
368 }
369 elseif ($tip_g == 'polar_l') {
370 guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, 'polarArea');
371 }
372 }
373
374 function guaven_sqlcharts_custom($title, $labels, $values, $ylabel, $pid){
375 do_action('guaven_sqlcharts_custom',$title, $labels, $values, $ylabel, $pid);
376 }
377
378 function gvn_chart_put_variables($sql,$pid){
379 $sql_initial=$sql;
380
381
382 $default_tag_keys=['{current_user_id}','{current_user_login}','{current_user_email}','{current_user_display_name}'];
383 if(is_user_logged_in( )){
384 $currentuser=wp_get_current_user();
385 $default_tag_values=[$currentuser->ID,$currentuser->user_login,$currentuser->user_email,$currentuser->user_display_name];
386 }
387 else {
388 $default_tag_values='';
389 }
390 $sql_initial=str_replace($default_tag_keys,$default_tag_values,$sql_initial);
391
392 $variables_raw=get_post_meta($pid,'guaven_sqlcharts_variables',true);
393 $variables_arr=explode("|",$variables_raw);
394 foreach($variables_arr as $varfield){
395 $varfield_arr=explode("~",$varfield);
396 if (count($varfield_arr)<3) continue;
397 $varfield_arr=array_map("trim",$varfield_arr);
398 if (!empty($_GET[$varfield_arr[0]])) $varreplacement=$_GET[$varfield_arr[0]]; else $varreplacement=$varfield_arr[1];
399 if (!is_numeric($varreplacement) and strpos($varreplacement,'()')===false) $varreplacement='"'.$varreplacement.'"';
400
401 $sql_initial=str_replace('{'.$varfield_arr[0].'}',$varreplacement,$sql_initial);
402 }
403 return $sql_initial;
404 }
405
406 function gvn_chart_top_form($atts){
407 if (get_post_meta($atts["id"],'guaven_sqlcharts_formpartrole',true)!='' and !is_user_logged_in()) return;
408 $topform='';$dateexists=false;
409 $variables_raw=get_post_meta($atts['id'],'guaven_sqlcharts_variables',true);
410 $variables_raw=explode("|",$variables_raw);
411 foreach ($variables_raw as $vrow){
412 $vrow_arr=explode("~",$vrow);
413 $vrow_arr=array_map("trim",$vrow_arr);
414 if (empty($vrow_arr[3])) continue;
415 $gvalue=!empty($_GET[$vrow_arr[0]])?esc_attr(urldecode($_GET[$vrow_arr[0]])):'';
416 $dvalue=(strpos($vrow_arr[1],'()')===false)?esc_attr($vrow_arr[1]):'';
417 if ($vrow_arr[3]=='date') {
418 $dateexists=true;
419 $topform.= $vrow_arr[2].' <input autocomplete="off" style="max-width:210px" type="text"
420 value="'.$gvalue.'"
421 data-toggle="datepicker" name="'.$vrow_arr[0].'" placeholder="'.$dvalue.'">
422 ';}
423 else {
424 $topform.= $vrow_arr[2].' <input autocomplete="off" style="max-width:210px;'.($vrow_arr[3]=='number'?'width:100px;':'').'"
425 type="'.$vrow_arr[3].'"
426 value="'.$gvalue.'" name="'.$vrow_arr[0].'" placeholder="'.$dvalue.'">
427 ';
428 }
429 }
430 if (!empty($topform)) {
431 $topform='<form method="get" action="" class="guaven_sqlcharts_form">'.$topform.'
432 <input type="submit"
433 value="'.(get_post_meta($atts['id'], 'guaven_sqlcharts_formpartbutton', true)!=''?esc_attr(get_post_meta($atts['id'], 'guaven_sqlcharts_formpartbutton', true)):'OK').'"></form>';
434 if ($dateexists) $topform.='<script>setTimeout(function(){jQuery(\'[data-toggle="datepicker"]\').datepicker({format: \'yyyy-mm-dd\'});
435 },300);</script>';
436 return $topform ;
437 }
438 return;
439 }
440
441
442 function guaven_sqlcharts_encrypt_decrypt($action, $string)
443 {
444 $output = false;
445 $encrypt_method = "AES-256-CBC";
446 $secret_key = 'GWSCHARTPL2022.2016.';
447 $secret_iv = 'GWSCHARTPL2016.2022';
448 $key = hash('sha256', $secret_key);
449 $iv = substr(hash('sha256', $secret_iv), 0, 16);
450 if ( $action == 'encrypt' ) {
451 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
452 $output = base64_encode($output);
453 } else if( $action == 'decrypt' ) {
454 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
455 }
456 return $output;
457 }
458
459 function guaven_sqlcharts_local_shortcode($atts)
460 {
461 $remote_host=get_post_meta($atts['id'], 'guaven_sqlcharts_dbhost', true);
462 if ($remote_host!=''){
463 $remote_db=get_post_meta($atts['id'], 'guaven_sqlcharts_dbname', true);
464 $remote_login=get_post_meta($atts['id'], 'guaven_sqlcharts_dblogin', true);
465 $remote_pass=get_post_meta($atts['id'], 'guaven_sqlcharts_dbpass', true);
466 if(is_array($remote_pass)){
467 $remote_pass=guaven_sqlcharts_encrypt_decrypt('decrypt',$remote_pass[1]);
468 }
469 $wpdb=new wpdb($remote_login,$remote_pass,$remote_db,$remote_host);
470 }
471 else {
472 global $wpdb;
473 }
474
475 $GLOBALS["guaven_sqlcharts_atts"]=$atts;
476
477 $sql = html_entity_decode(get_post_meta($atts['id'], 'guaven_sqlcharts_code', true));
478 $sql=gvn_chart_put_variables($sql,$atts['id']);
479 $blacklister_f = gvn_chart_check_sql_query($sql);
480 if ($blacklister_f == 1)
481 return 'You given SQL code contains forbidden commands. Remember that you should only use SELECT queries';
482 $tip_g = get_post_meta($atts['id'], 'guaven_sqlcharts_graphtype', true);
483
484 for($i=1;$i<20;$i++){ $replacearg=!empty($atts["arg".$i])?$atts["arg".$i]:0;
485 $sql=str_replace("{arg".$i."}",esc_sql($replacearg),$sql);}
486 $sql_split = explode(';', $sql);
487 $labels_and_values = array();
488 $post_g = get_post($atts['id']);
489
490 global $sqlcharts_inserted_script;
491 for ($i = 0; $i < count($sql_split); $i++) {
492 if (!empty($sql_split[$i])) {
493
494 $fvs = $wpdb->get_results($sql_split[$i]);
495 if (strpos($_SERVER["REQUEST_URI"],'wp-admin')!==false){
496 $wpdb->show_errors();
497 ob_start();
498 $wpdb->print_error();
499 $printerror = ob_get_clean();
500 if ($printerror != '' and strpos($printerror, "[]") === false)
501 return $printerror;
502 elseif (empty($fvs))
503 return 'Your SQL returnes empty date, please recheck your SQL query above';
504 }
505
506 ob_start();
507
508 if (empty($sqlcharts_inserted_script))
509 $sqlcharts_inserted_script = 1;
510 $labels_and_values[$i] = guaven_get_labels_and_values($atts['id'], $fvs);
511 $labels[$i] = $labels_and_values[$i][0];
512 $values[$i] = $labels_and_values[$i][1];
513 $ylabel[$i] = !empty($labels_and_values[$i][2][$i]) ? $labels_and_values[$i][2][$i] : '';
514 $xlabel[$i] = !empty($labels_and_values[$i][3][$i]) ? $labels_and_values[$i][3][$i] : '';
515 }
516 }
517
518 echo gvn_chart_top_form($atts);
519 ?>
520 <canvas
521 id="ct-chart_<?php echo $sqlcharts_inserted_script; ?>"
522 class="guaven_chart_canvas"
523 style="width: <?php echo get_post_meta($atts['id'], 'guaven_sqlcharts_chartwidth', true); ?>px !important; height: <?php echo get_post_meta($atts['id'], 'guaven_sqlcharts_chartheight', true); ?>px !important;"
524 ></canvas>
525
526 <script type="text/javascript" class="gvn_charts_script" async>
527 var ctx = jQuery("#ct-chart_<?php
528 echo $sqlcharts_inserted_script;
529 ?>");
530
531 <?php
532 guaven_sqlcharts_print_chart_js($tip_g, $post_g->post_title, $labels, $values, $ylabel, $atts['id']);
533 ?>
534 </script>
535
536 <?php
537 if (!empty($atts["table"])) echo guaven_sqlcharts_tablepart($post_g->post_title, $labels, $values, $ylabel,$xlabel);
538 $sqlcharts_inserted_script++;
539 return ob_get_clean();
540 }
541
542 add_shortcode('gvn_schart_2', 'guaven_sqlcharts_local_shortcode');
543
544 function guaven_sqlcharts_colors($index, $pid = null){
545 if(!isset($pid)) {
546 global $post;
547 $pid = $post->ID;
548 }
549 $colors=get_post_meta($pid,'guaven_sqlcharts_colors',true);
550 $colors=explode(",",$colors);
551 if (!empty($colors[$index])) return $colors[$index];
552 return rand(0, 255) . ',' . rand(0, 255) . ',' . rand(0, 255);
553 }
554
555 function guaven_sqlcharts_toshowlegend($pid){
556 $guaven_sqlcharts_legend_position=get_post_meta($pid, 'guaven_sqlcharts_legend_position', true);
557 if(in_array($guaven_sqlcharts_legend_position,['top','bottom','left','right'])){
558 $display='true';$position=$guaven_sqlcharts_legend_position;
559 }
560 else {
561 $display='false';$position='top';
562 }
563 return "legend: {display: ".$display.",position:'".$position."'},";
564 }
565
566 function guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, $type = 'bar', $pid = null)
567 {
568 ?>
569 var data = {
570 labels: [<?php
571 echo implode(",", $labels[0]);
572 ?>],
573 datasets: [
574 <?php
575 for ($i = 0; $i < count($values); $i++) {
576 ?>
577 {
578 <?php
579 if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){
580 //passing chartJS params via the shortcode
581 echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]);
582 }
583 ?>
584 label: "<?php
585 echo esc_attr($ylabel[$i]);
586 ?>",
587 backgroundColor: [
588 <?php
589 echo guaven_sqlcharts_colorgenerator(count($values[$i]), 0, 0, guaven_sqlcharts_colors($i, $pid));
590 ?>
591 ],
592 borderColor: [
593 <?php
594 echo guaven_sqlcharts_colorgenerator(count($values[$i]), 0, 0.2, guaven_sqlcharts_colors($i, $pid));
595 ?>
596 ],
597 borderWidth: 1,
598 data: [<?php
599 echo implode(",", $values[$i]);
600 ?>],
601 },
602 <?php
603 }
604 ?>
605 ]
606 };
607 var options={
608 <?php echo guaven_sqlcharts_toshowlegend($pid);?>
609 responsive: true,
610 scales: {
611 xAxes: [{
612 <?php echo (get_post_meta($pid, 'guaven_sqlcharts_nostacked', true) == 1) ? '':'stacked: true,';?>
613 ticks: {
614 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?>
615 }
616 }],
617 yAxes: [{
618 <?php echo (get_post_meta($pid, 'guaven_sqlcharts_nostacked', true) == 1) ? '':'stacked: true,';?>
619 ticks: {
620 <?php if(get_post_meta($pid, 'guaven_sqlcharts_round_y_values', true) == 1) echo 'precision: 0,'; ?>
621 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?>
622 }
623 }]
624 }
625 };
626 var myBarChart = new Chart(ctx, {
627 type: '<?php
628 echo $type;
629 ?>',
630 data: data,
631 options: options
632 });
633 <?php
634 }
635
636
637
638 function guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, $type = 'false', $pid = null)
639 {
640 ?>
641 var data = {
642 labels: [<?php
643 echo implode(",", $labels[0]);
644 ?>],
645 datasets: [
646 <?php
647 for ($i = 0; $i < count($values); $i++) {
648 ?>
649 {
650 <?php
651 if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){
652 //passing chartJS params via the shortcode
653 echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]);
654 }
655 ?>
656 label: "<?php
657 echo esc_attr($ylabel[$i]);
658 ?>",
659 fill: <?php echo $type=="false"?$type:($i==0?'"+1"':'"origin"');
660 ?>,
661 lineTension: 0.1,
662 backgroundColor: <?php
663 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
664 ?>
665 borderColor: <?php
666 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
667 ?>
668 pointBorderColor: <?php
669 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
670 ?>
671 pointHoverBackgroundColor: <?php
672 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
673 ?>
674 pointHoverBorderColor: <?php
675 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
676 ?>
677 data: [<?php
678 echo implode(",", $values[$i]);
679 ?>],
680 spanGaps: false,
681 },
682 <?php
683 }
684 ?>
685 ]
686 };
687 var myLineChart = new Chart(ctx, {
688 type: 'line',
689 data: data,
690 options: {
691 <?php echo guaven_sqlcharts_toshowlegend($pid);?>
692 responsive: true,
693 scales: {
694 xAxes: [{
695 display: true,
696 ticks: {
697 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?>
698 }
699 }],
700 yAxes: [{
701 ticks: {
702 <?php if(get_post_meta($pid, 'guaven_sqlcharts_round_y_values', true) == 1) echo 'precision: 0,'; ?>
703 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?>
704 }
705 }]
706 }
707 }
708 });
709 <?php
710 }
711
712
713 function guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, $type = 'pie')
714 {
715 ?>
716 var options={
717 <?php echo guaven_sqlcharts_toshowlegend($pid);?>
718 responsive: true,
719 };
720 var data = {
721 labels: [ <?php echo implode(",", $labels[0]);?>],
722 datasets: [
723 <?php
724 for ($i = 0; $i < count($values); $i++) {
725 ?>
726 {
727 <?php
728 if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){
729 //passing chartJS params via the shortcode
730 echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]);
731 }
732 ?>
733 data: [<?php
734 echo implode(",", $values[$i]);
735 ?>],
736 backgroundColor: [
737 <?php
738 foreach($values[$i] as $vci=>$valuecolor){
739 echo guaven_sqlcharts_colorgenerator(1, 0, 0, guaven_sqlcharts_colors($vci, $pid));
740 }
741 ?>
742 ],
743 hoverBackgroundColor: [
744 <?php
745 foreach($values[$i] as $vci=>$valuecolor){
746 echo guaven_sqlcharts_colorgenerator(1, 0, 0.2, guaven_sqlcharts_colors($vci, $pid));
747 }
748 ?>
749 ]
750 },
751 <?php
752 }
753 ?>
754 ]
755 };
756 var myPieChart = new Chart(ctx,{
757 type: '<?php
758 echo $type;
759 ?>',
760 data: data,
761 options: options
762 });
763 <?php
764 }
765
766
767
768
769 function guaven_sqlcharts_colorgenerator($count, $indic, $darkness = 0, $initcolor = '255,0,0')
770 {
771 if (strpos($initcolor,'#')===0) {
772 $split = str_split(substr($initcolor,1), 2);
773 $r = hexdec($split[0]);
774 $g = hexdec($split[1]);
775 $b = hexdec($split[2]);
776 $ret='';
777 for ($i = 0; $i < $count; $i++) {
778 $ret .= "'rgba(" . $r . ", " . $g . ", " . $b . ",".($darkness + 0.8 - $indic * $i * 0.8 / ($count)).")',
779 ";
780 }
781 return $ret;
782 }
783 $initial_colors = array(
784 'linebg' => 'red',
785 'linebr' => 'yellow',
786 'linebc' => 'green',
787 'linehbg' => 'white',
788 'linehbc' => 'black'
789 );
790 if (!empty($initial_colors[$count]))
791 return '"' . $initial_colors[$count] . '",
792 ';
793 $ret = '';
794 for ($i = 0; $i < $count; $i++) {
795 $ret .= "'rgba(" . $initcolor . "," . ($darkness + 0.8 - $indic * $i * 0.8 / ($count)) . ")',
796 ";
797 }
798 return $ret;
799 }
800
801 function guaven_sqlcharts_tablepart($title, $labels, $values, $ylabel,$xlabel){
802 $tabledata='';
803 $fcol=[];$scol=[];
804 $tablein='<br>';
805 foreach($values as $valuerow){
806 foreach ($valuerow as $key => $value) {
807 $fcol[$key]='<td>'.str_replace('"',"",$labels[0][$key]).'</td>';
808 $scol[$key][]='<td>'.$value.'</td>';
809
810 }
811 }
812 foreach($fcol as $key=>$value){
813 $tablein.='<tr>'.$value.implode(" ",$scol[$key]).'</tr>'.PHP_EOL;
814 }
815 $tabledata.='<table><tr><th>'.$xlabel[0].'</th><th>'.implode("</th><th>",$ylabel).'</th></tr>
816 '.$tablein.'</table><br>';
817 return $tabledata;
818
819 }
820