Conditions | 14 |
Paths | 33 |
Total Lines | 161 |
Lines | 5 |
Ratio | 3.11 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
166 | * |
||
167 | * This function is firing within wp-admin and checks (below) if it is in the midst of a deletion on the users |
||
168 | * page. Nonce will be already checked by WordPress, so we do not need to check ourselves. |
||
169 | */ |
||
170 | |||
171 | if ( ! isset( $current_screen->base ) || 'users' !== $current_screen->base ) { |
||
172 | return; |
||
173 | } |
||
174 | |||
175 | if ( ! isset( $_REQUEST['action'] ) || 'delete' !== $_REQUEST['action'] ) { |
||
176 | return; |
||
177 | } |
||
178 | |||
179 | // Get connection owner or bail. |
||
180 | $connection_manager = new Manager(); |
||
181 | $connection_owner_id = $connection_manager->get_connection_owner_id(); |
||
182 | if ( ! $connection_owner_id ) { |
||
183 | return; |
||
184 | } |
||
185 | $connection_owner_userdata = get_userdata( $connection_owner_id ); |
||
186 | |||
187 | // Bail if we're not trying to delete connection owner. |
||
188 | $user_ids_to_delete = array(); |
||
189 | View Code Duplication | if ( isset( $_REQUEST['users'] ) ) { |
|
190 | $user_ids_to_delete = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['users'] ) ); |
||
191 | } elseif ( isset( $_REQUEST['user'] ) ) { |
||
192 | $user_ids_to_delete[] = sanitize_text_field( wp_unslash( $_REQUEST['user'] ) ); |
||
193 | } |
||
194 | |||
195 | // phpcs:enable |
||
196 | $user_ids_to_delete = array_map( 'absint', $user_ids_to_delete ); |
||
197 | $deleting_connection_owner = in_array( $connection_owner_id, (array) $user_ids_to_delete, true ); |
||
198 | if ( ! $deleting_connection_owner ) { |
||
199 | return; |
||
200 | } |
||
201 | |||
202 | // Bail if they're trying to delete themselves to avoid confusion. |
||
203 | if ( get_current_user_id() === $connection_owner_id ) { |
||
204 | return; |
||
205 | } |
||
206 | |||
207 | // Track it! |
||
208 | if ( method_exists( $this->tracking, 'record_user_event' ) ) { |
||
209 | $this->tracking->record_user_event( 'delete_connection_owner_notice_view' ); |
||
210 | } |
||
211 | |||
212 | $connection_manager = new Manager(); |
||
213 | $connected_admins = $connection_manager->get_connected_users( 'jetpack_disconnect' ); |
||
214 | $user = is_a( $connection_owner_userdata, 'WP_User' ) ? esc_html( $connection_owner_userdata->data->user_login ) : ''; |
||
215 | |||
216 | echo "<div class='notice notice-warning' id='jetpack-notice-switch-connection-owner'>"; |
||
217 | echo '<h2>' . esc_html__( 'Important notice about your Jetpack connection:', 'jetpack' ) . '</h2>'; |
||
218 | echo '<p>' . sprintf( |
||
219 | /* translators: WordPress User, if available. */ |
||
220 | esc_html__( 'Warning! You are about to delete the Jetpack connection owner (%s) for this site, which may cause some of your Jetpack features to stop working.', 'jetpack' ), |
||
221 | esc_html( $user ) |
||
222 | ) . '</p>'; |
||
223 | |||
224 | if ( ! empty( $connected_admins ) && count( $connected_admins ) > 1 ) { |
||
225 | echo '<form id="jp-switch-connection-owner" action="" method="post">'; |
||
226 | echo "<label for='owner'>" . esc_html__( 'You can choose to transfer connection ownership to one of these already-connected admins:', 'jetpack' ) . ' </label>'; |
||
227 | |||
228 | $connected_admin_ids = array_map( |
||
229 | function( $connected_admin ) { |
||
230 | return $connected_admin->ID; |
||
231 | }, |
||
232 | $connected_admins |
||
233 | ); |
||
234 | |||
235 | wp_dropdown_users( |
||
236 | array( |
||
237 | 'name' => 'owner', |
||
238 | 'include' => array_diff( $connected_admin_ids, array( $connection_owner_id ) ), |
||
239 | 'show' => 'display_name_with_login', |
||
240 | ) |
||
241 | ); |
||
242 | |||
243 | echo '<p>'; |
||
244 | submit_button( esc_html__( 'Set new connection owner', 'jetpack' ), 'primary', 'jp-switch-connection-owner-submit', false ); |
||
245 | echo '</p>'; |
||
246 | |||
247 | echo "<div id='jp-switch-user-results'></div>"; |
||
248 | echo '</form>'; |
||
249 | ?> |
||
250 | <script type="text/javascript"> |
||
251 | jQuery( document ).ready( function( $ ) { |
||
252 | $( '#jp-switch-connection-owner' ).on( 'submit', function( e ) { |
||
253 | var formData = $( this ).serialize(); |
||
254 | var submitBtn = document.getElementById( 'jp-switch-connection-owner-submit' ); |
||
255 | var results = document.getElementById( 'jp-switch-user-results' ); |
||
256 | |||
257 | submitBtn.disabled = true; |
||
258 | |||
259 | $.ajax( { |
||
260 | type : "POST", |
||
261 | url : "<?php echo esc_url( get_rest_url() . 'jetpack/v4/connection/owner' ); ?>", |
||
262 | data : formData, |
||
263 | headers : { |
||
264 | 'X-WP-Nonce': "<?php echo esc_js( wp_create_nonce( 'wp_rest' ) ); ?>", |
||
265 | }, |
||
266 | success: function() { |
||
267 | results.innerHTML = "<?php esc_html_e( 'Success!', 'jetpack' ); ?>"; |
||
268 | setTimeout( function() { |
||
269 | $( '#jetpack-notice-switch-connection-owner' ).hide( 'slow' ); |
||
270 | }, 1000 ); |
||
271 | } |
||
272 | } ).done( function() { |
||
273 | submitBtn.disabled = false; |
||
274 | } ); |
||
275 | |||
276 | e.preventDefault(); |
||
277 | return false; |
||
278 | } ); |
||
279 | } ); |
||
280 | </script> |
||
281 | <?php |
||
282 | } else { |
||
283 | 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' ) . '</p>'; |
||
284 | $connect_url = \Jetpack::init()->build_connect_url( false, false, 'delete_connection_owner_notice' ); |
||
285 | echo "<a href='" . esc_url( $connect_url ) . "' target='_blank' rel='noopener noreferrer' class='button-primary'>" . esc_html__( 'Connect to WordPress.com', 'jetpack' ) . '</a>'; |
||
286 | } |
||
287 | |||
288 | echo '<p>'; |
||
289 | printf( |
||
290 | wp_kses( |
||
291 | /* translators: URL to Jetpack support doc regarding the primary user. */ |
||
292 | __( "<a href='%s' target='_blank' rel='noopener noreferrer'>Learn more</a> about the connection owner and what will break if you do not have one.", 'jetpack' ), |
||
293 | array( |
||
294 | 'a' => array( |
||
295 | 'href' => true, |
||
296 | 'target' => true, |
||
297 | 'rel' => true, |
||
298 | ), |
||
299 | ) |
||
300 | ), |
||
301 | 'https://jetpack.com/support/primary-user/' |
||
302 | ); |
||
303 | echo '</p>'; |
||
304 | echo '<p>'; |
||
305 | printf( |
||
306 | wp_kses( |
||
307 | /* translators: URL to contact Jetpack support. */ |
||
308 | __( 'As always, feel free to <a href="%s" target="_blank" rel="noopener noreferrer">contact our support team</a> if you have any questions.', 'jetpack' ), |
||
309 | array( |
||
310 | 'a' => array( |
||
311 | 'href' => true, |
||
312 | 'target' => true, |
||
313 | 'rel' => true, |
||
314 | ), |
||
315 | ) |
||
316 | ), |
||
317 | 'https://jetpack.com/contact-support' |
||
318 | ); |
||
319 | echo '</p>'; |
||
320 | echo '</div>'; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Injects the dom to show a JITM inside of wp-admin. |
||
325 | */ |
||
326 | public function ajax_message() { |
||
327 | if ( ! is_admin() ) { |
||
458 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..