Skip to content

Commit c8d56ed

Browse files
authored
Merge pull request #1194 from Automattic/query-var
Let query var always be used and always be 'amp' when theme support is added
2 parents 90acca4 + ab99cd7 commit c8d56ed

File tree

3 files changed

+138
-16
lines changed

3 files changed

+138
-16
lines changed

amp.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ function amp_deactivate() {
113113
function amp_after_setup_theme() {
114114
amp_get_slug(); // Ensure AMP_QUERY_VAR is set.
115115

116+
/**
117+
* Filters whether AMP is enabled on the current site.
118+
*
119+
* Useful if the plugin is network activated and you want to turn it off on select sites.
120+
*
121+
* @since 0.2
122+
*/
116123
if ( false === apply_filters( 'amp_is_enabled', true ) ) {
117124
return;
118125
}
@@ -183,6 +190,7 @@ function amp_force_query_var_value( $query_vars ) {
183190
* If the request is for an AMP page and this is in 'canonical mode,' redirect to the non-AMP page.
184191
* It won't need this plugin's template system, nor the frontend actions like the 'rel' link.
185192
*
193+
* @deprecated This function is not used when 'amp' theme support is added.
186194
* @global WP_Query $wp_query
187195
* @since 0.2
188196
* @return void
@@ -311,6 +319,7 @@ function amp_add_frontend_actions() {
311319
* Add post template actions.
312320
*
313321
* @since 0.2
322+
* @deprecated This function is not used when 'amp' theme support is added.
314323
*/
315324
function amp_add_post_template_actions() {
316325
require_once AMP__DIR__ . '/includes/amp-post-template-actions.php';
@@ -322,15 +331,18 @@ function amp_add_post_template_actions() {
322331
* Add action to do post template rendering at template_redirect action.
323332
*
324333
* @since 0.2
334+
* @since 1.0 The amp_render() function is called at template_redirect action priority 11 instead of priority 10.
335+
* @deprecated This function is not used when 'amp' theme support is added.
325336
*/
326337
function amp_prepare_render() {
327-
add_action( 'template_redirect', 'amp_render' );
338+
add_action( 'template_redirect', 'amp_render', 11 );
328339
}
329340

330341
/**
331342
* Render AMP for queried post.
332343
*
333344
* @since 0.1
345+
* @deprecated This function is not used when 'amp' theme support is added.
334346
*/
335347
function amp_render() {
336348
// Note that queried object is used instead of the ID so that the_preview for the queried post can apply.
@@ -345,6 +357,8 @@ function amp_render() {
345357
* Render AMP post template.
346358
*
347359
* @since 0.5
360+
* @deprecated This function is not used when 'amp' theme support is added.
361+
*
348362
* @param WP_Post|int $post Post.
349363
* @global WP_Query $wp_query
350364
*/
@@ -377,6 +391,8 @@ function amp_render_post( $post ) {
377391
/**
378392
* Fires before rendering a post in AMP.
379393
*
394+
* This action is not triggered when 'amp' theme support is present. Instead, you should use 'template_redirect' action and check if `is_amp_endpoint()`.
395+
*
380396
* @since 0.2
381397
*
382398
* @param int $post_id Post ID.
@@ -412,9 +428,11 @@ function _amp_bootstrap_customizer() {
412428
* Redirects the old AMP URL to the new AMP URL.
413429
* If post slug is updated the amp page with old post slug will be redirected to the updated url.
414430
*
415-
* @param string $link New URL of the post.
431+
* @since 0.5
432+
* @deprecated This function is irrelevant when 'amp' theme support is added.
416433
*
417-
* @return string $link URL to be redirected.
434+
* @param string $link New URL of the post.
435+
* @return string URL to be redirected.
418436
*/
419437
function amp_redirect_old_slug_to_new_url( $link ) {
420438

includes/amp-helper-functions.php

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,26 @@
88
/**
99
* Get the slug used in AMP for the query var, endpoint, and post type support.
1010
*
11-
* The return value can be overridden by previously defining a AMP_QUERY_VAR
11+
* This function always returns 'amp' when 'amp' theme support is present. Otherwise,
12+
* the return value can be overridden by previously defining a AMP_QUERY_VAR
1213
* constant or by adding a 'amp_query_var' filter, but *warning* this ability
1314
* may be deprecated in the future. Normally the slug should be just 'amp'.
1415
*
1516
* @since 0.7
17+
* @since 1.0 The return value is always 'amp' when 'amp' theme support is present, and the 'amp_query_var' filter no longer applies.
18+
*
1619
* @return string Slug used for query var, endpoint, and post type support.
1720
*/
1821
function amp_get_slug() {
22+
if ( current_theme_supports( 'amp' ) ) {
23+
if ( ! defined( 'AMP_QUERY_VAR' ) ) {
24+
define( 'AMP_QUERY_VAR', 'amp' );
25+
} elseif ( 'amp' !== AMP_QUERY_VAR ) {
26+
_doing_it_wrong( __FUNCTION__, esc_html__( 'The AMP_QUERY_VAR constant should only be defined as "amp" when "amp" theme support is present.', 'amp' ), '1.0' );
27+
}
28+
return 'amp';
29+
}
30+
1931
if ( defined( 'AMP_QUERY_VAR' ) ) {
2032
return AMP_QUERY_VAR;
2133
}
@@ -26,6 +38,8 @@ function amp_get_slug() {
2638
* Warning: This filter may become deprecated.
2739
*
2840
* @since 0.3.2
41+
* @since 1.0 This filter does not apply when 'amp' theme support is present.
42+
*
2943
* @param string $query_var The AMP query variable.
3044
*/
3145
$query_var = apply_filters( 'amp_query_var', 'amp' );
@@ -39,19 +53,30 @@ function amp_get_slug() {
3953
* Retrieves the full AMP-specific permalink for the given post ID.
4054
*
4155
* @since 0.1
56+
* @since 1.0 The query var 'amp' is always used exclusively when 'amp' theme support is present; the 'amp_pre_get_permalink' and 'amp_get_permalink' filters do not apply.
4257
*
4358
* @param int $post_id Post ID.
4459
*
4560
* @return string AMP permalink.
4661
*/
4762
function amp_get_permalink( $post_id ) {
4863

64+
// When theme support is present, the plain query var should always be used.
65+
if ( current_theme_supports( 'amp' ) ) {
66+
$permalink = get_permalink( $post_id );
67+
if ( ! amp_is_canonical() ) {
68+
$permalink = add_query_arg( 'amp', '', $permalink );
69+
}
70+
return $permalink;
71+
}
72+
4973
/**
5074
* Filters the AMP permalink to short-circuit normal generation.
5175
*
5276
* Returning a non-false value in this filter will cause the `get_permalink()` to get called and the `amp_get_permalink` filter to not apply.
5377
*
5478
* @since 0.4
79+
* @since 1.0 This filter does not apply when 'amp' theme support is present.
5580
*
5681
* @param false $url Short-circuited URL.
5782
* @param int $post_id Post ID.
@@ -62,22 +87,39 @@ function amp_get_permalink( $post_id ) {
6287
return $pre_url;
6388
}
6489

90+
$permalink = get_permalink( $post_id );
91+
6592
if ( amp_is_canonical() ) {
66-
$amp_url = get_permalink( $post_id );
93+
$amp_url = $permalink;
6794
} else {
68-
$parsed_url = wp_parse_url( get_permalink( $post_id ) );
69-
$structure = get_option( 'permalink_structure' );
70-
if ( empty( $structure ) || ! empty( $parsed_url['query'] ) || is_post_type_hierarchical( get_post_type( $post_id ) ) ) {
71-
$amp_url = add_query_arg( amp_get_slug(), '', get_permalink( $post_id ) );
95+
$parsed_url = wp_parse_url( get_permalink( $post_id ) );
96+
$structure = get_option( 'permalink_structure' );
97+
$use_query_var = (
98+
// If pretty permalinks aren't available, then query var must be used.
99+
empty( $structure )
100+
||
101+
// If there are existing query vars, then always use the amp query var as well.
102+
! empty( $parsed_url['query'] )
103+
||
104+
// If the post type is hierarchical then the /amp/ endpoint isn't available.
105+
is_post_type_hierarchical( get_post_type( $post_id ) )
106+
);
107+
if ( $use_query_var ) {
108+
$amp_url = add_query_arg( amp_get_slug(), '', $permalink );
72109
} else {
73-
$amp_url = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( amp_get_slug(), 'single_amp' );
110+
$amp_url = preg_replace( '/#.*/', '', $permalink );
111+
$amp_url = trailingslashit( $amp_url ) . user_trailingslashit( amp_get_slug(), 'single_amp' );
112+
if ( ! empty( $parsed_url['fragment'] ) ) {
113+
$amp_url .= '#' . $parsed_url['fragment'];
114+
}
74115
}
75116
}
76117

77118
/**
78119
* Filters AMP permalink.
79120
*
80121
* @since 0.2
122+
* @since 1.0 This filter does not apply when 'amp' theme support is present.
81123
*
82124
* @param false $amp_url AMP URL.
83125
* @param int $post_id Post ID.
@@ -166,7 +208,7 @@ function post_supports_amp( $post ) {
166208
/**
167209
* Are we currently on an AMP URL?
168210
*
169-
* Note: will always return `false` if called before the `parse_query` hook.
211+
* @since 1.0 This function can be called before the `parse_query` action because the 'amp' query var is specifically and exclusively used when 'amp' theme support is added.
170212
*
171213
* @return bool Whether it is the AMP endpoint.
172214
*/
@@ -175,15 +217,17 @@ function is_amp_endpoint() {
175217
return false;
176218
}
177219

178-
if ( amp_is_canonical() ) {
220+
// When 'amp' theme support is (or will be added) then these are the conditions that are key to be checked.
221+
if ( amp_is_canonical() || isset( $_GET[ amp_get_slug() ] ) ) { // WPCS: CSRF OK.
179222
return true;
180223
}
181224

182-
if ( 0 === did_action( 'parse_query' ) ) {
183-
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( "is_amp_endpoint() was called before the 'parse_query' hook was called. This function will always return 'false' before the 'parse_query' hook is called.", 'amp' ) ), '0.4.2' );
225+
// Condition for non-theme support when /amp/ endpoint is used.
226+
if ( false !== get_query_var( amp_get_slug(), false ) ) {
227+
return true;
184228
}
185229

186-
return false !== get_query_var( amp_get_slug(), false );
230+
return false;
187231
}
188232

189233
/**

tests/test-amp-helper-functions.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ public function test_amp_get_permalink_without_pretty_permalinks() {
8787
remove_filter( 'amp_pre_get_permalink', array( $this, 'return_example_url' ), 10 );
8888
$url = amp_get_permalink( $published_post );
8989
$this->assertContains( 'current_filter=amp_get_permalink', $url );
90+
remove_filter( 'amp_pre_get_permalink', array( $this, 'return_example_url' ) );
91+
92+
// Now check with theme support added (in paired mode).
93+
add_theme_support( 'amp', array( 'template_dir' => './' ) );
94+
$this->assertStringEndsWith( '&amp', amp_get_permalink( $published_post ) );
95+
$this->assertStringEndsWith( '&amp', amp_get_permalink( $drafted_post ) );
96+
$this->assertStringEndsWith( '&amp', amp_get_permalink( $published_page ) );
97+
add_filter( 'amp_get_permalink', array( $this, 'return_example_url' ), 10, 2 );
98+
$this->assertNotContains( 'current_filter=amp_get_permalink', amp_get_permalink( $published_post ) ); // Filter does not apply.
99+
add_filter( 'amp_pre_get_permalink', array( $this, 'return_example_url' ), 10, 2 );
100+
$this->assertNotContains( 'current_filter=amp_pre_get_permalink', amp_get_permalink( $published_post ) ); // Filter does not apply.
90101
}
91102

92103
/**
@@ -101,6 +112,10 @@ public function test_amp_get_permalink_with_pretty_permalinks() {
101112
$wp_rewrite->init();
102113
$wp_rewrite->flush_rules();
103114

115+
$add_anchor_fragment = function( $url ) {
116+
return $url . '#anchor';
117+
};
118+
104119
$drafted_post = $this->factory()->post->create( array(
105120
'post_name' => 'draft',
106121
'post_status' => 'draft',
@@ -114,11 +129,14 @@ public function test_amp_get_permalink_with_pretty_permalinks() {
114129
'post_status' => 'publish',
115130
'post_type' => 'page',
116131
) );
117-
118132
$this->assertStringEndsWith( '&amp', amp_get_permalink( $drafted_post ) );
119133
$this->assertStringEndsWith( '/amp/', amp_get_permalink( $published_post ) );
120134
$this->assertStringEndsWith( '?amp', amp_get_permalink( $published_page ) );
121135

136+
add_filter( 'post_link', $add_anchor_fragment );
137+
$this->assertStringEndsWith( '/amp/#anchor', amp_get_permalink( $published_post ) );
138+
remove_filter( 'post_link', $add_anchor_fragment );
139+
122140
add_filter( 'amp_pre_get_permalink', array( $this, 'return_example_url' ), 10, 2 );
123141
add_filter( 'amp_get_permalink', array( $this, 'return_example_url' ), 10, 2 );
124142
$url = amp_get_permalink( $published_post );
@@ -128,6 +146,21 @@ public function test_amp_get_permalink_with_pretty_permalinks() {
128146
remove_filter( 'amp_pre_get_permalink', array( $this, 'return_example_url' ), 10 );
129147
$url = amp_get_permalink( $published_post );
130148
$this->assertContains( 'current_filter=amp_get_permalink', $url );
149+
remove_filter( 'amp_get_permalink', array( $this, 'return_example_url' ), 10 );
150+
151+
// Now check with theme support added (in paired mode).
152+
add_theme_support( 'amp', array( 'template_dir' => './' ) );
153+
$this->assertStringEndsWith( '&amp', amp_get_permalink( $drafted_post ) );
154+
$this->assertStringEndsWith( '?amp', amp_get_permalink( $published_post ) );
155+
$this->assertStringEndsWith( '?amp', amp_get_permalink( $published_page ) );
156+
add_filter( 'amp_get_permalink', array( $this, 'return_example_url' ), 10, 2 );
157+
$this->assertNotContains( 'current_filter=amp_get_permalink', amp_get_permalink( $published_post ) ); // Filter does not apply.
158+
add_filter( 'amp_pre_get_permalink', array( $this, 'return_example_url' ), 10, 2 );
159+
$this->assertNotContains( 'current_filter=amp_pre_get_permalink', amp_get_permalink( $published_post ) ); // Filter does not apply.
160+
161+
// Make sure that if permalink has anchor that it is persists.
162+
add_filter( 'post_link', $add_anchor_fragment );
163+
$this->assertStringEndsWith( '/?amp#anchor', amp_get_permalink( $published_post ) );
131164
}
132165

133166
/**
@@ -164,6 +197,33 @@ public function test_amp_remove_endpoint() {
164197
$this->assertEquals( 'https://example.com/foo/?blaz', amp_remove_endpoint( 'https://example.com/foo/amp/?blaz' ) );
165198
}
166199

200+
/**
201+
* Test is_amp_endpoint() function.
202+
*
203+
* @covers \is_amp_endpoint()
204+
*/
205+
public function test_is_amp_endpoint() {
206+
$this->assertFalse( is_amp_endpoint() );
207+
208+
// Legacy query var.
209+
set_query_var( amp_get_slug(), '' );
210+
$this->assertTrue( is_amp_endpoint() );
211+
unset( $GLOBALS['wp_query']->query_vars[ amp_get_slug() ] );
212+
$this->assertFalse( is_amp_endpoint() );
213+
214+
// Paired theme support.
215+
add_theme_support( 'amp', array( 'template_dir' => './' ) );
216+
$_GET['amp'] = '';
217+
$this->assertTrue( is_amp_endpoint() );
218+
unset( $_GET['amp'] );
219+
$this->assertFalse( is_amp_endpoint() );
220+
remove_theme_support( 'amp' );
221+
222+
// Native theme support.
223+
add_theme_support( 'amp' );
224+
$this->assertTrue( is_amp_endpoint() );
225+
}
226+
167227
/**
168228
* Filter calls.
169229
*

0 commit comments

Comments
 (0)