Traits
1 month ago
CTF_About_Us.php
1 month ago
CTF_Admin_Notices.php
1 month ago
CTF_Cache_Handler.php
1 month ago
CTF_Global_Settings.php
1 month ago
CTF_HTTP_Request.php
1 month ago
CTF_New_User.php
1 month ago
CTF_Notifications.php
1 month ago
CTF_Response.php
1 month ago
CTF_Support.php
1 month ago
CTF_Upgrader.php
1 month ago
CTF_View.php
1 month ago
PluginSilentUpgrader.php
1 month ago
PluginSilentUpgraderSkin.php
1 month ago
SB_Twitter_Cache.php
1 month ago
addon-functions.php
1 month ago
class-install-skin.php
1 month ago
CTF_HTTP_Request.php
125 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class CTF_HTTP_Request |
| 4 | * |
| 5 | * This class with make remote request |
| 6 | * |
| 7 | * @since 2.0 |
| 8 | */ |
| 9 | namespace TwitterFeed\Admin; |
| 10 | if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 11 | |
| 12 | class CTF_HTTP_Request { |
| 13 | |
| 14 | public function __construct() { |
| 15 | |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Make the HTTP remote request |
| 20 | * |
| 21 | * @param string $method |
| 22 | * @param string $url |
| 23 | * @param array|null $data |
| 24 | * |
| 25 | * @since 2.0 |
| 26 | * |
| 27 | * @return array|WP_Error |
| 28 | */ |
| 29 | public static function request( $method, $url, $data = null ) { |
| 30 | $args = array( |
| 31 | 'headers' => array( |
| 32 | 'Content-Type' => 'application/json', |
| 33 | ), |
| 34 | ); |
| 35 | |
| 36 | $args = array_merge( $args, $data ); |
| 37 | |
| 38 | if ( 'GET' === $method ) { |
| 39 | $request = wp_remote_get( $url, $args ); |
| 40 | } elseif ( 'DELETE' === $method ) { |
| 41 | $args['method'] = 'DELETE'; |
| 42 | $request = wp_remote_request( $url, $args ); |
| 43 | } elseif ( 'PATCH' === $method ) { |
| 44 | $args['method'] = 'PATCH'; |
| 45 | $request = wp_remote_request( $url, $args ); |
| 46 | } elseif ( 'PUT' === $method ) { |
| 47 | $args['method'] = 'PUT'; |
| 48 | $request = wp_remote_request( $url, $args ); |
| 49 | } else { |
| 50 | $args['method'] = 'POST'; |
| 51 | $request = wp_remote_post( $url, $args ); |
| 52 | } |
| 53 | |
| 54 | return $request; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Check if WP_Error returned |
| 59 | * |
| 60 | * @param array|WP_Error $request |
| 61 | * |
| 62 | * @since 2.0 |
| 63 | * |
| 64 | * @return array|WP_Error |
| 65 | */ |
| 66 | public static function is_error( $request ) { |
| 67 | return is_wp_error( $request ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the remote call status code |
| 72 | * |
| 73 | * @param array|WP_Error $request |
| 74 | * |
| 75 | * @since 2.0 |
| 76 | * |
| 77 | * @return array|WP_Error |
| 78 | */ |
| 79 | public static function status( $request ) { |
| 80 | if ( is_wp_error( $request ) ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | return wp_remote_retrieve_response_code( $request ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get the remote call body data |
| 89 | * |
| 90 | * @param array|WP_Error $request |
| 91 | * |
| 92 | * @since 2.0 |
| 93 | * |
| 94 | * @return array $response |
| 95 | */ |
| 96 | public static function data( $request ) { |
| 97 | $response = wp_remote_retrieve_body( $request ); |
| 98 | return json_decode( $response ); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * Get Request Params |
| 104 | * |
| 105 | * |
| 106 | * @since 2.0 |
| 107 | * |
| 108 | * @return array $request_settings |
| 109 | */ |
| 110 | public static function get_request_settings(){ |
| 111 | $options = get_option( 'ctf_options' ); |
| 112 | $consumer_key = ! empty( $options['consumer_key'] ) && $options['have_own_tokens'] ? $options['consumer_key'] : ''; |
| 113 | $consumer_secret = ! empty( $options['consumer_secret'] ) && $options['have_own_tokens'] ? $options['consumer_secret'] : ''; |
| 114 | $request_settings = array( |
| 115 | 'consumer_key' => $consumer_key, |
| 116 | 'consumer_secret' => $consumer_secret, |
| 117 | 'access_token' => $options['access_token'], |
| 118 | 'access_token_secret' => $options['access_token_secret'] |
| 119 | ); |
| 120 | return $request_settings; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | } |
| 125 |