PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/google-analytics-for-wordpress/admin/api-libs/google/io/Google_WPIO.php

https://gitlab.com/iamgraeme/royalmile
PHP | 165 lines | 76 code | 28 blank | 61 comment | 15 complexity | 74624117193ce05d56da5291b496cf98 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2010 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * WP based implementation of apiIO.
  19. *
  20. */
  21. class Yoast_Google_WPIO extends Yoast_Google_IO {
  22. private static $ENTITY_HTTP_METHODS = array( "POST" => null, "PUT" => null );
  23. private static $HOP_BY_HOP = array(
  24. 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization',
  25. 'te', 'trailers', 'transfer-encoding', 'upgrade' );
  26. /**
  27. * Perform an authenticated / signed apiHttpRequest.
  28. * This function takes the apiHttpRequest, calls apiAuth->sign on it
  29. * (which can modify the request in what ever way fits the auth mechanism)
  30. * and then calls apiWPIO::makeRequest on the signed request
  31. *
  32. * @param Yoast_Google_HttpRequest $request
  33. *
  34. * @return Yoast_Google_HttpRequest The resulting HTTP response including the
  35. * responseHttpCode, responseHeaders and responseBody.
  36. */
  37. public function authenticatedRequest( Yoast_Google_HttpRequest $request ) {
  38. $request = Yoast_Google_Client::$auth->sign( $request );
  39. return $this->makeRequest( $request );
  40. }
  41. /**
  42. * Execute a apiHttpRequest
  43. *
  44. * @param Yoast_Google_HttpRequest $request the http request to be executed
  45. *
  46. * @return Yoast_Google_HttpRequest http request with the response http code, response
  47. * headers and response body filled in
  48. */
  49. public function makeRequest( Yoast_Google_HttpRequest $request ) {
  50. // First, check to see if we have a valid cached version.
  51. $cached = $this->getCachedRequest( $request );
  52. if ( $cached !== false ) {
  53. if ( ! $this->checkMustRevaliadateCachedRequest( $cached, $request ) ) {
  54. return $cached;
  55. }
  56. }
  57. if ( array_key_exists( $request->getRequestMethod(),
  58. self::$ENTITY_HTTP_METHODS ) ) {
  59. $request = $this->processEntityRequest( $request );
  60. }
  61. $params = array(
  62. 'user-agent' => $request->getUserAgent(),
  63. 'timeout' => 30,
  64. 'sslverify' => false,
  65. );
  66. $curl_version = $this->get_curl_version();
  67. if( $curl_version !== false ) {
  68. if ( version_compare( $curl_version, '7.19.0', '<=' ) && version_compare( $curl_version, '7.19.8', '>' ) ) {
  69. add_filter( 'http_api_transports', array( $this, 'filter_curl_from_transports' ) );
  70. }
  71. }
  72. if ( $request->getPostBody() ) {
  73. $params['body'] = $request->getPostBody();
  74. }
  75. $requestHeaders = $request->getRequestHeaders();
  76. if ( $requestHeaders && is_array( $requestHeaders ) ) {
  77. $params['headers'] = $requestHeaders;
  78. }
  79. switch ( $request->getRequestMethod() ) {
  80. case 'POST' :
  81. $response = wp_remote_post( $request->getUrl(), $params );
  82. break;
  83. case 'GET' :
  84. $response = wp_remote_get( $request->getUrl(), $params );
  85. break;
  86. }
  87. $responseBody = wp_remote_retrieve_body( $response );
  88. $respHttpCode = wp_remote_retrieve_response_code( $response );
  89. $responseHeaders = wp_remote_retrieve_headers( $response );
  90. if ( $respHttpCode == 304 && $cached ) {
  91. // If the server responded NOT_MODIFIED, return the cached request.
  92. $this->updateCachedRequest( $cached, $responseHeaders );
  93. return $cached;
  94. }
  95. // Fill in the apiHttpRequest with the response values
  96. $request->setResponseHttpCode( $respHttpCode );
  97. $request->setResponseHeaders( $responseHeaders );
  98. $request->setResponseBody( $responseBody );
  99. // Store the request in cache (the function checks to see if the request
  100. // can actually be cached)
  101. $this->setCachedRequest( $request );
  102. // And finally return it
  103. return $request;
  104. }
  105. /**
  106. * Remove Curl from the transport
  107. *
  108. * @param $transports
  109. *
  110. * @return mixed
  111. */
  112. public function filter_curl_from_transports( $transports ) {
  113. unset( $transports['curl'] );
  114. return $transports;
  115. }
  116. /**
  117. * Set options that update default behavior.
  118. *
  119. * @param array $optParams Multiple options used by a session.
  120. */
  121. public function setOptions( $optParams ) {
  122. }
  123. /**
  124. * Get the current curl verison if curl is installed
  125. *
  126. * @return bool|string
  127. */
  128. public function get_curl_version() {
  129. if ( function_exists( 'curl_version' ) ) {
  130. $curl = curl_version();
  131. if ( isset( $curl['version'] ) ) {
  132. return $curl['version'];
  133. }
  134. }
  135. return false;
  136. }
  137. }