Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a new class for properly handling Instagram oembeds in previews #263

Merged
merged 2 commits into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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