diff --git a/classes/Visualizer/Module/Frontend.php b/classes/Visualizer/Module/Frontend.php index 9d589da5..4dd63de8 100644 --- a/classes/Visualizer/Module/Frontend.php +++ b/classes/Visualizer/Module/Frontend.php @@ -475,9 +475,12 @@ public function renderChart( $atts ) { $key = $array[0]; $mime = end( $array ); } + if ( ! isset( $actions[ $key ] ) ) { + continue; + } $label = $actions[ $key ]['label']; $title = $actions[ $key ]['title']; - $actions_div .= sprintf( '_getDataAs( $atts['id'], 'csv' ); diff --git a/tests/test-frontend-actions.php b/tests/test-frontend-actions.php new file mode 100644 index 00000000..34d49f51 --- /dev/null +++ b/tests/test-frontend-actions.php @@ -0,0 +1,69 @@ +post->create( + array( + 'post_type' => Visualizer_Plugin::CPT_VISUALIZER, + 'post_content' => serialize( array( array( 'a', 1 ) ) ), + ) + ); + update_post_meta( $id, Visualizer_Plugin::CF_CHART_TYPE, 'pie' ); + update_post_meta( $id, Visualizer_Plugin::CF_SERIES, array( array( 'label' => 'x', 'type' => 'string' ), array( 'label' => 'y', 'type' => 'number' ) ) ); + update_post_meta( $id, Visualizer_Plugin::CF_SETTINGS, array( 'actions' => $actions ) ); + + return do_shortcode( '[visualizer id="' . $id . '"]' ); + } + + /** + * The part after the action name stays inside its attribute. + */ + public function test_action_mime_cannot_add_an_attribute() { + $markup = $this->render_chart_with_actions( array( 'print;x" onfocus="alert(1)' ) ); + + $this->assertStringContainsString( 'data-visualizer-mime="x" onfocus="alert(1)"', $markup ); + } + + /** + * An action that the plugin does not define gets no button. + */ + public function test_unknown_action_gets_no_button() { + $markup = $this->render_chart_with_actions( array( 'x" onfocus="alert(1)', 'csv;application/csv' ) ); + + $this->assertStringNotContainsString( 'onfocus', $markup ); + $this->assertStringContainsString( 'data-visualizer-type="csv"', $markup ); + } + + /** + * The key and the title of an action that a filter adds stay inside their attributes. + */ + public function test_filtered_action_key_and_title_cannot_add_an_attribute() { + add_filter( + 'visualizer_action_buttons', + function ( $actions ) { + $actions['share" data-x="1'] = array( + 'label' => 'Share', + 'title' => 'Share" onfocus="alert(1)', + ); + return $actions; + } + ); + + $markup = $this->render_chart_with_actions( array( 'share" data-x="1' ) ); + + $this->assertStringContainsString( 'data-visualizer-type="share" data-x="1"', $markup ); + $this->assertStringContainsString( 'title="Share" onfocus="alert(1)"', $markup ); + } +}