Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Simple Video Embed
- Plugin URI:
- Description: Insert an HTML5 video with simple controls and Flash fallback using a single shortcode.
- Version: 1.0
- Author: Marian Cerny
- Author URI: http://mariancerny.com
- License: GPL2
- */
- class mc_simple_video_embed
- {
- // *******************************************************************
- // ------------------------------------------------------------------
- // INITIALIZATION
- // ------------------------------------------------------------------
- // *******************************************************************
- var $settings = array(
- 'autoplay' => true,
- 'start_muted' => true,
- 'video_directory' => '/video/',
- 'width' => 320,
- 'height' => 240
- );
- var $video_dir_path;
- var $video_dir_url;
- /* CONSTRUCTOR */
- public function __construct()
- {
- $this->get_settings();
- /* GET PATH AND URL TO DEFAULT VIDEO FOLDER */
- $s_upload_dir = wp_upload_dir();
- $this->video_dir_url = $s_upload_dir['baseurl'] . $this->settings['video_directory'];
- $this->video_dir_path = $s_upload_dir['basedir'] . $this->settings['video_directory'];
- /* DEFINE CONSTANTS */
- define( 'PLUGIN_NAME', 'Simple Video Embed' );
- define( 'PLUGIN_SLUG', 'simple-video-embed' );
- define( 'PLUGIN_URL', plugins_url( '', __FILE__ ) );
- define( 'ASSETS_URL', plugins_url( '', __FILE__ ) . '/assets/' );
- /* CREATE THE VIDEO DIRECTORY */
- if ( !file_exists( $this->video_dir_path ) )
- mkdir( $this->video_dir_path );
- /* ADD ACTIONS */
- add_action( 'admin_init', array( $this, 'mc_register_settings') );
- add_action('wp_enqueue_scripts', array($this, 'mc_enqueue_scripts_and_styles' ));
- add_shortcode('sve', array($this, 'mc_shortcode'));
- add_filter('widget_text', 'do_shortcode');
- }
- /* ENQUEUE SCRIPTS */
- function mc_enqueue_scripts_and_styles()
- {
- wp_enqueue_script( 'jquery' );
- wp_enqueue_script( 'player_controller', ASSETS_URL . 'player-controller.js' );
- wp_enqueue_style( 'main', ASSETS_URL . 'styles.css' );
- }
- /* ASSIGN SETTINGS FROM PLUGIN OPTIONS TO SETTINGS VARIABLE */
- function get_settings()
- {
- foreach ( $this->settings as $key => $value )
- {
- $option = get_option( 'sve_options_' . $key );
- if ( !empty( $option ) )
- $this->settings[$key] = $value;
- }
- }
- // *******************************************************************
- // ------------------------------------------------------------------
- // CREATING THE OPTIONS MENU
- // ------------------------------------------------------------------
- // *******************************************************************
- /* CREATE AN ENTRY IN THE SETTINGS MENU AND REGISTER SETTINGS */
- function mc_register_settings()
- {
- add_options_page(
- PLUGIN_NAME,
- PLUGIN_NAME,
- 'manage_options',
- PLUGIN_SLUG,
- array( $this, 'mc_output_options_page' )
- );
- // CREATE 'GENERAL OPTIONS' SECTION
- add_settings_section(
- 'sve_general_options',
- 'General settings',
- array( $this, 'mc_output_settings_section_general' ),
- PLUGIN_SLUG
- );
- // ADD 'WIDTH' SETTING
- add_settings_field(
- 'sve_options_width',
- 'Video width in px (default is 320)',
- array($this, 'mc_output_option_width'),
- PLUGIN_SLUG,
- 'sve_general_options'
- );
- // ADD 'HEIGHT' SETTING
- add_settings_field(
- 'sve_options_height',
- 'Video height in px (default is 240',
- array($this, 'mc_output_option_height'),
- PLUGIN_SLUG,
- 'sve_general_options'
- );
- // ADD 'AUTOPLAY' SETTING
- add_settings_field(
- 'sve_options_autoplay',
- 'Automatically play videos on page load',
- array($this, 'mc_output_option_autoplay'),
- PLUGIN_SLUG,
- 'sve_general_options'
- );
- // ADD 'START MUTED' SETTING
- add_settings_field(
- 'sve_options_start_muted',
- 'Start videos muted',
- array($this, 'mc_output_option_start_muted'),
- PLUGIN_SLUG,
- 'sve_general_options'
- );
- // ADD 'VIDEO DIRECTORY' SETTING
- add_settings_field(
- 'sve_options_video_directory',
- 'Video directory relative to \'/wp-content/uploads\' (default is \'/video\'',
- array($this, 'mc_output_option_video_directory'),
- PLUGIN_SLUG,
- 'sve_general_options'
- );
- register_setting('mc_sve_options', 'sve_options_height');
- register_setting('mc_sve_options', 'sve_options_width');
- register_setting('mc_sve_options', 'sve_options_autoplay');
- register_setting('mc_sve_options', 'sve_options_start_muted');
- register_setting('mc_sve_options', 'sve_options_video_directory');
- }
- /* OUTPUT SETTINGS SECTION */
- function mc_output_settings_section_general()
- {
- echo 'These values are used only if they are not explicitly defined in the shortcode';
- }
- /* OUTPUT WIDTH SETTING FIELD */
- function mc_output_option_default_width()
- {
- echo '<input name="sve_options_width" id="sve_options_width" type="text" value='.get_option('sve_options_width'). ' />';
- }
- /* OUTPUT HEIGHT SETTING FIELD */
- function mc_output_option_default_height()
- {
- echo '<input name="sve_options_height" id="sve_options_height" type="text" value='.get_option('sve_options_height'). ' />';
- }
- /* OUTPUT AUTOPLAY SETTINGS FIELD */
- function mc_output_option_autoplay()
- {
- 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';
- }
- /* OUTPUT START MUTED SETTING FIELD */
- function mc_output_option_start_muted()
- {
- 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';
- }
- /* OUTPUT DEFAULT DIRECTORY SETTING FIELD */
- function mc_output_option_video_directory()
- {
- echo '<input name="sve_options_video_directory" id="sve_options_video_directory" type="text" value='.get_option('sve_options_video_directory'). ' />';
- }
- /* OUTPUT OPTIONS PAGE */
- function mc_output_options_page()
- {
- ?>
- <div class="wrap">
- <h2><?php echo PLUGIN_NAME; ?> Settings</h2>
- <form method="post" action="options.php">
- <?php settings_fields( 'mc_sve_general_options' ); ?>
- <?php do_settings_sections( 'mc_sve_general_options' ); ?>
- <?php submit_button(); ?>
- </form>
- </div>
- <?php
- }
- // *******************************************************************
- // ------------------------------------------------------------------
- // MAIN FUNCTIONS
- // ------------------------------------------------------------------
- // *******************************************************************
- /* EXECUTE SHORTCODE */
- public function mc_shortcode( $atts )
- {
- // EXTRACT ATTRIBUTES FROM SHORTCODE
- extract( shortcode_atts( array(
- 'filename' => 'video',
- 'width' => $this->settings['width'],
- 'height' => $this->settings['height'],
- 'autoplay' => $this->settings['autoplay'],
- 'start_muted' => $this->settings['start_muted'],
- 'video_directory' => $this->settings['video_directory'],
- ), $atts));
- /* CREATE OUTPUT VARIABLE AND ASSIGN THE VALUE IN THE OUTPUT FILE */
- $s_output = '';
- include('assets/output-video.php');
- return $s_output;
- }
- /* CHECK IF GIVEN FILE WITH THE GIVEN EXTENSION EXISTS AND RETURN THE DESIRED TAG/ATTRIBUTE */
- function mc_output_file_tag( $s_type, $s_filename, $s_extension )
- {
- // BUILD POSSIBLE FILENAME
- $s_possible_filename = $s_filename;
- $s_possible_filename .= ($s_type == 'poster') ? '_poster' : '';
- $s_possible_filename .= '.' . $s_extension;
- if ( file_exists( $this->video_dir_path . $s_possible_filename ) )
- {
- $s_file_url = $this->video_dir_url . $s_possible_filename;
- switch ( $s_type )
- {
- case 'poster' :
- return "poster='{$s_file_url}' ";
- case 'video' :
- return "<source src='{$s_file_url}' />";
- case 'flash' :
- return "file={$s_file_url}";
- }
- }
- return false;
- }
- }
- $mc_simple_video_embed = new mc_simple_video_embed();
- ?>
Add Comment
Please, Sign In to add comment