| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Simple YouTube Embed |
| 4 |
Version: 1.1.0.5 |
| 5 |
Plugin URI: https://noorsplugin.com/simple-youtube-embed-plugin/ |
| 6 |
Author: naa986 |
| 7 |
Author URI: https://noorsplugin.com/ |
| 8 |
Description: Embed YouTube videos in WordPress and customize the YouTube player |
| 9 |
Text Domain: simple-youtube-embed |
| 10 |
Domain Path: /languages |
| 11 |
*/ |
| 12 |
|
| 13 |
if(!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if(!class_exists('SIMPLE_YOUTUBE_EMBED')) |
| 18 |
{ |
| 19 |
class SIMPLE_YOUTUBE_EMBED |
| 20 |
{ |
| 21 |
var $plugin_version = '1.1.0.5'; |
| 22 |
var $plugin_url; |
| 23 |
var $plugin_path; |
| 24 |
function __construct() |
| 25 |
{ |
| 26 |
define('SIMPLE_YOUTUBE_EMBED_VERSION', $this->plugin_version); |
| 27 |
define('SIMPLE_YOUTUBE_EMBED_SITE_URL',site_url()); |
| 28 |
define('SIMPLE_YOUTUBE_EMBED_URL', $this->plugin_url()); |
| 29 |
define('SIMPLE_YOUTUBE_EMBED_PATH', $this->plugin_path()); |
| 30 |
$this->plugin_includes(); |
| 31 |
//add_action( 'wp_enqueue_scripts', array( $this, 'plugin_scripts' ), 0 ); |
| 32 |
} |
| 33 |
function plugin_includes() |
| 34 |
{ |
| 35 |
if(is_admin( ) ) |
| 36 |
{ |
| 37 |
include_once('extensions/simple-youtube-embed-extensions.php'); |
| 38 |
} |
| 39 |
add_action('plugins_loaded', array($this, 'plugins_loaded_handler')); |
| 40 |
add_action('admin_menu', array( $this, 'add_options_menu' )); |
| 41 |
//add_action('wp_head', 'simple_youtube_video_embed_js'); |
| 42 |
add_filter('embed_oembed_html', 'simple_youtube_video_embed', 10, 4); |
| 43 |
} |
| 44 |
function plugin_scripts() |
| 45 |
{ |
| 46 |
if (!is_admin()) |
| 47 |
{ |
| 48 |
wp_enqueue_script('jquery'); |
| 49 |
wp_register_script('waitforimages', SIMPLE_YOUTUBE_EMBED_URL.'/jquery.waitforimages.min.js', array('jquery'), SIMPLE_YOUTUBE_EMBED_VERSION); |
| 50 |
wp_enqueue_script('waitforimages'); |
| 51 |
wp_register_script('prettyembed', SIMPLE_YOUTUBE_EMBED_URL.'/jquery.prettyembed.min.js', array('jquery'), SIMPLE_YOUTUBE_EMBED_VERSION); |
| 52 |
wp_enqueue_script('prettyembed'); |
| 53 |
wp_register_script('fitvids', SIMPLE_YOUTUBE_EMBED_URL.'/jquery.fitvids.js', array('jquery'), SIMPLE_YOUTUBE_EMBED_VERSION); |
| 54 |
wp_enqueue_script('fitvids'); |
| 55 |
} |
| 56 |
} |
| 57 |
function plugin_url() |
| 58 |
{ |
| 59 |
if($this->plugin_url) { |
| 60 |
return $this->plugin_url; |
| 61 |
} |
| 62 |
return $this->plugin_url = plugins_url( basename( plugin_dir_path(__FILE__) ), basename( __FILE__ ) ); |
| 63 |
} |
| 64 |
function plugin_path(){ |
| 65 |
if ( $this->plugin_path ) { |
| 66 |
return $this->plugin_path; |
| 67 |
} |
| 68 |
return $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) ); |
| 69 |
} |
| 70 |
function add_plugin_action_links($links, $file) |
| 71 |
{ |
| 72 |
if ( $file == plugin_basename( dirname( __FILE__ ) . '/main.php' ) ) |
| 73 |
{ |
| 74 |
$links[] = '<a href="options-general.php?page=simple-youtube-embed-settings">'.__('Settings', 'simple-youtube-embed').'</a>'; |
| 75 |
} |
| 76 |
return $links; |
| 77 |
} |
| 78 |
|
| 79 |
function plugins_loaded_handler() |
| 80 |
{ |
| 81 |
if(is_admin() && current_user_can('manage_options')) |
| 82 |
{ |
| 83 |
add_filter('plugin_action_links', array($this, 'add_plugin_action_links'), 10, 2 ); |
| 84 |
} |
| 85 |
load_plugin_textdomain('simple-youtube-embed', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/'); |
| 86 |
} |
| 87 |
|
| 88 |
function add_options_menu() |
| 89 |
{ |
| 90 |
if(is_admin()) |
| 91 |
{ |
| 92 |
add_options_page(__('Simple YouTube Embed', 'simple-youtube-embed'), __('Simple YouTube Embed', 'simple-youtube-embed'), 'manage_options', 'simple-youtube-embed-settings', array($this, 'display_options_page')); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
function display_options_page() |
| 97 |
{ |
| 98 |
$plugin_tabs = array( |
| 99 |
'simple-youtube-embed-settings' => __('Extensions', 'simple-youtube-embed'), |
| 100 |
//'simple-youtube-embed-settings&action=extensions' => __('Extensions', 'simple-youtube-embed') |
| 101 |
); |
| 102 |
$url = "https://noorsplugin.com/simple-youtube-embed-plugin/"; |
| 103 |
$link_text = sprintf(wp_kses(__('Please visit the <a target="_blank" href="%s">Simple YouTube Embed</a> documentation page for usage instructions.', 'simple-youtube-embed'), array('a' => array('href' => array(), 'target' => array()))), esc_url($url)); |
| 104 |
echo '<div class="wrap">'; |
| 105 |
echo '<h2>Simple YouTube Embed - v'.$this->plugin_version.'</h2>'; |
| 106 |
echo '<div class="notice notice-info">'.$link_text.'</div>'; |
| 107 |
echo '<div id="poststuff"><div id="post-body">'; |
| 108 |
|
| 109 |
if (isset($_GET['page'])) { |
| 110 |
$current = sanitize_text_field($_GET['page']); |
| 111 |
if (isset($_GET['action'])) { |
| 112 |
$current .= "&action=" . sanitize_text_field($_GET['action']); |
| 113 |
} |
| 114 |
} |
| 115 |
$content = ''; |
| 116 |
$content .= '<h2 class="nav-tab-wrapper">'; |
| 117 |
foreach ($plugin_tabs as $location => $tabname) { |
| 118 |
if ($current == $location) { |
| 119 |
$class = ' nav-tab-active'; |
| 120 |
} else { |
| 121 |
$class = ''; |
| 122 |
} |
| 123 |
$content .= '<a class="nav-tab' . $class . '" href="?page=' . $location . '">' . $tabname . '</a>'; |
| 124 |
} |
| 125 |
$content .= '</h2>'; |
| 126 |
echo $content; |
| 127 |
|
| 128 |
if(isset($_GET['action'])) |
| 129 |
{ |
| 130 |
switch ($_GET['action']) |
| 131 |
{ |
| 132 |
case 'extensions': |
| 133 |
simple_youtube_embed_display_extensions(); |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
simple_youtube_embed_display_extensions(); |
| 140 |
} |
| 141 |
|
| 142 |
echo '</div></div>'; |
| 143 |
echo '</div>'; |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
$GLOBALS['simple_youtube_embed'] = new SIMPLE_YOUTUBE_EMBED(); |
| 148 |
} |
| 149 |
|
| 150 |
function simple_youtube_video_embed($html, $url, $attr, $post_ID) |
| 151 |
{ |
| 152 |
if(is_admin()){ //do not filter in visual mode so the video preview is shown |
| 153 |
return $html; |
| 154 |
} |
| 155 |
$data = array(); |
| 156 |
$parsed_url = parse_url($url); //parse the url to get query parameters |
| 157 |
if(isset($parsed_url['query']) && !empty($parsed_url['query'])){ |
| 158 |
parse_str($parsed_url['query'], $data); //get query parameters into an array |
| 159 |
} |
| 160 |
else{ |
| 161 |
return $html; |
| 162 |
} |
| 163 |
$src = ''; |
| 164 |
preg_match('/src="(.*?)"/', $html, $matches); |
| 165 |
if(isset($matches[1]) && !empty($matches[1])){ |
| 166 |
$src = $matches[1]; |
| 167 |
} |
| 168 |
else{ |
| 169 |
return $html; |
| 170 |
} |
| 171 |
/* WordPress automatically adds ?feature=oembed to YouTube iframe src. This causes an error in search console: Google could not determine the prominent video on the page */ |
| 172 |
if(strpos($src, 'feature') !== false){ |
| 173 |
$src = remove_query_arg('feature', $src); |
| 174 |
$html = preg_replace('/src="(.*?)"/', 'src="'.$src.'"', $html); |
| 175 |
} |
| 176 |
// |
| 177 |
if(isset($data['autoplay']) && $data['autoplay']=="1"){ |
| 178 |
if(strpos($src, 'autoplay') === false){ |
| 179 |
$src = add_query_arg('autoplay', '1', $src); |
| 180 |
$html = preg_replace('/src="(.*?)"/', 'src="'.$src.'"', $html); |
| 181 |
} |
| 182 |
} |
| 183 |
if(isset($data['controls']) && $data['controls']=="0"){ |
| 184 |
if(strpos($src, 'controls') === false){ |
| 185 |
$src = add_query_arg('controls', '0', $src); |
| 186 |
$html = preg_replace('/src="(.*?)"/', 'src="'.$src.'"', $html); |
| 187 |
} |
| 188 |
} |
| 189 |
if(isset($data['fs']) && $data['fs']=="0"){ |
| 190 |
if(strpos($src, 'fs') === false){ |
| 191 |
$src = add_query_arg('fs', '0', $src); |
| 192 |
$html = preg_replace('/src="(.*?)"/', 'src="'.$src.'"', $html); |
| 193 |
} |
| 194 |
} |
| 195 |
if(isset($data['rel']) && $data['rel']=="0"){ |
| 196 |
if(strpos($src, 'rel') === false){ |
| 197 |
$src = add_query_arg('rel', '0', $src); |
| 198 |
$html = preg_replace('/src="(.*?)"/', 'src="'.$src.'"', $html); |
| 199 |
} |
| 200 |
} |
| 201 |
if(isset($data['mute']) && $data['mute']=="1"){ |
| 202 |
if(strpos($src, 'mute') === false){ |
| 203 |
$src = add_query_arg('mute', '1', $src); |
| 204 |
$html = preg_replace('/src="(.*?)"/', 'src="'.$src.'"', $html); |
| 205 |
} |
| 206 |
} |
| 207 |
if(isset($data['iv_load_policy']) && $data['iv_load_policy']=="3"){ |
| 208 |
if(strpos($src, 'iv_load_policy') === false){ |
| 209 |
$src = add_query_arg('iv_load_policy', '3', $src); |
| 210 |
$html = preg_replace('/src="(.*?)"/', 'src="'.$src.'"', $html); |
| 211 |
} |
| 212 |
} |
| 213 |
if(isset($data['playlist']) && !empty($data['playlist'])){ |
| 214 |
if(strpos($src, 'playlist') === false){ |
| 215 |
$src = add_query_arg('playlist', $data['playlist'], $src); |
| 216 |
$html = preg_replace('/src="(.*?)"/', 'src="'.$src.'"', $html); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
$html = apply_filters('simple_youtube_embed_oembed_html', $html, $url, $attr, $post_ID, $data, $src); |
| 221 |
|
| 222 |
return $html; |
| 223 |
} |
| 224 |
|
| 225 |
function simple_youtube_video_embed_js() |
| 226 |
{ |
| 227 |
$output = <<<EOT |
| 228 |
<script type="text/javascript" charset="utf-8"> |
| 229 |
/* <![CDATA[ */ |
| 230 |
jQuery(document).ready(function($){ |
| 231 |
$(function(){ |
| 232 |
$().prettyEmbed({ useFitVids: true }); |
| 233 |
}); |
| 234 |
}); |
| 235 |
/* ]]> */ |
| 236 |
</script> |
| 237 |
EOT; |
| 238 |
echo $output; |
| 239 |
} |
| 240 |
|