Admin
3 years ago
Builder
3 years ago
Helpers
3 years ago
CFF_Autolink.php
3 years ago
CFF_Blocks.php
3 years ago
CFF_Cache.php
3 years ago
CFF_Education.php
3 years ago
CFF_Elementor_Base.php
3 years ago
CFF_Elementor_Widget.php
3 years ago
CFF_Error_Reporter.php
3 years ago
CFF_FB_Settings.php
3 years ago
CFF_Feed_Elementor_Control.php
3 years ago
CFF_Feed_Locator.php
3 years ago
CFF_Feed_Pro.php
3 years ago
CFF_GDPR_Integrations.php
3 years ago
CFF_Group_Posts.php
3 years ago
CFF_HTTP_Request.php
3 years ago
CFF_Oembed.php
3 years ago
CFF_Parse.php
3 years ago
CFF_Resizer.php
3 years ago
CFF_Response.php
3 years ago
CFF_Shortcode.php
3 years ago
CFF_Shortcode_Display.php
3 years ago
CFF_SiteHealth.php
3 years ago
CFF_Utils.php
3 years ago
CFF_View.php
3 years ago
Custom_Facebook_Feed.php
3 years ago
SB_Facebook_Data_Encryption.php
3 years ago
SB_Facebook_Data_Manager.php
3 years ago
CFF_Group_Posts.php
457 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class CFF_Groups_Post |
| 4 | * |
| 5 | * |
| 6 | * |
| 7 | * @since 3.19.3 |
| 8 | */ |
| 9 | namespace CustomFacebookFeed; |
| 10 | |
| 11 | use CustomFacebookFeed\SB_Facebook_Data_Encryption; |
| 12 | |
| 13 | if (!defined('ABSPATH')) |
| 14 | exit; // Exit if accessed directly |
| 15 | |
| 16 | |
| 17 | class CFF_Group_Posts |
| 18 | { |
| 19 | |
| 20 | /** |
| 21 | * @var string |
| 22 | */ |
| 23 | private $cache_name; |
| 24 | |
| 25 | /** |
| 26 | * @var string |
| 27 | */ |
| 28 | private $api_call_url; |
| 29 | |
| 30 | /** |
| 31 | * @var string |
| 32 | */ |
| 33 | private $data_att_html; |
| 34 | |
| 35 | /** |
| 36 | * @var array |
| 37 | */ |
| 38 | private $posts_array; |
| 39 | |
| 40 | /** |
| 41 | * @var array |
| 42 | */ |
| 43 | private $json_data; |
| 44 | |
| 45 | /** |
| 46 | * @var array |
| 47 | */ |
| 48 | private $posts_cache_data; |
| 49 | |
| 50 | /** |
| 51 | * @var array |
| 52 | */ |
| 53 | private $feed_options; |
| 54 | |
| 55 | /** |
| 56 | * @var int |
| 57 | */ |
| 58 | private $next_urls_arr_safe; |
| 59 | |
| 60 | /** |
| 61 | * @var bool |
| 62 | */ |
| 63 | private $is_event_page; |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * @var class |
| 68 | */ |
| 69 | private $encryption; |
| 70 | |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Construct. |
| 75 | * |
| 76 | * Construct Caching System |
| 77 | * |
| 78 | * @since 3.19.3 |
| 79 | * @access public |
| 80 | */ |
| 81 | function __construct($group_id, $feed_options, $api_call_url, $data_att_html, $is_event_page) |
| 82 | { |
| 83 | $this->encryption = new SB_Facebook_Data_Encryption(); |
| 84 | |
| 85 | $this->cache_name = '!cff_group_' . $group_id . '_' . str_replace(',', '_', $feed_options['type']); |
| 86 | $this->posts_cache_data = get_option($this->cache_name); |
| 87 | $this->feed_options = $feed_options; |
| 88 | $this->api_call_url = $api_call_url; |
| 89 | #$this->api_call_url = $api_call_url . '&limit=100'; |
| 90 | $this->data_att_html = $data_att_html; |
| 91 | $this->is_event_page = $is_event_page; |
| 92 | if (!$this->posts_cache_data) { |
| 93 | $this->posts_cache_data = new \stdClass(); |
| 94 | $this->posts_cache_data->api_url = $this->api_call_url; |
| 95 | $this->posts_cache_data->shortcode_options = $this->data_att_html; |
| 96 | $this->posts_cache_data->data = []; |
| 97 | } else { |
| 98 | $this->posts_cache_data = json_decode($this->encryption->maybe_decrypt($this->posts_cache_data)); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * |
| 107 | * @since 3.19.3 |
| 108 | * Returns Needed Information for the Group Posts |
| 109 | * |
| 110 | * @access public |
| 111 | */ |
| 112 | function init_group_posts($json_data, $load_more_date, $show_posts) |
| 113 | { |
| 114 | $this->json_data = json_decode($json_data); |
| 115 | $this->json_data = (isset($this->json_data->data) && $this->json_data->data > 0) ? $this->json_data->data : []; |
| 116 | $this->posts_array = isset($this->posts_cache_data->data) ? (array) $this->posts_cache_data->data : []; |
| 117 | $this->add_update_posts(); |
| 118 | $this->update_cache(); |
| 119 | $latest_record_date = CFF_Group_Posts::create_next_pagination($this->json_data, $show_posts); |
| 120 | $load_from_cache = false; |
| 121 | if (sizeof($this->json_data) <= 0 && !$this->is_event_page) { |
| 122 | if ($load_more_date === false) { |
| 123 | $json_data = $this->get_data_json(false, null); |
| 124 | } else { |
| 125 | $json_data = $this->get_data_json(true, $load_more_date); |
| 126 | } |
| 127 | $latest_record_date = CFF_Group_Posts::create_next_pagination($this->posts_cache_data->data, $show_posts); |
| 128 | $load_from_cache = true; |
| 129 | } else { |
| 130 | $json_data_check = json_decode($json_data); |
| 131 | $json_data = $this->check_posts_returned($json_data_check, $show_posts); |
| 132 | $this->json_data = json_decode($json_data); |
| 133 | $this->json_data = (isset($this->json_data->data) && $this->json_data->data > 0) ? $this->json_data->data : []; |
| 134 | $latest_record_date = CFF_Group_Posts::create_next_pagination($this->json_data, $show_posts); |
| 135 | } |
| 136 | |
| 137 | return [ |
| 138 | 'posts_json' => $json_data, |
| 139 | 'latest_record_date' => $latest_record_date, |
| 140 | 'load_from_cache' => $load_from_cache |
| 141 | ]; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * |
| 147 | * @since 3.19.3 |
| 148 | * Returns Needed Information for the Group Posts Events Page |
| 149 | * |
| 150 | * @access public |
| 151 | */ |
| 152 | function init_group_posts_events($json_data, $load_more_date, $show_posts) |
| 153 | { |
| 154 | |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * Get The pagination Values |
| 160 | * |
| 161 | * @since 3.19.3 |
| 162 | * @access public |
| 163 | */ |
| 164 | |
| 165 | static function check_duplicated_posts($prev_post, $ac_post) |
| 166 | { |
| 167 | $ac_message = isset($ac_post->message) ? $ac_post->message : ''; |
| 168 | $ac_link = isset($ac_post->link) ? $ac_post->link : ''; |
| 169 | $ac_description = isset($ac_post->description) ? $ac_post->description : ''; |
| 170 | |
| 171 | $prev_message = isset($prev_post->message) ? $prev_post->message : ''; |
| 172 | $prev_link = isset($prev_post->link) ? $prev_post->link : ''; |
| 173 | $prev_description = isset($prev_post->description) ? $prev_post->description : ''; |
| 174 | |
| 175 | $is_duplicate = (($prev_message == $ac_message) && ($prev_link == $ac_link) && ($prev_description == $ac_description) && !isset($ac_post->call_to_action->type)) ? true : false; |
| 176 | return $is_duplicate; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * |
| 182 | * @since 3.19.3 |
| 183 | * Checks if the returned posts number from the API o the transient cache |
| 184 | * Is equal to the number of posts defined |
| 185 | * IF NOT it does look for the posts from the Persistent Cache |
| 186 | * |
| 187 | * @access public |
| 188 | */ |
| 189 | function check_posts_returned($json_data, $show_posts) |
| 190 | { |
| 191 | if (isset($json_data->data)) { |
| 192 | $prev_post = []; |
| 193 | $result_array = []; |
| 194 | foreach ($json_data->data as $single_post) { |
| 195 | #$is_returned = !CFF_Group_Posts::check_duplicated_posts($prev_post, $single_post); |
| 196 | $is_returned = true; |
| 197 | $prev_post = $single_post; |
| 198 | if ($is_returned) { |
| 199 | array_push($result_array, $single_post); |
| 200 | } |
| 201 | } |
| 202 | $json_data->data = array_slice($result_array, 0, $show_posts); |
| 203 | if (sizeof($json_data->data) < $show_posts) { |
| 204 | $remaining_num = $show_posts - sizeof($json_data->data); |
| 205 | $remaining_json_data = json_decode($this->get_data_json(true, CFF_Group_Posts::create_next_pagination($json_data->data, sizeof($json_data->data)))); |
| 206 | $json_data->data = array_merge($json_data->data, $remaining_json_data->data); |
| 207 | } |
| 208 | } |
| 209 | if (sizeof($json_data->data) == 0) |
| 210 | unset($json_data->paging); |
| 211 | return json_encode($json_data); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | |
| 216 | /** |
| 217 | * |
| 218 | * @since 3.19.3 |
| 219 | * Add more POSTS to the Post array |
| 220 | * It can add or update |
| 221 | * @access public |
| 222 | */ |
| 223 | function add_update_posts() |
| 224 | { |
| 225 | $new_cached_posts = $this->posts_array; |
| 226 | $posts_array_api = $this->json_data; |
| 227 | foreach ($posts_array_api as $single_post) { |
| 228 | if (isset($single_post->id)) { |
| 229 | $key = array_search($single_post->id, array_column($this->posts_array, 'id')); |
| 230 | if ($key !== false) { |
| 231 | $new_cached_posts[$key] = $single_post; |
| 232 | } else { |
| 233 | array_push($new_cached_posts, $single_post); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | $this->posts_array = $new_cached_posts; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /** |
| 242 | * |
| 243 | * @since 3.19.3 |
| 244 | * @access public |
| 245 | */ |
| 246 | function get_data_json($is_load_more, $next_urls_arr_safe) |
| 247 | { |
| 248 | if ($is_load_more && isset($next_urls_arr_safe)) { |
| 249 | $this->next_urls_arr_safe = $next_urls_arr_safe; |
| 250 | $new_array = array_filter($this->posts_cache_data->data, function ($single_post) { |
| 251 | $the_time = isset($single_post->updated_time) ? $single_post->updated_time : $single_post->created_time; |
| 252 | $is_returned = ((int) strtotime($the_time) < (int) $this->next_urls_arr_safe) && |
| 253 | (!empty($single_post->message) || isset($single_post->call_to_action->type)); |
| 254 | return $is_returned; |
| 255 | }); |
| 256 | $latest_record = end($this->posts_cache_data->data); |
| 257 | $new_array = array_slice($new_array, 0, $this->feed_options['num']); |
| 258 | $this->posts_cache_data->data = sizeof($new_array) > 0 ? $new_array : []; |
| 259 | if ($latest_record == end($this->posts_cache_data->data)) { |
| 260 | $this->posts_cache_data->no_more = true; |
| 261 | } |
| 262 | } else { |
| 263 | $this->posts_cache_data->data = array_slice( |
| 264 | $this->posts_cache_data->data, |
| 265 | 0, $this->feed_options['num'] |
| 266 | ); |
| 267 | } |
| 268 | |
| 269 | return json_encode($this->posts_cache_data); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | /** |
| 276 | * Save latest 100 posts |
| 277 | * |
| 278 | * @since 3.19.3 |
| 279 | * @access public |
| 280 | */ |
| 281 | function update_cache() |
| 282 | { |
| 283 | usort($this->posts_array, function ($post_1, $post_2) { |
| 284 | $time_1 = isset($post_1->updated_time) ? $post_1->updated_time : $post_1->created_time; |
| 285 | $time_2 = isset($post_2->updated_time) ? $post_2->updated_time : $post_2->created_time; |
| 286 | return strcmp(strtotime($time_2), strtotime($time_1)); |
| 287 | }); |
| 288 | $this->posts_cache_data->is_event_page = $this->is_event_page; |
| 289 | $this->posts_cache_data->data = array_slice($this->posts_array, 0, 100); |
| 290 | if (sizeof($this->posts_array) > 0) { |
| 291 | update_option($this->cache_name, $this->encryption->maybe_encrypt(json_encode($this->posts_cache_data)), false); |
| 292 | } |
| 293 | |
| 294 | } |
| 295 | |
| 296 | |
| 297 | |
| 298 | static function create_next_pagination($json_data_arr, $show_posts) |
| 299 | { |
| 300 | if (isset($json_data_arr) && sizeof((array) $json_data_arr) > 0) { |
| 301 | $result_array = []; |
| 302 | $prev_post = []; |
| 303 | foreach ($json_data_arr as $single_post) { |
| 304 | $is_returned = true; |
| 305 | if ($is_returned) { |
| 306 | array_push($result_array, $single_post); |
| 307 | } |
| 308 | $prev_post = $single_post; |
| 309 | } |
| 310 | $json_data_arr = array_slice($result_array, 0, $show_posts); |
| 311 | if (sizeof($json_data_arr) > 0 && sizeof($result_array) >= $show_posts) { |
| 312 | $latest_one = end($json_data_arr); |
| 313 | return isset($latest_one->updated_time) ? strtotime($latest_one->updated_time) : strtotime($latest_one->created_time); |
| 314 | } |
| 315 | } |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /* |
| 321 | API CALL FUNCTIONS *CRON JOB* |
| 322 | */ |
| 323 | /** |
| 324 | * |
| 325 | * @since 3.19.3 |
| 326 | * Cron to Update the Persistent Cache for Group Posts |
| 327 | * Get the latest 100 Posts from groups and Update in |
| 328 | * @access public |
| 329 | */ |
| 330 | static function cron_update_group_persistent_cache() |
| 331 | { |
| 332 | global $wpdb; |
| 333 | $encryption = new SB_Facebook_Data_Encryption(); |
| 334 | $table_name = $wpdb->prefix . "options"; |
| 335 | $persistent_groups = $wpdb->get_results(" |
| 336 | SELECT `option_name` AS `name`, `option_value` AS `value` |
| 337 | FROM $table_name |
| 338 | WHERE `option_name` LIKE ('%!cff\_group\_%') |
| 339 | "); |
| 340 | foreach ($persistent_groups as $group) { |
| 341 | $group_json = json_decode($encryption->maybe_decrypt($group->value), true); |
| 342 | CFF_Group_Posts::update_or_add_group($group->name, $group_json); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Save latest 100 posts |
| 348 | * |
| 349 | * @since 3.19.3 |
| 350 | * @access public |
| 351 | */ |
| 352 | static function update_or_add_group($cache_name, $group_cache) |
| 353 | { |
| 354 | $api_url = $group_cache['api_url']; |
| 355 | $cached_posts = $group_cache['data']; |
| 356 | $is_event_page = isset($group_cache['is_event_page']) ? $group_cache['is_event_page'] : false; |
| 357 | $data_att_html = $group_cache['shortcode_options']; |
| 358 | $new_cached_posts = $cached_posts; |
| 359 | $encryption = new SB_Facebook_Data_Encryption(); |
| 360 | |
| 361 | $posts_array_api = json_decode(CFF_Group_Posts::api_call($api_url, $data_att_html)); |
| 362 | foreach ($posts_array_api->data as $single_post) { |
| 363 | if (isset($single_post->id) && ($is_event_page || (!empty($single_post->message) || isset($single_post->call_to_action->type)))) { |
| 364 | $key = array_search($single_post->id, array_column($cached_posts, 'id')); |
| 365 | if ($key !== false) { |
| 366 | $new_cached_posts[$key] = $single_post; |
| 367 | } else { |
| 368 | array_push($new_cached_posts, $single_post); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | $feed_id = CFF_Group_Posts::get_feed_id_from_name($cache_name); |
| 373 | $posts = array(); |
| 374 | $images_need_resizing = array(); |
| 375 | foreach ($posts_array_api->data as $new_post) { |
| 376 | $posts[] = $new_post; |
| 377 | $images_need_resizing[] = $new_post->id; |
| 378 | } |
| 379 | |
| 380 | $resizer = new CFF_Resizer($images_need_resizing, $feed_id, $posts, $group_cache['shortcode_options']); |
| 381 | $resizer->do_resizing_group(); |
| 382 | |
| 383 | |
| 384 | $posts_cache_data = new \stdClass(); |
| 385 | $posts_cache_data->api_url = $api_url; |
| 386 | $posts_cache_data->shortcode_options = $data_att_html; |
| 387 | $posts_cache_data->is_event_page = $is_event_page; |
| 388 | |
| 389 | usort($new_cached_posts, function ($post_1, $post_2) { |
| 390 | $time_1 = isset($post_1->updated_time) ? $post_1->updated_time : (isset($post_1->created_time) ? $post_1->created_time : 0); |
| 391 | $time_2 = isset($post_2->updated_time) ? $post_2->updated_time : (isset($post_2->created_time) ? $post_2->created_time : 0); |
| 392 | return strcmp(strtotime($time_2), strtotime($time_1)); |
| 393 | }); |
| 394 | $new_cached_posts = array_slice($new_cached_posts, 0, 100); |
| 395 | $posts_cache_data->data = $new_cached_posts; |
| 396 | if (sizeof($new_cached_posts) > 0) { |
| 397 | update_option($cache_name, $encryption->maybe_encrypt(json_encode($posts_cache_data)), false); |
| 398 | } |
| 399 | |
| 400 | } |
| 401 | |
| 402 | |
| 403 | static function get_feed_id_from_name($cache_name) |
| 404 | { |
| 405 | $cache_name = str_replace('!cff_group_', '', $cache_name); |
| 406 | $cache_name = explode(" ", $cache_name); |
| 407 | return (isset($cache_name[0]) ? $cache_name[0] : 0); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Api Call to get 100 posts from Group |
| 412 | * |
| 413 | * @since 3.19.3 |
| 414 | * @access public |
| 415 | */ |
| 416 | static function api_call($api_url, $data_att_html) |
| 417 | { |
| 418 | $api_url_100 = $api_url . '&limit=100'; |
| 419 | $posts_json = CFF_Utils::cff_fetchUrl($api_url_100); |
| 420 | $FBdata = json_decode($posts_json); |
| 421 | $prefix_data = '{"data":'; |
| 422 | $cff_featured_post = (substr($posts_json, 0, strlen($prefix_data)) == $prefix_data) ? false : true; |
| 423 | $prefix = '{'; |
| 424 | if (substr($posts_json, 0, strlen($prefix)) == $prefix) |
| 425 | $posts_json = substr($posts_json, strlen($prefix)); |
| 426 | $posts_json = '{"api_url":"' . $api_url . '", "shortcode_options":"' . $data_att_html . '", ' . $posts_json; |
| 427 | ($cff_featured_post) ? $FBdata = $FBdata : $FBdata = $FBdata->data; |
| 428 | |
| 429 | if (!empty($FBdata)) { |
| 430 | if (!isset($FBdata->error)) { |
| 431 | return $posts_json; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return '{"data":[]}'; |
| 436 | } |
| 437 | |
| 438 | static function group_schedule_event($cff_cache_cron_time_unix, $cff_cron_schedule) |
| 439 | { |
| 440 | if (!wp_next_scheduled('group_post_scheduler_cron')) { |
| 441 | wp_schedule_event($cff_cache_cron_time_unix, $cff_cron_schedule, 'group_post_scheduler_cron'); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | static function group_reschedule_event($cff_cache_cron_time_unix, $cff_cron_schedule) |
| 446 | { |
| 447 | $timestamp = wp_next_scheduled('group_post_scheduler_cron'); |
| 448 | if ($timestamp) { |
| 449 | wp_clear_scheduled_hook('group_post_scheduler_cron'); |
| 450 | wp_unschedule_event($timestamp, 'group_post_scheduler_cron'); |
| 451 | } |
| 452 | if (!wp_next_scheduled('group_post_scheduler_cron')) { |
| 453 | wp_schedule_event($cff_cache_cron_time_unix, $cff_cron_schedule, 'group_post_scheduler_cron'); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | } |