<?php
/**
 * SDK loader for embedded content in liveblog entries.
 *
 * @package Liveblog
 */

/**
 * Class WPCOM_Liveblog_Entry_Embed_SDKs
 *
 * As we're render posts in React it requires some SDKs to pulled in on page load
 */
class WPCOM_Liveblog_Entry_Embed_SDKs {

	/**
	 * A list of provider SDKs.
	 *
	 * @var array
	 */
	protected static $sdks = array(
		'facebook'  => 'https://connect.facebook.net/en_US/sdk.js#xfbml=1&amp;version=v2.5',
		'twitter'   => 'https://platform.twitter.com/widgets.js',
		'instagram' => 'https://www.instagram.com/embed.js',
		'reddit'    => 'https://embed.reddit.com/widgets.js',
	);

	/**
	 * Called by WPCOM_Liveblog::load(),
	 * acts as a constructor
	 */
	public static function load() {
		self::$sdks = apply_filters( 'liveblog_embed_sdks', self::$sdks );

		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue' ) );
		add_filter( 'script_loader_tag', array( __CLASS__, 'add_async_attribute' ), 10, 2 );
	}

	/**
	 * Loop through provider SDKs and enqueue them
	 *
	 * @return void
	 */
	public static function enqueue() {
		if ( ! WPCOM_Liveblog::is_viewing_liveblog_post() ) {
			return;
		}

		foreach ( self::$sdks as $name => $url ) {
			// Don't attach version with Reddit JS script file, it will generate 404 error.
			$version = 'reddit' === $name ? null : WPCOM_Liveblog::VERSION;
			wp_enqueue_script( $name, esc_url( $url ), array(), $version, false );
		}
	}

	/**
	 * Set scripts to use async.
	 *
	 * @param string $tag    The script tag.
	 * @param string $handle The script handle.
	 * @return string Modified script tag.
	 */
	public static function add_async_attribute( $tag, $handle ) {
		if ( ! in_array( $handle, array_keys( self::$sdks ), true ) ) {
			return $tag;
		}
		return str_replace( ' src', ' async="async" src', $tag );
	}
}
