| @@ -63,140 +63,70 @@ | ||
| 63 | 63 | add_filter( 'rest_visualizer_query', array( $this, 'add_rest_query_vars' ), 9, 2 ); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | - * Enqueue Gutenberg block assets. | |
| 67 | + * Enqueue front end and editor JavaScript and CSS | |
| 68 | 68 | */ |
| 69 | 69 | public function enqueue_gutenberg_scripts() { |
| 70 | - global $pagenow; | |
| 70 | + global $wp_version; | |
| 71 | 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; | |
| 72 | + $blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.js'; | |
| 73 | + $handsontableJS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.js'; | |
| 74 | + $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.css'; | |
| 75 | + $handsontableCSS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.css'; | |
| 76 | + | |
| 77 | + if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) { | |
| 78 | + $version = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/block.js' ); | |
| 78 | 79 | } else { |
| 79 | - $asset = array( | |
| 80 | - 'dependencies' => array(), | |
| 81 | - 'version' => $this->version, | |
| 82 | - ); | |
| 80 | + $version = $this->version; | |
| 83 | 81 | } |
| 84 | 82 | |
| 85 | - if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) { | |
| 86 | - $asset['version'] = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/index.js' ); | |
| 87 | - } | |
| 83 | + // Enqueue the bundled block JS file | |
| 84 | + wp_enqueue_script( 'handsontable', $handsontableJS ); | |
| 85 | + wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable', 'visualizer-datatables', 'moment' ), $version, true ); | |
| 88 | 86 | |
| 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 | - } | |
| 87 | + $type = 'community'; | |
| 92 | 88 | |
| 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 ); | |
| 89 | + if ( Visualizer_Module::is_pro() ) { | |
| 90 | + $type = 'pro'; | |
| 91 | + if ( apply_filters( 'visualizer_is_business', false ) ) { | |
| 92 | + $type = 'business'; | |
| 93 | + } | |
| 95 | 94 | } |
| 96 | 95 | |
| 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'] ); | |
| 113 | - } | |
| 114 | - $script_deps = array_values( array_unique( $script_deps ) ); | |
| 115 | - wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, $script_deps, $asset['version'], true ); | |
| 96 | + $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( null, false ); | |
| 116 | 97 | |
| 117 | 98 | $translation_array = array( |
| 99 | + 'isPro' => $type, | |
| 100 | + 'proTeaser' => Visualizer_Plugin::PRO_TEASER_URL, | |
| 101 | + 'absurl' => VISUALIZER_ABSURL, | |
| 102 | + 'charts' => Visualizer_Module_Admin::_getChartTypesLocalized(), | |
| 118 | 103 | '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' ), | |
| 104 | + 'sqlTable' => $table_col_mapping, | |
| 105 | + 'chartsPerPage' => defined( 'TI_CYPRESS_TESTING' ) ? 20 : 6, | |
| 124 | 106 | ); |
| 125 | 107 | wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array ); |
| 126 | 108 | |
| 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 | - } | |
| 109 | + // Enqueue frontend and editor block styles | |
| 110 | + wp_enqueue_style( 'handsontable', $handsontableCSS ); | |
| 111 | + wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $version ); | |
| 149 | 112 | |
| 150 | - // Enqueue frontend and editor block styles | |
| 151 | - wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $asset['version'] ); | |
| 113 | + if ( version_compare( $wp_version, '4.9.0', '>' ) ) { | |
| 152 | 114 | |
| 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', | |
| 115 | + wp_enqueue_code_editor( | |
| 187 | 116 | array( |
| 188 | - 'ajaxUrl' => admin_url( 'admin-ajax.php' ), | |
| 189 | - 'nonce' => wp_create_nonce( 'visualizer-ai-builder' ), | |
| 190 | - 'isPro' => Visualizer_Module::is_pro(), | |
| 117 | + 'type' => 'sql', | |
| 118 | + 'codemirror' => array( | |
| 119 | + 'autofocus' => true, | |
| 120 | + 'lineWrapping' => true, | |
| 121 | + 'dragDrop' => false, | |
| 122 | + 'matchBrackets' => true, | |
| 123 | + 'autoCloseBrackets' => true, | |
| 124 | + 'extraKeys' => array( 'Ctrl-Space' => 'autocomplete' ), | |
| 125 | + 'hintOptions' => array( 'tables' => $table_col_mapping ), | |
| 126 | + ), | |
| 191 | 127 | ) |
| 192 | 128 | ); |
| 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 | 129 | } |
| 200 | 130 | } |
| 201 | 131 | /** |
| 202 | 132 | * Hook server side rendering into render callback |
| @@ -208,11 +138,8 @@ | ||
| 208 | 138 | 'attributes' => array( |
| 209 | 139 | 'id' => array( |
| 210 | 140 | 'type' => 'number', |
| 211 | 141 | ), |
| 212 | - 'lazy' => array( | |
| 213 | - 'type' => 'string', | |
| 214 | - ), | |
| 215 | 142 | ), |
| 216 | 143 | ) |
| 217 | 144 | ); |
| 218 | 145 | } |
| @@ -219,45 +146,16 @@ | ||
| 219 | 146 | |
| 220 | 147 | /** |
| 221 | 148 | * Gutenberg Block Callback Function |
| 222 | 149 | */ |
| 223 | - public function gutenberg_block_callback( $atts ) { | |
| 224 | - // no id, no fun. | |
| 225 | - if ( ! isset( $atts['id'] ) ) { | |
| 226 | - return ''; | |
| 150 | + public function gutenberg_block_callback( $attr ) { | |
| 151 | + if ( isset( $attr['id'] ) ) { | |
| 152 | + $id = $attr['id']; | |
| 153 | + if ( empty( $id ) || $id === 'none' ) { | |
| 154 | + return ''; // no id = no fun | |
| 155 | + } | |
| 156 | + return '[visualizer id="' . $id . '"]'; | |
| 227 | 157 | } |
| 228 | - | |
| 229 | - $atts = shortcode_atts( | |
| 230 | - array( | |
| 231 | - 'id' => false, | |
| 232 | - 'lazy' => apply_filters( 'visualizer_lazy_by_default', false, $atts['id'] ), | |
| 233 | - // we are deliberating excluding the class attribute from here | |
| 234 | - // as this will be handled by the custom class in Gutenberg | |
| 235 | - ), | |
| 236 | - $atts | |
| 237 | - ); | |
| 238 | - | |
| 239 | - // no id, no fun. | |
| 240 | - if ( ! $atts['id'] ) { | |
| 241 | - return ''; | |
| 242 | - } | |
| 243 | - | |
| 244 | - if ( $atts['lazy'] === '-1' || $atts['lazy'] === false ) { | |
| 245 | - $atts['lazy'] = 'no'; | |
| 246 | - } | |
| 247 | - | |
| 248 | - // we don't want the chart in the editor lazy-loading. | |
| 249 | - if ( is_admin() ) { | |
| 250 | - unset( $atts['lazy'] ); | |
| 251 | - } | |
| 252 | - | |
| 253 | - $shortcode = '[visualizer'; | |
| 254 | - foreach ( $atts as $name => $value ) { | |
| 255 | - $shortcode .= sprintf( ' %s="%s"', $name, $value ); | |
| 256 | - } | |
| 257 | - $shortcode .= ']'; | |
| 258 | - | |
| 259 | - return $shortcode; | |
| 260 | 158 | } |
| 261 | 159 | |
| 262 | 160 | /** |
| 263 | 161 | * Hook server side rendering into render callback |
| @@ -269,8 +167,125 @@ | ||
| 269 | 167 | array( |
| 270 | 168 | 'get_callback' => array( $this, 'get_visualizer_data' ), |
| 271 | 169 | ) |
| 272 | 170 | ); |
| 171 | + | |
| 172 | + register_rest_route( | |
| 173 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 174 | + '/get-query-data', | |
| 175 | + array( | |
| 176 | + 'methods' => 'GET', | |
| 177 | + 'callback' => array( $this, 'get_query_data' ), | |
| 178 | + 'permission_callback' => function () { | |
| 179 | + return current_user_can( 'edit_posts' ); | |
| 180 | + }, | |
| 181 | + ) | |
| 182 | + ); | |
| 183 | + | |
| 184 | + register_rest_route( | |
| 185 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 186 | + '/get-json-root', | |
| 187 | + array( | |
| 188 | + 'methods' => 'GET', | |
| 189 | + 'callback' => array( $this, 'get_json_root_data' ), | |
| 190 | + 'args' => array( | |
| 191 | + 'url' => array( | |
| 192 | + 'sanitize_callback' => 'esc_url_raw', | |
| 193 | + ), | |
| 194 | + ), | |
| 195 | + 'permission_callback' => function () { | |
| 196 | + return current_user_can( 'edit_posts' ); | |
| 197 | + }, | |
| 198 | + ) | |
| 199 | + ); | |
| 200 | + | |
| 201 | + register_rest_route( | |
| 202 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 203 | + '/get-json-data', | |
| 204 | + array( | |
| 205 | + 'methods' => 'GET', | |
| 206 | + 'callback' => array( $this, 'get_json_data' ), | |
| 207 | + 'args' => array( | |
| 208 | + 'url' => array( | |
| 209 | + 'sanitize_callback' => 'esc_url_raw', | |
| 210 | + ), | |
| 211 | + 'chart' => array( | |
| 212 | + 'sanitize_callback' => 'absint', | |
| 213 | + ), | |
| 214 | + ), | |
| 215 | + 'permission_callback' => function () { | |
| 216 | + return current_user_can( 'edit_posts' ); | |
| 217 | + }, | |
| 218 | + ) | |
| 219 | + ); | |
| 220 | + | |
| 221 | + register_rest_route( | |
| 222 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 223 | + '/set-json-data', | |
| 224 | + array( | |
| 225 | + 'methods' => 'GET', | |
| 226 | + 'callback' => array( $this, 'set_json_data' ), | |
| 227 | + 'args' => array( | |
| 228 | + 'url' => array( | |
| 229 | + 'sanitize_callback' => 'esc_url_raw', | |
| 230 | + ), | |
| 231 | + ), | |
| 232 | + 'permission_callback' => function () { | |
| 233 | + return current_user_can( 'edit_posts' ); | |
| 234 | + }, | |
| 235 | + ) | |
| 236 | + ); | |
| 237 | + | |
| 238 | + register_rest_route( | |
| 239 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 240 | + '/update-chart', | |
| 241 | + array( | |
| 242 | + 'methods' => 'POST', | |
| 243 | + 'callback' => array( $this, 'update_chart_data' ), | |
| 244 | + 'args' => array( | |
| 245 | + 'id' => array( | |
| 246 | + 'sanitize_callback' => 'absint', | |
| 247 | + ), | |
| 248 | + ), | |
| 249 | + 'permission_callback' => function () { | |
| 250 | + return current_user_can( 'edit_posts' ); | |
| 251 | + }, | |
| 252 | + ) | |
| 253 | + ); | |
| 254 | + | |
| 255 | + register_rest_route( | |
| 256 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 257 | + '/upload-data', | |
| 258 | + array( | |
| 259 | + 'methods' => 'POST', | |
| 260 | + 'callback' => array( $this, 'upload_csv_data' ), | |
| 261 | + 'args' => array( | |
| 262 | + 'url' => array( | |
| 263 | + 'sanitize_callback' => 'esc_url_raw', | |
| 264 | + ), | |
| 265 | + ), | |
| 266 | + 'permission_callback' => function () { | |
| 267 | + return current_user_can( 'edit_posts' ); | |
| 268 | + }, | |
| 269 | + ) | |
| 270 | + ); | |
| 271 | + | |
| 272 | + register_rest_route( | |
| 273 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 274 | + '/get-permission-data', | |
| 275 | + array( | |
| 276 | + 'methods' => 'GET', | |
| 277 | + 'callback' => array( $this, 'get_permission_data' ), | |
| 278 | + 'args' => array( | |
| 279 | + 'type' => array( | |
| 280 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 281 | + ), | |
| 282 | + ), | |
| 283 | + 'permission_callback' => function () { | |
| 284 | + return current_user_can( 'edit_posts' ); | |
| 285 | + }, | |
| 286 | + ) | |
| 287 | + ); | |
| 273 | 288 | } |
| 274 | 289 | |
| 275 | 290 | /** |
| 276 | 291 | * Get Post Meta Fields |
| @@ -275,9 +290,9 @@ | ||
| 275 | 290 | /** |
| 276 | 291 | * Get Post Meta Fields |
| 277 | 292 | */ |
| 278 | 293 | public function get_visualizer_data( $post ) { |
| 279 | - if ( ! Visualizer_Module::can_edit_chart( $post['id'] ) ) { | |
| 294 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 280 | 295 | return false; |
| 281 | 296 | } |
| 282 | 297 | |
| 283 | 298 | $data = array(); |
| @@ -286,11 +301,8 @@ | ||
| 286 | 301 | $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 287 | 302 | |
| 288 | 303 | $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); |
| 289 | 304 | $data['visualizer-chart-library'] = $library; |
| 290 | - if ( 'd3' === $library ) { | |
| 291 | - $data['visualizer-d3-code'] = get_post_meta( $post_id, Visualizer_Module_AIBuilder::CF_D3_CODE, true ); | |
| 292 | - } | |
| 293 | 305 | |
| 294 | 306 | $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true ); |
| 295 | 307 | |
| 296 | 308 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| @@ -296,11 +308,9 @@ | ||
| 296 | 308 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| 297 | 309 | |
| 298 | 310 | // faetch and update settings |
| 299 | 311 | $data['visualizer-settings'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SETTINGS, true ); |
| 300 | - if ( empty( $data['visualizer-settings']['pagination'] ) ) { | |
| 301 | - $data['visualizer-settings']['pageSize'] = ''; | |
| 302 | - } | |
| 312 | + | |
| 303 | 313 | // handle series filter hooks |
| 304 | 314 | $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'] ); |
| 305 | 315 | |
| 306 | 316 | // handle settings filter hooks |
| @@ -306,20 +316,10 @@ | ||
| 306 | 316 | // handle settings filter hooks |
| 307 | 317 | $data['visualizer-settings'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $data['visualizer-settings'], $post_id, $data['visualizer-chart-type'] ); |
| 308 | 318 | |
| 309 | 319 | // handle data filter hooks |
| 310 | - $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'] ); | |
| 320 | + $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'] ); | |
| 311 | 321 | |
| 312 | - // we are going to format only for tabular charts, because we are not sure of the effect on others. | |
| 313 | - // this is to solve the case where boolean data shows up as all-ticks on gutenberg. | |
| 314 | - if ( in_array( $data['visualizer-chart-type'], array( 'tabular' ), true ) ) { | |
| 315 | - $data['visualizer-data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 316 | - } | |
| 317 | - | |
| 318 | - if ( ! isset( $data['visualizer-settings']['hAxis']['format'] ) ) { | |
| 319 | - $data['visualizer-settings']['hAxis']['format'] = ''; | |
| 320 | - } | |
| 321 | - | |
| 322 | 322 | $data['visualizer-data-exploded'] = ''; |
| 323 | 323 | // handle annotations for google charts |
| 324 | 324 | if ( 'GoogleCharts' === $library ) { |
| 325 | 325 | // this will contain the data of both axis. |
| @@ -336,12 +336,8 @@ | ||
| 336 | 336 | if ( ! empty( $serie['role'] ) ) { |
| 337 | 337 | // this series is some kind of annotation, so let's collect its index. |
| 338 | 338 | // the index will be +1 because the X axis value is index 0, which is being ignored. |
| 339 | 339 | $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role']; |
| 340 | - | |
| 341 | - if ( isset( $data['visualizer-series'][ intval( $index ) + 1 ] ) ) { | |
| 342 | - $data['visualizer-series'][ intval( $index ) + 1 ]['role'] = $serie['role']; | |
| 343 | - } | |
| 344 | 340 | } |
| 345 | 341 | } |
| 346 | 342 | } |
| 347 | 343 | if ( ! empty( $annotations ) ) { |
| @@ -386,10 +382,10 @@ | ||
| 386 | 382 | $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true ); |
| 387 | 383 | |
| 388 | 384 | $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true ); |
| 389 | 385 | |
| 390 | - if ( ! empty( $import ) && $schedule >= 0 ) { | |
| 391 | - $data['visualizer-chart-url'] = $import; | |
| 386 | + if ( ! empty( $import ) && ! empty( $schedule ) ) { | |
| 387 | + $data['visualizer-chart-url'] = $import; | |
| 392 | 388 | $data['visualizer-chart-schedule'] = $schedule; |
| 393 | 389 | } |
| 394 | 390 | |
| 395 | 391 | if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) { |
| @@ -408,34 +404,220 @@ | ||
| 408 | 404 | } |
| 409 | 405 | } |
| 410 | 406 | |
| 411 | 407 | if ( Visualizer_Module::is_pro() ) { |
| 412 | - $permissions = get_post_meta( $post_id, Visualizer_Pro::CF_PERMISSIONS, true ); | |
| 408 | + $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true ); | |
| 413 | 409 | |
| 414 | - if ( empty( $permissions ) ) { | |
| 415 | - $permissions = array( | |
| 416 | - 'permissions' => array( | |
| 417 | - 'read' => 'all', | |
| 418 | - 'edit' => 'roles', | |
| 419 | - 'edit-specific' => array( 'administrator' ), | |
| 420 | - ), | |
| 421 | - ); | |
| 410 | + if ( ! empty( $permissions ) ) { | |
| 411 | + $data['visualizer-permissions'] = $permissions; | |
| 422 | 412 | } |
| 413 | + } | |
| 423 | 414 | |
| 424 | - $data['visualizer-permissions'] = $permissions; | |
| 415 | + return $data; | |
| 416 | + } | |
| 417 | + | |
| 418 | + /** | |
| 419 | + * Returns the data for the query. | |
| 420 | + * | |
| 421 | + * @access public | |
| 422 | + */ | |
| 423 | + public function get_query_data( $data ) { | |
| 424 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 425 | + return false; | |
| 425 | 426 | } |
| 426 | 427 | |
| 427 | - return $data; | |
| 428 | + $source = new Visualizer_Source_Query( stripslashes( $data['query'] ) ); | |
| 429 | + $html = $source->fetch( true ); | |
| 430 | + $source->fetch( false ); | |
| 431 | + $name = $source->getSourceName(); | |
| 432 | + $series = $source->getSeries(); | |
| 433 | + $data = $source->getRawData(); | |
| 434 | + $error = ''; | |
| 435 | + if ( empty( $html ) ) { | |
| 436 | + $error = $source->get_error(); | |
| 437 | + wp_send_json_error( array( 'msg' => $error ) ); | |
| 438 | + } | |
| 439 | + wp_send_json_success( array( 'table' => $html, 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 428 | 440 | } |
| 429 | 441 | |
| 430 | 442 | /** |
| 443 | + * Returns the JSON root. | |
| 444 | + * | |
| 445 | + * @access public | |
| 446 | + */ | |
| 447 | + public function get_json_root_data( $data ) { | |
| 448 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 449 | + return false; | |
| 450 | + } | |
| 451 | + | |
| 452 | + $source = new Visualizer_Source_Json( $data ); | |
| 453 | + | |
| 454 | + $roots = $source->fetchRoots(); | |
| 455 | + if ( empty( $roots ) ) { | |
| 456 | + wp_send_json_error( array( 'msg' => $source->get_error() ) ); | |
| 457 | + } | |
| 458 | + | |
| 459 | + wp_send_json_success( array( 'url' => $data['url'], 'roots' => $roots ) ); | |
| 460 | + } | |
| 461 | + | |
| 462 | + /** | |
| 463 | + * Returns the JSON data. | |
| 464 | + * | |
| 465 | + * @access public | |
| 466 | + */ | |
| 467 | + public function get_json_data( $data ) { | |
| 468 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 469 | + return false; | |
| 470 | + } | |
| 471 | + | |
| 472 | + $chart_id = $data['chart']; | |
| 473 | + | |
| 474 | + if ( empty( $chart_id ) ) { | |
| 475 | + wp_die(); | |
| 476 | + } | |
| 477 | + | |
| 478 | + $source = new Visualizer_Source_Json( $data ); | |
| 479 | + $source->fetch(); | |
| 480 | + $table = $source->getRawData(); | |
| 481 | + | |
| 482 | + if ( empty( $table ) ) { | |
| 483 | + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 484 | + } | |
| 485 | + | |
| 486 | + $table = Visualizer_Render_Layout::show( 'editor-table', $table, $chart_id, 'viz-json-table', false, false ); | |
| 487 | + wp_send_json_success( array( 'table' => $table, 'root' => $data['root'], 'url' => $data['url'], 'paging' => $source->getPaginationElements() ) ); | |
| 488 | + } | |
| 489 | + | |
| 490 | + /** | |
| 491 | + * Set the JSON data. | |
| 492 | + * | |
| 493 | + * @access public | |
| 494 | + */ | |
| 495 | + public function set_json_data( $data ) { | |
| 496 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 497 | + return false; | |
| 498 | + } | |
| 499 | + | |
| 500 | + $source = new Visualizer_Source_Json( $data ); | |
| 501 | + | |
| 502 | + $table = $source->fetch(); | |
| 503 | + if ( empty( $table ) ) { | |
| 504 | + wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 505 | + } | |
| 506 | + | |
| 507 | + $source->fetchFromEditableTable(); | |
| 508 | + $name = $source->getSourceName(); | |
| 509 | + $series = json_encode( $source->getSeries() ); | |
| 510 | + $data = json_encode( $source->getRawData() ); | |
| 511 | + wp_send_json_success( array( 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 512 | + } | |
| 513 | + | |
| 514 | + /** | |
| 515 | + * Rest Callback Method | |
| 516 | + */ | |
| 517 | + public function update_chart_data( $data ) { | |
| 518 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 519 | + return false; | |
| 520 | + } | |
| 521 | + | |
| 522 | + if ( $data['id'] && ! is_wp_error( $data['id'] ) ) { | |
| 523 | + if ( get_post_type( $data['id'] ) !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 524 | + return new WP_Error( 'invalid_post_type', 'Invalid post type.' ); | |
| 525 | + } | |
| 526 | + $chart_type = sanitize_text_field( $data['visualizer-chart-type'] ); | |
| 527 | + $source_type = sanitize_text_field( $data['visualizer-source'] ); | |
| 528 | + | |
| 529 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type ); | |
| 530 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type ); | |
| 531 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] ); | |
| 532 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] ); | |
| 533 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] ); | |
| 534 | + | |
| 535 | + if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] ) { | |
| 536 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 537 | + $chart_schedule = intval( $data['visualizer-chart-schedule'] ); | |
| 538 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url ); | |
| 539 | + apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule ); | |
| 540 | + } else { | |
| 541 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); | |
| 542 | + apply_filters( 'visualizer_pro_remove_schedule', $data['id'] ); | |
| 543 | + } | |
| 544 | + | |
| 545 | + if ( $source_type === 'Visualizer_Source_Query' ) { | |
| 546 | + $db_schedule = intval( $data['visualizer-db-schedule'] ); | |
| 547 | + $db_query = $data['visualizer-db-query']; | |
| 548 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule ); | |
| 549 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) ); | |
| 550 | + } else { | |
| 551 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE ); | |
| 552 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY ); | |
| 553 | + } | |
| 554 | + | |
| 555 | + if ( $source_type === 'Visualizer_Source_Json' ) { | |
| 556 | + $json_schedule = intval( $data['visualizer-json-schedule'] ); | |
| 557 | + $json_url = esc_url_raw( $data['visualizer-json-url'] ); | |
| 558 | + $json_headers = esc_url_raw( $data['visualizer-json-headers'] ); | |
| 559 | + $json_root = $data['visualizer-json-root']; | |
| 560 | + $json_paging = $data['visualizer-json-paging']; | |
| 561 | + | |
| 562 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule ); | |
| 563 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url ); | |
| 564 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers ); | |
| 565 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root ); | |
| 566 | + | |
| 567 | + if ( ! empty( $json_paging ) ) { | |
| 568 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging ); | |
| 569 | + } else { | |
| 570 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 571 | + } | |
| 572 | + } else { | |
| 573 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE ); | |
| 574 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL ); | |
| 575 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS ); | |
| 576 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT ); | |
| 577 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 578 | + } | |
| 579 | + | |
| 580 | + if ( Visualizer_Module::is_pro() ) { | |
| 581 | + update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] ); | |
| 582 | + } | |
| 583 | + | |
| 584 | + if ( $data['visualizer-chart-url'] ) { | |
| 585 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 586 | + $content['source'] = $chart_url; | |
| 587 | + $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 588 | + } else { | |
| 589 | + $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 590 | + } | |
| 591 | + | |
| 592 | + $chart = array( | |
| 593 | + 'ID' => $data['id'], | |
| 594 | + 'post_content' => serialize( $content ), | |
| 595 | + ); | |
| 596 | + | |
| 597 | + wp_update_post( $chart ); | |
| 598 | + | |
| 599 | + $revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) ); | |
| 600 | + | |
| 601 | + if ( count( $revisions ) > 1 ) { | |
| 602 | + $revision_ids = array_keys( $revisions ); | |
| 603 | + | |
| 604 | + // delete all revisions. | |
| 605 | + foreach ( $revision_ids as $id ) { | |
| 606 | + wp_delete_post_revision( $id ); | |
| 607 | + } | |
| 608 | + } | |
| 609 | + | |
| 610 | + return new \WP_REST_Response( array( 'success' => sprintf( 'Chart updated' ) ) ); | |
| 611 | + } | |
| 612 | + } | |
| 613 | + | |
| 614 | + /** | |
| 431 | 615 | * Format chart data. |
| 432 | - * | |
| 433 | - * Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure. | |
| 434 | 616 | */ |
| 435 | 617 | public function format_chart_data( $data, $series ) { |
| 436 | 618 | foreach ( $series as $i => $row ) { |
| 437 | - // if no value exists for the series, then add null | |
| 619 | + // if no value exists for the seires, then add null | |
| 438 | 620 | if ( ! isset( $series[ $i ] ) ) { |
| 439 | 621 | $series[ $i ] = null; |
| 440 | 622 | } |
| 441 | 623 | |
| @@ -442,38 +624,39 @@ | ||
| 442 | 624 | if ( is_null( $series[ $i ] ) ) { |
| 443 | 625 | continue; |
| 444 | 626 | } |
| 445 | 627 | |
| 446 | - switch ( $row['type'] ) { | |
| 447 | - case 'number': | |
| 448 | - foreach ( $data as $o => $col ) { | |
| 449 | - $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); | |
| 628 | + if ( $row['type'] === 'number' ) { | |
| 629 | + foreach ( $data as $o => $col ) { | |
| 630 | + $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); | |
| 631 | + } | |
| 632 | + } | |
| 633 | + | |
| 634 | + if ( $row['type'] === 'boolean' ) { | |
| 635 | + foreach ( $data as $o => $col ) { | |
| 636 | + $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null; | |
| 637 | + } | |
| 638 | + } | |
| 639 | + | |
| 640 | + if ( $row['type'] === 'timeofday' ) { | |
| 641 | + foreach ( $data as $o => $col ) { | |
| 642 | + $date = new DateTime( '1984-03-16T' . $col[ $i ] ); | |
| 643 | + if ( $date ) { | |
| 644 | + $data[ $o ][ $i ] = array( | |
| 645 | + intval( $date->format( 'H' ) ), | |
| 646 | + intval( $date->format( 'i' ) ), | |
| 647 | + intval( $date->format( 's' ) ), | |
| 648 | + 0, | |
| 649 | + ); | |
| 450 | 650 | } |
| 451 | - break; | |
| 452 | - case 'boolean': | |
| 453 | - foreach ( $data as $o => $col ) { | |
| 454 | - $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : false; | |
| 455 | - } | |
| 456 | - break; | |
| 457 | - case 'timeofday': | |
| 458 | - foreach ( $data as $o => $col ) { | |
| 459 | - $date = new DateTime( '1984-03-16T' . $col[ $i ] ); | |
| 460 | - if ( $date ) { | |
| 461 | - $data[ $o ][ $i ] = array( | |
| 462 | - intval( $date->format( 'H' ) ), | |
| 463 | - intval( $date->format( 'i' ) ), | |
| 464 | - intval( $date->format( 's' ) ), | |
| 465 | - 0, | |
| 466 | - ); | |
| 467 | - } | |
| 468 | - } | |
| 469 | - break; | |
| 470 | - case 'string': | |
| 471 | - foreach ( $data as $o => $col ) { | |
| 472 | - $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); | |
| 473 | - } | |
| 474 | - break; | |
| 651 | + } | |
| 475 | 652 | } |
| 653 | + | |
| 654 | + if ( $row['type'] === 'string' ) { | |
| 655 | + foreach ( $data as $o => $col ) { | |
| 656 | + $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); | |
| 657 | + } | |
| 658 | + } | |
| 476 | 659 | } |
| 477 | 660 | |
| 478 | 661 | return $data; |
| 479 | 662 | } |
| @@ -485,8 +668,82 @@ | ||
| 485 | 668 | if ( ! function_exists( 'mb_detect_encoding' ) || mb_detect_encoding( $datum ) !== 'ASCII' ) { |
| 486 | 669 | $datum = \ForceUTF8\Encoding::toUTF8( $datum ); |
| 487 | 670 | } |
| 488 | 671 | return $datum; |
| 672 | + } | |
| 673 | + | |
| 674 | + /** | |
| 675 | + * Handle remote CSV data | |
| 676 | + */ | |
| 677 | + public function upload_csv_data( $data ) { | |
| 678 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 679 | + return false; | |
| 680 | + } | |
| 681 | + | |
| 682 | + if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) { | |
| 683 | + $source = new Visualizer_Source_Csv_Remote( $data['url'] ); | |
| 684 | + if ( $source->fetch() ) { | |
| 685 | + $temp = $source->getData(); | |
| 686 | + if ( is_string( $temp ) && is_array( unserialize( $temp ) ) ) { | |
| 687 | + $content['series'] = $source->getSeries(); | |
| 688 | + $content['data'] = $source->getRawData(); | |
| 689 | + return $content; | |
| 690 | + } else { | |
| 691 | + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 692 | + } | |
| 693 | + } else { | |
| 694 | + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 695 | + } | |
| 696 | + } else { | |
| 697 | + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 698 | + } | |
| 699 | + } | |
| 700 | + | |
| 701 | + /** | |
| 702 | + * Get permission data | |
| 703 | + */ | |
| 704 | + public function get_permission_data( $data ) { | |
| 705 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 706 | + return false; | |
| 707 | + } | |
| 708 | + | |
| 709 | + $options = array(); | |
| 710 | + switch ( $data['type'] ) { | |
| 711 | + case 'users': | |
| 712 | + $query = new WP_User_Query( | |
| 713 | + array( | |
| 714 | + 'number' => 1000, | |
| 715 | + 'orderby' => 'display_name', | |
| 716 | + 'fields' => array( 'ID', 'display_name' ), | |
| 717 | + 'count_total' => false, | |
| 718 | + ) | |
| 719 | + ); | |
| 720 | + $users = $query->get_results(); | |
| 721 | + if ( ! empty( $users ) ) { | |
| 722 | + $i = 0; | |
| 723 | + foreach ( $users as $user ) { | |
| 724 | + $options[ $i ]['value'] = $user->ID; | |
| 725 | + $options[ $i ]['label'] = $user->display_name; | |
| 726 | + $i++; | |
| 727 | + } | |
| 728 | + } | |
| 729 | + break; | |
| 730 | + case 'roles': | |
| 731 | + if ( ! function_exists( 'get_editable_roles' ) ) { | |
| 732 | + require_once ABSPATH . 'wp-admin/includes/user.php'; | |
| 733 | + } | |
| 734 | + $roles = get_editable_roles(); | |
| 735 | + if ( ! empty( $roles ) ) { | |
| 736 | + $i = 0; | |
| 737 | + foreach ( get_editable_roles() as $name => $info ) { | |
| 738 | + $options[ $i ]['value'] = $name; | |
| 739 | + $options[ $i ]['label'] = $name; | |
| 740 | + $i++; | |
| 741 | + } | |
| 742 | + } | |
| 743 | + break; | |
| 744 | + } | |
| 745 | + return $options; | |
| 489 | 746 | } |
| 490 | 747 | |
| 491 | 748 | /** |
| 492 | 749 | * Filter Rest Query |