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_Graph_Data.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_Graph_Data.php
719 lines
1 <?php
2
3 namespace CustomFacebookFeed\Integrations;
4
5 use CustomFacebookFeed\CFF_Cache;
6 use CustomFacebookFeed\CFF_Group_Posts;
7 use CustomFacebookFeed\CFF_Utils;
8 use CustomFacebookFeed\SB_Facebook_Data_Encryption;
9
10 if (!defined('ABSPATH')) {
11 exit; // Exit if accessed directly.
12 }
13
14 /**
15 * Class CFF_Graph_Data
16 * Build API cal URLs, Logic to get JSON from cache, backup or API
17 * Error Handling
18 *
19 * @since 4.X
20 */
21 class CFF_Graph_Data
22 {
23 /**
24 * Feed ID
25 *
26 * @var int
27 */
28 private $feed_id;
29
30 /**
31 * Page ID
32 *
33 * @var string
34 */
35 private $page_id;
36
37
38 /**
39 * Page IDs used for Multifeed
40 *
41 * @var string
42 */
43 private $page_ids;
44
45 /**
46 * Feed Setting
47 *
48 * @var array
49 */
50 private $feed_settings;
51
52 /**
53 * Feed Type
54 *
55 * @var string
56 */
57 private $feed_type;
58
59 /**
60 * Graph API URL
61 *
62 * @var string
63 */
64 private $graph_url;
65
66 /**
67 * Is Multifeed
68 *
69 * @var boolean
70 */
71 private $is_multifeed;
72
73 /**
74 * Is Event Feed
75 *
76 * @var boolean
77 */
78 private $is_event;
79
80 /**
81 * Is Group Source
82 *
83 * @var boolean
84 */
85 private $is_group;
86
87 /**
88 * Is Album Work Around
89 *
90 * @var boolean
91 */
92 private $is_album_workaround;
93
94 /**
95 * Cache Seconds
96 *
97 * @var int
98 */
99 private $cache_seconds;
100
101 /**
102 * Shortcode Atts
103 *
104 * @var array
105 */
106 private $shortcode_atts;
107
108
109 /**
110 * Post Limit
111 *
112 * @var int
113 */
114 private $post_limit;
115
116 /**
117 * Locale
118 *
119 * @var string
120 */
121 private $locale;
122
123 /**
124 * Transient Name
125 *
126 * @var string
127 */
128 private $transient_name;
129
130 /**
131 * Legacy Feeds
132 *
133 * @var boolean
134 */
135 private $is_legacy;
136
137 /**
138 * Access Token
139 *
140 * @var string
141 */
142 private $access_token;
143
144 /**
145 * Next Set Of Posts URL
146 *
147 * @var array
148 */
149 private $next_urls_arr_safe;
150
151
152 /**
153 * Feed Cache
154 *
155 * @var CFF_Cache
156 */
157 private $feed_cache;
158
159 /**
160 * Feed Page
161 *
162 * @var int
163 */
164 private $feed_page;
165
166 /**
167 * Post JSON Data
168 *
169 * @var object
170 */
171 private $posts_json;
172
173 /**
174 * Cache Type
175 *
176 * @var string
177 */
178 private $cache_type;
179
180 /**
181 * Cache Page Type
182 *
183 * @var string
184 */
185 private $cache_type_page;
186
187
188
189 /**
190 * Is Customizer
191 *
192 * @var boolean
193 */
194 private $is_customizer;
195
196 /**
197 * Is Customizer
198 *
199 * @var SB_Facebook_Data_Encryption
200 */
201 private $encryption;
202
203 /**
204 * Previeous Page Data
205 *
206 * @var array
207 */
208 private $prev_page_data;
209
210 /**
211 * CFF_Graph_Data constructor.
212 *
213 * @param int $page_id Source Page/Group ID
214 * @param int $feed_id the Feed ID
215 * @param array $feed_settings list of Feed Settings
216 * @since 5.0
217 */
218 public function __construct($page_id, $page_ids, $feed_id, $feed_settings, $data_att_html, $next_urls_arr_safe, $is_customizer = false, $prev_page_data = [])
219 {
220
221 $this->page_id = $page_id;
222 $this->feed_id = $feed_id;
223 $this->feed_settings = $feed_settings;
224 $this->is_customizer = $is_customizer;
225 $this->next_urls_arr_safe = $next_urls_arr_safe;
226 $this->feed_type = $this->feed_settings['feedtype'];
227 $this->is_group = $this->feed_settings['pagetype'] === 'group';
228 $this->encryption = new SB_Facebook_Data_Encryption();
229 $this->page_ids = $page_ids;
230 $this->prev_page_data = $prev_page_data;
231 $this->locale = !empty($this->feed_settings['locale']) ? $this->feed_settings['locale'] : get_option('cff_locale', 'en_US');
232 $this->shortcode_atts = is_array($data_att_html) ? $data_att_html : json_decode(str_replace('&quot;', '"', (isset($data_att_html) ? $data_att_html : '')), true);
233 $this->post_limit = $this->get_post_limit();
234
235 $this->transient_name = $this->get_transient_name();
236 $this->generate_feed_id_and_legacy();
237 $this->cache_seconds = $this->get_cache_seconds();
238 $this->access_token = $this->get_page_access_token();
239
240 $misc_args = [
241 'token' => $this->access_token,
242 'limit' => $this->post_limit,
243 'locale' => $this->locale,
244 'data_att_html' => $this->shortcode_atts
245 ];
246
247 $this->graph_url = CFF_Graph_Url::get_url($this->feed_type, $this->page_id, $this->feed_settings, $misc_args);
248 $this->feed_page = isset($this->shortcode_atts['feedPage']) ? $this->shortcode_atts['feedPage'] : 1;
249 $this->feed_cache = new CFF_Cache($this->feed_id, $this->feed_page, $this->cache_seconds, $this->is_legacy);
250 $this->feed_cache->retrieve_and_set();
251 $this->set_cache_type();
252 }
253
254
255
256 /**
257 * Generates Set the Feed ID and if Is Legacy
258 *
259 * @since 5.0
260 */
261 public function generate_feed_id_and_legacy()
262 {
263 if (!empty($this->shortcode_atts['feed'])) {
264 $this->feed_id = intval($this->shortcode_atts['feed']);
265 $this->is_legacy = false;
266 } else {
267 $this->feed_id = $this->transient_name;
268 $this->is_legacy = true;
269 }
270
271 $this->feed_id = $this->is_customizer ? '*' . $this->feed_id : $this->feed_id;
272 }
273
274 /**
275 * Get Page Ids
276 *
277 * @since 5.0
278 */
279 public function get_page_ids()
280 {
281 return [$this->page_id];
282 }
283
284 /**
285 * Get Post JSON
286 *
287 * @since 5.0
288 */
289 public function get_posts_json()
290 {
291 return $this->posts_json;
292 }
293
294 /**
295 * Set Post JSON
296 *
297 * @var object $posts_json
298 *
299 * @since 5.0
300 */
301 public function set_posts_json($posts_json)
302 {
303 $this->posts_json = $posts_json;
304 }
305
306 /**
307 * Set The Graph URL
308 *
309 * @since 5.0
310 */
311 public function set_graph_url($graph_url)
312 {
313 $this->graph_url = $graph_url;
314 }
315
316 /**
317 * Get Page Access Token
318 *
319 * @since 5.0
320 */
321 public function get_page_access_token()
322 {
323 $access_token = $this->feed_settings['accesstoken'];
324 return $this->encryption->maybe_decrypt($access_token);
325 }
326
327
328 /**
329 * Get Post Limit
330 *
331 * @since 5.0
332 */
333 public function get_post_limit()
334 {
335 $post_limit = $this->feed_settings['limit'];
336 $show_posts = isset($this->feed_settings['minnum']) ? $this->feed_settings['minnum'] : $this->feed_settings['num'];
337 $show_posts = empty($show_posts) || $show_posts === 0 ? 25 : intval($show_posts);
338 if (!isset($post_limit) || empty($post_limit)) {
339 switch ($show_posts) {
340 case $show_posts >= 50:
341 $post_limit = $show_posts + 7;
342 break;
343 case $show_posts < 50:
344 $post_limit = $show_posts + 5;
345 break;
346 case $show_posts < 25:
347 $post_limit = $show_posts + 4;
348 break;
349 case $show_posts < 10:
350 $post_limit = $show_posts + 3;
351 break;
352 case $show_posts < 6:
353 $post_limit = $show_posts + 2;
354 break;
355 case $show_posts < 2:
356 $post_limit = $show_posts + 1;
357 break;
358 }
359
360 $post_limit = $post_limit > 100 ? 100 : $post_limit;
361 $post_limit = $show_posts === '0' || $show_posts === 0 ? 1 : $post_limit;
362 }
363 $post_limit = $this->feed_settings['timelinepag'] === 'paging' ? $show_posts : $post_limit;
364 $post_limit = $this->feed_settings['gridpag'] === 'cursor' ? $show_posts : $post_limit;
365 if (isset($this->feed_settings['limit']) || $this->feed_settings['limit'] === '') {
366 $post_limit = intval($show_posts);
367 }
368 return $post_limit;
369 }
370
371
372 /**
373 * Get Feed Transient Name
374 *
375 * @since 5.0
376 */
377 public function get_transient_name()
378 {
379 return $this->build_default_transient_name();
380 }
381
382 /**
383 * Build Default Transient Name
384 *
385 * @since 5.0
386 */
387 public function build_default_transient_name()
388 {
389 $page_id_caching = ($this->feed_settings['playlist']) ? $this->feed_settings['playlist'] : $this->page_id;
390 $trans_items_arr = array(
391 'page_id' => $page_id_caching,
392 'post_limit' => substr($this->post_limit, 0, 3),
393 'show_posts_by' => substr($this->feed_settings['showpostsby'], 0, 2),
394 'locale' => $this->locale
395 );
396 $trans_arr_item_count = 1;
397
398
399 $albums_only = $this->check_onlyone_feed_type('album');
400 $photos_only = $this->check_onlyone_feed_type('photo');
401 $videos_only = $this->check_onlyone_feed_type('video');
402 $reviews_only = $this->check_onlyone_feed_type('review');
403 $trans_items_arr['albums_only'] = intval($albums_only);
404 $trans_items_arr['photos_only'] = intval($photos_only);
405 $trans_items_arr['videos_only'] = intval($videos_only);
406 $trans_items_arr['reviews'] = intval($reviews_only);
407 if ($albums_only) {
408 $trans_items_arr['albums_source'] = $this->feed_settings['albumsource'];
409 }
410
411 $arr_item_max_length = floor(28 / $trans_arr_item_count);
412 $arr_item_max_length_half = floor($arr_item_max_length / 2);
413 $transient_name = 'cff_';
414 foreach ($trans_items_arr as $key => $value) {
415 if ($value !== false) {
416 if ($key === 'page_id' || $key === 'featured_post' || $key === 'from' || $key === 'until') {
417 $transient_name .= substr($value, 0, $arr_item_max_length_half) . substr($value, $arr_item_max_length_half * -1);
418 }
419 if ($key === 'locale') {
420 $transient_name .= substr($value, 0, 2);
421 }
422 if ($key === 'post_limit' || $key === 'show_posts_by') {
423 $transient_name .= substr($value, 0, 3);
424 }
425 }
426 }
427 $transient_name = substr($transient_name, 0, 45);
428 return $transient_name;
429 }
430
431
432
433
434
435 /**
436 * Check if Only One Feed Type
437 * Function to check if Only => Album/Link/Video/Photo...
438 *
439 * @since 5.0
440 */
441 public function check_onlyone_feed_type($type)
442 {
443 $types_array = ['link', 'video', 'photo', 'album', 'status', 'review', 'event'];
444 unset($types_array[array_search($type, $types_array)]);
445 $feed_types = $this->feed_settings['type'];
446 $is_onlyone = CFF_Utils::stripos($type, 'event') !== false;
447 foreach ($types_array as $s_type) {
448 if (CFF_Utils::stripos($feed_types, $s_type) !== false) {
449 $is_onlyone = false;
450 }
451 }
452 return $is_onlyone;
453 }
454
455 /**
456 * Get Feed Cache Seconds
457 *
458 * @since 5.0
459 */
460 public function get_cache_seconds()
461 {
462 $cache_type = $this->feed_settings['cachetype'];
463
464 if ($cache_type === 'background') {
465 return 7 * DAY_IN_SECONDS;
466 }
467
468 $cache_unit = 60;
469 $cache_time = (intval($this->feed_settings['cachetime']) < 1) ? 1 : $this->feed_settings['cachetime'];
470
471 switch ($this->feed_settings['cacheunit']) {
472 case ('hour' || 'hours' || 0):
473 $cache_unit = 60 * 60;
474 break;
475 case ('day' || 'days'):
476 $cache_unit = 60 * 60 * 24;
477 break;
478 }
479
480 return intval($cache_unit) * intval($cache_time);
481 }
482
483
484 /**
485 * Get Feed Data
486 *
487 * @since 5.0
488 */
489 public function set_cache_type()
490 {
491 $cache_type = 'posts';
492 if (strpos($this->transient_name, 'cff_header_') !== false) {
493 $cache_type = 'header';
494 }
495 $cache_type_page = $cache_type;
496 if ($cache_type === 'posts' && $this->feed_page > 1) {
497 $cache_type_page = 'posts_' . $this->feed_page;
498 }
499 $this->cache_type = $cache_type;
500 $this->cache_type_page = $cache_type_page;
501 }
502
503 /**
504 * Get Posts JSON By Feed Type
505 *
506 * @since 5.0
507 */
508 public function get_posts_json_byfeed_type()
509 {
510 if ($this->is_group && !CFF_Graph_Data::should_make_group_call()) {
511 \cff_main()->cff_error_reporter->add_group_deprecation_error($this->page_id);
512 return [];
513 }
514 $posts_json = $this->get_remote_data();
515 return $posts_json;
516 }
517
518
519 /**
520 * Get Group Posts
521 *
522 * @since 5.0
523 */
524 public function get_group_posts()
525 {
526 $groups_post = new CFF_Group_Posts($this->page_id, $this->feed_settings, $this->graph_url, $this->shortcode_atts, false);
527 $latest_record_date = (isset($this->next_urls_arr_safe['latest_record_date'])) ? $this->next_urls_arr_safe['latest_record_date'] : false;
528 $groups_post_result = $groups_post->init_group_posts($this->posts_json, $latest_record_date, $this->post_limit);
529 return $groups_post_result['posts_json'];
530 }
531
532 /**
533 * Get Group Posts
534 *
535 * @since 5.0
536 */
537 public static function should_make_group_call()
538 {
539 return time() < strtotime('2024-4-22');
540 }
541
542 /**
543 * Get Feed Data
544 *
545 * @since 5.0
546 */
547 public function get_feed_data()
548 {
549 if ($this->feed_cache->is_expired($this->cache_type) || $this->is_customizer) {
550 $posts_json = $this->get_posts_json_byfeed_type();
551 $this->process_posts_json_response($posts_json);
552 } else {
553 $this->process_cached_posts_json();
554 }
555 if ($this->is_group) {
556 $posts_json = $this->get_group_posts();
557 $this->set_posts_json($posts_json);
558 }
559 return $this->get_posts_json();
560 }
561
562 /**
563 * Process Posts JSON Response
564 *
565 * @since 5.0
566 */
567 public function process_posts_json_response($posts_json)
568 {
569 $fb_data_json = !is_array($posts_json) ? json_decode($posts_json) : [];
570 $fb_data = [];
571 if (isset($fb_data_json->data)) {
572 $fb_data = $fb_data_json->data;
573 } else {
574 $fb_data = !is_array($posts_json) ? json_decode($posts_json) : [];
575 }
576 $posts_json = [
577 'api_url' => $this->graph_url,
578 'shortcode_options' => $this->shortcode_atts,
579 'data' => $fb_data
580 ];
581
582 if (isset($fb_data_json->paging)) {
583 $posts_json['paging'] = $fb_data_json->paging;
584 }
585 $posts_json = wp_json_encode($posts_json, true);
586 $this->set_posts_json($posts_json);
587 $this->process_backup_data($fb_data);
588 }
589
590 /**
591 * Process Posts JSON Response
592 *
593 * @since 5.0
594 */
595 public function process_cached_posts_json()
596 {
597 $posts_json = $this->feed_cache->get($this->cache_type_page);
598 if ($posts_json !== null && strpos($posts_json, '"error":{"message":') !== false && false !== get_transient('!cff_backup_' . $this->transient_name)) {
599 $posts_json = $this->feed_cache->get($this->cache_type . '_backup');
600 }
601 if ($posts_json === false) {
602 $posts_json = $this->get_remote_data();
603 }
604 $this->set_posts_json($posts_json);
605 }
606
607
608 /**
609 * Connect to API & get Remote data
610 *
611 * @since 5.0
612 */
613 public function get_remote_data()
614 {
615 $api_connect = new CFF_API_Connect($this->graph_url, '', [
616 'page_id' => $this->page_id
617 ]);
618 $api_connect->connect();
619 $response = $api_connect->get_json_data();
620 do_action('cff_api_connect_response', $response, $this->graph_url);
621 return $response;
622 }
623
624 /**
625 * Process API Error
626 *
627 * @since 5.0
628 */
629 public function process_api_error($fb_data, $cache_type)
630 {
631 $posts_json = [];
632 if (false !== $this->feed_cache->get($cache_type . '_backup')) {
633 $posts_json = $this->feed_cache->get($cache_type . '_backup');
634
635 $error_message = isset($fb_data->error->message) ? $fb_data->error->message : '';
636 $error_type = isset($fb_data->error->type) ? $fb_data->error->type : '';
637 $error_json = [
638 'cached_error' => [
639 'message' => $error_message,
640 'type' => $error_type
641 ]
642 ];
643 if (!empty($posts_json)) {
644 array_push(
645 $error_json,
646 json_decode($posts_json, true)
647 );
648 }
649 $posts_json = wp_json_encode($error_json);
650 }
651 return $posts_json;
652 }
653
654
655 /**
656 * Process API Erro
657 *
658 * @since 5.0
659 */
660 public function process_backup_data($fb_data)
661 {
662 if (!empty($fb_data)) {
663 if (isset($fb_data->error)) {
664 $posts_json = $this->process_api_error($fb_data, $this->cache_type);
665 $this->set_posts_json($posts_json);
666 } else {
667 if ($this->cache_type === 'posts') {
668 $this->feed_cache->after_new_posts_retrieved();
669 }
670 }
671 $this->feed_cache->update_or_insert($this->cache_type, $this->posts_json);
672 }
673 }
674 /**
675 * Triggered when clicking the Load More Button
676 * to Get new Feed Posts
677 *
678 * @since 5.0
679 */
680 public function load_more_feed_data()
681 {
682
683 if (is_null($this->next_urls_arr_safe) || empty($this->next_urls_arr_safe)) {
684 return '';
685 }
686 $more_posts = isset($this->next_urls_arr_safe[$this->page_id]) ? true : false;
687 $next_url_safe = isset($this->next_urls_arr_safe[$this->page_id]) ? $this->next_urls_arr_safe[$this->page_id] : '';
688 if (!$more_posts) {
689 return 'no_more_posts';
690 }
691 $feed_token = $this->access_token;
692 if (is_array($feed_token)) {
693 if (isset($feed_token[$this->page_id])) {
694 $feed_token = $feed_token[$this->page_id];
695 } else {
696 $feed_token = reset($feed_token);
697 }
698 }
699
700 $api_url = str_replace("x_cff_hide_token_x", $feed_token, $next_url_safe);
701 $url_bits = parse_url($api_url, PHP_URL_QUERY);
702 parse_str($url_bits, $url_bits_arr);
703 if (isset($url_bits_arr['until'])) {
704 $unique_string = $url_bits_arr['until'];
705 } elseif (isset($url_bits_arr['after'])) {
706 $unique_string = $url_bits_arr['after'];
707 if (strlen($unique_string) > 15) {
708 $unique_string = substr($unique_string, -15);
709 }
710 } elseif (isset($url_bits_arr['offset'])) {
711 $unique_string = $url_bits_arr['offset'];
712 } else {
713 $unique_string = '';
714 }
715 $this->set_graph_url($api_url);
716 return $this->get_feed_data();
717 }
718 }
719