| @@ -110,8 +110,9 @@ | ||
| 110 | 110 | add_action('admin_notices', 'guaven_sqlcharts_onboarding_notice'); |
| 111 | 111 | |
| 112 | 112 | function guaven_sqlcharts_onboarding_notice_dismissed(){ |
| 113 | 113 | check_ajax_referer('notice_dismissed', 'nonce'); |
| 114 | + if (!current_user_can('manage_options')) return; | |
| 114 | 115 | |
| 115 | 116 | if(empty($_POST['type']))return; |
| 116 | 117 | switch ($_POST['type']){ |
| 117 | 118 | case 'onboarding_notice': |
| @@ -224,9 +225,31 @@ | ||
| 224 | 225 | 'item_updated' => __('Chart updated.','guaven_sqlcharts'), |
| 225 | 226 | ), |
| 226 | 227 | |
| 227 | 228 | 'public' => true, |
| 229 | + 'show_in_rest' => false, | |
| 228 | 230 | 'menu_icon' => 'dashicons-chart-pie', |
| 231 | + // Charts execute SQL, so every primitive capability of this post type maps to manage_options. | |
| 232 | + // Contributors/Authors cannot create, edit, publish or delete charts through any WordPress | |
| 233 | + // entry point (admin UI, XML-RPC, REST). Published charts stay viewable on the front end. | |
| 234 | + // Only primitive capabilities are remapped: mapping the meta capabilities edit_post/read_post/ | |
| 235 | + // delete_post to manage_options would make WordPress treat manage_options itself as a meta | |
| 236 | + // capability and break that check site-wide. | |
| 237 | + 'capability_type' => 'post', | |
| 238 | + 'map_meta_cap' => true, | |
| 239 | + 'capabilities' => array( | |
| 240 | + 'edit_posts' => 'manage_options', | |
| 241 | + 'edit_others_posts' => 'manage_options', | |
| 242 | + 'edit_published_posts' => 'manage_options', | |
| 243 | + 'edit_private_posts' => 'manage_options', | |
| 244 | + 'publish_posts' => 'manage_options', | |
| 245 | + 'read_private_posts' => 'manage_options', | |
| 246 | + 'delete_posts' => 'manage_options', | |
| 247 | + 'delete_private_posts' => 'manage_options', | |
| 248 | + 'delete_published_posts' => 'manage_options', | |
| 249 | + 'delete_others_posts' => 'manage_options', | |
| 250 | + 'create_posts' => 'manage_options', | |
| 251 | + ), | |
| 229 | 252 | 'supports' => array( |
| 230 | 253 | 'title', |
| 231 | 254 | 'postmeta' |
| 232 | 255 | ), |
| @@ -235,8 +258,14 @@ | ||
| 235 | 258 | |
| 236 | 259 | guaven_sqlcharts_load_defaults(); |
| 237 | 260 | } |
| 238 | 261 | |
| 262 | +// All guaven_sqlcharts_* meta keys are protected: they cannot be written through the Custom Fields box, | |
| 263 | +// XML-RPC or the REST API. The plugin's own save handler (update_post_meta) is not affected. | |
| 264 | +add_filter('is_protected_meta', function ($protected, $meta_key) { | |
| 265 | + return strpos((string) $meta_key, 'guaven_sqlcharts_') === 0 ? true : $protected; | |
| 266 | +}, 10, 2); | |
| 267 | + | |
| 239 | 268 | // "Add title" placeholder on the chart edit screen |
| 240 | 269 | add_filter('enter_title_here', function ($title, $post) { |
| 241 | 270 | if (!empty($post) and $post->post_type == 'gvn_schart') return __('Chart name', 'guaven_sqlcharts'); |
| 242 | 271 | return $title; |
| @@ -423,8 +452,11 @@ | ||
| 423 | 452 | { |
| 424 | 453 | if (!isset($_POST['meta_box_nonce_field']) or !wp_verify_nonce($_POST['meta_box_nonce_field'], 'meta_box_nonce_action')) { |
| 425 | 454 | return $post->ID; |
| 426 | 455 | } |
| 456 | + if ($post->post_type != 'gvn_schart' or !current_user_can('manage_options') or (defined('DOING_AUTOSAVE') and DOING_AUTOSAVE)) { | |
| 457 | + return $post->ID; | |
| 458 | + } | |
| 427 | 459 | $fields = array( |
| 428 | 460 | "guaven_sqlcharts_chartheight", |
| 429 | 461 | "guaven_sqlcharts_chartwidth", |
| 430 | 462 | "guaven_sqlcharts_graphtype", |
| @@ -444,9 +476,10 @@ | ||
| 444 | 476 | "guaven_sqlcharts_begin_with_0_y", |
| 445 | 477 | "guaven_sqlcharts_round_y_values", |
| 446 | 478 | "guaven_sqlcharts_legend_position", |
| 447 | 479 | "guaven_sqlcharts_nostacked", |
| 448 | - "guaven_sqlcharts_forcetooltips" | |
| 480 | + "guaven_sqlcharts_forcetooltips", | |
| 481 | + "guaven_sqlcharts_timeaxis" | |
| 449 | 482 | ); |
| 450 | 483 | foreach ($fields as $key => $value) { |
| 451 | 484 | if(isset($_POST[$value]))$newval=esc_attr($_POST[$value]); |
| 452 | 485 | else $newval=''; |
| @@ -479,13 +512,57 @@ | ||
| 479 | 512 | // save the custom fields |
| 480 | 513 | |
| 481 | 514 | |
| 482 | 515 | |
| 516 | +// Removes string literals (contents only), backtick identifiers and comments from SQL so keyword checks | |
| 517 | +// see the same code MySQL will execute. "/*!" and "/*+" comments are executable in MySQL and are kept. | |
| 518 | +function guaven_sqlcharts_strip_sql_literals($sql) | |
| 519 | +{ | |
| 520 | + $out = ''; $len = strlen($sql); $i = 0; | |
| 521 | + while ($i < $len) { | |
| 522 | + $c = $sql[$i]; | |
| 523 | + if ($c === "'" or $c === '"' or $c === '`') { | |
| 524 | + $out .= $c . $c; $i++; | |
| 525 | + while ($i < $len) { | |
| 526 | + if ($sql[$i] === '\\' and $c !== '`') { $i += 2; continue; } | |
| 527 | + if ($sql[$i] === $c) { if ($i + 1 < $len and $sql[$i + 1] === $c) { $i += 2; continue; } $i++; break; } | |
| 528 | + $i++; | |
| 529 | + } | |
| 530 | + continue; | |
| 531 | + } | |
| 532 | + if ($c === '#' or ($c === '-' and substr($sql, $i, 2) === '--' and ($i + 2 >= $len or ctype_space($sql[$i + 2])))) { | |
| 533 | + $nl = strpos($sql, "\n", $i); $i = ($nl === false) ? $len : $nl; continue; | |
| 534 | + } | |
| 535 | + if ($c === '/' and substr($sql, $i, 2) === '/*' and !in_array(substr($sql, $i + 2, 1), array('!', '+'), true)) { | |
| 536 | + $close = strpos($sql, '*/', $i + 2); $i = ($close === false) ? $len : $close + 2; $out .= ' '; continue; | |
| 537 | + } | |
| 538 | + $out .= $c; $i++; | |
| 539 | + } | |
| 540 | + return $out; | |
| 541 | +} | |
| 542 | + | |
| 543 | +// Returns 1 when the (fully substituted) SQL must not run, 0 when it is a read-only query. | |
| 544 | +// Called after every {tag}/{argN} replacement so user-supplied values are covered too. | |
| 483 | 545 | function gvn_chart_check_sql_query($sql) |
| 484 | 546 | { |
| 485 | - // case-insensitive, word-boundary check: only read-only SELECT queries are allowed | |
| 486 | - $pattern = '/\b(delete|update|insert|replace|drop|truncate|alter|create|rename|grant|revoke|call|handler|load\s+data|load_file|outfile|dumpfile)\b/i'; | |
| 487 | - return preg_match($pattern, $sql) ? 1 : 0; | |
| 547 | + // 1) data-changing statements: checked on the raw text, exactly as in every previous version | |
| 548 | + $write = '/\b(delete|update|insert|replace|drop|truncate|alter|create|rename|grant|revoke|call|handler|load\s+data|load_file|outfile|dumpfile)\b/i'; | |
| 549 | + if (preg_match($write, $sql)) return 1; | |
| 550 | + | |
| 551 | + // 2) further dangerous statements, matched outside string literals and comments so that ordinary | |
| 552 | + // values such as status = 'reset' keep working | |
| 553 | + $danger = '/\b(prepare|execute|deallocate|lock|unlock|kill|shutdown|flush|reset|purge|install|uninstall|import' | |
| 554 | + . '|set\s+(?:global|session|persist|persist_only|password|@@)|start\s+(?:replica|slave|group_replication)|stop\s+(?:replica|slave)|change\s+(?:master|replication))\b/i'; | |
| 555 | + if (preg_match($danger, guaven_sqlcharts_strip_sql_literals($sql))) return 1; | |
| 556 | + | |
| 557 | + // 3) every ";"-separated statement must be a read statement. The renderer sends each segment to the | |
| 558 | + // database on its own, so this stops a value from smuggling a second statement behind a ";". | |
| 559 | + foreach (explode(';', $sql) as $segment) { | |
| 560 | + $segment = ltrim(guaven_sqlcharts_strip_sql_literals($segment), " \t\r\n("); | |
| 561 | + if ($segment === '') continue; | |
| 562 | + if (!preg_match('/^(select|with|show|describe|desc|explain)\b/i', $segment)) return 1; | |
| 563 | + } | |
| 564 | + return 0; | |
| 488 | 565 | } |
| 489 | 566 | |
| 490 | 567 | function guaven_get_labels_and_values($id, $fvs) |
| 491 | 568 | { |
| @@ -579,9 +656,9 @@ | ||
| 579 | 656 | if (count($varfield_arr)<3) continue; |
| 580 | 657 | $varfield_arr=array_map("trim",$varfield_arr); |
| 581 | 658 | if (!empty($_GET[$varfield_arr[0]])) { |
| 582 | 659 | // User-supplied input: no () bypass allowed — sanitize strictly |
| 583 | - $varreplacement = sanitize_text_field(wp_unslash($_GET[$varfield_arr[0]])); | |
| 660 | + $varreplacement = str_replace(';', '', sanitize_text_field(wp_unslash($_GET[$varfield_arr[0]]))); | |
| 584 | 661 | if (is_numeric($varreplacement)) { |
| 585 | 662 | $varreplacement = $varreplacement + 0; |
| 586 | 663 | } else { |
| 587 | 664 | $varreplacement = '"' . esc_sql($varreplacement) . '"'; |
| @@ -675,8 +752,10 @@ | ||
| 675 | 752 | |
| 676 | 753 | function guaven_sqlcharts_local_shortcode($atts) { |
| 677 | 754 | if(empty($atts['id']))return 'ID is missing.'; |
| 678 | 755 | $atts['id']=intval($atts['id']); |
| 756 | + $post_g = get_post($atts['id']); | |
| 757 | + if (!$post_g or $post_g->post_type != 'gvn_schart') return 'Chart not found.'; | |
| 679 | 758 | $remote_host=get_post_meta($atts['id'], 'guaven_sqlcharts_dbhost', true); |
| 680 | 759 | if ($remote_host!=''){ |
| 681 | 760 | $remote_db=get_post_meta($atts['id'], 'guaven_sqlcharts_dbname', true); |
| 682 | 761 | $remote_login=get_post_meta($atts['id'], 'guaven_sqlcharts_dblogin', true); |
| @@ -693,29 +772,34 @@ | ||
| 693 | 772 | $GLOBALS["guaven_sqlcharts_atts"]=$atts; |
| 694 | 773 | |
| 695 | 774 | $sql = guaven_sqlcharts_get_code($atts['id']); |
| 696 | 775 | if(empty($sql))return 'SQL query is missing.'; |
| 697 | - $sql=gvn_chart_put_variables($sql,$atts['id']); | |
| 698 | 776 | |
| 777 | + // {arg1}..{arg19} come from shortcode attributes: [gvn_schart_2 id="1" arg1="41"]. | |
| 778 | + // Substituted directly (not via wpdb::prepare) so the same tag may appear any number of times, | |
| 779 | + // e.g. in every query of a ";"-separated comparison chart. Numbers are inserted as-is, anything | |
| 780 | + // else is escaped and quoted; a tag already wrapped in quotes ('{arg1}') is not double-quoted. | |
| 781 | + // ";" is removed from values because the finished SQL is split on ";" below. | |
| 782 | + for($i=1;$i<20;$i++){ | |
| 783 | + $tag = '{arg'.$i.'}'; | |
| 784 | + if (strpos($sql, $tag) === false) continue; | |
| 785 | + $replacearg = !empty($atts['arg'.$i]) ? $atts['arg'.$i] : 0; | |
| 786 | + if (is_numeric($replacearg)) $replacearg = $replacearg + 0; | |
| 787 | + else $replacearg = "'" . esc_sql(str_replace(';', '', sanitize_text_field((string) $replacearg))) . "'"; | |
| 788 | + $sql = str_replace(array("'".$tag."'", '"'.$tag.'"', $tag), $replacearg, $sql); | |
| 789 | + } | |
| 699 | 790 | |
| 791 | + $sql=gvn_chart_put_variables($sql,$atts['id']); | |
| 700 | 792 | $sql=apply_filters('guaven_sqlcharts_rendered_sql',$sql,$atts); |
| 701 | 793 | |
| 794 | + // command check on the final SQL, after every shortcode argument and filter value is in place | |
| 702 | 795 | $blacklister_f = gvn_chart_check_sql_query($sql); |
| 703 | 796 | if ($blacklister_f == 1)return 'You given SQL code contains forbidden commands. Remember that you should only use SELECT queries'; |
| 704 | 797 | $tip_g = guaven_sqlcharts_normalize_type(get_post_meta($atts['id'], 'guaven_sqlcharts_graphtype', true)); |
| 705 | 798 | |
| 706 | - for($i=1;$i<20;$i++){ | |
| 707 | - if(strpos($sql,"{arg".$i."}")!==false){ | |
| 708 | - $replacearg=!empty($atts["arg".$i])?$atts["arg".$i]:0; | |
| 709 | - $sql = str_replace("{arg".$i."}", "%s", $sql); | |
| 710 | - $sql=$wpdb->prepare($sql,$replacearg); | |
| 711 | - } | |
| 712 | - | |
| 713 | - } | |
| 714 | - | |
| 715 | 799 | $sql_split = explode(';', $sql); |
| 716 | 800 | $labels_and_values = array(); |
| 717 | - $post_g = get_post($atts['id']); | |
| 801 | + $labels = $values = $ylabel = $xlabel = array(); | |
| 718 | 802 | |
| 719 | 803 | global $sqlcharts_inserted_script; |
| 720 | 804 | ob_start(); |
| 721 | 805 | for ($i = 0; $i < count($sql_split); $i++) { |
| @@ -789,17 +873,30 @@ | ||
| 789 | 873 | if (!shortcode_exists('gvn_schart')) { |
| 790 | 874 | add_shortcode('gvn_schart', 'guaven_sqlcharts_local_shortcode'); |
| 791 | 875 | } |
| 792 | 876 | |
| 877 | +// [gvn_schart_2_cached id="1" expire="3600" arg1=".."] – same as gvn_schart_2 but the output is kept in a | |
| 878 | +// transient. All other attributes (argN, width, height, table, params) are passed through, and each | |
| 879 | +// distinct set of attributes gets its own cache entry. Append ?force_sql_cache_reload to the URL to bypass. | |
| 793 | 880 | add_shortcode("gvn_schart_2_cached",function($atts){ |
| 794 | 881 | if(empty($atts["id"]))return; |
| 795 | 882 | $atts["id"]=intval($atts["id"]); |
| 796 | - $is_logged_in=is_user_logged_in()?'':'_guest'; | |
| 797 | 883 | $expire=!empty($atts["expire"])?intval($atts["expire"]):3600; |
| 798 | - $cached=get_transient('cached_sql_charts_'.$atts["id"].$is_logged_in); | |
| 884 | + $inner_atts=$atts; | |
| 885 | + unset($inner_atts['expire']); | |
| 886 | + // One cache entry per user (charts may use {current_user_*} tags), per set of shortcode attributes | |
| 887 | + // and per value of every dynamic filter this chart reads from the URL. A visitor can therefore | |
| 888 | + // never be served, or pre-seed, a result computed for someone else or for other filter values. | |
| 889 | + $key_parts = array('atts' => $inner_atts, 'user' => is_user_logged_in() ? get_current_user_id() : 0, 'get' => array()); | |
| 890 | + foreach (explode('|', (string) get_post_meta($atts['id'], 'guaven_sqlcharts_variables', true)) as $vrow) { | |
| 891 | + $vname = trim(current(explode('~', $vrow))); | |
| 892 | + if ($vname !== '' and isset($_GET[$vname])) $key_parts['get'][$vname] = sanitize_text_field(wp_unslash($_GET[$vname])); | |
| 893 | + } | |
| 894 | + $key = 'cached_sql_charts_' . $atts["id"] . '_' . md5(serialize($key_parts)); | |
| 895 | + $cached=get_transient($key); | |
| 799 | 896 | if(!empty($cached) and !isset($_GET["force_sql_cache_reload"]) )return $cached; |
| 800 | - $tobecached=do_shortcode('[gvn_schart_2 id="'.$atts["id"].'"]'); | |
| 801 | - set_transient('cached_sql_charts_'.$atts["id"].$is_logged_in, $tobecached,$expire);//you can change 3600 yourself | |
| 897 | + $tobecached=guaven_sqlcharts_local_shortcode($inner_atts); | |
| 898 | + set_transient($key, $tobecached,$expire); | |
| 802 | 899 | return $tobecached; |
| 803 | 900 | }); |
| 804 | 901 | |
| 805 | 902 | // fixed, colorblind-friendly default palette (Tableau 10) used when no custom colors are set |
| @@ -845,21 +942,70 @@ | ||
| 845 | 942 | if ($text === '' or ($which == 'y' and strpos($text, ';') !== false)) return ''; |
| 846 | 943 | return 'title: {display: true, text: ' . wp_json_encode($text) . '},'; |
| 847 | 944 | } |
| 848 | 945 | |
| 946 | +// "params" shortcode attribute: extra Chart.js dataset options, e.g. params="borderWidth: 3, borderDash: [5,5],". | |
| 947 | +// The text is placed inside the inline <script>, so only a conservative character set is accepted: | |
| 948 | +// no parentheses, semicolons, "=", "<", ">", "/", "\\", "+" or backticks, which rules out executable JavaScript. | |
| 949 | +function guaven_sqlcharts_dataset_params(){ | |
| 950 | + $params = isset($GLOBALS["guaven_sqlcharts_atts"]["params"]) ? (string) $GLOBALS["guaven_sqlcharts_atts"]["params"] : ''; | |
| 951 | + if ($params === '' or !preg_match('/^[A-Za-z0-9_\s,:.\'"#%\-\[\]{}]+$/', $params)) return ''; | |
| 952 | + return $params; | |
| 953 | +} | |
| 954 | + | |
| 849 | 955 | // dataset label as a safe JS string literal (labels saved before 3.0.1 may hold HTML entities) |
| 850 | 956 | function guaven_sqlcharts_js_label($label){ |
| 851 | 957 | return wp_json_encode(html_entity_decode((string) $label, ENT_QUOTES, 'UTF-8')); |
| 852 | 958 | } |
| 853 | 959 | |
| 960 | +// Parses an X value for the "time axis" option. Accepts YYYY, YYYY-MM, YYYY-MM-DD, optionally followed | |
| 961 | +// by HH:MM or HH:MM:SS. Returns a UTC timestamp in milliseconds, or false when the value is not a date. | |
| 962 | +function guaven_sqlcharts_parse_date($str){ | |
| 963 | + $str = trim((string) $str); | |
| 964 | + if (!preg_match('/^(\d{4})(?:-(\d{1,2})(?:-(\d{1,2})(?:[ T](\d{1,2}):(\d{2})(?::(\d{2}))?)?)?)?$/', $str, $m)) return false; | |
| 965 | + $y = (int) $m[1]; $mo = isset($m[2]) ? (int) $m[2] : 1; $d = isset($m[3]) ? (int) $m[3] : 1; | |
| 966 | + $h = isset($m[4]) ? (int) $m[4] : 0; $mi = isset($m[5]) ? (int) $m[5] : 0; $sec = isset($m[6]) ? (int) $m[6] : 0; | |
| 967 | + if (!checkdate($mo, $d, $y) or $h > 23 or $mi > 59 or $sec > 59) return false; | |
| 968 | + return gmmktime($h, $mi, $sec, $mo, $d, $y) * 1000; | |
| 969 | +} | |
| 970 | + | |
| 971 | +// "Scale X axis by date/time" option. Returns, per dataset, a list of "{x:<ms>,y:<value>}" JS point | |
| 972 | +// literals when the option is on and every X value is a date; false otherwise (normal category axis). | |
| 973 | +function guaven_sqlcharts_time_axis_points($pid, $values){ | |
| 974 | + if (get_post_meta($pid, 'guaven_sqlcharts_timeaxis', true) != 1) return false; | |
| 975 | + $out = array(); | |
| 976 | + $has_point = false; | |
| 977 | + foreach ($values as $key_ak => $series) { | |
| 978 | + $out[$key_ak] = array(); | |
| 979 | + foreach ($series as $x => $y) { | |
| 980 | + $ts = guaven_sqlcharts_parse_date($x); | |
| 981 | + if ($ts === false) return false; | |
| 982 | + $out[$key_ak][] = '{x:' . $ts . ',y:' . (is_numeric($y) ? $y + 0 : 'null') . '}'; | |
| 983 | + $has_point = true; | |
| 984 | + } | |
| 985 | + } | |
| 986 | + return $has_point ? $out : false; | |
| 987 | +} | |
| 988 | + | |
| 989 | +// X scale options for time-axis mode (globals from asset/front.js): gvnSqlChartsTimeTicks replaces the evenly | |
| 990 | +// spaced ticks Chart.js generates on a linear scale with the actual data dates, gvnSqlChartsTimeTick formats them | |
| 991 | +function guaven_sqlcharts_time_axis_scale(){ | |
| 992 | + return "type: 'linear', offset: true, afterBuildTicks: gvnSqlChartsTimeTicks, ticks: {callback: gvnSqlChartsTimeTick, maxRotation: 45, autoSkip: true},"; | |
| 993 | +} | |
| 994 | +// extra entry for the Chart.js "plugins" object in time-axis mode (tooltip title shown as a date) | |
| 995 | +function guaven_sqlcharts_time_axis_plugins($time_points){ | |
| 996 | + return $time_points !== false ? 'tooltip: {callbacks: {title: gvnSqlChartsTimeTooltipTitle}}' : ''; | |
| 997 | +} | |
| 998 | + | |
| 854 | 999 | function guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, $type = 'bar', $pid = null) |
| 855 | 1000 | { |
| 856 | 1001 | $horizontal = ($type == 'horizontalBar'); |
| 857 | 1002 | $forcestack = ($type == 'stackedBar'); |
| 858 | 1003 | $stacked = ($forcestack or get_post_meta($pid, 'guaven_sqlcharts_nostacked', true) != 1) ? 'true' : 'false'; |
| 1004 | + $time_points = $horizontal ? false : guaven_sqlcharts_time_axis_points($pid, $values); | |
| 859 | 1005 | ?> |
| 860 | 1006 | var data = { |
| 861 | - labels: [<?php guaven_sqlcharts_merge_labeldata($labels);?>], | |
| 1007 | + <?php if ($time_points === false) { ?>labels: [<?php guaven_sqlcharts_merge_labeldata($labels);?>],<?php } ?> | |
| 862 | 1008 | datasets: [ |
| 863 | 1009 | <?php |
| 864 | 1010 | $values_new=guaven_sqlcharts_key_normalizer($values,$labels,$ylabel)[0]; |
| 865 | 1011 | $i=-1; |
| @@ -864,30 +1010,29 @@ | ||
| 864 | 1010 | $values_new=guaven_sqlcharts_key_normalizer($values,$labels,$ylabel)[0]; |
| 865 | 1011 | $i=-1; |
| 866 | 1012 | foreach ($values_new as $key_ak=>$value_ak) { |
| 867 | 1013 | $i++; |
| 1014 | + $points = $time_points !== false ? $time_points[$key_ak] : $values_new[$key_ak]; | |
| 868 | 1015 | ?> |
| 869 | 1016 | { |
| 870 | 1017 | <?php |
| 871 | - if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){ | |
| 872 | - //passing chartJS params via the shortcode | |
| 873 | - echo wp_kses($GLOBALS["guaven_sqlcharts_atts"]["params"],[]); | |
| 874 | - } | |
| 1018 | + echo guaven_sqlcharts_dataset_params(); // "params" shortcode attribute (validated) | |
| 875 | 1019 | ?> |
| 876 | 1020 | label: <?php echo guaven_sqlcharts_js_label($ylabel[$key_ak]); ?>, |
| 877 | 1021 | backgroundColor: [ |
| 878 | 1022 | <?php |
| 879 | - echo wp_kses(guaven_sqlcharts_colorgenerator(count($values_new[$key_ak]), 0, 0, guaven_sqlcharts_colors($i, $pid)),[]); | |
| 1023 | + echo wp_kses(guaven_sqlcharts_colorgenerator(count($points), 0, 0, guaven_sqlcharts_colors($i, $pid)),[]); | |
| 880 | 1024 | ?> |
| 881 | 1025 | ], |
| 882 | 1026 | borderColor: [ |
| 883 | 1027 | <?php |
| 884 | - echo wp_kses(guaven_sqlcharts_colorgenerator(count($values_new[$key_ak]), 0, 0.2, guaven_sqlcharts_colors($i, $pid)),[]); | |
| 1028 | + echo wp_kses(guaven_sqlcharts_colorgenerator(count($points), 0, 0.2, guaven_sqlcharts_colors($i, $pid)),[]); | |
| 885 | 1029 | ?> |
| 886 | 1030 | ], |
| 887 | 1031 | borderWidth: 1, |
| 1032 | + <?php if ($time_points !== false) echo 'barThickness: 24,'; // fixed width: on a time axis Chart.js would otherwise size bars from the closest pair of dates ?> | |
| 888 | 1033 | data: [<?php |
| 889 | - echo wp_kses(implode(",", $values_new[$key_ak]),[]); | |
| 1034 | + echo wp_kses(implode(",", $points),[]); | |
| 890 | 1035 | ?>], |
| 891 | 1036 | }, |
| 892 | 1037 | <?php |
| 893 | 1038 | } |
| @@ -900,8 +1045,9 @@ | ||
| 900 | 1045 | <?php echo wp_kses(guaven_sqlcharts_value_labels($pid),[]); ?> |
| 901 | 1046 | <?php if ($horizontal) echo "indexAxis: 'y',"; ?> |
| 902 | 1047 | scales: { |
| 903 | 1048 | x: { |
| 1049 | + <?php if ($time_points !== false) echo guaven_sqlcharts_time_axis_scale(); ?> | |
| 904 | 1050 | <?php echo guaven_sqlcharts_axis_title($pid, $horizontal ? 'y' : 'x'); ?> |
| 905 | 1051 | stacked: <?php echo esc_js($stacked); ?>, |
| 906 | 1052 | beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?> |
| 907 | 1053 | }, |
| @@ -914,9 +1060,9 @@ | ||
| 914 | 1060 | } |
| 915 | 1061 | } |
| 916 | 1062 | } |
| 917 | 1063 | <?php |
| 918 | - guaven_sqlcharts_maybe_additional_parameters($pid); | |
| 1064 | + guaven_sqlcharts_maybe_additional_parameters($pid, guaven_sqlcharts_time_axis_plugins($time_points)); | |
| 919 | 1065 | ?> |
| 920 | 1066 | }; |
| 921 | 1067 | var myBarChart = new Chart(ctx, { |
| 922 | 1068 | type: 'bar', |
| @@ -936,11 +1082,12 @@ | ||
| 936 | 1082 | } |
| 937 | 1083 | |
| 938 | 1084 | function guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, $type = 'false', $pid = null, $charttype = 'line', $stepped = false) |
| 939 | 1085 | { |
| 1086 | + $time_points = ($charttype == 'radar') ? false : guaven_sqlcharts_time_axis_points($pid, $values); | |
| 940 | 1087 | ?> |
| 941 | 1088 | var data = { |
| 942 | - labels: [<?php guaven_sqlcharts_merge_labeldata($labels);?>], | |
| 1089 | + <?php if ($time_points === false) { ?>labels: [<?php guaven_sqlcharts_merge_labeldata($labels);?>],<?php } ?> | |
| 943 | 1090 | datasets: [ |
| 944 | 1091 | <?php |
| 945 | 1092 | $values_new=guaven_sqlcharts_key_normalizer($values,$labels,$ylabel)[0]; |
| 946 | 1093 | $dataset_count=count($values_new); |
| @@ -946,8 +1093,9 @@ | ||
| 946 | 1093 | $dataset_count=count($values_new); |
| 947 | 1094 | $i=-1; |
| 948 | 1095 | foreach ($values_new as $key_ak=>$value_ak) { |
| 949 | 1096 | $i++; |
| 1097 | + $points = $time_points !== false ? $time_points[$key_ak] : $values_new[$key_ak]; | |
| 950 | 1098 | if ($type == 'radarfill') $fill = "'origin'"; |
| 951 | 1099 | elseif ($type == 'false') $fill = 'false'; |
| 952 | 1100 | else $fill = ($i == 0 and $dataset_count > 1) ? '"+1"' : '"origin"'; |
| 953 | 1101 | ?> |
| @@ -952,12 +1100,9 @@ | ||
| 952 | 1100 | else $fill = ($i == 0 and $dataset_count > 1) ? '"+1"' : '"origin"'; |
| 953 | 1101 | ?> |
| 954 | 1102 | { |
| 955 | 1103 | <?php |
| 956 | - if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){ | |
| 957 | - //passing chartJS params via the shortcode | |
| 958 | - echo wp_kses($GLOBALS["guaven_sqlcharts_atts"]["params"],[]); | |
| 959 | - } | |
| 1104 | + echo guaven_sqlcharts_dataset_params(); // "params" shortcode attribute (validated) | |
| 960 | 1105 | ?> |
| 961 | 1106 | label: <?php echo guaven_sqlcharts_js_label($ylabel[$key_ak]); ?>, |
| 962 | 1107 | fill: <?php echo wp_kses($fill,[]); |
| 963 | 1108 | ?>, |
| @@ -978,9 +1123,9 @@ | ||
| 978 | 1123 | pointHoverBorderColor: <?php |
| 979 | 1124 | echo wp_kses_post(guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid))); |
| 980 | 1125 | ?> |
| 981 | 1126 | data: [<?php |
| 982 | - echo wp_kses_post(implode(",", $values_new[$key_ak])); | |
| 1127 | + echo wp_kses_post(implode(",", $points)); | |
| 983 | 1128 | ?>], |
| 984 | 1129 | spanGaps: false, |
| 985 | 1130 | }, |
| 986 | 1131 | <?php |
| @@ -1004,8 +1149,9 @@ | ||
| 1004 | 1149 | <?php } else { ?> |
| 1005 | 1150 | scales: { |
| 1006 | 1151 | x: { |
| 1007 | 1152 | display: true, |
| 1153 | + <?php if ($time_points !== false) echo guaven_sqlcharts_time_axis_scale(); ?> | |
| 1008 | 1154 | <?php echo guaven_sqlcharts_axis_title($pid, 'x'); ?> |
| 1009 | 1155 | beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?> |
| 1010 | 1156 | }, |
| 1011 | 1157 | y: { |
| @@ -1017,9 +1163,9 @@ | ||
| 1017 | 1163 | } |
| 1018 | 1164 | } |
| 1019 | 1165 | <?php } ?> |
| 1020 | 1166 | <?php |
| 1021 | - guaven_sqlcharts_maybe_additional_parameters($pid); | |
| 1167 | + guaven_sqlcharts_maybe_additional_parameters($pid, guaven_sqlcharts_time_axis_plugins($time_points)); | |
| 1022 | 1168 | ?> |
| 1023 | 1169 | |
| 1024 | 1170 | } |
| 1025 | 1171 | }); |
| @@ -1043,12 +1189,9 @@ | ||
| 1043 | 1189 | } |
| 1044 | 1190 | ?> |
| 1045 | 1191 | { |
| 1046 | 1192 | <?php |
| 1047 | - if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){ | |
| 1048 | - //passing chartJS params via the shortcode | |
| 1049 | - echo wp_kses($GLOBALS["guaven_sqlcharts_atts"]["params"],[]); | |
| 1050 | - } | |
| 1193 | + echo guaven_sqlcharts_dataset_params(); // "params" shortcode attribute (validated) | |
| 1051 | 1194 | ?> |
| 1052 | 1195 | label: <?php echo guaven_sqlcharts_js_label(isset($ylabel[$key_ak])?$ylabel[$key_ak]:''); ?>, |
| 1053 | 1196 | backgroundColor: <?php |
| 1054 | 1197 | echo wp_kses_post(guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid))); |
| @@ -1091,9 +1234,9 @@ | ||
| 1091 | 1234 | <?php |
| 1092 | 1235 | } |
| 1093 | 1236 | |
| 1094 | 1237 | |
| 1095 | -function guaven_sqlcharts_maybe_additional_parameters($pid){ | |
| 1238 | +function guaven_sqlcharts_maybe_additional_parameters($pid, $extra_plugins = ''){ | |
| 1096 | 1239 | if(function_exists('guaven_sqlcharts_maybe_additional_parameters_custom')){ |
| 1097 | 1240 | wp_kses(guaven_sqlcharts_maybe_additional_parameters_custom($pid),[]); |
| 1098 | 1241 | return; |
| 1099 | 1242 | } |
| @@ -1103,9 +1246,9 @@ | ||
| 1103 | 1246 | } |
| 1104 | 1247 | else { |
| 1105 | 1248 | $display='false';$position='top'; |
| 1106 | 1249 | } |
| 1107 | - echo wp_kses( ",plugins: {legend: {display: ".$display.",position:'".$position."'}}",[]); | |
| 1250 | + echo wp_kses( ",plugins: {legend: {display: ".$display.",position:'".$position."'}".($extra_plugins !== '' ? ','.$extra_plugins : '')."}",[]); | |
| 1108 | 1251 | } |
| 1109 | 1252 | |
| 1110 | 1253 | |
| 1111 | 1254 | |
| @@ -1128,12 +1271,9 @@ | ||
| 1128 | 1271 | for ($i = 0; $i < count($values); $i++) { |
| 1129 | 1272 | ?> |
| 1130 | 1273 | { |
| 1131 | 1274 | <?php |
| 1132 | - if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){ | |
| 1133 | - //passing chartJS params via the shortcode | |
| 1134 | - echo wp_kses($GLOBALS["guaven_sqlcharts_atts"]["params"],[]); | |
| 1135 | - } | |
| 1275 | + echo guaven_sqlcharts_dataset_params(); // "params" shortcode attribute (validated) | |
| 1136 | 1276 | ?> |
| 1137 | 1277 | data: [<?php |
| 1138 | 1278 | echo wp_kses(implode(",", $values[$i]),[]); |
| 1139 | 1279 | ?>], |