-
Notifications
You must be signed in to change notification settings - Fork 385
/
amp-helper-functions.php
92 lines (83 loc) · 2.5 KB
/
amp-helper-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* AMP Helper Functions
*
* @package AMP
*/
/**
* Retrieves the full AMP-specific permalink for the given post ID.
*
* @since 0.1
*
* @param int $post_id Post ID.
*
* @return string AMP permalink.
*/
function amp_get_permalink( $post_id ) {
/**
* Filters the AMP permalink to short-circuit normal generation.
*
* 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.
*
* @since 0.4
*
* @param false $url Short-circuited URL.
* @param int $post_id Post ID.
*/
$pre_url = apply_filters( 'amp_pre_get_permalink', false, $post_id );
if ( false !== $pre_url ) {
return $pre_url;
}
$parsed_url = wp_parse_url( get_permalink( $post_id ) );
$structure = get_option( 'permalink_structure' );
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' );
}
/**
* Filters AMP permalink.
*
* @since 0.2
*
* @param false $amp_url AMP URL.
* @param int $post_id Post ID.
*/
return apply_filters( 'amp_get_permalink', $amp_url, $post_id );
}
/**
* Determine whether a given post supports AMP.
*
* @since 0.1
* @since 0.6 Returns false when post has meta to disable AMP or when page is homepage or page for posts.
* @see AMP_Post_Type_Support::get_support_errors()
*
* @param WP_Post $post Post.
*
* @return bool Whether the post supports AMP.
*/
function post_supports_amp( $post ) {
return 0 === count( AMP_Post_Type_Support::get_support_errors( $post ) );
}
/**
* Are we currently on an AMP URL?
*
* Note: will always return `false` if called before the `parse_query` hook.
*
* @return bool Whether it is the AMP endpoint.
*/
function is_amp_endpoint() {
if ( 0 === did_action( 'parse_query' ) ) {
_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' );
}
return false !== get_query_var( AMP_QUERY_VAR, false );
}
/**
* Get AMP asset URL.
*
* @param string $file Relative path to file in assets directory.
* @return string URL.
*/
function amp_get_asset_url( $file ) {
return plugins_url( sprintf( 'assets/%s', $file ), AMP__FILE__ );
}