set_cookie_args = array(); add_action( 'init', array( $this, 'init_actions' ) ); } /** * Add in all hooks. */ public function init_actions() { global $wp_version; // Bail early if Jetpack's block editor extensions are disabled on the site. /* This filter is documented in class.jetpack-gutenberg.php */ if ( ! apply_filters( 'jetpack_gutenberg', true ) ) { return; } if ( $this->is_iframed_block_editor() ) { add_action( 'admin_init', array( $this, 'disable_send_frame_options_header' ), 9 ); add_filter( 'admin_body_class', array( $this, 'add_iframed_body_class' ) ); } require_once __DIR__ . '/functions.editor-type.php'; add_action( 'edit_form_top', 'Jetpack\EditorType\remember_classic_editor' ); add_action( 'login_init', array( $this, 'allow_block_editor_login' ), 1 ); add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ), 9 ); add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) ); add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugins' ) ); // @todo simplify once 5.8 is the minimum supported version. if ( version_compare( $wp_version, '5.8', '>=' ) ) { add_filter( 'block_editor_settings_all', 'Jetpack\EditorType\remember_block_editor', 10, 2 ); } else { add_filter( 'block_editor_settings', 'Jetpack\EditorType\remember_block_editor', 10, 2 ); } $this->enable_cross_site_auth_cookies(); } /** * Checks if we are embedding the block editor in an iframe in WordPress.com. * * @return bool Whether the current request is from the iframed block editor. */ public function is_iframed_block_editor() { global $pagenow; // phpcs:ignore WordPress.Security.NonceVerification return ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) && ! empty( $_GET['frame-nonce'] ); } /** * Prevents frame options header from firing if this is a allowed iframe request. */ public function disable_send_frame_options_header() { // phpcs:ignore WordPress.Security.NonceVerification if ( $this->framing_allowed( $_GET['frame-nonce'] ) ) { remove_action( 'admin_init', 'send_frame_options_header' ); } } /** * Adds custom admin body class if this is a allowed iframe request. * * @param string $classes Admin body classes. * @return string */ public function add_iframed_body_class( $classes ) { // phpcs:ignore WordPress.Security.NonceVerification if ( $this->framing_allowed( $_GET['frame-nonce'] ) ) { $classes .= ' is-iframed '; } return $classes; } /** * Checks to see if cookie can be set in current context. If 3rd party cookie blocking * is enabled the editor can't load in iFrame, so emiting X-Frame-Options: DENY will * force the editor to break out of the iFrame. */ private function check_iframe_cookie_setting() { if ( ! isset( $_SERVER['QUERY_STRING'] ) || ! strpos( $_SERVER['QUERY_STRING'], 'calypsoify%3D1%26block-editor' ) || isset( $_COOKIE['wordpress_test_cookie'] ) ) { return; } if ( empty( $_GET['calypsoify_cookie_check'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended header( 'Location: ' . esc_url_raw( $_SERVER['REQUEST_URI'] . '&calypsoify_cookie_check=true' ) ); exit; } header( 'X-Frame-Options: DENY' ); exit; } /** * Allows to iframe the login page if a user is logged out * while trying to access the block editor from wordpress.com. */ public function allow_block_editor_login() { // phpcs:ignore WordPress.Security.NonceVerification if ( empty( $_REQUEST['redirect_to'] ) ) { return; } $this->check_iframe_cookie_setting(); // phpcs:ignore WordPress.Security.NonceVerification $query = wp_parse_url( urldecode( $_REQUEST['redirect_to'] ), PHP_URL_QUERY ); $args = wp_parse_args( $query ); // Check nonce and make sure this is a Gutenframe request. if ( ! empty( $args['frame-nonce'] ) && $this->framing_allowed( $args['frame-nonce'] ) ) { // If SSO is active, we'll let WordPress.com handle authentication... if ( Jetpack::is_module_active( 'sso' ) ) { // ...but only if it's not an Atomic site. They already do that. if ( ! ( new Host() )->is_woa_site() ) { add_filter( 'jetpack_sso_bypass_login_forward_wpcom', '__return_true' ); } } else { $_REQUEST['interim-login'] = true; add_action( 'wp_login', array( $this, 'do_redirect' ) ); add_action( 'login_form', array( $this, 'add_login_html' ) ); add_filter( 'wp_login_errors', array( $this, 'add_login_message' ) ); remove_action( 'login_init', 'send_frame_options_header' ); wp_add_inline_style( 'login', '.interim-login #login{padding-top:8%}' ); } } } /** * Adds a login message. * * Intended to soften the expectation mismatch of ending up with a login screen rather than the editor. * * @param WP_Error $errors WP Error object. * @return \WP_Error */ public function add_login_message( $errors ) { $errors->remove( 'expired' ); $errors->add( 'info', __( 'Before we continue, please log in to your Jetpack site.', 'jetpack' ), 'message' ); return $errors; } /** * Maintains the `redirect_to` parameter in login form links. * Adds visual feedback of login in progress. */ public function add_login_html() { ?> verify_frame_nonce( $nonce, 'frame-' . Jetpack_Options::get_option( 'id' ) ); if ( is_wp_error( $verified ) ) { wp_die( $verified ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } if ( $verified && ! defined( 'IFRAME_REQUEST' ) ) { define( 'IFRAME_REQUEST', true ); } return (bool) $verified; } /** * Verify that correct nonce was used with time limit. * * The user is given an amount of time to use the token, so therefore, since the * UID and $action remain the same, the independent variable is the time. * * @param string $nonce Nonce that was used in the form to verify. * @param string $action Should give context to what is taking place and be the same when nonce was created. * @return boolean|WP_Error Whether the nonce is valid. */ public function verify_frame_nonce( $nonce, $action ) { if ( empty( $nonce ) ) { return false; } list( $expiration, $user_id, $hash ) = explode( ':', $nonce, 3 ); $this->nonce_user_id = (int) $user_id; if ( ! $this->nonce_user_id ) { return false; } $token = ( new Tokens() )->get_access_token( $this->nonce_user_id ); if ( ! $token ) { return false; } /* * Failures must return `false` (blocking the iframe) prior to the * signature verification. */ add_filter( 'salt', array( $this, 'filter_salt' ), 10, 2 ); $expected_hash = wp_hash( "$expiration|$action|{$this->nonce_user_id}", 'jetpack_frame_nonce' ); remove_filter( 'salt', array( $this, 'filter_salt' ) ); if ( ! hash_equals( $hash, $expected_hash ) ) { return false; } /* * Failures may return `WP_Error` (showing an error in the iframe) after the * signature verification passes. */ if ( time() > $expiration ) { return new WP_Error( 'nonce_invalid_expired', 'Expired nonce.', array( 'status' => 401 ) ); } // Check if it matches the current user, unless they're trying to log in. if ( get_current_user_id() !== $this->nonce_user_id && ! doing_action( 'login_init' ) ) { return new WP_Error( 'nonce_invalid_user_mismatch', 'User ID mismatch.', array( 'status' => 401 ) ); } return true; } /** * Filters the WordPress salt. * * @param string $salt Salt for the given scheme. * @param string $scheme Authentication scheme. * @return string */ public function filter_salt( $salt, $scheme ) { if ( 'jetpack_frame_nonce' === $scheme ) { $token = ( new Tokens() )->get_access_token( $this->nonce_user_id ); if ( $token ) { $salt = $token->secret; } } return $salt; } /** * Enqueues the WordPress.com block editor integration assets for the editor. */ public function enqueue_block_editor_assets() { global $pagenow; // Bail if we're not in the post editor, but on the widget settings screen. if ( is_customize_preview() || 'widgets.php' === $pagenow ) { return; } $debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; $version = gmdate( 'Ymd' ); wp_enqueue_script( 'wpcom-block-editor-default-editor-script', $debug ? '//widgets.wp.com/wpcom-block-editor/default.editor.js?minify=false' : '//widgets.wp.com/wpcom-block-editor/default.editor.min.js', array( 'jquery', 'lodash', 'wp-annotations', 'wp-compose', 'wp-data', 'wp-editor', 'wp-element', 'wp-rich-text', ), $version, true ); wp_localize_script( 'wpcom-block-editor-default-editor-script', 'wpcomGutenberg', array( 'richTextToolbar' => array( 'justify' => __( 'Justify', 'jetpack' ), 'underline' => __( 'Underline', 'jetpack' ), ), ) ); if ( ( new Host() )->is_woa_site() ) { wp_enqueue_script( 'wpcom-block-editor-wpcom-editor-script', $debug ? '//widgets.wp.com/wpcom-block-editor/wpcom.editor.js?minify=false' : '//widgets.wp.com/wpcom-block-editor/wpcom.editor.min.js', array( 'lodash', 'wp-blocks', 'wp-data', 'wp-dom-ready', 'wp-plugins', ), $version, true ); } if ( $this->is_iframed_block_editor() ) { wp_enqueue_script( 'wpcom-block-editor-calypso-editor-script', $debug ? '//widgets.wp.com/wpcom-block-editor/calypso.editor.js?minify=false' : '//widgets.wp.com/wpcom-block-editor/calypso.editor.min.js', array( 'calypsoify_wpadminmods_js', 'jquery', 'lodash', 'react', 'wp-blocks', 'wp-data', 'wp-hooks', 'wp-tinymce', 'wp-url', ), $version, true ); wp_enqueue_style( 'wpcom-block-editor-calypso-editor-styles', $debug ? '//widgets.wp.com/wpcom-block-editor/calypso.editor.css?minify=false' : '//widgets.wp.com/wpcom-block-editor/calypso.editor.min.css', array(), $version ); } } /** * Enqueues the WordPress.com block editor integration assets for both editor and front-end. */ public function enqueue_block_assets() { // These styles are manually copied from //widgets.wp.com/wpcom-block-editor/default.view.css in order to // improve the performance by avoiding an extra network request to download the CSS file on every page. wp_add_inline_style( 'wp-block-library', '.has-text-align-justify{text-align:justify;}' ); } /** * Determines if the current $post contains a justified paragraph block. * * @return boolean true if justified paragraph is found, false otherwise. */ public function has_justified_block() { global $post; if ( ! $post instanceof WP_Post ) { return false; }; if ( ! has_blocks( $post ) ) { return false; } return false !== strpos( $post->post_content, '