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