| Conditions | 17 | 
| Paths | 292 | 
| Total Lines | 91 | 
| 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 | ||
| 107 | 	public static function json_api( $args = array() ) { | ||
| 108 | $json_api_args = $args[0]; | ||
| 109 | $verify_api_user_args = $args[1]; | ||
| 110 | |||
| 111 | $method = (string) $json_api_args[0]; | ||
| 112 | $url = (string) $json_api_args[1]; | ||
| 113 | $post_body = is_null( $json_api_args[2] ) ? null : (string) $json_api_args[2]; | ||
| 114 | $user_details = (array) $json_api_args[4]; | ||
| 115 | $locale = (string) $json_api_args[5]; | ||
| 116 | |||
| 117 | 		if ( ! $verify_api_user_args ) { | ||
| 118 | $user_id = 0; | ||
| 119 | 		} elseif ( 'internal' === $verify_api_user_args[0] ) { | ||
| 120 | $user_id = (int) $verify_api_user_args[1]; | ||
| 121 | 			if ( $user_id ) { | ||
| 122 | $user = get_user_by( 'id', $user_id ); | ||
| 123 | 				if ( ! $user || is_wp_error( $user ) ) { | ||
| 124 | return false; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | 		} else { | ||
| 128 | $user_id = call_user_func( array( new Jetpack_XMLRPC_Server(), 'test_api_user_code' ), $verify_api_user_args ); | ||
| 129 | 			if ( ! $user_id ) { | ||
| 130 | return false; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | 		if ( 'en' !== $locale ) { | ||
| 135 | // .org mo files are named slightly different from .com, and all we have is this the locale -- try to guess them. | ||
| 136 | $new_locale = $locale; | ||
| 137 | 			if ( strpos( $locale, '-' ) !== false ) { | ||
| 138 | $locale_pieces = explode( '-', $locale ); | ||
| 139 | $new_locale = $locale_pieces[0]; | ||
| 140 | $new_locale .= ( ! empty( $locale_pieces[1] ) ) ? '_' . strtoupper( $locale_pieces[1] ) : ''; | ||
| 141 | 			} else { | ||
| 142 | // .com might pass 'fr' because thats what our language files are named as, where core seems | ||
| 143 | // to do fr_FR - so try that if we don't think we can load the file. | ||
| 144 | 				if ( ! file_exists( WP_LANG_DIR . '/' . $locale . '.mo' ) ) { | ||
| 145 | $new_locale = $locale . '_' . strtoupper( $locale ); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | 			if ( file_exists( WP_LANG_DIR . '/' . $new_locale . '.mo' ) ) { | ||
| 150 | unload_textdomain( 'default' ); | ||
| 151 | load_textdomain( 'default', WP_LANG_DIR . '/' . $new_locale . '.mo' ); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | $old_user = wp_get_current_user(); | ||
| 156 | wp_set_current_user( $user_id ); | ||
| 157 | |||
| 158 | 		if ( $user_id ) { | ||
| 159 | $token_key = false; | ||
| 160 | 		} else { | ||
| 161 | $verified = ( new Connection_Manager() )->verify_xml_rpc_signature(); | ||
| 162 | $token_key = $verified['token_key']; | ||
| 163 | } | ||
| 164 | |||
| 165 | $token = ( new Tokens() )->get_access_token( $user_id, $token_key ); | ||
| 166 | 		if ( ! $token || is_wp_error( $token ) ) { | ||
| 167 | return false; | ||
| 168 | } | ||
| 169 | |||
| 170 | define( 'REST_API_REQUEST', true ); | ||
| 171 | define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' ); | ||
| 172 | |||
| 173 | // needed? | ||
| 174 | require_once ABSPATH . 'wp-admin/includes/admin.php'; | ||
| 175 | |||
| 176 | require_once JETPACK__PLUGIN_DIR . 'class.json-api.php'; | ||
| 177 | $api = WPCOM_JSON_API::init( $method, $url, $post_body ); | ||
| 178 | $api->token_details['user'] = $user_details; | ||
| 179 | require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php'; | ||
| 180 | |||
| 181 | $display_errors = ini_set( 'display_errors', 0 ); // phpcs:ignore WordPress.PHP.IniSet | ||
| 182 | ob_start(); | ||
| 183 | $api->serve( false ); | ||
| 184 | $output = ob_get_clean(); | ||
| 185 | ini_set( 'display_errors', $display_errors ); // phpcs:ignore WordPress.PHP.IniSet | ||
| 186 | |||
| 187 | $nonce = wp_generate_password( 10, false ); | ||
| 188 | $hmac = hash_hmac( 'md5', $nonce . $output, $token->secret ); | ||
| 189 | |||
| 190 | wp_set_current_user( isset( $old_user->ID ) ? $old_user->ID : 0 ); | ||
| 191 | |||
| 192 | return array( | ||
| 193 | (string) $output, | ||
| 194 | (string) $nonce, | ||
| 195 | (string) $hmac, | ||
| 196 | ); | ||
| 197 | } | ||
| 198 | |||
| 211 |