| @@ -41,16 +41,8 @@ | ||
| 41 | 41 | */ |
| 42 | 42 | private $_charts = array(); |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | - * Lazy render script. | |
| 46 | - * | |
| 47 | - * @access private | |
| 48 | - * @var bool | |
| 49 | - */ | |
| 50 | - private $lazy_render_script = true; | |
| 51 | - | |
| 52 | - /** | |
| 53 | 45 | * Constructor. |
| 54 | 46 | * |
| 55 | 47 | * @since 1.0.0 |
| 56 | 48 | * @uses add_filter() To add "do_shortcode" hook for "widget_text" and "term_description" filters. |
| @@ -60,12 +52,9 @@ | ||
| 60 | 52 | */ |
| 61 | 53 | public function __construct( Visualizer_Plugin $plugin ) { |
| 62 | 54 | parent::__construct( $plugin ); |
| 63 | 55 | |
| 64 | - $this->_addAction( 'wp_print_footer_scripts', 'printFooterScripts' ); | |
| 65 | 56 | $this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' ); |
| 66 | - $this->_addAction( 'load-index.php', 'enqueueScripts' ); | |
| 67 | - $this->_addAction( 'load-visualizer_page_visualizer-setup-wizard', 'enqueueScripts' ); | |
| 68 | 57 | $this->_addAction( 'visualizer_enqueue_scripts', 'enqueueScripts' ); |
| 69 | 58 | $this->_addFilter( 'visualizer_get_language', 'getLanguage' ); |
| 70 | 59 | $this->_addShortcode( 'visualizer', 'renderChart' ); |
| 71 | 60 | |
| @@ -86,39 +75,21 @@ | ||
| 86 | 75 | |
| 87 | 76 | /** |
| 88 | 77 | * Adds the async attribute to certain scripts. |
| 89 | 78 | */ |
| 90 | - public function script_loader_tag( $tag, $handle, $src ) { | |
| 79 | + function script_loader_tag( $tag, $handle, $src ) { | |
| 91 | 80 | if ( is_admin() ) { |
| 92 | 81 | return $tag; |
| 93 | 82 | } |
| 94 | - // Async scripts. | |
| 95 | - $async_scripts = array( 'google-jsapi', 'chartjs', 'visualizer-datatables' ); | |
| 96 | - foreach ( $async_scripts as $async ) { | |
| 83 | + | |
| 84 | + $scripts = array( 'google-jsapi-new', 'google-jsapi-old', 'visualizer-render-google-lib', 'visualizer-render-google' ); | |
| 85 | + | |
| 86 | + foreach ( $scripts as $async ) { | |
| 97 | 87 | if ( $async === $handle ) { |
| 98 | - $tag = str_replace( ' src', ' async src', $tag ); | |
| 88 | + $tag = str_replace( ' src', ' async="async" defer="defer" src', $tag ); | |
| 99 | 89 | break; |
| 100 | 90 | } |
| 101 | 91 | } |
| 102 | - | |
| 103 | - // Async scripts. | |
| 104 | - $scripts = array( 'dom-to-image' ); | |
| 105 | - foreach ( array( 'async', 'defer' ) as $attr ) { | |
| 106 | - if ( wp_scripts()->get_data( $handle, $attr ) ) { | |
| 107 | - break; | |
| 108 | - } | |
| 109 | - if ( in_array( $handle, $async_scripts, true ) || false === strpos( $handle, 'visualizer-' ) ) { | |
| 110 | - break; | |
| 111 | - } | |
| 112 | - if ( ! preg_match( ":\s$attr(=|>|\s):", $tag ) ) { | |
| 113 | - $tag = preg_replace( ':(?=></script>):', " $attr", $tag, 1 ); | |
| 114 | - if ( $this->lazy_render_script ) { | |
| 115 | - $tag = str_replace( ' src', ' data-visualizer-script', $tag ); | |
| 116 | - } | |
| 117 | - } | |
| 118 | - // Only allow async or defer, not both. | |
| 119 | - break; | |
| 120 | - } | |
| 121 | 92 | return $tag; |
| 122 | 93 | } |
| 123 | 94 | |
| 124 | 95 | /** |
| @@ -123,9 +94,9 @@ | ||
| 123 | 94 | |
| 124 | 95 | /** |
| 125 | 96 | * Returns the language/locale. |
| 126 | 97 | */ |
| 127 | - public function getLanguage( $dummy, $only_language ) { | |
| 98 | + function getLanguage( $dummy, $only_language ) { | |
| 128 | 99 | return $this->get_language(); |
| 129 | 100 | } |
| 130 | 101 | |
| 131 | 102 | |
| @@ -131,38 +102,14 @@ | ||
| 131 | 102 | |
| 132 | 103 | /** |
| 133 | 104 | * Registers the endpoints |
| 134 | 105 | */ |
| 135 | - public function endpoint_register() { | |
| 106 | + function endpoint_register() { | |
| 136 | 107 | register_rest_route( |
| 137 | 108 | 'visualizer/v' . VISUALIZER_REST_VERSION, |
| 138 | 109 | '/action/(?P<chart>\d+)/(?P<type>.+)/', |
| 139 | 110 | array( |
| 140 | - // POST is required for save/cancel, GET for all others | |
| 141 | - 'methods' => array( 'POST', 'GET' ), | |
| 142 | - 'args' => array( | |
| 143 | - 'chart' => array( | |
| 144 | - 'required' => true, | |
| 145 | - 'sanitize_callback' => function ( $param ) { | |
| 146 | - return is_numeric( $param ) ? $param : null; | |
| 147 | - }, | |
| 148 | - ), | |
| 149 | - 'type' => array( | |
| 150 | - 'required' => true, | |
| 151 | - 'type' => 'string', | |
| 152 | - 'enum' => array_merge( array( 'save', 'cancel' ), array_keys( $this->get_actions() ) ), | |
| 153 | - ), | |
| 154 | - ), | |
| 155 | - 'permission_callback' => function ( WP_REST_Request $request ) { | |
| 156 | - $chart_id = filter_var( sanitize_text_field( $request->get_param( 'chart' ), FILTER_VALIDATE_INT ) ); | |
| 157 | - if ( ! empty( $chart_id ) && in_array( $request->get_param( 'type' ), array( 'save', 'cancel' ), true ) ) { | |
| 158 | - // let save and cancel go without any check as past version of pro | |
| 159 | - // did not send the X-WP-Nonce | |
| 160 | - // we can change this at a later date. | |
| 161 | - return true; | |
| 162 | - } | |
| 163 | - return ! empty( $chart_id ) && apply_filters( 'visualizer_pro_show_chart', true, $chart_id ); | |
| 164 | - }, | |
| 111 | + 'methods' => array( 'GET', 'POST' ), | |
| 165 | 112 | 'callback' => array( $this, 'perform_action' ), |
| 166 | 113 | ) |
| 167 | 114 | ); |
| 168 | 115 | } |
| @@ -172,42 +119,17 @@ | ||
| 172 | 119 | * |
| 173 | 120 | * @access private |
| 174 | 121 | */ |
| 175 | 122 | private function get_actions() { |
| 176 | - $actions = apply_filters( | |
| 123 | + return apply_filters( | |
| 177 | 124 | 'visualizer_action_buttons', |
| 178 | 125 | array( |
| 179 | - 'print' => array( | |
| 180 | - 'label' => esc_html__( 'Print', 'visualizer' ), | |
| 181 | - 'title' => esc_html__( 'Print Chart', 'visualizer' ), | |
| 182 | - ), | |
| 183 | - 'csv' => array( | |
| 184 | - 'label' => esc_html__( 'CSV', 'visualizer' ), | |
| 185 | - 'title' => esc_html__( 'Download as a CSV', 'visualizer' ), | |
| 186 | - ), | |
| 187 | - 'xls' => array( | |
| 188 | - 'label' => esc_html__( 'Excel', 'visualizer' ), | |
| 189 | - 'title' => esc_html__( 'Download as a spreadsheet', 'visualizer' ), | |
| 190 | - ), | |
| 191 | - 'copy' => array( | |
| 192 | - 'label' => esc_html__( 'Copy', 'visualizer' ), | |
| 193 | - 'title' => esc_html__( 'Copy data', 'visualizer' ), | |
| 194 | - ), | |
| 195 | - 'image' => array( | |
| 196 | - 'label' => esc_html__( 'Download', 'visualizer' ), | |
| 197 | - 'title' => esc_html__( 'Download as an image', 'visualizer' ), | |
| 198 | - ), | |
| 126 | + 'print' => __( 'Print', 'visualizer' ), | |
| 127 | + 'csv' => __( 'CSV', 'visualizer' ), | |
| 128 | + 'xls' => __( 'Excel', 'visualizer' ), | |
| 129 | + 'copy' => __( 'Copy', 'visualizer' ), | |
| 199 | 130 | ) |
| 200 | 131 | ); |
| 201 | - | |
| 202 | - // backward compatibility before label and title attributes were introduced. | |
| 203 | - if ( isset( $actions['edit'] ) && is_string( $actions['edit'] ) ) { | |
| 204 | - $actions['edit'] = array( | |
| 205 | - 'label' => esc_html__( 'Edit', 'visualizer' ), | |
| 206 | - 'title' => esc_html__( 'Edit data', 'visualizer' ), | |
| 207 | - ); | |
| 208 | - } | |
| 209 | - return $actions; | |
| 210 | 132 | } |
| 211 | 133 | |
| 212 | 134 | /** |
| 213 | 135 | * The print button |
| @@ -238,23 +160,8 @@ | ||
| 238 | 160 | break; |
| 239 | 161 | case 'xls': |
| 240 | 162 | $data = $this->_getDataAs( $chart_id, 'xls' ); |
| 241 | 163 | break; |
| 242 | - case 'image': | |
| 243 | - $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 244 | - $title = 'visualizer#' . $chart_id; | |
| 245 | - if ( ! empty( $settings['title'] ) ) { | |
| 246 | - $title = $settings['title']; | |
| 247 | - } | |
| 248 | - // for ChartJS, title is an array. | |
| 249 | - if ( is_array( $title ) && isset( $title['text'] ) ) { | |
| 250 | - $title = $title['text']; | |
| 251 | - } | |
| 252 | - if ( empty( $title ) ) { | |
| 253 | - $title = 'visualizer#' . $chart_id; | |
| 254 | - } | |
| 255 | - $data = array( 'name' => $title ); | |
| 256 | - break; | |
| 257 | 164 | default: |
| 258 | 165 | $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this ); |
| 259 | 166 | break; |
| 260 | 167 | } |
| @@ -271,8 +178,10 @@ | ||
| 271 | 178 | * @access public |
| 272 | 179 | */ |
| 273 | 180 | public function enqueueScripts() { |
| 274 | 181 | wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true ); |
| 182 | + wp_register_script( 'visualizer-clipboardjs', VISUALIZER_ABSURL . 'js/lib/clipboardjs/clipboard.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true ); | |
| 183 | + wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION ); | |
| 275 | 184 | do_action( 'visualizer_pro_frontend_load_resources' ); |
| 276 | 185 | } |
| 277 | 186 | |
| 278 | 187 | /** |
| @@ -291,110 +200,64 @@ | ||
| 291 | 200 | public function renderChart( $atts ) { |
| 292 | 201 | global $wp_version; |
| 293 | 202 | $atts = shortcode_atts( |
| 294 | 203 | array( |
| 295 | - // chart id | |
| 296 | - 'id' => false, | |
| 297 | - // chart class | |
| 298 | - 'class' => false, | |
| 299 | - // for lazy load | |
| 300 | - // set 'yes' to use the default intersection limit (300px) | |
| 301 | - // OR set a number (e.g. 700) to use 700px as the intersection limit | |
| 302 | - 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ), | |
| 303 | - // Use image chart | |
| 304 | - 'use_image' => 'no', | |
| 204 | + 'id' => false, // chart id | |
| 205 | + 'class' => false, // chart class | |
| 206 | + 'series' => false, // series filter hook | |
| 207 | + 'data' => false, // data filter hook | |
| 208 | + 'settings' => false, // data filter hook | |
| 305 | 209 | ), |
| 306 | 210 | $atts |
| 307 | 211 | ); |
| 308 | 212 | |
| 309 | - $atts['id'] = (int) $atts['id']; | |
| 310 | - $atts['class'] = esc_attr( $atts['class'] ); | |
| 311 | - $atts['lazy'] = esc_attr( $atts['lazy'] ); | |
| 312 | - $atts['use_image'] = esc_attr( $atts['use_image'] ); | |
| 313 | - | |
| 314 | - global $sitepress; | |
| 315 | - if ( Visualizer_Module::is_pro() && ( function_exists( 'icl_get_languages' ) && $sitepress instanceof \SitePress ) ) { | |
| 316 | - global $sitepress; | |
| 317 | - $locale = icl_get_current_language(); | |
| 318 | - $locale = strtolower( str_replace( '_', '-', $locale ) ); | |
| 319 | - $trid = $sitepress->get_element_trid( $atts['id'], 'post_' . Visualizer_Plugin::CPT_VISUALIZER ); | |
| 320 | - $translations = $sitepress->get_element_translations( $trid ); | |
| 321 | - if ( isset( $translations[ $locale ] ) && is_object( $translations[ $locale ] ) ) { | |
| 322 | - $atts['id'] = $translations[ $locale ]->element_id; | |
| 323 | - } | |
| 324 | - } | |
| 325 | - | |
| 326 | - $chart_data = $this->getChartData( Visualizer_Plugin::CF_CHART_CACHE, $atts['id'] ); | |
| 327 | - // if empty chart does not exists, then return empty string. | |
| 328 | - if ( ! $chart_data ) { | |
| 213 | + // if empty id or chart does not exists, then return empty string | |
| 214 | + if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 329 | 215 | return ''; |
| 330 | 216 | } |
| 331 | 217 | |
| 332 | - $chart = $chart_data['chart']; | |
| 333 | - $type = $chart_data['type']; | |
| 334 | - $series = $chart_data['series']; | |
| 335 | - // do not show the chart? | |
| 336 | 218 | if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) { |
| 337 | 219 | return ''; |
| 338 | 220 | } |
| 339 | 221 | |
| 340 | - if ( ! is_admin() && ! empty( $chart_data['is_woocommerce_report'] ) ) { | |
| 341 | - return ''; | |
| 222 | + // in case revisions exist. | |
| 223 | + // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found | |
| 224 | + if ( true === ( $revisions = $this->undoRevisions( $chart->ID, true ) ) ) { | |
| 225 | + $chart = get_post( $chart->ID ); | |
| 342 | 226 | } |
| 343 | 227 | |
| 344 | - // in case revisions exist (skip D3 charts to avoid reverting AI data). | |
| 345 | - $chart_library = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_LIBRARY, true ); | |
| 346 | - if ( 'd3' !== $chart_library ) { | |
| 347 | - $revisions = $this->undoRevisions( $chart->ID, true ); | |
| 348 | - if ( true === $revisions ) { | |
| 349 | - $chart = get_post( $chart->ID ); | |
| 350 | - } | |
| 351 | - } | |
| 352 | - | |
| 353 | 228 | $id = 'visualizer-' . $atts['id']; |
| 354 | 229 | $defaultClass = 'visualizer-front'; |
| 355 | 230 | $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] ); |
| 231 | + $class = $defaultClass . ' ' . $class . ' ' . 'visualizer-front-' . $atts['id']; | |
| 232 | + $class = ! empty( $class ) ? ' class="' . trim( $class ) . '"' : ''; | |
| 356 | 233 | |
| 357 | - $lazyClass = $atts['lazy'] === 'yes' || ctype_digit( $atts['lazy'] ) ? 'visualizer-lazy' : ''; | |
| 234 | + $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 358 | 235 | |
| 359 | - $class = sprintf( '%s %s %s %s', $defaultClass, $class, 'visualizer-front-' . $atts['id'], $lazyClass ); | |
| 360 | - $attributes = array(); | |
| 361 | - if ( ! empty( $class ) ) { | |
| 362 | - $attributes['class'] = trim( $class ); | |
| 363 | - } | |
| 364 | - | |
| 365 | - if ( ctype_digit( $atts['lazy'] ) ) { | |
| 366 | - $attributes['data-lazy-limit'] = $atts['lazy']; | |
| 367 | - } | |
| 368 | - | |
| 369 | - $attributes = apply_filters( 'visualizer_container_attributes', $attributes, $chart->ID ); | |
| 370 | - | |
| 371 | 236 | $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false ); |
| 372 | 237 | |
| 373 | - // Get and update settings. | |
| 374 | - $settings = $chart_data['settings']; | |
| 375 | - $settings = ! empty( $settings ) ? $settings : array(); | |
| 238 | + // fetch and update settings | |
| 239 | + $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 376 | 240 | if ( empty( $settings['height'] ) ) { |
| 377 | 241 | $settings['height'] = '400'; |
| 378 | 242 | } |
| 379 | 243 | |
| 380 | 244 | // handle series filter hooks |
| 381 | - $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, $series, $chart->ID, $type ); | |
| 382 | - | |
| 245 | + $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type ); | |
| 246 | + if ( ! empty( $atts['series'] ) ) { | |
| 247 | + $series = apply_filters( $atts['series'], $series, $chart->ID, $type ); | |
| 248 | + } | |
| 383 | 249 | // handle settings filter hooks |
| 384 | 250 | $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type ); |
| 385 | - | |
| 386 | - $lazy_load = isset( $settings['lazy_load_chart'] ) ? $settings['lazy_load_chart'] : false; | |
| 387 | - $lazy_load = apply_filters( 'visualizer_lazy_load_chart', $lazy_load, $chart->ID ); | |
| 388 | - $container_class = 'visualizer-front-container'; | |
| 389 | - if ( ! $lazy_load ) { | |
| 390 | - $this->lazy_render_script = false; | |
| 391 | - } else { | |
| 392 | - $container_class .= ' visualizer-lazy-render'; | |
| 251 | + if ( ! empty( $atts['settings'] ) ) { | |
| 252 | + $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type ); | |
| 393 | 253 | } |
| 394 | 254 | |
| 395 | 255 | // handle data filter hooks |
| 396 | - $data = self::get_chart_data( $chart, $type ); | |
| 256 | + $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type ); | |
| 257 | + if ( ! empty( $atts['data'] ) ) { | |
| 258 | + $data = apply_filters( $atts['data'], $data, $chart->ID, $type ); | |
| 259 | + } | |
| 397 | 260 | |
| 398 | 261 | $css = ''; |
| 399 | 262 | $arguments = $this->get_inline_custom_css( $id, $settings ); |
| 400 | 263 | if ( ! empty( $arguments ) ) { |
| @@ -407,37 +270,13 @@ | ||
| 407 | 270 | $id = $id . '-' . rand(); |
| 408 | 271 | |
| 409 | 272 | $amp = Visualizer_Plugin::instance()->getModule( Visualizer_Module_AMP::NAME ); |
| 410 | 273 | if ( $amp && $amp->is_amp() ) { |
| 411 | - return '<div data-amp-auto-lightbox-disable id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>'; | |
| 274 | + return '<div id="' . $id . '"' . $class . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>'; | |
| 412 | 275 | } |
| 413 | 276 | |
| 414 | - if ( 'yes' === $atts['use_image'] ) { | |
| 415 | - $chart_image = $chart_data['chart_image']; | |
| 416 | - if ( $chart_image ) { | |
| 417 | - return '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '>' . wp_get_attachment_image( $chart_image, 'full' ) . '</div>'; | |
| 418 | - } | |
| 419 | - } | |
| 420 | - // Unset chart image base64 string. | |
| 421 | - if ( isset( $settings['chart-img'] ) ) { | |
| 422 | - unset( $settings['chart-img'] ); | |
| 423 | - } | |
| 424 | - | |
| 425 | - $enable_controls = false; | |
| 426 | - if ( isset( $settings['controls'] ) && ! empty( $settings['controls']['controlType'] ) ) { | |
| 427 | - $column_index = $settings['controls']['filterColumnIndex']; | |
| 428 | - $column_label = $settings['controls']['filterColumnLabel']; | |
| 429 | - if ( 'false' !== $column_index || 'false' !== $column_label ) { | |
| 430 | - $enable_controls = true; | |
| 431 | - } | |
| 432 | - } | |
| 433 | - | |
| 434 | - if ( ! $enable_controls ) { | |
| 435 | - unset( $settings['controls'] ); | |
| 436 | - } | |
| 437 | - | |
| 438 | 277 | // add chart to the array |
| 439 | - $chart_entry = array( | |
| 278 | + $this->_charts[ $id ] = array( | |
| 440 | 279 | 'type' => $type, |
| 441 | 280 | 'series' => $series, |
| 442 | 281 | 'settings' => $settings, |
| 443 | 282 | 'data' => $data, |
| @@ -443,14 +282,33 @@ | ||
| 443 | 282 | 'data' => $data, |
| 444 | 283 | 'library' => $library, |
| 445 | 284 | ); |
| 446 | 285 | |
| 447 | - // For D3 charts, include the stored code so the renderer can execute it. | |
| 448 | - if ( 'd3' === $library ) { | |
| 449 | - $chart_entry['code'] = get_post_meta( $chart->ID, Visualizer_Module_AIBuilder::CF_D3_CODE, true ); | |
| 450 | - } | |
| 286 | + wp_register_script( | |
| 287 | + "visualizer-render-$library", | |
| 288 | + VISUALIZER_ABSURL . 'js/render-facade.js', | |
| 289 | + apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ), | |
| 290 | + Visualizer_Plugin::VERSION, | |
| 291 | + true | |
| 292 | + ); | |
| 451 | 293 | |
| 452 | - $this->_charts[ $id ] = $chart_entry; | |
| 294 | + wp_enqueue_script( "visualizer-render-$library" ); | |
| 295 | + wp_localize_script( | |
| 296 | + "visualizer-render-$library", | |
| 297 | + 'visualizer', | |
| 298 | + array( | |
| 299 | + 'charts' => $this->_charts, | |
| 300 | + 'language' => $this->get_language(), | |
| 301 | + 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 302 | + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', | |
| 303 | + 'i10n' => array( | |
| 304 | + 'copied' => __( 'Copied!', 'visualizer' ), | |
| 305 | + ), | |
| 306 | + 'page_type' => 'frontend', | |
| 307 | + 'is_front' => true, | |
| 308 | + ) | |
| 309 | + ); | |
| 310 | + wp_enqueue_style( 'visualizer-front' ); | |
| 453 | 311 | |
| 454 | 312 | $actions_div = ''; |
| 455 | 313 | $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] ); |
| 456 | 314 | if ( ! empty( $actions_visible ) ) { |
| @@ -463,16 +321,15 @@ | ||
| 463 | 321 | $array = explode( ';', $action_type ); |
| 464 | 322 | $key = $array[0]; |
| 465 | 323 | $mime = end( $array ); |
| 466 | 324 | } |
| 467 | - $label = $actions[ $key ]['label']; | |
| 468 | - $title = $actions[ $key ]['title']; | |
| 469 | - $actions_div .= sprintf( '<a href="#" class="visualizer-action visualizer-action-%s" data-visualizer-type="%s" data-visualizer-chart-id="%s" data-visualizer-container-id="%s" data-visualizer-mime="%s" title="%s"', $key, $key, $atts['id'], $id, $mime, $title ); | |
| 325 | + $label = $actions[ $key ]; | |
| 326 | + $actions_div .= '<a href="#" class="visualizer-action visualizer-action-' . $key . '" data-visualizer-type="' . $key . '" data-visualizer-chart-id="' . $atts['id'] . '" data-visualizer-container-id="' . $id . '" data-visualizer-mime="' . $mime . '" title="' . $label . '" '; | |
| 470 | 327 | |
| 471 | 328 | if ( 'copy' === $key ) { |
| 472 | 329 | $copy = $this->_getDataAs( $atts['id'], 'csv' ); |
| 473 | 330 | $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"'; |
| 474 | - wp_enqueue_script( 'clipboard' ); | |
| 331 | + wp_enqueue_script( 'visualizer-clipboardjs' ); | |
| 475 | 332 | } |
| 476 | 333 | |
| 477 | 334 | $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] ); |
| 478 | 335 | $actions_div .= '>' . $label . '</a> '; |
| @@ -482,339 +339,8 @@ | ||
| 482 | 339 | } |
| 483 | 340 | |
| 484 | 341 | $actions_div .= $css; |
| 485 | 342 | |
| 486 | - $_charts = array(); | |
| 487 | - $_charts_type = ''; | |
| 488 | - $count = 0; | |
| 489 | - static $visualizer_localized = false; | |
| 490 | - static $visualizer_charts = array(); | |
| 491 | - static $d3_localized = false; | |
| 492 | - $facade_handle = 'visualizer-render-facade'; | |
| 493 | - foreach ( $this->_charts as $id => $array ) { | |
| 494 | - $_charts = $this->_charts; | |
| 495 | - $library = $array['library']; | |
| 496 | - $_charts_type = $library; | |
| 497 | - $facade_deps = apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ); | |
| 498 | - if ( ! wp_script_is( $facade_handle, 'registered' ) ) { | |
| 499 | - wp_register_script( | |
| 500 | - $facade_handle, | |
| 501 | - VISUALIZER_ABSURL . 'js/render-facade.js', | |
| 502 | - $facade_deps, | |
| 503 | - Visualizer_Plugin::VERSION, | |
| 504 | - true | |
| 505 | - ); | |
| 506 | - wp_enqueue_script( $facade_handle ); | |
| 507 | - } else { | |
| 508 | - // Ensure newly discovered dependencies are attached to the facade. | |
| 509 | - global $wp_scripts; | |
| 510 | - if ( isset( $wp_scripts->registered[ $facade_handle ] ) ) { | |
| 511 | - $wp_scripts->registered[ $facade_handle ]->deps = array_unique( | |
| 512 | - array_merge( $wp_scripts->registered[ $facade_handle ]->deps, $facade_deps ) | |
| 513 | - ); | |
| 514 | - } | |
| 515 | - foreach ( $facade_deps as $dep_handle ) { | |
| 516 | - wp_enqueue_script( $dep_handle ); | |
| 517 | - } | |
| 518 | - } | |
| 519 | - | |
| 520 | - // For D3 charts, also enqueue the dedicated renderer bundle. | |
| 521 | - if ( 'd3' === $library ) { | |
| 522 | - $d3_renderer_asset = VISUALIZER_ABSPATH . '/classes/Visualizer/D3Renderer/build/index.asset.php'; | |
| 523 | - if ( file_exists( $d3_renderer_asset ) && ! wp_script_is( 'visualizer-d3-renderer', 'registered' ) ) { | |
| 524 | - /** | |
| 525 | - * Ignore missing build asset in source checkout. | |
| 526 | - * | |
| 527 | - * @phpstan-ignore-next-line | |
| 528 | - */ | |
| 529 | - $d3_asset = include $d3_renderer_asset; | |
| 530 | - wp_register_script( | |
| 531 | - 'visualizer-d3-renderer', | |
| 532 | - VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/index.js', | |
| 533 | - array_merge( $d3_asset['dependencies'], array( 'jquery' ) ), | |
| 534 | - $d3_asset['version'], | |
| 535 | - true | |
| 536 | - ); | |
| 537 | - wp_enqueue_script( 'visualizer-d3-renderer' ); | |
| 538 | - } | |
| 539 | - if ( ! $d3_localized ) { | |
| 540 | - wp_localize_script( | |
| 541 | - 'visualizer-d3-renderer', | |
| 542 | - 'vizD3Renderer', | |
| 543 | - array( | |
| 544 | - 'iframeJsUrl' => VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/iframe.js', | |
| 545 | - ) | |
| 546 | - ); | |
| 547 | - $d3_localized = true; | |
| 548 | - } | |
| 549 | - } | |
| 550 | - | |
| 551 | - if ( wp_script_is( $facade_handle ) && 0 === $count ) { | |
| 552 | - if ( ! $visualizer_localized ) { | |
| 553 | - $visualizer_charts = $this->_charts; | |
| 554 | - wp_localize_script( | |
| 555 | - $facade_handle, | |
| 556 | - 'visualizer', | |
| 557 | - array( | |
| 558 | - 'charts' => $this->_charts, | |
| 559 | - 'language' => $this->get_language(), | |
| 560 | - 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 561 | - 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', | |
| 562 | - 'wp_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 563 | - 'i10n' => array( | |
| 564 | - 'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ), | |
| 565 | - ), | |
| 566 | - 'page_type' => 'frontend', | |
| 567 | - 'is_front' => true, | |
| 568 | - ) | |
| 569 | - ); | |
| 570 | - $visualizer_localized = true; | |
| 571 | - } else { | |
| 572 | - $new_charts = array_diff_key( $this->_charts, $visualizer_charts ); | |
| 573 | - if ( ! empty( $new_charts ) ) { | |
| 574 | - wp_add_inline_script( | |
| 575 | - $facade_handle, | |
| 576 | - 'window.visualizer = window.visualizer || {};' . | |
| 577 | - 'window.visualizer.charts = Object.assign(window.visualizer.charts || {}, ' . wp_json_encode( $new_charts ) . ');', | |
| 578 | - 'before' | |
| 579 | - ); | |
| 580 | - $visualizer_charts = array_merge( $visualizer_charts, $new_charts ); | |
| 581 | - } | |
| 582 | - } | |
| 583 | - } | |
| 584 | - ++$count; | |
| 585 | - } | |
| 586 | - $prefix = 'C' . 'h' . 'a' . 'rt'; | |
| 587 | - if ( $type === 'tabular' ) { | |
| 588 | - $prefix = 'T' . 'a' . 'bl' . 'e'; | |
| 589 | - } | |
| 590 | - if ( Visualizer_Module::is_pro() && $enable_controls ) { | |
| 591 | - $actions_div .= '<div id="control_wrapper_' . $id . '"></div>'; | |
| 592 | - } | |
| 593 | 343 | // return placeholder div |
| 594 | - return '<div class="' . $container_class . '" id="chart_wrapper_' . $id . '">' . $actions_div . '<div id="' . $id . '"' . $this->getHtmlAttributes( $attributes ) . '></div>' . $this->addSchema( $chart->ID ) . '</div>'; | |
| 595 | - } | |
| 596 | - | |
| 597 | - /** | |
| 598 | - * Converts the array of attributes to a string of HTML attributes. | |
| 599 | - * | |
| 600 | - * @since ? | |
| 601 | - * | |
| 602 | - * @access private | |
| 603 | - */ | |
| 604 | - private function getHtmlAttributes( $attributes ) { | |
| 605 | - $string = ''; | |
| 606 | - if ( ! $attributes ) { | |
| 607 | - return $string; | |
| 608 | - } | |
| 609 | - | |
| 610 | - foreach ( $attributes as $name => $value ) { | |
| 611 | - $string .= sprintf( ' %s="%s"', esc_attr( $name ), esc_attr( $value ) ); | |
| 612 | - } | |
| 613 | - return $string; | |
| 614 | - } | |
| 615 | - | |
| 616 | - /** | |
| 617 | - * Adds the schema corresponding to the dataset. | |
| 618 | - * | |
| 619 | - * @since 3.3.2 | |
| 620 | - * | |
| 621 | - * @access private | |
| 622 | - */ | |
| 623 | - private function addSchema( $id ) { | |
| 624 | - // should we show informative errors as HTML comments? | |
| 625 | - $show_errors = apply_filters( 'visualizer_schema_show_errors', true, $id ); | |
| 626 | - | |
| 627 | - $settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 628 | - $title = ''; | |
| 629 | - if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) { | |
| 630 | - $title = $settings['title']; | |
| 631 | - if ( is_array( $title ) ) { | |
| 632 | - $title = $settings['title']['text']; | |
| 633 | - } | |
| 634 | - } | |
| 635 | - $title = apply_filters( 'visualizer_schema_name', $title, $id ); | |
| 636 | - if ( empty( $title ) ) { | |
| 637 | - if ( $show_errors ) { | |
| 638 | - return "<!-- Not showing structured data for chart $id because title is empty -->"; | |
| 639 | - } | |
| 640 | - return ''; | |
| 641 | - } | |
| 642 | - | |
| 643 | - $desc = ''; | |
| 644 | - if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) { | |
| 645 | - $desc = $settings['description']; | |
| 646 | - } | |
| 647 | - $desc = apply_filters( 'visualizer_schema_description', $desc, $id ); | |
| 648 | - if ( empty( $desc ) ) { | |
| 649 | - if ( $show_errors ) { | |
| 650 | - return "<!-- Not showing structured data for chart $id because description is empty -->"; | |
| 651 | - } | |
| 652 | - return ''; | |
| 653 | - } | |
| 654 | - | |
| 655 | - // descriptions below 50 chars are not allowed. | |
| 656 | - if ( strlen( $desc ) < 50 ) { | |
| 657 | - if ( $show_errors ) { | |
| 658 | - return "<!-- Not showing structured data for chart $id because description is shorter than 50 characters -->"; | |
| 659 | - } | |
| 660 | - return ''; | |
| 661 | - } | |
| 662 | - | |
| 663 | - $license = ''; | |
| 664 | - if ( isset( $settings['license'] ) && ! empty( $settings['license'] ) ) { | |
| 665 | - $license = $settings['license']; | |
| 666 | - if ( is_array( $license ) ) { | |
| 667 | - $license = $settings['license']['text']; | |
| 668 | - } | |
| 669 | - } | |
| 670 | - $license = apply_filters( 'visualizer_schema_license', $license, $id ); | |
| 671 | - if ( empty( $license ) ) { | |
| 672 | - if ( $show_errors ) { | |
| 673 | - return "<!-- Not showing structured data for chart $id because license is empty -->"; | |
| 674 | - } | |
| 675 | - return ''; | |
| 676 | - } | |
| 677 | - | |
| 678 | - $creator = ''; | |
| 679 | - if ( isset( $settings['creator'] ) && ! empty( $settings['creator'] ) ) { | |
| 680 | - $creator = $settings['creator']; | |
| 681 | - if ( is_array( $creator ) ) { | |
| 682 | - $creator = $settings['creator']['text']; | |
| 683 | - } | |
| 684 | - } | |
| 685 | - $creator = apply_filters( 'visualizer_schema_creator', $creator, $id ); | |
| 686 | - if ( empty( $creator ) ) { | |
| 687 | - if ( $show_errors ) { | |
| 688 | - return "<!-- Not showing structured data for chart $id because creator is empty -->"; | |
| 689 | - } | |
| 690 | - return ''; | |
| 691 | - } | |
| 692 | - | |
| 693 | - $schema = apply_filters( | |
| 694 | - 'visualizer_schema', | |
| 695 | - '{ | |
| 696 | - "@context":"https://schema.org/", | |
| 697 | - "@type":"Dataset", | |
| 698 | - "name":"' . esc_html( $title ) . '", | |
| 699 | - "description":"' . esc_html( $desc ) . '", | |
| 700 | - "license": "' . esc_html( $license ) . '", | |
| 701 | - "creator": { | |
| 702 | - "@type": "Person", | |
| 703 | - "name": "' . esc_html( $creator ) . '" | |
| 704 | - } | |
| 705 | - }', | |
| 706 | - $id | |
| 707 | - ); | |
| 708 | - | |
| 709 | - if ( empty( $schema ) ) { | |
| 710 | - return ''; | |
| 711 | - } | |
| 712 | - | |
| 713 | - return '<script type="application/ld+json">' . $schema . '</script>'; | |
| 714 | - } | |
| 715 | - | |
| 716 | - /** | |
| 717 | - * Get chart by ID. | |
| 718 | - * | |
| 719 | - * @param string $cache_key Cache key. | |
| 720 | - * @param int $chart_id Chart ID. | |
| 721 | - * @return mixed | |
| 722 | - */ | |
| 723 | - private function getChartData( $cache_key = '', $chart_id = 0 ) { | |
| 724 | - if ( ! $chart_id ) { | |
| 725 | - return false; | |
| 726 | - } | |
| 727 | - // Create unique cache key of each chart. | |
| 728 | - $cache_key .= '_' . $chart_id; | |
| 729 | - // Get chart from cache. | |
| 730 | - $chart = get_transient( $cache_key ); | |
| 731 | - if ( $chart ) { | |
| 732 | - return $chart; | |
| 733 | - } | |
| 734 | - | |
| 735 | - // Get chart by ID. | |
| 736 | - $chart = get_post( $chart_id ); | |
| 737 | - if ( $chart && Visualizer_Plugin::CPT_VISUALIZER === $chart->post_type ) { | |
| 738 | - $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 739 | - $series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ); | |
| 740 | - $is_woocommerce_report = get_post_meta( $chart->ID, Visualizer_Plugin::CF_IS_WOOCOMMERCE_SOURCE, true ); | |
| 741 | - | |
| 742 | - if ( isset( $settings['series'] ) && ! ( count( $settings['series'] ) - count( $series ) > 1 ) ) { | |
| 743 | - $diff_total_series = abs( count( $settings['series'] ) - count( $series ) ); | |
| 744 | - if ( $diff_total_series ) { | |
| 745 | - foreach ( range( 1, $diff_total_series ) as $k => $diff_series ) { | |
| 746 | - $settings['series'][] = end( $settings['series'] ); | |
| 747 | - } | |
| 748 | - } | |
| 749 | - } | |
| 750 | - $chart_data = array( | |
| 751 | - 'chart' => $chart, | |
| 752 | - 'type' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ), | |
| 753 | - 'settings' => $settings, | |
| 754 | - 'series' => $series, | |
| 755 | - 'chart_image' => get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_IMAGE, true ), | |
| 756 | - 'is_woocommerce_report' => $is_woocommerce_report, | |
| 757 | - ); | |
| 758 | - | |
| 759 | - // Put the results in a transient. Expire after 12 hours. | |
| 760 | - set_transient( $cache_key, $chart_data, apply_filters( Visualizer_Plugin::FILTER_HANDLE_CACHE_EXPIRATION_TIME, 12 * HOUR_IN_SECONDS ) ); | |
| 761 | - return $chart_data; | |
| 762 | - } | |
| 763 | - | |
| 764 | - return false; | |
| 765 | - } | |
| 766 | - | |
| 767 | - /** | |
| 768 | - * Print footer script. | |
| 769 | - */ | |
| 770 | - public function printFooterScripts() { | |
| 771 | - if ( $this->lazy_render_script ) { | |
| 772 | - ?> | |
| 773 | - <script type="text/javascript"> | |
| 774 | - var visualizerUserInteractionEvents = [ | |
| 775 | - "scroll", | |
| 776 | - "mouseover", | |
| 777 | - "keydown", | |
| 778 | - "touchmove", | |
| 779 | - "touchstart" | |
| 780 | - ]; | |
| 781 | - | |
| 782 | - visualizerUserInteractionEvents.forEach(function(event) { | |
| 783 | - window.addEventListener(event, visualizerTriggerScriptLoader, { passive: true }); | |
| 784 | - }); | |
| 785 | - | |
| 786 | - function visualizerTriggerScriptLoader() { | |
| 787 | - visualizerLoadScripts(); | |
| 788 | - visualizerUserInteractionEvents.forEach(function(event) { | |
| 789 | - window.removeEventListener(event, visualizerTriggerScriptLoader, { passive: true }); | |
| 790 | - }); | |
| 791 | - } | |
| 792 | - | |
| 793 | - function visualizerLoadScripts() { | |
| 794 | - document.querySelectorAll("script[data-visualizer-script]").forEach(function(elem) { | |
| 795 | - jQuery.getScript( elem.getAttribute("data-visualizer-script") ) | |
| 796 | - .done( function( script, textStatus ) { | |
| 797 | - elem.setAttribute("src", elem.getAttribute("data-visualizer-script")); | |
| 798 | - elem.removeAttribute("data-visualizer-script"); | |
| 799 | - setTimeout( function() { | |
| 800 | - visualizerRefreshChart(); | |
| 801 | - } ); | |
| 802 | - } ); | |
| 803 | - }); | |
| 804 | - } | |
| 805 | - | |
| 806 | - function visualizerRefreshChart() { | |
| 807 | - jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize(); | |
| 808 | - if ( jQuery( 'div.viz-facade-loaded:not(.visualizer-lazy):empty' ).length > 0 ) { | |
| 809 | - visualizerUserInteractionEvents.forEach( function( event ) { | |
| 810 | - window.addEventListener( event, function() { | |
| 811 | - jQuery( '.visualizer-front:not(.visualizer-chart-loaded)' ).resize(); | |
| 812 | - }, { passive: true } ); | |
| 813 | - } ); | |
| 814 | - } | |
| 815 | - } | |
| 816 | - </script> | |
| 817 | - <?php | |
| 818 | - } | |
| 344 | + return $actions_div . '<div id="' . $id . '"' . $class . '></div>'; | |
| 819 | 345 | } |
| 820 | 346 | } |