| @@ -45,9 +45,9 @@ | ||
| 45 | 45 | /** |
| 46 | 46 | * Returns an instance of this class. |
| 47 | 47 | */ |
| 48 | 48 | public static function get_instance() { |
| 49 | - if ( null == self::$instance ) { | |
| 49 | + if ( null === self::$instance ) { | |
| 50 | 50 | self::$instance = new Visualizer_Gutenberg_Block(); |
| 51 | 51 | } |
| 52 | 52 | return self::$instance; |
| 53 | 53 | } |
| @@ -59,60 +59,177 @@ | ||
| 59 | 59 | $this->version = Visualizer_Plugin::VERSION; |
| 60 | 60 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_gutenberg_scripts' ) ); |
| 61 | 61 | add_action( 'init', array( $this, 'register_block_type' ) ); |
| 62 | 62 | add_action( 'rest_api_init', array( $this, 'register_rest_endpoints' ) ); |
| 63 | + add_filter( 'rest_visualizer_query', array( $this, 'add_rest_query_vars' ), 9, 2 ); | |
| 63 | 64 | } |
| 64 | 65 | |
| 65 | 66 | /** |
| 66 | - * Enqueue front end and editor JavaScript and CSS | |
| 67 | + * Enqueue Gutenberg block assets. | |
| 67 | 68 | */ |
| 68 | 69 | public function enqueue_gutenberg_scripts() { |
| 69 | - $blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.js'; | |
| 70 | - $handsontableJS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.js'; | |
| 71 | - $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.css'; | |
| 72 | - $handsontableCSS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.css'; | |
| 70 | + global $pagenow; | |
| 73 | 71 | |
| 72 | + $blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/index.js'; | |
| 73 | + $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/style-index.css'; | |
| 74 | + $asset_path = VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/index.asset.php'; | |
| 75 | + if ( file_exists( $asset_path ) ) { | |
| 76 | + // @phpstan-ignore-next-line | |
| 77 | + $asset = require $asset_path; | |
| 78 | + } else { | |
| 79 | + $asset = array( | |
| 80 | + 'dependencies' => array(), | |
| 81 | + 'version' => $this->version, | |
| 82 | + ); | |
| 83 | + } | |
| 84 | + | |
| 74 | 85 | if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) { |
| 75 | - $version = filemtime( VISUALIZER_ABSPATH . 'classes/Visualizer/Gutenberg/build/block.js' ); | |
| 76 | - } else { | |
| 77 | - $version = $this->version; | |
| 86 | + $asset['version'] = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/index.js' ); | |
| 78 | 87 | } |
| 79 | 88 | |
| 80 | - // Enqueue the bundled block JS file | |
| 81 | - wp_enqueue_script( 'handsontable', $handsontableJS ); | |
| 82 | - wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable' ), $version, true ); | |
| 89 | + if ( ! wp_script_is( 'visualizer-datatables', 'registered' ) ) { | |
| 90 | + wp_register_script( 'visualizer-datatables', VISUALIZER_ABSURL . 'js/lib/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION ); | |
| 91 | + } | |
| 83 | 92 | |
| 84 | - $type = 'community'; | |
| 93 | + if ( ! wp_style_is( 'visualizer-datatables', 'registered' ) ) { | |
| 94 | + wp_register_style( 'visualizer-datatables', VISUALIZER_ABSURL . 'css/lib/datatables.min.css', array(), Visualizer_Plugin::VERSION ); | |
| 95 | + } | |
| 85 | 96 | |
| 86 | - if ( VISUALIZER_PRO ) { | |
| 87 | - $type = 'pro'; | |
| 88 | - if ( apply_filters( 'visualizer_is_business', false ) ) { | |
| 89 | - $type = 'developer'; | |
| 90 | - } | |
| 97 | + // Enqueue the bundled block JS file | |
| 98 | + $script_deps = array( | |
| 99 | + 'wp-api', | |
| 100 | + 'wp-blocks', | |
| 101 | + 'wp-block-editor', | |
| 102 | + 'wp-components', | |
| 103 | + 'wp-editor', | |
| 104 | + 'wp-element', | |
| 105 | + 'wp-i18n', | |
| 106 | + 'lodash', | |
| 107 | + 'moment', | |
| 108 | + 'react', | |
| 109 | + 'visualizer-datatables', | |
| 110 | + ); | |
| 111 | + if ( isset( $asset['dependencies'] ) && is_array( $asset['dependencies'] ) ) { | |
| 112 | + $script_deps = array_merge( $script_deps, $asset['dependencies'] ); | |
| 91 | 113 | } |
| 114 | + $script_deps = array_values( array_unique( $script_deps ) ); | |
| 115 | + wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, $script_deps, $asset['version'], true ); | |
| 92 | 116 | |
| 93 | 117 | $translation_array = array( |
| 94 | - 'isPro' => $type, | |
| 95 | - 'proTeaser' => Visualizer_Plugin::PRO_TEASER_URL, | |
| 96 | - 'absurl' => VISUALIZER_ABSURL, | |
| 118 | + 'adminPage' => menu_page_url( 'visualizer', false ), | |
| 119 | + 'createChart' => add_query_arg( array( 'action' => 'visualizer-create-chart', 'library' => 'yes', 'type' => '', 'chart-library' => '', 'tab' => 'visualizer' ), admin_url( 'admin-ajax.php' ) ), | |
| 120 | + 'chartsPerPage' => defined( 'TI_E2E_TESTING' ) ? 20 : 6, | |
| 121 | + 'isFullSiteEditor' => 'site-editor.php' === $pagenow, | |
| 122 | + /* translators: %1$s: opening tag, %2$s: closing tag */ | |
| 123 | + 'chartEditUrl' => admin_url( 'admin-ajax.php' ), | |
| 97 | 124 | ); |
| 98 | 125 | wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array ); |
| 99 | 126 | |
| 127 | + $d3_renderer_asset = VISUALIZER_ABSPATH . '/classes/Visualizer/D3Renderer/build/index.asset.php'; | |
| 128 | + if ( file_exists( $d3_renderer_asset ) && ! wp_script_is( 'visualizer-d3-renderer', 'registered' ) ) { | |
| 129 | + // @phpstan-ignore-next-line | |
| 130 | + $d3_asset = include $d3_renderer_asset; | |
| 131 | + wp_register_script( | |
| 132 | + 'visualizer-d3-renderer', | |
| 133 | + VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/index.js', | |
| 134 | + array_merge( $d3_asset['dependencies'], array( 'jquery' ) ), | |
| 135 | + $d3_asset['version'], | |
| 136 | + true | |
| 137 | + ); | |
| 138 | + } | |
| 139 | + if ( wp_script_is( 'visualizer-d3-renderer', 'registered' ) ) { | |
| 140 | + wp_enqueue_script( 'visualizer-d3-renderer' ); | |
| 141 | + wp_localize_script( | |
| 142 | + 'visualizer-d3-renderer', | |
| 143 | + 'vizD3Renderer', | |
| 144 | + array( | |
| 145 | + 'iframeJsUrl' => VISUALIZER_ABSURL . 'classes/Visualizer/D3Renderer/build/iframe.js', | |
| 146 | + ) | |
| 147 | + ); | |
| 148 | + } | |
| 149 | + | |
| 100 | 150 | // Enqueue frontend and editor block styles |
| 101 | - wp_enqueue_style( 'handsontable', $handsontableCSS ); | |
| 102 | - wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, '', $version ); | |
| 151 | + wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $asset['version'] ); | |
| 152 | + | |
| 153 | + // Enqueue ChartBuilder (AI Builder) so the D3 edit modal is available in the block editor. | |
| 154 | + $chart_builder_asset = VISUALIZER_ABSPATH . '/classes/Visualizer/ChartBuilder/build/index.asset.php'; | |
| 155 | + if ( file_exists( $chart_builder_asset ) && ! wp_script_is( 'visualizer-chart-builder', 'enqueued' ) ) { | |
| 156 | + /** | |
| 157 | + * Ignore missing build asset in source checkout. | |
| 158 | + * | |
| 159 | + * @phpstan-ignore-next-line | |
| 160 | + */ | |
| 161 | + $cb_asset = include $chart_builder_asset; | |
| 162 | + wp_enqueue_script( | |
| 163 | + 'visualizer-chart-builder', | |
| 164 | + VISUALIZER_ABSURL . 'classes/Visualizer/ChartBuilder/build/index.js', | |
| 165 | + $cb_asset['dependencies'], | |
| 166 | + $cb_asset['version'], | |
| 167 | + true | |
| 168 | + ); | |
| 169 | + wp_enqueue_style( | |
| 170 | + 'visualizer-chart-builder', | |
| 171 | + VISUALIZER_ABSURL . 'classes/Visualizer/ChartBuilder/build/style-index.css', | |
| 172 | + array(), | |
| 173 | + $cb_asset['version'] | |
| 174 | + ); | |
| 175 | + $chart_builder_css = VISUALIZER_ABSPATH . '/classes/Visualizer/ChartBuilder/build/index.css'; | |
| 176 | + if ( file_exists( $chart_builder_css ) ) { | |
| 177 | + wp_enqueue_style( | |
| 178 | + 'visualizer-chart-builder-runtime', | |
| 179 | + VISUALIZER_ABSURL . 'classes/Visualizer/ChartBuilder/build/index.css', | |
| 180 | + array( 'visualizer-chart-builder' ), | |
| 181 | + $cb_asset['version'] | |
| 182 | + ); | |
| 183 | + } | |
| 184 | + wp_localize_script( | |
| 185 | + 'visualizer-chart-builder', | |
| 186 | + 'vizAIBuilder', | |
| 187 | + array( | |
| 188 | + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), | |
| 189 | + 'nonce' => wp_create_nonce( 'visualizer-ai-builder' ), | |
| 190 | + 'isPro' => Visualizer_Module::is_pro(), | |
| 191 | + ) | |
| 192 | + ); | |
| 193 | + // Inject the mount point the ChartBuilder React app needs. | |
| 194 | + wp_add_inline_script( | |
| 195 | + 'visualizer-chart-builder', | |
| 196 | + 'document.body.insertAdjacentHTML("beforeend","<div id=\"viz-chart-builder-root\"></div>");', | |
| 197 | + 'before' | |
| 198 | + ); | |
| 199 | + } | |
| 103 | 200 | } |
| 104 | 201 | /** |
| 105 | 202 | * Hook server side rendering into render callback |
| 106 | 203 | */ |
| 107 | 204 | public function register_block_type() { |
| 205 | + $asset_path = VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/index.asset.php'; | |
| 206 | + $version = $this->version; | |
| 207 | + if ( file_exists( $asset_path ) ) { | |
| 208 | + // @phpstan-ignore-next-line | |
| 209 | + $asset = require $asset_path; | |
| 210 | + $version = isset( $asset['version'] ) ? $asset['version'] : $version; | |
| 211 | + } | |
| 212 | + if ( ! wp_style_is( 'visualizer-datatables', 'registered' ) ) { | |
| 213 | + wp_register_style( 'visualizer-datatables', VISUALIZER_ABSURL . 'css/lib/datatables.min.css', array(), Visualizer_Plugin::VERSION ); | |
| 214 | + } | |
| 215 | + if ( ! wp_style_is( 'visualizer-gutenberg-block', 'registered' ) ) { | |
| 216 | + wp_register_style( 'visualizer-gutenberg-block', VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/style-index.css', array( 'visualizer-datatables' ), $version ); | |
| 217 | + } | |
| 108 | 218 | register_block_type( |
| 109 | 219 | 'visualizer/chart', array( |
| 220 | + // The editor_style registration is what gets the stylesheet into the | |
| 221 | + // iframed editor canvas; styles enqueued via enqueue_block_editor_assets | |
| 222 | + // only reach the parent document. | |
| 223 | + 'editor_style' => 'visualizer-gutenberg-block', | |
| 110 | 224 | 'render_callback' => array( $this, 'gutenberg_block_callback' ), |
| 111 | 225 | 'attributes' => array( |
| 112 | 226 | 'id' => array( |
| 113 | 227 | 'type' => 'number', |
| 114 | 228 | ), |
| 229 | + 'lazy' => array( | |
| 230 | + 'type' => 'string', | |
| 231 | + ), | |
| 115 | 232 | ), |
| 116 | 233 | ) |
| 117 | 234 | ); |
| 118 | 235 | } |
| @@ -119,16 +236,45 @@ | ||
| 119 | 236 | |
| 120 | 237 | /** |
| 121 | 238 | * Gutenberg Block Callback Function |
| 122 | 239 | */ |
| 123 | - public function gutenberg_block_callback( $attr ) { | |
| 124 | - if ( isset( $attr['id'] ) ) { | |
| 125 | - $id = $attr['id']; | |
| 126 | - if ( empty( $id ) || $id === 'none' ) { | |
| 127 | - return ''; // no id = no fun | |
| 128 | - } | |
| 129 | - return '[visualizer id="' . $id . '"]'; | |
| 240 | + public function gutenberg_block_callback( $atts ) { | |
| 241 | + // no id, no fun. | |
| 242 | + if ( ! isset( $atts['id'] ) ) { | |
| 243 | + return ''; | |
| 130 | 244 | } |
| 245 | + | |
| 246 | + $atts = shortcode_atts( | |
| 247 | + array( | |
| 248 | + 'id' => false, | |
| 249 | + 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ), | |
| 250 | + // we are deliberating excluding the class attribute from here | |
| 251 | + // as this will be handled by the custom class in Gutenberg | |
| 252 | + ), | |
| 253 | + $atts | |
| 254 | + ); | |
| 255 | + | |
| 256 | + // no id, no fun. | |
| 257 | + if ( ! $atts['id'] ) { | |
| 258 | + return ''; | |
| 259 | + } | |
| 260 | + | |
| 261 | + if ( $atts['lazy'] === '-1' || $atts['lazy'] === false ) { | |
| 262 | + $atts['lazy'] = 'no'; | |
| 263 | + } | |
| 264 | + | |
| 265 | + // we don't want the chart in the editor lazy-loading. | |
| 266 | + if ( is_admin() ) { | |
| 267 | + unset( $atts['lazy'] ); | |
| 268 | + } | |
| 269 | + | |
| 270 | + $shortcode = '[visualizer'; | |
| 271 | + foreach ( $atts as $name => $value ) { | |
| 272 | + $shortcode .= sprintf( ' %s="%s"', $name, $value ); | |
| 273 | + } | |
| 274 | + $shortcode .= ']'; | |
| 275 | + | |
| 276 | + return $shortcode; | |
| 131 | 277 | } |
| 132 | 278 | |
| 133 | 279 | /** |
| 134 | 280 | * Hook server side rendering into render callback |
| @@ -140,50 +286,8 @@ | ||
| 140 | 286 | array( |
| 141 | 287 | 'get_callback' => array( $this, 'get_visualizer_data' ), |
| 142 | 288 | ) |
| 143 | 289 | ); |
| 144 | - | |
| 145 | - register_rest_route( | |
| 146 | - 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 147 | - '/update-chart', | |
| 148 | - array( | |
| 149 | - 'methods' => 'POST', | |
| 150 | - 'callback' => array( $this, 'update_chart_data' ), | |
| 151 | - 'args' => array( | |
| 152 | - 'id' => array( | |
| 153 | - 'sanitize_callback' => 'absint', | |
| 154 | - ), | |
| 155 | - ), | |
| 156 | - ) | |
| 157 | - ); | |
| 158 | - | |
| 159 | - register_rest_route( | |
| 160 | - 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 161 | - '/upload-data', | |
| 162 | - array( | |
| 163 | - 'methods' => 'POST', | |
| 164 | - 'callback' => array( $this, 'upload_csv_data' ), | |
| 165 | - 'args' => array( | |
| 166 | - 'url' => array( | |
| 167 | - 'sanitize_callback' => 'esc_url_raw', | |
| 168 | - ), | |
| 169 | - ), | |
| 170 | - ) | |
| 171 | - ); | |
| 172 | - | |
| 173 | - register_rest_route( | |
| 174 | - 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 175 | - '/get-permission-data', | |
| 176 | - array( | |
| 177 | - 'methods' => 'GET', | |
| 178 | - 'callback' => array( $this, 'get_permission_data' ), | |
| 179 | - 'args' => array( | |
| 180 | - 'type' => array( | |
| 181 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 182 | - ), | |
| 183 | - ), | |
| 184 | - ) | |
| 185 | - ); | |
| 186 | 290 | } |
| 187 | 291 | |
| 188 | 292 | /** |
| 189 | 293 | * Get Post Meta Fields |
| @@ -188,13 +292,23 @@ | ||
| 188 | 292 | /** |
| 189 | 293 | * Get Post Meta Fields |
| 190 | 294 | */ |
| 191 | 295 | public function get_visualizer_data( $post ) { |
| 296 | + if ( ! Visualizer_Module::can_edit_chart( $post['id'] ) ) { | |
| 297 | + return false; | |
| 298 | + } | |
| 299 | + | |
| 192 | 300 | $data = array(); |
| 193 | 301 | $post_id = $post['id']; |
| 194 | 302 | |
| 195 | 303 | $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 196 | 304 | |
| 305 | + $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); | |
| 306 | + $data['visualizer-chart-library'] = $library; | |
| 307 | + if ( 'd3' === $library ) { | |
| 308 | + $data['visualizer-d3-code'] = get_post_meta( $post_id, Visualizer_Module_AIBuilder::CF_D3_CODE, true ); | |
| 309 | + } | |
| 310 | + | |
| 197 | 311 | $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true ); |
| 198 | 312 | |
| 199 | 313 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| 200 | 314 | |
| @@ -199,9 +313,14 @@ | ||
| 199 | 313 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| 200 | 314 | |
| 201 | 315 | // faetch and update settings |
| 202 | 316 | $data['visualizer-settings'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SETTINGS, true ); |
| 203 | - | |
| 317 | + if ( ! is_array( $data['visualizer-settings'] ) ) { | |
| 318 | + $data['visualizer-settings'] = array(); | |
| 319 | + } | |
| 320 | + if ( empty( $data['visualizer-settings']['pagination'] ) ) { | |
| 321 | + $data['visualizer-settings']['pageSize'] = ''; | |
| 322 | + } | |
| 204 | 323 | // handle series filter hooks |
| 205 | 324 | $data['visualizer-series'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SERIES, get_post_meta( $post_id, Visualizer_Plugin::CF_SERIES, true ), $post_id, $data['visualizer-chart-type'] ); |
| 206 | 325 | |
| 207 | 326 | // handle settings filter hooks |
| @@ -207,89 +326,136 @@ | ||
| 207 | 326 | // handle settings filter hooks |
| 208 | 327 | $data['visualizer-settings'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $data['visualizer-settings'], $post_id, $data['visualizer-chart-type'] ); |
| 209 | 328 | |
| 210 | 329 | // handle data filter hooks |
| 211 | - $data['visualizer-data'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( get_the_content( $post_id ) ) ), $post_id, $data['visualizer-chart-type'] ); | |
| 330 | + $data['visualizer-data'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, Visualizer_Module::decode_content( html_entity_decode( get_post_field( 'post_content', $post_id, 'raw' ) ) ), $post_id, $data['visualizer-chart-type'] ); | |
| 212 | 331 | |
| 213 | - $import = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_URL, true ); | |
| 332 | + // we are going to format only for tabular charts, because we are not sure of the effect on others. | |
| 333 | + // this is to solve the case where boolean data shows up as all-ticks on gutenberg. | |
| 334 | + if ( in_array( $data['visualizer-chart-type'], array( 'tabular' ), true ) ) { | |
| 335 | + $data['visualizer-data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 336 | + } | |
| 214 | 337 | |
| 215 | - $schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true ); | |
| 338 | + if ( ! isset( $data['visualizer-settings']['hAxis']['format'] ) ) { | |
| 339 | + $data['visualizer-settings']['hAxis']['format'] = ''; | |
| 340 | + } | |
| 216 | 341 | |
| 217 | - if ( ! empty( $import ) && ! empty( $schedule ) ) { | |
| 218 | - $data['visualizer-chart-url'] = $import; | |
| 219 | - $data['visualizer-chart-schedule'] = $schedule; | |
| 220 | - } | |
| 342 | + $data['visualizer-data-exploded'] = ''; | |
| 343 | + // handle annotations for google charts | |
| 344 | + if ( 'GoogleCharts' === $library ) { | |
| 345 | + // this will contain the data of both axis. | |
| 346 | + $settings = $data['visualizer-settings']; | |
| 347 | + // this will contain data only of Y axis. | |
| 348 | + $series = $data['visualizer-series']; | |
| 349 | + $annotations = array(); | |
| 350 | + if ( isset( $settings['series'] ) ) { | |
| 351 | + foreach ( $settings['series'] as $index => $serie ) { | |
| 352 | + // skip X axis data. | |
| 353 | + if ( $index === 0 ) { | |
| 354 | + continue; | |
| 355 | + } | |
| 356 | + if ( ! empty( $serie['role'] ) ) { | |
| 357 | + // this series is some kind of annotation, so let's collect its index. | |
| 358 | + // the index will be +1 because the X axis value is index 0, which is being ignored. | |
| 359 | + $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role']; | |
| 221 | 360 | |
| 222 | - if ( VISUALIZER_PRO ) { | |
| 223 | - $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true ); | |
| 361 | + if ( isset( $data['visualizer-series'][ intval( $index ) + 1 ] ) ) { | |
| 362 | + $data['visualizer-series'][ intval( $index ) + 1 ]['role'] = $serie['role']; | |
| 363 | + } | |
| 364 | + } | |
| 365 | + } | |
| 366 | + } | |
| 367 | + if ( ! empty( $annotations ) ) { | |
| 368 | + $exploded_data = array(); | |
| 369 | + $series_names = array(); | |
| 370 | + foreach ( $series as $index => $serie ) { | |
| 371 | + // skip X axis data. | |
| 372 | + if ( $index === 0 ) { | |
| 373 | + continue; | |
| 374 | + } | |
| 375 | + if ( array_key_exists( 'role' . $index, $annotations ) ) { | |
| 376 | + $series_names[] = (object) array( 'role' => $annotations[ 'role' . $index ], 'type' => $serie['type'] ); | |
| 377 | + } else { | |
| 378 | + $series_names[] = $serie['label']; | |
| 379 | + } | |
| 380 | + } | |
| 381 | + $exploded_data[] = $series_names; | |
| 224 | 382 | |
| 225 | - if ( ! empty( $permissions ) ) { | |
| 226 | - $data['visualizer-permissions'] = $permissions; | |
| 383 | + foreach ( $data['visualizer-data'] as $datum ) { | |
| 384 | + // skip X axis data. | |
| 385 | + unset( $datum[0] ); | |
| 386 | + $exploded_data[] = $datum; | |
| 387 | + } | |
| 388 | + $data['visualizer-data-exploded'] = array( $exploded_data ); | |
| 227 | 389 | } |
| 228 | 390 | } |
| 229 | 391 | |
| 230 | - return $data; | |
| 231 | - } | |
| 392 | + $import = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_URL, true ); | |
| 232 | 393 | |
| 233 | - /** | |
| 234 | - * Rest Callback Method | |
| 235 | - */ | |
| 236 | - public function update_chart_data( $data ) { | |
| 237 | - if ( $data['id'] && ! is_wp_error( $data['id'] ) ) { | |
| 394 | + $schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true ); | |
| 238 | 395 | |
| 239 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $data['visualizer-chart-type'] ); | |
| 240 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $data['visualizer-source'] ); | |
| 241 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] ); | |
| 242 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] ); | |
| 243 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] ); | |
| 396 | + $db_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_SCHEDULE, true ); | |
| 244 | 397 | |
| 245 | - if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] ) { | |
| 246 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $data['visualizer-chart-url'] ); | |
| 247 | - apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $data['visualizer-chart-url'], $data['visualizer-chart-schedule'] ); | |
| 248 | - } else { | |
| 249 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); | |
| 250 | - apply_filters( 'visualizer_pro_remove_schedule', $data['id'] ); | |
| 251 | - } | |
| 398 | + $db_query = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_QUERY, true ); | |
| 252 | 399 | |
| 253 | - if ( VISUALIZER_PRO ) { | |
| 254 | - update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] ); | |
| 255 | - } | |
| 400 | + $json_url = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_URL, true ); | |
| 256 | 401 | |
| 257 | - if ( $data['visualizer-chart-url'] ) { | |
| 258 | - $content['source'] = $data['visualizer-chart-url']; | |
| 259 | - $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 260 | - } else { | |
| 261 | - $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 262 | - } | |
| 402 | + $json_headers = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_HEADERS, true ); | |
| 263 | 403 | |
| 264 | - $chart = array( | |
| 265 | - 'ID' => $data['id'], | |
| 266 | - 'post_content' => serialize( $content ), | |
| 267 | - ); | |
| 404 | + $json_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true ); | |
| 268 | 405 | |
| 269 | - wp_update_post( $chart ); | |
| 406 | + $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true ); | |
| 270 | 407 | |
| 271 | - $revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) ); | |
| 408 | + $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true ); | |
| 272 | 409 | |
| 273 | - if ( count( $revisions ) > 1 ) { | |
| 274 | - $revision_ids = array_keys( $revisions ); | |
| 410 | + if ( ! empty( $import ) && $schedule >= 0 ) { | |
| 411 | + $data['visualizer-chart-url'] = $import; | |
| 412 | + $data['visualizer-chart-schedule'] = $schedule; | |
| 413 | + } | |
| 275 | 414 | |
| 276 | - // delete all revisions. | |
| 277 | - foreach ( $revision_ids as $id ) { | |
| 278 | - wp_delete_post_revision( $id ); | |
| 279 | - } | |
| 415 | + if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) { | |
| 416 | + $data['visualizer-db-schedule'] = $db_schedule; | |
| 417 | + $data['visualizer-db-query'] = $db_query; | |
| 418 | + } | |
| 419 | + | |
| 420 | + if ( ! empty( $json_url ) ) { | |
| 421 | + $data['visualizer-json-schedule'] = $json_schedule; | |
| 422 | + $data['visualizer-json-url'] = $json_url; | |
| 423 | + $data['visualizer-json-headers'] = $json_headers; | |
| 424 | + $data['visualizer-json-root'] = $json_root; | |
| 425 | + | |
| 426 | + if ( Visualizer_Module::is_pro() && ! empty( $json_paging ) ) { | |
| 427 | + $data['visualizer-json-paging'] = $json_paging; | |
| 280 | 428 | } |
| 429 | + } | |
| 281 | 430 | |
| 282 | - return new \WP_REST_Response( array( 'success' => sprintf( 'Chart updated' ) ) ); | |
| 431 | + if ( Visualizer_Module::is_pro() ) { | |
| 432 | + $permissions = get_post_meta( $post_id, Visualizer_Pro::CF_PERMISSIONS, true ); | |
| 433 | + | |
| 434 | + if ( empty( $permissions ) ) { | |
| 435 | + $permissions = array( | |
| 436 | + 'permissions' => array( | |
| 437 | + 'read' => 'all', | |
| 438 | + 'edit' => 'roles', | |
| 439 | + 'edit-specific' => array( 'administrator' ), | |
| 440 | + ), | |
| 441 | + ); | |
| 442 | + } | |
| 443 | + | |
| 444 | + $data['visualizer-permissions'] = $permissions; | |
| 283 | 445 | } |
| 446 | + | |
| 447 | + return $data; | |
| 284 | 448 | } |
| 285 | 449 | |
| 286 | 450 | /** |
| 287 | 451 | * Format chart data. |
| 452 | + * | |
| 453 | + * Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure. | |
| 288 | 454 | */ |
| 289 | 455 | public function format_chart_data( $data, $series ) { |
| 290 | 456 | foreach ( $series as $i => $row ) { |
| 291 | - // if no value exists for the seires, then add null | |
| 457 | + // if no value exists for the series, then add null | |
| 292 | 458 | if ( ! isset( $series[ $i ] ) ) { |
| 293 | 459 | $series[ $i ] = null; |
| 294 | 460 | } |
| 295 | 461 | |
| @@ -296,39 +462,38 @@ | ||
| 296 | 462 | if ( is_null( $series[ $i ] ) ) { |
| 297 | 463 | continue; |
| 298 | 464 | } |
| 299 | 465 | |
| 300 | - if ( $row['type'] == 'number' ) { | |
| 301 | - foreach ( $data as $o => $col ) { | |
| 302 | - $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); | |
| 303 | - } | |
| 304 | - } | |
| 305 | - | |
| 306 | - if ( $row['type'] == 'boolean' ) { | |
| 307 | - foreach ( $data as $o => $col ) { | |
| 308 | - $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_validate( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null; | |
| 309 | - } | |
| 310 | - } | |
| 311 | - | |
| 312 | - if ( $row['type'] == 'timeofday' ) { | |
| 313 | - foreach ( $data as $o => $col ) { | |
| 314 | - $date = new DateTime( '1984-03-16T' . $col[ $i ] ); | |
| 315 | - if ( $date ) { | |
| 316 | - $data[ $o ][ $i ] = array( | |
| 317 | - intval( $date->format( 'H' ) ), | |
| 318 | - intval( $date->format( 'i' ) ), | |
| 319 | - intval( $date->format( 's' ) ), | |
| 320 | - 0, | |
| 321 | - ); | |
| 466 | + switch ( $row['type'] ) { | |
| 467 | + case 'number': | |
| 468 | + foreach ( $data as $o => $col ) { | |
| 469 | + $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); | |
| 322 | 470 | } |
| 323 | - } | |
| 471 | + break; | |
| 472 | + case 'boolean': | |
| 473 | + foreach ( $data as $o => $col ) { | |
| 474 | + $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : false; | |
| 475 | + } | |
| 476 | + break; | |
| 477 | + case 'timeofday': | |
| 478 | + foreach ( $data as $o => $col ) { | |
| 479 | + $date = new DateTime( '1984-03-16T' . $col[ $i ] ); | |
| 480 | + if ( $date ) { | |
| 481 | + $data[ $o ][ $i ] = array( | |
| 482 | + intval( $date->format( 'H' ) ), | |
| 483 | + intval( $date->format( 'i' ) ), | |
| 484 | + intval( $date->format( 's' ) ), | |
| 485 | + 0, | |
| 486 | + ); | |
| 487 | + } | |
| 488 | + } | |
| 489 | + break; | |
| 490 | + case 'string': | |
| 491 | + foreach ( $data as $o => $col ) { | |
| 492 | + $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); | |
| 493 | + } | |
| 494 | + break; | |
| 324 | 495 | } |
| 325 | - | |
| 326 | - if ( $row['type'] == 'string' ) { | |
| 327 | - foreach ( $data as $o => $col ) { | |
| 328 | - $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); | |
| 329 | - } | |
| 330 | - } | |
| 331 | 496 | } |
| 332 | 497 | |
| 333 | 498 | return $data; |
| 334 | 499 | } |
| @@ -343,70 +508,25 @@ | ||
| 343 | 508 | return $datum; |
| 344 | 509 | } |
| 345 | 510 | |
| 346 | 511 | /** |
| 347 | - * Handle remote CSV data | |
| 512 | + * Filter Rest Query | |
| 348 | 513 | */ |
| 349 | - public function upload_csv_data( $data ) { | |
| 350 | - if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) { | |
| 351 | - $source = new Visualizer_Source_Csv_Remote( $data['url'] ); | |
| 352 | - if ( $source->fetch() ) { | |
| 353 | - $temp = $source->getData(); | |
| 354 | - if ( is_string( $temp ) && is_array( unserialize( $temp ) ) ) { | |
| 355 | - $content['series'] = $source->getSeries(); | |
| 356 | - $content['data'] = $source->getRawData(); | |
| 357 | - return $content; | |
| 358 | - } else { | |
| 359 | - return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 360 | - } | |
| 361 | - } else { | |
| 362 | - return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 363 | - } | |
| 364 | - } else { | |
| 365 | - return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 514 | + public function add_rest_query_vars( $args, \WP_REST_Request $request ) { | |
| 515 | + if ( isset( $request['meta_key'] ) && isset( $request['meta_value'] ) ) { | |
| 516 | + $args['meta_query'] = array( | |
| 517 | + 'relation' => 'OR', | |
| 518 | + array( | |
| 519 | + 'key' => $request->get_param( 'meta_key' ), | |
| 520 | + 'value' => $request->get_param( 'meta_value' ), | |
| 521 | + 'compare' => '!=', | |
| 522 | + ), | |
| 523 | + array( | |
| 524 | + 'key' => $request->get_param( 'meta_key' ), | |
| 525 | + 'value' => $request->get_param( 'meta_value' ), | |
| 526 | + 'compare' => 'NOT EXISTS', | |
| 527 | + ), | |
| 528 | + ); | |
| 366 | 529 | } |
| 530 | + return $args; | |
| 367 | 531 | } |
| 368 | - | |
| 369 | - /** | |
| 370 | - * Get permission data | |
| 371 | - */ | |
| 372 | - public function get_permission_data( $data ) { | |
| 373 | - $options = array(); | |
| 374 | - switch ( $data['type'] ) { | |
| 375 | - case 'users': | |
| 376 | - $query = new WP_User_Query( | |
| 377 | - array( | |
| 378 | - 'number' => 1000, | |
| 379 | - 'orderby' => 'display_name', | |
| 380 | - 'fields' => array( 'ID', 'display_name' ), | |
| 381 | - 'count_total' => false, | |
| 382 | - ) | |
| 383 | - ); | |
| 384 | - $users = $query->get_results(); | |
| 385 | - if ( ! empty( $users ) ) { | |
| 386 | - $i = 0; | |
| 387 | - foreach ( $users as $user ) { | |
| 388 | - $options[ $i ]['value'] = $user->ID; | |
| 389 | - $options[ $i ]['label'] = $user->display_name; | |
| 390 | - $i++; | |
| 391 | - } | |
| 392 | - } | |
| 393 | - break; | |
| 394 | - case 'roles': | |
| 395 | - if ( ! function_exists( 'get_editable_roles' ) ) { | |
| 396 | - require_once ABSPATH . 'wp-admin/includes/user.php'; | |
| 397 | - } | |
| 398 | - $roles = get_editable_roles(); | |
| 399 | - if ( ! empty( $roles ) ) { | |
| 400 | - $i = 0; | |
| 401 | - foreach ( get_editable_roles() as $name => $info ) { | |
| 402 | - $options[ $i ]['value'] = $name; | |
| 403 | - $options[ $i ]['label'] = $name; | |
| 404 | - $i++; | |
| 405 | - } | |
| 406 | - } | |
| 407 | - break; | |
| 408 | - } | |
| 409 | - return $options; | |
| 410 | - } | |
| 411 | - | |
| 412 | 532 | } |