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