home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / widgets / class-wp-widget-media-video.php < prev    next >
Encoding:
PHP Script  |  2017-10-18  |  8.0 KB  |  252 lines

  1. <?php
  2. /**
  3.  * Widget API: WP_Widget_Media_Video class
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Widgets
  7.  * @since 4.8.0
  8.  */
  9.  
  10. /**
  11.  * Core class that implements a video widget.
  12.  *
  13.  * @since 4.8.0
  14.  *
  15.  * @see WP_Widget
  16.  */
  17. class WP_Widget_Media_Video extends WP_Widget_Media {
  18.  
  19.     /**
  20.      * Constructor.
  21.      *
  22.      * @since  4.8.0
  23.      */
  24.     public function __construct() {
  25.         parent::__construct( 'media_video', __( 'Video' ), array(
  26.             'description' => __( 'Displays a video from the media library or from YouTube, Vimeo, or another provider.' ),
  27.             'mime_type'   => 'video',
  28.         ) );
  29.  
  30.         $this->l10n = array_merge( $this->l10n, array(
  31.             'no_media_selected' => __( 'No video selected' ),
  32.             'add_media' => _x( 'Add Video', 'label for button in the video widget' ),
  33.             'replace_media' => _x( 'Replace Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ),
  34.             'edit_media' => _x( 'Edit Video', 'label for button in the video widget; should preferably not be longer than ~13 characters long' ),
  35.             'missing_attachment' => sprintf(
  36.                 /* translators: %s: URL to media library */
  37.                 __( 'We can’t find that video. Check your <a href="%s">media library</a> and make sure it wasn’t deleted.' ),
  38.                 esc_url( admin_url( 'upload.php' ) )
  39.             ),
  40.             /* translators: %d: widget count */
  41.             'media_library_state_multi' => _n_noop( 'Video Widget (%d)', 'Video Widget (%d)' ),
  42.             'media_library_state_single' => __( 'Video Widget' ),
  43.             /* translators: %s: a list of valid video file extensions */
  44.             'unsupported_file_type' => sprintf( __( 'Sorry, we can’t load the video at the supplied URL. Please check that the URL is for a supported video file (%s) or stream (e.g. YouTube and Vimeo).' ), '<code>.' . implode( '</code>, <code>.', wp_get_video_extensions() ) . '</code>' ),
  45.         ) );
  46.     }
  47.  
  48.     /**
  49.      * Get schema for properties of a widget instance (item).
  50.      *
  51.      * @since  4.8.0
  52.      *
  53.      * @see WP_REST_Controller::get_item_schema()
  54.      * @see WP_REST_Controller::get_additional_fields()
  55.      * @link https://core.trac.wordpress.org/ticket/35574
  56.      * @return array Schema for properties.
  57.      */
  58.     public function get_instance_schema() {
  59.         $schema = array_merge(
  60.             parent::get_instance_schema(),
  61.             array(
  62.                 'preload' => array(
  63.                     'type' => 'string',
  64.                     'enum' => array( 'none', 'auto', 'metadata' ),
  65.                     'default' => 'metadata',
  66.                     'description' => __( 'Preload' ),
  67.                     'should_preview_update' => false,
  68.                 ),
  69.                 'loop' => array(
  70.                     'type' => 'boolean',
  71.                     'default' => false,
  72.                     'description' => __( 'Loop' ),
  73.                     'should_preview_update' => false,
  74.                 ),
  75.                 'content' => array(
  76.                     'type' => 'string',
  77.                     'default' => '',
  78.                     'sanitize_callback' => 'wp_kses_post',
  79.                     'description' => __( 'Tracks (subtitles, captions, descriptions, chapters, or metadata)' ),
  80.                     'should_preview_update' => false,
  81.                 ),
  82.             )
  83.         );
  84.  
  85.         foreach ( wp_get_video_extensions() as $video_extension ) {
  86.             $schema[ $video_extension ] = array(
  87.                 'type' => 'string',
  88.                 'default' => '',
  89.                 'format' => 'uri',
  90.                 /* translators: %s: video extension */
  91.                 'description' => sprintf( __( 'URL to the %s video source file' ), $video_extension ),
  92.             );
  93.         }
  94.  
  95.         return $schema;
  96.     }
  97.  
  98.     /**
  99.      * Render the media on the frontend.
  100.      *
  101.      * @since  4.8.0
  102.      *
  103.      * @param array $instance Widget instance props.
  104.      *
  105.      * @return void
  106.      */
  107.     public function render_media( $instance ) {
  108.         $instance = array_merge( wp_list_pluck( $this->get_instance_schema(), 'default' ), $instance );
  109.         $attachment = null;
  110.  
  111.         if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
  112.             $attachment = get_post( $instance['attachment_id'] );
  113.         }
  114.  
  115.         $src = $instance['url'];
  116.         if ( $attachment ) {
  117.             $src = wp_get_attachment_url( $attachment->ID );
  118.         }
  119.  
  120.         if ( empty( $src ) ) {
  121.             return;
  122.         }
  123.  
  124.         $youtube_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
  125.         $vimeo_pattern   = '#^https?://(.+\.)?vimeo\.com/.*#';
  126.  
  127.         if ( $attachment || preg_match( $youtube_pattern, $src ) || preg_match( $vimeo_pattern, $src ) ) {
  128.             add_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
  129.  
  130.             echo wp_video_shortcode(
  131.                 array_merge(
  132.                     $instance,
  133.                     compact( 'src' )
  134.                 ),
  135.                 $instance['content']
  136.             );
  137.  
  138.             remove_filter( 'wp_video_shortcode', array( $this, 'inject_video_max_width_style' ) );
  139.         } else {
  140.             echo $this->inject_video_max_width_style( wp_oembed_get( $src ) );
  141.         }
  142.     }
  143.  
  144.     /**
  145.      * Inject max-width and remove height for videos too constrained to fit inside sidebars on frontend.
  146.      *
  147.      * @since 4.8.0
  148.      *
  149.      * @param string $html Video shortcode HTML output.
  150.      * @return string HTML Output.
  151.      */
  152.     public function inject_video_max_width_style( $html ) {
  153.         $html = preg_replace( '/\sheight="\d+"/', '', $html );
  154.         $html = preg_replace( '/\swidth="\d+"/', '', $html );
  155.         $html = preg_replace( '/(?<=width:)\s*\d+px(?=;?)/', '100%', $html );
  156.         return $html;
  157.     }
  158.  
  159.     /**
  160.      * Enqueue preview scripts.
  161.      *
  162.      * These scripts normally are enqueued just-in-time when a video shortcode is used.
  163.      * In the customizer, however, widgets can be dynamically added and rendered via
  164.      * selective refresh, and so it is important to unconditionally enqueue them in
  165.      * case a widget does get added.
  166.      *
  167.      * @since 4.8.0
  168.      */
  169.     public function enqueue_preview_scripts() {
  170.         /** This filter is documented in wp-includes/media.php */
  171.         if ( 'mediaelement' === apply_filters( 'wp_video_shortcode_library', 'mediaelement' ) ) {
  172.             wp_enqueue_style( 'wp-mediaelement' );
  173.             wp_enqueue_script( 'mediaelement-vimeo' );
  174.             wp_enqueue_script( 'wp-mediaelement' );
  175.         }
  176.     }
  177.  
  178.     /**
  179.      * Loads the required scripts and styles for the widget control.
  180.      *
  181.      * @since 4.8.0
  182.      */
  183.     public function enqueue_admin_scripts() {
  184.         parent::enqueue_admin_scripts();
  185.  
  186.         $handle = 'media-video-widget';
  187.         wp_enqueue_script( $handle );
  188.  
  189.         $exported_schema = array();
  190.         foreach ( $this->get_instance_schema() as $field => $field_schema ) {
  191.             $exported_schema[ $field ] = wp_array_slice_assoc( $field_schema, array( 'type', 'default', 'enum', 'minimum', 'format', 'media_prop', 'should_preview_update' ) );
  192.         }
  193.         wp_add_inline_script(
  194.             $handle,
  195.             sprintf(
  196.                 'wp.mediaWidgets.modelConstructors[ %s ].prototype.schema = %s;',
  197.                 wp_json_encode( $this->id_base ),
  198.                 wp_json_encode( $exported_schema )
  199.             )
  200.         );
  201.  
  202.         wp_add_inline_script(
  203.             $handle,
  204.             sprintf(
  205.                 '
  206.                     wp.mediaWidgets.controlConstructors[ %1$s ].prototype.mime_type = %2$s;
  207.                     wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n = _.extend( {}, wp.mediaWidgets.controlConstructors[ %1$s ].prototype.l10n, %3$s );
  208.                 ',
  209.                 wp_json_encode( $this->id_base ),
  210.                 wp_json_encode( $this->widget_options['mime_type'] ),
  211.                 wp_json_encode( $this->l10n )
  212.             )
  213.         );
  214.     }
  215.  
  216.     /**
  217.      * Render form template scripts.
  218.      *
  219.      * @since 4.8.0
  220.      */
  221.     public function render_control_template_scripts() {
  222.         parent::render_control_template_scripts()
  223.         ?>
  224.         <script type="text/html" id="tmpl-wp-media-widget-video-preview">
  225.             <# if ( data.error && 'missing_attachment' === data.error ) { #>
  226.                 <div class="notice notice-error notice-alt notice-missing-attachment">
  227.                     <p><?php echo $this->l10n['missing_attachment']; ?></p>
  228.                 </div>
  229.             <# } else if ( data.error && 'unsupported_file_type' === data.error ) { #>
  230.                 <div class="notice notice-error notice-alt notice-missing-attachment">
  231.                     <p><?php echo $this->l10n['unsupported_file_type']; ?></p>
  232.                 </div>
  233.             <# } else if ( data.error ) { #>
  234.                 <div class="notice notice-error notice-alt">
  235.                     <p><?php _e( 'Unable to preview media due to an unknown error.' ); ?></p>
  236.                 </div>
  237.             <# } else if ( data.is_oembed && data.model.poster ) { #>
  238.                 <a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link">
  239.                     <img src="{{ data.model.poster }}" />
  240.                 </a>
  241.             <# } else if ( data.is_oembed ) { #>
  242.                 <a href="{{ data.model.src }}" target="_blank" class="media-widget-video-link no-poster">
  243.                     <span class="dashicons dashicons-format-video"></span>
  244.                 </a>
  245.             <# } else if ( data.model.src ) { #>
  246.                 <?php wp_underscore_video_template() ?>
  247.             <# } #>
  248.         </script>
  249.         <?php
  250.     }
  251. }
  252.