| @@ -63,141 +63,47 @@ | ||
| 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 | + $blockPath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.js'; | |
| 71 | + $handsontableJS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.js'; | |
| 72 | + $stylePath = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/block.css'; | |
| 73 | + $handsontableCSS = VISUALIZER_ABSURL . 'classes/Visualizer/Gutenberg/build/handsontable.css'; | |
| 71 | 74 | |
| 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; | |
| 75 | + if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) { | |
| 76 | + $version = filemtime( VISUALIZER_ABSPATH . 'classes/Visualizer/Gutenberg/build/block.js' ); | |
| 78 | 77 | } else { |
| 79 | - $asset = array( | |
| 80 | - 'dependencies' => array(), | |
| 81 | - 'version' => $this->version, | |
| 82 | - ); | |
| 78 | + $version = $this->version; | |
| 83 | 79 | } |
| 84 | 80 | |
| 85 | - if ( VISUALIZER_TEST_JS_CUSTOMIZATION ) { | |
| 86 | - $asset['version'] = filemtime( VISUALIZER_ABSPATH . '/classes/Visualizer/Gutenberg/build/index.js' ); | |
| 87 | - } | |
| 81 | + // Enqueue the bundled block JS file | |
| 82 | + wp_enqueue_script( 'handsontable', $handsontableJS ); | |
| 83 | + wp_enqueue_script( 'visualizer-gutenberg-block', $blockPath, array( 'wp-api', 'handsontable', 'visualizer-datatables', 'moment' ), $version, true ); | |
| 88 | 84 | |
| 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 | - } | |
| 85 | + $type = 'community'; | |
| 92 | 86 | |
| 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 ); | |
| 87 | + if ( Visualizer_Module::is_pro() ) { | |
| 88 | + $type = 'pro'; | |
| 89 | + if ( apply_filters( 'visualizer_is_business', false ) ) { | |
| 90 | + $type = 'business'; | |
| 91 | + } | |
| 95 | 92 | } |
| 96 | 93 | |
| 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 ); | |
| 116 | - | |
| 117 | 94 | $translation_array = array( |
| 95 | + 'isPro' => $type, | |
| 96 | + 'proTeaser' => Visualizer_Plugin::PRO_TEASER_URL, | |
| 97 | + 'absurl' => VISUALIZER_ABSURL, | |
| 98 | + 'charts' => Visualizer_Module_Admin::_getChartTypesLocalized(), | |
| 118 | 99 | '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' ), | |
| 124 | 100 | ); |
| 125 | 101 | wp_localize_script( 'visualizer-gutenberg-block', 'visualizerLocalize', $translation_array ); |
| 126 | 102 | |
| 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 | - | |
| 150 | 103 | // Enqueue frontend and editor block styles |
| 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 | - } | |
| 104 | + wp_enqueue_style( 'handsontable', $handsontableCSS ); | |
| 105 | + wp_enqueue_style( 'visualizer-gutenberg-block', $stylePath, array( 'visualizer-datatables' ), $version ); | |
| 200 | 106 | } |
| 201 | 107 | /** |
| 202 | 108 | * Hook server side rendering into render callback |
| 203 | 109 | */ |
| @@ -208,11 +114,8 @@ | ||
| 208 | 114 | 'attributes' => array( |
| 209 | 115 | 'id' => array( |
| 210 | 116 | 'type' => 'number', |
| 211 | 117 | ), |
| 212 | - 'lazy' => array( | |
| 213 | - 'type' => 'string', | |
| 214 | - ), | |
| 215 | 118 | ), |
| 216 | 119 | ) |
| 217 | 120 | ); |
| 218 | 121 | } |
| @@ -219,45 +122,16 @@ | ||
| 219 | 122 | |
| 220 | 123 | /** |
| 221 | 124 | * Gutenberg Block Callback Function |
| 222 | 125 | */ |
| 223 | - public function gutenberg_block_callback( $atts ) { | |
| 224 | - // no id, no fun. | |
| 225 | - if ( ! isset( $atts['id'] ) ) { | |
| 226 | - return ''; | |
| 126 | + public function gutenberg_block_callback( $attr ) { | |
| 127 | + if ( isset( $attr['id'] ) ) { | |
| 128 | + $id = $attr['id']; | |
| 129 | + if ( empty( $id ) || $id === 'none' ) { | |
| 130 | + return ''; // no id = no fun | |
| 131 | + } | |
| 132 | + return '[visualizer id="' . $id . '"]'; | |
| 227 | 133 | } |
| 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 | 134 | } |
| 261 | 135 | |
| 262 | 136 | /** |
| 263 | 137 | * Hook server side rendering into render callback |
| @@ -269,8 +143,59 @@ | ||
| 269 | 143 | array( |
| 270 | 144 | 'get_callback' => array( $this, 'get_visualizer_data' ), |
| 271 | 145 | ) |
| 272 | 146 | ); |
| 147 | + | |
| 148 | + register_rest_route( | |
| 149 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 150 | + '/update-chart', | |
| 151 | + array( | |
| 152 | + 'methods' => 'POST', | |
| 153 | + 'callback' => array( $this, 'update_chart_data' ), | |
| 154 | + 'args' => array( | |
| 155 | + 'id' => array( | |
| 156 | + 'sanitize_callback' => 'absint', | |
| 157 | + ), | |
| 158 | + ), | |
| 159 | + 'permission_callback' => function () { | |
| 160 | + return current_user_can( 'edit_posts' ); | |
| 161 | + }, | |
| 162 | + ) | |
| 163 | + ); | |
| 164 | + | |
| 165 | + register_rest_route( | |
| 166 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 167 | + '/upload-data', | |
| 168 | + array( | |
| 169 | + 'methods' => 'POST', | |
| 170 | + 'callback' => array( $this, 'upload_csv_data' ), | |
| 171 | + 'args' => array( | |
| 172 | + 'url' => array( | |
| 173 | + 'sanitize_callback' => 'esc_url_raw', | |
| 174 | + ), | |
| 175 | + ), | |
| 176 | + 'permission_callback' => function () { | |
| 177 | + return current_user_can( 'edit_posts' ); | |
| 178 | + }, | |
| 179 | + ) | |
| 180 | + ); | |
| 181 | + | |
| 182 | + register_rest_route( | |
| 183 | + 'visualizer/v' . VISUALIZER_REST_VERSION, | |
| 184 | + '/get-permission-data', | |
| 185 | + array( | |
| 186 | + 'methods' => 'GET', | |
| 187 | + 'callback' => array( $this, 'get_permission_data' ), | |
| 188 | + 'args' => array( | |
| 189 | + 'type' => array( | |
| 190 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 191 | + ), | |
| 192 | + ), | |
| 193 | + 'permission_callback' => function () { | |
| 194 | + return current_user_can( 'edit_posts' ); | |
| 195 | + }, | |
| 196 | + ) | |
| 197 | + ); | |
| 273 | 198 | } |
| 274 | 199 | |
| 275 | 200 | /** |
| 276 | 201 | * Get Post Meta Fields |
| @@ -275,9 +200,9 @@ | ||
| 275 | 200 | /** |
| 276 | 201 | * Get Post Meta Fields |
| 277 | 202 | */ |
| 278 | 203 | public function get_visualizer_data( $post ) { |
| 279 | - if ( ! Visualizer_Module::can_edit_chart( $post['id'] ) ) { | |
| 204 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 280 | 205 | return false; |
| 281 | 206 | } |
| 282 | 207 | |
| 283 | 208 | $data = array(); |
| @@ -284,13 +209,9 @@ | ||
| 284 | 209 | $post_id = $post['id']; |
| 285 | 210 | |
| 286 | 211 | $data['visualizer-chart-type'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 287 | 212 | |
| 288 | - $library = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); | |
| 289 | - $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 | - } | |
| 213 | + $data['visualizer-chart-library'] = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); | |
| 293 | 214 | |
| 294 | 215 | $data['visualizer-source'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SOURCE, true ); |
| 295 | 216 | |
| 296 | 217 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| @@ -296,11 +217,9 @@ | ||
| 296 | 217 | $data['visualizer-default-data'] = get_post_meta( $post_id, Visualizer_Plugin::CF_DEFAULT_DATA, true ); |
| 297 | 218 | |
| 298 | 219 | // faetch and update settings |
| 299 | 220 | $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 | - } | |
| 221 | + | |
| 303 | 222 | // handle series filter hooks |
| 304 | 223 | $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 | 224 | |
| 306 | 225 | // handle settings filter hooks |
| @@ -306,136 +225,99 @@ | ||
| 306 | 225 | // handle settings filter hooks |
| 307 | 226 | $data['visualizer-settings'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, $data['visualizer-settings'], $post_id, $data['visualizer-chart-type'] ); |
| 308 | 227 | |
| 309 | 228 | // 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'] ); | |
| 229 | + $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 | 230 | |
| 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 | - } | |
| 231 | + $import = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_URL, true ); | |
| 317 | 232 | |
| 318 | - if ( ! isset( $data['visualizer-settings']['hAxis']['format'] ) ) { | |
| 319 | - $data['visualizer-settings']['hAxis']['format'] = ''; | |
| 233 | + $schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true ); | |
| 234 | + | |
| 235 | + if ( ! empty( $import ) && ! empty( $schedule ) ) { | |
| 236 | + $data['visualizer-chart-url'] = $import; | |
| 237 | + $data['visualizer-chart-schedule'] = $schedule; | |
| 320 | 238 | } |
| 321 | 239 | |
| 322 | - $data['visualizer-data-exploded'] = ''; | |
| 323 | - // handle annotations for google charts | |
| 324 | - if ( 'GoogleCharts' === $library ) { | |
| 325 | - // this will contain the data of both axis. | |
| 326 | - $settings = $data['visualizer-settings']; | |
| 327 | - // this will contain data only of Y axis. | |
| 328 | - $series = $data['visualizer-series']; | |
| 329 | - $annotations = array(); | |
| 330 | - if ( isset( $settings['series'] ) ) { | |
| 331 | - foreach ( $settings['series'] as $index => $serie ) { | |
| 332 | - // skip X axis data. | |
| 333 | - if ( $index === 0 ) { | |
| 334 | - continue; | |
| 335 | - } | |
| 336 | - if ( ! empty( $serie['role'] ) ) { | |
| 337 | - // this series is some kind of annotation, so let's collect its index. | |
| 338 | - // the index will be +1 because the X axis value is index 0, which is being ignored. | |
| 339 | - $annotations[ 'role' . ( intval( $index ) + 1 ) ] = $serie['role']; | |
| 240 | + if ( Visualizer_Module::is_pro() ) { | |
| 241 | + $permissions = get_post_meta( $post_id, Visualizer_PRO::CF_PERMISSIONS, true ); | |
| 340 | 242 | |
| 341 | - if ( isset( $data['visualizer-series'][ intval( $index ) + 1 ] ) ) { | |
| 342 | - $data['visualizer-series'][ intval( $index ) + 1 ]['role'] = $serie['role']; | |
| 343 | - } | |
| 344 | - } | |
| 345 | - } | |
| 243 | + if ( ! empty( $permissions ) ) { | |
| 244 | + $data['visualizer-permissions'] = $permissions; | |
| 346 | 245 | } |
| 347 | - if ( ! empty( $annotations ) ) { | |
| 348 | - $exploded_data = array(); | |
| 349 | - $series_names = array(); | |
| 350 | - foreach ( $series as $index => $serie ) { | |
| 351 | - // skip X axis data. | |
| 352 | - if ( $index === 0 ) { | |
| 353 | - continue; | |
| 354 | - } | |
| 355 | - if ( array_key_exists( 'role' . $index, $annotations ) ) { | |
| 356 | - $series_names[] = (object) array( 'role' => $annotations[ 'role' . $index ], 'type' => $serie['type'] ); | |
| 357 | - } else { | |
| 358 | - $series_names[] = $serie['label']; | |
| 359 | - } | |
| 360 | - } | |
| 361 | - $exploded_data[] = $series_names; | |
| 362 | - | |
| 363 | - foreach ( $data['visualizer-data'] as $datum ) { | |
| 364 | - // skip X axis data. | |
| 365 | - unset( $datum[0] ); | |
| 366 | - $exploded_data[] = $datum; | |
| 367 | - } | |
| 368 | - $data['visualizer-data-exploded'] = array( $exploded_data ); | |
| 369 | - } | |
| 370 | 246 | } |
| 371 | 247 | |
| 372 | - $import = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_URL, true ); | |
| 248 | + return $data; | |
| 249 | + } | |
| 373 | 250 | |
| 374 | - $schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_CHART_SCHEDULE, true ); | |
| 251 | + /** | |
| 252 | + * Rest Callback Method | |
| 253 | + */ | |
| 254 | + public function update_chart_data( $data ) { | |
| 255 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 256 | + return false; | |
| 257 | + } | |
| 375 | 258 | |
| 376 | - $db_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_SCHEDULE, true ); | |
| 259 | + if ( $data['id'] && ! is_wp_error( $data['id'] ) ) { | |
| 377 | 260 | |
| 378 | - $db_query = get_post_meta( $post_id, Visualizer_Plugin::CF_DB_QUERY, true ); | |
| 261 | + $chart_type = sanitize_text_field( $data['visualizer-chart-type'] ); | |
| 262 | + $source_type = sanitize_text_field( $data['visualizer-source'] ); | |
| 379 | 263 | |
| 380 | - $json_url = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_URL, true ); | |
| 264 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type ); | |
| 265 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type ); | |
| 266 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $data['visualizer-default-data'] ); | |
| 267 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $data['visualizer-series'] ); | |
| 268 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $data['visualizer-settings'] ); | |
| 381 | 269 | |
| 382 | - $json_headers = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_HEADERS, true ); | |
| 270 | + if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] ) { | |
| 271 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 272 | + $chart_schedule = intval( $data['visualizer-chart-schedule'] ); | |
| 273 | + update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url ); | |
| 274 | + apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule ); | |
| 275 | + } else { | |
| 276 | + delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL ); | |
| 277 | + apply_filters( 'visualizer_pro_remove_schedule', $data['id'] ); | |
| 278 | + } | |
| 383 | 279 | |
| 384 | - $json_schedule = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_SCHEDULE, true ); | |
| 280 | + if ( Visualizer_Module::is_pro() ) { | |
| 281 | + update_post_meta( $data['id'], Visualizer_PRO::CF_PERMISSIONS, $data['visualizer-permissions'] ); | |
| 282 | + } | |
| 385 | 283 | |
| 386 | - $json_root = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_ROOT, true ); | |
| 284 | + if ( $data['visualizer-chart-url'] ) { | |
| 285 | + $chart_url = esc_url_raw( $data['visualizer-chart-url'] ); | |
| 286 | + $content['source'] = $chart_url; | |
| 287 | + $content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 288 | + } else { | |
| 289 | + $content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] ); | |
| 290 | + } | |
| 387 | 291 | |
| 388 | - $json_paging = get_post_meta( $post_id, Visualizer_Plugin::CF_JSON_PAGING, true ); | |
| 292 | + $chart = array( | |
| 293 | + 'ID' => $data['id'], | |
| 294 | + 'post_content' => serialize( $content ), | |
| 295 | + ); | |
| 389 | 296 | |
| 390 | - if ( ! empty( $import ) && $schedule >= 0 ) { | |
| 391 | - $data['visualizer-chart-url'] = $import; | |
| 392 | - $data['visualizer-chart-schedule'] = $schedule; | |
| 393 | - } | |
| 297 | + wp_update_post( $chart ); | |
| 394 | 298 | |
| 395 | - if ( ! empty( $db_schedule ) && ! empty( $db_query ) ) { | |
| 396 | - $data['visualizer-db-schedule'] = $db_schedule; | |
| 397 | - $data['visualizer-db-query'] = $db_query; | |
| 398 | - } | |
| 299 | + $revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) ); | |
| 399 | 300 | |
| 400 | - if ( ! empty( $json_url ) ) { | |
| 401 | - $data['visualizer-json-schedule'] = $json_schedule; | |
| 402 | - $data['visualizer-json-url'] = $json_url; | |
| 403 | - $data['visualizer-json-headers'] = $json_headers; | |
| 404 | - $data['visualizer-json-root'] = $json_root; | |
| 301 | + if ( count( $revisions ) > 1 ) { | |
| 302 | + $revision_ids = array_keys( $revisions ); | |
| 405 | 303 | |
| 406 | - if ( Visualizer_Module::is_pro() && ! empty( $json_paging ) ) { | |
| 407 | - $data['visualizer-json-paging'] = $json_paging; | |
| 304 | + // delete all revisions. | |
| 305 | + foreach ( $revision_ids as $id ) { | |
| 306 | + wp_delete_post_revision( $id ); | |
| 307 | + } | |
| 408 | 308 | } |
| 409 | - } | |
| 410 | 309 | |
| 411 | - if ( Visualizer_Module::is_pro() ) { | |
| 412 | - $permissions = get_post_meta( $post_id, Visualizer_Pro::CF_PERMISSIONS, true ); | |
| 413 | - | |
| 414 | - if ( empty( $permissions ) ) { | |
| 415 | - $permissions = array( | |
| 416 | - 'permissions' => array( | |
| 417 | - 'read' => 'all', | |
| 418 | - 'edit' => 'roles', | |
| 419 | - 'edit-specific' => array( 'administrator' ), | |
| 420 | - ), | |
| 421 | - ); | |
| 422 | - } | |
| 423 | - | |
| 424 | - $data['visualizer-permissions'] = $permissions; | |
| 310 | + return new \WP_REST_Response( array( 'success' => sprintf( 'Chart updated' ) ) ); | |
| 425 | 311 | } |
| 426 | - | |
| 427 | - return $data; | |
| 428 | 312 | } |
| 429 | 313 | |
| 430 | 314 | /** |
| 431 | 315 | * 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 | 316 | */ |
| 435 | 317 | public function format_chart_data( $data, $series ) { |
| 436 | 318 | foreach ( $series as $i => $row ) { |
| 437 | - // if no value exists for the series, then add null | |
| 319 | + // if no value exists for the seires, then add null | |
| 438 | 320 | if ( ! isset( $series[ $i ] ) ) { |
| 439 | 321 | $series[ $i ] = null; |
| 440 | 322 | } |
| 441 | 323 | |
| @@ -442,38 +324,39 @@ | ||
| 442 | 324 | if ( is_null( $series[ $i ] ) ) { |
| 443 | 325 | continue; |
| 444 | 326 | } |
| 445 | 327 | |
| 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 ); | |
| 328 | + if ( $row['type'] === 'number' ) { | |
| 329 | + foreach ( $data as $o => $col ) { | |
| 330 | + $data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null ); | |
| 331 | + } | |
| 332 | + } | |
| 333 | + | |
| 334 | + if ( $row['type'] === 'boolean' ) { | |
| 335 | + foreach ( $data as $o => $col ) { | |
| 336 | + $data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_validate( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null; | |
| 337 | + } | |
| 338 | + } | |
| 339 | + | |
| 340 | + if ( $row['type'] === 'timeofday' ) { | |
| 341 | + foreach ( $data as $o => $col ) { | |
| 342 | + $date = new DateTime( '1984-03-16T' . $col[ $i ] ); | |
| 343 | + if ( $date ) { | |
| 344 | + $data[ $o ][ $i ] = array( | |
| 345 | + intval( $date->format( 'H' ) ), | |
| 346 | + intval( $date->format( 'i' ) ), | |
| 347 | + intval( $date->format( 's' ) ), | |
| 348 | + 0, | |
| 349 | + ); | |
| 450 | 350 | } |
| 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; | |
| 351 | + } | |
| 475 | 352 | } |
| 353 | + | |
| 354 | + if ( $row['type'] === 'string' ) { | |
| 355 | + foreach ( $data as $o => $col ) { | |
| 356 | + $data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] ); | |
| 357 | + } | |
| 358 | + } | |
| 476 | 359 | } |
| 477 | 360 | |
| 478 | 361 | return $data; |
| 479 | 362 | } |
| @@ -488,25 +371,89 @@ | ||
| 488 | 371 | return $datum; |
| 489 | 372 | } |
| 490 | 373 | |
| 491 | 374 | /** |
| 375 | + * Handle remote CSV data | |
| 376 | + */ | |
| 377 | + public function upload_csv_data( $data ) { | |
| 378 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 379 | + return false; | |
| 380 | + } | |
| 381 | + | |
| 382 | + if ( $data['url'] && ! is_wp_error( $data['url'] ) && filter_var( $data['url'], FILTER_VALIDATE_URL ) ) { | |
| 383 | + $source = new Visualizer_Source_Csv_Remote( $data['url'] ); | |
| 384 | + if ( $source->fetch() ) { | |
| 385 | + $temp = $source->getData(); | |
| 386 | + if ( is_string( $temp ) && is_array( unserialize( $temp ) ) ) { | |
| 387 | + $content['series'] = $source->getSeries(); | |
| 388 | + $content['data'] = $source->getRawData(); | |
| 389 | + return $content; | |
| 390 | + } else { | |
| 391 | + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 392 | + } | |
| 393 | + } else { | |
| 394 | + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 395 | + } | |
| 396 | + } else { | |
| 397 | + return new \WP_REST_Response( array( 'failed' => sprintf( 'Invalid CSV URL' ) ) ); | |
| 398 | + } | |
| 399 | + } | |
| 400 | + | |
| 401 | + /** | |
| 402 | + * Get permission data | |
| 403 | + */ | |
| 404 | + public function get_permission_data( $data ) { | |
| 405 | + if ( ! current_user_can( 'edit_posts' ) ) { | |
| 406 | + return false; | |
| 407 | + } | |
| 408 | + | |
| 409 | + $options = array(); | |
| 410 | + switch ( $data['type'] ) { | |
| 411 | + case 'users': | |
| 412 | + $query = new WP_User_Query( | |
| 413 | + array( | |
| 414 | + 'number' => 1000, | |
| 415 | + 'orderby' => 'display_name', | |
| 416 | + 'fields' => array( 'ID', 'display_name' ), | |
| 417 | + 'count_total' => false, | |
| 418 | + ) | |
| 419 | + ); | |
| 420 | + $users = $query->get_results(); | |
| 421 | + if ( ! empty( $users ) ) { | |
| 422 | + $i = 0; | |
| 423 | + foreach ( $users as $user ) { | |
| 424 | + $options[ $i ]['value'] = $user->ID; | |
| 425 | + $options[ $i ]['label'] = $user->display_name; | |
| 426 | + $i++; | |
| 427 | + } | |
| 428 | + } | |
| 429 | + break; | |
| 430 | + case 'roles': | |
| 431 | + if ( ! function_exists( 'get_editable_roles' ) ) { | |
| 432 | + require_once ABSPATH . 'wp-admin/includes/user.php'; | |
| 433 | + } | |
| 434 | + $roles = get_editable_roles(); | |
| 435 | + if ( ! empty( $roles ) ) { | |
| 436 | + $i = 0; | |
| 437 | + foreach ( get_editable_roles() as $name => $info ) { | |
| 438 | + $options[ $i ]['value'] = $name; | |
| 439 | + $options[ $i ]['label'] = $name; | |
| 440 | + $i++; | |
| 441 | + } | |
| 442 | + } | |
| 443 | + break; | |
| 444 | + } | |
| 445 | + return $options; | |
| 446 | + } | |
| 447 | + | |
| 448 | + /** | |
| 492 | 449 | * Filter Rest Query |
| 493 | 450 | */ |
| 494 | 451 | public function add_rest_query_vars( $args, \WP_REST_Request $request ) { |
| 495 | 452 | if ( isset( $request['meta_key'] ) && isset( $request['meta_value'] ) ) { |
| 496 | - $args['meta_query'] = array( | |
| 497 | - 'relation' => 'OR', | |
| 498 | - array( | |
| 499 | - 'key' => $request->get_param( 'meta_key' ), | |
| 500 | - 'value' => $request->get_param( 'meta_value' ), | |
| 501 | - 'compare' => '!=', | |
| 502 | - ), | |
| 503 | - array( | |
| 504 | - 'key' => $request->get_param( 'meta_key' ), | |
| 505 | - 'value' => $request->get_param( 'meta_value' ), | |
| 506 | - 'compare' => 'NOT EXISTS', | |
| 507 | - ), | |
| 508 | - ); | |
| 453 | + $args['meta_key'] = $request->get_param( 'meta_key' ); | |
| 454 | + $args['meta_value'] = $request->get_param( 'meta_value' ); | |
| 455 | + $args['meta_compare'] = '!='; | |
| 509 | 456 | } |
| 510 | 457 | return $args; |
| 511 | 458 | } |
| 512 | 459 | } |