PluginProbe ʕ •ᴥ•ʔ
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress / 4.1.5
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress v4.1.5
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_Utils.php
custom-facebook-feed / inc Last commit date
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_Utils.php
867 lines
1 <?php
2 /**
3 * Class CFF_Utils
4 *
5 * Contains miscellaneous CFF functions
6 *
7 * @since 2.19
8 */
9 namespace CustomFacebookFeed;
10 use CustomFacebookFeed\SB_Facebook_Data_Encryption;
11
12
13
14
15 class CFF_Utils{
16
17 /**
18 * Get JSON object of feed data
19 * @access public
20 * @static
21 * @since 2.19
22 */
23 static function cff_fetchUrl( $url, $check_admin = true){
24 $response = wp_remote_get( $url );
25 if ( ! CFF_Utils::cff_is_wp_error( $response ) ) {
26 $feedData = wp_remote_retrieve_body( $response );
27
28 if ( ! CFF_Utils::cff_is_fb_error( $feedData ) ) {
29
30
31 \cff_main()->cff_error_reporter->remove_error( 'connection' );
32
33 $feedData = apply_filters( 'cff_filter_api_data', $feedData );
34
35 return $feedData;
36 } else {
37 if ( strpos( $url, '&limit=' ) !== false ) {
38 CFF_Utils::cff_log_fb_error( $feedData, $url, $check_admin );
39 }
40
41
42 $error = json_decode( $feedData, true );
43 $reporter = CFF_Utils::cff_is_pro_version() ? \cff_main_pro()->cff_error_reporter : \cff_main()->cff_error_reporter;
44
45 if ( $reporter->is_critical_error( $error ) ) {
46 $parsed_url = parse_url( $url );
47 if ( ! empty( $parsed_url['query'] ) ) {
48 parse_str( $parsed_url['query'], $parsed );
49
50 if ( isset( $parsed['access_token'] ) ) {
51 $args = [
52 'access_token' => $parsed['access_token']
53 ];
54 $source_data = \CustomFacebookFeed\Builder\CFF_Db::source_query( $args );
55
56 if ( $source_data ) {
57 foreach ( $source_data as $source ) {
58 \CustomFacebookFeed\Builder\CFF_Source::add_error( $source['account_id'], $feedData );
59 }
60 }
61 }
62 }
63 }
64
65 return $feedData;
66 }
67
68 } else {
69 if ( strpos( $url, '&limit=' ) !== false ) {
70 CFF_Utils::cff_log_wp_error( $response, $url );
71 }
72
73 return '{}';
74 }
75 }
76
77
78 /**
79 *
80 * @access public
81 * @static
82 * @since 2.19
83 */
84 static function cff_desc_tags($description){
85 preg_match_all( "/@\[(.*?)\]/", $description, $cff_tag_matches );
86 $replace_strings_arr = array();
87 foreach ( $cff_tag_matches[1] as $cff_tag_match ) {
88 $cff_tag_parts = explode( ':', $cff_tag_match );
89 $replace_strings_arr[] = '<a href="https://facebook.com/'.$cff_tag_parts[0].'">'.$cff_tag_parts[2].'</a>';
90 }
91 $cff_tag_iterator = 0;
92 $cff_description_tagged = '';
93 $cff_text_split = preg_split( "/@\[(.*?)\]/" , $description );
94 foreach ( $cff_text_split as $cff_desc_split ) {
95 if ( $cff_tag_iterator < count( $replace_strings_arr ) ) {
96 $cff_description_tagged .= $cff_desc_split . $replace_strings_arr[ $cff_tag_iterator ];
97 } else {
98 $cff_description_tagged .= $cff_desc_split;
99 }
100 $cff_tag_iterator++;
101 }
102
103 return $cff_description_tagged;
104 }
105
106
107 /**
108 * Sort message tags by offset value
109 * @access public
110 * @static
111 * @since 2.19
112 */
113 static function cffSortTags($a, $b){
114 return $a['offset'] - $b['offset'];
115 }
116
117
118 /**
119 *
120 * @access public
121 * @static
122 * @since 2.19
123 */
124 static function cff_is_wp_error( $response ) {
125 return is_wp_error( $response );
126 }
127
128
129 /**
130 *
131 * @access public
132 * @static
133 * @since 2.19
134 */
135 static function cff_log_wp_error( $response, $url ) {
136 if ( is_wp_error( $response ) ) {
137
138 delete_option( 'cff_dismiss_critical_notice' );
139
140 $admin_message_error = '';
141 if ( isset( $response ) && isset( $response->errors ) ) {
142 foreach ( $response->errors as $key => $item ) {
143 $admin_message_error .= ' '.$key . ' - ' . $item[0];
144 }
145 }
146
147 $admin_message = __( 'Error connecting to the Facebook API:', 'custom-facebook-feed' ) . ' ' . $admin_message_error;
148 $public_message =__( 'Unable to make remote requests to the Facebook API. Log in as an admin to view more details.', 'custom-facebook-feed' );
149 $frontend_directions = '<p class="cff-error-directions"><a href="https://smashballoon.com/custom-facebook-feed/docs/errors/" target="_blank" rel="noopener">' . __( 'Directions on How to Resolve This Issue', 'custom-facebook-feed' ) . '</a></p>';
150 $backend_directions = '<a class="button button-primary" href="https://smashballoon.com/custom-facebook-feed/docs/errors/" target="_blank" rel="noopener">' . __( 'Directions on How to Resolve This Issue', 'custom-facebook-feed' ) . '</a>';
151 $error = array(
152 'accesstoken' => 'none',
153 'public_message' => $public_message,
154 'admin_message' => $admin_message,
155 'frontend_directions' => $frontend_directions,
156 'backend_directions' => $backend_directions,
157 'post_id' => get_the_ID(),
158 'errorno' => 'wp_remote_get'
159 );
160 \cff_main()->cff_error_reporter->add_error( 'wp_remote_get', $error );
161 }else{
162 \cff_main()->cff_error_reporter->remove_error( 'connection' );
163 }
164 }
165
166
167 /**
168 *
169 * @access public
170 * @static
171 * @since 2.19
172 */
173 static function cff_is_fb_error( $response ) {
174 return (strpos( $response, '{"error":' ) === 0);
175 }
176
177
178 /**
179 *
180 * @access public
181 * @static
182 * @since 2.19
183 */
184 static function cff_log_fb_error( $response, $url, $check_admin = true ) {
185 if ( is_admin() && $check_admin == true ) {
186 return;
187 }
188 delete_option( 'cff_dismiss_critical_notice' );
189
190 $access_token_refresh_errors = array( 10, 4, 200 );
191
192 $response = json_decode( $response, true );
193 $api_error_code = $response['error']['code'];
194
195 //Page Public Content Access error
196 $ppca_error = false;
197 if( strpos($response['error']['message'], 'Public Content Access') !== false ) $ppca_error = true;
198
199 if ( in_array( (int)$api_error_code, $access_token_refresh_errors, true ) && !$ppca_error ) {
200 $pieces = explode( 'access_token=', $url );
201 $accesstoken_parts = isset( $pieces[1] ) ? explode( '&', $pieces[1] ) : 'none';
202 $accesstoken = $accesstoken_parts[0];
203
204 $api_error_number_message = sprintf( __( 'API Error %s:', 'custom-facebook-feed' ), $api_error_code );
205 $link = admin_url( 'admin.php?page=cff-settings' );
206 $error = array(
207 'accesstoken' => $accesstoken,
208 'post_id' => get_the_ID(),
209 'errorno' => $api_error_code
210 );
211
212 \cff_main()->cff_error_reporter->add_error( 'accesstoken', $error, $accesstoken );
213 } else {
214 \cff_main()->cff_error_reporter->add_error( 'api', $response );
215 }
216 }
217
218
219 /**
220 * Make links into span instead when the post text is made clickable
221 * @access public
222 * @static
223 * @since 2.19
224 */
225 static function cff_wrap_span($text) {
226 $pattern = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
227 return preg_replace_callback($pattern, array('CustomFacebookFeed\CFF_Utils','cff_wrap_span_callback'), $text);
228 }
229
230 /**
231 * @return Json Encode
232 *
233 * @since 2.1.1
234 */
235 static function cff_json_encode( $thing ) {
236 return wp_json_encode( $thing );
237 }
238
239 /**
240 *
241 * @access public
242 * @static
243 * @since 2.19
244 */
245 static function cff_wrap_span_callback($matches) {
246 $max_url_length = 100;
247 $max_depth_if_over_length = 2;
248 $ellipsis = '&hellip;';
249 $target = 'target="_blank"';
250 $url_full = $matches[0];
251 $url_short = '';
252 if (strlen($url_full) > $max_url_length) {
253 $parts = parse_url($url_full);
254 $url_short = $parts['scheme'] . '://' . preg_replace('/^www\./', '', $parts['host']) . '/';
255 $path_components = explode('/', trim($parts['path'], '/'));
256 foreach ($path_components as $dir) {
257 $url_string_components[] = $dir . '/';
258 }
259 if (!empty($parts['query'])) {
260 $url_string_components[] = '?' . $parts['query'];
261 }
262 if (!empty($parts['fragment'])) {
263 $url_string_components[] = '#' . $parts['fragment'];
264 }
265 for ($k = 0; $k < count($url_string_components); $k++) {
266 $curr_component = $url_string_components[$k];
267 if ($k >= $max_depth_if_over_length || strlen($url_short) + strlen($curr_component) > $max_url_length) {
268 if ($k == 0 && strlen($url_short) < $max_url_length) {
269 // Always show a portion of first directory
270 $url_short .= substr($curr_component, 0, $max_url_length - strlen($url_short));
271 }
272 $url_short .= $ellipsis;
273 break;
274 }
275 $url_short .= $curr_component;
276 }
277 } else {
278 $url_short = $url_full;
279 }
280 return "<span class='cff-break-word'>$url_short</span>";
281 }
282
283
284 /**
285 * Use the timezone to offset the date as all post dates are in UTC +0000
286 * @access public
287 * @static
288 * @since 2.19
289 */
290 static function cff_set_timezone($original, $cff_timezone){
291 $cff_date_time = new \DateTime(date('m/d g:i a'), new \DateTimeZone('UTC'));
292 $cff_date_time->setTimeZone(new \DateTimeZone($cff_timezone));
293 $cff_date_time_offset = $cff_date_time->getOffset();
294
295 $original = $original + $cff_date_time_offset;
296
297 return $original;
298 }
299
300
301 /**
302 * Time stamp functison - used for posts
303 * @access public
304 * @static
305 * @since 2.19
306 */
307 static function cff_getdate($original, $date_format, $custom_date, $cff_date_translate_strings, $cff_timezone) {
308 //Offset the date by the timezone
309 $new_time = CFF_Utils::cff_set_timezone($original, $cff_timezone);
310 switch ($date_format) {
311 case '2':
312 $print = date_i18n('F jS, g:i a', $new_time);
313 break;
314 case '3':
315 $print = date_i18n('F jS', $new_time);
316 break;
317 case '4':
318 $print = date_i18n('D F jS', $new_time);
319 break;
320 case '5':
321 $print = date_i18n('l F jS', $new_time);
322 break;
323 case '6':
324 $print = date_i18n('D M jS, Y', $new_time);
325 break;
326 case '7':
327 $print = date_i18n('l F jS, Y', $new_time);
328 break;
329 case '8':
330 $print = date_i18n('l F jS, Y - g:i a', $new_time);
331 break;
332 case '9':
333 $print = date_i18n("l M jS, 'y", $new_time);
334 break;
335 case '10':
336 $print = date_i18n('m.d.y', $new_time);
337 break;
338 case '11':
339 $print = date_i18n('m/d/y', $new_time);
340 break;
341 case '12':
342 $print = date_i18n('d.m.y', $new_time);
343 break;
344 case '13':
345 $print = date_i18n('d/m/y', $new_time);
346 break;
347 case '14':
348 $print = date_i18n('d-m-Y, G:i', $new_time);
349 break;
350 case '15':
351 $print = date_i18n('jS F Y, G:i', $new_time);
352 break;
353 case '16':
354 $print = date_i18n('d M Y, G:i', $new_time);
355 break;
356 case '17':
357 $print = date_i18n('l jS F Y, G:i', $new_time);
358 break;
359 case '18':
360 $print = date_i18n('m.d.y - G:i', $new_time);
361 break;
362 case '19':
363 $print = date_i18n('d.m.y - G:i', $new_time);
364 break;
365 default:
366
367 $cff_second = $cff_date_translate_strings['cff_translate_second'];
368 $cff_seconds = $cff_date_translate_strings['cff_translate_seconds'];
369 $cff_minute = $cff_date_translate_strings['cff_translate_minute'];
370 $cff_minutes = $cff_date_translate_strings['cff_translate_minutes'];
371 $cff_hour = $cff_date_translate_strings['cff_translate_hour'];
372 $cff_hours = $cff_date_translate_strings['cff_translate_hours'];
373 $cff_day = $cff_date_translate_strings['cff_translate_day'];
374 $cff_days = $cff_date_translate_strings['cff_translate_days'];
375 $cff_week = $cff_date_translate_strings['cff_translate_week'];
376 $cff_weeks = $cff_date_translate_strings['cff_translate_weeks'];
377 $cff_month = $cff_date_translate_strings['cff_translate_month'];
378 $cff_months = $cff_date_translate_strings['cff_translate_months'];
379 $cff_year = $cff_date_translate_strings['cff_translate_years'];
380 $cff_years = $cff_date_translate_strings['cff_translate_years'];
381 $cff_ago = $cff_date_translate_strings['cff_translate_ago'];
382
383 $periods = array($cff_second, $cff_minute, $cff_hour, $cff_day, $cff_week, $cff_month, $cff_year, "decade");
384 $periods_plural = array($cff_seconds, $cff_minutes, $cff_hours, $cff_days, $cff_weeks, $cff_months, $cff_years, "decade");
385
386 $lengths = array("60","60","24","7","4.35","12","10");
387 $now = time();
388
389 // is it future date or past date
390 if($now > $original) {
391 $difference = $now - $original;
392 $tense = $cff_ago;
393 } else {
394 $difference = $original - $now;
395 $tense = $cff_ago;
396 }
397 for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
398 $difference /= $lengths[$j];
399 }
400
401 $difference = round($difference);
402
403 if($difference != 1) {
404 $periods[$j] = $periods_plural[$j];
405 }
406 $print = "$difference $periods[$j] {$tense}";
407
408 break;
409
410 }
411 if ( !empty($custom_date) ){
412 $print = date_i18n($custom_date, $new_time);
413 }
414
415 return $print;
416 }
417
418
419 /**
420 *
421 * @access public
422 * @static
423 * @since 2.19
424 */
425 static function cff_eventdate($original, $date_format, $custom_date) {
426 switch ($date_format) {
427 case '2':
428 $print = date_i18n('<k>F jS, </k>g:ia', $original);
429 break;
430 case '3':
431 $print = date_i18n('g:ia<k> - F jS</k>', $original);
432 break;
433 case '4':
434 $print = date_i18n('g:ia<k>, F jS</k>', $original);
435 break;
436 case '5':
437 $print = date_i18n('<k>l F jS - </k> g:ia', $original);
438 break;
439 case '6':
440 $print = date_i18n('<k>D M jS, Y, </k>g:iA', $original);
441 break;
442 case '7':
443 $print = date_i18n('<k>l F jS, Y, </k>g:iA', $original);
444 break;
445 case '8':
446 $print = date_i18n('<k>l F jS, Y - </k>g:ia', $original);
447 break;
448 case '9':
449 $print = date_i18n("<k>l M jS, 'y</k>", $original);
450 break;
451 case '10':
452 $print = date_i18n('<k>m.d.y - </k>g:iA', $original);
453 break;
454 case '11':
455 $print = date_i18n('<k>m/d/y, </k>g:ia', $original);
456 break;
457 case '12':
458 $print = date_i18n('<k>d.m.y - </k>g:iA', $original);
459 break;
460 case '13':
461 $print = date_i18n('<k>d/m/y, </k>g:ia', $original);
462 break;
463 case '14':
464 $print = date_i18n('<k>M j, </k>g:ia', $original);
465 break;
466 case '15':
467 $print = date_i18n('<k>M j, </k>G:i', $original);
468 break;
469 case '16':
470 $print = date_i18n('<k>d-m-Y, </k>G:i', $original);
471 break;
472 case '17':
473 $print = date_i18n('<k>jS F Y, </k>G:i', $original);
474 break;
475 case '18':
476 $print = date_i18n('<k>d M Y, </k>G:i', $original);
477 break;
478 case '19':
479 $print = date_i18n('<k>l jS F Y, </k>G:i', $original);
480 break;
481 case '20':
482 $print = date_i18n('<k>m.d.y - </k>G:i', $original);
483 break;
484 case '21':
485 $print = date_i18n('<k>d.m.y - </k>G:i', $original);
486 break;
487 default:
488 $print = date_i18n('<k>F j, Y, </k>g:ia', $original);
489 break;
490 }
491 if ( !empty($custom_date) ){
492 $print = date_i18n($custom_date, $original);
493 }
494 return $print;
495 }
496
497
498 /**
499 * Use custom stripos function if it's not available (only available in PHP 5+)
500 * @access public
501 * @static
502 * @since 2.19
503 */
504 static function stripos($haystack, $needle){
505 if( empty( stristr( $haystack, $needle ) ) )
506 return false;
507 return strpos($haystack, stristr( $haystack, $needle ) );
508 }
509
510
511 /**
512 *
513 * @access public
514 * @static
515 * @since 2.19
516 */
517 static function cff_stripos_arr($haystack, $needle) {
518 if(!is_array($needle)) $needle = array($needle);
519 foreach($needle as $what) {
520 if(($pos = CFF_Utils::stripos($haystack, ltrim($what) ))!==false) return $pos;
521 }
522 return false;
523 }
524
525
526 /**
527 *
528 * @access public
529 * @static
530 * @since 2.19
531 */
532 static function cff_mb_substr_replace($string, $replacement, $start, $length=NULL) {
533 if (is_array($string)) {
534 $num = count($string);
535 // $replacement
536 $replacement = is_array($replacement) ? array_slice($replacement, 0, $num) : array_pad(array($replacement), $num, $replacement);
537 // $start
538 if (is_array($start)) {
539 $start = array_slice($start, 0, $num);
540 foreach ($start as $key => $value)
541 $start[$key] = is_int($value) ? $value : 0;
542 }
543 else {
544 $start = array_pad(array($start), $num, $start);
545 }
546 // $length
547 if (!isset($length)) {
548 $length = array_fill(0, $num, 0);
549 }
550 elseif (is_array($length)) {
551 $length = array_slice($length, 0, $num);
552 foreach ($length as $key => $value)
553 $length[$key] = isset($value) ? (is_int($value) ? $value : $num) : 0;
554 }
555 else {
556 $length = array_pad(array($length), $num, $length);
557 }
558 // Recursive call
559 return array_map(__FUNCTION__, $string, $replacement, $start, $length);
560 }
561 preg_match_all('/./us', (string)$string, $smatches);
562 preg_match_all('/./us', (string)$replacement, $rmatches);
563 if ($length === NULL) $length = mb_strlen($string);
564 array_splice($smatches[0], $start, $length, $rmatches[0]);
565 return join($smatches[0]);
566 }
567
568
569 /**
570 * Push to assoc array
571 * @access public
572 * @static
573 * @since 2.19
574 */
575 static function cff_array_push_assoc($array, $key, $value){
576 $array[$key] = $value;
577 return $array;
578 }
579
580
581 /**
582 * Convert string to slug
583 * @access public
584 * @static
585 * @since 2.19
586 */
587 static function cff_to_slug($string){
588 return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
589 }
590
591
592 /**
593 * Convert string to slug
594 * @access public
595 * @static
596 * @since 2.19
597 */
598 static function cff_get_utc_offset() {
599 return get_option( 'gmt_offset', 0 ) * HOUR_IN_SECONDS;
600 }
601
602
603 static function cff_schedule_report_email() {
604 $options = get_option('cff_style_settings');
605 $input = isset( $options[ 'email_notification' ] ) ? $options[ 'email_notification' ] : 'monday';
606 $timestamp = strtotime( 'next ' . $input );
607 $timestamp = $timestamp + (3600 * 24 * 7);
608 $six_am_local = $timestamp + CFF_Utils::cff_get_utc_offset() + (6*60*60);
609 wp_schedule_event( $six_am_local, 'cffweekly', 'cff_feed_issue_email' );
610 }
611
612
613 /**
614 *
615 * @access public
616 * @static
617 * @since 2.19
618 */
619 static function cff_is_pro_version() {
620 return defined( 'CFFWELCOME_VER' );
621 }
622
623 /**
624 *
625 * @access public
626 * @static
627 * @since 2.19
628 */
629 static function cff_get_set_cache($cff_posts_json_url, $transient_name, $cff_cache_time, $cache_seconds, $data_att_html, $cff_show_access_token, $access_token, $backup=false) {
630 $cache_seconds = max( $cache_seconds, 60 );
631
632 // Trying to use new cache table
633 $shortcode_atts = is_array( $data_att_html ) ? $data_att_html : json_decode( str_replace( '&quot;', '"', $data_att_html ), true );
634
635 if ( ! empty( $shortcode_atts['feed'] ) ) {
636 $feed_id = (int)$shortcode_atts['feed'];
637 $is_legacy = false;
638 } else {
639 $feed_id = $transient_name;
640 $is_legacy = true;
641 }
642
643 $feed_page = isset( $shortcode_atts['feedPage'] ) ? $shortcode_atts['feedPage'] : 1;
644 $feed_cache = new CFF_Cache( $feed_id, $feed_page, $cache_seconds, $is_legacy );
645 $feed_cache->retrieve_and_set();
646
647 $cache_type = 'posts';
648 if ( strpos( $transient_name, 'cff_header_' ) !== false ) {
649 $cache_type = 'header';
650 }
651
652 // no pages in free
653 $cache_type_page = $cache_type;
654 if ( $cache_type === 'posts' && $feed_page > 1 ) {
655 $cache_type_page = 'posts_' . $feed_page;
656 }
657
658 if ( $feed_cache->is_expired( $cache_type ) ) {
659 //Get the contents of the Facebook page
660 $posts_json = CFF_Utils::cff_fetchUrl($cff_posts_json_url);
661
662 if ( $is_legacy && $cache_type === 'header' ) {
663 $feed_cache->update_or_insert( $cache_type, $posts_json );
664 }
665
666 //Check whether any data is returned from the API. If it isn't then don't cache the error response and instead keep checking the API on every page load until data is returned.
667 $FBdata = json_decode($posts_json);
668
669 //Check whether the JSON is wrapped in a "data" property as if it doesn't then it's a featured post
670 $prefix_data = '{"data":';
671 (substr($posts_json, 0, strlen($prefix_data)) == $prefix_data) ? $cff_featured_post = false : $cff_featured_post = true;
672
673 //Add API URL to beginning of JSON array
674 $prefix = '{';
675 if (substr($posts_json, 0, strlen($prefix)) == $prefix) $posts_json = substr($posts_json, strlen($prefix));
676
677 //Encode and replace quotes so can be stored as a string
678 $data_att_html = str_replace( '"', '&quot;', json_encode($data_att_html) );
679 $posts_json = '{"api_url":"'.$cff_posts_json_url.'", "shortcode_options":"'.$data_att_html.'", ' . $posts_json;
680
681 //If it's a featured post then it doesn't contain 'data'
682 ( $cff_featured_post ) ? $FBdata = $FBdata : $FBdata = $FBdata->data;
683
684 //Check the API response
685 if( !empty($FBdata) ) {
686
687 //Error returned by API
688 if( isset($FBdata->error) ){
689
690 //Cache the error JSON so doesn't keep making repeated requests
691 //See if a backup cache exists
692 if ( false !== $feed_cache->get( $cache_type . '_backup' ) ) {
693
694 $posts_json = $feed_cache->get( $cache_type . '_backup' );
695
696 //Add error message to backup cache so can be displayed at top of feed
697 isset( $FBdata->error->message ) ? $error_message = $FBdata->error->message : $error_message = '';
698 isset( $FBdata->error->type ) ? $error_type = $FBdata->error->type : $error_type = '';
699 $prefix = '{';
700 if (substr($posts_json, 0, strlen($prefix)) == $prefix) $posts_json = substr($posts_json, strlen($prefix));
701 $error_json = '{"cached_error": { "message": "'.$error_message.'", "type": "'.$error_type.'" }';
702 $error_json .= !empty($posts_json) ? ', ' . $posts_json : '}';
703 $posts_json = $error_json;
704 }
705
706 //Posts data returned by API
707 } else {
708
709 //If a backup should be created for this data then create one
710 if ( $backup ){
711 $feed_cache->update_or_insert( $cache_type . '_backup', $posts_json );
712 }
713
714 if ( $cache_type === 'posts' ) {
715 $feed_cache->after_new_posts_retrieved();
716 }
717 }
718
719 $feed_cache->update_or_insert( $cache_type, $posts_json );
720 }
721 } else {
722 $posts_json = $feed_cache->get( $cache_type_page );
723 if ( strpos($posts_json, '"error":{"message":') !== false ){
724 //Use backup cache if exists
725 $posts_json = $feed_cache->get( $cache_type . '_backup' );
726 }
727
728 //If we can't find the transient then fall back to just getting the json from the api
729 if ($posts_json == false){
730 $posts_json = CFF_Utils::cff_fetchUrl( $cff_posts_json_url );
731 }
732 }
733
734 return $posts_json;
735 }
736
737
738 /**
739 * Check if On
740 * Function to check if a shortcode options is set to ON or TRUE
741 *
742 * @access public
743 * @static
744 * @since 2.19
745 * @return boolean
746 */
747 static function check_if_on( $value ){
748 return ( isset( $value ) && !empty( $value ) && ( $value == 'true' || $value == 'on') ) ? true : false;
749 }
750
751 /**
752 * Check if On
753 * Function to check if a shortcode options is set to ON or TRUE
754 *
755 * @access public
756 * @static
757 * @since 2.19
758 * @return boolean
759 */
760 static function check_if_onexist( $value ){
761 return ( ( isset( $value ) ) ) ? true : false;
762 }
763
764 /**
765 * Check Value
766 * Function to check a value if exists or return a default one
767 *
768 * @access public
769 * @static
770 * @since 2.19
771 * @return mixed
772 */
773 static function return_value( $value , $default = ''){
774 return ( isset( $value ) && !empty( $value ) ) ? $value : $default;
775 }
776
777
778 /**
779 * Get CSS value
780 * Checks if the value is a valid CSS distance
781 *
782 * @access public
783 * @static
784 * @since 2.19
785 * @return string
786 */
787 static function get_css_distance( $value ){
788 return ( is_numeric(substr($value, -1, 1)) ) ? $value . 'px' : $value;
789 }
790
791
792 /**
793 *
794 *
795 * This function will get the Profile Pic, Cover, Name, About
796 * For the visual header display
797 *
798 * @access public
799 * @static
800 * @since 2.19
801 */
802 static function fetch_header_data( $page_id, $cff_is_group, $access_token, $cff_cache_time, $cff_multifeed_active = false, $data_att_html = array() ){
803 // Create Transient Name
804 $transient_name = 'cff_header_' . $page_id;
805 $transient_name = substr($transient_name, 0, 45);
806
807 //These fields only apply to pages
808 !$cff_is_group ? $page_only_fields = ',fan_count,about' : $page_only_fields = '';
809
810 $header_access_token = $access_token;
811 if( is_array($access_token) ){
812 $header_access_token = reset($access_token);
813 if( empty($header_access_token) ) $header_access_token = key($access_token);
814 }
815
816 $encryption = new SB_Facebook_Data_Encryption();
817 $header_access_token = $encryption->decrypt($header_access_token) ? $encryption->decrypt($header_access_token) : $header_access_token;
818
819
820 $header_details_json_url = 'https://graph.facebook.com/v4.0/'.$page_id.'?fields=id,picture.height(150).width(150),cover,name,link'.$page_only_fields.'&access_token='. $header_access_token;
821
822 //Get the data
823 $header_details = CFF_Utils::cff_get_set_cache( $header_details_json_url, $transient_name, $cff_cache_time, WEEK_IN_SECONDS, $data_att_html, false, $access_token, true );
824 $header_details = json_decode( $header_details );
825 return $header_details;
826 }
827
828
829 /**
830 *
831 *
832 * Print Template
833 * returns an HTML Template
834 *
835 * @access public
836 * @static
837 * @since 2.19
838 */
839 static function print_template_part( $template_name, $args = array(), $this_class = null){
840 $this_class = $this_class;
841 extract($args);
842 ob_start();
843 include trailingslashit( CFF_PLUGIN_DIR ) . 'templates/' . $template_name . '.php';
844 $template = ob_get_contents();
845 ob_get_clean();
846 return $template;
847 }
848
849 /**
850 *
851 * Get Connected Accounts
852 * @since 2.19
853 */
854 static function cff_get_connected_accounts() {
855 $cff_connected_accounts = get_option('cff_connected_accounts', array());
856 if( !empty($cff_connected_accounts) ){
857 $cff_connected_accounts = str_replace('\"','"', $cff_connected_accounts);
858 $cff_connected_accounts = str_replace("\'","'", $cff_connected_accounts);
859 $cff_connected_accounts = json_decode( $cff_connected_accounts, true );
860 }
861 if(!is_array($cff_connected_accounts) || $cff_connected_accounts == null){
862 $cff_connected_accounts = [];
863 }
864 return $cff_connected_accounts;
865 }
866
867 }