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