PluginProbe ʕ •ᴥ•ʔ
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress / 4.10.0
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress v4.10.0
4.12.0 4.10.0 4.9.0 4.8.1 trunk 1.0 1.1 1.12.1 1.2.3 1.2.4 1.2.5 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5 1.5.1 1.5.2 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.5.1 1.6.6 1.6.6.1 1.6.6.2 1.6.6.3 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.9.0 1.9.1 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.8.1 1.9.9 1.9.9.1 1.9.9.2 1.9.9.3 2.0 2.0.1 2.1 2.1.1 2.1.2 2.1.3 2.10 2.11 2.11.1 2.12 2.12.1 2.12.2 2.12.3 2.12.4 2.13 2.14 2.14.1 2.15 2.15.1 2.16 2.16.1 2.17 2.17.1 2.18 2.18.1 2.18.2 2.18.3 2.19 2.19.1 2.19.2 2.19.3 2.2 2.2.1 2.3 2.3.1 2.3.10 2.3.2 2.3.3 2.3.4 2.3.6 2.3.7 2.3.8 2.3.9 2.4 2.4.1 2.4.1.1 2.4.1.2 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5 2.5.1 2.5.2 2.6 2.6.1 2.6.2 2.6.3 2.6.4 2.7 2.7.1 2.7.2 2.8 2.9 2.9.1 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.1 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.7.5 4.7.6 4.7.7
custom-facebook-feed / inc / Integrations / CFF_API_Connect.php
custom-facebook-feed / inc / Integrations Last commit date
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