| @@ -53,8 +53,9 @@ | ||
| 53 | 53 | public function __construct( Visualizer_Plugin $plugin ) { |
| 54 | 54 | parent::__construct( $plugin ); |
| 55 | 55 | |
| 56 | 56 | $this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' ); |
| 57 | + $this->_addAction( 'visualizer_enqueue_scripts', 'enqueueScripts' ); | |
| 57 | 58 | $this->_addFilter( 'visualizer_get_language', 'getLanguage' ); |
| 58 | 59 | $this->_addShortcode( 'visualizer', 'renderChart' ); |
| 59 | 60 | |
| 60 | 61 | // add do_shortocde hook for widget_text filter |
| @@ -67,11 +68,32 @@ | ||
| 67 | 68 | add_filter( 'term_description', 'do_shortcode' ); |
| 68 | 69 | } |
| 69 | 70 | |
| 70 | 71 | add_action( 'rest_api_init', array( $this, 'endpoint_register' ) ); |
| 72 | + | |
| 73 | + $this->_addFilter( 'script_loader_tag', 'script_loader_tag', null, 10, 3 ); | |
| 71 | 74 | } |
| 72 | 75 | |
| 73 | 76 | /** |
| 77 | + * Adds the async attribute to certain scripts. | |
| 78 | + */ | |
| 79 | + function script_loader_tag( $tag, $handle, $src ) { | |
| 80 | + if ( is_admin() ) { | |
| 81 | + return $tag; | |
| 82 | + } | |
| 83 | + | |
| 84 | + $scripts = array( 'google-jsapi-new', 'google-jsapi-old', 'visualizer-render-google-lib', 'visualizer-render-google' ); | |
| 85 | + | |
| 86 | + foreach ( $scripts as $async ) { | |
| 87 | + if ( $async === $handle ) { | |
| 88 | + $tag = str_replace( ' src', ' defer="defer" src', $tag ); | |
| 89 | + break; | |
| 90 | + } | |
| 91 | + } | |
| 92 | + return $tag; | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 74 | 96 | * Returns the language/locale. |
| 75 | 97 | */ |
| 76 | 98 | function getLanguage( $dummy, $only_language ) { |
| 77 | 99 | return $this->get_language(); |
| @@ -98,9 +120,10 @@ | ||
| 98 | 120 | * @access private |
| 99 | 121 | */ |
| 100 | 122 | private function get_actions() { |
| 101 | 123 | return apply_filters( |
| 102 | - 'visualizer_action_buttons', array( | |
| 124 | + 'visualizer_action_buttons', | |
| 125 | + array( | |
| 103 | 126 | 'print' => __( 'Print', 'visualizer' ), |
| 104 | 127 | 'csv' => __( 'CSV', 'visualizer' ), |
| 105 | 128 | 'xls' => __( 'Excel', 'visualizer' ), |
| 106 | 129 | 'copy' => __( 'Copy', 'visualizer' ), |
| @@ -154,11 +177,9 @@ | ||
| 154 | 177 | * |
| 155 | 178 | * @access public |
| 156 | 179 | */ |
| 157 | 180 | public function enqueueScripts() { |
| 158 | - wp_register_script( 'visualizer-google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true ); | |
| 159 | - wp_register_script( 'visualizer-google-jsapi-old', '//www.google.com/jsapi', array( 'visualizer-google-jsapi-new' ), null, true ); | |
| 160 | - wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi-old', 'jquery' ), Visualizer_Plugin::VERSION, true ); | |
| 181 | + wp_register_script( 'visualizer-customization', $this->get_user_customization_js(), array(), null, true ); | |
| 161 | 182 | wp_register_script( 'visualizer-clipboardjs', VISUALIZER_ABSURL . 'js/lib/clipboardjs/clipboard.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true ); |
| 162 | 183 | wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION ); |
| 163 | 184 | do_action( 'visualizer_pro_frontend_load_resources' ); |
| 164 | 185 | } |
| @@ -184,13 +205,14 @@ | ||
| 184 | 205 | 'class' => false, // chart class |
| 185 | 206 | 'series' => false, // series filter hook |
| 186 | 207 | 'data' => false, // data filter hook |
| 187 | 208 | 'settings' => false, // data filter hook |
| 188 | - ), $atts | |
| 209 | + ), | |
| 210 | + $atts | |
| 189 | 211 | ); |
| 190 | 212 | |
| 191 | 213 | // if empty id or chart does not exists, then return empty string |
| 192 | - if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 214 | + if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 193 | 215 | return ''; |
| 194 | 216 | } |
| 195 | 217 | |
| 196 | 218 | if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) { |
| @@ -196,8 +218,14 @@ | ||
| 196 | 218 | if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) { |
| 197 | 219 | return ''; |
| 198 | 220 | } |
| 199 | 221 | |
| 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 ); | |
| 226 | + } | |
| 227 | + | |
| 200 | 228 | $id = 'visualizer-' . $atts['id']; |
| 201 | 229 | $defaultClass = 'visualizer-front'; |
| 202 | 230 | $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] ); |
| 203 | 231 | $class = $defaultClass . ' ' . $class . ' ' . 'visualizer-front-' . $atts['id']; |
| @@ -204,9 +232,11 @@ | ||
| 204 | 232 | $class = ! empty( $class ) ? ' class="' . trim( $class ) . '"' : ''; |
| 205 | 233 | |
| 206 | 234 | $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 207 | 235 | |
| 208 | - // faetch and update settings | |
| 236 | + $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false ); | |
| 237 | + | |
| 238 | + // fetch and update settings | |
| 209 | 239 | $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); |
| 210 | 240 | if ( empty( $settings['height'] ) ) { |
| 211 | 241 | $settings['height'] = '400'; |
| 212 | 242 | } |
| @@ -222,19 +252,29 @@ | ||
| 222 | 252 | $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type ); |
| 223 | 253 | } |
| 224 | 254 | |
| 225 | 255 | // handle data filter hooks |
| 226 | - $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type ); | |
| 256 | + $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( $chart->post_content ) ), $chart->ID, $type ); | |
| 227 | 257 | if ( ! empty( $atts['data'] ) ) { |
| 228 | 258 | $data = apply_filters( $atts['data'], $data, $chart->ID, $type ); |
| 229 | 259 | } |
| 230 | 260 | |
| 261 | + $css = ''; | |
| 262 | + $arguments = $this->get_inline_custom_css( $id, $settings ); | |
| 263 | + if ( ! empty( $arguments ) ) { | |
| 264 | + $css = $arguments[0]; | |
| 265 | + $settings = $arguments[1]; | |
| 266 | + } | |
| 267 | + | |
| 268 | + $library = $this->load_chart_type( $chart->ID ); | |
| 269 | + | |
| 231 | 270 | $id = $id . '-' . rand(); |
| 232 | - $arguments = array( '', $id, $settings ); | |
| 233 | - apply_filters_ref_array( 'visualizer_pro_inline_css', array( &$arguments ) ); | |
| 234 | - $css = $arguments[0]; | |
| 235 | - $settings = $arguments[2]; | |
| 236 | 271 | |
| 272 | + $amp = Visualizer_Plugin::instance()->getModule( Visualizer_Module_AMP::NAME ); | |
| 273 | + if ( $amp && $amp->is_amp() ) { | |
| 274 | + return '<div id="' . $id . '"' . $class . '>' . $amp->get_chart( $chart, $data, $series, $settings ) . '</div>'; | |
| 275 | + } | |
| 276 | + | |
| 237 | 277 | // add chart to the array |
| 238 | 278 | $this->_charts[ $id ] = array( |
| 239 | 279 | 'type' => $type, |
| 240 | 280 | 'series' => $series, |
| @@ -239,25 +279,11 @@ | ||
| 239 | 279 | 'type' => $type, |
| 240 | 280 | 'series' => $series, |
| 241 | 281 | 'settings' => $settings, |
| 242 | 282 | 'data' => $data, |
| 283 | + 'library' => $library, | |
| 243 | 284 | ); |
| 244 | 285 | |
| 245 | - // enqueue visualizer render and update render localizations | |
| 246 | - wp_enqueue_script( 'visualizer-render' ); | |
| 247 | - wp_localize_script( | |
| 248 | - 'visualizer-render', 'visualizer', array( | |
| 249 | - 'charts' => $this->_charts, | |
| 250 | - 'language' => $this->get_language(), | |
| 251 | - 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 252 | - 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', | |
| 253 | - 'i10n' => array( | |
| 254 | - 'copied' => __( 'Copied!', 'visualizer' ), | |
| 255 | - ), | |
| 256 | - ) | |
| 257 | - ); | |
| 258 | - wp_enqueue_style( 'visualizer-front' ); | |
| 259 | - | |
| 260 | 286 | $actions_div = ''; |
| 261 | 287 | $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] ); |
| 262 | 288 | if ( ! empty( $actions_visible ) ) { |
| 263 | 289 | $actions = $this->get_actions(); |
| @@ -270,9 +296,9 @@ | ||
| 270 | 296 | $key = $array[0]; |
| 271 | 297 | $mime = end( $array ); |
| 272 | 298 | } |
| 273 | 299 | $label = $actions[ $key ]; |
| 274 | - $actions_div .= '<a href="#" class="visualizer-action visualizer-action-' . $key . '" data-visualizer-type="' . $key . '" data-visualizer-chart-id="' . $atts['id'] . '" data-visualizer-mime="' . $mime . '" title="' . $label . '" '; | |
| 300 | + $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 . '" '; | |
| 275 | 301 | |
| 276 | 302 | if ( 'copy' === $key ) { |
| 277 | 303 | $copy = $this->_getDataAs( $atts['id'], 'csv' ); |
| 278 | 304 | $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"'; |
| @@ -287,8 +313,86 @@ | ||
| 287 | 313 | } |
| 288 | 314 | |
| 289 | 315 | $actions_div .= $css; |
| 290 | 316 | |
| 317 | + foreach ( $this->_charts as $id => $attributes ) { | |
| 318 | + $library = $attributes['library']; | |
| 319 | + wp_register_script( | |
| 320 | + "visualizer-render-$library", | |
| 321 | + VISUALIZER_ABSURL . 'js/render-facade.js', | |
| 322 | + apply_filters( 'visualizer_assets_render', array( 'jquery', 'visualizer-customization' ), true ), | |
| 323 | + Visualizer_Plugin::VERSION, | |
| 324 | + true | |
| 325 | + ); | |
| 326 | + | |
| 327 | + wp_enqueue_script( "visualizer-render-$library" ); | |
| 328 | + wp_localize_script( | |
| 329 | + "visualizer-render-$library", | |
| 330 | + 'visualizer', | |
| 331 | + array( | |
| 332 | + 'charts' => $this->_charts, | |
| 333 | + 'language' => $this->get_language(), | |
| 334 | + 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 335 | + 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', | |
| 336 | + 'i10n' => array( | |
| 337 | + 'copied' => __( 'Copied!', 'visualizer' ), | |
| 338 | + ), | |
| 339 | + 'page_type' => 'frontend', | |
| 340 | + 'is_front' => true, | |
| 341 | + ) | |
| 342 | + ); | |
| 343 | + wp_enqueue_style( 'visualizer-front' ); | |
| 344 | + } | |
| 345 | + | |
| 291 | 346 | // return placeholder div |
| 292 | - return $actions_div . '<div id="' . $id . '"' . $class . '></div>'; | |
| 347 | + return $actions_div . '<div id="' . $id . '"' . $class . '></div>' . $this->addSchema( $chart->ID ); | |
| 348 | + } | |
| 349 | + | |
| 350 | + /** | |
| 351 | + * Adds the schema corresponding to the dataset. | |
| 352 | + * | |
| 353 | + * @since 3.3.2 | |
| 354 | + * | |
| 355 | + * @access private | |
| 356 | + */ | |
| 357 | + private function addSchema( $id ) { | |
| 358 | + $settings = get_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, true ); | |
| 359 | + $title = ''; | |
| 360 | + if ( isset( $settings['title'] ) && ! empty( $settings['title'] ) ) { | |
| 361 | + $title = $settings['title']; | |
| 362 | + if ( is_array( $title ) ) { | |
| 363 | + $title = $settings['title']['text']; | |
| 364 | + } | |
| 365 | + } | |
| 366 | + $title = apply_filters( 'visualizer_schema_name', $title, $id ); | |
| 367 | + if ( empty( $title ) ) { | |
| 368 | + return ''; | |
| 369 | + } | |
| 370 | + | |
| 371 | + $desc = ''; | |
| 372 | + if ( isset( $settings['description'] ) && ! empty( $settings['description'] ) ) { | |
| 373 | + $desc = $settings['description']; | |
| 374 | + } | |
| 375 | + $desc = apply_filters( 'visualizer_schema_description', $desc, $id ); | |
| 376 | + // descriptions below 50 chars are not allowed. | |
| 377 | + if ( empty( $desc ) || strlen( $desc ) < 50 ) { | |
| 378 | + return ''; | |
| 379 | + } | |
| 380 | + | |
| 381 | + $schema = apply_filters( | |
| 382 | + 'visualizer_schema', | |
| 383 | + '{ | |
| 384 | + "@context":"https://schema.org/", | |
| 385 | + "@type":"Dataset", | |
| 386 | + "name":"' . esc_html( $title ) . '", | |
| 387 | + "description":"' . esc_html( $desc ) . '" | |
| 388 | + }', | |
| 389 | + $id | |
| 390 | + ); | |
| 391 | + | |
| 392 | + if ( empty( $schema ) ) { | |
| 393 | + return ''; | |
| 394 | + } | |
| 395 | + | |
| 396 | + return '<script type="application/ld+json">' . $schema . '</script>'; | |
| 293 | 397 | } |
| 294 | 398 | } |