Skip to content

Commit

Permalink
Merge pull request #263 from Automattic/fix-instagram-oembed-preview
Browse files Browse the repository at this point in the history
Adding a new class for properly handling Instagram oembeds in previews
  • Loading branch information
philipjohn committed Sep 13, 2016
2 parents c6fed8e + 2a13320 commit d369b7e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
63 changes: 63 additions & 0 deletions classes/class-wpcom-liveblog-entry-instagram-oembed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Class WPCOM_Liveblog_Entry_Instagram_oEmbed
*
* This addresses the issues with Instagram oEmbeds in Liveblog
*/
class WPCOM_Liveblog_Entry_Instagram_oEmbed {

/**
* Called by WPCOM_Liveblog::load(),
* it registers the initial filter
*/
public static function load() {

add_filter( 'liveblog_entry_enable_embeds', array( __CLASS__, 'register_filters' ) );

}

/**
* Register the filters just in time - when we are about to return HTML in endpoint
*/
public static function register_filters( $return ) {

add_filter( 'oembed_fetch_url', array( __CLASS__, 'add_omitscript_arg' ), 10, 3 );
add_filter( 'embed_oembed_html', array( __CLASS__, 'add_custom_script' ), 10, 4 );
add_filter( 'comment_text', array( __CLASS__, 'unregister_filters' ), 0, 1 );
return $return;
}

/**
* Deregister the filter we added
*/
public static function unregister_filters( $return ) {
remove_filter( 'oembed_fetch_url', array( __CLASS__, 'add_omitscript_arg' ), 10 );
remove_filter( 'embed_oembed_html', array( __CLASS__, 'add_custom_script' ), 10 );
return $return;
}

public static function add_omitscript_arg( $provider, $url, $args ) {
if ( true === self::is_instagram_provider( $url ) ) {
$provider = add_query_arg( 'omitscript', rawurlencode( true ), $provider );
}
return $provider;
}

public static function add_custom_script( $html, $url, $attr, $post_ID ) {
if ( true === self::is_instagram_provider( $url ) ) {
$instagram_script_uri = '//platform.instagram.com/en_US/embeds.js';
$script = '<script type="text/javascript">';
$script .= '(function( $ ){ if ( undefined === window.instgrm ) { $.getScript( '.wp_json_encode( $instagram_script_uri ).' ); } $(".instagram-media a").each( function(){ if ( -1 === $(this).attr("href").indexOf("instagr") ) { $(this).replaceWith( $(this).text() ) } } ); window.instgrm.Embeds.process(); })(jQuery);';
$script .= '</script>';
$html .= $script;
}
return $html;
}

private static function is_instagram_provider( $url ) {
$wp_oembed = _wp_oembed_get_object();
$provider = $wp_oembed->get_provider( $url );
return ( 'https://api.instagram.com/oembed' === $provider );
}
}
2 changes: 2 additions & 0 deletions liveblog.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public static function load() {
WPCOM_Liveblog_Entry_Extend::load();
WPCOM_Liveblog_Lazyloader::load();
WPCOM_Liveblog_Socketio_Loader::load();
WPCOM_Liveblog_Entry_Instagram_oEmbed::load();
}

public static function add_custom_post_type_support( $query ) {
Expand Down Expand Up @@ -122,6 +123,7 @@ private static function includes() {
require( dirname( __FILE__ ) . '/classes/class-wpcom-liveblog-entry-extend-feature-authors.php' );
require( dirname( __FILE__ ) . '/classes/class-wpcom-liveblog-lazyloader.php' );
require( dirname( __FILE__ ) . '/classes/class-wpcom-liveblog-socketio-loader.php' );
require( dirname( __FILE__ ) . '/classes/class-wpcom-liveblog-entry-instagram-oembed.php' );

// Manually include ms.php theme-side in multisite environments because
// we need its filesize and available space functions.
Expand Down

0 comments on commit d369b7e

Please sign in to comment.