| @@ -18,8 +18,9 @@ | ||
| 18 | 18 | // | MA 02110-1301 USA | |
| 19 | 19 | // +----------------------------------------------------------------------+ |
| 20 | 20 | // | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 | 21 | // +----------------------------------------------------------------------+ |
| 22 | + | |
| 22 | 23 | /** |
| 23 | 24 | * Frontend module class. |
| 24 | 25 | * |
| 25 | 26 | * @category Visualizer |
| @@ -56,89 +57,19 @@ | ||
| 56 | 57 | $this->_addAction( 'wp_enqueue_scripts', 'enqueueScripts' ); |
| 57 | 58 | $this->_addShortcode( 'visualizer', 'renderChart' ); |
| 58 | 59 | |
| 59 | 60 | // add do_shortocde hook for widget_text filter |
| 60 | - if ( ! has_filter( 'widget_text', 'do_shortcode' ) ) { | |
| 61 | + if ( !has_filter( 'widget_text', 'do_shortcode' ) ) { | |
| 61 | 62 | add_filter( 'widget_text', 'do_shortcode' ); |
| 62 | 63 | } |
| 63 | 64 | |
| 64 | 65 | // add do_shortcode hook for term_description filter |
| 65 | - if ( ! has_filter( 'term_description', 'do_shortcode' ) ) { | |
| 66 | + if ( !has_filter( 'term_description', 'do_shortcode' ) ) { | |
| 66 | 67 | add_filter( 'term_description', 'do_shortcode' ); |
| 67 | 68 | } |
| 68 | - | |
| 69 | - add_action( 'rest_api_init', array( $this, 'endpoint_register' ) ); | |
| 70 | 69 | } |
| 71 | 70 | |
| 72 | 71 | /** |
| 73 | - * Registers the endpoints | |
| 74 | - */ | |
| 75 | - function endpoint_register() { | |
| 76 | - register_rest_route( | |
| 77 | - 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 78 | - '/action/(?P<chart>\d+)/(?P<type>.+)/', | |
| 79 | - array( | |
| 80 | - 'methods' => array( 'GET', 'POST' ), | |
| 81 | - 'callback' => array( $this, 'perform_action' ), | |
| 82 | - ) | |
| 83 | - ); | |
| 84 | - } | |
| 85 | - | |
| 86 | - /** | |
| 87 | - * All possible actions and their labels | |
| 88 | - * | |
| 89 | - * @access private | |
| 90 | - */ | |
| 91 | - private function get_actions() { | |
| 92 | - return apply_filters( | |
| 93 | - 'visualizer_action_buttons', array( | |
| 94 | - 'print' => __( 'Print', 'visualizer' ), | |
| 95 | - 'csv' => __( 'CSV', 'visualizer' ), | |
| 96 | - 'xls' => __( 'Excel', 'visualizer' ), | |
| 97 | - 'copy' => __( 'Copy', 'visualizer' ), | |
| 98 | - ) | |
| 99 | - ); | |
| 100 | - } | |
| 101 | - | |
| 102 | - /** | |
| 103 | - * The print button | |
| 104 | - * | |
| 105 | - * @since 1.0.0 | |
| 106 | - * | |
| 107 | - * @access public | |
| 108 | - */ | |
| 109 | - public function perform_action( WP_REST_Request $params ) { | |
| 110 | - $chart_id = filter_var( sanitize_text_field( $params['chart'] ), FILTER_VALIDATE_INT ); | |
| 111 | - $type = sanitize_text_field( $params['type'] ); | |
| 112 | - $data = null; | |
| 113 | - | |
| 114 | - if ( ! $chart_id ) { | |
| 115 | - return new WP_REST_Response( array( 'error' => new WP_Error( __( 'Invalid chart ID', 'visualizer' ) ) ) ); | |
| 116 | - } | |
| 117 | - | |
| 118 | - if ( ! $type ) { | |
| 119 | - return new WP_REST_Response( array( 'error' => new WP_Error( __( 'Invalid action', 'visualizer' ) ) ) ); | |
| 120 | - } | |
| 121 | - | |
| 122 | - switch ( $type ) { | |
| 123 | - case 'print': | |
| 124 | - $data = $this->_getDataAs( $chart_id, 'print' ); | |
| 125 | - break; | |
| 126 | - case 'csv': | |
| 127 | - $data = $this->_getDataAs( $chart_id, 'csv' ); | |
| 128 | - break; | |
| 129 | - case 'xls': | |
| 130 | - $data = $this->_getDataAs( $chart_id, 'xls' ); | |
| 131 | - break; | |
| 132 | - default: | |
| 133 | - $data = apply_filters( 'visualizer_action_data', $data, $chart_id, $type, $params, $this ); | |
| 134 | - break; | |
| 135 | - } | |
| 136 | - | |
| 137 | - return new WP_REST_Response( array( 'data' => $data ) ); | |
| 138 | - } | |
| 139 | - | |
| 140 | - /** | |
| 141 | 72 | * Registers scripts for charts rendering. |
| 142 | 73 | * |
| 143 | 74 | * @since 1.0.0 |
| 144 | 75 | * @uses wp_register_script To register javascript file. |
| @@ -145,15 +76,10 @@ | ||
| 145 | 76 | * |
| 146 | 77 | * @access public |
| 147 | 78 | */ |
| 148 | 79 | 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 ); | |
| 152 | - 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 | - wp_register_style( 'visualizer-front', VISUALIZER_ABSURL . 'css/front.css', array(), Visualizer_Plugin::VERSION ); | |
| 155 | - do_action( 'visualizer_pro_frontend_load_resources' ); | |
| 80 | + wp_register_script( 'visualizer-google-jsapi', '//www.google.com/jsapi', array(), null, true ); | |
| 81 | + wp_register_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array( 'visualizer-google-jsapi', 'jquery' ), Visualizer_Plugin::VERSION, true ); | |
| 156 | 82 | } |
| 157 | 83 | |
| 158 | 84 | /** |
| 159 | 85 | * Returns placeholder for chart according to visualizer shortcode attributes. |
| @@ -168,33 +94,23 @@ | ||
| 168 | 94 | * @access public |
| 169 | 95 | * @param array $atts The array of shortcode attributes. |
| 170 | 96 | */ |
| 171 | 97 | public function renderChart( $atts ) { |
| 172 | - global $wp_version; | |
| 173 | - $atts = shortcode_atts( | |
| 174 | - array( | |
| 175 | - 'id' => false, // chart id | |
| 176 | - 'class' => false, // chart class | |
| 177 | - 'series' => false, // series filter hook | |
| 178 | - 'data' => false, // data filter hook | |
| 179 | - 'settings' => false, // data filter hook | |
| 180 | - ), $atts | |
| 181 | - ); | |
| 98 | + $atts = shortcode_atts( array( | |
| 99 | + 'id' => false, // chart id | |
| 100 | + 'class' => false, // chart class | |
| 101 | + 'series' => false, // series filter hook | |
| 102 | + 'data' => false, // data filter hook | |
| 103 | + ), $atts ); | |
| 182 | 104 | |
| 183 | 105 | // 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 ) { | |
| 106 | + if ( !$atts['id'] || !( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 185 | 107 | return ''; |
| 186 | 108 | } |
| 187 | 109 | |
| 188 | - if ( ! apply_filters( 'visualizer_pro_show_chart', true, $atts['id'] ) ) { | |
| 189 | - return ''; | |
| 190 | - } | |
| 191 | - | |
| 192 | 110 | $id = 'visualizer-' . $atts['id']; |
| 193 | - $defaultClass = 'visualizer-front'; | |
| 194 | 111 | $class = apply_filters( Visualizer_Plugin::FILTER_CHART_WRAPPER_CLASS, $atts['class'], $atts['id'] ); |
| 195 | - $class = $defaultClass . ' ' . $class . ' ' . 'visualizer-front-' . $atts['id']; | |
| 196 | - $class = ! empty( $class ) ? ' class="' . trim( $class ) . '"' : ''; | |
| 112 | + $class = !empty( $class ) ? ' class="' . $class . '"' : ''; | |
| 197 | 113 | |
| 198 | 114 | $type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 199 | 115 | |
| 200 | 116 | // faetch and update settings |
| @@ -204,31 +120,20 @@ | ||
| 204 | 120 | } |
| 205 | 121 | |
| 206 | 122 | // handle series filter hooks |
| 207 | 123 | $series = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ), $chart->ID, $type ); |
| 208 | - if ( ! empty( $atts['series'] ) ) { | |
| 124 | + if ( !empty( $atts['series'] ) ) { | |
| 209 | 125 | $series = apply_filters( $atts['series'], $series, $chart->ID, $type ); |
| 210 | 126 | } |
| 211 | - // handle settings filter hooks | |
| 212 | - $settings = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $settings, $chart->ID, $type ); | |
| 213 | - if ( ! empty( $atts['settings'] ) ) { | |
| 214 | - $settings = apply_filters( $atts['settings'], $settings, $chart->ID, $type ); | |
| 215 | - } | |
| 216 | 127 | |
| 217 | 128 | // handle data filter hooks |
| 218 | 129 | $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( $chart->post_content ), $chart->ID, $type ); |
| 219 | - if ( ! empty( $atts['data'] ) ) { | |
| 130 | + if ( !empty( $atts['data'] ) ) { | |
| 220 | 131 | $data = apply_filters( $atts['data'], $data, $chart->ID, $type ); |
| 221 | 132 | } |
| 222 | 133 | |
| 223 | - $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 | - | |
| 229 | 134 | // add chart to the array |
| 230 | - $this->_charts[ $id ] = array( | |
| 135 | + $this->_charts[$id] = array( | |
| 231 | 136 | 'type' => $type, |
| 232 | 137 | 'series' => $series, |
| 233 | 138 | 'settings' => $settings, |
| 234 | 139 | 'data' => $data, |
| @@ -235,50 +140,11 @@ | ||
| 235 | 140 | ); |
| 236 | 141 | |
| 237 | 142 | // enqueue visualizer render and update render localizations |
| 238 | 143 | wp_enqueue_script( 'visualizer-render' ); |
| 239 | - wp_localize_script( | |
| 240 | - 'visualizer-render', 'visualizer', array( | |
| 241 | - 'charts' => $this->_charts, | |
| 242 | - 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 243 | - 'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '', | |
| 244 | - 'i10n' => array( | |
| 245 | - 'copied' => __( 'Copied!', 'visualizer' ), | |
| 246 | - ), | |
| 247 | - ) | |
| 248 | - ); | |
| 249 | - wp_enqueue_style( 'visualizer-front' ); | |
| 144 | + wp_localize_script( 'visualizer-render', 'visualizer', array( 'charts' => $this->_charts ) ); | |
| 250 | 145 | |
| 251 | - $actions_div = ''; | |
| 252 | - $actions_visible = apply_filters( 'visualizer_pro_add_actions', isset( $settings['actions'] ) ? $settings['actions'] : array(), $atts['id'] ); | |
| 253 | - if ( ! empty( $actions_visible ) ) { | |
| 254 | - $actions = $this->get_actions(); | |
| 255 | - $actions_div = '<div class="visualizer-actions">'; | |
| 256 | - foreach ( $actions_visible as $action_type ) { | |
| 257 | - $key = $action_type; | |
| 258 | - $mime = ''; | |
| 259 | - if ( strpos( $action_type, ';' ) !== false ) { | |
| 260 | - $array = explode( ';', $action_type ); | |
| 261 | - $key = $array[0]; | |
| 262 | - $mime = end( $array ); | |
| 263 | - } | |
| 264 | - $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 . '" '; | |
| 266 | - | |
| 267 | - if ( 'copy' === $key ) { | |
| 268 | - $copy = $this->_getDataAs( $atts['id'], 'csv' ); | |
| 269 | - $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"'; | |
| 270 | - } | |
| 271 | - | |
| 272 | - $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] ); | |
| 273 | - $actions_div .= '>' . $label . '</a> '; | |
| 274 | - } | |
| 275 | - | |
| 276 | - $actions_div .= '</div>'; | |
| 277 | - } | |
| 278 | - | |
| 279 | - $actions_div .= $css; | |
| 280 | - | |
| 281 | 146 | // return placeholder div |
| 282 | - return $actions_div . '<div id="' . $id . '"' . $class . '></div>'; | |
| 147 | + return '<div id="' . $id . '"' . $class . '></div>'; | |
| 283 | 148 | } |
| 284 | -} | |
| 149 | + | |
| 150 | +} | |