| Conditions | 17 |
| Paths | 292 |
| Total Lines | 102 |
| 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 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 | /* phpcs:ignore |
||
| 134 | debugging |
||
| 135 | error_log( "-- begin json api via jetpack debugging -- " ); |
||
| 136 | error_log( "METHOD: $method" ); |
||
| 137 | error_log( "URL: $url" ); |
||
| 138 | error_log( "POST BODY: $post_body" ); |
||
| 139 | error_log( "VERIFY_ARGS: " . print_r( $verify_api_user_args, 1 ) ); |
||
| 140 | error_log( "VERIFIED USER_ID: " . (int) $user_id ); |
||
| 141 | error_log( "-- end json api via jetpack debugging -- " ); |
||
| 142 | */ |
||
| 143 | |||
| 144 | if ( 'en' !== $locale ) { |
||
| 145 | // .org mo files are named slightly different from .com, and all we have is this the locale -- try to guess them. |
||
| 146 | $new_locale = $locale; |
||
| 147 | if ( strpos( $locale, '-' ) !== false ) { |
||
| 148 | $locale_pieces = explode( '-', $locale ); |
||
| 149 | $new_locale = $locale_pieces[0]; |
||
| 150 | $new_locale .= ( ! empty( $locale_pieces[1] ) ) ? '_' . strtoupper( $locale_pieces[1] ) : ''; |
||
| 151 | } else { |
||
| 152 | // .com might pass 'fr' because thats what our language files are named as, where core seems |
||
| 153 | // to do fr_FR - so try that if we don't think we can load the file. |
||
| 154 | if ( ! file_exists( WP_LANG_DIR . '/' . $locale . '.mo' ) ) { |
||
| 155 | $new_locale = $locale . '_' . strtoupper( $locale ); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | if ( file_exists( WP_LANG_DIR . '/' . $new_locale . '.mo' ) ) { |
||
| 160 | unload_textdomain( 'default' ); |
||
| 161 | load_textdomain( 'default', WP_LANG_DIR . '/' . $new_locale . '.mo' ); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | $old_user = wp_get_current_user(); |
||
| 166 | wp_set_current_user( $user_id ); |
||
| 167 | |||
| 168 | if ( $user_id ) { |
||
| 169 | $token_key = false; |
||
| 170 | } else { |
||
| 171 | $verified = ( new Connection_Manager() )->verify_xml_rpc_signature(); |
||
| 172 | $token_key = $verified['token_key']; |
||
| 173 | } |
||
| 174 | |||
| 175 | $token = ( new Tokens() )->get_access_token( $user_id, $token_key ); |
||
| 176 | if ( ! $token || is_wp_error( $token ) ) { |
||
| 177 | return false; |
||
| 178 | } |
||
| 179 | |||
| 180 | define( 'REST_API_REQUEST', true ); |
||
| 181 | define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' ); |
||
| 182 | |||
| 183 | // needed? |
||
| 184 | require_once ABSPATH . 'wp-admin/includes/admin.php'; |
||
| 185 | |||
| 186 | require_once JETPACK__PLUGIN_DIR . 'class.json-api.php'; |
||
| 187 | $api = WPCOM_JSON_API::init( $method, $url, $post_body ); |
||
| 188 | $api->token_details['user'] = $user_details; |
||
| 189 | require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php'; |
||
| 190 | |||
| 191 | $display_errors = ini_set( 'display_errors', 0 ); // phpcs:ignore WordPress.PHP.IniSet |
||
| 192 | ob_start(); |
||
| 193 | $api->serve( false ); |
||
| 194 | $output = ob_get_clean(); |
||
| 195 | ini_set( 'display_errors', $display_errors ); // phpcs:ignore WordPress.PHP.IniSet |
||
| 196 | |||
| 197 | $nonce = wp_generate_password( 10, false ); |
||
| 198 | $hmac = hash_hmac( 'md5', $nonce . $output, $token->secret ); |
||
| 199 | |||
| 200 | wp_set_current_user( isset( $old_user->ID ) ? $old_user->ID : 0 ); |
||
| 201 | |||
| 202 | return array( |
||
| 203 | (string) $output, |
||
| 204 | (string) $nonce, |
||
| 205 | (string) $hmac, |
||
| 206 | ); |
||
| 207 | } |
||
| 208 | } |
||
| 209 |