|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WordPress.com Block Editor |
|
4
|
|
|
* Allow new block editor posts to be composed on WordPress.com. |
|
5
|
|
|
* This is auto-loaded as of Jetpack v7.4 for sites connected to WordPress.com only. |
|
6
|
|
|
* |
|
7
|
|
|
* @package Jetpack |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* WordPress.com Block editor for Jetpack |
|
12
|
|
|
*/ |
|
13
|
|
|
class Jetpack_WPCOM_Block_Editor { |
|
14
|
|
|
/** |
|
15
|
|
|
* ID of the user who signed the nonce. |
|
16
|
|
|
* |
|
17
|
|
|
* @var int |
|
18
|
|
|
*/ |
|
19
|
|
|
private $nonce_user_id; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Singleton |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function init() { |
|
25
|
|
|
static $instance = false; |
|
26
|
|
|
|
|
27
|
|
|
if ( ! $instance ) { |
|
28
|
|
|
$instance = new Jetpack_WPCOM_Block_Editor(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $instance; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Jetpack_WPCOM_Block_Editor constructor. |
|
36
|
|
|
*/ |
|
37
|
|
|
private function __construct() { |
|
38
|
|
|
if ( $this->is_iframed_block_editor() ) { |
|
39
|
|
|
add_action( 'admin_init', array( $this, 'disable_send_frame_options_header' ), 9 ); |
|
40
|
|
|
add_filter( 'admin_body_class', array( $this, 'add_iframed_body_class' ) ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
add_action( 'login_init', array( $this, 'allow_block_editor_login' ), 1 ); |
|
44
|
|
|
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts' ), 9 ); |
|
45
|
|
|
add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles' ) ); |
|
46
|
|
|
add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugins' ) ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Checks if we are embedding the block editor in an iframe in WordPress.com. |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool Whether the current request is from the iframed block editor. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function is_iframed_block_editor() { |
|
55
|
|
|
global $pagenow; |
|
56
|
|
|
|
|
57
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification |
|
58
|
|
|
return ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) && ! empty( $_GET['frame-nonce'] ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Prevents frame options header from firing if this is a whitelisted iframe request. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function disable_send_frame_options_header() { |
|
65
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification |
|
66
|
|
|
if ( $this->framing_allowed( $_GET['frame-nonce'] ) ) { |
|
67
|
|
|
remove_action( 'admin_init', 'send_frame_options_header' ); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Adds custom admin body class if this is a whitelisted iframe request. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $classes Admin body classes. |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function add_iframed_body_class( $classes ) { |
|
78
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification |
|
79
|
|
|
if ( $this->framing_allowed( $_GET['frame-nonce'] ) ) { |
|
80
|
|
|
$classes .= ' is-iframed '; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $classes; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Allows to iframe the login page if a user is logged out |
|
88
|
|
|
* while trying to access the block editor from wordpress.com. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function allow_block_editor_login() { |
|
91
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification |
|
92
|
|
|
if ( empty( $_REQUEST['redirect_to'] ) ) { |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification |
|
97
|
|
|
$query = wp_parse_url( urldecode( $_REQUEST['redirect_to'] ), PHP_URL_QUERY ); |
|
98
|
|
|
$args = wp_parse_args( $query ); |
|
99
|
|
|
|
|
100
|
|
|
// Check nonce and make sure this is a Gutenframe request. |
|
101
|
|
|
if ( ! empty( $args['frame-nonce'] ) && $this->framing_allowed( $args['frame-nonce'] ) ) { |
|
102
|
|
|
|
|
103
|
|
|
// If SSO is active, we'll let WordPress.com handle authentication... |
|
104
|
|
|
if ( Jetpack::is_module_active( 'sso' ) ) { |
|
105
|
|
|
// ...but only if it's not an Atomic site. They already do that. |
|
106
|
|
|
if ( ! jetpack_is_atomic_site() ) { |
|
107
|
|
|
add_filter( 'jetpack_sso_bypass_login_forward_wpcom', '__return_true' ); |
|
108
|
|
|
} |
|
109
|
|
|
} else { |
|
110
|
|
|
$_REQUEST['interim-login'] = true; |
|
111
|
|
|
add_action( 'wp_login', array( $this, 'do_redirect' ) ); |
|
112
|
|
|
add_action( 'login_form', array( $this, 'add_login_html' ) ); |
|
113
|
|
|
add_filter( 'wp_login_errors', array( $this, 'add_login_message' ) ); |
|
114
|
|
|
remove_action( 'login_init', 'send_frame_options_header' ); |
|
115
|
|
|
wp_add_inline_style( 'login', '.interim-login #login{padding-top:8%}' ); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Adds a login message. |
|
122
|
|
|
* |
|
123
|
|
|
* Intended to soften the expectation mismatch of ending up with a login screen rather than the editor. |
|
124
|
|
|
* |
|
125
|
|
|
* @param WP_Error $errors WP Error object. |
|
126
|
|
|
* @return \WP_Error |
|
127
|
|
|
*/ |
|
128
|
|
|
public function add_login_message( $errors ) { |
|
129
|
|
|
$errors->remove( 'expired' ); |
|
|
|
|
|
|
130
|
|
|
$errors->add( 'info', __( 'Before we continue, please log in to your Jetpack site.', 'jetpack' ), 'message' ); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
return $errors; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Maintains the `redirect_to` parameter in login form links. |
|
137
|
|
|
* Adds visual feedback of login in progress. |
|
138
|
|
|
*/ |
|
139
|
|
|
public function add_login_html() { |
|
140
|
|
|
?> |
|
141
|
|
|
<input type="hidden" name="redirect_to" value="<?php echo esc_url( $_REQUEST['redirect_to'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>" /> |
|
142
|
|
|
<script type="application/javascript"> |
|
143
|
|
|
document.getElementById( 'loginform' ).addEventListener( 'submit' , function() { |
|
144
|
|
|
document.getElementById( 'wp-submit' ).setAttribute( 'disabled', 'disabled' ); |
|
145
|
|
|
document.getElementById( 'wp-submit' ).value = '<?php echo esc_js( __( 'Logging In...', 'jetpack' ) ); ?>'; |
|
146
|
|
|
} ); |
|
147
|
|
|
</script> |
|
148
|
|
|
<?php |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Does the redirect to the block editor. |
|
153
|
|
|
*/ |
|
154
|
|
|
public function do_redirect() { |
|
155
|
|
|
wp_safe_redirect( $GLOBALS['redirect_to'] ); |
|
156
|
|
|
exit; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Checks whether this is a whitelisted iframe request. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $nonce Nonce to verify. |
|
163
|
|
|
* @return bool |
|
164
|
|
|
*/ |
|
165
|
|
|
public function framing_allowed( $nonce ) { |
|
166
|
|
|
$verified = $this->verify_frame_nonce( $nonce, 'frame-' . Jetpack_Options::get_option( 'id' ) ); |
|
167
|
|
|
|
|
168
|
|
|
if ( is_wp_error( $verified ) ) { |
|
169
|
|
|
wp_die( $verified ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if ( $verified && ! defined( 'IFRAME_REQUEST' ) ) { |
|
173
|
|
|
define( 'IFRAME_REQUEST', true ); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return (bool) $verified; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Verify that correct nonce was used with time limit. |
|
181
|
|
|
* |
|
182
|
|
|
* The user is given an amount of time to use the token, so therefore, since the |
|
183
|
|
|
* UID and $action remain the same, the independent variable is the time. |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $nonce Nonce that was used in the form to verify. |
|
186
|
|
|
* @param string $action Should give context to what is taking place and be the same when nonce was created. |
|
187
|
|
|
* @return boolean|WP_Error Whether the nonce is valid. |
|
188
|
|
|
*/ |
|
189
|
|
|
public function verify_frame_nonce( $nonce, $action ) { |
|
190
|
|
|
if ( empty( $nonce ) ) { |
|
191
|
|
|
return false; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
list( $expiration, $user_id, $hash ) = explode( ':', $nonce, 3 ); |
|
195
|
|
|
|
|
196
|
|
|
$this->nonce_user_id = (int) $user_id; |
|
197
|
|
|
if ( ! $this->nonce_user_id ) { |
|
198
|
|
|
return false; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$token = Jetpack_Data::get_access_token( $this->nonce_user_id ); |
|
|
|
|
|
|
202
|
|
|
if ( ! $token ) { |
|
203
|
|
|
return false; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/* |
|
207
|
|
|
* Failures must return `false` (blocking the iframe) prior to the |
|
208
|
|
|
* signature verification. |
|
209
|
|
|
*/ |
|
210
|
|
|
|
|
211
|
|
|
add_filter( 'salt', array( $this, 'filter_salt' ), 10, 2 ); |
|
212
|
|
|
$expected_hash = wp_hash( "$expiration|$action|{$this->nonce_user_id}", 'jetpack_frame_nonce' ); |
|
213
|
|
|
remove_filter( 'salt', array( $this, 'filter_salt' ) ); |
|
214
|
|
|
|
|
215
|
|
|
if ( ! hash_equals( $hash, $expected_hash ) ) { |
|
216
|
|
|
return false; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/* |
|
220
|
|
|
* Failures may return `WP_Error` (showing an error in the iframe) after the |
|
221
|
|
|
* signature verification passes. |
|
222
|
|
|
*/ |
|
223
|
|
|
|
|
224
|
|
|
if ( time() > $expiration ) { |
|
225
|
|
|
return new WP_Error( 'nonce_invalid_expired', 'Expired nonce.', array( 'status' => 401 ) ); |
|
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
// Check if it matches the current user, unless they're trying to log in. |
|
229
|
|
|
if ( get_current_user_id() !== $this->nonce_user_id && ! doing_action( 'login_init' ) ) { |
|
230
|
|
|
return new WP_Error( 'nonce_invalid_user_mismatch', 'User ID mismatch.', array( 'status' => 401 ) ); |
|
|
|
|
|
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return true; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Filters the WordPress salt. |
|
238
|
|
|
* |
|
239
|
|
|
* @param string $salt Salt for the given scheme. |
|
240
|
|
|
* @param string $scheme Authentication scheme. |
|
241
|
|
|
* @return string |
|
242
|
|
|
*/ |
|
243
|
|
|
public function filter_salt( $salt, $scheme ) { |
|
244
|
|
|
if ( 'jetpack_frame_nonce' === $scheme ) { |
|
245
|
|
|
$token = Jetpack_Data::get_access_token( $this->nonce_user_id ); |
|
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
if ( $token ) { |
|
248
|
|
|
$salt = $token->secret; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
return $salt; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Enqueue the scripts for the WordPress.com block editor integration. |
|
257
|
|
|
*/ |
|
258
|
|
|
public function enqueue_scripts() { |
|
259
|
|
|
$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; |
|
260
|
|
|
$version = gmdate( 'Ymd' ); |
|
261
|
|
|
|
|
262
|
|
|
$src_common = $debug |
|
263
|
|
|
? '//widgets.wp.com/wpcom-block-editor/common.js?minify=false' |
|
264
|
|
|
: '//widgets.wp.com/wpcom-block-editor/common.min.js'; |
|
265
|
|
|
|
|
266
|
|
|
wp_enqueue_script( |
|
267
|
|
|
'wpcom-block-editor-common', |
|
268
|
|
|
$src_common, |
|
269
|
|
|
array( |
|
270
|
|
|
'jquery', |
|
271
|
|
|
'lodash', |
|
272
|
|
|
'wp-blocks', |
|
273
|
|
|
'wp-compose', |
|
274
|
|
|
'wp-data', |
|
275
|
|
|
'wp-dom-ready', |
|
276
|
|
|
'wp-editor', |
|
277
|
|
|
'wp-nux', |
|
278
|
|
|
'wp-plugins', |
|
279
|
|
|
'wp-polyfill', |
|
280
|
|
|
'wp-rich-text', |
|
281
|
|
|
), |
|
282
|
|
|
$version, |
|
283
|
|
|
true |
|
284
|
|
|
); |
|
285
|
|
|
wp_localize_script( |
|
286
|
|
|
'wpcom-block-editor-common', |
|
287
|
|
|
'wpcomGutenberg', |
|
288
|
|
|
array( |
|
289
|
|
|
'switchToClassic' => array( |
|
290
|
|
|
'isVisible' => $this->is_iframed_block_editor(), |
|
291
|
|
|
'label' => __( 'Switch to Classic Editor', 'jetpack' ), |
|
292
|
|
|
'url' => Jetpack_Calypsoify::getInstance()->get_switch_to_classic_editor_url(), |
|
293
|
|
|
), |
|
294
|
|
|
'richTextToolbar' => array( |
|
295
|
|
|
'justify' => __( 'Justify', 'jetpack' ), |
|
296
|
|
|
'underline' => __( 'Underline', 'jetpack' ), |
|
297
|
|
|
), |
|
298
|
|
|
) |
|
299
|
|
|
); |
|
300
|
|
|
|
|
301
|
|
|
if ( $this->is_iframed_block_editor() ) { |
|
302
|
|
|
$src_calypso_iframe_bridge = $debug |
|
303
|
|
|
? '//widgets.wp.com/wpcom-block-editor/calypso-iframe-bridge-server.js?minify=false' |
|
304
|
|
|
: '//widgets.wp.com/wpcom-block-editor/calypso-iframe-bridge-server.min.js'; |
|
305
|
|
|
|
|
306
|
|
|
wp_enqueue_script( |
|
307
|
|
|
'wpcom-block-editor-calypso-iframe-bridge', |
|
308
|
|
|
$src_calypso_iframe_bridge, |
|
309
|
|
|
array( |
|
310
|
|
|
'calypsoify_wpadminmods_js', |
|
311
|
|
|
'jquery', |
|
312
|
|
|
'lodash', |
|
313
|
|
|
'react', |
|
314
|
|
|
'wp-blocks', |
|
315
|
|
|
'wp-data', |
|
316
|
|
|
'wp-hooks', |
|
317
|
|
|
'wp-polyfill', |
|
318
|
|
|
'wp-tinymce', |
|
319
|
|
|
'wp-url', |
|
320
|
|
|
), |
|
321
|
|
|
$version, |
|
322
|
|
|
true |
|
323
|
|
|
); |
|
324
|
|
|
} |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Enqueue WP.com block editor common styles. |
|
329
|
|
|
*/ |
|
330
|
|
|
public function enqueue_styles() { |
|
331
|
|
|
// Enqueue only for the block editor in WP Admin. |
|
332
|
|
|
global $pagenow; |
|
333
|
|
|
if ( is_admin() && ! in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) ) { |
|
334
|
|
|
return; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
// Enqueue on the front-end only if justified blocks are present. |
|
338
|
|
|
if ( ! is_admin() && ! $this->has_justified_block() ) { |
|
339
|
|
|
return; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; |
|
343
|
|
|
$version = gmdate( 'Ymd' ); |
|
344
|
|
|
|
|
345
|
|
|
$src_styles = $debug |
|
346
|
|
|
? '//widgets.wp.com/wpcom-block-editor/common.css?minify=false' |
|
347
|
|
|
: '//widgets.wp.com/wpcom-block-editor/common.min.css'; |
|
348
|
|
|
wp_enqueue_style( |
|
349
|
|
|
'wpcom-block-editor-styles', |
|
350
|
|
|
$src_styles, |
|
351
|
|
|
array(), |
|
352
|
|
|
$version |
|
353
|
|
|
); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* Determines if the current $post contains a justified paragraph block. |
|
358
|
|
|
* |
|
359
|
|
|
* @return boolean true if justified paragraph is found, false otherwise. |
|
360
|
|
|
*/ |
|
361
|
|
|
public function has_justified_block() { |
|
362
|
|
|
global $post; |
|
363
|
|
|
if ( ! $post instanceof WP_Post ) { |
|
|
|
|
|
|
364
|
|
|
return false; |
|
365
|
|
|
}; |
|
366
|
|
|
|
|
367
|
|
|
if ( ! has_blocks( $post ) ) { |
|
368
|
|
|
return false; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
return false !== strpos( $post->post_content, '<!-- wp:paragraph {"align":"justify"' ); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Register the Tiny MCE plugins for the WordPress.com block editor integration. |
|
376
|
|
|
* |
|
377
|
|
|
* @param array $plugin_array An array of external Tiny MCE plugins. |
|
378
|
|
|
* @return array External TinyMCE plugins. |
|
379
|
|
|
*/ |
|
380
|
|
|
public function add_tinymce_plugins( $plugin_array ) { |
|
381
|
|
|
if ( $this->is_iframed_block_editor() ) { |
|
382
|
|
|
$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; |
|
383
|
|
|
$src_calypso_tinymce = $debug |
|
384
|
|
|
? '//widgets.wp.com/wpcom-block-editor/calypso-tinymce.js?minify=false' |
|
385
|
|
|
: '//widgets.wp.com/wpcom-block-editor/calypso-tinymce.min.js'; |
|
386
|
|
|
|
|
387
|
|
|
$plugin_array['gutenberg-wpcom-iframe-media-modal'] = add_query_arg( |
|
388
|
|
|
'v', |
|
389
|
|
|
gmdate( 'YW' ), |
|
390
|
|
|
$src_calypso_tinymce |
|
391
|
|
|
); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
return $plugin_array; |
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
Jetpack_WPCOM_Block_Editor::init(); |
|
399
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.