Conditions | 10 |
Paths | 7 |
Total Lines | 41 |
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 |
||
31 | public static function get_access_token() { |
||
32 | // If the site provides its own Mapbox access token, return it. |
||
33 | $service_api_key = Jetpack_Options::get_option( self::$site_option_key ); |
||
34 | if ( $service_api_key ) { |
||
35 | return self::format_access_token( $service_api_key ); |
||
36 | } |
||
37 | |||
38 | $site_id = self::get_wpcom_site_id(); |
||
39 | |||
40 | // If on WordPress.com, try to return the access token straight away. |
||
41 | if ( self::is_wpcom() && defined( 'WPCOM_MAPBOX_ACCESS_TOKEN' ) ) { |
||
42 | jetpack_require_lib( 'mapbox-blacklist' ); |
||
43 | return wpcom_is_site_blacklisted_from_map_block( $site_id ) |
||
44 | ? self::format_access_token() |
||
45 | : self::format_access_token( WPCOM_MAPBOX_ACCESS_TOKEN, 'wpcom' ); |
||
46 | } |
||
47 | |||
48 | // If not on WordPress.com or Atomic, return an empty access token. |
||
49 | if ( ! $site_id || ( ! self::is_wpcom() && ! jetpack_is_atomic_site() ) ) { |
||
50 | return self::format_access_token(); |
||
51 | } |
||
52 | |||
53 | // If there is a cached token, return it. |
||
54 | $cached_token = get_transient( self::$transient_key ); |
||
55 | if ( $cached_token ) { |
||
56 | return self::format_access_token( $cached_token, 'wpcom' ); |
||
57 | } |
||
58 | |||
59 | // Otherwise get it from the WordPress.com endpoint. |
||
60 | $request_url = 'https://public-api.wordpress.com/wpcom/v2/sites/' . $site_id . '/mapbox'; |
||
61 | $response = wp_remote_get( esc_url_raw( $request_url ) ); |
||
62 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
||
63 | return self::format_access_token(); |
||
64 | } |
||
65 | |||
66 | $response_body = json_decode( wp_remote_retrieve_body( $response ) ); |
||
67 | $wpcom_mapbox_access_token = $response_body->wpcom_mapbox_access_token; |
||
68 | |||
69 | set_transient( self::$transient_key, $wpcom_mapbox_access_token, HOUR_IN_SECONDS ); |
||
70 | return self::format_access_token( $wpcom_mapbox_access_token, 'wpcom' ); |
||
71 | } |
||
72 | |||
110 |