[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/widgets/ -> class-wp-widget-media-video.php (source)

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


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7