Completed
Push — update/move_connection_owner_d... ( a0bfdc )
by
unknown
08:37
created

delete_user_update_connection_owner_notice()   C

Complexity

Conditions 14
Paths 33

Size

Total Lines 162

Duplication

Lines 5
Ratio 3.09 %

Importance

Changes 0
Metric Value
cc 14
nc 33
nop 0
dl 5
loc 162
rs 5.0133
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * The Connection\Owner class file.
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
use Automattic\Jetpack\Redirect;
11
use Automattic\Jetpack\Tracking;
12
13
/**
14
 * The Connection\Owner class.
15
 */
16
class Owner {
17
18
	/**
19
	 * This is an entire admin notice dedicated to messaging and handling of the case where a user is trying to delete
20
	 * the connection owner.
21
	 */
22
	public function delete_user_update_connection_owner_notice() {
23
		global $current_screen;
24
25
		/*
26
		 * phpcs:disable WordPress.Security.NonceVerification.Recommended
27
		 *
28
		 * This function is firing within wp-admin and checks (below) if it is in the midst of a deletion on the users
29
		 * page. Nonce will be already checked by WordPress, so we do not need to check ourselves.
30
		 */
31
32
		if ( ! isset( $current_screen->base ) || 'users' !== $current_screen->base ) {
33
			return;
34
		}
35
36
		if ( ! isset( $_REQUEST['action'] ) || 'delete' !== $_REQUEST['action'] ) {
37
			return;
38
		}
39
40
		// Get connection owner or bail.
41
		$connection_manager  = new Manager();
42
		$connection_owner_id = $connection_manager->get_connection_owner_id();
43
		if ( ! $connection_owner_id ) {
44
			return;
45
		}
46
		$connection_owner_userdata = get_userdata( $connection_owner_id );
47
48
		// Bail if we're not trying to delete connection owner.
49
		$user_ids_to_delete = array();
50 View Code Duplication
		if ( isset( $_REQUEST['users'] ) ) {
51
			$user_ids_to_delete = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['users'] ) );
52
		} elseif ( isset( $_REQUEST['user'] ) ) {
53
			$user_ids_to_delete[] = sanitize_text_field( wp_unslash( $_REQUEST['user'] ) );
54
		}
55
56
		// phpcs:enable
57
		$user_ids_to_delete        = array_map( 'absint', $user_ids_to_delete );
58
		$deleting_connection_owner = in_array( $connection_owner_id, (array) $user_ids_to_delete, true );
59
		if ( ! $deleting_connection_owner ) {
60
			return;
61
		}
62
63
		// Bail if they're trying to delete themselves to avoid confusion.
64
		if ( get_current_user_id() === $connection_owner_id ) {
65
			return;
66
		}
67
68
		// Track it!
69
		$tracking = new Tracking();
70
		if ( method_exists( $tracking, 'record_user_event' ) ) {
71
			$tracking->record_user_event( 'delete_connection_owner_notice_view' );
72
		}
73
74
		$connected_admins = $connection_manager->get_connected_users( 'jetpack_disconnect' );
75
		$user             = is_a( $connection_owner_userdata, 'WP_User' ) ? esc_html( $connection_owner_userdata->data->user_login ) : '';
76
77
		echo "<div class='notice notice-warning' id='jetpack-notice-switch-connection-owner'>";
78
		echo '<h2>' . esc_html__( 'Important notice about your Jetpack connection:', 'jetpack' ) . '</h2>';
79
		echo '<p>' . sprintf(
80
			/* translators: WordPress User, if available. */
81
			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' ),
82
			esc_html( $user )
83
		) . '</p>';
84
85
		if ( ! empty( $connected_admins ) && count( $connected_admins ) > 1 ) {
86
			echo '<form id="jp-switch-connection-owner" action="" method="post">';
87
			echo "<label for='owner'>" . esc_html__( 'You can choose to transfer connection ownership to one of these already-connected admins:', 'jetpack' ) . ' </label>';
88
89
			$connected_admin_ids = array_map(
90
				function( $connected_admin ) {
91
						return $connected_admin->ID;
92
				},
93
				$connected_admins
94
			);
95
96
			wp_dropdown_users(
97
				array(
98
					'name'    => 'owner',
99
					'include' => array_diff( $connected_admin_ids, array( $connection_owner_id ) ),
100
					'show'    => 'display_name_with_login',
101
				)
102
			);
103
104
			echo '<p>';
105
			submit_button( esc_html__( 'Set new connection owner', 'jetpack' ), 'primary', 'jp-switch-connection-owner-submit', false );
106
			echo '</p>';
107
108
			echo "<div id='jp-switch-user-results'></div>";
109
			echo '</form>';
110
			?>
111
			<script type="text/javascript">
112
				jQuery( document ).ready( function( $ ) {
113
					$( '#jp-switch-connection-owner' ).on( 'submit', function( e ) {
114
						var formData = $( this ).serialize();
115
						var submitBtn = document.getElementById( 'jp-switch-connection-owner-submit' );
116
						var results = document.getElementById( 'jp-switch-user-results' );
117
118
						submitBtn.disabled = true;
119
120
						$.ajax( {
121
							type        : "POST",
122
							url         : "<?php echo esc_url( get_rest_url() . 'jetpack/v4/connection/owner' ); ?>",
123
							data        : formData,
124
							headers     : {
125
								'X-WP-Nonce': "<?php echo esc_js( wp_create_nonce( 'wp_rest' ) ); ?>",
126
							},
127
							success: function() {
128
								results.innerHTML = "<?php esc_html_e( 'Success!', 'jetpack' ); ?>";
129
								setTimeout( function() {
130
									$( '#jetpack-notice-switch-connection-owner' ).hide( 'slow' );
131
								}, 1000 );
132
							},
133
						} ).done( function() {
134
							submitBtn.disabled = false;
135
						} );
136
137
						e.preventDefault();
138
						return false;
139
					} );
140
				} );
141
			</script>
142
			<?php
143
		} else {
144
			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>';
145
			$connect_url = $connection_manager->get_authorization_url();
146
			$connect_url = add_query_arg( 'from', 'delete_connection_owner_notice', $connect_url );
147
			echo "<a href='" . esc_url( $connect_url ) . "' target='_blank' rel='noopener noreferrer' class='button-primary'>" . esc_html__( 'Connect to WordPress.com', 'jetpack' ) . '</a>';
148
		}
149
150
		echo '<p>';
151
		printf(
152
			wp_kses(
153
				/* translators: URL to Jetpack support doc regarding the primary user. */
154
				__( "<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' ),
155
				array(
156
					'a' => array(
157
						'href'   => true,
158
						'target' => true,
159
						'rel'    => true,
160
					),
161
				)
162
			),
163
			esc_url( Redirect::get_url( 'jetpack-support-primary-user' ) )
164
		);
165
		echo '</p>';
166
		echo '<p>';
167
		printf(
168
			wp_kses(
169
				/* translators: URL to contact Jetpack support. */
170
				__( 'As always, feel free to <a href="%s" target="_blank" rel="noopener noreferrer">contact our support team</a> if you have any questions.', 'jetpack' ),
171
				array(
172
					'a' => array(
173
						'href'   => true,
174
						'target' => true,
175
						'rel'    => true,
176
					),
177
				)
178
			),
179
			esc_url( Redirect::get_url( 'jetpack-contact-support' ) )
180
		);
181
		echo '</p>';
182
		echo '</div>';
183
	}
184
}
185