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