Created
June 20, 2013 06:56
-
-
Save elct9620/5820758 to your computer and use it in GitHub Desktop.
WordPress Plugin Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Aotoki; | |
class YoutubeShortcode { | |
static private $_instance; | |
private function __construct() | |
{ | |
} | |
static public function get_instance() | |
{ | |
if(self::$_instance == null) { | |
self::$_instance = new YoutubeShortcode(); | |
} | |
return self::$_instance; | |
} | |
public function init() | |
{ | |
// Register Shortcode | |
add_shortcode('youtube', array(&$this, 'handle_shortcode')); | |
// Add Script into WP | |
add_action('wp_enqueue_scripts', array(&$this, 'handle_enqueue_script')); | |
} | |
public function handle_enqueue_script() | |
{ | |
// Register JWPlayer Script | |
wp_enqueue_script('jwplayer', plugin_dir_url(YS_BASEDIR) . "vendor/jwplayer/jwplayer.js", array(), "6.4"); | |
} | |
public function handle_shortcode($atts) { | |
extract( | |
shortcode_atts( | |
array( | |
'multi' => false, | |
'url' => null | |
), $atts | |
) | |
); | |
if(empty($url)) { | |
// Didn't get url, then return noting | |
return; | |
} | |
$is_multi = (bool) $multi; | |
if($is_multi) { | |
// if have multi url, then split it | |
$url = explode(',', $url); | |
} | |
return $this->script_generator($url); | |
} | |
public function script_generator($urls) { | |
// Generate JWPlayer Script | |
$scripts = ""; | |
$identify = "ys-" . md5(time() + rand()); // Make ID unique | |
$scripts .= '<div id="'.$identify.'">Loading video...</div>'; | |
$scripts .= '<script>'; | |
if(is_array($urls)) { | |
$playlist = array(); | |
foreach($urls as $url) { | |
array_push($playlist, array('file' => $url)); | |
} | |
$playlist = json_encode($playlist); | |
$scripts .= 'jwplayer("'.$identify.'").setup({playlist: '.$playlist.'});'; | |
} else { | |
$scripts .= 'jwplayer("'.$identify.'").setup({file: "'.$urls.'"});'; | |
} | |
$scripts .= '</script>'; | |
return $scripts; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment