nuclearping

WordPress VideoJS Tweaks

May 6th, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.65 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Videojs HTML5 Player
  4. Version: 1.1.3
  5. Plugin URI: https://wphowto.net/videojs-html5-player-for-wordpress-757
  6. Author: naa986
  7. Author URI: https://wphowto.net/
  8. Description: Easily embed videos using videojs html5 player
  9. Text Domain: videojs-html5-player
  10. Domain Path: /languages
  11.  */
  12.  
  13. /*
  14. - Removes the hardcoded "px" appendix to width and allows for more user freedom.
  15. - Utilizes the "class" parameter, which was ignored before, even though you could add it to the shortcode parameters.
  16. - See https://wordpress.org/support/topic/tweaks-to-width/
  17. */
  18.  
  19. if (!defined('ABSPATH')) {
  20.     exit;
  21. }
  22. if (!class_exists('VIDEOJS_HTML5_PLAYER')) {
  23.  
  24.     class VIDEOJS_HTML5_PLAYER {
  25.  
  26.         var $plugin_version = '1.1.3';
  27.         var $videojs_version = '5.0.0';
  28.  
  29.         function __construct() {
  30.             define('VIDEOJS_HTML5_PLAYER_VERSION', $this->plugin_version);
  31.             $this->plugin_includes();
  32.         }
  33.  
  34.         function plugin_includes() {
  35.             if (is_admin()) {
  36.                 add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2);
  37.             }
  38.             add_action('plugins_loaded', array($this, 'plugins_loaded_handler'));
  39.             add_action('wp_enqueue_scripts', 'videojs_html5_player_enqueue_scripts');
  40.             add_action('admin_menu', array($this, 'add_options_menu'));
  41.             add_action('wp_head', 'videojs_html5_player_header');
  42.             add_shortcode('videojs_video', 'videojs_html5_video_embed_handler');
  43.             //allows shortcode execution in the widget, excerpt and content
  44.             add_filter('widget_text', 'do_shortcode');
  45.             add_filter('the_excerpt', 'do_shortcode', 11);
  46.             add_filter('the_content', 'do_shortcode', 11);
  47.         }
  48.  
  49.         function plugin_url() {
  50.             if ($this->plugin_url)
  51.                 return $this->plugin_url;
  52.             return $this->plugin_url = plugins_url(basename(plugin_dir_path(__FILE__)), basename(__FILE__));
  53.         }
  54.  
  55.         function plugin_action_links($links, $file) {
  56.             if ($file == plugin_basename(dirname(__FILE__) . '/videojs-html5-player.php')) {
  57.                 $links[] = '<a href="options-general.php?page=videojs-html5-player-settings">'.__('Settings', 'videojs-html5-player').'</a>';
  58.             }
  59.             return $links;
  60.         }
  61.        
  62.         function plugins_loaded_handler()
  63.         {
  64.             load_plugin_textdomain('videojs-html5-player', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
  65.         }
  66.  
  67.         function add_options_menu() {
  68.             if (is_admin()) {
  69.                 add_options_page(__('Videojs Settings', 'videojs-html5-player'), __('Videojs HTML5 Player', 'videojs-html5-player'), 'manage_options', 'videojs-html5-player-settings', array($this, 'options_page'));
  70.             }
  71.         }
  72.  
  73.         function options_page() {
  74.             $url = "https://wphowto.net/videojs-html5-player-for-wordpress-757";
  75.             $link_text = sprintf(wp_kses(__('For detailed documentation please visit the plugin homepage <a target="_blank" href="%s">here</a>.', 'videojs-html5-player'), array('a' => array('href' => array(), 'target' => array()))), esc_url($url));
  76.             ?>
  77.             <div class="wrap"><h2>Videojs HTML5 Player - v<?php echo $this->plugin_version; ?></h2>
  78.             <div class="update-nag"><?php echo $link_text;?></div>
  79.             </div>
  80.             <?php
  81.         }
  82.  
  83.     }
  84.  
  85.     $GLOBALS['easy_video_player'] = new VIDEOJS_HTML5_PLAYER();
  86. }
  87.  
  88. function videojs_html5_player_enqueue_scripts() {
  89.     if (!is_admin()) {
  90.         $plugin_url = plugins_url('', __FILE__);
  91.         wp_enqueue_script('jquery');
  92.         wp_register_style('videojs', $plugin_url . '/videojs/video-js.min.css');
  93.         wp_enqueue_style('videojs');
  94.         /*
  95.         wp_register_style('videojs-style', $plugin_url . '/videojs-html5-player.css');
  96.         wp_enqueue_style('videojs-style');
  97.         */
  98.         wp_register_script('videojs', $plugin_url . '/videojs/video.min.js', array('jquery'), VIDEOJS_HTML5_PLAYER_VERSION, true);
  99.         wp_enqueue_script('videojs');
  100.     }
  101. }
  102.  
  103. function videojs_html5_player_header() {
  104.     if (!is_admin()) {
  105.         $config = '<!-- This site is embedding videos using the Videojs HTML5 Player plugin v' . VIDEOJS_HTML5_PLAYER_VERSION . ' - http://wphowto.net/videojs-html5-player-for-wordpress-757 -->';
  106.         echo $config;
  107.     }
  108. }
  109.  
  110. function videojs_html5_video_embed_handler($atts) {
  111.     extract(shortcode_atts(array(
  112.         'url' => '',
  113.         'webm' => '',
  114.         'ogv' => '',
  115.         'width' => '',
  116.         'controls' => '',
  117.         'preload' => 'auto',
  118.         'autoplay' => 'false',
  119.         'loop' => '',
  120.         'muted' => '',
  121.         'poster' => '',
  122.         'class' => '',
  123.     ), $atts));
  124.     if(empty($url)){
  125.         return __('you need to specify the src of the video file', 'videojs-html5-player');
  126.     }
  127.     //src
  128.     $src = '<source src="'.$url.'" type="video/mp4" />';
  129.     if (!empty($webm)) {
  130.         $webm = '<source src="'.$webm.'" type="video/webm" />';
  131.         $src = $src.$webm;
  132.     }
  133.     if (!empty($ogv)) {
  134.         $ogv = '<source src="'.$ogv.'" type="video/ogg" />';
  135.         $src = $src.$ogv;
  136.     }
  137.     //controls
  138.     if($controls == "false") {
  139.         $controls = "";
  140.     }
  141.     else{
  142.         $controls = " controls";
  143.     }
  144.     //preload
  145.     if($preload == "metadata") {
  146.         $preload = ' preload="metadata"';
  147.     }
  148.     else if($preload == "none") {
  149.         $preload = ' preload="none"';
  150.     }
  151.     else{
  152.         $preload = ' preload="auto"';
  153.     }
  154.     //autoplay
  155.     if($autoplay == "true"){
  156.         $autoplay = " autoplay";
  157.     }
  158.     else{
  159.         $autoplay = "";
  160.     }
  161.     //loop
  162.     if($loop == "true"){
  163.         $loop = " loop";
  164.     }
  165.     else{
  166.         $loop = "";
  167.     }
  168.     //muted
  169.     if($muted == "true"){
  170.         $muted = " muted";
  171.     }
  172.     else{
  173.         $muted = "";
  174.     }
  175.     //poster
  176.     if(!empty($poster)) {
  177.         $poster = ' poster="'.$poster.'"';
  178.     }
  179.     //playsinline
  180.     $playsinline = ' playsinline';
  181.     $player = "videojs" . uniqid();
  182.     //custom style
  183.     $style = '';
  184.     if(!empty($width)){
  185.         if (is_numeric($width[strlen($width) - 1]))
  186.         {
  187.             $width .= "px";
  188.         }
  189.         $style = <<<EOT
  190.         <style>
  191.         #$player {
  192.             max-width:{$width};
  193.         }
  194.         </style>
  195. EOT;
  196.     }
  197.     $output = <<<EOT
  198.     <video id="$player" class="video-js vjs-default-skin vjs-big-play-centered {$class}"{$controls}{$preload}{$autoplay}{$loop}{$muted}{$poster}{$playsinline} data-setup='{"fluid": true}'>
  199.         $src
  200.     </video>
  201.     $style
  202. EOT;
  203.     return $output;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment