Admin
1 month ago
Builder
1 month ago
Helpers
1 month ago
Integrations
1 month ago
UsageTracking
1 month ago
CFF_Autolink.php
1 month ago
CFF_Blocks.php
1 month ago
CFF_Cache.php
1 month ago
CFF_Education.php
1 month ago
CFF_Elementor_Base.php
1 month ago
CFF_Elementor_Widget.php
1 month ago
CFF_Error_Reporter.php
1 month ago
CFF_FB_Settings.php
1 month ago
CFF_Feed_Elementor_Control.php
1 month ago
CFF_Feed_Locator.php
1 month ago
CFF_Feed_Pro.php
1 month ago
CFF_GDPR_Integrations.php
1 month ago
CFF_Group_Posts.php
1 month ago
CFF_HTTP_Request.php
1 month ago
CFF_Oembed.php
1 month ago
CFF_Parse.php
1 month ago
CFF_Resizer.php
1 month ago
CFF_Response.php
1 month ago
CFF_Shortcode.php
1 month ago
CFF_Shortcode_Display.php
1 month ago
CFF_SiteHealth.php
1 month ago
CFF_Utils.php
1 month ago
CFF_View.php
1 month ago
Custom_Facebook_Feed.php
1 month ago
Email_Notification.php
1 month ago
Platform_Data.php
1 month ago
SB_Facebook_Data_Encryption.php
1 month ago
SB_Facebook_Data_Manager.php
1 month ago
index.php
1 month ago
CFF_Feed_Pro.php
400 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class CFF_Feed_Pro |
| 5 | */ |
| 6 | |
| 7 | namespace CustomFacebookFeed; |
| 8 | |
| 9 | if (! defined('ABSPATH')) { |
| 10 | exit; // Exit if accessed directly |
| 11 | } |
| 12 | |
| 13 | |
| 14 | class CFF_Feed_Pro |
| 15 | { |
| 16 | /** |
| 17 | * @var string |
| 18 | */ |
| 19 | private $regular_feed_transient_name; |
| 20 | |
| 21 | /** |
| 22 | * @var array |
| 23 | */ |
| 24 | private $post_data; |
| 25 | |
| 26 | /** |
| 27 | * @var array |
| 28 | */ |
| 29 | private $next_pages; |
| 30 | |
| 31 | /** |
| 32 | * @var bool |
| 33 | */ |
| 34 | private $should_paginate; |
| 35 | |
| 36 | /** |
| 37 | * @var int |
| 38 | */ |
| 39 | private $num_api_calls; |
| 40 | |
| 41 | /** |
| 42 | * @var bool |
| 43 | */ |
| 44 | private $should_use_backup; |
| 45 | |
| 46 | /** |
| 47 | * @var array |
| 48 | */ |
| 49 | private $report; |
| 50 | |
| 51 | private $resized_images; |
| 52 | |
| 53 | private $is_customizer; |
| 54 | |
| 55 | protected $one_post_found; |
| 56 | |
| 57 | protected $max_api_calls; |
| 58 | |
| 59 | public function __construct($transient_name, $is_customizer = false) |
| 60 | { |
| 61 | $this->regular_feed_transient_name = $transient_name; |
| 62 | |
| 63 | $this->post_data = array(); |
| 64 | $this->next_pages = array(); |
| 65 | $this->should_paginate = true; |
| 66 | |
| 67 | // this is a count of how many api calls have been made for each feed |
| 68 | // type and term. |
| 69 | // By default the limit is 10 |
| 70 | $this->num_api_calls = 0; |
| 71 | $this->max_api_calls = 10; |
| 72 | $this->should_use_backup = false; |
| 73 | |
| 74 | // used for errors and the sbi_debug report |
| 75 | $this->report = array(); |
| 76 | |
| 77 | $this->resized_images = array(); |
| 78 | |
| 79 | $this->one_post_found = false; |
| 80 | $this->is_customizer = $is_customizer; |
| 81 | } |
| 82 | |
| 83 | public function get_post_data() |
| 84 | { |
| 85 | return $this->post_data; |
| 86 | } |
| 87 | |
| 88 | public function set_post_data($post_data) |
| 89 | { |
| 90 | $this->post_data = $post_data; |
| 91 | } |
| 92 | |
| 93 | public function get_next_pages() |
| 94 | { |
| 95 | return $this->next_pages; |
| 96 | } |
| 97 | |
| 98 | public function need_posts($num, $offset = 0) |
| 99 | { |
| 100 | $num_existing_posts = is_array($this->post_data) ? count($this->post_data) : 0; |
| 101 | $num_needed_for_page = (int)$num + (int)$offset; |
| 102 | |
| 103 | ($num_existing_posts < $num_needed_for_page) ? $this->add_report('need more posts') : $this->add_report('have enough posts'); |
| 104 | |
| 105 | return ($num_existing_posts < $num_needed_for_page); |
| 106 | } |
| 107 | |
| 108 | public function can_get_more_posts() |
| 109 | { |
| 110 | $one_type_and_term_has_more_ages = $this->next_pages !== false; |
| 111 | $max_concurrent_api_calls_not_met = $this->num_api_calls < $this->max_api_calls; |
| 112 | $max_concurrent_api_calls_not_met ? $this->add_report('max conccurrent requests not met') : $this->add_report('max concurrent met'); |
| 113 | $one_type_and_term_has_more_ages ? $this->add_report('more pages available') : $this->add_report('no next page'); |
| 114 | |
| 115 | return ($one_type_and_term_has_more_ages && $max_concurrent_api_calls_not_met); |
| 116 | } |
| 117 | |
| 118 | public function add_remote_posts($settings, $feed_id = false) |
| 119 | { |
| 120 | $new_post_sets = array(); |
| 121 | $next_pages = $this->next_pages; |
| 122 | |
| 123 | $settings['include_extras'] = true; |
| 124 | |
| 125 | $one_post_found = false; |
| 126 | $next_page_found = false; |
| 127 | $settings['the_feed_id'] = $feed_id; |
| 128 | |
| 129 | if (! empty($next_pages) && $next_pages !== '{}') { |
| 130 | $next_pages = json_decode(str_replace(array( '\"', '"' ), '"', $next_pages), true); |
| 131 | $new_post_sets = CFF_Shortcode::cff_get_json_data($settings, $next_pages, '', $this->is_customizer); |
| 132 | } else { |
| 133 | $new_post_sets = CFF_Shortcode::cff_get_json_data($settings, null, '', $this->is_customizer); |
| 134 | } |
| 135 | |
| 136 | $reporter = CFF_Utils::cff_is_pro_version() ? \cff_main_pro()->cff_error_reporter : \cff_main()->cff_error_reporter; |
| 137 | if ( |
| 138 | ! $reporter->are_critical_errors() |
| 139 | && isset($settings['sources']) |
| 140 | && is_array($settings['sources']) |
| 141 | ) { |
| 142 | foreach ($settings['sources'] as $source) { |
| 143 | if (! empty($source['error'])) { |
| 144 | \CustomFacebookFeed\Builder\CFF_Source::clear_error($source['account_id']); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (! empty($new_post_sets)) { |
| 150 | $next_pages = CFF_Shortcode::cff_get_next_url_parts($new_post_sets); |
| 151 | if (! empty($next_pages) && $next_pages !== '{}') { |
| 152 | $next_page_found = true; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | $posts = $this->merge_posts($new_post_sets, $settings); |
| 157 | |
| 158 | $posts = $this->filter_posts($posts, $settings); |
| 159 | |
| 160 | |
| 161 | if (isset($posts[0])) { |
| 162 | $one_post_found = true; |
| 163 | } else { |
| 164 | $next_page_found = false; |
| 165 | } |
| 166 | |
| 167 | if (! empty($this->post_data) && is_array($this->post_data)) { |
| 168 | $posts = array_merge($this->post_data, $posts); |
| 169 | } elseif ($one_post_found) { |
| 170 | $this->one_post_found = true; |
| 171 | } |
| 172 | |
| 173 | $this->post_data = $posts; |
| 174 | |
| 175 | |
| 176 | |
| 177 | if (isset($next_page_found) && $next_page_found) { |
| 178 | $this->next_pages = $next_pages; |
| 179 | } else { |
| 180 | $this->next_pages = false; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public function set_next_pages($next_pages) |
| 185 | { |
| 186 | $this->next_pages = $next_pages; |
| 187 | } |
| 188 | |
| 189 | private function merge_posts($post_sets, $settings) |
| 190 | { |
| 191 | $merged_posts = array(); |
| 192 | $settings['sortby'] = isset($settings['sortby']) ? $settings['sortby'] : 'date'; |
| 193 | |
| 194 | $i = 0; |
| 195 | foreach ($post_sets as $post_set) { |
| 196 | $post_data = []; |
| 197 | if (isset($post_set->data)) { |
| 198 | $post_data = $post_set->data; |
| 199 | } elseif (isset($post_set)) { |
| 200 | $post_data = [$post_set]; |
| 201 | } |
| 202 | if ( |
| 203 | isset($post_data[ $i ]) |
| 204 | && (isset($post_data[ $i ]->id) || isset($post_data[ $i ]->created_time) ) |
| 205 | ) { |
| 206 | $merged_posts = array_merge($merged_posts, $post_data); |
| 207 | } |
| 208 | $i++; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | return $merged_posts; |
| 213 | } |
| 214 | |
| 215 | public function should_use_pagination($settings, $offset = 0) |
| 216 | { |
| 217 | if ($settings['minnum'] < 1) { |
| 218 | return false; |
| 219 | } |
| 220 | $posts_available = count($this->post_data) - ($offset + $settings['num']); |
| 221 | $show_loadmore_button_by_settings = ($settings['showbutton'] == 'on' || $settings['showbutton'] == 'true' || $settings['showbutton'] == true ) && $settings['showbutton'] !== 'false'; |
| 222 | |
| 223 | if ($show_loadmore_button_by_settings) { |
| 224 | if ($posts_available > 0) { |
| 225 | $this->add_report('do pagination, posts available'); |
| 226 | return true; |
| 227 | } |
| 228 | $pages = $this->next_pages; |
| 229 | |
| 230 | if ($pages && ! $this->should_use_backup()) { |
| 231 | foreach ($pages as $page) { |
| 232 | if (! empty($page)) { |
| 233 | return true; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | $this->add_report('no pagination, no posts available'); |
| 240 | |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | public function add_report($to_add) |
| 245 | { |
| 246 | $this->report[] = $to_add; |
| 247 | } |
| 248 | |
| 249 | public function get_report() |
| 250 | { |
| 251 | return $this->report; |
| 252 | } |
| 253 | |
| 254 | protected function filter_posts($post_set, $settings = array()) |
| 255 | { |
| 256 | |
| 257 | if (isset($settings['filter'])) { |
| 258 | $settings['includewords'] = $settings['filter']; |
| 259 | } |
| 260 | |
| 261 | if (isset($settings['exfilter'])) { |
| 262 | $settings['excludewords'] = $settings['exfilter']; |
| 263 | } |
| 264 | |
| 265 | if ( |
| 266 | empty($settings['includewords']) |
| 267 | && empty($settings['excludewords']) |
| 268 | && empty($settings['whitelist']) |
| 269 | && empty($settings['hidephotos']) |
| 270 | ) { |
| 271 | return $post_set; |
| 272 | } |
| 273 | |
| 274 | $includewords = ! empty($settings['includewords']) ? explode(',', $settings['includewords']) : array(); |
| 275 | $excludewords = ! empty($settings['excludewords']) ? explode(',', $settings['excludewords']) : array(); |
| 276 | $hide_photos = ! empty($settings['hidephotos']) && empty($settings['doingModerationMode']) ? explode(',', str_replace(' ', '', $settings['hidephotos'])) : array(); |
| 277 | $white_list = false; |
| 278 | $media_filter = false; |
| 279 | |
| 280 | $filtered_posts = array(); |
| 281 | foreach ($post_set as $post) { |
| 282 | $keep_post = false; |
| 283 | $caption = CFF_Parse::get_message($post); |
| 284 | |
| 285 | $padded_caption = ' ' . str_replace(array( '+', '%0A' ), ' ', urlencode(str_replace(array( '#', '@' ), array( ' HASHTAG', ' MENTION' ), strtolower($caption)))) . ' '; |
| 286 | $id = CFF_Parse::get_post_id($post); |
| 287 | |
| 288 | $is_hidden = false; |
| 289 | $passes_media_filter = true; |
| 290 | if ( |
| 291 | ! empty($hide_photos) |
| 292 | && (in_array($id, $hide_photos, true) || in_array('cff_' . $id, $hide_photos, true)) |
| 293 | ) { |
| 294 | $is_hidden = true; |
| 295 | if ($white_list) { |
| 296 | if (in_array($id, $white_list, true) || in_array('cff_' . $id, $white_list, true)) { |
| 297 | $is_hidden = false; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | if ($media_filter) { |
| 303 | $media_type = ''; |
| 304 | if ($media_filter === 'videos') { |
| 305 | if ($media_type !== 'video') { |
| 306 | $passes_media_filter = false; |
| 307 | } |
| 308 | } else { |
| 309 | if ($media_type === 'video') { |
| 310 | $passes_media_filter = false; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // any blocked photos will not pass any additional filters so don't bother processing |
| 316 | if (! $is_hidden && $passes_media_filter) { |
| 317 | $is_on_white_list = false; |
| 318 | $has_includeword = false; |
| 319 | $has_excludeword = false; |
| 320 | $passes_word_filter = false; |
| 321 | |
| 322 | if ($white_list) { |
| 323 | if (in_array($id, $white_list, true) || in_array('cff_' . $id, $white_list, true)) { |
| 324 | $is_on_white_list = true; |
| 325 | } |
| 326 | } elseif (! empty($includewords) || ! empty($excludewords)) { |
| 327 | if (! empty($includewords)) { |
| 328 | foreach ($includewords as $includeword) { |
| 329 | if (! empty($includeword)) { |
| 330 | $converted_includeword = trim(str_replace('+', ' ', urlencode(str_replace(array( '#', '@' ), array( ' HASHTAG', ' MENTION' ), strtolower($includeword))))); |
| 331 | |
| 332 | if (preg_match('/\b' . $converted_includeword . '\b/i', $padded_caption, $matches)) { |
| 333 | $has_includeword = true; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | if (! empty($excludewords)) { |
| 340 | foreach ($excludewords as $excludeword) { |
| 341 | if (! empty($excludeword)) { |
| 342 | $converted_excludeword = trim(str_replace('+', ' ', urlencode(str_replace(array( '#', '@' ), array( ' HASHTAG', ' MENTION' ), strtolower($excludeword))))); |
| 343 | if (preg_match('/\b' . $converted_excludeword . '\b/i', $padded_caption, $matches)) { |
| 344 | $has_excludeword = true; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | if (! empty($excludewords) && ! empty($includewords)) { |
| 350 | $passes_word_filter = $has_includeword && ! $has_excludeword; |
| 351 | } elseif (! empty($includewords)) { |
| 352 | $passes_word_filter = $has_includeword; |
| 353 | } else { |
| 354 | $passes_word_filter = !$has_excludeword; |
| 355 | } |
| 356 | } else { |
| 357 | // no other filters so it belongs in the feed |
| 358 | $keep_post = true; |
| 359 | } |
| 360 | |
| 361 | if ($is_on_white_list || $passes_word_filter) { |
| 362 | $keep_post = true; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | $keep_post = apply_filters('cff_passes_filter', $keep_post, $post, $settings); |
| 367 | if ($keep_post) { |
| 368 | $filtered_posts[] = $post; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return $filtered_posts; |
| 373 | } |
| 374 | |
| 375 | protected function handle_no_posts_found($settings = array(), $feed_types_and_terms = array()) |
| 376 | { |
| 377 | } |
| 378 | |
| 379 | protected function remove_duplicate_posts() |
| 380 | { |
| 381 | $posts = $this->post_data; |
| 382 | $ids_in_feed = array(); |
| 383 | $non_duplicate_posts = array(); |
| 384 | $removed = array(); |
| 385 | |
| 386 | foreach ($posts as $post) { |
| 387 | $post_id = CFF_Parse::get_post_id($post); |
| 388 | if (! in_array($post_id, $ids_in_feed, true)) { |
| 389 | $ids_in_feed[] = $post_id; |
| 390 | $non_duplicate_posts[] = $post; |
| 391 | } else { |
| 392 | $removed[] = $post_id; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | $this->add_report('removed duplicates: ' . implode(', ', $removed)); |
| 397 | $this->set_post_data($non_duplicate_posts); |
| 398 | } |
| 399 | } |
| 400 |