暫無描述

class-jetpack-wpcom-block-editor.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 automattic/jetpack
  8. */
  9. use Automattic\Jetpack\Connection\Tokens;
  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. * An array to store auth cookies until we can determine if they should be sent
  22. *
  23. * @var array
  24. */
  25. private $set_cookie_args;
  26. /**
  27. * Singleton
  28. */
  29. public static function init() {
  30. static $instance = false;
  31. if ( ! $instance ) {
  32. $instance = new Jetpack_WPCOM_Block_Editor();
  33. }
  34. return $instance;
  35. }
  36. /**
  37. * Jetpack_WPCOM_Block_Editor constructor.
  38. */
  39. private function __construct() {
  40. $this->set_cookie_args = array();
  41. add_action( 'init', array( $this, 'init_actions' ) );
  42. }
  43. /**
  44. * Add in all hooks.
  45. */
  46. public function init_actions() {
  47. global $wp_version;
  48. // Bail early if Jetpack's block editor extensions are disabled on the site.
  49. /* This filter is documented in class.jetpack-gutenberg.php */
  50. if ( ! apply_filters( 'jetpack_gutenberg', true ) ) {
  51. return;
  52. }
  53. if ( $this->is_iframed_block_editor() ) {
  54. add_action( 'admin_init', array( $this, 'disable_send_frame_options_header' ), 9 );
  55. add_filter( 'admin_body_class', array( $this, 'add_iframed_body_class' ) );
  56. }
  57. require_once __DIR__ . '/functions.editor-type.php';
  58. add_action( 'edit_form_top', 'Jetpack\EditorType\remember_classic_editor' );
  59. add_action( 'login_init', array( $this, 'allow_block_editor_login' ), 1 );
  60. add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ), 9 );
  61. add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) );
  62. add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugins' ) );
  63. // @todo simplify once 5.8 is the minimum supported version.
  64. if ( version_compare( $wp_version, '5.8', '>=' ) ) {
  65. add_filter( 'block_editor_settings_all', 'Jetpack\EditorType\remember_block_editor', 10, 2 );
  66. } else {
  67. add_filter( 'block_editor_settings', 'Jetpack\EditorType\remember_block_editor', 10, 2 );
  68. }
  69. $this->enable_cross_site_auth_cookies();
  70. }
  71. /**
  72. * Checks if we are embedding the block editor in an iframe in WordPress.com.
  73. *
  74. * @return bool Whether the current request is from the iframed block editor.
  75. */
  76. public function is_iframed_block_editor() {
  77. global $pagenow;
  78. // phpcs:ignore WordPress.Security.NonceVerification
  79. return ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) && ! empty( $_GET['frame-nonce'] );
  80. }
  81. /**
  82. * Prevents frame options header from firing if this is a allowed iframe request.
  83. */
  84. public function disable_send_frame_options_header() {
  85. // phpcs:ignore WordPress.Security.NonceVerification
  86. if ( $this->framing_allowed( $_GET['frame-nonce'] ) ) {
  87. remove_action( 'admin_init', 'send_frame_options_header' );
  88. }
  89. }
  90. /**
  91. * Adds custom admin body class if this is a allowed iframe request.
  92. *
  93. * @param string $classes Admin body classes.
  94. * @return string
  95. */
  96. public function add_iframed_body_class( $classes ) {
  97. // phpcs:ignore WordPress.Security.NonceVerification
  98. if ( $this->framing_allowed( $_GET['frame-nonce'] ) ) {
  99. $classes .= ' is-iframed ';
  100. }
  101. return $classes;
  102. }
  103. /**
  104. * Checks to see if cookie can be set in current context. If 3rd party cookie blocking
  105. * is enabled the editor can't load in iFrame, so emiting X-Frame-Options: DENY will
  106. * force the editor to break out of the iFrame.
  107. */
  108. private function check_iframe_cookie_setting() {
  109. if ( ! isset( $_SERVER['QUERY_STRING'] ) || ! strpos( $_SERVER['QUERY_STRING'], 'calypsoify%3D1%26block-editor' ) || isset( $_COOKIE['wordpress_test_cookie'] ) ) {
  110. return;
  111. }
  112. if ( empty( $_GET['calypsoify_cookie_check'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  113. header( 'Location: ' . esc_url_raw( $_SERVER['REQUEST_URI'] . '&calypsoify_cookie_check=true' ) );
  114. exit;
  115. }
  116. header( 'X-Frame-Options: DENY' );
  117. exit;
  118. }
  119. /**
  120. * Allows to iframe the login page if a user is logged out
  121. * while trying to access the block editor from wordpress.com.
  122. */
  123. public function allow_block_editor_login() {
  124. // phpcs:ignore WordPress.Security.NonceVerification
  125. if ( empty( $_REQUEST['redirect_to'] ) ) {
  126. return;
  127. }
  128. $this->check_iframe_cookie_setting();
  129. // phpcs:ignore WordPress.Security.NonceVerification
  130. $query = wp_parse_url( urldecode( $_REQUEST['redirect_to'] ), PHP_URL_QUERY );
  131. $args = wp_parse_args( $query );
  132. // Check nonce and make sure this is a Gutenframe request.
  133. if ( ! empty( $args['frame-nonce'] ) && $this->framing_allowed( $args['frame-nonce'] ) ) {
  134. // If SSO is active, we'll let WordPress.com handle authentication...
  135. if ( Jetpack::is_module_active( 'sso' ) ) {
  136. // ...but only if it's not an Atomic site. They already do that.
  137. if ( ! jetpack_is_atomic_site() ) {
  138. add_filter( 'jetpack_sso_bypass_login_forward_wpcom', '__return_true' );
  139. }
  140. } else {
  141. $_REQUEST['interim-login'] = true;
  142. add_action( 'wp_login', array( $this, 'do_redirect' ) );
  143. add_action( 'login_form', array( $this, 'add_login_html' ) );
  144. add_filter( 'wp_login_errors', array( $this, 'add_login_message' ) );
  145. remove_action( 'login_init', 'send_frame_options_header' );
  146. wp_add_inline_style( 'login', '.interim-login #login{padding-top:8%}' );
  147. }
  148. }
  149. }
  150. /**
  151. * Adds a login message.
  152. *
  153. * Intended to soften the expectation mismatch of ending up with a login screen rather than the editor.
  154. *
  155. * @param WP_Error $errors WP Error object.
  156. * @return \WP_Error
  157. */
  158. public function add_login_message( $errors ) {
  159. $errors->remove( 'expired' );
  160. $errors->add( 'info', __( 'Before we continue, please log in to your Jetpack site.', 'jetpack' ), 'message' );
  161. return $errors;
  162. }
  163. /**
  164. * Maintains the `redirect_to` parameter in login form links.
  165. * Adds visual feedback of login in progress.
  166. */
  167. public function add_login_html() {
  168. ?>
  169. <input type="hidden" name="redirect_to" value="<?php echo esc_url( $_REQUEST['redirect_to'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>" />
  170. <script type="application/javascript">
  171. document.getElementById( 'loginform' ).addEventListener( 'submit' , function() {
  172. document.getElementById( 'wp-submit' ).setAttribute( 'disabled', 'disabled' );
  173. document.getElementById( 'wp-submit' ).value = '<?php echo esc_js( __( 'Logging In...', 'jetpack' ) ); ?>';
  174. } );
  175. </script>
  176. <?php
  177. }
  178. /**
  179. * Does the redirect to the block editor.
  180. */
  181. public function do_redirect() {
  182. wp_safe_redirect( $GLOBALS['redirect_to'] );
  183. exit;
  184. }
  185. /**
  186. * Checks whether this is an allowed iframe request.
  187. *
  188. * @param string $nonce Nonce to verify.
  189. * @return bool
  190. */
  191. public function framing_allowed( $nonce ) {
  192. $verified = $this->verify_frame_nonce( $nonce, 'frame-' . Jetpack_Options::get_option( 'id' ) );
  193. if ( is_wp_error( $verified ) ) {
  194. wp_die( $verified ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  195. }
  196. if ( $verified && ! defined( 'IFRAME_REQUEST' ) ) {
  197. define( 'IFRAME_REQUEST', true );
  198. }
  199. return (bool) $verified;
  200. }
  201. /**
  202. * Verify that correct nonce was used with time limit.
  203. *
  204. * The user is given an amount of time to use the token, so therefore, since the
  205. * UID and $action remain the same, the independent variable is the time.
  206. *
  207. * @param string $nonce Nonce that was used in the form to verify.
  208. * @param string $action Should give context to what is taking place and be the same when nonce was created.
  209. * @return boolean|WP_Error Whether the nonce is valid.
  210. */
  211. public function verify_frame_nonce( $nonce, $action ) {
  212. if ( empty( $nonce ) ) {
  213. return false;
  214. }
  215. list( $expiration, $user_id, $hash ) = explode( ':', $nonce, 3 );
  216. $this->nonce_user_id = (int) $user_id;
  217. if ( ! $this->nonce_user_id ) {
  218. return false;
  219. }
  220. $token = ( new Tokens() )->get_access_token( $this->nonce_user_id );
  221. if ( ! $token ) {
  222. return false;
  223. }
  224. /*
  225. * Failures must return `false` (blocking the iframe) prior to the
  226. * signature verification.
  227. */
  228. add_filter( 'salt', array( $this, 'filter_salt' ), 10, 2 );
  229. $expected_hash = wp_hash( "$expiration|$action|{$this->nonce_user_id}", 'jetpack_frame_nonce' );
  230. remove_filter( 'salt', array( $this, 'filter_salt' ) );
  231. if ( ! hash_equals( $hash, $expected_hash ) ) {
  232. return false;
  233. }
  234. /*
  235. * Failures may return `WP_Error` (showing an error in the iframe) after the
  236. * signature verification passes.
  237. */
  238. if ( time() > $expiration ) {
  239. return new WP_Error( 'nonce_invalid_expired', 'Expired nonce.', array( 'status' => 401 ) );
  240. }
  241. // Check if it matches the current user, unless they're trying to log in.
  242. if ( get_current_user_id() !== $this->nonce_user_id && ! doing_action( 'login_init' ) ) {
  243. return new WP_Error( 'nonce_invalid_user_mismatch', 'User ID mismatch.', array( 'status' => 401 ) );
  244. }
  245. return true;
  246. }
  247. /**
  248. * Filters the WordPress salt.
  249. *
  250. * @param string $salt Salt for the given scheme.
  251. * @param string $scheme Authentication scheme.
  252. * @return string
  253. */
  254. public function filter_salt( $salt, $scheme ) {
  255. if ( 'jetpack_frame_nonce' === $scheme ) {
  256. $token = ( new Tokens() )->get_access_token( $this->nonce_user_id );
  257. if ( $token ) {
  258. $salt = $token->secret;
  259. }
  260. }
  261. return $salt;
  262. }
  263. /**
  264. * Enqueues the WordPress.com block editor integration assets for the editor.
  265. */
  266. public function enqueue_block_editor_assets() {
  267. global $pagenow;
  268. // Bail if we're not in the post editor, but on the widget settings screen.
  269. if ( is_customize_preview() || 'widgets.php' === $pagenow ) {
  270. return;
  271. }
  272. $debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
  273. $version = gmdate( 'Ymd' );
  274. wp_enqueue_script(
  275. 'wpcom-block-editor-default-editor-script',
  276. $debug
  277. ? '//widgets.wp.com/wpcom-block-editor/default.editor.js?minify=false'
  278. : '//widgets.wp.com/wpcom-block-editor/default.editor.min.js',
  279. array(
  280. 'jquery',
  281. 'lodash',
  282. 'wp-annotations',
  283. 'wp-compose',
  284. 'wp-data',
  285. 'wp-editor',
  286. 'wp-element',
  287. 'wp-rich-text',
  288. ),
  289. $version,
  290. true
  291. );
  292. wp_localize_script(
  293. 'wpcom-block-editor-default-editor-script',
  294. 'wpcomGutenberg',
  295. array(
  296. 'richTextToolbar' => array(
  297. 'justify' => __( 'Justify', 'jetpack' ),
  298. 'underline' => __( 'Underline', 'jetpack' ),
  299. ),
  300. )
  301. );
  302. if ( jetpack_is_atomic_site() ) {
  303. wp_enqueue_script(
  304. 'wpcom-block-editor-wpcom-editor-script',
  305. $debug
  306. ? '//widgets.wp.com/wpcom-block-editor/wpcom.editor.js?minify=false'
  307. : '//widgets.wp.com/wpcom-block-editor/wpcom.editor.min.js',
  308. array(
  309. 'lodash',
  310. 'wp-blocks',
  311. 'wp-data',
  312. 'wp-dom-ready',
  313. 'wp-plugins',
  314. ),
  315. $version,
  316. true
  317. );
  318. }
  319. if ( $this->is_iframed_block_editor() ) {
  320. wp_enqueue_script(
  321. 'wpcom-block-editor-calypso-editor-script',
  322. $debug
  323. ? '//widgets.wp.com/wpcom-block-editor/calypso.editor.js?minify=false'
  324. : '//widgets.wp.com/wpcom-block-editor/calypso.editor.min.js',
  325. array(
  326. 'calypsoify_wpadminmods_js',
  327. 'jquery',
  328. 'lodash',
  329. 'react',
  330. 'wp-blocks',
  331. 'wp-data',
  332. 'wp-hooks',
  333. 'wp-tinymce',
  334. 'wp-url',
  335. ),
  336. $version,
  337. true
  338. );
  339. wp_enqueue_style(
  340. 'wpcom-block-editor-calypso-editor-styles',
  341. $debug
  342. ? '//widgets.wp.com/wpcom-block-editor/calypso.editor.css?minify=false'
  343. : '//widgets.wp.com/wpcom-block-editor/calypso.editor.min.css',
  344. array(),
  345. $version
  346. );
  347. }
  348. }
  349. /**
  350. * Enqueues the WordPress.com block editor integration assets for both editor and front-end.
  351. */
  352. public function enqueue_block_assets() {
  353. // These styles are manually copied from //widgets.wp.com/wpcom-block-editor/default.view.css in order to
  354. // improve the performance by avoiding an extra network request to download the CSS file on every page.
  355. wp_add_inline_style( 'wp-block-library', '.has-text-align-justify{text-align:justify;}' );
  356. }
  357. /**
  358. * Determines if the current $post contains a justified paragraph block.
  359. *
  360. * @return boolean true if justified paragraph is found, false otherwise.
  361. */
  362. public function has_justified_block() {
  363. global $post;
  364. if ( ! $post instanceof WP_Post ) {
  365. return false;
  366. };
  367. if ( ! has_blocks( $post ) ) {
  368. return false;
  369. }
  370. return false !== strpos( $post->post_content, '<!-- wp:paragraph {"align":"justify"' );
  371. }
  372. /**
  373. * Register the Tiny MCE plugins for the WordPress.com block editor integration.
  374. *
  375. * @param array $plugin_array An array of external Tiny MCE plugins.
  376. * @return array External TinyMCE plugins.
  377. */
  378. public function add_tinymce_plugins( $plugin_array ) {
  379. if ( $this->is_iframed_block_editor() ) {
  380. $debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
  381. $plugin_array['gutenberg-wpcom-iframe-media-modal'] = add_query_arg(
  382. 'v',
  383. gmdate( 'YW' ),
  384. $debug
  385. ? '//widgets.wp.com/wpcom-block-editor/calypso.tinymce.js?minify=false'
  386. : '//widgets.wp.com/wpcom-block-editor/calypso.tinymce.min.js'
  387. );
  388. }
  389. return $plugin_array;
  390. }
  391. /**
  392. * Ensures the authentication cookies are designated for cross-site access.
  393. */
  394. private function enable_cross_site_auth_cookies() {
  395. /**
  396. * Allow plugins to disable the cross-site auth cookies.
  397. *
  398. * @since 8.1.1
  399. *
  400. * @param false bool Whether auth cookies should be disabled for cross-site access. False by default.
  401. */
  402. if ( apply_filters( 'jetpack_disable_cross_site_auth_cookies', false ) ) {
  403. return;
  404. }
  405. add_action( 'set_auth_cookie', array( $this, 'set_samesite_auth_cookies' ), 10, 5 );
  406. add_action( 'set_logged_in_cookie', array( $this, 'set_samesite_logged_in_cookies' ), 10, 4 );
  407. add_filter( 'send_auth_cookies', array( $this, 'maybe_send_cookies' ), 9999 );
  408. }
  409. /**
  410. * Checks if we've stored any cookies to send and then sends them
  411. * if the send_auth_cookies value is true.
  412. *
  413. * @param bool $send_cookies The filtered value that determines whether to send auth cookies.
  414. */
  415. public function maybe_send_cookies( $send_cookies ) {
  416. if ( ! empty( $this->set_cookie_args ) && $send_cookies ) {
  417. array_map(
  418. function ( $cookie ) {
  419. call_user_func_array( 'jetpack_shim_setcookie', $cookie );
  420. },
  421. $this->set_cookie_args
  422. );
  423. $this->set_cookie_args = array();
  424. return false;
  425. }
  426. return $send_cookies;
  427. }
  428. /**
  429. * Gets the SameSite attribute to use in auth cookies.
  430. *
  431. * @param bool $secure Whether the connection is secure.
  432. * @return string SameSite attribute to use on auth cookies.
  433. */
  434. public function get_samesite_attr_for_auth_cookies( $secure ) {
  435. $samesite = $secure ? 'None' : 'Lax';
  436. /**
  437. * Filters the SameSite attribute to use in auth cookies.
  438. *
  439. * @param string $samesite SameSite attribute to use in auth cookies.
  440. *
  441. * @since 8.1.1
  442. */
  443. $samesite = apply_filters( 'jetpack_auth_cookie_samesite', $samesite );
  444. return $samesite;
  445. }
  446. /**
  447. * Generates cross-site auth cookies so they can be accessed by WordPress.com.
  448. *
  449. * @param string $auth_cookie Authentication cookie value.
  450. * @param int $expire The time the login grace period expires as a UNIX timestamp.
  451. * Default is 12 hours past the cookie's expiration time.
  452. * @param int $expiration The time when the authentication cookie expires as a UNIX timestamp.
  453. * Default is 14 days from now.
  454. * @param int $user_id User ID.
  455. * @param string $scheme Authentication scheme. Values include 'auth' or 'secure_auth'.
  456. */
  457. public function set_samesite_auth_cookies( $auth_cookie, $expire, $expiration, $user_id, $scheme ) {
  458. if ( wp_startswith( $scheme, 'secure_' ) ) {
  459. $secure = true;
  460. $auth_cookie_name = SECURE_AUTH_COOKIE;
  461. } else {
  462. $secure = false;
  463. $auth_cookie_name = AUTH_COOKIE;
  464. }
  465. $samesite = $this->get_samesite_attr_for_auth_cookies( $secure );
  466. $this->set_cookie_args[] = array(
  467. $auth_cookie_name,
  468. $auth_cookie,
  469. array(
  470. 'expires' => $expire,
  471. 'path' => PLUGINS_COOKIE_PATH,
  472. 'domain' => COOKIE_DOMAIN,
  473. 'secure' => $secure,
  474. 'httponly' => true,
  475. 'samesite' => $samesite,
  476. ),
  477. );
  478. $this->set_cookie_args[] = array(
  479. $auth_cookie_name,
  480. $auth_cookie,
  481. array(
  482. 'expires' => $expire,
  483. 'path' => ADMIN_COOKIE_PATH,
  484. 'domain' => COOKIE_DOMAIN,
  485. 'secure' => $secure,
  486. 'httponly' => true,
  487. 'samesite' => $samesite,
  488. ),
  489. );
  490. }
  491. /**
  492. * Generates cross-site logged in cookies so they can be accessed by WordPress.com.
  493. *
  494. * @param string $logged_in_cookie The logged-in cookie value.
  495. * @param int $expire The time the login grace period expires as a UNIX timestamp.
  496. * Default is 12 hours past the cookie's expiration time.
  497. * @param int $expiration The time when the logged-in cookie expires as a UNIX timestamp.
  498. * Default is 14 days from now.
  499. * @param int $user_id User ID.
  500. */
  501. public function set_samesite_logged_in_cookies( $logged_in_cookie, $expire, $expiration, $user_id ) {
  502. $secure = is_ssl();
  503. // Front-end cookie is secure when the auth cookie is secure and the site's home URL is forced HTTPS.
  504. $secure_logged_in_cookie = $secure && 'https' === wp_parse_url( get_option( 'home' ), PHP_URL_SCHEME );
  505. /** This filter is documented in core/src/wp-includes/pluggable.php */
  506. $secure = apply_filters( 'secure_auth_cookie', $secure, $user_id );
  507. /** This filter is documented in core/src/wp-includes/pluggable.php */
  508. $secure_logged_in_cookie = apply_filters( 'secure_logged_in_cookie', $secure_logged_in_cookie, $user_id, $secure );
  509. $samesite = $this->get_samesite_attr_for_auth_cookies( $secure_logged_in_cookie );
  510. $this->set_cookie_args[] = array(
  511. LOGGED_IN_COOKIE,
  512. $logged_in_cookie,
  513. array(
  514. 'expires' => $expire,
  515. 'path' => COOKIEPATH,
  516. 'domain' => COOKIE_DOMAIN,
  517. 'secure' => $secure_logged_in_cookie,
  518. 'httponly' => true,
  519. 'samesite' => $samesite,
  520. ),
  521. );
  522. if ( COOKIEPATH !== SITECOOKIEPATH ) {
  523. $this->set_cookie_args[] = array(
  524. LOGGED_IN_COOKIE,
  525. $logged_in_cookie,
  526. array(
  527. 'expires' => $expire,
  528. 'path' => SITECOOKIEPATH,
  529. 'domain' => COOKIE_DOMAIN,
  530. 'secure' => $secure_logged_in_cookie,
  531. 'httponly' => true,
  532. 'samesite' => $samesite,
  533. ),
  534. );
  535. }
  536. }
  537. }
  538. Jetpack_WPCOM_Block_Editor::init();