Skip to content

Instantly share code, notes, and snippets.

@samikeijonen
Last active November 3, 2016 22:53
Show Gist options
  • Save samikeijonen/10f60fd3d1520cfb39dd to your computer and use it in GitHub Desktop.
Save samikeijonen/10f60fd3d1520cfb39dd to your computer and use it in GitHub Desktop.
This plugin makes your oEmbed responsive without JQuery using FluidVids script.
<?php
/**
* Plugin Name: Responsive oEmbed
* Plugin URI: https://foxnet-themes.fi
* Description: This plugin makes your oEmbed responsive without JQuery using FluidVids script.
* Version: 1.0.0
* Author: Sami Keijonen
* Author URI: http://foxnet.fi
* Text Domain: responsive-oembed
* Domain Path: /languages
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package Responsive oEmbed
* @version 1.0.0
* @since 1.0.0
* @author Sami Keijonen <[email protected]>
* @copyright Copyright (c) 2014, Sami Keijonen
* @link https://foxnet-themes.fi
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/* Exit if accessed directly. */
if ( ! defined( 'ABSPATH' ) ) exit;
final class Responsive_oEmbed {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.0.0
* @var string
*/
protected $plugin_version = '1.0.0';
/**
* fluidvids version, used for cache-busting of script file references.
*
* @since 1.0.0
* @var string
*/
protected $fluidvids_version = '2.4.1';
/**
* Holds the instances of this class.
*
* @since 1.0.0
* @access private
* @var object
*/
private static $instance;
/**
* Plugin setup.
*
* @since 1.0.0
* @access public
* @return void
*/
public function __construct() {
/* Set the constants needed by the plugin. */
add_action( 'plugins_loaded', array( $this, 'constants' ), 1 );
/* Internationalize the text strings used. */
add_action( 'plugins_loaded', array( $this, 'i18n' ), 2 );
/* Enqueue scripts and styles. */
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
/* Add JS when oembeds are around. */
add_filter( 'wp_video_shortcode', array( $this, 'add_js_oembed' ) );
add_filter( 'embed_oembed_html', array( $this, 'add_js_oembed' ) );
add_filter( 'video_embed_html', array( $this, 'add_js_oembed' ) );
}
/**
* Defines constants used by the plugin.
*
* @since 1.0.0
* @access public
* @return void
*/
public static function constants() {
/* Set constant path to the plugin directory. */
define( 'ROV_DIR', trailingslashit( plugin_dir_path( __FILE__ ) ) );
/* Set the constant path to the plugin directory URI. */
define( 'ROV_URI', trailingslashit( plugin_dir_url( __FILE__ ) ) );
}
/**
* Loads the translation files.
*
* @since 1.0.0
* @access public
* @return void
*/
public static function i18n() {
/* Load the translation of the plugin. */
load_plugin_textdomain( 'responsive-oembed', false, 'responsive-oembed/languages' );
}
/**
* Loads the stylesheet for the plugin.
*
* @since 1.0.0
* @access public
* @return void
*/
public function enqueue_scripts() {
/* Use the .min stylesheet if SCRIPT_DEBUG is turned off. */
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
/* Register the JS file. */
wp_register_script( 'responsive-oembed', plugins_url( '/js/responsive-oembed.js', __FILE__ ), array(), $this->fluidvids_version, true );
}
/**
* Add JS when oembeds are around to make them responsive.
*
* @since 1.0.0
* @access public
* @return void
*/
function add_js_oembed( $html ) {
/* Return if empty. */
if ( empty( $html ) || ! is_string( $html ) ) {
return $html;
}
/* Enqueue the JS file. */
wp_enqueue_script( 'responsive-oembed' );
/* Add JS in the footer. */
add_action( 'wp_footer', array( $this, 'add_js_footer' ), 21 );
/* Return html. */
return $html;
}
/**
* Add JS in footer.
*
* @since 1.0.0
* @access public
* @return void
*/
function add_js_footer() { ?>
<script>
fluidvids.init({
selector: ['embed', 'iframe', 'object'], // runs querySelectorAll()
players: ['www.youtube.com', 'player.vimeo.com'] // players to support, not needed actually
});
</script>
<?php
}
/**
* Returns the instance.
*
* @since 1.0.0
* @access public
* @return object
*/
public static function get_instance() {
if ( !self::$instance )
self::$instance = new self;
return self::$instance;
}
}
Responsive_oEmbed::get_instance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment