video.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @class FLVideoModule
  4. */
  5. class FLVideoModule extends FLBuilderModule {
  6. /**
  7. * @property $data
  8. */
  9. public $data = null;
  10. /**
  11. * @method __construct
  12. */
  13. public function __construct() {
  14. parent::__construct(array(
  15. 'name' => __( 'Video', 'fl-builder' ),
  16. 'description' => __( 'Render a WordPress or embedable video.', 'fl-builder' ),
  17. 'category' => __( 'Basic', 'fl-builder' ),
  18. 'partial_refresh' => true,
  19. 'icon' => 'format-video.svg',
  20. ));
  21. $this->add_js( 'jquery-fitvids' );
  22. add_filter( 'wp_video_shortcode', __CLASS__ . '::mute_video', 10, 4 );
  23. }
  24. /**
  25. * @method get_data
  26. */
  27. public function get_data() {
  28. if ( ! $this->data ) {
  29. $this->data = FLBuilderPhoto::get_attachment_data( $this->settings->video );
  30. if ( ! $this->data && isset( $this->settings->data ) ) {
  31. $this->data = $this->settings->data;
  32. }
  33. if ( $this->data ) {
  34. $parts = explode( '.', $this->data->filename );
  35. $this->data->extension = array_pop( $parts );
  36. $this->data->poster = isset( $this->settings->poster_src ) ? $this->settings->poster_src : '';
  37. $this->data->loop = isset( $this->settings->loop ) && $this->settings->loop ? ' loop="yes"' : '';
  38. $this->data->autoplay = isset( $this->settings->autoplay ) && $this->settings->autoplay ? ' autoplay="yes"' : '';
  39. // WebM format
  40. $webm_data = FLBuilderPhoto::get_attachment_data( $this->settings->video_webm );
  41. $this->data->video_webm = isset( $this->settings->video_webm ) && $webm_data ? ' webm="' . $webm_data->url . '"' : '';
  42. }
  43. }
  44. return $this->data;
  45. }
  46. /**
  47. * @method update
  48. * @param $settings {object}
  49. */
  50. public function update( $settings ) {
  51. // Cache the attachment data.
  52. if ( 'media_library' == $settings->video_type ) {
  53. $video = FLBuilderPhoto::get_attachment_data( $settings->video );
  54. if ( $video ) {
  55. $settings->data = $video;
  56. } else {
  57. $settings->data = null;
  58. }
  59. }
  60. return $settings;
  61. }
  62. /**
  63. * Temporary fix for autoplay in Chrome & Safari. Video shortcode doesn't support `muted` parameter.
  64. * Bug report: https://core.trac.wordpress.org/ticket/42718.
  65. *
  66. * @since 2.1.3
  67. * @param string $output Video shortcode HTML output.
  68. * @param array $atts Array of video shortcode attributes.
  69. * @param string $video Video file.
  70. * @param int $post_id Post ID.
  71. * @return string
  72. */
  73. static public function mute_video( $output, $atts, $video, $post_id ) {
  74. if ( false !== strpos( $output, 'autoplay="1"' ) && FLBuilderModel::get_post_id() == $post_id ) {
  75. $output = str_replace( '<video', '<video muted', $output );
  76. }
  77. return $output;
  78. }
  79. }
  80. /**
  81. * Register the module and its form settings.
  82. */
  83. FLBuilder::register_module('FLVideoModule', array(
  84. 'general' => array(
  85. 'title' => __( 'General', 'fl-builder' ),
  86. 'sections' => array(
  87. 'general' => array(
  88. 'title' => '',
  89. 'fields' => array(
  90. 'video_type' => array(
  91. 'type' => 'select',
  92. 'label' => __( 'Video Type', 'fl-builder' ),
  93. 'default' => 'wordpress',
  94. 'options' => array(
  95. 'media_library' => __( 'Media Library', 'fl-builder' ),
  96. 'embed' => __( 'Embed', 'fl-builder' ),
  97. ),
  98. 'toggle' => array(
  99. 'media_library' => array(
  100. 'fields' => array( 'video', 'video_webm', 'poster', 'autoplay', 'loop' ),
  101. ),
  102. 'embed' => array(
  103. 'fields' => array( 'embed_code' ),
  104. ),
  105. ),
  106. ),
  107. 'video' => array(
  108. 'type' => 'video',
  109. 'label' => __( 'Video (MP4)', 'fl-builder' ),
  110. 'help' => __( 'A video in the MP4 format. Most modern browsers support this format.', 'fl-builder' ),
  111. 'show_remove' => true,
  112. ),
  113. 'video_webm' => array(
  114. 'type' => 'video',
  115. 'label' => __( 'Video (WebM)', 'fl-builder' ),
  116. 'help' => __( 'A video in the WebM format to use as fallback. This format is required to support browsers such as FireFox and Opera.', 'fl-builder' ),
  117. 'preview' => array(
  118. 'type' => 'none',
  119. ),
  120. ),
  121. 'poster' => array(
  122. 'type' => 'photo',
  123. 'show_remove' => true,
  124. 'label' => _x( 'Poster', 'Video preview/fallback image.', 'fl-builder' ),
  125. ),
  126. 'autoplay' => array(
  127. 'type' => 'select',
  128. 'label' => __( 'Auto Play', 'fl-builder' ),
  129. 'default' => '0',
  130. 'options' => array(
  131. '0' => __( 'No', 'fl-builder' ),
  132. '1' => __( 'Yes', 'fl-builder' ),
  133. ),
  134. 'preview' => array(
  135. 'type' => 'none',
  136. ),
  137. ),
  138. 'loop' => array(
  139. 'type' => 'select',
  140. 'label' => __( 'Loop', 'fl-builder' ),
  141. 'default' => '0',
  142. 'options' => array(
  143. '0' => __( 'No', 'fl-builder' ),
  144. '1' => __( 'Yes', 'fl-builder' ),
  145. ),
  146. 'preview' => array(
  147. 'type' => 'none',
  148. ),
  149. ),
  150. 'embed_code' => array(
  151. 'type' => 'code',
  152. 'wrap' => true,
  153. 'editor' => 'html',
  154. 'label' => '',
  155. 'rows' => '9',
  156. 'connections' => array( 'custom_field' ),
  157. ),
  158. ),
  159. ),
  160. ),
  161. ),
  162. ));