Skip to content

Commit 2a8a6d4

Browse files
add "permission_callback" and additional validations
1 parent bdd8650 commit 2a8a6d4

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

classes/Visualizer/Module/Frontend.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,24 @@ function endpoint_register() {
108108
'visualizer/v' . VISUALIZER_REST_VERSION,
109109
'/action/(?P<chart>\d+)/(?P<type>.+)/',
110110
array(
111-
'methods' => array( 'GET', 'POST' ),
111+
'methods' => 'GET',
112+
'args' => array(
113+
'chart' => array(
114+
'required' => true,
115+
'sanitize_callback' => function( $param ) {
116+
return is_numeric( $param ) ? $param : null;
117+
},
118+
),
119+
'type' => array(
120+
'required' => true,
121+
'type' => 'string',
122+
'enum' => array_keys( $this->get_actions() ),
123+
),
124+
),
125+
'permission_callback' => function ( WP_REST_Request $request ) {
126+
$chart_id = filter_var( sanitize_text_field( $request->get_param( 'chart' ), FILTER_VALIDATE_INT ) );
127+
return ! empty( $chart_id ) && apply_filters( 'visualizer_pro_show_chart', true, $chart_id );
128+
},
112129
'callback' => array( $this, 'perform_action' ),
113130
)
114131
);
@@ -380,6 +397,7 @@ public function renderChart( $atts ) {
380397
'language' => $this->get_language(),
381398
'map_api_key' => get_option( 'visualizer-map-api-key' ),
382399
'rest_url' => version_compare( $wp_version, '4.7.0', '>=' ) ? rest_url( 'visualizer/v' . VISUALIZER_REST_VERSION . '/action/#id#/#type#/' ) : '',
400+
'wp_nonce' => wp_create_nonce( 'wp_rest' ),
383401
'i10n' => array(
384402
'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
385403
),

js/render-facade.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
e.preventDefault();
3232
$.ajax({
3333
url : v.rest_url.replace('#id#', chart).replace('#type#', type),
34+
beforeSend: function ( xhr ) {
35+
xhr.setRequestHeader( 'X-WP-Nonce', v.wp_nonce );
36+
},
3437
success: function(data) {
3538
if (data && data.data) {
3639
switch(type){

0 commit comments

Comments
 (0)