| @@ -53,8 +53,10 @@ | ||
| 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' ); | |
| 58 | + $this->_addFilter( 'visualizer_get_language', 'getLanguage' ); | |
| 57 | 59 | $this->_addShortcode( 'visualizer', 'renderChart' ); |
| 58 | 60 | |
| 59 | 61 | // add do_shortocde hook for widget_text filter |
| 60 | 62 | if ( ! has_filter( 'widget_text', 'do_shortcode' ) ) { |
| @@ -66,11 +68,40 @@ | ||
| 66 | 68 | add_filter( 'term_description', 'do_shortcode' ); |
| 67 | 69 | } |
| 68 | 70 | |
| 69 | 71 | add_action( 'rest_api_init', array( $this, 'endpoint_register' ) ); |
| 72 | + | |
| 73 | + $this->_addFilter( 'script_loader_tag', 'script_loader_tag', null, 10, 3 ); | |
| 70 | 74 | } |
| 71 | 75 | |
| 72 | 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 | + /** | |
| 96 | + * Returns the language/locale. | |
| 97 | + */ | |
| 98 | + function getLanguage( $dummy, $only_language ) { | |
| 99 | + return $this->get_language(); | |
| 100 | + } | |
| 101 | + | |
| 102 | + | |
| 103 | + /** | |
| 73 | 104 | * Registers the endpoints |
| 74 | 105 | */ |
| 75 | 106 | function endpoint_register() { |
| 76 | 107 | register_rest_route( |
| @@ -89,9 +120,10 @@ | ||
| 89 | 120 | * @access private |
| 90 | 121 | */ |
| 91 | 122 | private function get_actions() { |
| 92 | 123 | return apply_filters( |
| 93 | - 'visualizer_action_buttons', array( | |
| 124 | + 'visualizer_action_buttons', | |
| 125 | + array( | |
| 94 | 126 | 'print' => __( 'Print', 'visualizer' ), |
| 95 | 127 | 'csv' => __( 'CSV', 'visualizer' ), |
| 96 | 128 | 'xls' => __( 'Excel', 'visualizer' ), |
| 97 | 129 | 'copy' => __( 'Copy', 'visualizer' ), |
| @@ -145,13 +177,10 @@ | ||
| 145 | 177 | * |
| 146 | 178 | * @access public |
| 147 | 179 | */ |
| 148 | 180 | public function enqueueScripts() { |
| 149 | - wp_register_script( 'visualizer-google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true ); | |
| 150 | - wp_register_script( 'visualizer-google-jsapi-old', '//www.google.com/jsapi', array( 'visualizer-google-jsapi-new' ), null, true ); | |
| 151 | - 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 ); | |
| 152 | 182 | wp_register_script( 'visualizer-clipboardjs', VISUALIZER_ABSURL . 'js/lib/clipboardjs/clipboard.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true ); |
| 153 | - wp_enqueue_script( 'visualizer-clipboardjs' ); | |
| 154 | 183 | wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION ); |
| 155 | 184 | do_action( 'visualizer_pro_frontend_load_resources' ); |
| 156 | 185 | } |
| 157 | 186 | |
| @@ -176,13 +205,14 @@ | ||
| 176 | 205 | 'class' => false, // chart class |
| 177 | 206 | 'series' => false, // series filter hook |
| 178 | 207 | 'data' => false, // data filter hook |
| 179 | 208 | 'settings' => false, // data filter hook |
| 180 | - ), $atts | |
| 209 | + ), | |
| 210 | + $atts | |
| 181 | 211 | ); |
| 182 | 212 | |
| 183 | 213 | // if empty id or chart does not exists, then return empty string |
| 184 | - 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 ) { | |
| 185 | 215 | return ''; |
| 186 | 216 | } |
| 187 | 217 | |
| 188 | 218 | if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) { |
| @@ -188,8 +218,14 @@ | ||
| 188 | 218 | if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) { |
| 189 | 219 | return ''; |
| 190 | 220 | } |
| 191 | 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 | + | |
| 192 | 228 | $id = 'visualizer-' . $atts['id']; |
| 193 | 229 | $defaultClass = 'visualizer-front'; |
| 194 | 230 | $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] ); |
| 195 | 231 | $class = $defaultClass . ' ' . $class . ' ' . 'visualizer-front-' . $atts['id']; |
| @@ -196,9 +232,11 @@ | ||
| 196 | 232 | $class = ! empty( $class ) ? ' class="' . trim( $class ) . '"' : ''; |
| 197 | 233 | |
| 198 | 234 | $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 199 | 235 | |
| 200 | - // faetch and update settings | |
| 236 | + $chart = apply_filters( 'visualizer_schedule_refresh_chart', $chart, $chart->ID, false ); | |
| 237 | + | |
| 238 | + // fetch and update settings | |
| 201 | 239 | $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); |
| 202 | 240 | if ( empty( $settings['height'] ) ) { |
| 203 | 241 | $settings['height'] = '400'; |
| 204 | 242 | } |
| @@ -214,19 +252,29 @@ | ||
| 214 | 252 | $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type ); |
| 215 | 253 | } |
| 216 | 254 | |
| 217 | 255 | // handle data filter hooks |
| 218 | - $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 ); | |
| 219 | 257 | if ( ! empty( $atts['data'] ) ) { |
| 220 | 258 | $data = apply_filters( $atts['data'], $data, $chart->ID, $type ); |
| 221 | 259 | } |
| 222 | 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 | + | |
| 223 | 270 | $id = $id . '-' . rand(); |
| 224 | - $arguments = array( '', $id, $settings ); | |
| 225 | - apply_filters_ref_array( 'visualizer_pro_inline_css', array( &$arguments ) ); | |
| 226 | - $css = $arguments[0]; | |
| 227 | - $settings = $arguments[2]; | |
| 228 | 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 | + | |
| 229 | 277 | // add chart to the array |
| 230 | 278 | $this->_charts[ $id ] = array( |
| 231 | 279 | 'type' => $type, |
| 232 | 280 | 'series' => $series, |
| @@ -231,20 +279,33 @@ | ||
| 231 | 279 | 'type' => $type, |
| 232 | 280 | 'series' => $series, |
| 233 | 281 | 'settings' => $settings, |
| 234 | 282 | 'data' => $data, |
| 283 | + 'library' => $library, | |
| 235 | 284 | ); |
| 236 | 285 | |
| 237 | - // enqueue visualizer render and update render localizations | |
| 238 | - wp_enqueue_script( 'visualizer-render' ); | |
| 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 | + ); | |
| 293 | + | |
| 294 | + wp_enqueue_script( "visualizer-render-$library" ); | |
| 239 | 295 | wp_localize_script( |
| 240 | - 'visualizer-render', 'visualizer', array( | |
| 296 | + "visualizer-render-$library", | |
| 297 | + 'visualizer', | |
| 298 | + array( | |
| 241 | 299 | 'charts' => $this->_charts, |
| 300 | + 'language' => $this->get_language(), | |
| 242 | 301 | 'map_api_key' => get_option( 'visualizer-map-api-key' ), |
| 243 | 302 | 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', |
| 244 | 303 | 'i10n' => array( |
| 245 | 304 | 'copied' => __( 'Copied!', 'visualizer' ), |
| 246 | 305 | ), |
| 306 | + 'page_type' => 'frontend', | |
| 307 | + 'is_front' => true, | |
| 247 | 308 | ) |
| 248 | 309 | ); |
| 249 | 310 | wp_enqueue_style( 'visualizer-front' ); |
| 250 | 311 | |
| @@ -261,13 +322,14 @@ | ||
| 261 | 322 | $key = $array[0]; |
| 262 | 323 | $mime = end( $array ); |
| 263 | 324 | } |
| 264 | 325 | $label = $actions[ $key ]; |
| 265 | - $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 . '" '; | |
| 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 . '" '; | |
| 266 | 327 | |
| 267 | 328 | if ( 'copy' === $key ) { |
| 268 | 329 | $copy = $this->_getDataAs( $atts['id'], 'csv' ); |
| 269 | 330 | $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"'; |
| 331 | + wp_enqueue_script( 'visualizer-clipboardjs' ); | |
| 270 | 332 | } |
| 271 | 333 | |
| 272 | 334 | $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] ); |
| 273 | 335 | $actions_div .= '>' . $label . '</a> '; |