MonsterInsights_GA_IO

The Google Analytics for WordPress by MonsterInsights MonsterInsights GA IO class.

Defined (1)

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

/assets/lib/google/class-ga-io.php  
  1. class MonsterInsights_GA_IO extends MonsterInsights_GA_Lib_IO_Abstract { 
  2. const TIMEOUT = "timeout"; 
  3. private $options = array(); 
  4.  
  5. private static $ENTITY_HTTP_METHODS = array( "POST" => null, "PUT" => null, "DELETE" => null ); 
  6. private static $HOP_BY_HOP = array( 
  7. 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization',  
  8. 'te', 'trailers', 'transfer-encoding', 'upgrade' ); 
  9.  
  10. /** 
  11. * Execute a apiHttpRequest 
  12. * @param Yoast_Google_HttpRequest $request the http request to be executed 
  13. * @return Yoast_Google_HttpRequest http request with the response http code, response 
  14. * headers and response body filled in 
  15. */ 
  16. public function executeRequest( MonsterInsights_GA_Lib_Http_Request $request ) { 
  17.  
  18. // First, check to see if we have a valid cached version. 
  19. $cached = $this->getCachedRequest( $request ); 
  20. if ( $cached !== false ) { 
  21. if ( ! $this->checkMustRevaliadateCachedRequest( $cached, $request ) ) { 
  22. return $cached; 
  23.  
  24. if ( array_key_exists( $request->getRequestMethod(), self::$ENTITY_HTTP_METHODS ) ) { 
  25. $request = $this->processEntityRequest( $request ); 
  26.  
  27. $params = array( 
  28. 'user-agent' => $request->getUserAgent(),  
  29. 'timeout' => 30,  
  30. 'sslverify' => false,  
  31. ); 
  32.  
  33. $curl_version = $this->get_curl_version(); 
  34. if ( $curl_version !== false ) { // @todo fix this 
  35. if ( version_compare( $curl_version, '7.19.0', '<=' ) && version_compare( $curl_version, '7.19.8', '>' ) ) { 
  36. add_filter( 'http_api_transports', array( $this, 'filter_curl_from_transports' ) ); 
  37.  
  38. if ( $request->getPostBody() ) { 
  39. $params['body'] = $request->getPostBody(); 
  40.  
  41. $requestHeaders = $request->getRequestHeaders(); 
  42. if ( $requestHeaders && is_array( $requestHeaders ) ) { 
  43. $params['headers'] = $requestHeaders; 
  44.  
  45. // There might be some problems with decompressing, so we prevent this by setting the param to false 
  46. $params['decompress'] = false; 
  47.  
  48.  
  49. switch ( $request->getRequestMethod() ) { 
  50. case 'POST' : 
  51. $response = wp_remote_post( $request->getUrl(), $params ); 
  52. break; 
  53.  
  54. case 'GET' : 
  55. $response = wp_remote_get( $request->getUrl(), $params ); 
  56. break; 
  57. case 'DELETE' : 
  58. $params['method'] = 'DELETE'; 
  59. $response = wp_remote_get( $request->getUrl(), $params ); 
  60. break; 
  61.  
  62. $responseBody = wp_remote_retrieve_body( $response ); 
  63. $respHttpCode = wp_remote_retrieve_response_code( $response ); 
  64. $responseHeaders = wp_remote_retrieve_headers( $response ); 
  65.  
  66.  
  67. $this->client->getLogger()->debug( 
  68. 'Stream response',  
  69. array( 
  70. 'code' => $respHttpCode,  
  71. 'headers' => $responseHeaders,  
  72. 'body' => $responseBody,  
  73. ); 
  74.  
  75. // And finally return it 
  76. return array( $responseBody, $responseHeaders, $respHttpCode ); 
  77.  
  78. /** 
  79. * Remove Curl from the transport 
  80. * @param $transports 
  81. * @return mixed 
  82. */ 
  83. public function filter_curl_from_transports( $transports ) { 
  84. unset( $transports['curl'] ); 
  85.  
  86. return $transports; 
  87.  
  88. /** 
  89. * Set options that update default behavior. 
  90. * @param array $optParams Multiple options used by a session. 
  91. */ 
  92. public function setOptions($options) { 
  93. $this->options = $options + $this->options; 
  94.  
  95. /** 
  96. * Get the current curl verison if curl is installed 
  97. * @return bool|string 
  98. */ 
  99. public function get_curl_version() { 
  100. if ( function_exists( 'curl_version' ) ) { 
  101. $curl = curl_version(); 
  102.  
  103. if ( isset( $curl['version'] ) ) { 
  104. return $curl['version']; 
  105.  
  106. return false; 
  107.  
  108. /** 
  109. * Set the maximum request time in seconds. 
  110. * @param $timeout in seconds 
  111. */ 
  112. public function setTimeout($timeout) 
  113. $this->options[self::TIMEOUT] = $timeout; 
  114.  
  115. /** 
  116. * Get the maximum request time in seconds. 
  117. * @return timeout in seconds 
  118. */ 
  119. public function getTimeout() 
  120. return $this->options[self::TIMEOUT]; 
  121.  
  122. /** 
  123. * Test for the presence of a cURL header processing bug 
  124. * {@inheritDoc} 
  125. * @return boolean 
  126. */ 
  127. protected function needsQuirk() 
  128. return false; 
  129.