Conditions | 23 |
Paths | 2777 |
Total Lines | 157 |
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 |
||
320 | public function get_messages( $message_path, $query, $full_jp_logo_exists ) { |
||
321 | // Custom filters go here. |
||
322 | add_filter( 'jitm_woocommerce_services_msg', array( $this, 'jitm_woocommerce_services_msg' ) ); |
||
323 | add_filter( 'jitm_jetpack_woo_services_install', array( $this, 'jitm_jetpack_woo_services_install' ) ); |
||
324 | add_filter( 'jitm_jetpack_woo_services_activate', array( $this, 'jitm_jetpack_woo_services_activate' ) ); |
||
325 | |||
326 | $user = wp_get_current_user(); |
||
327 | |||
328 | // Unauthenticated or invalid requests just bail. |
||
329 | if ( ! $user ) { |
||
330 | return array(); |
||
331 | } |
||
332 | |||
333 | $user_roles = implode( ',', $user->roles ); |
||
334 | $site_id = \Jetpack_Options::get_option( 'id' ); |
||
335 | |||
336 | // Build our jitm request. |
||
337 | $path = add_query_arg( |
||
338 | array( |
||
339 | 'external_user_id' => urlencode_deep( $user->ID ), |
||
340 | 'user_roles' => urlencode_deep( $user_roles ), |
||
341 | 'query_string' => urlencode_deep( $query ), |
||
342 | 'mobile_browser' => jetpack_is_mobile( 'smart' ) ? 1 : 0, |
||
343 | '_locale' => get_user_locale(), |
||
344 | ), |
||
345 | sprintf( '/sites/%d/jitm/%s', $site_id, $message_path ) |
||
346 | ); |
||
347 | |||
348 | // Attempt to get from cache. |
||
349 | $envelopes = get_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ) ); |
||
350 | |||
351 | // If something is in the cache and it was put in the cache after the last sync we care about, use it. |
||
352 | $use_cache = false; |
||
353 | |||
354 | /** This filter is documented in class.jetpack.php */ |
||
355 | if ( apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) { |
||
356 | $use_cache = true; |
||
357 | } |
||
358 | |||
359 | if ( $use_cache ) { |
||
360 | $last_sync = (int) get_transient( 'jetpack_last_plugin_sync' ); |
||
361 | $from_cache = $envelopes && $last_sync > 0 && $last_sync < $envelopes['last_response_time']; |
||
362 | } else { |
||
363 | $from_cache = false; |
||
364 | } |
||
365 | |||
366 | // Otherwise, ask again. |
||
367 | if ( ! $from_cache ) { |
||
368 | $wpcom_response = Client::wpcom_json_api_request_as_blog( |
||
369 | $path, |
||
370 | '2', |
||
371 | array( |
||
372 | 'user_id' => $user->ID, |
||
373 | 'user_roles' => implode( ',', $user->roles ), |
||
374 | ), |
||
375 | null, |
||
376 | 'wpcom' |
||
377 | ); |
||
378 | |||
379 | // silently fail...might be helpful to track it? |
||
380 | if ( is_wp_error( $wpcom_response ) ) { |
||
381 | return array(); |
||
382 | } |
||
383 | |||
384 | $envelopes = json_decode( $wpcom_response['body'] ); |
||
385 | |||
386 | if ( ! is_array( $envelopes ) ) { |
||
387 | return array(); |
||
388 | } |
||
389 | |||
390 | $expiration = isset( $envelopes[0] ) ? $envelopes[0]->ttl : 300; |
||
391 | |||
392 | // Do not cache if expiration is 0 or we're not using the cache. |
||
393 | if ( 0 !== $expiration && $use_cache ) { |
||
394 | $envelopes['last_response_time'] = time(); |
||
395 | |||
396 | set_transient( 'jetpack_jitm_' . substr( md5( $path ), 0, 31 ), $envelopes, $expiration ); |
||
397 | } |
||
398 | } |
||
399 | |||
400 | $hidden_jitms = \Jetpack_Options::get_option( 'hide_jitm' ); |
||
401 | unset( $envelopes['last_response_time'] ); |
||
402 | |||
403 | /** |
||
404 | * Allow adding your own custom JITMs after a set of JITMs has been received. |
||
405 | * |
||
406 | * @since 6.9.0 |
||
407 | * @since 8.3.0 - Added Message path. |
||
408 | * |
||
409 | * @param array $envelopes array of existing JITMs. |
||
410 | * @param string $message_path The message path to ask for. |
||
411 | */ |
||
412 | $envelopes = apply_filters( 'jetpack_jitm_received_envelopes', $envelopes, $message_path ); |
||
413 | |||
414 | foreach ( $envelopes as $idx => &$envelope ) { |
||
415 | |||
416 | $dismissed_feature = isset( $hidden_jitms[ $envelope->feature_class ] ) && is_array( $hidden_jitms[ $envelope->feature_class ] ) ? $hidden_jitms[ $envelope->feature_class ] : null; |
||
417 | |||
418 | // If the this feature class has been dismissed and the request has not passed the ttl, skip it as it's been dismissed. |
||
419 | if ( is_array( $dismissed_feature ) && ( time() - $dismissed_feature['last_dismissal'] < $envelope->expires || $dismissed_feature['number'] >= $envelope->max_dismissal ) ) { |
||
420 | unset( $envelopes[ $idx ] ); |
||
421 | continue; |
||
422 | } |
||
423 | |||
424 | $this->tracking->record_user_event( |
||
425 | 'jitm_view_client', |
||
426 | array( |
||
427 | 'jitm_id' => $envelope->id, |
||
428 | ) |
||
429 | ); |
||
430 | |||
431 | $normalized_site_url = \Jetpack::build_raw_urls( get_home_url() ); |
||
432 | |||
433 | $url_params = array( |
||
434 | 'source' => "jitm-$envelope->id", |
||
435 | 'site' => $normalized_site_url, |
||
436 | 'u' => $user->ID, |
||
437 | ); |
||
438 | |||
439 | // Get affiliate code and add it to the array of URL parameters. |
||
440 | $aff = Partner::init()->get_partner_code( Partner::AFFILIATE_CODE ); |
||
441 | if ( '' !== $aff ) { |
||
442 | $url_params['aff'] = $aff; |
||
443 | } |
||
444 | |||
445 | $envelope->url = add_query_arg( $url_params, 'https://jetpack.com/redirect/' ); |
||
446 | |||
447 | $envelope->jitm_stats_url = \Jetpack::build_stats_url( array( 'x_jetpack-jitm' => $envelope->id ) ); |
||
448 | |||
449 | // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
||
450 | // $CTA is not valid per PHPCS, but it is part of the return from WordPress.com, so allowing. |
||
451 | if ( $envelope->CTA->hook ) { |
||
452 | $envelope->url = apply_filters( 'jitm_' . $envelope->CTA->hook, $envelope->url ); |
||
453 | unset( $envelope->CTA->hook ); |
||
454 | } |
||
455 | // phpcs:enable |
||
456 | |||
457 | if ( isset( $envelope->content->hook ) ) { |
||
458 | $envelope->content = apply_filters( 'jitm_' . $envelope->content->hook, $envelope->content ); |
||
459 | unset( $envelope->content->hook ); |
||
460 | } |
||
461 | |||
462 | // No point in showing an empty message. |
||
463 | if ( empty( $envelope->content->message ) ) { |
||
464 | unset( $envelopes[ $idx ] ); |
||
465 | continue; |
||
466 | } |
||
467 | |||
468 | $envelope->content->icon = $this->generate_icon( $envelope->content->icon, $full_jp_logo_exists ); |
||
469 | |||
470 | $jetpack = \Jetpack::init(); |
||
471 | $jetpack->stat( 'jitm', $envelope->id . '-viewed-' . JETPACK__VERSION ); |
||
472 | $jetpack->do_stats( 'server_side' ); |
||
473 | } |
||
474 | |||
475 | return $envelopes; |
||
476 | } |
||
477 | |||
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..