marian-cerny

Simple Video Embed WP Plugin

Oct 3rd, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.10 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Simple Video Embed
  4. Plugin URI:
  5. Description: Insert an HTML5 video with simple controls and Flash fallback using a single shortcode.
  6. Version: 1.0
  7. Author: Marian Cerny
  8. Author URI: http://mariancerny.com
  9. License: GPL2
  10. */
  11.  
  12.  
  13.  
  14. class mc_simple_video_embed
  15. {  
  16.    
  17.    
  18. // *******************************************************************
  19. // ------------------------------------------------------------------
  20. //                  INITIALIZATION
  21. // ------------------------------------------------------------------
  22. // *******************************************************************
  23.    
  24.     var $settings = array(
  25.         'autoplay' => true,
  26.         'start_muted' => true,
  27.         'video_directory' => '/video/',
  28.         'width' => 320,
  29.         'height' => 240
  30.     );     
  31.     var $video_dir_path;
  32.     var $video_dir_url;
  33.        
  34.     /* CONSTRUCTOR */
  35.     public function __construct()
  36.     {      
  37.         $this->get_settings();
  38.        
  39.         /* GET PATH AND URL TO DEFAULT VIDEO FOLDER */
  40.         $s_upload_dir = wp_upload_dir();
  41.         $this->video_dir_url = $s_upload_dir['baseurl'] . $this->settings['video_directory'];
  42.         $this->video_dir_path = $s_upload_dir['basedir'] . $this->settings['video_directory'];
  43.        
  44.         /* DEFINE CONSTANTS */
  45.         define( 'PLUGIN_NAME', 'Simple Video Embed' );
  46.         define( 'PLUGIN_SLUG', 'simple-video-embed' );
  47.         define( 'PLUGIN_URL', plugins_url( '', __FILE__ ) );
  48.         define( 'ASSETS_URL', plugins_url( '', __FILE__ ) . '/assets/' );
  49.        
  50.         /* CREATE THE VIDEO DIRECTORY */
  51.         if ( !file_exists( $this->video_dir_path ) )
  52.             mkdir( $this->video_dir_path );
  53.        
  54.         /* ADD ACTIONS */
  55.         add_action( 'admin_init', array( $this, 'mc_register_settings') );
  56.         add_action('wp_enqueue_scripts', array($this, 'mc_enqueue_scripts_and_styles' ));
  57.         add_shortcode('sve', array($this, 'mc_shortcode'));  
  58.         add_filter('widget_text', 'do_shortcode');
  59.     }
  60.    
  61.     /* ENQUEUE SCRIPTS */
  62.     function mc_enqueue_scripts_and_styles()
  63.     {
  64.         wp_enqueue_script( 'jquery' );
  65.         wp_enqueue_script( 'player_controller', ASSETS_URL . 'player-controller.js' );
  66.        
  67.         wp_enqueue_style( 'main', ASSETS_URL . 'styles.css' );
  68.     }
  69.    
  70.    
  71.     /* ASSIGN SETTINGS FROM PLUGIN OPTIONS TO SETTINGS VARIABLE */
  72.     function get_settings()
  73.     {
  74.         foreach ( $this->settings as $key => $value )
  75.         {
  76.             $option = get_option( 'sve_options_' . $key );
  77.             if ( !empty( $option ) )
  78.                 $this->settings[$key] = $value;
  79.         }
  80.     }
  81.    
  82.    
  83. // *******************************************************************
  84. // ------------------------------------------------------------------
  85. //                  CREATING THE OPTIONS MENU
  86. // ------------------------------------------------------------------
  87. // *******************************************************************
  88.     /* CREATE AN ENTRY IN THE SETTINGS MENU AND REGISTER SETTINGS */
  89.     function mc_register_settings()
  90.     {      
  91.         add_options_page(
  92.             PLUGIN_NAME,
  93.             PLUGIN_NAME,
  94.             'manage_options',
  95.             PLUGIN_SLUG,
  96.             array( $this, 'mc_output_options_page' )
  97.         );
  98.        
  99.         // CREATE 'GENERAL OPTIONS' SECTION
  100.         add_settings_section(
  101.             'sve_general_options',
  102.             'General settings',
  103.             array( $this, 'mc_output_settings_section_general' ),
  104.             PLUGIN_SLUG
  105.         );
  106.        
  107.         // ADD 'WIDTH' SETTING
  108.         add_settings_field(
  109.             'sve_options_width',
  110.             'Video width in px (default is 320)',
  111.             array($this, 'mc_output_option_width'),
  112.             PLUGIN_SLUG,
  113.             'sve_general_options'
  114.         );
  115.        
  116.         // ADD 'HEIGHT' SETTING
  117.         add_settings_field(
  118.             'sve_options_height',
  119.             'Video height in px (default is 240',
  120.             array($this, 'mc_output_option_height'),
  121.             PLUGIN_SLUG,
  122.             'sve_general_options'
  123.         );
  124.        
  125.         // ADD 'AUTOPLAY' SETTING
  126.         add_settings_field(
  127.             'sve_options_autoplay',
  128.             'Automatically play videos on page load',
  129.             array($this, 'mc_output_option_autoplay'),
  130.             PLUGIN_SLUG,
  131.             'sve_general_options'
  132.         );
  133.        
  134.         // ADD 'START MUTED' SETTING
  135.         add_settings_field(
  136.             'sve_options_start_muted',
  137.             'Start videos muted',
  138.             array($this, 'mc_output_option_start_muted'),
  139.             PLUGIN_SLUG,
  140.             'sve_general_options'
  141.         );
  142.        
  143.         // ADD 'VIDEO DIRECTORY' SETTING
  144.         add_settings_field(
  145.             'sve_options_video_directory',
  146.             'Video directory relative to \'/wp-content/uploads\' (default is \'/video\'',
  147.             array($this, 'mc_output_option_video_directory'),
  148.             PLUGIN_SLUG,
  149.             'sve_general_options'
  150.         );
  151.        
  152.         register_setting('mc_sve_options', 'sve_options_height');
  153.         register_setting('mc_sve_options', 'sve_options_width');
  154.         register_setting('mc_sve_options', 'sve_options_autoplay');
  155.         register_setting('mc_sve_options', 'sve_options_start_muted');
  156.         register_setting('mc_sve_options', 'sve_options_video_directory');
  157.     }
  158.    
  159.    
  160.     /* OUTPUT SETTINGS SECTION */
  161.     function mc_output_settings_section_general()
  162.     {
  163.         echo 'These values are used only if they are not explicitly defined in the shortcode';
  164.     }
  165.    
  166.    
  167.     /* OUTPUT WIDTH SETTING FIELD */
  168.     function mc_output_option_default_width()
  169.     {
  170.         echo '<input name="sve_options_width" id="sve_options_width" type="text" value='.get_option('sve_options_width'). ' />';
  171.     }
  172.    
  173.    
  174.     /* OUTPUT HEIGHT SETTING FIELD */
  175.     function mc_output_option_default_height()
  176.     {
  177.         echo '<input name="sve_options_height" id="sve_options_height" type="text" value='.get_option('sve_options_height'). ' />';
  178.     }
  179.    
  180.    
  181.     /* OUTPUT AUTOPLAY SETTINGS FIELD */
  182.     function mc_output_option_autoplay()
  183.     {
  184.         echo '<input name="sve_options_autoplay" id="sve_options_autoplay" type="checkbox" value="1" class="code" ' . checked( 1, get_option('sve_options_autoplay'), false ) . ' /> Enabled';
  185.     }
  186.    
  187.    
  188.     /* OUTPUT START MUTED SETTING FIELD */
  189.     function mc_output_option_start_muted()
  190.     {
  191.         echo '<input name="sve_options_start_muted" id="sve_options_start_muted" type="checkbox" value="1" class="code" ' . checked( 1, get_option('sve_options_start_muted'), false ) . ' /> Enabled';
  192.     }
  193.    
  194.    
  195.     /* OUTPUT DEFAULT DIRECTORY SETTING FIELD */
  196.     function mc_output_option_video_directory()
  197.     {
  198.         echo '<input name="sve_options_video_directory" id="sve_options_video_directory" type="text" value='.get_option('sve_options_video_directory'). ' />';
  199.     }
  200.    
  201.    
  202.     /* OUTPUT OPTIONS PAGE */
  203.     function mc_output_options_page()
  204.     {
  205.         ?>
  206.         <div class="wrap">
  207.         <h2><?php echo PLUGIN_NAME; ?> Settings</h2>
  208.        
  209.         <form method="post" action="options.php">
  210.        
  211.             <?php settings_fields( 'mc_sve_general_options' ); ?>
  212.             <?php do_settings_sections( 'mc_sve_general_options' ); ?>    
  213.             <?php submit_button(); ?>
  214.        
  215.         </form>
  216.         </div>
  217.         <?php
  218.     }
  219.    
  220. // *******************************************************************
  221. // ------------------------------------------------------------------
  222. //                  MAIN FUNCTIONS
  223. // ------------------------------------------------------------------
  224. // *******************************************************************
  225.    
  226.      
  227.     /* EXECUTE SHORTCODE */
  228.     public function mc_shortcode( $atts )  
  229.     {
  230.         // EXTRACT ATTRIBUTES FROM SHORTCODE
  231.         extract( shortcode_atts( array(
  232.             'filename' => 'video',  
  233.             'width' => $this->settings['width'],
  234.             'height' => $this->settings['height'],
  235.             'autoplay' => $this->settings['autoplay'],  
  236.             'start_muted' => $this->settings['start_muted'],
  237.             'video_directory' => $this->settings['video_directory'],  
  238.         ), $atts));
  239.        
  240.         /* CREATE OUTPUT VARIABLE AND ASSIGN THE VALUE IN THE OUTPUT FILE */
  241.         $s_output = '';
  242.         include('assets/output-video.php');
  243.         return $s_output;    
  244.     }
  245.    
  246.    
  247.     /* CHECK IF GIVEN FILE WITH THE GIVEN EXTENSION EXISTS AND RETURN THE DESIRED TAG/ATTRIBUTE  */
  248.     function mc_output_file_tag( $s_type, $s_filename, $s_extension )
  249.     {
  250.         // BUILD POSSIBLE FILENAME
  251.         $s_possible_filename = $s_filename;
  252.         $s_possible_filename .= ($s_type == 'poster') ? '_poster' : '';
  253.         $s_possible_filename .= '.' . $s_extension;
  254.        
  255.         if ( file_exists( $this->video_dir_path . $s_possible_filename ) )
  256.         {
  257.             $s_file_url =  $this->video_dir_url . $s_possible_filename;
  258.            
  259.             switch ( $s_type )
  260.             {
  261.                 case 'poster' :
  262.                     return "poster='{$s_file_url}' ";
  263.                 case 'video' :
  264.                     return "<source src='{$s_file_url}' />";
  265.                 case 'flash' :
  266.                     return "file={$s_file_url}";
  267.             }
  268.         }
  269.        
  270.         return false;
  271.     }
  272.    
  273.        
  274.    
  275. }
  276.  
  277. $mc_simple_video_embed = new mc_simple_video_embed();
  278.  
  279.  
  280.  
  281.  
  282.  
  283. ?>
Add Comment
Please, Sign In to add comment