Skip to content

Commit 0b720dd

Browse files
committed
chore(dev): remove legacy block editor code
1 parent 613c8ad commit 0b720dd

5 files changed

Lines changed: 16 additions & 246 deletions

File tree

classes/Visualizer/Gutenberg/Block.php

Lines changed: 1 addition & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,6 @@ public function register_rest_endpoints() {
177177
)
178178
);
179179

180-
register_rest_route(
181-
'visualizer/v' . VISUALIZER_REST_VERSION,
182-
'/update-chart',
183-
array(
184-
'methods' => 'POST',
185-
'callback' => array( $this, 'update_chart_data' ),
186-
'args' => array(
187-
'id' => array(
188-
'sanitize_callback' => 'absint',
189-
),
190-
),
191-
'permission_callback' => function () {
192-
return current_user_can( 'edit_posts' );
193-
},
194-
)
195-
);
196-
197180
}
198181

199182
/**
@@ -348,139 +331,14 @@ public function get_visualizer_data( $post ) {
348331
return $data;
349332
}
350333

351-
/**
352-
* Rest Callback Method
353-
*/
354-
public function update_chart_data( $data ) {
355-
if ( ! current_user_can( 'edit_posts' ) ) {
356-
return false;
357-
}
358-
359-
if ( $data['id'] && ! is_wp_error( $data['id'] ) ) {
360-
if ( get_post_type( $data['id'] ) !== Visualizer_Plugin::CPT_VISUALIZER ) {
361-
return new WP_Error( 'invalid_post_type', 'Invalid post type.' );
362-
}
363-
$chart_type = sanitize_text_field( $data['visualizer-chart-type'] );
364-
$source_type = sanitize_text_field( $data['visualizer-source'] );
365-
$default_data = (int) $data['visualizer-default-data'];
366-
$series_data = map_deep( $data['visualizer-series'], array( $this, 'sanitize_value' ) );
367-
$settings_data = map_deep( $data['visualizer-settings'], array( $this, 'sanitize_value' ) );
368-
369-
update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_TYPE, $chart_type );
370-
update_post_meta( $data['id'], Visualizer_Plugin::CF_SOURCE, $source_type );
371-
update_post_meta( $data['id'], Visualizer_Plugin::CF_DEFAULT_DATA, $default_data );
372-
update_post_meta( $data['id'], Visualizer_Plugin::CF_SERIES, $series_data );
373-
update_post_meta( $data['id'], Visualizer_Plugin::CF_SETTINGS, $settings_data );
374-
375-
if ( $data['visualizer-chart-url'] && $data['visualizer-chart-schedule'] >= 0 ) {
376-
$chart_url = esc_url_raw( $data['visualizer-chart-url'] );
377-
$chart_schedule = intval( $data['visualizer-chart-schedule'] );
378-
update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $chart_url );
379-
apply_filters( 'visualizer_pro_chart_schedule', $data['id'], $chart_url, $chart_schedule );
380-
} else {
381-
delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL );
382-
apply_filters( 'visualizer_pro_remove_schedule', $data['id'] );
383-
}
384-
385-
// let's check if this is not an external db chart
386-
// as there is no support for that in the block editor interface
387-
$external_params = get_post_meta( $data['id'], Visualizer_Plugin::CF_REMOTE_DB_PARAMS, true );
388-
if ( empty( $external_params ) ) {
389-
if ( $source_type === 'Visualizer_Source_Query' ) {
390-
$db_schedule = intval( $data['visualizer-db-schedule'] );
391-
$db_query = $data['visualizer-db-query'];
392-
update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE, $db_schedule );
393-
update_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY, stripslashes( $db_query ) );
394-
} else {
395-
delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_SCHEDULE );
396-
delete_post_meta( $data['id'], Visualizer_Plugin::CF_DB_QUERY );
397-
}
398-
399-
if ( 'Visualizer_Source_Csv_Remote' === $source_type ) {
400-
$schedule_url = esc_url_raw( $data['visualizer-chart-url'] );
401-
$schedule_id = intval( $data['visualizer-chart-schedule'] );
402-
update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL, $schedule_url );
403-
update_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_SCHEDULE, $schedule_id );
404-
} else {
405-
delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_URL );
406-
delete_post_meta( $data['id'], Visualizer_Plugin::CF_CHART_SCHEDULE );
407-
}
408-
}
409-
410-
if ( $source_type === 'Visualizer_Source_Json' ) {
411-
$json_schedule = intval( $data['visualizer-json-schedule'] );
412-
$json_url = esc_url_raw( $data['visualizer-json-url'] );
413-
$json_headers = esc_url_raw( $data['visualizer-json-headers'] );
414-
$json_root = sanitize_text_field( $data['visualizer-json-root'] );
415-
$json_paging = sanitize_text_field( $data['visualizer-json-paging'] );
416-
417-
update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE, $json_schedule );
418-
update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL, $json_url );
419-
update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS, $json_headers );
420-
update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT, $json_root );
421-
422-
if ( ! empty( $json_paging ) ) {
423-
update_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING, $json_paging );
424-
} else {
425-
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
426-
}
427-
} else {
428-
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_SCHEDULE );
429-
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_URL );
430-
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_HEADERS );
431-
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_ROOT );
432-
delete_post_meta( $data['id'], Visualizer_Plugin::CF_JSON_PAGING );
433-
}
434-
435-
if ( Visualizer_Module::is_pro() ) {
436-
$permissions_data = map_deep( $data['visualizer-permissions'], array( $this, 'sanitize_value' ) );
437-
update_post_meta( $data['id'], Visualizer_Pro::CF_PERMISSIONS, $permissions_data );
438-
}
439-
440-
if ( $data['visualizer-chart-url'] ) {
441-
$chart_url = esc_url_raw( $data['visualizer-chart-url'] );
442-
$content['source'] = $chart_url;
443-
$content['data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
444-
} else {
445-
$content = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
446-
}
447-
448-
$chart = array(
449-
'ID' => $data['id'],
450-
'post_content' => serialize( $content ),
451-
);
452-
453-
wp_update_post( $chart );
454-
455-
// Clear existing chart cache.
456-
$cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $data['id'];
457-
if ( get_transient( $cache_key ) ) {
458-
delete_transient( $cache_key );
459-
}
460-
461-
$revisions = wp_get_post_revisions( $data['id'], array( 'order' => 'ASC' ) );
462-
463-
if ( count( $revisions ) > 1 ) {
464-
$revision_ids = array_keys( $revisions );
465-
466-
// delete all revisions.
467-
foreach ( $revision_ids as $id ) {
468-
wp_delete_post_revision( $id );
469-
}
470-
}
471-
472-
return new \WP_REST_Response( array( 'success' => sprintf( 'Chart updated' ) ) );
473-
}
474-
}
475-
476334
/**
477335
* Format chart data.
478336
*
479337
* Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure.
480338
*/
481339
public function format_chart_data( $data, $series ) {
482340
foreach ( $series as $i => $row ) {
483-
// if no value exists for the seires, then add null
341+
// if no value exists for the series, then add null
484342
if ( ! isset( $series[ $i ] ) ) {
485343
$series[ $i ] = null;
486344
}
@@ -556,17 +414,4 @@ public function add_rest_query_vars( $args, \WP_REST_Request $request ) {
556414
return $args;
557415
}
558416

559-
/**
560-
* Sanitize value.
561-
*
562-
* @param mixed $value The value to sanitize.
563-
* @return mixed Sanitized value.
564-
*/
565-
public function sanitize_value( $value ) {
566-
if ( is_string( $value ) ) {
567-
return sanitize_text_field( $value );
568-
}
569-
570-
return $value;
571-
}
572417
}

classes/Visualizer/Gutenberg/build/block.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

classes/Visualizer/Gutenberg/src/Components/ChartSelect.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,9 @@ class ChartSelect extends Component {
114114
}
115115
);
116116
const updateChartState = async() => {
117-
await this.setState({
118-
isLoading: 'getChart'
119-
});
120-
121117
let result = await apiFetch({ path: `wp/v2/visualizer/${chartId}` });
122118
await this.props.editSettings( result['chart_data']['visualizer-settings']);
123119
await this.props.getChartData( chartId );
124-
125-
await this.setState({ route: 'home', chart: result['chart_data'], isModified: true, isLoading: true });
126120
};
127121
// eslint-disable-next-line camelcase
128122
window.send_to_editor = function() {

classes/Visualizer/Gutenberg/src/Editor.js

Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ const { __ } = wp.i18n;
1616

1717
const { apiFetch } = wp;
1818

19-
const {
20-
Component,
21-
Fragment
22-
} = wp.element;
19+
const { Component } = wp.element;
2320

2421
const {
2522
Button,
@@ -38,7 +35,6 @@ class Editor extends Component {
3835
this.editChart = this.editChart.bind( this );
3936
this.editSettings = this.editSettings.bind( this );
4037
this.getChartData = this.getChartData.bind( this );
41-
this.updateChart = this.updateChart.bind( this );
4238
this.createChart = this.createChart.bind( this );
4339

4440
this.state = {
@@ -53,7 +49,6 @@ class Editor extends Component {
5349
*/
5450
route: ( this.props.attributes.route ? this.props.attributes.route : 'home' ),
5551
chart: null,
56-
isModified: false,
5752
isLoading: false
5853
};
5954
}
@@ -95,8 +90,7 @@ class Editor extends Component {
9590
this.setState({
9691
route: 'chartSelect',
9792
chart: chartDataRequest.result['chart_data'],
98-
isLoading: true,
99-
isModified: true
93+
isLoading: true
10094
});
10195

10296
this.props.setAttributes({
@@ -118,8 +112,7 @@ class Editor extends Component {
118112
}
119113
chart['visualizer-settings'] = settings;
120114
this.setState({
121-
chart,
122-
isModified: true
115+
chart
123116
});
124117
}
125118

@@ -143,49 +136,6 @@ class Editor extends Component {
143136
});
144137
}
145138

146-
updateChart() {
147-
this.setState({ isLoading: 'updateChart' });
148-
149-
const data = this.state.chart;
150-
151-
let dataChartStylingOption = 'series';
152-
153-
if ( 'pie' === data['visualizer-chart-type']) {
154-
dataChartStylingOption = 'slices';
155-
}
156-
157-
// no series for bubble and timeline charts.
158-
if (
159-
undefined !== data['visualizer-settings'][dataChartStylingOption] &&
160-
-1 >= [ 'bubble', 'timeline' ].indexOf( data['visualizer-chart-type'])
161-
) {
162-
Object.keys( data['visualizer-settings'][dataChartStylingOption])
163-
.map( i => {
164-
if ( data['visualizer-settings'][dataChartStylingOption][i] !== undefined ) {
165-
if ( data['visualizer-settings'][dataChartStylingOption][i].temp !== undefined ) {
166-
delete data['visualizer-settings'][dataChartStylingOption][i].temp;
167-
}
168-
}
169-
}
170-
);
171-
}
172-
173-
apiRequest({ path: `/visualizer/v1/update-chart?id=${ this.props.attributes.id }`, method: 'POST', data: data }).then(
174-
( data ) => {
175-
176-
this.setState({
177-
isLoading: false,
178-
isModified: false
179-
});
180-
181-
return data;
182-
},
183-
( err ) => {
184-
return err;
185-
}
186-
);
187-
}
188-
189139
/**
190140
* Create a new chart via popup.
191141
*
@@ -346,33 +296,17 @@ class Editor extends Component {
346296
</Button>
347297

348298
{ 'chartSelect' === this.state.route &&
349-
<Fragment>
350-
351-
{ false === this.state.isModified ?
352-
<Button
353-
variant="secondary"
354-
isLarge
355-
className="visualizer-bttn-done"
356-
onClick={ () => {
357-
this.setState({ route: 'renderChart', isModified: true });
358-
this.props.setAttributes({ route: 'renderChart' });
359-
} }
360-
>
361-
{ __( 'Done' ) }
362-
</Button> :
363-
<Button
364-
isPrimary
365-
isLarge
366-
className="visualizer-bttn-save"
367-
isBusy={ 'updateChart' === this.state.isLoading }
368-
disabled={ 'updateChart' === this.state.isLoading }
369-
onClick={ this.updateChart }
370-
>
371-
{ __( 'Save' ) }
372-
</Button>
373-
}
374-
375-
</Fragment>
299+
<Button
300+
variant="secondary"
301+
isLarge
302+
className="visualizer-bttn-done"
303+
onClick={ () => {
304+
this.setState({ route: 'renderChart' });
305+
this.props.setAttributes({ route: 'renderChart' });
306+
} }
307+
>
308+
{ __( 'Done' ) }
309+
</Button>
376310
}
377311

378312
</ButtonGroup>

tests/e2e/specs/gutenberg-editor.spec.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ test.describe( 'Charts with Gutenberg Editor', () => {
4141
await page.frameLocator('iframe').getByRole('button', { name: 'Next' }).click();
4242
await page.frameLocator('iframe').getByRole('button', { name: 'Create Chart' }).click();
4343

44-
await expect( page.getByRole('button', { name: 'Save', exact: true }) ).toBeVisible();
45-
await page.getByRole('button', { name: 'Save', exact: true }).click();
4644
await expect( page.getByRole('button', { name: 'Done' }) ).toBeVisible();
4745
await page.getByRole('button', { name: 'Done' }).click();
4846

@@ -62,7 +60,6 @@ test.describe( 'Charts with Gutenberg Editor', () => {
6260

6361
// Check if it was inserted correctly then enter view mode for the block.
6462
expect( page.getByLabel('Block: Visualizer Chart').getByText('Visualizer') ).not.toBeNull();
65-
await page.getByRole('button', { name: 'Save', exact: true }).click();
6663
await page.getByRole('button', { name: 'Done' }).click();
6764

6865
// Check if the Chart did not crash the editor.

0 commit comments

Comments
 (0)