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