Yoast_Google_WPIO

WP based implementation of apiIO.

Defined (1)

The class is defined in the following location(s).

/vendor/yoast/api-libs/google/io/Google_WPIO.php  
  1. class Yoast_Google_WPIO extends Yoast_Google_IO { 
  2. private static $ENTITY_HTTP_METHODS = array( "POST" => null, "PUT" => null, "DELETE" => null ); 
  3. private static $HOP_BY_HOP = array( 
  4. 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization',  
  5. 'te', 'trailers', 'transfer-encoding', 'upgrade' ); 
  6.  
  7. /** 
  8. * Perform an authenticated / signed apiHttpRequest. 
  9. * This function takes the apiHttpRequest, calls apiAuth->sign on it 
  10. * (which can modify the request in what ever way fits the auth mechanism) 
  11. * and then calls apiWPIO::makeRequest on the signed request 
  12. * @param Yoast_Google_HttpRequest $request 
  13. * @return Yoast_Google_HttpRequest The resulting HTTP response including the 
  14. * responseHttpCode, responseHeaders and responseBody. 
  15. */ 
  16. public function authenticatedRequest( Yoast_Google_HttpRequest $request ) { 
  17. $request = Yoast_Google_Client::$auth->sign( $request ); 
  18.  
  19. return $this->makeRequest( $request ); 
  20.  
  21. /** 
  22. * Execute a apiHttpRequest 
  23. * @param Yoast_Google_HttpRequest $request the http request to be executed 
  24. * @return Yoast_Google_HttpRequest http request with the response http code, response 
  25. * headers and response body filled in 
  26. */ 
  27. public function makeRequest( Yoast_Google_HttpRequest $request ) { 
  28.  
  29. // First, check to see if we have a valid cached version. 
  30. $cached = $this->getCachedRequest( $request ); 
  31. if ( $cached !== false ) { 
  32. if ( ! $this->checkMustRevaliadateCachedRequest( $cached, $request ) ) { 
  33. return $cached; 
  34.  
  35. if ( array_key_exists( $request->getRequestMethod(), self::$ENTITY_HTTP_METHODS ) ) { 
  36. $request = $this->processEntityRequest( $request ); 
  37.  
  38. $params = array( 
  39. 'user-agent' => $request->getUserAgent(),  
  40. 'timeout' => 30,  
  41. 'sslverify' => false,  
  42. ); 
  43.  
  44. $curl_version = $this->get_curl_version(); 
  45. if ( $curl_version !== false ) { 
  46. if ( version_compare( $curl_version, '7.19.0', '<=' ) && version_compare( $curl_version, '7.19.8', '>' ) ) { 
  47. add_filter( 'http_api_transports', array( $this, 'filter_curl_from_transports' ) ); 
  48.  
  49. if ( $request->getPostBody() ) { 
  50. $params['body'] = $request->getPostBody(); 
  51.  
  52. $requestHeaders = $request->getRequestHeaders(); 
  53. if ( $requestHeaders && is_array( $requestHeaders ) ) { 
  54. $params['headers'] = $requestHeaders; 
  55.  
  56. // There might be some problems with decompressing, so we prevent this by setting the param to false 
  57. $params['decompress'] = false; 
  58.  
  59.  
  60. switch ( $request->getRequestMethod() ) { 
  61. case 'POST' : 
  62. $response = wp_remote_post( $request->getUrl(), $params ); 
  63. break; 
  64.  
  65. case 'GET' : 
  66. $response = wp_remote_get( $request->getUrl(), $params ); 
  67. break; 
  68. case 'DELETE' : 
  69. $params['method'] = 'DELETE'; 
  70. $response = wp_remote_get( $request->getUrl(), $params ); 
  71. break; 
  72.  
  73. $responseBody = wp_remote_retrieve_body( $response ); 
  74. $respHttpCode = wp_remote_retrieve_response_code( $response ); 
  75. $responseHeaders = wp_remote_retrieve_headers( $response ); 
  76.  
  77. if ( $respHttpCode == 304 && $cached ) { 
  78. // If the server responded NOT_MODIFIED, return the cached request. 
  79. $this->updateCachedRequest( $cached, $responseHeaders ); 
  80.  
  81. return $cached; 
  82.  
  83. // Fill in the apiHttpRequest with the response values 
  84. $request->setResponseHttpCode( $respHttpCode ); 
  85. $request->setResponseHeaders( $responseHeaders ); 
  86.  
  87. $request->setResponseBody( $responseBody ); 
  88. // Store the request in cache (the function checks to see if the request 
  89. // can actually be cached) 
  90. $this->setCachedRequest( $request ); 
  91.  
  92. // And finally return it 
  93. return $request; 
  94.  
  95. /** 
  96. * Remove Curl from the transport 
  97. * @param $transports 
  98. * @return mixed 
  99. */ 
  100. public function filter_curl_from_transports( $transports ) { 
  101. unset( $transports['curl'] ); 
  102.  
  103. return $transports; 
  104.  
  105. /** 
  106. * Set options that update default behavior. 
  107. * @param array $optParams Multiple options used by a session. 
  108. */ 
  109. public function setOptions( $optParams ) { 
  110.  
  111.  
  112. /** 
  113. * Get the current curl verison if curl is installed 
  114. * @return bool|string 
  115. */ 
  116. public function get_curl_version() { 
  117. if ( function_exists( 'curl_version' ) ) { 
  118. $curl = curl_version(); 
  119.  
  120. if ( isset( $curl['version'] ) ) { 
  121. return $curl['version']; 
  122.  
  123. return false; 
  124.