| Conditions | 35 |
| Paths | > 20000 |
| Total Lines | 131 |
| Code Lines | 70 |
| Lines | 15 |
| Ratio | 11.45 % |
| 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 |
||
| 3 | public static function register_and_build_request_body( $named_args ) { |
||
| 4 | $url_args = array( |
||
| 5 | 'home_url' => 'WP_HOME', |
||
| 6 | 'site_url' => 'WP_SITEURL', |
||
| 7 | ); |
||
| 8 | |||
| 9 | foreach ( $url_args as $url_arg => $constant_name ) { |
||
| 10 | // Anonymous functions were introduced in 5.3.0. So, if we're running on |
||
| 11 | // >= 5.3.0, use an anonymous function to set the home/siteurl value%s. |
||
| 12 | // |
||
| 13 | // Otherwise, fallback to setting the home/siteurl value via the WP_HOME and |
||
| 14 | // WP_SITEURL constants if the constant hasn't already been defined. |
||
| 15 | if ( isset( $named_args[ $url_arg ] ) ) { |
||
| 16 | if ( version_compare( phpversion(), '5.3.0', '>=' ) ) { |
||
| 17 | add_filter( $url_arg, function( $url ) use ( $url_arg, $named_args ) { |
||
| 18 | return $named_args[ $url_arg ]; |
||
| 19 | }, 11 ); |
||
| 20 | } else if ( ! defined( $constant_name ) ) { |
||
| 21 | define( $constant_name, $named_args[ $url_arg ] ); |
||
| 22 | } |
||
| 23 | } |
||
| 24 | } |
||
| 25 | |||
| 26 | // If Jetpack is currently connected, and is not in Safe Mode already, kick off a sync of the current |
||
| 27 | // functions/callables so that we can test if this site is in IDC. |
||
| 28 | if ( Jetpack::is_active() && ! Jetpack::validate_sync_error_idc_option() && Jetpack_Sync_Actions::sync_allowed() ) { |
||
| 29 | Jetpack_Sync_Actions::do_full_sync( array( 'functions' => true ) ); |
||
| 30 | Jetpack_Sync_Actions::$sender->do_full_sync(); |
||
|
|
|||
| 31 | } |
||
| 32 | |||
| 33 | if ( Jetpack::validate_sync_error_idc_option() ) { |
||
| 34 | return new WP_Error( |
||
| 35 | 'site_in_safe_mode', |
||
| 36 | esc_html__( 'Can not provision a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' ) |
||
| 37 | ); |
||
| 38 | } |
||
| 39 | |||
| 40 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 41 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 42 | |||
| 43 | if ( ! $blog_id || ! $blog_token || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) { |
||
| 44 | // this code mostly copied from Jetpack::admin_page_load |
||
| 45 | Jetpack::maybe_set_version_option(); |
||
| 46 | $registered = Jetpack::try_registration(); |
||
| 47 | if ( is_wp_error( $registered ) ) { |
||
| 48 | return $registered; |
||
| 49 | } elseif ( ! $registered ) { |
||
| 50 | return new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) ); |
||
| 51 | } |
||
| 52 | |||
| 53 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 54 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 55 | } |
||
| 56 | |||
| 57 | // if the user isn't specified, but we have a current master user, then set that to current user |
||
| 58 | if ( ! get_current_user_id() && $master_user_id = Jetpack_Options::get_option( 'master_user' ) ) { |
||
| 59 | wp_set_current_user( $master_user_id ); |
||
| 60 | } |
||
| 61 | |||
| 62 | $site_icon = ( function_exists( 'has_site_icon' ) && has_site_icon() ) |
||
| 63 | ? get_site_icon_url() |
||
| 64 | : false; |
||
| 65 | |||
| 66 | $auto_enable_sso = ( ! Jetpack::is_active() || Jetpack::is_module_active( 'sso' ) ); |
||
| 67 | |||
| 68 | /** This filter is documented in class.jetpack-cli.php */ |
||
| 69 | if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) { |
||
| 70 | $redirect_uri = add_query_arg( |
||
| 71 | array( 'action' => 'jetpack-sso', 'redirect_to' => urlencode( admin_url() ) ), |
||
| 72 | wp_login_url() // TODO: come back to Jetpack dashboard? |
||
| 73 | ); |
||
| 74 | } else { |
||
| 75 | $redirect_uri = admin_url(); |
||
| 76 | } |
||
| 77 | |||
| 78 | $request_body = array( |
||
| 79 | 'jp_version' => JETPACK__VERSION, |
||
| 80 | 'redirect_uri' => $redirect_uri, |
||
| 81 | ); |
||
| 82 | |||
| 83 | if ( $site_icon ) { |
||
| 84 | $request_body['site_icon'] = $site_icon; |
||
| 85 | } |
||
| 86 | |||
| 87 | if ( get_current_user_id() ) { |
||
| 88 | $user = wp_get_current_user(); |
||
| 89 | |||
| 90 | // role |
||
| 91 | $role = Jetpack::translate_current_user_to_role(); |
||
| 92 | $signed_role = Jetpack::sign_role( $role ); |
||
| 93 | |||
| 94 | $secrets = Jetpack::init()->generate_secrets( 'authorize' ); |
||
| 95 | |||
| 96 | // Jetpack auth stuff |
||
| 97 | $request_body['scope'] = $signed_role; |
||
| 98 | $request_body['secret'] = $secrets['secret_1']; |
||
| 99 | |||
| 100 | // User stuff |
||
| 101 | $request_body['user_id'] = $user->ID; |
||
| 102 | $request_body['user_email'] = $user->user_email; |
||
| 103 | $request_body['user_login'] = $user->user_login; |
||
| 104 | } |
||
| 105 | |||
| 106 | // optional additional params |
||
| 107 | View Code Duplication | if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) { |
|
| 108 | $request_body['wpcom_user_id'] = $named_args['wpcom_user_id']; |
||
| 109 | } |
||
| 110 | |||
| 111 | // override email of selected user |
||
| 112 | View Code Duplication | if ( isset( $named_args['wpcom_user_email'] ) && ! empty( $named_args['wpcom_user_email'] ) ) { |
|
| 113 | $request_body['user_email'] = $named_args['wpcom_user_email']; |
||
| 114 | } |
||
| 115 | |||
| 116 | View Code Duplication | if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) { |
|
| 117 | $request_body['plan'] = $named_args['plan']; |
||
| 118 | } |
||
| 119 | |||
| 120 | View Code Duplication | if ( isset( $named_args['onboarding'] ) && ! empty( $named_args['onboarding'] ) ) { |
|
| 121 | $request_body['onboarding'] = intval( $named_args['onboarding'] ); |
||
| 122 | } |
||
| 123 | |||
| 124 | View Code Duplication | if ( isset( $named_args['force_connect'] ) && ! empty( $named_args['force_connect'] ) ) { |
|
| 125 | $request_body['force_connect'] = intval( $named_args['force_connect'] ); |
||
| 126 | } |
||
| 127 | |||
| 128 | if ( isset( $request_body['onboarding'] ) && (bool) $request_body['onboarding'] ) { |
||
| 129 | Jetpack::create_onboarding_token(); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $request_body; |
||
| 133 | } |
||
| 134 | |||
| 256 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.