/
amp-post-template-functions.php
169 lines (154 loc) · 4.92 KB
/
amp-post-template-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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* Callbacks for adding content to an AMP template.
*
* @package AMP
*/
/**
* Register hooks.
*
* @internal
*/
function amp_post_template_init_hooks() {
if ( version_compare( strtok( get_bloginfo( 'version' ), '-' ), '5.7', '>=' ) ) {
add_action( 'amp_post_template_head', 'wp_robots' );
} else {
add_action( 'amp_post_template_head', 'noindex' );
}
add_action( 'amp_post_template_head', 'amp_post_template_add_title' );
add_action( 'amp_post_template_head', 'amp_post_template_add_canonical' );
add_action( 'amp_post_template_head', 'amp_post_template_add_fonts' );
add_action( 'amp_post_template_head', 'amp_add_generator_metadata' );
add_action( 'amp_post_template_head', 'wp_generator' );
add_action( 'amp_post_template_head', 'amp_post_template_add_block_styles' );
add_action( 'amp_post_template_head', 'amp_post_template_add_default_styles' );
add_action( 'amp_post_template_css', 'amp_post_template_add_styles', 99 );
add_action( 'amp_post_template_footer', 'amp_post_template_add_analytics_data' );
add_action( 'admin_bar_init', [ AMP_Theme_Support::class, 'init_admin_bar' ] );
add_action( 'amp_post_template_footer', 'wp_admin_bar_render' );
// Printing scripts here is done primarily for the benefit of the admin bar. Note that wp_enqueue_scripts() is not called.
add_action( 'amp_post_template_head', 'wp_print_head_scripts' );
add_action( 'amp_post_template_footer', 'wp_print_footer_scripts' );
}
/**
* Add title.
*
* @internal
*
* @param AMP_Post_Template $amp_template template.
*/
function amp_post_template_add_title( $amp_template ) {
?>
<title><?php echo esc_html( $amp_template->get( 'document_title' ) ); ?></title>
<?php
}
/**
* Add canonical link.
*
* @internal
*
* @param AMP_Post_Template $amp_template Template.
*/
function amp_post_template_add_canonical( $amp_template ) {
?>
<link rel="canonical" href="<?php echo esc_url( $amp_template->get( 'canonical_url' ) ); ?>" />
<?php
}
/**
* Print fonts.
*
* @internal
*
* @param AMP_Post_Template $amp_template Template.
*/
function amp_post_template_add_fonts( $amp_template ) {
$font_urls = $amp_template->get( 'font_urls', [] );
foreach ( $font_urls as $url ) {
printf( '<link rel="stylesheet" href="%s">', esc_url( esc_url( $url ) ) ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
}
}
/**
* Add block styles for core blocks and third-party blocks.
*
* @internal
*
* @since 1.5.0
*/
function amp_post_template_add_block_styles() {
add_theme_support( 'wp-block-styles' );
if ( function_exists( 'wp_common_block_scripts_and_styles' ) ) {
wp_common_block_scripts_and_styles();
}
// Note that this will also print the admin-bar styles since WP_Admin_Bar::initialize() has been called.
wp_styles()->do_items();
}
/**
* Print default styles.
*
* @since 2.0.1
* @internal
*/
function amp_post_template_add_default_styles() {
wp_print_styles( 'amp-default' );
}
/**
* Print styles.
*
* @internal
*
* @param AMP_Post_Template $amp_template Template.
*/
function amp_post_template_add_styles( $amp_template ) {
$stylesheets = $amp_template->get( 'post_amp_stylesheets' );
if ( ! empty( $stylesheets ) ) {
echo '/* Inline stylesheets */' . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo implode( '', $stylesheets ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
$styles = $amp_template->get( 'post_amp_styles' );
if ( ! empty( $styles ) ) {
echo '/* Inline styles */' . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
foreach ( $styles as $selector => $declarations ) {
$declarations = implode( ';', $declarations ) . ';';
printf( '%1$s{%2$s}', $selector, $declarations ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}
/**
* Add custom analytics.
*
* This is currently only used for legacy AMP post templates.
*
* @since 0.5
* @see amp_get_analytics()
* @internal
*
* @param array $analytics Analytics.
* @return array Analytics.
*/
function amp_add_custom_analytics( $analytics = [] ) {
$analytics = amp_get_analytics( $analytics );
/**
* Add amp-analytics tags.
*
* This filter allows you to easily insert any amp-analytics tags without needing much heavy lifting.
* This filter should be used to alter entries for legacy AMP templates.
*
* @since 0.4
*
* @param array $analytics An associative array of the analytics entries we want to output. Each array entry must have a unique key, and the value should be an array with the following keys: `type`, `attributes`, `script_data`. See readme for more details.
* @param WP_Post $post The current post.
*/
$analytics = apply_filters( 'amp_post_template_analytics', $analytics, get_queried_object() );
return $analytics;
}
/**
* Print analytics data.
*
* @internal
*
* @since 0.3.2
*/
function amp_post_template_add_analytics_data() {
$analytics = amp_add_custom_analytics();
amp_print_analytics( $analytics );
}