| @@ -64,11 +64,81 @@ | ||
| 64 | 64 | // add do_shortcode hook for term_description filter |
| 65 | 65 | if ( ! has_filter( 'term_description', 'do_shortcode' ) ) { |
| 66 | 66 | add_filter( 'term_description', 'do_shortcode' ); |
| 67 | 67 | } |
| 68 | + | |
| 69 | + add_action( 'rest_api_init', array( $this, 'endpoint_register' ) ); | |
| 68 | 70 | } |
| 69 | 71 | |
| 70 | 72 | /** |
| 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' => 'GET', | |
| 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 ); | |
| 134 | + break; | |
| 135 | + } | |
| 136 | + | |
| 137 | + return new WP_REST_Response( array( 'data' => $data ) ); | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 71 | 141 | * Registers scripts for charts rendering. |
| 72 | 142 | * |
| 73 | 143 | * @since 1.0.0 |
| 74 | 144 | * @uses wp_register_script To register javascript file. |
| @@ -78,8 +148,10 @@ | ||
| 78 | 148 | public function enqueueScripts() { |
| 79 | 149 | wp_register_script( 'visualizer-google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true ); |
| 80 | 150 | wp_register_script( 'visualizer-google-jsapi-old', '//www.google.com/jsapi', array( 'visualizer-google-jsapi-new' ), null, true ); |
| 81 | 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' ); | |
| 82 | 154 | } |
| 83 | 155 | |
| 84 | 156 | /** |
| 85 | 157 | * Returns placeholder for chart according to visualizer shortcode attributes. |
| @@ -94,15 +166,17 @@ | ||
| 94 | 166 | * @access public |
| 95 | 167 | * @param array $atts The array of shortcode attributes. |
| 96 | 168 | */ |
| 97 | 169 | public function renderChart( $atts ) { |
| 98 | - $atts = shortcode_atts( array( | |
| 99 | - 'id' => false, // chart id | |
| 170 | + $atts = shortcode_atts( | |
| 171 | + array( | |
| 172 | + 'id' => false, // chart id | |
| 100 | 173 | 'class' => false, // chart class |
| 101 | 174 | 'series' => false, // series filter hook |
| 102 | 175 | 'data' => false, // data filter hook |
| 103 | 176 | 'settings' => false, // data filter hook |
| 104 | - ), $atts ); | |
| 177 | + ), $atts | |
| 178 | + ); | |
| 105 | 179 | |
| 106 | 180 | // if empty id or chart does not exists, then return empty string |
| 107 | 181 | if ( ! $atts['id'] || ! ( $chart = get_post( $atts['id'] ) ) || $chart->post_type != Visualizer_Plugin::CPT_VISUALIZER ) { |
| 108 | 182 | return ''; |
| @@ -150,14 +224,46 @@ | ||
| 150 | 224 | ); |
| 151 | 225 | |
| 152 | 226 | // enqueue visualizer render and update render localizations |
| 153 | 227 | wp_enqueue_script( 'visualizer-render' ); |
| 154 | - wp_localize_script( 'visualizer-render', 'visualizer', array( | |
| 155 | - 'charts' => $this->_charts, | |
| 156 | - 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 157 | - ) ); | |
| 228 | + wp_localize_script( | |
| 229 | + 'visualizer-render', 'visualizer', array( | |
| 230 | + 'charts' => $this->_charts, | |
| 231 | + 'map_api_key' => get_option( 'visualizer-map-api-key' ), | |
| 232 | + 'rest_url' => rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ), | |
| 233 | + 'i10n' => array( | |
| 234 | + 'copied' => __( 'Copied!', 'visualizer' ), | |
| 235 | + ), | |
| 236 | + ) | |
| 237 | + ); | |
| 158 | 238 | |
| 239 | + $actions_div = ''; | |
| 240 | + if ( ! empty( $settings['actions'] ) ) { | |
| 241 | + $actions = $this->get_actions(); | |
| 242 | + $actions_div = '<div class="visualizer-actions">'; | |
| 243 | + foreach ( $settings['actions'] as $action_type ) { | |
| 244 | + $key = $action_type; | |
| 245 | + $mime = ''; | |
| 246 | + if ( strpos( $action_type, ';' ) !== false ) { | |
| 247 | + $array = explode( ';', $action_type ); | |
| 248 | + $key = $array[0]; | |
| 249 | + $mime = end( $array ); | |
| 250 | + } | |
| 251 | + $label = $actions[ $key ]; | |
| 252 | + $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 . '" '; | |
| 253 | + | |
| 254 | + if ( 'copy' === $key ) { | |
| 255 | + $copy = $this->_getDataAs( $atts['id'], 'csv' ); | |
| 256 | + $actions_div .= ' data-clipboard-text="' . esc_attr( $copy['csv'] ) . '"'; | |
| 257 | + } | |
| 258 | + | |
| 259 | + $actions_div .= apply_filters( 'visualizer_action_attributes', '', $key, $atts['id'] ); | |
| 260 | + $actions_div .= '>' . $label . '</a> '; | |
| 261 | + } | |
| 262 | + | |
| 263 | + $actions_div .= '</div>'; | |
| 264 | + } | |
| 159 | 265 | // return placeholder div |
| 160 | - return '<div id="' . $id . '"' . $class . '></div>'; | |
| 266 | + return $actions_div . '<div id="' . $id . '"' . $class . '></div>'; | |
| 161 | 267 | } |
| 162 | 268 | |
| 163 | 269 | } |