| @@ -63,22 +63,28 @@ | ||
| 63 | 63 | add_filter( 'rest_visualizer_query', array( $this, 'add_rest_query_vars' ), 9, 2 ); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | - * Enqueue front end and editor JavaScript and CSS | |
| 67 | + * Enqueue Gutenberg block assets. | |
| 68 | 68 | */ |
| 69 | 69 | public function enqueue_gutenberg_scripts() { |
| 70 | - global $wp_version, $pagenow; | |
| 70 | + global $pagenow; | |
| 71 | 71 | |
| 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'; | |
| 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 | + } | |
| 76 | 84 | |
| 77 | 85 | if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) { |
| 78 | - $version = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/block.js' ); | |
| 79 | - } else { | |
| 80 | - $version = $this->version; | |
| 86 | + $asset['version'] = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/index.js' ); | |
| 81 | 87 | } |
| 82 | 88 | |
| 83 | 89 | if ( ! wp_script_is( 'visualizer-datatables', 'registered' ) ) { |
| 84 | 90 | wp_register_script( 'visualizer-datatables', VISUALIZER_ABSURL . 'js/lib/datatables.min.js', array( 'jquery-ui-core' ), Visualizer_Plugin::VERSION ); |
| @@ -88,55 +94,109 @@ | ||
| 88 | 94 | wp_register_style( 'visualizer-datatables', VISUALIZER_ABSURL . 'css/lib/datatables.min.css', array(), Visualizer_Plugin::VERSION ); |
| 89 | 95 | } |
| 90 | 96 | |
| 91 | 97 | // Enqueue the bundled block JS file |
| 92 | - wp_enqueue_script( 'handsontable', $handsontableJS ); | |
| 93 | - wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable', 'visualizer-datatables', 'moment', 'lodash' ), $version, true ); | |
| 94 | - | |
| 95 | - $type = 'community'; | |
| 96 | - | |
| 97 | - if ( Visualizer_Module::is_pro() ) { | |
| 98 | - $type = 'pro'; | |
| 99 | - if ( apply_filters( 'visualizer_is_business', false ) ) { | |
| 100 | - $type = 'business'; | |
| 101 | - } | |
| 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'] ); | |
| 102 | 113 | } |
| 114 | + $script_deps = array_values( array_unique( $script_deps ) ); | |
| 115 | + wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, $script_deps, $asset['version'], true ); | |
| 103 | 116 | |
| 104 | - $table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( null, false ); | |
| 105 | - | |
| 106 | 117 | $translation_array = array( |
| 107 | - 'isPro' => $type, | |
| 108 | - 'proTeaser' => tsdk_utmify( Visualizer_Plugin::PRO_TEASER_URL, 'blockupsell'), | |
| 109 | - 'absurl' => VISUALIZER_ABSURL, | |
| 110 | - 'charts' => Visualizer_Module_Admin::_getChartTypesLocalized(), | |
| 111 | 118 | 'adminPage' => menu_page_url( 'visualizer', false ), |
| 112 | - 'sqlTable' => $table_col_mapping, | |
| 113 | - 'chartsPerPage' => defined( 'TI_CYPRESS_TESTING' ) ? 20 : 6, | |
| 114 | - 'proFeaturesLocked' => Visualizer_Module_Admin::proFeaturesLocked(), | |
| 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, | |
| 115 | 121 | 'isFullSiteEditor' => 'site-editor.php' === $pagenow, |
| 122 | + /* translators: %1$s: opening tag, %2$s: closing tag */ | |
| 123 | + 'chartEditUrl' => admin_url( 'admin-ajax.php' ), | |
| 116 | 124 | ); |
| 117 | 125 | wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array ); |
| 118 | 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 | + | |
| 119 | 150 | // Enqueue frontend and editor block styles |
| 120 | - wp_enqueue_style( 'handsontable', $handsontableCSS ); | |
| 121 | - wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $version ); | |
| 151 | + wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $asset['version'] ); | |
| 122 | 152 | |
| 123 | - if ( version_compare( $wp_version, '4.9.0', '>' ) ) { | |
| 124 | - | |
| 125 | - wp_enqueue_code_editor( | |
| 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', | |
| 126 | 187 | array( |
| 127 | - 'type' => 'sql', | |
| 128 | - 'codemirror' => array( | |
| 129 | - 'autofocus' => true, | |
| 130 | - 'lineWrapping' => true, | |
| 131 | - 'dragDrop' => false, | |
| 132 | - 'matchBrackets' => true, | |
| 133 | - 'autoCloseBrackets' => true, | |
| 134 | - 'extraKeys' => array( 'Ctrl-Space' => 'autocomplete' ), | |
| 135 | - 'hintOptions' => array( 'tables' => $table_col_mapping ), | |
| 136 | - ), | |
| 188 | + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), | |
| 189 | + 'nonce' => wp_create_nonce( 'visualizer-ai-builder' ), | |
| 190 | + 'isPro' => Visualizer_Module::is_pro(), | |
| 137 | 191 | ) |
| 138 | 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 | + ); | |
| 139 | 199 | } |
| 140 | 200 | } |
| 141 | 201 | /** |
| 142 | 202 | * Hook server side rendering into render callback |
| @@ -141,10 +201,27 @@ | ||
| 141 | 201 | /** |
| 142 | 202 | * Hook server side rendering into render callback |
| 143 | 203 | */ |
| 144 | 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 | + } | |
| 145 | 218 | register_block_type( |
| 146 | 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', | |
| 147 | 224 | 'render_callback' => array( $this, 'gutenberg_block_callback' ), |
| 148 | 225 | 'attributes' => array( |
| 149 | 226 | 'id' => array( |
| 150 | 227 | 'type' => 'number', |
| @@ -180,10 +257,9 @@ | ||
| 180 | 257 | if ( ! $atts['id'] ) { |
| 181 | 258 | return ''; |
| 182 | 259 | } |
| 183 | 260 | |
| 184 | - // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison | |
| 185 | - if ( $atts['lazy'] == -1 || $atts['lazy'] == false ) { | |
| 261 | + if ( $atts['lazy'] === '-1' || $atts['lazy'] === false ) { | |
| 186 | 262 | $atts['lazy'] = 'no'; |
| 187 | 263 | } |
| 188 | 264 | |
| 189 | 265 | // we don't want the chart in the editor lazy-loading. |
| @@ -210,125 +286,8 @@ | ||
| 210 | 286 | array( |
| 211 | 287 | 'get_callback' => array( $this, 'get_visualizer_data' ), |
| 212 | 288 | ) |
| 213 | 289 | ); |
| 214 | - | |
| 215 | - register_rest_route( | |
| 216 | - 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 217 | - '/get-query-data', | |
| 218 | - array( | |
| 219 | - 'methods' => 'GET', | |
| 220 | - 'callback' => array( $this, 'get_query_data' ), | |
| 221 | - 'permission_callback' => function () { | |
| 222 | - return current_user_can( 'edit_posts' ); | |
| 223 | - }, | |
| 224 | - ) | |
| 225 | - ); | |
| 226 | - | |
| 227 | - register_rest_route( | |
| 228 | - 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 229 | - '/get-json-root', | |
| 230 | - array( | |
| 231 | - 'methods' => 'GET', | |
| 232 | - 'callback' => array( $this, 'get_json_root_data' ), | |
| 233 | - 'args' => array( | |
| 234 | - 'url' => array( | |
| 235 | - 'sanitize_callback' => 'esc_url_raw', | |
| 236 | - ), | |
| 237 | - ), | |
| 238 | - 'permission_callback' => function () { | |
| 239 | - return current_user_can( 'edit_posts' ); | |
| 240 | - }, | |
| 241 | - ) | |
| 242 | - ); | |
| 243 | - | |
| 244 | - register_rest_route( | |
| 245 | - 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 246 | - '/get-json-data', | |
| 247 | - array( | |
| 248 | - 'methods' => 'GET', | |
| 249 | - 'callback' => array( $this, 'get_json_data' ), | |
| 250 | - 'args' => array( | |
| 251 | - 'url' => array( | |
| 252 | - 'sanitize_callback' => 'esc_url_raw', | |
| 253 | - ), | |
| 254 | - 'chart' => array( | |
| 255 | - 'sanitize_callback' => 'absint', | |
| 256 | - ), | |
| 257 | - ), | |
| 258 | - 'permission_callback' => function () { | |
| 259 | - return current_user_can( 'edit_posts' ); | |
| 260 | - }, | |
| 261 | - ) | |
| 262 | - ); | |
| 263 | - | |
| 264 | - register_rest_route( | |
| 265 | - 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 266 | - '/set-json-data', | |
| 267 | - array( | |
| 268 | - 'methods' => 'GET', | |
| 269 | - 'callback' => array( $this, 'set_json_data' ), | |
| 270 | - 'args' => array( | |
| 271 | - 'url' => array( | |
| 272 | - 'sanitize_callback' => 'esc_url_raw', | |
| 273 | - ), | |
| 274 | - ), | |
| 275 | - 'permission_callback' => function () { | |
| 276 | - return current_user_can( 'edit_posts' ); | |
| 277 | - }, | |
| 278 | - ) | |
| 279 | - ); | |
| 280 | - | |
| 281 | - register_rest_route( | |
| 282 | - 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 283 | - '/update-chart', | |
| 284 | - array( | |
| 285 | - 'methods' => 'POST', | |
| 286 | - 'callback' => array( $this, 'update_chart_data' ), | |
| 287 | - 'args' => array( | |
| 288 | - 'id' => array( | |
| 289 | - 'sanitize_callback' => 'absint', | |
| 290 | - ), | |
| 291 | - ), | |
| 292 | - 'permission_callback' => function () { | |
| 293 | - return current_user_can( 'edit_posts' ); | |
| 294 | - }, | |
| 295 | - ) | |
| 296 | - ); | |
| 297 | - | |
| 298 | - register_rest_route( | |
| 299 | - 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 300 | - '/upload-data', | |
| 301 | - array( | |
| 302 | - 'methods' => 'POST', | |
| 303 | - 'callback' => array( $this, 'upload_csv_data' ), | |
| 304 | - 'args' => array( | |
| 305 | - 'url' => array( | |
| 306 | - 'sanitize_callback' => 'esc_url_raw', | |
| 307 | - ), | |
| 308 | - ), | |
| 309 | - 'permission_callback' => function () { | |
| 310 | - return current_user_can( 'edit_posts' ); | |
| 311 | - }, | |
| 312 | - ) | |
| 313 | - ); | |
| 314 | - | |
| 315 | - register_rest_route( | |
| 316 | - 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 317 | - '/get-permission-data', | |
| 318 | - array( | |
| 319 | - 'methods' => 'GET', | |
| 320 | - 'callback' => array( $this, 'get_permission_data' ), | |
| 321 | - 'args' => array( | |
| 322 | - 'type' => array( | |
| 323 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 324 | - ), | |
| 325 | - ), | |
| 326 | - 'permission_callback' => function () { | |
| 327 | - return current_user_can( 'edit_posts' ); | |
| 328 | - }, | |
| 329 | - ) | |
| 330 | - ); | |
| 331 | 290 | } |
| 332 | 291 | |
| 333 | 292 | /** |
| 334 | 293 | * Get Post Meta Fields |
| @@ -333,9 +292,9 @@ | ||
| 333 | 292 | /** |
| 334 | 293 | * Get Post Meta Fields |
| 335 | 294 | */ |
| 336 | 295 | public function get_visualizer_data( $post ) { |
| 337 | - if ( ! current_user_can( 'edit_posts' ) ) { | |
| 296 | + if ( ! Visualizer_Module::can_edit_chart( $post['id'] ) ) { | |
| 338 | 297 | return false; |
| 339 | 298 | } |
| 340 | 299 | |
| 341 | 300 | $data = array(); |
| @@ -344,8 +303,11 @@ | ||
| 344 | 303 | $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 345 | 304 | |
| 346 | 305 | $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); |
| 347 | 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 | + } | |
| 348 | 310 | |
| 349 | 311 | $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true ); |
| 350 | 312 | |
| 351 | 313 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| @@ -351,8 +313,11 @@ | ||
| 351 | 313 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| 352 | 314 | |
| 353 | 315 | // faetch and update settings |
| 354 | 316 | $data['visualizer-settings'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SETTINGS, true ); |
| 317 | + if ( ! is_array( $data['visualizer-settings'] ) ) { | |
| 318 | + $data['visualizer-settings'] = array(); | |
| 319 | + } | |
| 355 | 320 | if ( empty( $data['visualizer-settings']['pagination'] ) ) { |
| 356 | 321 | $data['visualizer-settings']['pageSize'] = ''; |
| 357 | 322 | } |
| 358 | 323 | // handle series filter hooks |
| @@ -361,9 +326,9 @@ | ||
| 361 | 326 | // handle settings filter hooks |
| 362 | 327 | $data['visualizer-settings'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $data['visualizer-settings'], $post_id, $data['visualizer-chart-type'] ); |
| 363 | 328 | |
| 364 | 329 | // handle data filter hooks |
| 365 | - $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'] ); | |
| 366 | 331 | |
| 367 | 332 | // we are going to format only for tabular charts, because we are not sure of the effect on others. |
| 368 | 333 | // this is to solve the case where boolean data shows up as all-ticks on gutenberg. |
| 369 | 334 | if ( in_array( $data['visualizer-chart-type'], array( 'tabular' ), true ) ) { |
| @@ -463,16 +428,17 @@ | ||
| 463 | 428 | } |
| 464 | 429 | } |
| 465 | 430 | |
| 466 | 431 | if ( Visualizer_Module::is_pro() ) { |
| 467 | - $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true ); | |
| 432 | + $permissions = get_post_meta( $post_id, Visualizer_Pro::CF_PERMISSIONS, true ); | |
| 468 | 433 | |
| 469 | 434 | if ( empty( $permissions ) ) { |
| 470 | - $permissions = array( 'permissions' => array( | |
| 435 | + $permissions = array( | |
| 436 | + 'permissions' => array( | |
| 471 | 437 | 'read' => 'all', |
| 472 | 438 | 'edit' => 'roles', |
| 473 | 439 | 'edit-specific' => array( 'administrator' ), |
| 474 | - ), | |
| 440 | + ), | |
| 475 | 441 | ); |
| 476 | 442 | } |
| 477 | 443 | |
| 478 | 444 | $data['visualizer-permissions'] = $permissions; |
| @@ -481,225 +447,8 @@ | ||
| 481 | 447 | return $data; |
| 482 | 448 | } |
| 483 | 449 | |
| 484 | 450 | /** |
| 485 | - * Returns the data for the query. | |
| 486 | - * | |
| 487 | - * @access public | |
| 488 | - */ | |
| 489 | - public function get_query_data( $data ) { | |
| 490 | - if ( ! current_user_can( 'edit_posts' ) ) { | |
| 491 | - return false; | |
| 492 | - } | |
| 493 | - | |
| 494 | - $source = new Visualizer_Source_Query( stripslashes( $data['query'] ) ); | |
| 495 | - $html = $source->fetch( true ); | |
| 496 | - $source->fetch( false ); | |
| 497 | - $name = $source->getSourceName(); | |
| 498 | - $series = $source->getSeries(); | |
| 499 | - $data = $source->getRawData(); | |
| 500 | - $error = ''; | |
| 501 | - if ( empty( $html ) ) { | |
| 502 | - $error = $source->get_error(); | |
| 503 | - wp_send_json_error( array( 'msg' => $error ) ); | |
| 504 | - } | |
| 505 | - wp_send_json_success( array( 'table' => $html, 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 506 | - } | |
| 507 | - | |
| 508 | - /** | |
| 509 | - * Returns the JSON root. | |
| 510 | - * | |
| 511 | - * @access public | |
| 512 | - */ | |
| 513 | - public function get_json_root_data( $data ) { | |
| 514 | - if ( ! current_user_can( 'edit_posts' ) ) { | |
| 515 | - return false; | |
| 516 | - } | |
| 517 | - | |
| 518 | - $source = new Visualizer_Source_Json( $data ); | |
| 519 | - | |
| 520 | - $roots = $source->fetchRoots(); | |
| 521 | - if ( empty( $roots ) ) { | |
| 522 | - wp_send_json_error( array( 'msg' => $source->get_error() ) ); | |
| 523 | - } | |
| 524 | - | |
| 525 | - wp_send_json_success( array( 'url' => $data['url'], 'roots' => $roots ) ); | |
| 526 | - } | |
| 527 | - | |
| 528 | - /** | |
| 529 | - * Returns the JSON data. | |
| 530 | - * | |
| 531 | - * @access public | |
| 532 | - */ | |
| 533 | - public function get_json_data( $data ) { | |
| 534 | - if ( ! current_user_can( 'edit_posts' ) ) { | |
| 535 | - return false; | |
| 536 | - } | |
| 537 | - | |
| 538 | - $chart_id = $data['chart']; | |
| 539 | - | |
| 540 | - if ( empty( $chart_id ) ) { | |
| 541 | - wp_die(); | |
| 542 | - } | |
| 543 | - | |
| 544 | - $source = new Visualizer_Source_Json( $data ); | |
| 545 | - $source->fetch(); | |
| 546 | - $table = $source->getRawData(); | |
| 547 | - | |
| 548 | - if ( empty( $table ) ) { | |
| 549 | - wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 550 | - } | |
| 551 | - | |
| 552 | - $table = Visualizer_Render_Layout::show( 'editor-table', $table, $chart_id, 'viz-json-table', false, false ); | |
| 553 | - wp_send_json_success( array( 'table' => $table, 'root' => $data['root'], 'url' => $data['url'], 'paging' => $source->getPaginationElements() ) ); | |
| 554 | - } | |
| 555 | - | |
| 556 | - /** | |
| 557 | - * Set the JSON data. | |
| 558 | - * | |
| 559 | - * @access public | |
| 560 | - */ | |
| 561 | - public function set_json_data( $data ) { | |
| 562 | - if ( ! current_user_can( 'edit_posts' ) ) { | |
| 563 | - return false; | |
| 564 | - } | |
| 565 | - | |
| 566 | - $source = new Visualizer_Source_Json( $data ); | |
| 567 | - | |
| 568 | - $table = $source->fetch(); | |
| 569 | - if ( empty( $table ) ) { | |
| 570 | - wp_send_json_error( array( 'msg' => esc_html__( 'Unable to fetch data from the endpoint. Please try again.', 'visualizer' ) ) ); | |
| 571 | - } | |
| 572 | - | |
| 573 | - $source->fetchFromEditableTable(); | |
| 574 | - $name = $source->getSourceName(); | |
| 575 | - $series = json_encode( $source->getSeries() ); | |
| 576 | - $data = json_encode( $source->getRawData() ); | |
| 577 | - wp_send_json_success( array( 'name' => $name, 'series' => $series, 'data' => $data ) ); | |
| 578 | - } | |
| 579 | - | |
| 580 | - /** | |
| 581 | - * Rest Callback Method | |
| 582 | - */ | |
| 583 | - public function update_chart_data( $data ) { | |
| 584 | - if ( ! current_user_can( 'edit_posts' ) ) { | |
| 585 | - return false; | |
| 586 | - } | |
| 587 | - | |
| 588 | - if ( $data['id'] && ! is_wp_error( $data['id'] ) ) { | |
| 589 | - if ( get_post_type( $data['id'] ) !== Visualizer_Plugin::CPT_VISUALIZER ) { | |
| 590 | - return new WP_Error( 'invalid_post_type', 'Invalid post type.' ); | |
| 591 | - } | |
| 592 | - $chart_type = sanitize_text_field( $data['visualizer-chart-type'] ); | |
| 593 | - $source_type = sanitize_text_field( $data['visualizer-source'] ); | |
| 594 | - | |
| 595 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type ); | |
| 596 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type ); | |
| 597 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] ); | |
| 598 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] ); | |
| 599 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] ); | |
| 600 | - | |
| 601 | - if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] >= 0 ) { | |
| 602 | - $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 603 | - $chart_schedule = intval( $data['visualizer-chart-schedule'] ); | |
| 604 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url ); | |
| 605 | - apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule ); | |
| 606 | - } else { | |
| 607 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); | |
| 608 | - apply_filters( 'visualizer_pro_remove_schedule', $data['id'] ); | |
| 609 | - } | |
| 610 | - | |
| 611 | - // let's check if this is not an external db chart | |
| 612 | - // as there is no support for that in the block editor interface | |
| 613 | - $external_params = get_post_meta( $data['id'], Visualizer_Plugin::CF_REMOTE_DB_PARAMS, true ); | |
| 614 | - if ( empty( $external_params ) ) { | |
| 615 | - if ( $source_type === 'Visualizer_Source_Query' ) { | |
| 616 | - $db_schedule = intval( $data['visualizer-db-schedule'] ); | |
| 617 | - $db_query = $data['visualizer-db-query']; | |
| 618 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule ); | |
| 619 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) ); | |
| 620 | - } else { | |
| 621 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE ); | |
| 622 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY ); | |
| 623 | - } | |
| 624 | - | |
| 625 | - if ( 'Visualizer_Source_Csv_Remote' === $source_type ) { | |
| 626 | - $schedule_url = $data['visualizer-chart-url']; | |
| 627 | - $schedule_id = $data['visualizer-chart-schedule']; | |
| 628 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $schedule_url ); | |
| 629 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_SCHEDULE, $schedule_id ); | |
| 630 | - } else { | |
| 631 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); | |
| 632 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_SCHEDULE ); | |
| 633 | - } | |
| 634 | - } | |
| 635 | - | |
| 636 | - if ( $source_type === 'Visualizer_Source_Json' ) { | |
| 637 | - $json_schedule = intval( $data['visualizer-json-schedule'] ); | |
| 638 | - $json_url = esc_url_raw( $data['visualizer-json-url'] ); | |
| 639 | - $json_headers = esc_url_raw( $data['visualizer-json-headers'] ); | |
| 640 | - $json_root = $data['visualizer-json-root']; | |
| 641 | - $json_paging = $data['visualizer-json-paging']; | |
| 642 | - | |
| 643 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule ); | |
| 644 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url ); | |
| 645 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers ); | |
| 646 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root ); | |
| 647 | - | |
| 648 | - if ( ! empty( $json_paging ) ) { | |
| 649 | - update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging ); | |
| 650 | - } else { | |
| 651 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 652 | - } | |
| 653 | - } else { | |
| 654 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE ); | |
| 655 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL ); | |
| 656 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS ); | |
| 657 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT ); | |
| 658 | - delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING ); | |
| 659 | - } | |
| 660 | - | |
| 661 | - if ( Visualizer_Module::is_pro() ) { | |
| 662 | - update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] ); | |
| 663 | - } | |
| 664 | - | |
| 665 | - if ( $data['visualizer-chart-url'] ) { | |
| 666 | - $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 667 | - $content['source'] = $chart_url; | |
| 668 | - $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 669 | - } else { | |
| 670 | - $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 671 | - } | |
| 672 | - | |
| 673 | - $chart = array( | |
| 674 | - 'ID' => $data['id'], | |
| 675 | - 'post_content' => serialize( $content ), | |
| 676 | - ); | |
| 677 | - | |
| 678 | - wp_update_post( $chart ); | |
| 679 | - | |
| 680 | - // Clear existing chart cache. | |
| 681 | - $cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $data['id']; | |
| 682 | - if ( get_transient( $cache_key ) ) { | |
| 683 | - delete_transient( $cache_key ); | |
| 684 | - } | |
| 685 | - | |
| 686 | - $revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) ); | |
| 687 | - | |
| 688 | - if ( count( $revisions ) > 1 ) { | |
| 689 | - $revision_ids = array_keys( $revisions ); | |
| 690 | - | |
| 691 | - // delete all revisions. | |
| 692 | - foreach ( $revision_ids as $id ) { | |
| 693 | - wp_delete_post_revision( $id ); | |
| 694 | - } | |
| 695 | - } | |
| 696 | - | |
| 697 | - return new \WP_REST_Response( array( 'success' => sprintf( 'Chart updated' ) ) ); | |
| 698 | - } | |
| 699 | - } | |
| 700 | - | |
| 701 | - /** | |
| 702 | 451 | * Format chart data. |
| 703 | 452 | * |
| 704 | 453 | * Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure. |
| 705 | 454 | */ |
| @@ -704,9 +453,9 @@ | ||
| 704 | 453 | * Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure. |
| 705 | 454 | */ |
| 706 | 455 | public function format_chart_data( $data, $series ) { |
| 707 | 456 | foreach ( $series as $i => $row ) { |
| 708 | - // if no value exists for the seires, then add null | |
| 457 | + // if no value exists for the series, then add null | |
| 709 | 458 | if ( ! isset( $series[ $i ] ) ) { |
| 710 | 459 | $series[ $i ] = null; |
| 711 | 460 | } |
| 712 | 461 | |
| @@ -756,86 +505,8 @@ | ||
| 756 | 505 | if ( ! function_exists( 'mb_detect_encoding' ) || mb_detect_encoding( $datum ) !== 'ASCII' ) { |
| 757 | 506 | $datum = \ForceUTF8\Encoding::toUTF8( $datum ); |
| 758 | 507 | } |
| 759 | 508 | return $datum; |
| 760 | - } | |
| 761 | - | |
| 762 | - /** | |
| 763 | - * Handle remote CSV data | |
| 764 | - */ | |
| 765 | - public function upload_csv_data( $data ) { | |
| 766 | - if ( ! current_user_can( 'edit_posts' ) ) { | |
| 767 | - return false; | |
| 768 | - } | |
| 769 | - | |
| 770 | - $remote_data = false; | |
| 771 | - if ( isset( $data['url'] ) && function_exists( 'wp_http_validate_url' ) ) { | |
| 772 | - $remote_data = wp_http_validate_url( $data['url'] ); | |
| 773 | - } | |
| 774 | - if ( false !== $remote_data && ! is_wp_error( $remote_data ) ) { | |
| 775 | - $source = new Visualizer_Source_Csv_Remote( $remote_data ); | |
| 776 | - if ( $source->fetch() ) { | |
| 777 | - $temp = $source->getData(); | |
| 778 | - if ( is_string( $temp ) && is_array( unserialize( $temp ) ) ) { | |
| 779 | - $content['series'] = $source->getSeries(); | |
| 780 | - $content['data'] = $source->getRawData(); | |
| 781 | - return $content; | |
| 782 | - } else { | |
| 783 | - return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 784 | - } | |
| 785 | - } else { | |
| 786 | - return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 787 | - } | |
| 788 | - } else { | |
| 789 | - return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 790 | - } | |
| 791 | - } | |
| 792 | - | |
| 793 | - /** | |
| 794 | - * Get permission data | |
| 795 | - */ | |
| 796 | - public function get_permission_data( $data ) { | |
| 797 | - if ( ! current_user_can( 'edit_posts' ) ) { | |
| 798 | - return false; | |
| 799 | - } | |
| 800 | - | |
| 801 | - $options = array(); | |
| 802 | - switch ( $data['type'] ) { | |
| 803 | - case 'users': | |
| 804 | - $query = new WP_User_Query( | |
| 805 | - array( | |
| 806 | - 'number' => 1000, | |
| 807 | - 'orderby' => 'display_name', | |
| 808 | - 'fields' => array( 'ID', 'display_name' ), | |
| 809 | - 'count_total' => false, | |
| 810 | - ) | |
| 811 | - ); | |
| 812 | - $users = $query->get_results(); | |
| 813 | - if ( ! empty( $users ) ) { | |
| 814 | - $i = 0; | |
| 815 | - foreach ( $users as $user ) { | |
| 816 | - $options[ $i ]['value'] = $user->ID; | |
| 817 | - $options[ $i ]['label'] = $user->display_name; | |
| 818 | - $i++; | |
| 819 | - } | |
| 820 | - } | |
| 821 | - break; | |
| 822 | - case 'roles': | |
| 823 | - if ( ! function_exists( 'get_editable_roles' ) ) { | |
| 824 | - require_once ABSPATH . 'wp-admin/includes/user.php'; | |
| 825 | - } | |
| 826 | - $roles = get_editable_roles(); | |
| 827 | - if ( ! empty( $roles ) ) { | |
| 828 | - $i = 0; | |
| 829 | - foreach ( get_editable_roles() as $name => $info ) { | |
| 830 | - $options[ $i ]['value'] = $name; | |
| 831 | - $options[ $i ]['label'] = $name; | |
| 832 | - $i++; | |
| 833 | - } | |
| 834 | - } | |
| 835 | - break; | |
| 836 | - } | |
| 837 | - return $options; | |
| 838 | 509 | } |
| 839 | 510 | |
| 840 | 511 | /** |
| 841 | 512 | * Filter Rest Query |