1 | <?php |
||
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() { |
||
33 | |||
34 | /** |
||
35 | * Jetpack_WPCOM_Block_Editor constructor. |
||
36 | */ |
||
37 | private function __construct() { |
||
38 | if ( $this->is_iframed_block_editor() ) { |
||
39 | add_action( 'init', array( $this, 'show_error_if_logged_out' ) ); |
||
40 | add_action( 'admin_init', array( $this, 'disable_send_frame_options_header' ), 9 ); |
||
41 | add_filter( 'admin_body_class', array( $this, 'add_iframed_body_class' ) ); |
||
42 | } |
||
43 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts' ) ); |
||
44 | add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugins' ) ); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Checks if we are embedding the block editor in an iframe in WordPress.com. |
||
49 | * |
||
50 | * @return bool Whether the current request is from the iframed block editor. |
||
51 | */ |
||
52 | public function is_iframed_block_editor() { |
||
53 | $is_calypsoify = 1 === (int) get_user_meta( get_current_user_id(), 'calypsoify', true ); |
||
54 | global $pagenow; |
||
55 | // phpcs:ignore WordPress.Security.NonceVerification |
||
56 | return ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) && ! empty( $_GET['frame-nonce'] ) && $is_calypsoify; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Shows a custom message if the user is logged out. |
||
61 | * |
||
62 | * The iframed block editor can be only embedded in WordPress.com if the user is logged |
||
63 | * into the Jetpack site. So we abort the default redirection to the login page (which |
||
64 | * cannot be embedded in a iframe) and instead we explain that we need the user to log |
||
65 | * into Jetpack. |
||
66 | */ |
||
67 | public function show_error_if_logged_out() { |
||
68 | if ( ! get_current_user_id() ) { |
||
69 | $login_url = wp_login_url(); |
||
70 | /* translators: %s: Login URL */ |
||
71 | wp_die( sprintf( __( 'In order to use the block editor of this Jetpack site in WordPress.com, you need to <a href="%s" target="_blank" rel="noopener noreferrer">log into</a> the Jetpack site.', 'jetpack' ), $login_url ) ); // // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
||
72 | } |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Prevents frame options header from firing if this is a whitelisted iframe request. |
||
77 | */ |
||
78 | public function disable_send_frame_options_header() { |
||
79 | if ( $this->framing_allowed() ) { |
||
80 | remove_action( 'admin_init', 'send_frame_options_header' ); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Adds custom admin body class if this is a whitelisted iframe request. |
||
86 | * |
||
87 | * @param string $classes Admin body classes. |
||
88 | * @return string |
||
89 | */ |
||
90 | public function add_iframed_body_class( $classes ) { |
||
91 | if ( $this->framing_allowed() ) { |
||
92 | $classes .= ' is-iframed '; |
||
93 | } |
||
94 | |||
95 | return $classes; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Checks whether this is a whitelisted iframe request. |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | public function framing_allowed() { |
||
104 | $verified = $this->verify_frame_nonce( |
||
105 | $_GET['frame-nonce'], // phpcs:ignore WordPress.Security.NonceVerification |
||
106 | 'frame-' . Jetpack_Options::get_option( 'id' ) |
||
107 | ); |
||
108 | |||
109 | if ( is_wp_error( $verified ) ) { |
||
110 | wp_die( $verified ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
||
111 | } |
||
112 | |||
113 | if ( $verified && ! defined( 'IFRAME_REQUEST' ) ) { |
||
114 | define( 'IFRAME_REQUEST', true ); |
||
115 | } |
||
116 | |||
117 | return (bool) $verified; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Verify that correct nonce was used with time limit. |
||
122 | * |
||
123 | * The user is given an amount of time to use the token, so therefore, since the |
||
124 | * UID and $action remain the same, the independent variable is the time. |
||
125 | * |
||
126 | * @param string $nonce Nonce that was used in the form to verify. |
||
127 | * @param string $action Should give context to what is taking place and be the same when nonce was created. |
||
128 | * @return boolean|WP_Error Whether the nonce is valid. |
||
129 | */ |
||
130 | public function verify_frame_nonce( $nonce, $action ) { |
||
175 | |||
176 | /** |
||
177 | * Filters the WordPress salt. |
||
178 | * |
||
179 | * @param string $salt Salt for the given scheme. |
||
180 | * @param string $scheme Authentication scheme. |
||
181 | * @return string |
||
182 | */ |
||
183 | public function filter_salt( $salt, $scheme ) { |
||
194 | |||
195 | /** |
||
196 | * Enqueue the scripts for the WordPress.com block editor integration. |
||
197 | */ |
||
198 | public function enqueue_scripts() { |
||
240 | |||
241 | /** |
||
242 | * Register the Tiny MCE plugins for the WordPress.com block editor integration. |
||
243 | * |
||
244 | * @param array $plugin_array An array of external Tiny MCE plugins. |
||
245 | * @return array External TinyMCE plugins. |
||
246 | */ |
||
247 | public function add_tinymce_plugins( $plugin_array ) { |
||
261 | } |
||
262 | |||
264 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: