-
Notifications
You must be signed in to change notification settings - Fork 384
/
class-amp-post-meta-box.php
200 lines (180 loc) · 4.19 KB
/
class-amp-post-meta-box.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* AMP meta box settings.
*
* @package AMP
* @since 0.6
*/
/**
* Post meta box class.
*
* @since 0.6
*/
class AMP_Post_Meta_Box {
/**
* Assets handle.
*
* @since 0.6
* @var string
*/
const ASSETS_HANDLE = 'amp-post-meta-box';
/**
* The post meta key for whether the post is skipped.
*
* @since 0.6
* @var string
*/
const DISABLED_POST_META_KEY = 'amp_disabled';
/**
* The field name for the enabled/disabled radio buttons.
*
* @since 0.6
* @var string
*/
const STATUS_INPUT_NAME = 'amp_status';
/**
* The nonce name.
*
* @since 0.6
* @var string
*/
const NONCE_NAME = 'amp-status-nonce';
/**
* The nonce action.
*
* @since 0.6
* @var string
*/
const NONCE_ACTION = 'amp-update-status';
/**
* Initialize.
*
* @since 0.6
*/
public function init() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
add_action( 'post_submitbox_misc_actions', array( $this, 'render_status' ) );
add_action( 'save_post', array( $this, 'save_amp_status' ) );
add_filter( 'preview_post_link', array( $this, 'preview_post_link' ) );
}
/**
* Enqueue admin assets.
*
* @since 0.6
*/
public function enqueue_admin_assets() {
$post = get_post();
$screen = get_current_screen();
$validate = (
isset( $screen->base )
&&
'post' === $screen->base
&&
post_type_supports( $post->post_type, AMP_QUERY_VAR )
);
if ( ! $validate ) {
return;
}
// Styles.
wp_enqueue_style(
self::ASSETS_HANDLE,
amp_get_asset_url( 'css/amp-post-meta-box.css' ),
false,
AMP__VERSION
);
// Scripts.
wp_enqueue_script(
self::ASSETS_HANDLE,
amp_get_asset_url( 'js/amp-post-meta-box.js' ),
array( 'jquery' ),
AMP__VERSION
);
wp_add_inline_script( self::ASSETS_HANDLE, sprintf( 'ampPostMetaBox.boot( %s );',
wp_json_encode( array(
'previewLink' => esc_url_raw( add_query_arg( AMP_QUERY_VAR, '', get_preview_post_link( $post ) ) ),
'disabled' => (bool) get_post_meta( $post->ID, self::DISABLED_POST_META_KEY, true ),
'statusInputName' => self::STATUS_INPUT_NAME,
'l10n' => array(
'ampPreviewBtnLabel' => __( 'Preview changes in AMP (opens in new window)', 'amp' ),
),
) )
) );
}
/**
* Render AMP status.
*
* @since 0.6
* @param WP_Post $post Post.
*/
public function render_status( $post ) {
$verify = (
isset( $post->ID )
&&
isset( $post->post_type )
&&
post_type_supports( $post->post_type, AMP_QUERY_VAR )
&&
current_user_can( 'edit_post', $post->ID )
);
if ( true !== $verify ) {
return;
}
// The following variables are used inside amp-status.php template.
$disabled = (bool) get_post_meta( $post->ID, self::DISABLED_POST_META_KEY, true );
$status = $disabled ? 'disabled' : 'enabled';
$labels = array(
'enabled' => __( 'Enabled', 'amp' ),
'disabled' => __( 'Disabled', 'amp' ),
);
include_once AMP__DIR__ . '/templates/admin/amp-status.php';
}
/**
* Save AMP Status.
*
* @since 0.6
* @param int $post_id The Post ID.
*/
public function save_amp_status( $post_id ) {
$verify = (
isset( $_POST[ self::NONCE_NAME ] )
&&
isset( $_POST[ self::STATUS_INPUT_NAME ] )
&&
wp_verify_nonce( sanitize_key( wp_unslash( $_POST[ self::NONCE_NAME ] ) ), self::NONCE_ACTION )
&&
current_user_can( 'edit_post', $post_id )
&&
! wp_is_post_revision( $post_id )
&&
! wp_is_post_autosave( $post_id )
);
if ( true === $verify ) {
if ( 'disabled' === $_POST[ self::STATUS_INPUT_NAME ] ) {
update_post_meta( $post_id, self::DISABLED_POST_META_KEY, true );
} else {
delete_post_meta( $post_id, self::DISABLED_POST_META_KEY );
}
}
}
/**
* Modify post preview link.
*
* Add the AMP query var is the amp-preview flag is set.
*
* @since 0.6
*
* @param string $link The post preview link.
* @return string Preview URL.
*/
public function preview_post_link( $link ) {
$is_amp = (
isset( $_POST['amp-preview'] ) // WPCS: CSRF ok.
&&
'do-preview' === sanitize_key( wp_unslash( $_POST['amp-preview'] ) ) // WPCS: CSRF ok.
);
if ( $is_amp ) {
$link = add_query_arg( AMP_QUERY_VAR, true, $link );
}
return $link;
}
}