     1: <?php
     2: // @todo: Need to support HTTP PUT AND DETEE
     3: /**
     4:  * An HTTP Agent for RESTian that uses WordPress' WP_Http class and related functions to make HTTP requests.
     5:  */
     6: class RESTian_WordPress_Http_Agent extends RESTian_Http_Agent_Base {
     7:   /**
     8:    * Makes an HTTP request using WordPress' WP_Http class and related functions.
     9:    *
    10:    * @param RESTian_Request $request
    11:    * @param RESTian_Response $response
    12:    * @return RESTian_Response
    13:    */
    14:   function make_request( $request, $response ) {
    15:
    16:     switch ( $request->http_method ) {
    17:       case 'GET':
    18:         $url = $request->get_url();
    19:         $args = $this->get_args( $request );
    20:         $result = wp_remote_get( $url, $args );
    21:         break;
    22:       case 'POST':
    23:         if ( $content_type = $request->get_content_type() )
    24:           $request->add_header( 'Content-type', $content_type );
    25:         $url = $request->get_url();
    26:         $args = $this->get_args( $request );
    27:         $result = wp_remote_post( $url, $args );
    28:         break;
    29:       case 'PUT':
    30:         $result = new WP_Error( -1, 'HTTP PUT not yet supported.' );
    31:         break;
    32:       case 'DELETE':
    33:         $result = new WP_Error( -2, 'HTTP DELETE not yet supported.' );
    34:         break;
    35:     }
    36:
    37:     if ( method_exists( $request->client, 'filter_result' ) )
    38:       $result = $request->client->filter_result( $result, $response );
    39:
    40:     if ( is_wp_error( $result ) ) {
    41:       /**
    42:        * These errors likely won't be 100% compatible with the errors from CURL when standalone
    43:        */
    44:       $error_num = $result->get_error_code();
    45:       $response->set_http_error( $error_num, $result->get_error_message( $error_num ) );
    46:     } else {
    47:       $response->message = wp_remote_retrieve_response_message( $result );
    48:     }
    49:
    50:     $response->status_code = wp_remote_retrieve_response_code( $result );
    51:     $response->body = wp_remote_retrieve_body( $result );
    52:     $response->body = $request->client->apply_filters( 'result_body', $response->body, $response );
    53:
    54:     $response->result = $result;
    55:
    56:     return $response;
    57:   }
    58:
    59:   /**
    60:    * @param RESTian_Request $request
    61:    *
    62:    * @return array
    63:    */
    64:   function get_args( $request ) {
    65:     $args = array(
    66:       'method'      => $request->http_method,
    67:       'headers'     => $request->get_headers(),
    68:       'body'        => $request->get_body(),
    69:       'sslverify'   => $request->sslverify,
    70:       'user-agent'  => $request->client->get_user_agent(),
    71:     );
    72:     return $args;
    73:   }
    74:
    75:
    76: }
    77:
    78: