| @@ -18,12 +18,49 @@ | ||
| 18 | 18 | gvn_chart_sample_nonxml_data(); |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 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} | |
| 39 | + </a> | |
| 40 | + <img src="'.plugin_dir_url( __FILE__ ) . 'asset/img/recommended1.jpg" style="max-width: 430px;"></td></tr> | |
| 41 | + </tbody></table>'; | |
| 42 | +} | |
| 22 | 43 | |
| 44 | + | |
| 45 | + | |
| 23 | 46 | function guaven_sqlcharts_my_admin_notice() |
| 24 | 47 | { |
| 25 | 48 | global $post; |
| 49 | + | |
| 50 | + | |
| 51 | + if( | |
| 52 | + (!empty($_SERVER["REQUEST_URI"]) and strpos(sanitize_text_field(wp_unslash($_SERVER["REQUEST_URI"])),'post_type=gvn_schart')!==false) | |
| 53 | + or | |
| 54 | + (!empty($post) and $post->post_type == 'gvn_schart') | |
| 55 | + ){ | |
| 56 | + echo str_replace('{svg}','<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"> | |
| 57 | + <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> | |
| 58 | + </svg>',wp_kses_post(guaven_sqlcharts_recommended(),[])); | |
| 59 | + } | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 26 | 63 | if (!empty($post) and $post->post_type == 'gvn_schart'): |
| 27 | 64 | if (!current_user_can('manage_options')) { |
| 28 | 65 | echo '<br><br> |
| 29 | 66 | <div class="updated gf-alert gf-alert-danger">Only administrators can manage this page</div>'; |
| @@ -29,23 +66,34 @@ | ||
| 29 | 66 | <div class="updated gf-alert gf-alert-danger">Only administrators can manage this page</div>'; |
| 30 | 67 | die(); |
| 31 | 68 | } |
| 32 | 69 | echo '<div class="updated gf-alert gf-alert-info">'; |
| 33 | - if (empty($_GET["post"]) and strpos($_SERVER["REQUEST_URI"], "post-new") === false): | |
| 34 | - $gf_message = 'Use <b>Add new</b> button above to create new sql report. And click on any existing rule names below | |
| 35 | - to manage them. '; | |
| 36 | - else: | |
| 37 | - $gf_message = ' | |
| 38 | - 1. Give any name to your report.<br> | |
| 39 | - 2. Choose chart type, type sql query, enter field names, labels and then press to Publish/Update<br> | |
| 40 | - 3. After update you will see needed shortcode below. You can use that shortcode anywhere in your website: in pages, posts, widgets etc. <br> | |
| 41 | - '; | |
| 42 | - endif; | |
| 43 | - _e('<div style="float:left">' . $gf_message . '</div>', 'guaven_sqlcharts'); | |
| 70 | + if (empty($_GET["post"]) && strpos(sanitize_text_field(wp_unslash($_SERVER["REQUEST_URI"])), "post-new") === false) { | |
| 71 | + $gf_message = __( | |
| 72 | + 'Use <b>Add new</b> button above to create a new SQL report. And click on any existing rule names below to manage them.', | |
| 73 | + 'guaven_sqlcharts' | |
| 74 | + ); | |
| 75 | + } else { | |
| 76 | + $gf_message = __( | |
| 77 | + '1. Give any name to your report.<br> | |
| 78 | + 2. Pick a chart type, build your SQL query with the visual builder (or type it — autocomplete will help), map the X/Y columns and press Publish/Update.<br> | |
| 79 | + 3. After update, you will see the shortcode of this chart at the bottom of the page. You can use that shortcode anywhere in your website: in pages, posts, widgets, etc.', | |
| 80 | + 'guaven_sqlcharts' | |
| 81 | + ); | |
| 82 | + } | |
| 83 | + | |
| 84 | + echo '<div style="float:left;max-width:calc(100% - 345px)">'; | |
| 85 | + echo wp_kses_post( $gf_message ); | |
| 86 | + echo '</div>'; | |
| 87 | + | |
| 44 | 88 | echo '<div style="float: right; |
| 45 | 89 | margin-top: 0px; |
| 46 | - padding-top: 0px;"><a target="_blank" style="border:0px solid #6200ee;border-radius:0px;color:white;font-weight:bold;background: #6200ee;" | |
| 47 | - class="button button-secondary" href="https://guaven.com/contact/solution-request/">Get Premium Support </a></div> </div>'; | |
| 90 | + padding-top: 0px;"><a target="_blank" style="text-align:center;border:0px solid #6200ee;border-radius:0px;color:white;font-weight:bold;background: #6200ee;" | |
| 91 | + class="button button-secondary" href="https://guaven.com/contact/solution-request/">Get Premium Support </a> | |
| 92 | + <span style="line-height: 30px;padding: 0 5px;">OR</span> | |
| 93 | + <a target="_blank" style="text-align:center;border:0px solid #26b286;border-radius:0px;color:white;font-weight:bold;background: #26b286;" | |
| 94 | + class="button button-secondary" href="https://guaven.com/service/small-thankyou-premium-support-service/">Make a Small Donation</a> | |
| 95 | + </div> </div>'; | |
| 48 | 96 | endif; |
| 49 | 97 | } |
| 50 | 98 | add_action('admin_notices', 'guaven_sqlcharts_my_admin_notice'); |
| 51 | 99 | |
| @@ -62,11 +110,14 @@ | ||
| 62 | 110 | add_action('admin_notices', 'guaven_sqlcharts_onboarding_notice'); |
| 63 | 111 | |
| 64 | 112 | function guaven_sqlcharts_onboarding_notice_dismissed(){ |
| 65 | 113 | check_ajax_referer('notice_dismissed', 'nonce'); |
| 114 | + if (!current_user_can('manage_options')) return; | |
| 115 | + | |
| 116 | + if(empty($_POST['type']))return; | |
| 66 | 117 | switch ($_POST['type']){ |
| 67 | 118 | case 'onboarding_notice': |
| 68 | - update_option('guaven_sqlcharts_onboarding_notice_dismissed', 1); | |
| 119 | + update_option('guaven_sqlcharts_onboarding_notice_dismissed', 1); | |
| 69 | 120 | break; |
| 70 | 121 | } |
| 71 | 122 | } |
| 72 | 123 | add_action('wp_ajax_guaven_sqlcharts_onboarding_notice_dismissed', 'guaven_sqlcharts_onboarding_notice_dismissed'); |
| @@ -72,10 +123,12 @@ | ||
| 72 | 123 | add_action('wp_ajax_guaven_sqlcharts_onboarding_notice_dismissed', 'guaven_sqlcharts_onboarding_notice_dismissed'); |
| 73 | 124 | |
| 74 | 125 | function guaven_sqlcharts_enqueue_chart() |
| 75 | 126 | { |
| 76 | - wp_enqueue_script('guaven_sqlcharts_chartjs', plugins_url('asset/bundle.min.js', __FILE__),array('jquery'),GVNSQLCHARTS_VERSION); | |
| 77 | - wp_localize_script('guaven_sqlcharts_chartjs', 'guaven_sqlcharts_notice_dismissed', array( | |
| 127 | + wp_enqueue_script('guaven_sqlcharts_chartjs', plugins_url('asset/chart.umd.min.js', __FILE__),array('jquery'),GVNSQLCHARTS_VERSION,false); | |
| 128 | + wp_enqueue_script('guaven_sqlcharts_datepicker', plugins_url('asset/datepicker.min.js', __FILE__),array('jquery'),GVNSQLCHARTS_VERSION,false); | |
| 129 | + wp_enqueue_script('guaven_sqlcharts_front', plugins_url('asset/front.js', __FILE__),array('jquery','guaven_sqlcharts_chartjs','guaven_sqlcharts_datepicker'),GVNSQLCHARTS_VERSION,false); | |
| 130 | + wp_localize_script('guaven_sqlcharts_front', 'guaven_sqlcharts_notice_dismissed', array( | |
| 78 | 131 | 'action' => 'guaven_sqlcharts_onboarding_notice_dismissed', |
| 79 | 132 | 'nonce' => wp_create_nonce('notice_dismissed') |
| 80 | 133 | )); |
| 81 | 134 | |
| @@ -86,11 +139,55 @@ | ||
| 86 | 139 | function guaven_sqlcharts_enqueue_main_style() |
| 87 | 140 | { |
| 88 | 141 | wp_enqueue_style('guaven_sqlcharts_main_style', plugins_url('asset/guaven_sqlcharts.css', __FILE__),array(),GVNSQLCHARTS_VERSION); |
| 89 | 142 | } |
| 143 | +add_action('wp_enqueue_scripts', 'guaven_sqlcharts_enqueue_main_style'); | |
| 90 | 144 | add_action('admin_enqueue_scripts', 'guaven_sqlcharts_enqueue_main_style'); |
| 91 | 145 | |
| 146 | +// admin-only assets: SQL builder, autocomplete, new metabox UI | |
| 147 | +function guaven_sqlcharts_admin_assets($hook) | |
| 148 | +{ | |
| 149 | + if (!in_array($hook, array('post.php', 'post-new.php'))) return; | |
| 150 | + $screen = function_exists('get_current_screen') ? get_current_screen() : null; | |
| 151 | + if (empty($screen->post_type) or $screen->post_type != 'gvn_schart') return; | |
| 152 | + if (!current_user_can('manage_options')) return; | |
| 92 | 153 | |
| 154 | + wp_enqueue_style('guaven_sqlcharts_admin_style', plugins_url('asset/admin.css', __FILE__), array(), GVNSQLCHARTS_VERSION); | |
| 155 | + wp_enqueue_script('guaven_sqlcharts_admin', plugins_url('asset/admin.js', __FILE__), array('jquery'), GVNSQLCHARTS_VERSION, true); | |
| 156 | + | |
| 157 | + global $wpdb; | |
| 158 | + $tables = $wpdb->get_col('SHOW TABLES'); | |
| 159 | + if (!is_array($tables)) $tables = array(); | |
| 160 | + wp_localize_script('guaven_sqlcharts_admin', 'gvnSqlBuilder', array( | |
| 161 | + 'ajaxurl' => admin_url('admin-ajax.php'), | |
| 162 | + 'nonce' => wp_create_nonce('gvnsql_schema'), | |
| 163 | + 'tables' => array_values($tables), | |
| 164 | + 'prefix' => $wpdb->prefix, | |
| 165 | + )); | |
| 166 | +} | |
| 167 | +add_action('admin_enqueue_scripts', 'guaven_sqlcharts_admin_assets'); | |
| 168 | + | |
| 169 | +// returns column names of one table for the live SQL builder/autocomplete | |
| 170 | +function guaven_sqlcharts_ajax_columns() | |
| 171 | +{ | |
| 172 | + check_ajax_referer('gvnsql_schema', 'nonce'); | |
| 173 | + if (!current_user_can('manage_options')) wp_send_json_error('forbidden', 403); | |
| 174 | + global $wpdb; | |
| 175 | + $table = isset($_POST['table']) ? sanitize_text_field(wp_unslash($_POST['table'])) : ''; | |
| 176 | + $tables = $wpdb->get_col('SHOW TABLES'); | |
| 177 | + if (!is_array($tables) or !in_array($table, $tables, true)) wp_send_json_error('unknown table', 400); | |
| 178 | + $cols = $wpdb->get_results('SHOW COLUMNS FROM `' . str_replace('`', '', $table) . '`'); | |
| 179 | + $out = array(); | |
| 180 | + if (is_array($cols)) { | |
| 181 | + foreach ($cols as $col) { | |
| 182 | + $out[] = array('name' => $col->Field, 'type' => $col->Type); | |
| 183 | + } | |
| 184 | + } | |
| 185 | + wp_send_json_success($out); | |
| 186 | +} | |
| 187 | +add_action('wp_ajax_gvnsql_get_columns', 'guaven_sqlcharts_ajax_columns'); | |
| 188 | + | |
| 189 | + | |
| 93 | 190 | function guaven_sqlcharts_isJson($string) |
| 94 | 191 | { |
| 95 | 192 | json_decode($string); |
| 96 | 193 | return (json_last_error() == JSON_ERROR_NONE); |
| @@ -98,17 +195,61 @@ | ||
| 98 | 195 | |
| 99 | 196 | add_action('init', 'guaven_sqlcharts_register_post'); |
| 100 | 197 | function guaven_sqlcharts_register_post() |
| 101 | 198 | { |
| 102 | - //register_taxonomy('guaven_update_push_tag', 'termin'); | |
| 103 | 199 | register_post_type('gvn_schart', array( |
| 104 | 200 | 'labels' => array( |
| 105 | - 'name' => __('My SQL Charts'), | |
| 106 | - 'singular_name' => __('My SQL chart') | |
| 201 | + 'name' => __('My SQL Charts','guaven_sqlcharts'), | |
| 202 | + 'singular_name' => __('SQL Chart','guaven_sqlcharts'), | |
| 203 | + 'menu_name' => __('My SQL Charts','guaven_sqlcharts'), | |
| 204 | + 'add_new' => __('Add Chart','guaven_sqlcharts'), | |
| 205 | + 'add_new_item' => __('Add New Chart','guaven_sqlcharts'), | |
| 206 | + 'edit_item' => __('Edit Chart','guaven_sqlcharts'), | |
| 207 | + 'new_item' => __('New Chart','guaven_sqlcharts'), | |
| 208 | + 'view_item' => __('View Chart','guaven_sqlcharts'), | |
| 209 | + 'view_items' => __('View Charts','guaven_sqlcharts'), | |
| 210 | + 'search_items' => __('Search Charts','guaven_sqlcharts'), | |
| 211 | + 'not_found' => __('No charts found','guaven_sqlcharts'), | |
| 212 | + 'not_found_in_trash' => __('No charts found in Trash','guaven_sqlcharts'), | |
| 213 | + 'all_items' => __('All Charts','guaven_sqlcharts'), | |
| 214 | + 'archives' => __('Chart Archives','guaven_sqlcharts'), | |
| 215 | + 'attributes' => __('Chart Attributes','guaven_sqlcharts'), | |
| 216 | + 'insert_into_item' => __('Insert into chart','guaven_sqlcharts'), | |
| 217 | + 'uploaded_to_this_item' => __('Uploaded to this chart','guaven_sqlcharts'), | |
| 218 | + 'filter_items_list' => __('Filter charts list','guaven_sqlcharts'), | |
| 219 | + 'items_list_navigation' => __('Charts list navigation','guaven_sqlcharts'), | |
| 220 | + 'items_list' => __('Charts list','guaven_sqlcharts'), | |
| 221 | + 'item_published' => __('Chart published.','guaven_sqlcharts'), | |
| 222 | + 'item_published_privately' => __('Chart published privately.','guaven_sqlcharts'), | |
| 223 | + 'item_reverted_to_draft' => __('Chart reverted to draft.','guaven_sqlcharts'), | |
| 224 | + 'item_scheduled' => __('Chart scheduled.','guaven_sqlcharts'), | |
| 225 | + 'item_updated' => __('Chart updated.','guaven_sqlcharts'), | |
| 107 | 226 | ), |
| 108 | 227 | |
| 109 | 228 | 'public' => true, |
| 110 | - //'taxonomies' => array('guaven_update_push_tag'), | |
| 229 | + 'show_in_rest' => false, | |
| 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 | + ), | |
| 111 | 252 | 'supports' => array( |
| 112 | 253 | 'title', |
| 113 | 254 | 'postmeta' |
| 114 | 255 | ), |
| @@ -117,8 +258,20 @@ | ||
| 117 | 258 | |
| 118 | 259 | guaven_sqlcharts_load_defaults(); |
| 119 | 260 | } |
| 120 | 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 | + | |
| 268 | +// "Add title" placeholder on the chart edit screen | |
| 269 | +add_filter('enter_title_here', function ($title, $post) { | |
| 270 | + if (!empty($post) and $post->post_type == 'gvn_schart') return __('Chart name', 'guaven_sqlcharts'); | |
| 271 | + return $title; | |
| 272 | +}, 10, 2); | |
| 273 | + | |
| 121 | 274 | add_action('admin_footer', 'guaven_sqlcharts_admin_front'); |
| 122 | 275 | |
| 123 | 276 | |
| 124 | 277 | function guaven_sqlcharts_admin_front() |
| @@ -133,9 +286,9 @@ | ||
| 133 | 286 | |
| 134 | 287 | // metabox for editor |
| 135 | 288 | function guaven_sqlcharts_metabox_area() |
| 136 | 289 | { |
| 137 | - add_meta_box('guaven_sqlcharts_metabox', 'Configure your graph chart', 'guaven_sqlcharts_metabox', 'gvn_schart', 'advanced', 'default'); | |
| 290 | + add_meta_box('guaven_sqlcharts_metabox', 'Chart Builder', 'guaven_sqlcharts_metabox', 'gvn_schart', 'advanced', 'default'); | |
| 138 | 291 | } |
| 139 | 292 | |
| 140 | 293 | function guaven_sqlcharts_metabox() |
| 141 | 294 | { |
| @@ -141,25 +294,127 @@ | ||
| 141 | 294 | { |
| 142 | 295 | require_once(dirname(__FILE__) . "/admin_metabox.php"); |
| 143 | 296 | } |
| 144 | 297 | |
| 298 | +/** | |
| 299 | + * Catalog of all supported chart types: label, group, per-type usage guide and example query. | |
| 300 | + * Used by the admin UI to render the type cards and the contextual guides. | |
| 301 | + */ | |
| 302 | +function guaven_sqlcharts_type_catalog() | |
| 303 | +{ | |
| 304 | + global $wpdb; | |
| 305 | + $p = $wpdb->posts; | |
| 306 | + $u = $wpdb->users; | |
| 307 | + return array( | |
| 308 | + 'pie_l' => array( | |
| 309 | + 'label' => 'Pie', | |
| 310 | + 'group' => 'Circular', | |
| 311 | + 'guide' => 'Best for showing how a total splits into a few parts (shares/percentages). Use one query that returns a label column (X) and a numeric value column (Y). Keep it under ~8 slices for readability.', | |
| 312 | + 'example_sql' => "select count(*) postcount, SUBSTR(post_date,1,4) yearnum from $p group by yearnum order by yearnum asc limit 10", | |
| 313 | + 'example_x' => 'yearnum', 'example_y' => 'postcount', | |
| 314 | + ), | |
| 315 | + 'donut_l' => array( | |
| 316 | + 'label' => 'Doughnut', | |
| 317 | + 'group' => 'Circular', | |
| 318 | + 'guide' => 'Same as Pie but with a hole in the middle — slightly easier to compare slice sizes. One query: label column (X) + numeric column (Y).', | |
| 319 | + 'example_sql' => "select count(*) postcount, post_type from $p group by post_type order by postcount desc limit 8", | |
| 320 | + 'example_x' => 'post_type', 'example_y' => 'postcount', | |
| 321 | + ), | |
| 322 | + 'polar_l' => array( | |
| 323 | + 'label' => 'Polar Area', | |
| 324 | + 'group' => 'Circular', | |
| 325 | + 'guide' => 'Like a pie, but every slice has the same angle and the value controls the radius. Good for cyclic data (months, weekdays). One query: label (X) + numeric value (Y).', | |
| 326 | + 'example_sql' => "select count(*) postcount, SUBSTRING(post_date,6,2) monthnum from $p group by monthnum order by monthnum", | |
| 327 | + 'example_x' => 'monthnum', 'example_y' => 'postcount', | |
| 328 | + ), | |
| 329 | + 'radar_l' => array( | |
| 330 | + 'label' => 'Radar', | |
| 331 | + 'group' => 'Circular', | |
| 332 | + 'guide' => 'Compares one or more series across several categories arranged in a circle. Great for profiles/ratings. Use one query per series, separated with ";".', | |
| 333 | + 'example_sql' => "select count(*) postcount, SUBSTRING(post_date,6,2) monthnum from $p where post_type=\"post\" group by monthnum order by monthnum", | |
| 334 | + 'example_x' => 'monthnum', 'example_y' => 'postcount', | |
| 335 | + ), | |
| 336 | + 'line_l' => array( | |
| 337 | + 'label' => 'Line', | |
| 338 | + 'group' => 'Line', | |
| 339 | + 'guide' => 'The classic choice for trends over time (per day/month/year). X should be an ordered value like a date. Add more queries separated with ";" for comparison lines.', | |
| 340 | + 'example_sql' => "select count(*) postcount, SUBSTRING(post_date,1,7) monthandyear from $p group by monthandyear order by monthandyear", | |
| 341 | + 'example_x' => 'monthandyear', 'example_y' => 'postcount', | |
| 342 | + ), | |
| 343 | + 'area_l' => array( | |
| 344 | + 'label' => 'Area', | |
| 345 | + 'group' => 'Line', | |
| 346 | + 'guide' => 'A line chart with the region under the line filled — emphasizes volume/magnitude of a trend. Works well with 2 queries (";" separated) to compare filled regions.', | |
| 347 | + 'example_sql' => "select count(*) postcount, SUBSTRING(post_date,1,7) monthandyear from $p where post_type=\"post\" group by monthandyear order by monthandyear;\nselect count(*) postcount, SUBSTRING(post_date,1,7) monthandyear from $p where post_type!=\"post\" group by monthandyear order by monthandyear", | |
| 348 | + 'example_x' => 'monthandyear', 'example_y' => 'Posts;Other content', | |
| 349 | + ), | |
| 350 | + 'steppedline_l' => array( | |
| 351 | + 'label' => 'Stepped Line', | |
| 352 | + 'group' => 'Line', | |
| 353 | + 'guide' => 'Line chart that moves in steps instead of slopes — perfect for values that change at discrete moments (prices, stock level, settings history).', | |
| 354 | + 'example_sql' => "select count(*) postcount, SUBSTRING(post_date,1,4) yearnum from $p group by yearnum order by yearnum", | |
| 355 | + 'example_x' => 'yearnum', 'example_y' => 'postcount', | |
| 356 | + ), | |
| 357 | + 'bar_l' => array( | |
| 358 | + 'label' => 'Bar', | |
| 359 | + 'group' => 'Bar', | |
| 360 | + 'guide' => 'Compares values across categories with vertical bars. One query: category (X) + numeric value (Y). Multiple ";" separated queries become grouped/stacked bars.', | |
| 361 | + 'example_sql' => "select count(*) postcount, SUBSTRING(post_date,1,7) monthandyear from $p group by monthandyear order by monthandyear", | |
| 362 | + 'example_x' => 'monthandyear', 'example_y' => 'postcount', | |
| 363 | + ), | |
| 364 | + 'horizontalbar_l' => array( | |
| 365 | + 'label' => 'Horizontal Bar', | |
| 366 | + 'group' => 'Bar', | |
| 367 | + 'guide' => 'Bar chart rotated 90° — the best pick when category names are long (user names, product titles).', | |
| 368 | + 'example_sql' => "select count(*) as postcount, b.display_name as dname from $p a inner join $u b ON a.post_author=b.ID where a.post_status=\"publish\" group by a.post_author order by postcount desc limit 10", | |
| 369 | + 'example_x' => 'dname', 'example_y' => 'postcount', | |
| 370 | + ), | |
| 371 | + 'stackedbar_l' => array( | |
| 372 | + 'label' => 'Stacked Bar', | |
| 373 | + 'group' => 'Bar', | |
| 374 | + 'guide' => 'Shows how each category total is composed of parts. Use 2+ queries separated with ";" — each query becomes one segment color of the stack.', | |
| 375 | + 'example_sql' => "select count(*) postcount, SUBSTRING(post_date,1,4) yearnum from $p where post_type=\"post\" group by yearnum order by yearnum;\nselect count(*) postcount, SUBSTRING(post_date,1,4) yearnum from $p where post_type!=\"post\" group by yearnum order by yearnum", | |
| 376 | + 'example_x' => 'yearnum', 'example_y' => 'Posts;Other content', | |
| 377 | + ), | |
| 378 | + 'scatter_l' => array( | |
| 379 | + 'label' => 'Scatter', | |
| 380 | + 'group' => 'Other', | |
| 381 | + 'guide' => 'Plots points on numeric X/Y axes to reveal correlation between two numbers. Both X and Y columns must be numeric (e.g. comment_count vs menu_order).', | |
| 382 | + 'example_sql' => "select comment_count ccount, ID from $p where post_status=\"publish\" order by ID limit 100", | |
| 383 | + 'example_x' => 'ID', 'example_y' => 'ccount', | |
| 384 | + ), | |
| 385 | + ); | |
| 386 | +} | |
| 387 | + | |
| 388 | +/** | |
| 389 | + * Maps deprecated Google-Chart era type slugs to their Chart.js equivalents. | |
| 390 | + * Old charts keep working without any manual migration; when the post is re-saved | |
| 391 | + * from the new UI the normalized value is stored automatically. | |
| 392 | + */ | |
| 393 | +function guaven_sqlcharts_normalize_type($type) | |
| 394 | +{ | |
| 395 | + $map = array( | |
| 396 | + 'pie' => 'pie_l', | |
| 397 | + '3dpie' => 'pie_l', | |
| 398 | + 'column' => 'bar_l', | |
| 399 | + 'bar' => 'horizontalbar_l', | |
| 400 | + 'area' => 'area_l', | |
| 401 | + ); | |
| 402 | + return isset($map[$type]) ? $map[$type] : $type; | |
| 403 | +} | |
| 404 | + | |
| 145 | 405 | function guaven_gutenberg_wrapper($atts){ |
| 146 | 406 | if(isset($atts['sqlcharts_inserted_script'])){ |
| 147 | 407 | global $sqlcharts_inserted_script; |
| 148 | 408 | $sqlcharts_inserted_script = $atts['sqlcharts_inserted_script']; |
| 149 | 409 | } |
| 150 | - | |
| 151 | - // if( isset($_GET["post_id"],$_GET["context"]) and $_GET["context"]=='edit' | |
| 152 | - // and strpos($_SERVER["REQUEST_URI"],'block-renderer/guaven-sqlcharts/gvn-chart-gutenberg')!==false | |
| 153 | - // ){ | |
| 154 | - // $atts['chart_id']=$_GET["post_id"]; | |
| 155 | - // } | |
| 410 | + | |
| 156 | 411 | $post = get_post($atts['chart_id']); |
| 157 | 412 | if( ! isset($atts['chart_id']) or !isset($post) or $post->post_type != 'gvn_schart'){ |
| 158 | - | |
| 413 | + | |
| 159 | 414 | return "Invalid id"; |
| 160 | 415 | } |
| 161 | - | |
| 416 | + | |
| 162 | 417 | return guaven_sqlcharts_local_shortcode(array('id' => $atts['chart_id'])); // temporary explicit value |
| 163 | 418 | } |
| 164 | 419 | function guaven_register_gutenberg_blocks() |
| 165 | 420 | { |
| @@ -166,14 +421,15 @@ | ||
| 166 | 421 | wp_register_script( |
| 167 | 422 | 'gvn_gutenberg_charts', |
| 168 | 423 | plugins_url( 'asset/guaven_gutenberg_charts.js', __FILE__ ), |
| 169 | 424 | array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-server-side-render' ), |
| 170 | - GVNSQLCHARTS_VERSION.'_'.filemtime( plugin_dir_path( __FILE__ ) . 'asset/guaven_gutenberg_charts.js' ) | |
| 425 | + GVNSQLCHARTS_VERSION.'_'.filemtime( plugin_dir_path( __FILE__ ) . 'asset/guaven_gutenberg_charts.js'), | |
| 426 | + false | |
| 171 | 427 | ); |
| 172 | 428 | wp_localize_script('gvn_gutenberg_charts', 'guaven', array( |
| 173 | 429 | 'description' => 'Add My SQL Chart to your post', |
| 174 | 430 | )); |
| 175 | - | |
| 431 | + | |
| 176 | 432 | register_block_type( 'guaven-sqlcharts/gvn-chart-gutenberg', array( |
| 177 | 433 | 'editor_script' => 'gvn_gutenberg_charts', |
| 178 | 434 | 'render_callback' => 'guaven_gutenberg_wrapper', |
| 179 | 435 | 'attributes' => array( |
| @@ -196,8 +452,11 @@ | ||
| 196 | 452 | { |
| 197 | 453 | if (!isset($_POST['meta_box_nonce_field']) or !wp_verify_nonce($_POST['meta_box_nonce_field'], 'meta_box_nonce_action')) { |
| 198 | 454 | return $post->ID; |
| 199 | 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 | + } | |
| 200 | 459 | $fields = array( |
| 201 | 460 | "guaven_sqlcharts_chartheight", |
| 202 | 461 | "guaven_sqlcharts_chartwidth", |
| 203 | 462 | "guaven_sqlcharts_graphtype", |
| @@ -215,9 +474,12 @@ | ||
| 215 | 474 | "guaven_sqlcharts_colors", |
| 216 | 475 | "guaven_sqlcharts_begin_with_0_x", |
| 217 | 476 | "guaven_sqlcharts_begin_with_0_y", |
| 218 | 477 | "guaven_sqlcharts_round_y_values", |
| 219 | - "guaven_sqlcharts_legend_position" | |
| 478 | + "guaven_sqlcharts_legend_position", | |
| 479 | + "guaven_sqlcharts_nostacked", | |
| 480 | + "guaven_sqlcharts_forcetooltips", | |
| 481 | + "guaven_sqlcharts_timeaxis" | |
| 220 | 482 | ); |
| 221 | 483 | foreach ($fields as $key => $value) { |
| 222 | 484 | if(isset($_POST[$value]))$newval=esc_attr($_POST[$value]); |
| 223 | 485 | else $newval=''; |
| @@ -227,59 +489,80 @@ | ||
| 227 | 489 | if(!empty($_POST["guaven_sqlcharts_dbpass"])){ |
| 228 | 490 | $encpass=guaven_sqlcharts_encrypt_decrypt('encrypt',$_POST["guaven_sqlcharts_dbpass"]); |
| 229 | 491 | update_post_meta($post->ID, 'guaven_sqlcharts_dbpass', ['encrypted',$encpass]); |
| 230 | 492 | } |
| 231 | - update_post_meta($post->ID, 'guaven_sqlcharts_code', esc_attr(str_replace("'",'"',stripslashes($_POST['guaven_sqlcharts_code']))) ); | |
| 493 | + // Store the SQL as typed. Do not HTML-encode it and do not rewrite quotes: | |
| 494 | + // the editor escapes it on output and the front end decodes entities before running it. | |
| 495 | + $sql_code = isset($_POST['guaven_sqlcharts_code']) ? wp_check_invalid_utf8(wp_unslash($_POST['guaven_sqlcharts_code'])) : ''; | |
| 496 | + update_post_meta($post->ID, 'guaven_sqlcharts_code', $sql_code); | |
| 497 | + // Flag that this chart stores raw SQL. Charts without the flag were saved by | |
| 498 | + // versions before 3.0.1, which HTML-encoded the query, and still need decoding. | |
| 499 | + update_post_meta($post->ID, 'guaven_sqlcharts_code_raw', 1); | |
| 232 | 500 | } |
| 501 | + | |
| 502 | +// Returns the stored SQL query exactly as the user typed it. | |
| 503 | +function guaven_sqlcharts_get_code($post_id) | |
| 504 | +{ | |
| 505 | + $sql = get_post_meta($post_id, 'guaven_sqlcharts_code', true); | |
| 506 | + if (get_post_meta($post_id, 'guaven_sqlcharts_code_raw', true) != 1) { | |
| 507 | + $sql = html_entity_decode($sql, ENT_QUOTES, 'UTF-8'); | |
| 508 | + } | |
| 509 | + return $sql; | |
| 510 | +} | |
| 233 | 511 | add_action('save_post', 'guaven_sqlcharts_save_metabox_area', 1, 2); |
| 234 | 512 | // save the custom fields |
| 235 | 513 | |
| 236 | 514 | |
| 237 | 515 | |
| 238 | - | |
| 239 | -function guaven_sqlcharts_libloads($type, $step) | |
| 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) | |
| 240 | 519 | { |
| 241 | - $stty = array( | |
| 242 | - 'bar' => array( | |
| 243 | - 'packages' => "'corechart', 'bar'", | |
| 244 | - 'charts' => "BarChart" | |
| 245 | - ), | |
| 246 | - 'column' => array( | |
| 247 | - 'packages' => "'corechart', 'bar'", | |
| 248 | - 'charts' => "ColumnChart" | |
| 249 | - ), | |
| 250 | - 'area' => array( | |
| 251 | - 'packages' => "'corechart'", | |
| 252 | - 'charts' => "AreaChart" | |
| 253 | - ), | |
| 254 | - 'pie' => array( | |
| 255 | - 'packages' => "'corechart'", | |
| 256 | - 'charts' => "PieChart" | |
| 257 | - ), | |
| 258 | - '3dpie' => array( | |
| 259 | - 'packages' => "'corechart'", | |
| 260 | - 'charts' => "PieChart" | |
| 261 | - ) | |
| 262 | - ); | |
| 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 | +} | |
| 263 | 542 | |
| 264 | - return $stty[$type][$step]; | |
| 265 | -} | |
| 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. | |
| 266 | 545 | function gvn_chart_check_sql_query($sql) |
| 267 | 546 | { |
| 268 | - $blacklister = array( | |
| 269 | - "delete", | |
| 270 | - "update", | |
| 271 | - "insert", | |
| 272 | - "drop", | |
| 273 | - "truncate", | |
| 274 | - "alter" | |
| 275 | - ); //add all | |
| 276 | - $blacklister_f = 0; | |
| 277 | - foreach ($blacklister as $key => $value) { | |
| 278 | - if (strpos($sql, $value) !== false) | |
| 279 | - $blacklister_f = 1; | |
| 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; | |
| 280 | 563 | } |
| 281 | - return $blacklister_f; | |
| 564 | + return 0; | |
| 282 | 565 | } |
| 283 | 566 | |
| 284 | 567 | function guaven_get_labels_and_values($id, $fvs) |
| 285 | 568 | { |
| @@ -285,22 +568,16 @@ | ||
| 285 | 568 | { |
| 286 | 569 | $values = array(); |
| 287 | 570 | $labels = array(); |
| 288 | 571 | $xarg_s = get_post_meta($id, 'guaven_sqlcharts_xarg_s', true); |
| 289 | - $xarg_l = get_post_meta($id, 'guaven_sqlcharts_xarg_l', true); | |
| 290 | 572 | $yarg_s = get_post_meta($id, 'guaven_sqlcharts_yarg_s', true); |
| 291 | - $yarg_l = get_post_meta($id, 'guaven_sqlcharts_yarg_l', true); | |
| 292 | - $chartype = array( | |
| 293 | - 'line_l' => 'Line', | |
| 294 | - 'pie_l' => 'Pie', | |
| 295 | - 'donut_l' => 'Pie', | |
| 296 | - 'bar_l' => 'Bar', | |
| 297 | - 'horizontalbar_l' => 'Horizontal Bar', | |
| 298 | - 'area_l' => 'Line' | |
| 299 | - ); | |
| 573 | + // labels are saved through esc_attr, so "&" is stored as "&"; decode before splitting on ";" | |
| 574 | + // or the entity's own ";" would be taken as a series separator | |
| 575 | + $xarg_l = html_entity_decode((string) get_post_meta($id, 'guaven_sqlcharts_xarg_l', true), ENT_QUOTES, 'UTF-8'); | |
| 576 | + $yarg_l = html_entity_decode((string) get_post_meta($id, 'guaven_sqlcharts_yarg_l', true), ENT_QUOTES, 'UTF-8'); | |
| 300 | 577 | foreach ($fvs as $key => $value) { |
| 301 | - $values[] = $value->$yarg_s; | |
| 302 | - $labels[] = '"' . $value->$xarg_s . '"'; | |
| 578 | + $values[$value->$xarg_s] = $value->$yarg_s; | |
| 579 | + $labels[$value->$xarg_s] = '"' . $value->$xarg_s . '"'; | |
| 303 | 580 | } |
| 304 | 581 | return array( |
| 305 | 582 | $labels, |
| 306 | 583 | $values, |
| @@ -308,30 +585,51 @@ | ||
| 308 | 585 | explode(";", $xarg_l) |
| 309 | 586 | ); |
| 310 | 587 | } |
| 311 | 588 | |
| 312 | -function guaven_sqlcharts_print_chart_js($tip_g, $title, $labels, $values, $ylabel, $pid = null) | |
| 589 | +function guaven_sqlcharts_print_chart_js($print_data) | |
| 313 | 590 | { |
| 314 | - if ($tip_g == 'line_l') { | |
| 315 | - guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'false', $pid); | |
| 591 | + extract($print_data); | |
| 592 | + $tip_g = guaven_sqlcharts_normalize_type($tip_g); | |
| 593 | + | |
| 594 | + switch ($tip_g) { | |
| 595 | + case 'line_l': | |
| 596 | + guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'false', $pid); | |
| 597 | + break; | |
| 598 | + case 'area_l': | |
| 599 | + guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'true', $pid); | |
| 600 | + break; | |
| 601 | + case 'steppedline_l': | |
| 602 | + guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'false', $pid, 'line', true); | |
| 603 | + break; | |
| 604 | + case 'radar_l': | |
| 605 | + guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'radarfill', $pid, 'radar'); | |
| 606 | + break; | |
| 607 | + case 'pie_l': | |
| 608 | + guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid); | |
| 609 | + break; | |
| 610 | + case 'donut_l': | |
| 611 | + guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, 'doughnut'); | |
| 612 | + break; | |
| 613 | + case 'polar_l': | |
| 614 | + guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, 'polarArea'); | |
| 615 | + break; | |
| 616 | + case 'bar_l': | |
| 617 | + guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, 'bar', $pid); | |
| 618 | + break; | |
| 619 | + case 'horizontalbar_l': | |
| 620 | + guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, 'horizontalBar', $pid); | |
| 621 | + break; | |
| 622 | + case 'stackedbar_l': | |
| 623 | + guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, 'stackedBar', $pid); | |
| 624 | + break; | |
| 625 | + case 'scatter_l': | |
| 626 | + guaven_sqlcharts_scatterdata($title, $labels, $values, $ylabel, $pid); | |
| 627 | + break; | |
| 628 | + case 'custom': | |
| 629 | + guaven_sqlcharts_custom($title, $labels, $values, $ylabel, $pid); | |
| 630 | + break; | |
| 316 | 631 | } |
| 317 | - if ($tip_g == 'area_l') { | |
| 318 | - guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'true', $pid); | |
| 319 | - } elseif ($tip_g == 'pie_l') { | |
| 320 | - guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid); | |
| 321 | - } elseif ($tip_g == 'donut_l') { | |
| 322 | - guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, 'doughnut'); | |
| 323 | - } elseif ($tip_g == 'bar_l') { | |
| 324 | - guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, 'bar', $pid); | |
| 325 | - } elseif ($tip_g == 'horizontalbar_l') { | |
| 326 | - guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, 'horizontalBar', $pid); | |
| 327 | - } | |
| 328 | - elseif($tip_g == 'custom'){ | |
| 329 | - guaven_sqlcharts_custom($title, $labels, $values, $ylabel, $pid); | |
| 330 | - } | |
| 331 | - elseif ($tip_g == 'polar_l') { | |
| 332 | - guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, 'polarArea'); | |
| 333 | - } | |
| 334 | 632 | } |
| 335 | 633 | |
| 336 | 634 | function guaven_sqlcharts_custom($title, $labels, $values, $ylabel, $pid){ |
| 337 | 635 | do_action('guaven_sqlcharts_custom',$title, $labels, $values, $ylabel, $pid); |
| @@ -356,10 +654,23 @@ | ||
| 356 | 654 | foreach($variables_arr as $varfield){ |
| 357 | 655 | $varfield_arr=explode("~",$varfield); |
| 358 | 656 | if (count($varfield_arr)<3) continue; |
| 359 | 657 | $varfield_arr=array_map("trim",$varfield_arr); |
| 360 | - if (!empty($_GET[$varfield_arr[0]])) $varreplacement=$_GET[$varfield_arr[0]]; else $varreplacement=$varfield_arr[1]; | |
| 361 | - if (!is_numeric($varreplacement) and strpos($varreplacement,'()')===false) $varreplacement='"'.$varreplacement.'"'; | |
| 658 | + if (!empty($_GET[$varfield_arr[0]])) { | |
| 659 | + // User-supplied input: no () bypass allowed — sanitize strictly | |
| 660 | + $varreplacement = str_replace(';', '', sanitize_text_field(wp_unslash($_GET[$varfield_arr[0]]))); | |
| 661 | + if (is_numeric($varreplacement)) { | |
| 662 | + $varreplacement = $varreplacement + 0; | |
| 663 | + } else { | |
| 664 | + $varreplacement = '"' . esc_sql($varreplacement) . '"'; | |
| 665 | + } | |
| 666 | + } else { | |
| 667 | + // Admin-configured default value: allow () for SQL functions (e.g. NOW()) | |
| 668 | + $varreplacement = $varfield_arr[1]; | |
| 669 | + if (!is_numeric($varreplacement) && strpos($varreplacement,'()')===false) { | |
| 670 | + $varreplacement = '"' . esc_sql($varreplacement) . '"'; | |
| 671 | + } | |
| 672 | + } | |
| 362 | 673 | |
| 363 | 674 | $sql_initial=str_replace('{'.$varfield_arr[0].'}',$varreplacement,$sql_initial); |
| 364 | 675 | } |
| 365 | 676 | return $sql_initial; |
| @@ -377,38 +688,59 @@ | ||
| 377 | 688 | $gvalue=!empty($_GET[$vrow_arr[0]])?esc_attr(urldecode($_GET[$vrow_arr[0]])):''; |
| 378 | 689 | $dvalue=(strpos($vrow_arr[1],'()')===false)?esc_attr($vrow_arr[1]):''; |
| 379 | 690 | if ($vrow_arr[3]=='date') { |
| 380 | 691 | $dateexists=true; |
| 381 | - $topform.= $vrow_arr[2].' <input autocomplete="off" style="max-width:210px" type="text" | |
| 692 | + $topform.= '<span class="gvn-filter-field"><label>'.$vrow_arr[2].'</label> <input class="gws_datepicker" autocomplete="off" type="text" | |
| 382 | 693 | value="'.$gvalue.'" |
| 383 | - data-toggle="datepicker" name="'.$vrow_arr[0].'" placeholder="'.$dvalue.'"> | |
| 694 | + data-toggle="datepicker" name="'.$vrow_arr[0].'" placeholder="'.$dvalue.'"></span> | |
| 384 | 695 | ';} |
| 385 | 696 | else { |
| 386 | - $topform.= $vrow_arr[2].' <input autocomplete="off" style="max-width:210px;'.($vrow_arr[3]=='number'?'width:100px;':'').'" | |
| 697 | + $topform.= '<span class="gvn-filter-field"><label>'.$vrow_arr[2].'</label> <input autocomplete="off" | |
| 387 | 698 | type="'.$vrow_arr[3].'" |
| 388 | - value="'.$gvalue.'" name="'.$vrow_arr[0].'" placeholder="'.$dvalue.'"> | |
| 699 | + value="'.$gvalue.'" name="'.$vrow_arr[0].'" placeholder="'.$dvalue.'"></span> | |
| 389 | 700 | '; |
| 390 | 701 | } |
| 391 | 702 | } |
| 392 | 703 | if (!empty($topform)) { |
| 393 | - $topform='<form method="get" action="" class="guaven_sqlcharts_form">'.$topform.' | |
| 394 | - <input type="submit" | |
| 395 | - value="'.(get_post_meta($atts['id'], 'guaven_sqlcharts_formpartbutton', true)!=''?esc_attr(get_post_meta($atts['id'], 'guaven_sqlcharts_formpartbutton', true)):'OK').'"></form>'; | |
| 396 | - if ($dateexists) $topform.='<script>setTimeout(function(){jQuery(\'[data-toggle="datepicker"]\').datepicker({format: \'yyyy-mm-dd\'}); | |
| 397 | -},300);</script>'; | |
| 398 | - return $topform ; | |
| 704 | + $allowed_html = array( | |
| 705 | + 'form' => array( | |
| 706 | + 'method' => array(), | |
| 707 | + 'action' => array(), | |
| 708 | + 'class' => array() | |
| 709 | + ), | |
| 710 | + 'input' => array( | |
| 711 | + 'type' => array(), | |
| 712 | + 'value' => array(), | |
| 713 | + 'name' => array(), | |
| 714 | + 'class' => array(), | |
| 715 | + 'data-toggle'=>array(), | |
| 716 | + 'placeholder'=>array(), | |
| 717 | + 'autocomplete'=>array(), | |
| 718 | + 'style'=>[] | |
| 719 | + ), | |
| 720 | + 'span' => array('class' => array()), | |
| 721 | + 'label' => array(), | |
| 722 | + ); | |
| 723 | + | |
| 724 | + $submit_button_value = get_post_meta($atts['id'], 'guaven_sqlcharts_formpartbutton', true) != '' | |
| 725 | + ? esc_attr(get_post_meta($atts['id'], 'guaven_sqlcharts_formpartbutton', true)) | |
| 726 | + : 'OK'; | |
| 727 | + | |
| 728 | + $topform = '<form method="get" action="" class="guaven_sqlcharts_form">' . $topform . ' | |
| 729 | + <input type="submit" value="' . $submit_button_value . '"></form>'; | |
| 730 | + | |
| 731 | + echo wp_kses($topform, $allowed_html); | |
| 399 | 732 | } |
| 400 | - return; | |
| 401 | 733 | } |
| 402 | 734 | |
| 403 | 735 | |
| 404 | -function guaven_sqlcharts_encrypt_decrypt($action, $string) | |
| 736 | +function guaven_sqlcharts_encrypt_decrypt($action, $string) | |
| 405 | 737 | { |
| 406 | 738 | $output = false; |
| 407 | 739 | $encrypt_method = "AES-256-CBC"; |
| 408 | 740 | $secret_key = 'GWSCHARTPL2022.2016.'; |
| 409 | 741 | $secret_iv = 'GWSCHARTPL2016.2022'; |
| 410 | - $key = hash('sha256', $secret_key); | |
| 742 | + $key = hash('sha256', $secret_key); | |
| 411 | 743 | $iv = substr(hash('sha256', $secret_iv), 0, 16); |
| 412 | 744 | if ( $action == 'encrypt' ) { |
| 413 | 745 | $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv); |
| 414 | 746 | $output = base64_encode($output); |
| @@ -417,10 +749,13 @@ | ||
| 417 | 749 | } |
| 418 | 750 | return $output; |
| 419 | 751 | } |
| 420 | 752 | |
| 421 | -function guaven_sqlcharts_local_shortcode($atts) | |
| 422 | -{ | |
| 753 | +function guaven_sqlcharts_local_shortcode($atts) { | |
| 754 | + if(empty($atts['id']))return 'ID is missing.'; | |
| 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.'; | |
| 423 | 758 | $remote_host=get_post_meta($atts['id'], 'guaven_sqlcharts_dbhost', true); |
| 424 | 759 | if ($remote_host!=''){ |
| 425 | 760 | $remote_db=get_post_meta($atts['id'], 'guaven_sqlcharts_dbname', true); |
| 426 | 761 | $remote_login=get_post_meta($atts['id'], 'guaven_sqlcharts_dblogin', true); |
| @@ -435,22 +770,39 @@ | ||
| 435 | 770 | } |
| 436 | 771 | |
| 437 | 772 | $GLOBALS["guaven_sqlcharts_atts"]=$atts; |
| 438 | 773 | |
| 439 | - $sql = html_entity_decode(get_post_meta($atts['id'], 'guaven_sqlcharts_code', true)); | |
| 774 | + $sql = guaven_sqlcharts_get_code($atts['id']); | |
| 775 | + if(empty($sql))return 'SQL query is missing.'; | |
| 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 | + } | |
| 790 | + | |
| 440 | 791 | $sql=gvn_chart_put_variables($sql,$atts['id']); |
| 792 | + $sql=apply_filters('guaven_sqlcharts_rendered_sql',$sql,$atts); | |
| 793 | + | |
| 794 | + // command check on the final SQL, after every shortcode argument and filter value is in place | |
| 441 | 795 | $blacklister_f = gvn_chart_check_sql_query($sql); |
| 442 | - if ($blacklister_f == 1) | |
| 443 | - return 'You given SQL code contains forbidden commands. Remember that you should only use SELECT queries'; | |
| 444 | - $tip_g = get_post_meta($atts['id'], 'guaven_sqlcharts_graphtype', true); | |
| 796 | + if ($blacklister_f == 1)return 'You given SQL code contains forbidden commands. Remember that you should only use SELECT queries'; | |
| 797 | + $tip_g = guaven_sqlcharts_normalize_type(get_post_meta($atts['id'], 'guaven_sqlcharts_graphtype', true)); | |
| 445 | 798 | |
| 446 | - for($i=1;$i<20;$i++){ $replacearg=!empty($atts["arg".$i])?$atts["arg".$i]:0; | |
| 447 | - $sql=str_replace("{arg".$i."}",esc_sql($replacearg),$sql);} | |
| 448 | 799 | $sql_split = explode(';', $sql); |
| 449 | 800 | $labels_and_values = array(); |
| 450 | - $post_g = get_post($atts['id']); | |
| 801 | + $labels = $values = $ylabel = $xlabel = array(); | |
| 451 | 802 | |
| 452 | 803 | global $sqlcharts_inserted_script; |
| 804 | + ob_start(); | |
| 453 | 805 | for ($i = 0; $i < count($sql_split); $i++) { |
| 454 | 806 | if (!empty($sql_split[$i])) { |
| 455 | 807 | |
| 456 | 808 | $fvs = $wpdb->get_results($sql_split[$i]); |
| @@ -458,52 +810,104 @@ | ||
| 458 | 810 | $wpdb->show_errors(); |
| 459 | 811 | ob_start(); |
| 460 | 812 | $wpdb->print_error(); |
| 461 | 813 | $printerror = ob_get_clean(); |
| 462 | - if ($printerror != '' and strpos($printerror, "[]") === false) | |
| 814 | + if ($printerror != '' and strpos($printerror, "[]") === false){ | |
| 815 | + ob_end_clean(); | |
| 463 | 816 | return $printerror; |
| 464 | - elseif (empty($fvs)) | |
| 465 | - return 'Your SQL returnes empty date, please recheck your SQL query above'; | |
| 817 | + } | |
| 818 | + elseif (empty($fvs)){ | |
| 819 | + ob_end_clean(); | |
| 820 | + return 'Your SQL returnes empty data, please recheck your SQL query above'; | |
| 821 | + } | |
| 466 | 822 | } |
| 467 | 823 | |
| 468 | - ob_start(); | |
| 469 | - | |
| 470 | 824 | if (empty($sqlcharts_inserted_script)) |
| 471 | 825 | $sqlcharts_inserted_script = 1; |
| 472 | - $labels_and_values[$i] = guaven_get_labels_and_values($atts['id'], $fvs); | |
| 473 | - $labels[$i] = $labels_and_values[$i][0]; | |
| 474 | - $values[$i] = $labels_and_values[$i][1]; | |
| 475 | - $ylabel[$i] = !empty($labels_and_values[$i][2][$i]) ? $labels_and_values[$i][2][$i] : ''; | |
| 476 | - $xlabel[$i] = !empty($labels_and_values[$i][3][$i]) ? $labels_and_values[$i][3][$i] : ''; | |
| 826 | + $labels_and_values[$i] = guaven_get_labels_and_values($atts['id'], $fvs); | |
| 827 | + $labels[$i] = $labels_and_values[$i][0]; | |
| 828 | + $values[$i] = $labels_and_values[$i][1]; | |
| 829 | + $ylabel[$i] = !empty($labels_and_values[$i][2][$i]) ? $labels_and_values[$i][2][$i] : ''; | |
| 830 | + $xlabel[$i] = !empty($labels_and_values[$i][3][$i]) ? $labels_and_values[$i][3][$i] : ''; | |
| 477 | 831 | } |
| 478 | 832 | } |
| 479 | 833 | |
| 480 | -echo gvn_chart_top_form($atts); | |
| 481 | -?> | |
| 482 | - <canvas | |
| 483 | - id="ct-chart_<?php echo $sqlcharts_inserted_script; ?>" | |
| 834 | + gvn_chart_top_form($atts); | |
| 835 | + | |
| 836 | + // shortcode width/height attributes override the saved defaults | |
| 837 | + $chart_w = !empty($atts['width']) ? $atts['width'] : get_post_meta($atts['id'], 'guaven_sqlcharts_chartwidth', true); | |
| 838 | + $chart_h = !empty($atts['height']) ? $atts['height'] : get_post_meta($atts['id'], 'guaven_sqlcharts_chartheight', true); | |
| 839 | + $wrap_style = ''; | |
| 840 | + if ($chart_w != '') $wrap_style .= 'max-width:' . (int) $chart_w . 'px;'; | |
| 841 | + if ($chart_h != '') $wrap_style .= 'height:' . (int) $chart_h . 'px;'; | |
| 842 | + ?> | |
| 843 | + <div class="gvn-chartwrap"<?php echo $wrap_style != '' ? ' style="' . esc_attr($wrap_style) . '"' : ''; ?>> | |
| 844 | + <canvas | |
| 845 | + id="ct-chart_<?php echo esc_attr($sqlcharts_inserted_script); ?>" | |
| 484 | 846 | class="guaven_chart_canvas" |
| 485 | - 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;" | |
| 486 | 847 | ></canvas> |
| 848 | + </div> | |
| 487 | 849 | |
| 488 | 850 | <script type="text/javascript" class="gvn_charts_script" async> |
| 489 | 851 | var ctx = jQuery("#ct-chart_<?php |
| 490 | - echo $sqlcharts_inserted_script; | |
| 491 | -?>"); | |
| 852 | + echo esc_attr($sqlcharts_inserted_script); | |
| 853 | + ?>"); | |
| 492 | 854 | |
| 493 | -<?php | |
| 494 | - guaven_sqlcharts_print_chart_js($tip_g, $post_g->post_title, $labels, $values, $ylabel, $atts['id']); | |
| 495 | -?> | |
| 496 | -</script> | |
| 855 | + <?php | |
| 856 | + $print_data=apply_filters('guaven_sqlcharts_pre_print_vars',['tip_g'=>$tip_g, 'title'=>$post_g->post_title, | |
| 857 | + 'labels'=>$labels, 'values'=>$values, 'ylabel'=>$ylabel, 'pid'=>$atts['id']]); | |
| 858 | + guaven_sqlcharts_print_chart_js($print_data); | |
| 859 | + ?> | |
| 860 | + </script> | |
| 497 | 861 | |
| 498 | -<?php | |
| 499 | - if (!empty($atts["table"])) echo guaven_sqlcharts_tablepart($post_g->post_title, $labels, $values, $ylabel,$xlabel); | |
| 862 | + <?php | |
| 863 | + if (!empty($atts["table"])) guaven_sqlcharts_tablepart($post_g->post_title, $labels, $values, $ylabel,$xlabel); | |
| 500 | 864 | $sqlcharts_inserted_script++; |
| 501 | - return ob_get_clean(); | |
| 865 | + $ret=ob_get_clean(); | |
| 866 | + $ret=apply_filters( 'guaven_sqlcharts_final_output', $ret, $atts ); | |
| 867 | + return $ret; | |
| 502 | 868 | } |
| 503 | 869 | |
| 504 | 870 | add_shortcode('gvn_schart_2', 'guaven_sqlcharts_local_shortcode'); |
| 505 | 871 | |
| 872 | +// legacy alias: old Google-Chart era posts produced [gvn_schart id=".."] shortcodes | |
| 873 | +if (!shortcode_exists('gvn_schart')) { | |
| 874 | + add_shortcode('gvn_schart', 'guaven_sqlcharts_local_shortcode'); | |
| 875 | +} | |
| 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. | |
| 880 | +add_shortcode("gvn_schart_2_cached",function($atts){ | |
| 881 | + if(empty($atts["id"]))return; | |
| 882 | + $atts["id"]=intval($atts["id"]); | |
| 883 | + $expire=!empty($atts["expire"])?intval($atts["expire"]):3600; | |
| 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); | |
| 896 | + if(!empty($cached) and !isset($_GET["force_sql_cache_reload"]) )return $cached; | |
| 897 | + $tobecached=guaven_sqlcharts_local_shortcode($inner_atts); | |
| 898 | + set_transient($key, $tobecached,$expire); | |
| 899 | + return $tobecached; | |
| 900 | +}); | |
| 901 | + | |
| 902 | +// fixed, colorblind-friendly default palette (Tableau 10) used when no custom colors are set | |
| 903 | +function guaven_sqlcharts_default_palette(){ | |
| 904 | + return apply_filters('guaven_sqlcharts_default_palette', array( | |
| 905 | + '#4E79A7', '#F28E2B', '#E15759', '#76B7B2', '#59A14F', | |
| 906 | + '#EDC948', '#B07AA1', '#FF9DA7', '#9C755F', '#BAB0AC' | |
| 907 | + )); | |
| 908 | +} | |
| 909 | + | |
| 506 | 910 | function guaven_sqlcharts_colors($index, $pid = null){ |
| 507 | 911 | if(!isset($pid)) { |
| 508 | 912 | global $post; |
| 509 | 913 | $pid = $post->ID; |
| @@ -510,56 +914,124 @@ | ||
| 510 | 914 | } |
| 511 | 915 | $colors=get_post_meta($pid,'guaven_sqlcharts_colors',true); |
| 512 | 916 | $colors=explode(",",$colors); |
| 513 | 917 | if (!empty($colors[$index])) return $colors[$index]; |
| 514 | - return rand(0, 255) . ',' . rand(0, 255) . ',' . rand(0, 255); | |
| 918 | + $palette = guaven_sqlcharts_default_palette(); | |
| 919 | + return $palette[$index % count($palette)]; | |
| 515 | 920 | } |
| 516 | 921 | |
| 517 | -function guaven_sqlcharts_toshowlegend($pid){ | |
| 518 | - $guaven_sqlcharts_legend_position=get_post_meta($pid, 'guaven_sqlcharts_legend_position', true); | |
| 519 | - if(in_array($guaven_sqlcharts_legend_position,['top','bottom','left','right'])){ | |
| 520 | - $display='true';$position=$guaven_sqlcharts_legend_position; | |
| 922 | +// outputs 'maintainAspectRatio:false,' when an explicit height is set, so the | |
| 923 | +// chart fills its sized wrapper instead of keeping the default aspect ratio | |
| 924 | +function guaven_sqlcharts_mar($pid){ | |
| 925 | + $atts = isset($GLOBALS["guaven_sqlcharts_atts"]) ? $GLOBALS["guaven_sqlcharts_atts"] : array(); | |
| 926 | + $h = !empty($atts['height']) ? $atts['height'] : get_post_meta($pid, 'guaven_sqlcharts_chartheight', true); | |
| 927 | + return $h != '' ? 'maintainAspectRatio: false,' : ''; | |
| 928 | +} | |
| 929 | + | |
| 930 | +// outputs 'showAllTooltips: true,' when "Value labels" is checked; the values are drawn by the | |
| 931 | +// gvnShowAllValues plugin in asset/front.js (works for every chart type) | |
| 932 | +function guaven_sqlcharts_value_labels($pid){ | |
| 933 | + return get_post_meta($pid, 'guaven_sqlcharts_forcetooltips', true) != '' ? 'showAllTooltips: true,' : ''; | |
| 934 | +} | |
| 935 | + | |
| 936 | +// Chart.js scale title block built from the "X axis label" / "Y axis label" fields. | |
| 937 | +// $which is 'x' or 'y' (the *field* to use, not the scale). The Y label is only used as an axis | |
| 938 | +// title for single-series charts; with several ";"-separated series the legend names them instead. | |
| 939 | +function guaven_sqlcharts_axis_title($pid, $which){ | |
| 940 | + $key = $which == 'x' ? 'guaven_sqlcharts_xarg_l' : 'guaven_sqlcharts_yarg_l'; | |
| 941 | + $text = trim(html_entity_decode((string) get_post_meta($pid, $key, true), ENT_QUOTES, 'UTF-8')); | |
| 942 | + if ($text === '' or ($which == 'y' and strpos($text, ';') !== false)) return ''; | |
| 943 | + return 'title: {display: true, text: ' . wp_json_encode($text) . '},'; | |
| 944 | +} | |
| 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 | + | |
| 955 | +// dataset label as a safe JS string literal (labels saved before 3.0.1 may hold HTML entities) | |
| 956 | +function guaven_sqlcharts_js_label($label){ | |
| 957 | + return wp_json_encode(html_entity_decode((string) $label, ENT_QUOTES, 'UTF-8')); | |
| 958 | +} | |
| 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 | + } | |
| 521 | 985 | } |
| 522 | - else { | |
| 523 | - $display='false';$position='top'; | |
| 524 | - } | |
| 525 | - return "legend: {display: ".$display.",position:'".$position."'},"; | |
| 986 | + return $has_point ? $out : false; | |
| 526 | 987 | } |
| 527 | 988 | |
| 989 | +// X scale options for time-axis mode; gvnSqlChartsTimeTick (asset/front.js) formats the ticks as dates | |
| 990 | +function guaven_sqlcharts_time_axis_scale(){ | |
| 991 | + return "type: 'linear', offset: true, ticks: {callback: gvnSqlChartsTimeTick, maxRotation: 45},"; | |
| 992 | +} | |
| 993 | +// extra entry for the Chart.js "plugins" object in time-axis mode (tooltip title shown as a date) | |
| 994 | +function guaven_sqlcharts_time_axis_plugins($time_points){ | |
| 995 | + return $time_points !== false ? 'tooltip: {callbacks: {title: gvnSqlChartsTimeTooltipTitle}}' : ''; | |
| 996 | +} | |
| 997 | + | |
| 528 | 998 | function guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, $type = 'bar', $pid = null) |
| 529 | 999 | { |
| 1000 | + $horizontal = ($type == 'horizontalBar'); | |
| 1001 | + $forcestack = ($type == 'stackedBar'); | |
| 1002 | + $stacked = ($forcestack or get_post_meta($pid, 'guaven_sqlcharts_nostacked', true) != 1) ? 'true' : 'false'; | |
| 1003 | + $time_points = $horizontal ? false : guaven_sqlcharts_time_axis_points($pid, $values); | |
| 530 | 1004 | ?> |
| 531 | 1005 | var data = { |
| 532 | - labels: [<?php | |
| 533 | - echo implode(",", $labels[0]); | |
| 534 | -?>], | |
| 1006 | + <?php if ($time_points === false) { ?>labels: [<?php guaven_sqlcharts_merge_labeldata($labels);?>],<?php } ?> | |
| 535 | 1007 | datasets: [ |
| 536 | 1008 | <?php |
| 537 | - for ($i = 0; $i < count($values); $i++) { | |
| 1009 | + $values_new=guaven_sqlcharts_key_normalizer($values,$labels,$ylabel)[0]; | |
| 1010 | + $i=-1; | |
| 1011 | + foreach ($values_new as $key_ak=>$value_ak) { | |
| 1012 | + $i++; | |
| 1013 | + $points = $time_points !== false ? $time_points[$key_ak] : $values_new[$key_ak]; | |
| 538 | 1014 | ?> |
| 539 | 1015 | { |
| 540 | - <?php | |
| 541 | - if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){ | |
| 542 | - //passing chartJS params via the shortcode | |
| 543 | - echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]); | |
| 544 | - } | |
| 1016 | + <?php | |
| 1017 | + echo guaven_sqlcharts_dataset_params(); // "params" shortcode attribute (validated) | |
| 545 | 1018 | ?> |
| 546 | - label: "<?php | |
| 547 | - echo esc_attr($ylabel[$i]); | |
| 548 | -?>", | |
| 1019 | + label: <?php echo guaven_sqlcharts_js_label($ylabel[$key_ak]); ?>, | |
| 549 | 1020 | backgroundColor: [ |
| 550 | 1021 | <?php |
| 551 | - echo guaven_sqlcharts_colorgenerator(count($values[$i]), 0, 0, guaven_sqlcharts_colors($i, $pid)); | |
| 1022 | + echo wp_kses(guaven_sqlcharts_colorgenerator(count($points), 0, 0, guaven_sqlcharts_colors($i, $pid)),[]); | |
| 552 | 1023 | ?> |
| 553 | 1024 | ], |
| 554 | 1025 | borderColor: [ |
| 555 | 1026 | <?php |
| 556 | - echo guaven_sqlcharts_colorgenerator(count($values[$i]), 0, 0.2, guaven_sqlcharts_colors($i, $pid)); | |
| 1027 | + echo wp_kses(guaven_sqlcharts_colorgenerator(count($points), 0, 0.2, guaven_sqlcharts_colors($i, $pid)),[]); | |
| 557 | 1028 | ?> |
| 558 | 1029 | ], |
| 559 | 1030 | borderWidth: 1, |
| 1031 | + <?php if ($time_points !== false) echo 'maxBarThickness: 48,'; ?> | |
| 560 | 1032 | data: [<?php |
| 561 | - echo implode(",", $values[$i]); | |
| 1033 | + echo wp_kses(implode(",", $points),[]); | |
| 562 | 1034 | ?>], |
| 563 | 1035 | }, |
| 564 | 1036 | <?php |
| 565 | 1037 | } |
| @@ -566,30 +1038,34 @@ | ||
| 566 | 1038 | ?> |
| 567 | 1039 | ] |
| 568 | 1040 | }; |
| 569 | 1041 | var options={ |
| 570 | - <?php echo guaven_sqlcharts_toshowlegend($pid);?> | |
| 571 | 1042 | responsive: true, |
| 1043 | + <?php echo wp_kses(guaven_sqlcharts_mar($pid),[]); ?> | |
| 1044 | + <?php echo wp_kses(guaven_sqlcharts_value_labels($pid),[]); ?> | |
| 1045 | + <?php if ($horizontal) echo "indexAxis: 'y',"; ?> | |
| 572 | 1046 | scales: { |
| 573 | - xAxes: [{ | |
| 574 | - stacked: true, | |
| 1047 | + x: { | |
| 1048 | + <?php if ($time_points !== false) echo guaven_sqlcharts_time_axis_scale(); ?> | |
| 1049 | + <?php echo guaven_sqlcharts_axis_title($pid, $horizontal ? 'y' : 'x'); ?> | |
| 1050 | + stacked: <?php echo esc_js($stacked); ?>, | |
| 1051 | + beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?> | |
| 1052 | + }, | |
| 1053 | + y: { | |
| 1054 | + <?php echo guaven_sqlcharts_axis_title($pid, $horizontal ? 'x' : 'y'); ?> | |
| 1055 | + stacked: <?php echo esc_js($stacked); ?>, | |
| 1056 | + beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?>, | |
| 575 | 1057 | ticks: { |
| 576 | - beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?> | |
| 577 | - } | |
| 578 | - }], | |
| 579 | - yAxes: [{ | |
| 580 | - stacked: true, | |
| 581 | - ticks: { | |
| 582 | 1058 | <?php if(get_post_meta($pid, 'guaven_sqlcharts_round_y_values', true) == 1) echo 'precision: 0,'; ?> |
| 583 | - beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?> | |
| 584 | 1059 | } |
| 585 | - }] | |
| 1060 | + } | |
| 586 | 1061 | } |
| 1062 | + <?php | |
| 1063 | + guaven_sqlcharts_maybe_additional_parameters($pid, guaven_sqlcharts_time_axis_plugins($time_points)); | |
| 1064 | + ?> | |
| 587 | 1065 | }; |
| 588 | 1066 | var myBarChart = new Chart(ctx, { |
| 589 | - type: '<?php | |
| 590 | - echo $type; | |
| 591 | -?>', | |
| 1067 | + type: 'bar', | |
| 592 | 1068 | data: data, |
| 593 | 1069 | options: options |
| 594 | 1070 | }); |
| 595 | 1071 | <?php |
| @@ -594,51 +1070,61 @@ | ||
| 594 | 1070 | }); |
| 595 | 1071 | <?php |
| 596 | 1072 | } |
| 597 | 1073 | |
| 1074 | +function guaven_sqlcharts_merge_labeldata($labels){ | |
| 1075 | + if(count($labels)==1){echo wp_kses(implode(",",$labels[0]),[]);return;} | |
| 1076 | + $merged=[]; | |
| 1077 | + foreach($labels as $label){ | |
| 1078 | + $merged=array_merge($merged,$label); | |
| 1079 | + } | |
| 1080 | + echo wp_kses(implode(",",array_unique($merged)),[]); | |
| 1081 | +} | |
| 598 | 1082 | |
| 599 | - | |
| 600 | -function guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, $type = 'false', $pid = null) | |
| 1083 | +function guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, $type = 'false', $pid = null, $charttype = 'line', $stepped = false) | |
| 601 | 1084 | { |
| 1085 | + $time_points = ($charttype == 'radar') ? false : guaven_sqlcharts_time_axis_points($pid, $values); | |
| 602 | 1086 | ?> |
| 603 | 1087 | var data = { |
| 604 | - labels: [<?php | |
| 605 | - echo implode(",", $labels[0]); | |
| 606 | -?>], | |
| 1088 | + <?php if ($time_points === false) { ?>labels: [<?php guaven_sqlcharts_merge_labeldata($labels);?>],<?php } ?> | |
| 607 | 1089 | datasets: [ |
| 608 | 1090 | <?php |
| 609 | - for ($i = 0; $i < count($values); $i++) { | |
| 1091 | + $values_new=guaven_sqlcharts_key_normalizer($values,$labels,$ylabel)[0]; | |
| 1092 | + $dataset_count=count($values_new); | |
| 1093 | + $i=-1; | |
| 1094 | + foreach ($values_new as $key_ak=>$value_ak) { | |
| 1095 | + $i++; | |
| 1096 | + $points = $time_points !== false ? $time_points[$key_ak] : $values_new[$key_ak]; | |
| 1097 | + if ($type == 'radarfill') $fill = "'origin'"; | |
| 1098 | + elseif ($type == 'false') $fill = 'false'; | |
| 1099 | + else $fill = ($i == 0 and $dataset_count > 1) ? '"+1"' : '"origin"'; | |
| 610 | 1100 | ?> |
| 611 | 1101 | { |
| 612 | - <?php | |
| 613 | - if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){ | |
| 614 | - //passing chartJS params via the shortcode | |
| 615 | - echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]); | |
| 616 | - } | |
| 1102 | + <?php | |
| 1103 | + echo guaven_sqlcharts_dataset_params(); // "params" shortcode attribute (validated) | |
| 617 | 1104 | ?> |
| 618 | - label: "<?php | |
| 619 | - echo esc_attr($ylabel[$i]); | |
| 620 | -?>", | |
| 621 | - fill: <?php echo $type=="false"?$type:($i==0?'"+1"':'"origin"'); | |
| 1105 | + label: <?php echo guaven_sqlcharts_js_label($ylabel[$key_ak]); ?>, | |
| 1106 | + fill: <?php echo wp_kses($fill,[]); | |
| 622 | 1107 | ?>, |
| 623 | - lineTension: 0.1, | |
| 1108 | + tension: 0.1, | |
| 1109 | + <?php if ($stepped) echo 'stepped: true,'; ?> | |
| 624 | 1110 | backgroundColor: <?php |
| 625 | - echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid)); | |
| 1111 | + echo wp_kses_post(guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid))); | |
| 626 | 1112 | ?> |
| 627 | 1113 | borderColor: <?php |
| 628 | - echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid)); | |
| 1114 | + echo wp_kses_post(guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid))); | |
| 629 | 1115 | ?> |
| 630 | 1116 | pointBorderColor: <?php |
| 631 | - echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid)); | |
| 1117 | + echo wp_kses_post(guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid))); | |
| 632 | 1118 | ?> |
| 633 | 1119 | pointHoverBackgroundColor: <?php |
| 634 | - echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid)); | |
| 1120 | + echo wp_kses_post(guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid))); | |
| 635 | 1121 | ?> |
| 636 | 1122 | pointHoverBorderColor: <?php |
| 637 | - echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid)); | |
| 1123 | + echo wp_kses_post(guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid))); | |
| 638 | 1124 | ?> |
| 639 | 1125 | data: [<?php |
| 640 | - echo implode(",", $values[$i]); | |
| 1126 | + echo wp_kses_post(implode(",", $points)); | |
| 641 | 1127 | ?>], |
| 642 | 1128 | spanGaps: false, |
| 643 | 1129 | }, |
| 644 | 1130 | <?php |
| @@ -646,27 +1132,103 @@ | ||
| 646 | 1132 | ?> |
| 647 | 1133 | ] |
| 648 | 1134 | }; |
| 649 | 1135 | var myLineChart = new Chart(ctx, { |
| 650 | - type: 'line', | |
| 1136 | + type: '<?php echo esc_attr($charttype); ?>', | |
| 651 | 1137 | data: data, |
| 652 | 1138 | options: { |
| 653 | - <?php echo guaven_sqlcharts_toshowlegend($pid);?> | |
| 654 | 1139 | responsive: true, |
| 1140 | + <?php echo wp_kses(guaven_sqlcharts_mar($pid),[]); ?> | |
| 1141 | + <?php echo wp_kses(guaven_sqlcharts_value_labels($pid),[]); ?> | |
| 1142 | + <?php if ($charttype == 'radar') { ?> | |
| 655 | 1143 | scales: { |
| 656 | - xAxes: [{ | |
| 1144 | + r: { | |
| 1145 | + beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?> | |
| 1146 | + } | |
| 1147 | + } | |
| 1148 | + <?php } else { ?> | |
| 1149 | + scales: { | |
| 1150 | + x: { | |
| 657 | 1151 | display: true, |
| 1152 | + <?php if ($time_points !== false) echo guaven_sqlcharts_time_axis_scale(); ?> | |
| 1153 | + <?php echo guaven_sqlcharts_axis_title($pid, 'x'); ?> | |
| 1154 | + beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?> | |
| 1155 | + }, | |
| 1156 | + y: { | |
| 1157 | + <?php echo guaven_sqlcharts_axis_title($pid, 'y'); ?> | |
| 1158 | + beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?>, | |
| 658 | 1159 | ticks: { |
| 659 | - beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?> | |
| 1160 | + <?php if(get_post_meta($pid, 'guaven_sqlcharts_round_y_values', true) == 1) echo 'precision: 0,'; ?> | |
| 660 | 1161 | } |
| 661 | - }], | |
| 662 | - yAxes: [{ | |
| 1162 | + } | |
| 1163 | + } | |
| 1164 | + <?php } ?> | |
| 1165 | + <?php | |
| 1166 | + guaven_sqlcharts_maybe_additional_parameters($pid, guaven_sqlcharts_time_axis_plugins($time_points)); | |
| 1167 | + ?> | |
| 1168 | + | |
| 1169 | + } | |
| 1170 | +}); | |
| 1171 | + <?php | |
| 1172 | +} | |
| 1173 | + | |
| 1174 | +function guaven_sqlcharts_scatterdata($title, $labels, $values, $ylabel, $pid = null) | |
| 1175 | +{ | |
| 1176 | +?> | |
| 1177 | +var data = { | |
| 1178 | + datasets: [ | |
| 1179 | + <?php | |
| 1180 | + $i=-1; | |
| 1181 | + foreach ($values as $key_ak=>$value_ak) { | |
| 1182 | + $i++; | |
| 1183 | + $points=array(); | |
| 1184 | + foreach ($value_ak as $xval=>$yval) { | |
| 1185 | + $x = is_numeric($xval) ? $xval : '"'.esc_js($xval).'"'; | |
| 1186 | + $y = is_numeric($yval) ? $yval : '"'.esc_js($yval).'"'; | |
| 1187 | + $points[] = '{x:'.$x.',y:'.$y.'}'; | |
| 1188 | + } | |
| 1189 | +?> | |
| 1190 | + { | |
| 1191 | + <?php | |
| 1192 | + echo guaven_sqlcharts_dataset_params(); // "params" shortcode attribute (validated) | |
| 1193 | + ?> | |
| 1194 | + label: <?php echo guaven_sqlcharts_js_label(isset($ylabel[$key_ak])?$ylabel[$key_ak]:''); ?>, | |
| 1195 | + backgroundColor: <?php | |
| 1196 | + echo wp_kses_post(guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid))); | |
| 1197 | +?> | |
| 1198 | + borderColor: <?php | |
| 1199 | + echo wp_kses_post(guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid))); | |
| 1200 | +?> | |
| 1201 | + data: [<?php echo wp_kses(implode(",", $points),[]); ?>], | |
| 1202 | + }, | |
| 1203 | + <?php | |
| 1204 | + } | |
| 1205 | +?> | |
| 1206 | + ] | |
| 1207 | +}; | |
| 1208 | +var myScatterChart = new Chart(ctx, { | |
| 1209 | + type: 'scatter', | |
| 1210 | + data: data, | |
| 1211 | + options: { | |
| 1212 | + responsive: true, | |
| 1213 | + <?php echo wp_kses(guaven_sqlcharts_mar($pid),[]); ?> | |
| 1214 | + <?php echo wp_kses(guaven_sqlcharts_value_labels($pid),[]); ?> | |
| 1215 | + scales: { | |
| 1216 | + x: { | |
| 1217 | + <?php echo guaven_sqlcharts_axis_title($pid, 'x'); ?> | |
| 1218 | + beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?> | |
| 1219 | + }, | |
| 1220 | + y: { | |
| 1221 | + <?php echo guaven_sqlcharts_axis_title($pid, 'y'); ?> | |
| 1222 | + beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?>, | |
| 663 | 1223 | ticks: { |
| 664 | - <?php if(get_post_meta($pid, 'guaven_sqlcharts_round_y_values', true) == 1) echo 'precision: 0,'; ?> | |
| 665 | - beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?> | |
| 1224 | + <?php if(get_post_meta($pid, 'guaven_sqlcharts_round_y_values', true) == 1) echo 'precision: 0,'; ?> | |
| 666 | 1225 | } |
| 667 | - }] | |
| 1226 | + } | |
| 668 | 1227 | } |
| 1228 | + <?php | |
| 1229 | + guaven_sqlcharts_maybe_additional_parameters($pid); | |
| 1230 | + ?> | |
| 669 | 1231 | } |
| 670 | 1232 | }); |
| 671 | 1233 | <?php |
| 672 | 1234 | } |
| @@ -671,43 +1233,66 @@ | ||
| 671 | 1233 | <?php |
| 672 | 1234 | } |
| 673 | 1235 | |
| 674 | 1236 | |
| 1237 | +function guaven_sqlcharts_maybe_additional_parameters($pid, $extra_plugins = ''){ | |
| 1238 | + if(function_exists('guaven_sqlcharts_maybe_additional_parameters_custom')){ | |
| 1239 | + wp_kses(guaven_sqlcharts_maybe_additional_parameters_custom($pid),[]); | |
| 1240 | + return; | |
| 1241 | + } | |
| 1242 | + $guaven_sqlcharts_legend_position=get_post_meta($pid, 'guaven_sqlcharts_legend_position', true); | |
| 1243 | + if(in_array($guaven_sqlcharts_legend_position,['top','bottom','left','right'])){ | |
| 1244 | + $display='true';$position=$guaven_sqlcharts_legend_position; | |
| 1245 | + } | |
| 1246 | + else { | |
| 1247 | + $display='false';$position='top'; | |
| 1248 | + } | |
| 1249 | + echo wp_kses( ",plugins: {legend: {display: ".$display.",position:'".$position."'}".($extra_plugins !== '' ? ','.$extra_plugins : '')."}",[]); | |
| 1250 | +} | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 675 | 1255 | function guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, $type = 'pie') |
| 676 | 1256 | { |
| 677 | 1257 | ?> |
| 678 | 1258 | var options={ |
| 679 | - <?php echo guaven_sqlcharts_toshowlegend($pid);?> | |
| 680 | - responsive: true, | |
| 1259 | + <?php echo wp_kses(guaven_sqlcharts_value_labels($pid),[]); ?> | |
| 1260 | + responsive: true | |
| 1261 | + <?php echo get_post_meta($pid,'guaven_sqlcharts_chartheight',true)!=''||!empty($GLOBALS["guaven_sqlcharts_atts"]['height'])?',maintainAspectRatio: false':''; ?> | |
| 1262 | + <?php | |
| 1263 | + guaven_sqlcharts_maybe_additional_parameters($pid); | |
| 1264 | + ?> | |
| 681 | 1265 | }; |
| 682 | 1266 | var data = { |
| 683 | - labels: [ <?php echo implode(",", $labels[0]);?>], | |
| 1267 | + labels: [ <?php guaven_sqlcharts_merge_labeldata($labels);?>], | |
| 684 | 1268 | datasets: [ |
| 685 | 1269 | <?php |
| 686 | 1270 | for ($i = 0; $i < count($values); $i++) { |
| 687 | 1271 | ?> |
| 688 | 1272 | { |
| 689 | - <?php | |
| 690 | - if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){ | |
| 691 | - //passing chartJS params via the shortcode | |
| 692 | - echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]); | |
| 693 | - } | |
| 1273 | + <?php | |
| 1274 | + echo guaven_sqlcharts_dataset_params(); // "params" shortcode attribute (validated) | |
| 694 | 1275 | ?> |
| 695 | 1276 | data: [<?php |
| 696 | - echo implode(",", $values[$i]); | |
| 1277 | + echo wp_kses(implode(",", $values[$i]),[]); | |
| 697 | 1278 | ?>], |
| 698 | 1279 | backgroundColor: [ |
| 699 | 1280 | <?php |
| 1281 | + $ii=0; | |
| 700 | 1282 | foreach($values[$i] as $vci=>$valuecolor){ |
| 701 | - echo guaven_sqlcharts_colorgenerator(1, 0, 0, guaven_sqlcharts_colors($vci, $pid)); | |
| 702 | - } | |
| 1283 | + echo wp_kses_post(guaven_sqlcharts_colorgenerator(1, 0, -0.1, guaven_sqlcharts_colors($ii, $pid))); | |
| 1284 | + $ii++; | |
| 1285 | + } | |
| 703 | 1286 | ?> |
| 704 | 1287 | ], |
| 705 | 1288 | hoverBackgroundColor: [ |
| 706 | 1289 | <?php |
| 1290 | + $ii=0; | |
| 707 | 1291 | foreach($values[$i] as $vci=>$valuecolor){ |
| 708 | - echo guaven_sqlcharts_colorgenerator(1, 0, 0.2, guaven_sqlcharts_colors($vci, $pid)); | |
| 709 | - } | |
| 1292 | + echo wp_kses_post(guaven_sqlcharts_colorgenerator(1, 0, 0.2, guaven_sqlcharts_colors($ii, $pid))); | |
| 1293 | + $ii++; | |
| 1294 | + } | |
| 710 | 1295 | ?> |
| 711 | 1296 | ] |
| 712 | 1297 | }, |
| 713 | 1298 | <?php |
| @@ -716,9 +1301,9 @@ | ||
| 716 | 1301 | ] |
| 717 | 1302 | }; |
| 718 | 1303 | var myPieChart = new Chart(ctx,{ |
| 719 | 1304 | type: '<?php |
| 720 | - echo $type; | |
| 1305 | + echo esc_attr($type); | |
| 721 | 1306 | ?>', |
| 722 | 1307 | data: data, |
| 723 | 1308 | options: options |
| 724 | 1309 | }); |
| @@ -762,20 +1347,67 @@ | ||
| 762 | 1347 | |
| 763 | 1348 | function guaven_sqlcharts_tablepart($title, $labels, $values, $ylabel,$xlabel){ |
| 764 | 1349 | $tabledata=''; |
| 765 | 1350 | $fcol=[];$scol=[]; |
| 766 | - $tablein='<br>'; | |
| 767 | - foreach($values as $valuerow){ | |
| 1351 | + $empty_cell=apply_filters( 'guaven_sqlcharts_table_empty_cell','<td></td>'); | |
| 1352 | + $tablein=''; | |
| 1353 | + foreach($values as $row=>$valuerow){ | |
| 768 | 1354 | foreach ($valuerow as $key => $value) { |
| 769 | - $fcol[$key]='<td>'.str_replace('"',"",$labels[0][$key]).'</td>'; | |
| 770 | - $scol[$key][]='<td>'.$value.'</td>'; | |
| 771 | - | |
| 1355 | + $putval=$labels[$row][$key]??''; | |
| 1356 | + $fcol[$key]='<td>'.str_replace('"',"",$putval).'</td>'; | |
| 1357 | + $scol[$key][$row]='<td>'.$value.'</td>'; | |
| 772 | 1358 | } |
| 1359 | + foreach($scol as $scolkey=>$scolvalue){ | |
| 1360 | + for($i=0;$i<count($values);$i++){ | |
| 1361 | + //echo $i; | |
| 1362 | + if(!isset($scolvalue[$i]))$scol[$scolkey][$i]=$empty_cell;; | |
| 1363 | + } | |
| 1364 | + ksort($scol[$scolkey]); | |
| 1365 | + } | |
| 773 | 1366 | } |
| 1367 | + | |
| 774 | 1368 | foreach($fcol as $key=>$value){ |
| 775 | 1369 | $tablein.='<tr>'.$value.implode(" ",$scol[$key]).'</tr>'.PHP_EOL; |
| 776 | 1370 | } |
| 777 | - $tabledata.='<table><tr><th>'.$xlabel[0].'</th><th>'.implode("</th><th>",$ylabel).'</th></tr> | |
| 778 | - '.$tablein.'</table><br>'; | |
| 779 | - return $tabledata; | |
| 1371 | + $tabledata.='<div class="gvn-tablewrap"><table class="gvn-table"><tr><th>'.$xlabel[0].'</th><th>'.implode("</th><th>",$ylabel).'</th></tr> | |
| 1372 | + '.$tablein.'</table></div><br>'; | |
| 780 | 1373 | |
| 1374 | + echo wp_kses_post($tabledata); | |
| 1375 | + | |
| 781 | 1376 | } |
| 1377 | + | |
| 1378 | +function guaven_sqlcharts_graphtype($post){ | |
| 1379 | + if (strpos(get_post_meta($post->ID, 'guaven_sqlcharts_graphtype', true), "_l") !== false) | |
| 1380 | + $postfix = '_2'; | |
| 1381 | + else $postfix = ''; | |
| 1382 | + return $postfix; | |
| 1383 | +} | |
| 1384 | + | |
| 1385 | +add_filter('the_content',function($content){ | |
| 1386 | + if(!is_singular('gvn_schart'))return $content; | |
| 1387 | + global $post; | |
| 1388 | + $postfix=guaven_sqlcharts_graphtype($post); | |
| 1389 | + return '[gvn_schart'.$postfix.' id="'.$post->ID.'"'. | |
| 1390 | + (get_post_meta($post->ID,'guaven_sqlcharts_tablepart',true)!=''?' table="1"':'') | |
| 1391 | + .']'; | |
| 1392 | +}); | |
| 1393 | + | |
| 1394 | +function guaven_sqlcharts_key_normalizer($values,$labels,$ylabel){ | |
| 1395 | + $normalize_keys=[]; | |
| 1396 | + $empty_value=apply_filters( 'guaven_sqlcharts_table_empty_value',''); | |
| 1397 | + foreach ($values as $key_ak=>$value_ak) { | |
| 1398 | + $normalize_keys=array_merge($normalize_keys,array_keys($values[$key_ak])); | |
| 1399 | + } | |
| 1400 | + $values_normalized=[];$labels_normalized=[];$ylabel_normalized=[]; | |
| 1401 | + foreach($normalize_keys as $normalized_key){ | |
| 1402 | + foreach ($values as $key_ak=>$value_ak) { | |
| 1403 | + $values_normalized[$key_ak][$normalized_key]=isset( $values[$key_ak][$normalized_key])? $values[$key_ak][$normalized_key]:"'".$empty_value."'"; | |
| 1404 | + $labels_normalized[$key_ak][$normalized_key]=isset( $labels[$key_ak][$normalized_key])? $labels[$key_ak][$normalized_key]:"''"; | |
| 1405 | + $ylabel_normalized[$key_ak][$normalized_key]=isset( $ylabel[$key_ak][$normalized_key])? $ylabel[$key_ak][$normalized_key]:""; | |
| 1406 | + } | |
| 1407 | + } | |
| 1408 | + return [$values_normalized,$labels_normalized,$ylabel_normalized]; | |
| 1409 | +} | |
| 1410 | + | |
| 1411 | + | |
| 1412 | +add_filter('guaven_sqlcharts_table_empty_cell',function($str){return '<td>#</td>';}); | |
| 1413 | +add_filter('guaven_sqlcharts_table_empty_value',function($str){return 'N/A';}); | |