Analytics
3 weeks ago
Divi
3 weeks ago
Elementor
3 weeks ago
CFF_API_Connect.php
3 weeks ago
CFF_Graph_Data.php
3 weeks ago
CFF_Graph_Url.php
3 weeks ago
CFF_Integration.php
3 weeks ago
index.php
3 weeks ago
CFF_API_Connect.php
197 lines
| 1 | <?php |
| 2 | |
| 3 | namespace CustomFacebookFeed\Integrations; |
| 4 | |
| 5 | if (!defined('ABSPATH')) { |
| 6 | exit; // Exit if accessed directly. |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * Class CFF_API_Connect |
| 11 | * Connect to the Facebook Graph and make API Calls |
| 12 | * |
| 13 | * @since 4.X |
| 14 | */ |
| 15 | class CFF_API_Connect |
| 16 | { |
| 17 | /** |
| 18 | * API url call |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | private $url; |
| 23 | |
| 24 | /** |
| 25 | * API url response |
| 26 | * |
| 27 | * @var object |
| 28 | */ |
| 29 | private $response; |
| 30 | |
| 31 | /** |
| 32 | * API call params |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | private $params; |
| 37 | |
| 38 | /** |
| 39 | * CFF_API_Connect constructor. |
| 40 | * |
| 41 | * @param mixed|array|string $connected_account_or_url either the connected account. |
| 42 | * data for this request or the complete url for the request. |
| 43 | * @param string $endpoint (optional) is optional only if the complete url is provided. |
| 44 | * otherwise is they key for the endpoint needed for the request (ex. "header"). |
| 45 | * @param array $params (optional) used with the connected account and endpoint to add. |
| 46 | * additional query parameters to the url if needed. |
| 47 | * @since 5.0 |
| 48 | */ |
| 49 | public function __construct($connected_account_or_url, $endpoint = '', $params = array()) |
| 50 | { |
| 51 | if (is_array($connected_account_or_url) && isset($connected_account_or_url['access_token'])) { |
| 52 | $this->set_url($connected_account_or_url, $endpoint, $params); |
| 53 | } elseif (!is_array($connected_account_or_url) && strpos($connected_account_or_url, 'https') !== false) { |
| 54 | $this->url = $connected_account_or_url; |
| 55 | } else { |
| 56 | $this->url = ''; |
| 57 | } |
| 58 | $this->params = $params; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * If url needs to be generated from the connected account, endpoint, |
| 63 | * and params, this function is used to do so. |
| 64 | * |
| 65 | * @param string $url (API URL). |
| 66 | */ |
| 67 | public function set_url_from_args($url) |
| 68 | { |
| 69 | $this->url = $url; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * GET API URL |
| 74 | * |
| 75 | * @return string |
| 76 | * |
| 77 | * @since 5.0 |
| 78 | */ |
| 79 | public function get_url() |
| 80 | { |
| 81 | return $this->url; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * If the server is unable to connect to the url, returns true |
| 86 | * |
| 87 | * @return bool |
| 88 | * |
| 89 | * @since 5.0 |
| 90 | */ |
| 91 | public function is_wp_error() |
| 92 | { |
| 93 | return is_wp_error($this->response); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Connect to the Facebook API and record the response |
| 98 | * |
| 99 | * @since 5.0 |
| 100 | */ |
| 101 | public function connect() |
| 102 | { |
| 103 | if (empty($this->url)) { |
| 104 | $this->response = array(); |
| 105 | return; |
| 106 | } |
| 107 | $args = array( |
| 108 | 'timeout' => 20 |
| 109 | ); |
| 110 | $response = wp_safe_remote_get($this->url, $args); |
| 111 | $body = json_decode(wp_remote_retrieve_body($response), true); |
| 112 | $this->response = $response; |
| 113 | if (is_wp_error($body) || isset($body['error'])) { |
| 114 | $this->log_fb_error(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns the response data from Facebook |
| 120 | * |
| 121 | * @return array|object |
| 122 | * |
| 123 | * @since 5.0 |
| 124 | */ |
| 125 | public function get_data($only_body = false) |
| 126 | { |
| 127 | if ($this->is_wp_error()) { |
| 128 | return array(); |
| 129 | } |
| 130 | if (!empty($this->response['body'])) { |
| 131 | $body = json_decode($this->response['body']); |
| 132 | return $body; |
| 133 | } else { |
| 134 | return $this->response; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /** |
| 140 | * Returns the response data from Facebook |
| 141 | * |
| 142 | * @return string |
| 143 | * |
| 144 | * @since 5.0 |
| 145 | */ |
| 146 | public function get_json_data() |
| 147 | { |
| 148 | return wp_json_encode($this->get_data(), true); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Returns the response from Facebook |
| 153 | * |
| 154 | * @return array|object |
| 155 | * |
| 156 | * @since 5.0 |
| 157 | */ |
| 158 | public function get_response() |
| 159 | { |
| 160 | return $this->response; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * Log Error |
| 166 | * |
| 167 | * @since 5.0 |
| 168 | */ |
| 169 | public function log_fb_error() |
| 170 | { |
| 171 | delete_option('cff_dismiss_critical_notice'); |
| 172 | $access_token_refresh_errors = array(10, 4, 200); |
| 173 | $response = json_decode($this->response['body'], true); |
| 174 | $page_id = isset($this->params['page_id']) ? $this->params['page_id'] : false; |
| 175 | $api_error_code = $response['error']['code']; |
| 176 | $ppca_error = false; |
| 177 | if (strpos($response['error']['message'], 'Public Content Access') !== false) { |
| 178 | $ppca_error = true; |
| 179 | } |
| 180 | |
| 181 | if (in_array((int) $api_error_code, $access_token_refresh_errors, true) && !$ppca_error) { |
| 182 | $pieces = explode('access_token=', $this->url); |
| 183 | $accesstoken_parts = isset($pieces[1]) ? explode('&', $pieces[1]) : 'none'; |
| 184 | $accesstoken = $accesstoken_parts[0]; |
| 185 | |
| 186 | $error = array( |
| 187 | 'accesstoken' => $accesstoken, |
| 188 | 'post_id' => get_the_ID(), |
| 189 | 'errorno' => $api_error_code |
| 190 | ); |
| 191 | \cff_main()->cff_error_reporter->add_error('accesstoken', $error, $page_id); |
| 192 | } else { |
| 193 | \cff_main()->cff_error_reporter->add_error('api', $response, $page_id); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 |