PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | jetpack_vendor/automattic/jetpack-connection/src/class-connection-notice.php +94 -69 12.3.2 → 16.3 View file →
@@ -11,8 +11,10 @@
11 11 use Automattic\Jetpack\Tracking;
12 12
13 13 /**
14 14 * Admin connection notices.
15 + *
16 + * @phan-constructor-used-for-side-effects
15 17 */
16 18 class Connection_Notice {
17 19
18 20 /**
@@ -39,9 +41,9 @@
39 41 *
40 42 * @return void
41 43 */
42 44 public function initialize_notices( $screen ) {
43 - if ( ! in_array(
45 + if ( in_array(
44 46 $screen->id,
45 47 array(
46 48 'jetpack_page_akismet-key-config',
47 49 'admin_page_jetpack_modules',
@@ -47,19 +49,11 @@
47 49 'admin_page_jetpack_modules',
48 50 ),
49 51 true
50 52 ) ) {
51 - add_action( 'admin_notices', array( $this, 'delete_user_update_connection_owner_notice' ) );
53 + return;
52 54 }
53 - }
54 55
55 - /**
56 - * This is an entire admin notice dedicated to messaging and handling of the case where a user is trying to delete
57 - * the connection owner.
58 - */
59 - public function delete_user_update_connection_owner_notice() {
60 - global $current_screen;
61 -
62 56 /*
63 57 * phpcs:disable WordPress.Security.NonceVerification.Recommended
64 58 *
65 59 * This function is firing within wp-admin and checks (below) if it is in the midst of a deletion on the users
@@ -65,16 +59,20 @@
65 59 * This function is firing within wp-admin and checks (below) if it is in the midst of a deletion on the users
66 60 * page. Nonce will be already checked by WordPress, so we do not need to check ourselves.
67 61 */
68 62
69 - if ( ! isset( $current_screen->base ) || 'users' !== $current_screen->base ) {
70 - return;
63 + if ( isset( $screen->base ) && 'users' === $screen->base
64 + && isset( $_REQUEST['action'] ) && 'delete' === $_REQUEST['action']
65 + ) {
66 + add_action( 'admin_notices', array( $this, 'delete_user_update_connection_owner_notice' ) );
71 67 }
68 + }
72 69
73 - if ( ! isset( $_REQUEST['action'] ) || 'delete' !== $_REQUEST['action'] ) {
74 - return;
75 - }
76 -
70 + /**
71 + * This is an entire admin notice dedicated to messaging and handling of the case where a user is trying to delete
72 + * the connection owner.
73 + */
74 + public function delete_user_update_connection_owner_notice() {
77 75 // Get connection owner or bail.
78 76 $connection_manager = new Manager();
79 77 $connection_owner_id = $connection_manager->get_connection_owner_id();
80 78 if ( ! $connection_owner_id ) {
@@ -144,62 +142,10 @@
144 142 echo '</p>';
145 143
146 144 echo "<div id='jp-switch-user-results'></div>";
147 145 echo '</form>';
148 - ?>
149 - <script type="text/javascript">
150 - ( function() {
151 - const switchOwnerButton = document.getElementById('jp-switch-connection-owner');
152 - if ( ! switchOwnerButton ) {
153 - return;
154 - }
155 146
156 - switchOwnerButton.addEventListener( 'submit', function ( e ) {
157 - e.preventDefault();
158 -
159 - const submitBtn = document.getElementById('jp-switch-connection-owner-submit');
160 - submitBtn.disabled = true;
161 -
162 - const results = document.getElementById('jp-switch-user-results');
163 - results.innerHTML = '';
164 - results.classList.remove( 'error-message' );
165 -
166 - const handleAPIError = ( message ) => {
167 - submitBtn.disabled = false;
168 -
169 - results.classList.add( 'error-message' );
170 - results.innerHTML = message || "<?php esc_html_e( 'Something went wrong. Please try again.', 'jetpack-connection' ); ?>";
171 - }
172 -
173 - fetch(
174 - <?php echo wp_json_encode( esc_url_raw( get_rest_url() . 'jetpack/v4/connection/owner' ), JSON_HEX_TAG | JSON_HEX_AMP ); ?>,
175 - {
176 - method: 'POST',
177 - headers: {
178 - 'X-WP-Nonce': <?php echo wp_json_encode( wp_create_nonce( 'wp_rest' ), JSON_HEX_TAG | JSON_HEX_AMP ); ?>,
179 - },
180 - body: new URLSearchParams( new FormData( this ) ),
181 - }
182 - )
183 - .then( response => response.json() )
184 - .then( data => {
185 - if ( data.hasOwnProperty( 'code' ) && data.code === 'success' ) {
186 - // Owner successfully changed.
187 - results.innerHTML = <?php echo wp_json_encode( esc_html__( 'Success!', 'jetpack-connection' ), JSON_HEX_TAG | JSON_HEX_AMP ); ?>;
188 - setTimeout(function () {
189 - document.getElementById( 'jetpack-notice-switch-connection-owner' ).style.display = 'none';
190 - }, 1000);
191 -
192 - return;
193 - }
194 -
195 - handleAPIError( data?.message );
196 - } )
197 - .catch( () => handleAPIError() );
198 - });
199 - } )();
200 - </script>
201 - <?php
147 + $this->enqueue_connection_owner_script();
202 148 } else {
203 149 echo '<p>' . esc_html__( 'Every Jetpack site needs at least one connected admin for the features to work properly. Please connect to your WordPress.com account via the button below. Once you connect, you may refresh this page to see an option to change the connection owner.', 'jetpack-connection' ) . '</p>';
204 150 $connect_url = $connection_manager->get_authorization_url();
205 151 $connect_url = add_query_arg( 'from', 'delete_connection_owner_notice', $connect_url );
@@ -240,5 +186,84 @@
240 186 echo '</p>';
241 187 echo '</div>';
242 188 }
243 189
190 + /**
191 + * Enqueue the script that hands the chosen owner to the connection owner REST endpoint.
192 + *
193 + * The notice prints on `admin_notices`, after `admin_enqueue_scripts` has run, so the handle
194 + * has no source of its own and goes in the footer, where the form it binds to already exists.
195 + *
196 + * @return void
197 + */
198 + private function enqueue_connection_owner_script() {
199 + $handle = 'jetpack-connection-owner-notice';
200 +
201 + wp_register_script( $handle, false, array(), Package_Version::PACKAGE_VERSION, true );
202 + wp_enqueue_script( $handle );
203 + wp_add_inline_script( $handle, $this->get_connection_owner_script() );
204 + }
205 +
206 + /**
207 + * Build the script that hands the chosen owner to the connection owner REST endpoint.
208 + *
209 + * @return string
210 + */
211 + private function get_connection_owner_script() {
212 + $json_flags = JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP;
213 +
214 + ob_start();
215 + ?>
216 +( function() {
217 + const switchOwnerButton = document.getElementById('jp-switch-connection-owner');
218 + if ( ! switchOwnerButton ) {
219 + return;
220 + }
221 +
222 + switchOwnerButton.addEventListener( 'submit', function ( e ) {
223 + e.preventDefault();
224 +
225 + const submitBtn = document.getElementById('jp-switch-connection-owner-submit');
226 + submitBtn.disabled = true;
227 +
228 + const results = document.getElementById('jp-switch-user-results');
229 + results.innerHTML = '';
230 + results.classList.remove( 'error-message' );
231 +
232 + const handleAPIError = ( message ) => {
233 + submitBtn.disabled = false;
234 +
235 + results.classList.add( 'error-message' );
236 + results.innerHTML = message || <?php echo wp_json_encode( esc_html__( 'Something went wrong. Please try again.', 'jetpack-connection' ), $json_flags ); ?>;
237 + }
238 +
239 + fetch(
240 + <?php echo wp_json_encode( esc_url_raw( get_rest_url() . 'jetpack/v4/connection/owner' ), $json_flags ); ?>,
241 + {
242 + method: 'POST',
243 + headers: {
244 + 'X-WP-Nonce': <?php echo wp_json_encode( wp_create_nonce( 'wp_rest' ), $json_flags ); ?>,
245 + },
246 + body: new URLSearchParams( new FormData( this ) ),
247 + }
248 + )
249 + .then( response => response.json() )
250 + .then( data => {
251 + if ( data.hasOwnProperty( 'code' ) && data.code === 'success' ) {
252 + // Owner successfully changed.
253 + results.innerHTML = <?php echo wp_json_encode( esc_html__( 'Success!', 'jetpack-connection' ), $json_flags ); ?>;
254 + setTimeout(function () {
255 + document.getElementById( 'jetpack-notice-switch-connection-owner' ).style.display = 'none';
256 + }, 1000);
257 +
258 + return;
259 + }
260 +
261 + handleAPIError( data?.message );
262 + } )
263 + .catch( () => handleAPIError() );
264 + });
265 +} )();
266 + <?php
267 + return ob_get_clean();
268 + }
244 269 }