Skip to content

Commit

Permalink
Issue #841: Use wp_json_encode in 'on' attribute.
Browse files Browse the repository at this point in the history
At Weston's suggestion.
This is easier to understand.
  • Loading branch information
Ryan Kienstra committed Feb 15, 2018
1 parent ca78258 commit 78f7368
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions includes/embeds/class-amp-playlist-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ public function tracks( $type, $container_id ) {
foreach ( $this->data['tracks'] as $index => $track ) {
$title = $this->get_title( $track );
if ( 'audio' === $type ) {
$on = 'tap:AMP.setState({' . $container_id . ': {selectedSlide: ' . $i . '}})';
$on = 'tap:AMP.setState(' . wp_json_encode( array( $container_id => array( 'selectedSlide' => $i ) ) ) . ')';
$item_class = esc_attr( $i ) . ' == ' . esc_attr( $container_id ) . '.selectedSlide ? "wp-playlist-item wp-playlist-playing" : "wp-playlist-item"';
} elseif ( 'video' === $type ) {
$on = 'tap:AMP.setState({' . $container_id . ': {currentVideo: ' . $index . '}})';
$on = 'tap:AMP.setState(' . wp_json_encode( array( $container_id => array( 'currentVideo' => $index ) ) ) . ')';
$item_class = esc_attr( $index ) . ' == ' . esc_attr( $container_id ) . '.currentVideo ? "wp-playlist-item wp-playlist-playing" : "wp-playlist-item"';
}

Expand Down
8 changes: 4 additions & 4 deletions tests/test-class-amp-playlist-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function test_video_playlist() {
$this->assertContains( $this->file_1, $playlist );
$this->assertContains( $this->file_2, $playlist );
$this->assertContains( '[src]="playlist0[playlist0.currentVideo].videoUrl"', $playlist );
$this->assertContains( 'on="tap:AMP.setState({playlist0: {currentVideo: 0}})"', $playlist );
$this->assertContains( 'on="tap:AMP.setState({"playlist0":{"currentVideo":0}})"', $playlist );
}

/**
Expand Down Expand Up @@ -192,7 +192,7 @@ public function test_audio_playlist() {
$this->assertContains( '<amp-audio', $playlist );
$this->assertContains( $this->file_1, $playlist );
$this->assertContains( $this->file_2, $playlist );
$this->assertContains( 'tap:AMP.setState({ampPlaylistCarousel0: {selectedSlide: 0}})"', $playlist );
$this->assertContains( 'tap:AMP.setState({&quot;ampPlaylistCarousel0&quot;:{&quot;selectedSlide&quot;:0}})"', $playlist );
}

/**
Expand All @@ -205,7 +205,7 @@ public function test_tracks() {
$attr = $this->get_attributes( $type );
$this->instance->data = $this->instance->get_data( $attr );
$container_id = 'fooContainerId0';
$expected_on = 'tap:AMP.setState({' . $container_id . ': {currentVideo: 0}})';
$expected_on = 'tap:AMP.setState({&quot;' . $container_id . '&quot;:{&quot;currentVideo&quot;:0}})';

ob_start();
$this->instance->tracks( $type, $container_id );
Expand All @@ -217,7 +217,7 @@ public function test_tracks() {
$type = 'audio';
$attr = $this->get_attributes( $type );
$this->instance->data = $this->instance->get_data( $attr );
$expected_on = 'tap:AMP.setState({' . $container_id . ': {selectedSlide: 0}})';
$expected_on = 'tap:AMP.setState({&quot;' . $container_id . '&quot;:{&quot;selectedSlide&quot;:0}})';

ob_start();
$this->instance->tracks( $type, $container_id );
Expand Down

0 comments on commit 78f7368

Please sign in to comment.