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_Oembed.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_Oembed.php
408 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 $exploded = explode( '?', $url );
154 if ( isset( $exploded[1] ) ) {
155 $provider = str_replace( urlencode( '?' . $exploded[1] ), '', $provider );
156 }
157 }
158 $provider = add_query_arg( 'access_token', $access_token, $provider );
159 }
160
161 return $provider;
162 }
163
164 /**
165 * New oembeds are wrapped in a div for easy detection of older oembeds
166 * that will need to be updated
167 *
168 * @param string $html
169 * @param string $url
170 * @param array $args
171 *
172 * @return string
173 *
174 * @since 2.16/3.16
175 */
176 public static function oembed_result( $html, $url, $args ) {
177 $post_embed_providers = CFF_Oembed::post_providers();
178 foreach ( $post_embed_providers as $post_provider ) {
179 if ( preg_match( $post_provider, $url ) === 1 ) {
180 if ( strpos( $html, 'class="fb-post"' ) !== false ) {
181 $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>';
182 }
183 }
184 }
185
186 $video_embed_providers = CFF_Oembed::video_providers();
187 foreach ( $video_embed_providers as $video_provider ) {
188 if ( preg_match( $video_provider, $url ) === 1 ) {
189 if ( strpos( $html, 'class="fb-video"' ) !== false ) {
190 $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>';
191 }
192 }
193 }
194
195 return $html;
196 }
197
198 /**
199 * Extend the "time to live" for oEmbeds created with access tokens that expire
200 *
201 * @param $ttl
202 * @param $url
203 * @param $attr
204 * @param $post_ID
205 *
206 * @return float|int
207 *
208 * @since 2.16/3.16
209 */
210 public static function oembed_ttl( $ttl, $url, $attr, $post_ID ) {
211 $providers = CFF_Oembed::post_providers();
212 foreach ( $providers as $provider ) {
213 if ( preg_match( $provider, $url ) === 1 ) {
214 $ttl = 30 * YEAR_IN_SECONDS;
215 }
216 }
217
218 $providers = CFF_Oembed::video_providers();
219 foreach ( $providers as $provider ) {
220 if ( preg_match( $provider, $url ) === 1 ) {
221 $ttl = 30 * YEAR_IN_SECONDS;
222 }
223 }
224
225 return $ttl;
226 }
227
228 /**
229 * Only one api URL for FB
230 *
231 * @return bool|string
232 *
233 * @since 2.16/3.16
234 */
235 public static function oembed_url() {
236 return 'https://graph.facebook.com/';
237 }
238
239 /**
240 * Any access token will work for oembeds so the access token
241 * saved in settings is used
242 *
243 * @return bool|string
244 *
245 * @since 2.16/3.16
246 */
247 public static function last_access_token() {
248 $oembed_token_settings = get_option( 'cff_oembed_token', array() );
249 $will_expire = CFF_Oembed::oembed_access_token_will_expire();
250 if ( ! empty( $oembed_token_settings['access_token'] )
251 && (! $will_expire || $will_expire > time()) ) {
252 return str_replace(":", ":02Sb981f26534g75h091287a46p5l63", $oembed_token_settings['access_token']);
253 } else {
254 $settings_access_token = trim(get_option('cff_access_token'));
255 if ( ! empty( $settings_access_token ) ) {
256 return str_replace(":", ":02Sb981f26534g75h091287a46p5l63", $settings_access_token);
257 }
258
259 if ( class_exists( 'SB_Instagram_Oembed' ) ) {
260 $sbi_oembed_token_settings = get_option( 'sbi_oembed_token', array() );
261 if ( ! empty( $sbi_oembed_token_settings['access_token'] ) ) {
262 return $sbi_oembed_token_settings['access_token'];
263 }
264 }
265 }
266
267 return false;
268 }
269
270 /**
271 * Access tokens created from FB accounts not connected to an
272 * FB page expire after 60 days.
273 *
274 * @return bool|int
275 */
276 public static function oembed_access_token_will_expire() {
277 $oembed_token_settings = get_option( 'cff_oembed_token', array() );
278 $will_expire = isset( $oembed_token_settings['expiration_date'] ) && (int)$oembed_token_settings['expiration_date'] > 0 ? (int)$oembed_token_settings['expiration_date'] : false;
279
280 return $will_expire;
281 }
282
283 /**
284 * Before links in the content are processed, old oembed post meta
285 * records are deleted so new oembed data will be retrieved and saved.
286 * If this check has been done and no old oembeds are found, a flag
287 * is saved as post meta to skip the process.
288 *
289 * @since 2.16/3.16
290 */
291 public static function check_page_for_old_oembeds() {
292 if ( is_admin() ) {
293 return;
294 }
295
296 $post_ID = get_the_ID();
297 $done_checking = (int)get_post_meta( $post_ID, '_cff_oembed_done_checking', true ) === 1;
298
299 if ( ! $done_checking ) {
300
301 $num_found = CFF_Oembed::delete_facebook_oembed_caches( $post_ID );
302 if ( $num_found === 0 ) {
303 update_post_meta( $post_ID, '_cff_oembed_done_checking', 1 );
304 }
305 }
306 }
307
308 /**
309 * Loop through post meta data and if it's an oembed and has content
310 * that looks like a Facebook oembed, delete it
311 *
312 * @param $post_ID
313 *
314 * @return int number of old oembed caches found
315 *
316 * @since 2.16/3.16
317 */
318 public static function delete_facebook_oembed_caches( $post_ID ) {
319 $post_metas = get_post_meta( $post_ID );
320 if ( empty( $post_metas ) ) {
321 return 0;
322 }
323
324 $total_found = 0;
325 foreach ( $post_metas as $post_meta_key => $post_meta_value ) {
326 if ( '_oembed_' === substr( $post_meta_key, 0, 8 ) ) {
327 if ( strpos( $post_meta_value[0], 'class="fb-post"' ) !== false
328 && strpos( $post_meta_value[0], 'cff-embed-wrap' ) === false ) {
329 $total_found++;
330 delete_post_meta( $post_ID, $post_meta_key );
331 if ( '_oembed_time_' !== substr( $post_meta_key, 0, 13 ) ) {
332 delete_post_meta( $post_ID, str_replace( '_oembed_', '_oembed_time_', $post_meta_key ) );
333 }
334 } elseif ( strpos( $post_meta_value[0], 'class="fb-video"' ) !== false
335 && strpos( $post_meta_value[0], 'cff-embed-wrap' ) === false ) {
336 $total_found++;
337 delete_post_meta( $post_ID, $post_meta_key );
338 if ( '_oembed_time_' !== substr( $post_meta_key, 0, 13 ) ) {
339 delete_post_meta( $post_ID, str_replace( '_oembed_', '_oembed_time_', $post_meta_key ) );
340 }
341 }
342 }
343 }
344
345 return $total_found;
346 }
347
348 /**
349 * Current list of regex to identify FB URLs that could become oembeds using
350 * the 'oembed_post' endpoint.
351 *
352 * @return array
353 *
354 * @since 2.16/3.16
355 */
356 public static function post_providers() {
357 $post_embed_providers = array(
358 '#https?://www\.facebook\.com/.*/posts/.*#i',
359 '#https?://www\.facebook\.com/.*/activity/.*#i',
360 '#https?://www\.facebook\.com/.*/photos/.*#i',
361 '#https?://www\.facebook\.com/photo(s/|\.php).*#i',
362 '#https?://www\.facebook\.com/permalink\.php.*#i',
363 '#https?://www\.facebook\.com/media/.*#i',
364 '#https?://www\.facebook\.com/questions/.*#i',
365 '#https?://www\.facebook\.com/notes/.*#i',
366 );
367
368 return $post_embed_providers;
369 }
370
371 /**
372 * Current list of regex to identify FB URLs that could become oembeds using
373 * the 'oembed_video' endpoint.
374 *
375 * @return array
376 *
377 * @since 2.16/3.16
378 */
379 public static function video_providers() {
380 $video_embed_providers = array(
381 '#https?://www\.facebook\.com/.*/videos/.*#i',
382 '#https?://www\.facebook\.com/video\.php.*#i'
383 );
384
385 return $video_embed_providers;
386 }
387
388 /**
389 * Used for clearing the oembed update check flag for all posts
390 *
391 * @since 2.16/3.16
392 */
393 public static function clear_checks() {
394 global $wpdb;
395 $table_name = esc_sql( $wpdb->prefix . "postmeta" );
396 $result = $wpdb->query("
397 DELETE
398 FROM $table_name
399 WHERE meta_key = '_cff_oembed_done_checking';");
400 }
401 }
402
403 /*
404 function cffOembedInit() {
405 return new CFF_Oembed();
406 }
407 cffOembedInit();
408 */