Skip to content

Commit

Permalink
Always use amp query var in permalinks for hierarchical post types
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Dec 8, 2017
1 parent c5231b0 commit dcf7eb1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function amp_get_permalink( $post_id ) {

$parsed_url = wp_parse_url( get_permalink( $post_id ) );
$structure = get_option( 'permalink_structure' );
if ( empty( $structure ) || ! empty( $parsed_url['query'] ) ) {
if ( empty( $structure ) || ! empty( $parsed_url['query'] ) || is_post_type_hierarchical( get_post_type( $post_id ) ) ) {
$amp_url = add_query_arg( AMP_QUERY_VAR, '', get_permalink( $post_id ) );
} else {
$amp_url = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( AMP_QUERY_VAR, 'single_amp' );
Expand Down
19 changes: 17 additions & 2 deletions tests/test-amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,22 @@ public function test_amp_get_permalink_without_pretty_permalinks() {
$drafted_post = $this->factory()->post->create( array(
'post_name' => 'draft',
'post_status' => 'draft',
'post_type' => 'post',
) );
$published_post = $this->factory()->post->create( array(
'post_name' => 'publish',
'post_status' => 'publish',
'post_type' => 'post',
) );
$published_page = $this->factory()->post->create( array(
'post_name' => 'publish',
'post_status' => 'publish',
'post_type' => 'page',
) );

$this->assertStringEndsWith( '&amp', amp_get_permalink( $published_post ) );
$this->assertStringEndsWith( '&amp', amp_get_permalink( $drafted_post ) );
$this->assertStringEndsWith( '&amp', amp_get_permalink( $published_page ) );

add_filter( 'amp_pre_get_permalink', array( $this, 'return_example_url' ), 10, 2 );
add_filter( 'amp_get_permalink', array( $this, 'return_example_url' ), 10, 2 );
Expand All @@ -61,9 +69,10 @@ public function test_amp_get_permalink_without_pretty_permalinks() {
*/
public function test_amp_get_permalink_with_pretty_permalinks() {
global $wp_rewrite;
$wp_rewrite->use_trailing_slashes = true;
update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
flush_rewrite_rules();
$wp_rewrite->use_trailing_slashes = true;
$wp_rewrite->init();
$wp_rewrite->flush_rules();

$drafted_post = $this->factory()->post->create( array(
'post_name' => 'draft',
Expand All @@ -73,9 +82,15 @@ public function test_amp_get_permalink_with_pretty_permalinks() {
'post_name' => 'publish',
'post_status' => 'publish',
) );
$published_page = $this->factory()->post->create( array(
'post_name' => 'publish',
'post_status' => 'publish',
'post_type' => 'page',
) );

$this->assertStringEndsWith( '&amp', amp_get_permalink( $drafted_post ) );
$this->assertStringEndsWith( '/amp/', amp_get_permalink( $published_post ) );
$this->assertStringEndsWith( '?amp', amp_get_permalink( $published_page ) );

add_filter( 'amp_pre_get_permalink', array( $this, 'return_example_url' ), 10, 2 );
add_filter( 'amp_get_permalink', array( $this, 'return_example_url' ), 10, 2 );
Expand Down

0 comments on commit dcf7eb1

Please sign in to comment.