Services
1 month ago
AuthRoutine.php
1 month ago
CronUpdaterManager.php
1 month ago
ErrorReport.php
1 month ago
Request.php
1 month ago
SettingsFilter.php
1 month ago
SinglePostCache.php
1 month ago
TweetAggregator.php
1 month ago
TweetFilterer.php
1 month ago
TweetRepository.php
1 month ago
TweetSetModifier.php
1 month ago
Request.php
117 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Request |
| 4 | * |
| 5 | * Performs a request to the Smash Balloon Twitter API. |
| 6 | * |
| 7 | * @since 2.1 |
| 8 | */ |
| 9 | namespace TwitterFeed\SmashTwitter; |
| 10 | |
| 11 | class Request |
| 12 | { |
| 13 | protected $endpoint; |
| 14 | |
| 15 | protected $term; |
| 16 | |
| 17 | protected $args; |
| 18 | |
| 19 | protected $auth_token; |
| 20 | |
| 21 | protected $license; |
| 22 | |
| 23 | protected $error_code; |
| 24 | |
| 25 | protected $error_message; |
| 26 | |
| 27 | public function __construct($endpoint, $term, $args = array(), $auth_token = false) |
| 28 | { |
| 29 | $this->endpoint = $endpoint; |
| 30 | |
| 31 | $this->term = $term; |
| 32 | |
| 33 | $this->args = $args; |
| 34 | |
| 35 | $this->auth_token = $auth_token; |
| 36 | } |
| 37 | |
| 38 | public function fetch() |
| 39 | { |
| 40 | $headers = array( |
| 41 | 'Accept' => 'application/json', |
| 42 | 'Content-Type' => 'application/json' |
| 43 | ); |
| 44 | |
| 45 | if ($this->auth_token) { |
| 46 | $headers['Authorization'] = 'Bearer ' . $this->auth_token; |
| 47 | } |
| 48 | |
| 49 | $this->args['headers'] = $headers; |
| 50 | |
| 51 | $endpoint_relative_url = ''; |
| 52 | $method = 'post'; |
| 53 | $this->args['method'] = 'POST'; |
| 54 | if ( empty( $this->args['timeout'] ) ) { |
| 55 | $this->args['timeout'] = 20; |
| 56 | } |
| 57 | |
| 58 | if ( $this->endpoint === 'usertimeline' ) { |
| 59 | $method = 'get'; |
| 60 | $this->args['method'] = 'GET'; |
| 61 | $endpoint_relative_url = SMASH_TWITTER_TIMELINE_PATH . '?tweet_mode=extended&screen_name=' . urlencode( trim( $this->term ) ) . '&user_type=licenseless'; |
| 62 | } elseif ( $this->endpoint === 'register' ) { |
| 63 | $endpoint_relative_url = '1.1/auth/register'; |
| 64 | } elseif ( $this->endpoint === 'license' ) { |
| 65 | $endpoint_relative_url = '1.1/auth/license'; |
| 66 | } |
| 67 | |
| 68 | $endpoint_url = SMASH_TWITTER_URL . $endpoint_relative_url . SMASH_TWITTER_URL_EXTRA_GET_PARAMS; |
| 69 | |
| 70 | $args = $this->args; |
| 71 | |
| 72 | |
| 73 | if ( $method === 'get' ) { |
| 74 | $return = wp_remote_get( $endpoint_url, $args ); |
| 75 | } else { |
| 76 | $return = wp_remote_post( $endpoint_url, $args ); |
| 77 | } |
| 78 | |
| 79 | if ( is_wp_error( $return ) ) { |
| 80 | return $return; |
| 81 | } |
| 82 | |
| 83 | $response_code = wp_remote_retrieve_response_code( $return ); |
| 84 | if ( $response_code >= 200 && $response_code < 300 ) { |
| 85 | if ( isset( $return['body'] )) { |
| 86 | $body = $return['body']; |
| 87 | $decoded = json_decode( $body, true ); |
| 88 | |
| 89 | |
| 90 | if ( $decoded ) { |
| 91 | return $decoded; |
| 92 | } |
| 93 | } |
| 94 | } else { |
| 95 | if ( isset( $return['body'] )) { |
| 96 | $this->error_code = $response_code; |
| 97 | $this->error_message = $return['body']; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | return $return; |
| 104 | } |
| 105 | |
| 106 | public function get_error() { |
| 107 | if ( empty( $this->error_code ) ) { |
| 108 | return array(); |
| 109 | } |
| 110 | |
| 111 | return array( |
| 112 | 'code' => $this->error_code, |
| 113 | 'message' => $this->error_message |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 |