Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Post_Connection_JITM often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Post_Connection_JITM, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Post_Connection_JITM extends JITM { |
||
23 | |||
24 | /** |
||
25 | * Tracking object. |
||
26 | * |
||
27 | * @var Automattic\Jetpack\Tracking |
||
28 | * |
||
29 | * @access private |
||
30 | */ |
||
31 | public $tracking; |
||
32 | |||
33 | /** |
||
34 | * JITM constructor. |
||
35 | */ |
||
36 | public function __construct() { |
||
37 | $this->tracking = new Tracking(); |
||
|
|||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Prepare actions according to screen and post type. |
||
42 | * |
||
43 | * @since 3.8.2 |
||
44 | * |
||
45 | * @uses Jetpack_Autoupdate::get_possible_failures() |
||
46 | * |
||
47 | * @param \WP_Screen $screen WP Core's screen object. |
||
48 | */ |
||
49 | public function prepare_jitms( $screen ) { |
||
50 | parent::prepare_jitms( $screen ); |
||
51 | if ( ! in_array( |
||
52 | $screen->id, |
||
53 | array( |
||
54 | 'jetpack_page_akismet-key-config', |
||
55 | 'admin_page_jetpack_modules', |
||
56 | ), |
||
57 | true |
||
58 | ) ) { |
||
59 | // Not really a JITM. Don't know where else to put this :) . |
||
60 | add_action( 'admin_notices', array( $this, 'delete_user_update_connection_owner_notice' ) ); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * A special filter for WooCommerce, to set a message based on local state. |
||
66 | * |
||
67 | * @param string $content The current message. |
||
68 | * |
||
69 | * @return array The new message. |
||
70 | */ |
||
71 | public static function jitm_woocommerce_services_msg( $content ) { |
||
72 | if ( ! function_exists( 'wc_get_base_location' ) ) { |
||
73 | return $content; |
||
74 | } |
||
75 | |||
76 | $base_location = wc_get_base_location(); |
||
77 | |||
78 | switch ( $base_location['country'] ) { |
||
79 | case 'US': |
||
80 | $content->message = esc_html__( 'New free service: Show USPS shipping rates on your store! Added bonus: print shipping labels without leaving WooCommerce.', 'jetpack' ); |
||
81 | break; |
||
82 | case 'CA': |
||
83 | $content->message = esc_html__( 'New free service: Show Canada Post shipping rates on your store!', 'jetpack' ); |
||
84 | break; |
||
85 | default: |
||
86 | $content->message = ''; |
||
87 | } |
||
88 | |||
89 | return $content; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * A special filter for WooCommerce Call To Action button |
||
94 | * |
||
95 | * @return string The new CTA |
||
96 | */ |
||
97 | public static function jitm_jetpack_woo_services_install() { |
||
98 | return wp_nonce_url( |
||
99 | add_query_arg( |
||
100 | array( |
||
101 | 'wc-services-action' => 'install', |
||
102 | ), |
||
103 | admin_url( 'admin.php?page=wc-settings' ) |
||
104 | ), |
||
105 | 'wc-services-install' |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * A special filter for WooCommerce Call To Action button. |
||
111 | * |
||
112 | * @return string The new CTA |
||
113 | */ |
||
114 | public static function jitm_jetpack_woo_services_activate() { |
||
115 | return wp_nonce_url( |
||
116 | add_query_arg( |
||
117 | array( |
||
118 | 'wc-services-action' => 'activate', |
||
119 | ), |
||
120 | admin_url( 'admin.php?page=wc-settings' ) |
||
121 | ), |
||
122 | 'wc-services-install' |
||
123 | ); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * This is an entire admin notice dedicated to messaging and handling of the case where a user is trying to delete |
||
128 | * the connection owner. |
||
129 | */ |
||
130 | public function delete_user_update_connection_owner_notice() { |
||
131 | global $current_screen; |
||
132 | |||
133 | /* |
||
134 | * phpcs:disable WordPress.Security.NonceVerification.Recommended |
||
135 | * |
||
136 | * This function is firing within wp-admin and checks (below) if it is in the midst of a deletion on the users |
||
137 | * page. Nonce will be already checked by WordPress, so we do not need to check ourselves. |
||
138 | */ |
||
139 | |||
140 | if ( ! isset( $current_screen->base ) || 'users' !== $current_screen->base ) { |
||
141 | return; |
||
142 | } |
||
143 | |||
144 | if ( ! isset( $_REQUEST['action'] ) || 'delete' !== $_REQUEST['action'] ) { |
||
145 | return; |
||
146 | } |
||
147 | |||
148 | // Get connection owner or bail. |
||
149 | $connection_manager = new Manager(); |
||
150 | $connection_owner_id = $connection_manager->get_connection_owner_id(); |
||
151 | if ( ! $connection_owner_id ) { |
||
152 | return; |
||
153 | } |
||
154 | $connection_owner_userdata = get_userdata( $connection_owner_id ); |
||
155 | |||
156 | // Bail if we're not trying to delete connection owner. |
||
157 | $user_ids_to_delete = array(); |
||
158 | View Code Duplication | if ( isset( $_REQUEST['users'] ) ) { |
|
159 | $user_ids_to_delete = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['users'] ) ); |
||
160 | } elseif ( isset( $_REQUEST['user'] ) ) { |
||
161 | $user_ids_to_delete[] = sanitize_text_field( wp_unslash( $_REQUEST['user'] ) ); |
||
162 | } |
||
163 | |||
164 | // phpcs:enable |
||
165 | $user_ids_to_delete = array_map( 'absint', $user_ids_to_delete ); |
||
166 | $deleting_connection_owner = in_array( $connection_owner_id, (array) $user_ids_to_delete, true ); |
||
167 | if ( ! $deleting_connection_owner ) { |
||
168 | return; |
||
169 | } |
||
170 | |||
171 | // Bail if they're trying to delete themselves to avoid confusion. |
||
172 | if ( get_current_user_id() === $connection_owner_id ) { |
||
173 | return; |
||
174 | } |
||
175 | |||
176 | // Track it! |
||
177 | if ( method_exists( $this->tracking, 'record_user_event' ) ) { |
||
178 | $this->tracking->record_user_event( 'delete_connection_owner_notice_view' ); |
||
179 | } |
||
180 | |||
181 | $connected_admins = $connection_manager->get_connected_users( 'jetpack_disconnect' ); |
||
182 | $user = is_a( $connection_owner_userdata, 'WP_User' ) ? esc_html( $connection_owner_userdata->data->user_login ) : ''; |
||
183 | |||
184 | echo "<div class='notice notice-warning' id='jetpack-notice-switch-connection-owner'>"; |
||
185 | echo '<h2>' . esc_html__( 'Important notice about your Jetpack connection:', 'jetpack' ) . '</h2>'; |
||
186 | echo '<p>' . sprintf( |
||
187 | /* translators: WordPress User, if available. */ |
||
188 | 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' ), |
||
189 | esc_html( $user ) |
||
190 | ) . '</p>'; |
||
191 | |||
192 | if ( ! empty( $connected_admins ) && count( $connected_admins ) > 1 ) { |
||
193 | echo '<form id="jp-switch-connection-owner" action="" method="post">'; |
||
194 | echo "<label for='owner'>" . esc_html__( 'You can choose to transfer connection ownership to one of these already-connected admins:', 'jetpack' ) . ' </label>'; |
||
195 | |||
196 | $connected_admin_ids = array_map( |
||
197 | function( $connected_admin ) { |
||
198 | return $connected_admin->ID; |
||
199 | }, |
||
200 | $connected_admins |
||
201 | ); |
||
202 | |||
203 | wp_dropdown_users( |
||
204 | array( |
||
205 | 'name' => 'owner', |
||
206 | 'include' => array_diff( $connected_admin_ids, array( $connection_owner_id ) ), |
||
207 | 'show' => 'display_name_with_login', |
||
208 | ) |
||
209 | ); |
||
210 | |||
211 | echo '<p>'; |
||
212 | submit_button( esc_html__( 'Set new connection owner', 'jetpack' ), 'primary', 'jp-switch-connection-owner-submit', false ); |
||
213 | echo '</p>'; |
||
214 | |||
215 | echo "<div id='jp-switch-user-results'></div>"; |
||
216 | echo '</form>'; |
||
217 | ?> |
||
218 | <script type="text/javascript"> |
||
219 | jQuery( document ).ready( function( $ ) { |
||
220 | $( '#jp-switch-connection-owner' ).on( 'submit', function( e ) { |
||
221 | var formData = $( this ).serialize(); |
||
222 | var submitBtn = document.getElementById( 'jp-switch-connection-owner-submit' ); |
||
223 | var results = document.getElementById( 'jp-switch-user-results' ); |
||
224 | |||
225 | submitBtn.disabled = true; |
||
226 | |||
227 | $.ajax( { |
||
228 | type : "POST", |
||
229 | url : "<?php echo esc_url( get_rest_url() . 'jetpack/v4/connection/owner' ); ?>", |
||
230 | data : formData, |
||
231 | headers : { |
||
232 | 'X-WP-Nonce': "<?php echo esc_js( wp_create_nonce( 'wp_rest' ) ); ?>", |
||
233 | }, |
||
234 | success: function() { |
||
235 | results.innerHTML = "<?php esc_html_e( 'Success!', 'jetpack' ); ?>"; |
||
236 | setTimeout( function() { |
||
237 | $( '#jetpack-notice-switch-connection-owner' ).hide( 'slow' ); |
||
238 | }, 1000 ); |
||
239 | } |
||
240 | } ).done( function() { |
||
241 | submitBtn.disabled = false; |
||
242 | } ); |
||
243 | |||
244 | e.preventDefault(); |
||
245 | return false; |
||
246 | } ); |
||
247 | } ); |
||
248 | </script> |
||
249 | <?php |
||
250 | } else { |
||
251 | 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>'; |
||
252 | $connect_url = \Jetpack::init()->build_connect_url( false, false, 'delete_connection_owner_notice' ); |
||
253 | echo "<a href='" . esc_url( $connect_url ) . "' target='_blank' rel='noopener noreferrer' class='button-primary'>" . esc_html__( 'Connect to WordPress.com', 'jetpack' ) . '</a>'; |
||
254 | } |
||
255 | |||
256 | echo '<p>'; |
||
257 | printf( |
||
258 | wp_kses( |
||
259 | /* translators: URL to Jetpack support doc regarding the primary user. */ |
||
260 | __( "<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' ), |
||
261 | array( |
||
262 | 'a' => array( |
||
263 | 'href' => true, |
||
264 | 'target' => true, |
||
265 | 'rel' => true, |
||
266 | ), |
||
267 | ) |
||
268 | ), |
||
269 | esc_url( Redirect::get_url( 'jetpack-support-primary-user' ) ) |
||
270 | ); |
||
271 | echo '</p>'; |
||
272 | echo '<p>'; |
||
273 | printf( |
||
274 | wp_kses( |
||
275 | /* translators: URL to contact Jetpack support. */ |
||
276 | __( 'As always, feel free to <a href="%s" target="_blank" rel="noopener noreferrer">contact our support team</a> if you have any questions.', 'jetpack' ), |
||
277 | array( |
||
278 | 'a' => array( |
||
279 | 'href' => true, |
||
280 | 'target' => true, |
||
281 | 'rel' => true, |
||
282 | ), |
||
283 | ) |
||
284 | ), |
||
285 | esc_url( Redirect::get_url( 'jetpack-contact-support' ) ) |
||
286 | ); |
||
287 | echo '</p>'; |
||
288 | echo '</div>'; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Dismisses a JITM feature class so that it will no longer be shown. |
||
293 | * |
||
294 | * @param string $id The id of the JITM that was dismissed. |
||
295 | * @param string $feature_class The feature class of the JITM that was dismissed. |
||
296 | * |
||
297 | * @return bool Always true. |
||
298 | */ |
||
299 | public function dismiss( $id, $feature_class ) { |
||
310 | |||
311 | /** |
||
312 | * Asks the wpcom API for the current message to display keyed on query string and message path |
||
313 | * |
||
314 | * @param string $message_path The message path to ask for. |
||
315 | * @param string $query The query string originally from the front end. |
||
316 | * @param bool $full_jp_logo_exists If there is a full Jetpack logo already on the page. |
||
317 | * |
||
318 | * @return array The JITM's to show, or an empty array if there is nothing to show |
||
319 | */ |
||
320 | public function get_messages( $message_path, $query, $full_jp_logo_exists ) { |
||
477 | |||
478 | } |
||
479 |
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..