PluginProbe ʕ •ᴥ•ʔ
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress / 4.1.7
Smash Balloon Social Post Feed – Simple Social Feeds for WordPress v4.1.7
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_Oembed.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_Oembed.php
434 lines
1 <?php
2 /**
3 * Class CFF_Oembed
4 *
5 * Replaces the native WordPress functionality for Facebook oembed
6 * to allow authenticated oembeds
7 *
8 * @since 2.16/3.16
9 */
10
11 namespace CustomFacebookFeed;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 die( '-1' );
15 }
16
17 class CFF_Oembed
18 {
19 /**
20 * CFF_Oembed constructor.
21 *
22 * If an account has been connected, hooks are added
23 * to change how Facebook links are handled for oembeds
24 *
25 * @since 2.16/3.16
26 */
27 public function __construct() {
28 if ( CFF_Oembed::can_do_oembed() ) {
29 if ( CFF_Oembed::can_check_for_old_oembeds() ) {
30 add_action( 'the_post', array( 'CustomFacebookFeed\CFF_Oembed', 'check_page_for_old_oembeds' ) );
31 }
32 add_filter( 'oembed_providers', array( 'CustomFacebookFeed\CFF_Oembed', 'oembed_providers' ), 10, 1 );
33 add_filter( 'oembed_fetch_url', array( 'CustomFacebookFeed\CFF_Oembed', 'oembed_set_fetch_url' ), 10, 3 );
34 add_filter( 'oembed_result', array( 'CustomFacebookFeed\CFF_Oembed', 'oembed_result' ), 10, 3 );
35 }
36 if ( CFF_Oembed::should_extend_ttl() ) {
37 add_filter( 'oembed_ttl', array( 'CustomFacebookFeed\CFF_Oembed', 'oembed_ttl' ), 10, 4 );
38 }
39 }
40
41 /**
42 * Check to make sure there is a saved access token to
43 * enable authenticated oembeds
44 *
45 * @return bool
46 *
47 * @since 2.16/3.16
48 */
49 public static function can_do_oembed() {
50 $oembed_token_settings = get_option( 'cff_oembed_token', array() );
51
52 if ( isset( $oembed_token_settings['disabled'] ) && $oembed_token_settings['disabled'] ) {
53 return false;
54 }
55
56 $access_token = CFF_Oembed::last_access_token();
57 if ( ! $access_token ) {
58 return false;
59 }
60
61 return true;
62 }
63
64 /**
65 * The "time to live" for Instagram oEmbeds is extended if the access token expires.
66 * Even if new oEmbeds will not use the Instagram Feed system due to an expired token
67 * the time to live should continue to be extended.
68 *
69 * @return bool
70 *
71 * @since 2.16/3.16
72 */
73 public static function should_extend_ttl() {
74 $oembed_token_settings = get_option( 'cff_oembed_token', array() );
75
76 if ( isset( $oembed_token_settings['disabled'] ) && $oembed_token_settings['disabled'] ) {
77 return false;
78 }
79
80 $will_expire = CFF_Oembed::oembed_access_token_will_expire();
81 if ( $will_expire ) {
82 return true;
83 }
84
85 return false;
86 }
87
88 /**
89 * Checking for old oembeds makes permanent changes to posts
90 * so we want the user to turn it off and on
91 *
92 * @return bool
93 *
94 * @since 2.16/3.16
95 */
96 public static function can_check_for_old_oembeds() {
97 /**
98 * TODO: if setting is enabled
99 */
100 return true;
101 }
102
103 /**
104 * Filters the WordPress list of oembed providers to
105 * change what url is used for remote requests for the
106 * oembed data
107 *
108 * @param array $providers
109 *
110 * @return mixed
111 *
112 * @since 2.16/3.16
113 */
114 public static function oembed_providers( $providers ) {
115 $oembed_url = CFF_Oembed::oembed_url();
116 if ( $oembed_url ) {
117 $post_embed_providers = CFF_Oembed::post_providers();
118 foreach ( $post_embed_providers as $post_provider ) {
119 $providers[ $post_provider ] = array( $oembed_url . 'oembed_post', true );
120 }
121
122 $video_embed_providers = CFF_Oembed::video_providers();
123 foreach ( $video_embed_providers as $video_provider ) {
124 $providers[ $video_provider ] = array( $oembed_url . 'oembed_video', true );
125 }
126 }
127
128 return $providers;
129 }
130
131 /**
132 * Add the access token from a connected account to make an authenticated
133 * call to get oembed data from Facebook
134 *
135 * @param string $provider
136 * @param string $url
137 * @param array $args
138 *
139 * @return string
140 *
141 * @since 2.16/3.16
142 */
143 public static function oembed_set_fetch_url( $provider, $url, $args ) {
144 $access_token = CFF_Oembed::last_access_token();
145 if ( ! $access_token ) {
146 return $provider;
147 }
148
149 if ( strpos( $provider, 'oembed_post' ) !== false
150 || strpos( $provider, 'oembed_video' ) !== false ) {
151
152 if ( strpos( $url, '?' ) !== false ) {
153 $provider = self::get_provider_from_url_with_query_vars( $provider, $url );
154 }
155 $provider = add_query_arg( 'access_token', $access_token, $provider );
156 }
157
158 return $provider;
159 }
160
161 /**
162 * URLs with query variables are handled specially
163 *
164 * @param $provider
165 * @param $url
166 *
167 * @return array|mixed|string|string[]
168 */
169 public static function get_provider_from_url_with_query_vars( $provider, $url ) {
170 $exploded = explode( '?', $url );
171 if ( ! empty( $exploded[1] ) ) {
172 if ( strpos( $url, '?v=' ) !== false ) {
173 $exploded = explode( '&', $url );
174 $provider = str_replace( urlencode( '&' . $exploded[1] ), '', $provider );
175 }
176 }
177
178 return $provider;
179 }
180
181 /**
182 * New oembeds are wrapped in a div for easy detection of older oembeds
183 * that will need to be updated
184 *
185 * @param string $html
186 * @param string $url
187 * @param array $args
188 *
189 * @return string
190 *
191 * @since 2.16/3.16
192 */
193 public static function oembed_result( $html, $url, $args ) {
194 $post_embed_providers = CFF_Oembed::post_providers();
195 foreach ( $post_embed_providers as $post_provider ) {
196 if ( preg_match( $post_provider, $url ) === 1 ) {
197 if ( strpos( $html, 'class="fb-post"' ) !== false ) {
198 $html = '<div class="cff-embed-wrap cff-post-embed-wrap">' . str_replace( 'class="fb-post"', 'class="fb-post cff-embed cff-post-embed"', $html ) . '</div>';
199 }
200 }
201 }
202
203 $video_embed_providers = CFF_Oembed::video_providers();
204 foreach ( $video_embed_providers as $video_provider ) {
205 if ( preg_match( $video_provider, $url ) === 1 ) {
206 if ( strpos( $html, 'class="fb-video"' ) !== false ) {
207 $html = '<div class="cff-embed-wrap cff-video-embed-wrap">' . str_replace( 'class="fb-video"', 'class="fb-video cff-embed cff-video-embed"', $html ) . '</div>';
208 }
209 }
210 }
211
212 return $html;
213 }
214
215 /**
216 * Extend the "time to live" for oEmbeds created with access tokens that expire
217 *
218 * @param $ttl
219 * @param $url
220 * @param $attr
221 * @param $post_ID
222 *
223 * @return float|int
224 *
225 * @since 2.16/3.16
226 */
227 public static function oembed_ttl( $ttl, $url, $attr, $post_ID ) {
228 $providers = CFF_Oembed::post_providers();
229 foreach ( $providers as $provider ) {
230 if ( preg_match( $provider, $url ) === 1 ) {
231 $ttl = 30 * YEAR_IN_SECONDS;
232 }
233 }
234
235 $providers = CFF_Oembed::video_providers();
236 foreach ( $providers as $provider ) {
237 if ( preg_match( $provider, $url ) === 1 ) {
238 $ttl = 30 * YEAR_IN_SECONDS;
239 }
240 }
241
242 return $ttl;
243 }
244
245 /**
246 * Only one api URL for FB
247 *
248 * @return bool|string
249 *
250 * @since 2.16/3.16
251 */
252 public static function oembed_url() {
253 return 'https://graph.facebook.com/';
254 }
255
256 /**
257 * Any access token will work for oembeds so the access token
258 * saved in settings is used
259 *
260 * @return bool|string
261 *
262 * @since 2.16/3.16
263 */
264 public static function last_access_token() {
265 $oembed_token_settings = get_option( 'cff_oembed_token', array() );
266 $will_expire = CFF_Oembed::oembed_access_token_will_expire();
267 $encryption = new \CustomFacebookFeed\SB_Facebook_Data_Encryption();
268
269 if ( ! empty( $oembed_token_settings['access_token'] )
270 && (! $will_expire || $will_expire > time()) ) {
271 $oembed_token_settings['access_token'] = $encryption->maybe_decrypt( $oembed_token_settings['access_token'] );
272
273 return str_replace(":", ":02Sb981f26534g75h091287a46p5l63", $oembed_token_settings['access_token']);
274 } else {
275 $settings_access_token = trim(get_option('cff_access_token'));
276 $settings_access_token = $encryption->maybe_decrypt( $settings_access_token );
277 if ( ! empty( $settings_access_token ) ) {
278 return str_replace(":", ":02Sb981f26534g75h091287a46p5l63", $settings_access_token);
279 }
280
281 if ( class_exists( 'SB_Instagram_Oembed' ) ) {
282 $sbi_oembed_token_settings = get_option( 'sbi_oembed_token', array() );
283 if ( ! empty( $sbi_oembed_token_settings['access_token'] ) ) {
284 $sbi_oembed_token_settings['access_token'] = $encryption->maybe_decrypt( $sbi_oembed_token_settings['access_token'] );
285 return $sbi_oembed_token_settings['access_token'];
286 }
287 }
288 }
289
290 return false;
291 }
292
293 /**
294 * Access tokens created from FB accounts not connected to an
295 * FB page expire after 60 days.
296 *
297 * @return bool|int
298 */
299 public static function oembed_access_token_will_expire() {
300 $oembed_token_settings = get_option( 'cff_oembed_token', array() );
301 $will_expire = isset( $oembed_token_settings['expiration_date'] ) && (int)$oembed_token_settings['expiration_date'] > 0 ? (int)$oembed_token_settings['expiration_date'] : false;
302
303 return $will_expire;
304 }
305
306 /**
307 * Before links in the content are processed, old oembed post meta
308 * records are deleted so new oembed data will be retrieved and saved.
309 * If this check has been done and no old oembeds are found, a flag
310 * is saved as post meta to skip the process.
311 *
312 * @since 2.16/3.16
313 */
314 public static function check_page_for_old_oembeds() {
315 if ( is_admin() ) {
316 return;
317 }
318
319 $post_ID = get_the_ID();
320 $done_checking = (int)get_post_meta( $post_ID, '_cff_oembed_done_checking', true ) === 1;
321
322 if ( ! $done_checking ) {
323
324 $num_found = CFF_Oembed::delete_facebook_oembed_caches( $post_ID );
325 if ( $num_found === 0 ) {
326 update_post_meta( $post_ID, '_cff_oembed_done_checking', 1 );
327 }
328 }
329 }
330
331 /**
332 * Loop through post meta data and if it's an oembed and has content
333 * that looks like a Facebook oembed, delete it
334 *
335 * @param $post_ID
336 *
337 * @return int number of old oembed caches found
338 *
339 * @since 2.16/3.16
340 */
341 public static function delete_facebook_oembed_caches( $post_ID ) {
342 $post_metas = get_post_meta( $post_ID );
343 if ( empty( $post_metas ) ) {
344 return 0;
345 }
346
347 $total_found = 0;
348 foreach ( $post_metas as $post_meta_key => $post_meta_value ) {
349 if ( '_oembed_' === substr( $post_meta_key, 0, 8 ) ) {
350 if ( strpos( $post_meta_value[0], 'class="fb-post"' ) !== false
351 && strpos( $post_meta_value[0], 'cff-embed-wrap' ) === false ) {
352 $total_found++;
353 delete_post_meta( $post_ID, $post_meta_key );
354 if ( '_oembed_time_' !== substr( $post_meta_key, 0, 13 ) ) {
355 delete_post_meta( $post_ID, str_replace( '_oembed_', '_oembed_time_', $post_meta_key ) );
356 }
357 } elseif ( strpos( $post_meta_value[0], 'class="fb-video"' ) !== false
358 && strpos( $post_meta_value[0], 'cff-embed-wrap' ) === false ) {
359 $total_found++;
360 delete_post_meta( $post_ID, $post_meta_key );
361 if ( '_oembed_time_' !== substr( $post_meta_key, 0, 13 ) ) {
362 delete_post_meta( $post_ID, str_replace( '_oembed_', '_oembed_time_', $post_meta_key ) );
363 }
364 }
365 }
366 }
367
368 return $total_found;
369 }
370
371 /**
372 * Current list of regex to identify FB URLs that could become oembeds using
373 * the 'oembed_post' endpoint.
374 *
375 * @return array
376 *
377 * @since 2.16/3.16
378 */
379 public static function post_providers() {
380 $post_embed_providers = array(
381 '#https?://www\.facebook\.com/.*/posts/.*#i',
382 '#https?://www\.facebook\.com/.*/activity/.*#i',
383 '#https?://www\.facebook\.com/.*/photos/.*#i',
384 '#https?://www\.facebook\.com/photo(s/|\.php).*#i',
385 '#https?://www\.facebook\.com/permalink\.php.*#i',
386 '#https?://www\.facebook\.com/media/.*#i',
387 '#https?://www\.facebook\.com/questions/.*#i',
388 '#https?://www\.facebook\.com/notes/.*#i',
389 );
390
391 return $post_embed_providers;
392 }
393
394 /**
395 * Current list of regex to identify FB URLs that could become oembeds using
396 * the 'oembed_video' endpoint.
397 *
398 * @return array
399 *
400 * @since 2.16/3.16
401 */
402 public static function video_providers() {
403 $video_embed_providers = array(
404 '#https?://www\.facebook\.com/.*/videos/.*#i',
405 '#https?://www\.facebook\.com/video\.php.*#i',
406 '#https?://www\.facebook\.com/watch/.*#i',
407 '#https?://fb\.watch/.*#i'
408 );
409
410 return $video_embed_providers;
411 }
412
413 /**
414 * Used for clearing the oembed update check flag for all posts
415 *
416 * @since 2.16/3.16
417 */
418 public static function clear_checks() {
419 global $wpdb;
420 $table_name = esc_sql( $wpdb->prefix . "postmeta" );
421 $result = $wpdb->query("
422 DELETE
423 FROM $table_name
424 WHERE meta_key = '_cff_oembed_done_checking';");
425 }
426 }
427
428 /*
429 function cffOembedInit() {
430 return new CFF_Oembed();
431 }
432 cffOembedInit();
433 */
434