Conditions | 23 |
Paths | 2777 |
Total Lines | 161 |
Lines | 0 |
Ratio | 0 % |
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 |
||
176 | public function get_messages( $message_path, $query, $full_jp_logo_exists ) { |
||
177 | // WooCommerce Services. |
||
178 | add_filter( 'jitm_woocommerce_services_msg', array( $this, 'jitm_woocommerce_services_msg' ) ); |
||
179 | add_filter( 'jitm_jetpack_woo_services_install', array( $this, 'jitm_jetpack_woo_services_install' ) ); |
||
180 | add_filter( 'jitm_jetpack_woo_services_activate', array( $this, 'jitm_jetpack_woo_services_activate' ) ); |
||
181 | |||
182 | // Creative Mail. |
||
183 | add_filter( 'jitm_jetpack_creative_mail_install', array( $this, 'jitm_jetpack_creative_mail_install' ) ); |
||
184 | add_filter( 'jitm_jetpack_creative_mail_activate', array( $this, 'jitm_jetpack_creative_mail_activate' ) ); |
||
185 | |||
186 | $user = wp_get_current_user(); |
||
187 | |||
188 | // Unauthenticated or invalid requests just bail. |
||
189 | if ( ! $user ) { |
||
190 | return array(); |
||
191 | } |
||
192 | |||
193 | $user_roles = implode( ',', $user->roles ); |
||
194 | $site_id = \Jetpack_Options::get_option( 'id' ); |
||
195 | |||
196 | // Build our jitm request. |
||
197 | $path = add_query_arg( |
||
198 | array( |
||
199 | 'external_user_id' => urlencode_deep( $user->ID ), |
||
200 | 'user_roles' => urlencode_deep( $user_roles ), |
||
201 | 'query_string' => urlencode_deep( $query ), |
||
202 | 'mobile_browser' => jetpack_is_mobile( 'smart' ) ? 1 : 0, |
||
203 | '_locale' => get_user_locale(), |
||
204 | ), |
||
205 | sprintf( '/sites/%d/jitm/%s', $site_id, $message_path ) |
||
206 | ); |
||
207 | |||
208 | // Attempt to get from cache. |
||
209 | $envelopes = get_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ) ); |
||
210 | |||
211 | // If something is in the cache and it was put in the cache after the last sync we care about, use it. |
||
212 | $use_cache = false; |
||
213 | |||
214 | /** This filter is documented in class.jetpack.php */ |
||
215 | if ( apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) { |
||
216 | $use_cache = true; |
||
217 | } |
||
218 | |||
219 | if ( $use_cache ) { |
||
220 | $last_sync = (int) get_transient( 'jetpack_last_plugin_sync' ); |
||
221 | $from_cache = $envelopes && $last_sync > 0 && $last_sync < $envelopes['last_response_time']; |
||
222 | } else { |
||
223 | $from_cache = false; |
||
224 | } |
||
225 | |||
226 | // Otherwise, ask again. |
||
227 | if ( ! $from_cache ) { |
||
228 | $wpcom_response = Client::wpcom_json_api_request_as_blog( |
||
229 | $path, |
||
230 | '2', |
||
231 | array( |
||
232 | 'user_id' => $user->ID, |
||
233 | 'user_roles' => implode( ',', $user->roles ), |
||
234 | ), |
||
235 | null, |
||
236 | 'wpcom' |
||
237 | ); |
||
238 | |||
239 | // silently fail...might be helpful to track it? |
||
240 | if ( is_wp_error( $wpcom_response ) ) { |
||
241 | return array(); |
||
242 | } |
||
243 | |||
244 | $envelopes = json_decode( $wpcom_response['body'] ); |
||
245 | |||
246 | if ( ! is_array( $envelopes ) ) { |
||
247 | return array(); |
||
248 | } |
||
249 | |||
250 | $expiration = isset( $envelopes[0] ) ? $envelopes[0]->ttl : 300; |
||
251 | |||
252 | // Do not cache if expiration is 0 or we're not using the cache. |
||
253 | if ( 0 !== $expiration && $use_cache ) { |
||
254 | $envelopes['last_response_time'] = time(); |
||
255 | |||
256 | set_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ), $envelopes, $expiration ); |
||
257 | } |
||
258 | } |
||
259 | |||
260 | $hidden_jitms = \Jetpack_Options::get_option( 'hide_jitm' ); |
||
261 | unset( $envelopes['last_response_time'] ); |
||
262 | |||
263 | /** |
||
264 | * Allow adding your own custom JITMs after a set of JITMs has been received. |
||
265 | * |
||
266 | * @since 6.9.0 |
||
267 | * @since 8.3.0 - Added Message path. |
||
268 | * |
||
269 | * @param array $envelopes array of existing JITMs. |
||
270 | * @param string $message_path The message path to ask for. |
||
271 | */ |
||
272 | $envelopes = apply_filters( 'jetpack_jitm_received_envelopes', $envelopes, $message_path ); |
||
273 | |||
274 | foreach ( $envelopes as $idx => &$envelope ) { |
||
275 | |||
276 | $dismissed_feature = isset( $hidden_jitms[ $envelope->feature_class ] ) && is_array( $hidden_jitms[ $envelope->feature_class ] ) ? $hidden_jitms[ $envelope->feature_class ] : null; |
||
277 | |||
278 | // If the this feature class has been dismissed and the request has not passed the ttl, skip it as it's been dismissed. |
||
279 | if ( is_array( $dismissed_feature ) && ( time() - $dismissed_feature['last_dismissal'] < $envelope->expires || $dismissed_feature['number'] >= $envelope->max_dismissal ) ) { |
||
280 | unset( $envelopes[ $idx ] ); |
||
281 | continue; |
||
282 | } |
||
283 | |||
284 | $this->tracking->record_user_event( |
||
285 | 'jitm_view_client', |
||
286 | array( |
||
287 | 'jitm_id' => $envelope->id, |
||
288 | ) |
||
289 | ); |
||
290 | |||
291 | $normalized_site_url = \Jetpack::build_raw_urls( get_home_url() ); |
||
292 | |||
293 | $url_params = array( |
||
294 | 'source' => "jitm-$envelope->id", |
||
295 | 'site' => $normalized_site_url, |
||
296 | 'u' => $user->ID, |
||
297 | ); |
||
298 | |||
299 | // Get affiliate code and add it to the array of URL parameters. |
||
300 | $aff = Partner::init()->get_partner_code( Partner::AFFILIATE_CODE ); |
||
301 | if ( '' !== $aff ) { |
||
302 | $url_params['aff'] = $aff; |
||
303 | } |
||
304 | |||
305 | $envelope->url = add_query_arg( $url_params, 'https://jetpack.com/redirect/' ); |
||
306 | |||
307 | $envelope->jitm_stats_url = \Jetpack::build_stats_url( array( 'x_jetpack-jitm' => $envelope->id ) ); |
||
308 | |||
309 | // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
||
310 | // $CTA is not valid per PHPCS, but it is part of the return from WordPress.com, so allowing. |
||
311 | if ( $envelope->CTA->hook ) { |
||
312 | $envelope->url = apply_filters( 'jitm_' . $envelope->CTA->hook, $envelope->url ); |
||
313 | unset( $envelope->CTA->hook ); |
||
314 | } |
||
315 | // phpcs:enable |
||
316 | |||
317 | if ( isset( $envelope->content->hook ) ) { |
||
318 | $envelope->content = apply_filters( 'jitm_' . $envelope->content->hook, $envelope->content ); |
||
319 | unset( $envelope->content->hook ); |
||
320 | } |
||
321 | |||
322 | // No point in showing an empty message. |
||
323 | if ( empty( $envelope->content->message ) ) { |
||
324 | unset( $envelopes[ $idx ] ); |
||
325 | continue; |
||
326 | } |
||
327 | |||
328 | $envelope->content->icon = $this->generate_icon( $envelope->content->icon, $full_jp_logo_exists ); |
||
329 | |||
330 | $jetpack = \Jetpack::init(); |
||
331 | $jetpack->stat( 'jitm', $envelope->id . '-viewed-' . JETPACK__VERSION ); |
||
332 | $jetpack->do_stats( 'server_side' ); |
||
333 | } |
||
334 | |||
335 | return $envelopes; |
||
336 | } |
||
337 | |||
339 |
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..