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