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