Advertisement
Guest User

Untitled

a guest
Oct 9th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.24 KB | None | 0 0
  1. <?php
  2. /**
  3. * Video
  4. *
  5. * Shortcode which display a video
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly
  8.  
  9.  
  10. if ( ! class_exists( 'avia_sc_video' ) )
  11. {
  12. class avia_sc_video extends aviaShortcodeTemplate
  13. {
  14. /**
  15. * Create the config array for the shortcode button
  16. */
  17. function shortcode_insert_button()
  18. {
  19. $this->config['self_closing'] = 'yes';
  20.  
  21. $this->config['name'] = __( 'Video', 'avia_framework' );
  22. $this->config['tab'] = __('Media Elements', 'avia_framework' );
  23. $this->config['icon'] = AviaBuilder::$path['imagesURL']."sc-video.png";
  24. $this->config['order'] = 90;
  25. $this->config['target'] = 'avia-target-insert';
  26. $this->config['shortcode'] = 'av_video';
  27. // $this->config['modal_data'] = array( 'modal_class' => 'mediumscreen' );
  28. $this->config['tooltip'] = __( 'Display a video', 'avia_framework' );
  29. $this->config['disabling_allowed'] = false; //only allowed to be disabled by extra options
  30. $this->config['disabled'] = array(
  31. 'condition' => ( avia_get_option( 'disable_mediaelement' ) == 'disable_mediaelement' && avia_get_option( 'disable_video' ) == 'disable_video' ),
  32. 'text' => __( 'This element is disabled in your theme options. You can enable it in Enfold &raquo; Performance', 'avia_framework' )
  33. );
  34. $this->config['id_name'] = 'id';
  35. $this->config['id_show'] = 'yes';
  36. }
  37.  
  38.  
  39. function extra_assets()
  40. {
  41. //load css
  42. wp_enqueue_style( 'avia-module-video' , AviaBuilder::$path['pluginUrlRoot'].'avia-shortcodes/video/video.css' , array('avia-layout'), false );
  43. wp_enqueue_script( 'avia-module-slideshow-video' , AviaBuilder::$path['pluginUrlRoot'].'avia-shortcodes/slideshow/slideshow-video.js' , array('avia-shortcodes'), false, true );
  44. wp_enqueue_script( 'avia-module-video' , AviaBuilder::$path['pluginUrlRoot'].'avia-shortcodes/video/video.js' , array('avia-shortcodes'), false, true );
  45.  
  46.  
  47. }
  48.  
  49.  
  50. /**
  51. * Popup Elements
  52. *
  53. * If this function is defined in a child class the element automatically gets an edit button, that, when pressed
  54. * opens a modal window that allows to edit the element properties
  55. *
  56. * @return void
  57. */
  58. function popup_elements()
  59. {
  60. $text = "";
  61.  
  62. //if the element is disabled
  63. if($this->config['disabled']['condition'] == true)
  64. {
  65. $this->elements = array(
  66.  
  67. array(
  68. "name" => __("Element disabled",'avia_framework' ),
  69. "desc" => $this->config['disabled']['text'].
  70. '<br/><br/><a target="_blank" href="'.admin_url('admin.php?page=avia#goto_performance').'">'.__("Enable it here",'avia_framework' )."</a>",
  71. "type" => "heading",
  72. "description_class" => "av-builder-note av-error",
  73. )
  74. );
  75.  
  76. return;
  77. }
  78. //if self hosted is disabled
  79. else if(avia_get_option('disable_mediaelement') == 'disable_mediaelement')
  80. {
  81. $text = __("Please link to an external video by URL",'avia_framework' )."<br/><br/>".
  82. __("A list of all supported Video Services can be found on",'avia_framework' ).
  83. " <a target='_blank' href='http://codex.wordpress.org/Embeds#Okay.2C_So_What_Sites_Can_I_Embed_From.3F'>WordPress.org</a>. Youtube videos will display additional info like title, share link, related videos, ...<br/><br/>".
  84. __( 'Working examples:', 'avia_framework' ) . "<br/>" .
  85. "<strong>https://vimeo.com/1084537</strong><br/>" .
  86. "<strong>https://www.youtube.com/watch?v=G0k3kHtyoqc</strong><br/><br/>".
  87. "<strong class='av-builder-note'>" . __( 'Using self hosted videos is currently disabled. You can enable it in Enfold &raquo; Performance', 'avia_framework' ) . "</strong><br/>";
  88.  
  89. }
  90. //if youtube/vimeo is disabled
  91. else if(avia_get_option('disable_video') == 'disable_video')
  92. {
  93. $text = __("Either upload a new video or choose an existing video from your media library",'avia_framework' )."<br/><br/>".
  94. __("Different Browsers support different file types (mp4, ogv, webm). If you embed an example.mp4 video the video player will automatically check if an example.ogv and example.webm video is available and display those versions in case its possible and necessary",'avia_framework' )."<br/><br/><strong class='av-builder-note'>".
  95. __("Using external services like Youtube or Vimeo is currently disabled. You can enable it in Enfold &raquo; Performance",'avia_framework' )."</strong><br/>";
  96.  
  97. }
  98. //all video enabled
  99. else
  100. {
  101. $text = __("Either upload a new video, choose an existing video from your media library or link to a video by URL",'avia_framework' )."<br/><br/>".
  102. __("A list of all supported Video Services can be found on",'avia_framework' ).
  103. " <a target='_blank' href='http://codex.wordpress.org/Embeds#Okay.2C_So_What_Sites_Can_I_Embed_From.3F'>WordPress.org</a>. YouTube videos will display additional info like title, share link, related videos, ...<br/><br/>".
  104. __("Working examples, in case you want to use an external service:",'avia_framework' ). "<br/>".
  105. "<strong>https://vimeo.com/1084537</strong><br/>".
  106. "<strong>https://www.youtube.com/watch?v=G0k3kHtyoqc</strong><br/><br/>".
  107. "<strong>".__("Attention when using self hosted HTML 5 Videos",'avia_framework' ). ":</strong><br/>".
  108. __("Different Browsers support different file types (mp4, ogv, webm). If you embed an example.mp4 video the video player will automatically check if an example.ogv and example.webm video is available and display those versions in case its possible and necessary",'avia_framework' )."<br/>";
  109. }
  110.  
  111.  
  112.  
  113.  
  114. $this->elements = array(
  115.  
  116. array(
  117. "type" => "tab_container", 'nodescription' => true
  118. ),
  119.  
  120. array(
  121. "type" => "tab",
  122. "name" => __("Content" , 'avia_framework'),
  123. 'nodescription' => true
  124. ),
  125.  
  126. array(
  127. "name" => __("Choose Video",'avia_framework' ),
  128. "desc" => $text,
  129. "id" => "src",
  130. "type" => "video",
  131. "title" => __("Insert Video",'avia_framework' ),
  132. "button" => __("Insert",'avia_framework' ),
  133. "std" => ""
  134. ),
  135.  
  136.  
  137. array(
  138. "name" => __("Choose a preview/fallback image",'avia_framework' ),
  139. "desc" => __("Either upload a new, or choose an existing image from your media library",'avia_framework' )."<br/><small>".__("Video on most mobile devices can't be controlled properly with JavaScript, so you can upload a fallback image which will be displayed instead. This image is also used if lazy loading is active.", 'avia_framework' ) ."</small>" ,
  140. "id" => "mobile_image",
  141. "type" => "image",
  142. "title" => __("Choose Image",'avia_framework' ),
  143. "button" => __("Choose Image",'avia_framework' ),
  144. "std" => ""),
  145.  
  146. array(
  147. "name" => __("Video Format", 'avia_framework' ),
  148. "desc" => __("Choose if you want to display a modern 16:9 or classic 4:3 Video, or use a custom ratio", 'avia_framework' ),
  149. "id" => "format",
  150. "type" => "select",
  151. "std" => "16:9",
  152. "subtype" => array(
  153. __( '16:9', 'avia_framework' ) =>'16-9',
  154. __( '4:3', 'avia_framework' ) =>'4-3',
  155. __( 'Custom Ratio', 'avia_framework' ) =>'custom',
  156. )
  157. ),
  158.  
  159. array(
  160. "name" => __("Video width", 'avia_framework' ),
  161. "desc" => __("Enter a value for the width", 'avia_framework' ),
  162. "id" => "width",
  163. "type" => "input",
  164. "std" => "16",
  165. "required" => array('format','equals','custom')
  166. ),
  167.  
  168. array(
  169. "name" => __("Video height", 'avia_framework' ),
  170. "desc" => __("Enter a value for the height", 'avia_framework' ),
  171. "id" => "height",
  172. "type" => "input",
  173. "std" => "9",
  174. "required" => array('format','equals','custom')
  175. ),
  176.  
  177. array(
  178. "name" => __("Lazy Load videos", 'avia_framework' ),
  179. "desc" => __("Option to only load the preview image. The actual video will only be fetched once the user clicks on the image (Waiting for user interaction speeds up the inital pageload)", 'avia_framework' ),
  180. "id" => "conditional_play",
  181. "type" => "select",
  182. "std" => "",
  183. "subtype" => array(
  184. __( 'Always load videos', 'avia_framework' ) => '',
  185. __( 'Wait for user interaction to load the video', 'avia_framework' ) => 'confirm_all'
  186. ),
  187. ),
  188.  
  189. array(
  190. "name" => __("Hide Video Controls", 'avia_framework' ),
  191. "desc" => __("Check if you want to hide the controls (works for youtube and self hosted videos)", 'avia_framework' ) ,
  192. "id" => "video_controls",
  193. "std" => "",
  194. "type" => "checkbox"),
  195.  
  196. array(
  197. "name" => __("Mute Video Player", 'avia_framework' ),
  198. "desc" => __("Check if you want to mute the video", 'avia_framework' ) ,
  199. "id" => "video_mute",
  200. "std" => "",
  201. "type" => "checkbox"),
  202.  
  203. array(
  204. "name" => __("Loop Video Player", 'avia_framework' ),
  205. "desc" => __("Check if you want to loop the video (instead of showing the next slide the video will play from the beginning again)", 'avia_framework' ) ,
  206. "id" => "video_loop",
  207. "std" => "",
  208. "type" => "checkbox"),
  209.  
  210. array(
  211. "name" => __("Disable Autoplay", 'avia_framework' ),
  212. "desc" => __("Check if you want to disable video autoplay when this slide shows", 'avia_framework' ) ,
  213. "id" => "video_autoplay",
  214. "std" => "",
  215. "type" => "checkbox"),
  216.  
  217.  
  218.  
  219. array(
  220. "type" => "close_div",
  221. 'nodescription' => true
  222. ),
  223.  
  224.  
  225. array(
  226. 'type' => 'template',
  227. 'template_id' => 'screen_options_tab'
  228. ),
  229.  
  230.  
  231. array(
  232. "type" => "close_div",
  233. 'nodescription' => true
  234. ),
  235.  
  236.  
  237. );
  238.  
  239. if(current_theme_supports('avia_template_builder_custom_html5_video_urls'))
  240. {
  241. for ($i = 2; $i > 0; $i--)
  242. {
  243. $element = $this->elements[2];
  244. $element['id'] = 'src_'.$i;
  245. $element['name'] = __("Choose Another Video (HTML5 Only)",'avia_framework');
  246. $element['desc'] = __("Either upload a new video, choose an existing video from your media library or link to a video by URL.
  247. If you want to make sure that all browser can display your video upload a mp4, an ogv and a webm version of your video.",'avia_framework' );
  248.  
  249. array_splice($this->elements, 3, 0, array($element));
  250. }
  251. }
  252. }
  253.  
  254. /**
  255. * Editor Element - this function defines the visual appearance of an element on the AviaBuilder Canvas
  256. * Most common usage is to define some markup in the $params['innerHtml'] which is then inserted into the drag and drop container
  257. * Less often used: $params['data'] to add data attributes, $params['class'] to modify the className
  258. *
  259. *
  260. * @param array $params this array holds the default values for $content and $args.
  261. * @return $params the return array usually holds an innerHtml key that holds item specific markup.
  262. */
  263. function editor_element($params)
  264. {
  265. $template = $this->update_template("src", "URL: {{src}}");
  266. $url = isset( $params['args']['src'] ) ? $params['args']['src'] : '';
  267.  
  268. $params['content'] = null;
  269. $params['innerHtml'] = "<img src='".$this->config['icon']."' title='".$this->config['name']."' />";
  270. $params['innerHtml'].= "<div class='avia-element-label'>".$this->config['name']."</div>";
  271. $params['innerHtml'].= "<div class='avia-element-url' {$template}> URL: ". $url ."</div>";
  272. $params['class'] = "avia-video-element";
  273.  
  274. return $params;
  275. }
  276.  
  277. /**
  278. * Frontend Shortcode Handler
  279. *
  280. * @param array $atts array of attributes
  281. * @param string $content text within enclosing form of shortcode element
  282. * @param string $shortcodename the shortcode found, when == callback name
  283. * @return string $output returns the modified html string
  284. */
  285. function shortcode_handler( $atts, $content = "", $shortcodename = "", $meta = "" )
  286. {
  287.  
  288. extract( AviaHelper::av_mobile_sizes( $atts ) ); //return $av_font_classes, $av_title_font_classes and $av_display_classes
  289.  
  290. extract( shortcode_atts( array(
  291. 'src' => '',
  292. 'src_1' => '',
  293. 'src_2' => '',
  294. 'mobile_image' => '',
  295. 'fallback_link' => '',
  296. 'format' => '16:9',
  297. 'height' => '9',
  298. 'width' => '16',
  299. 'conditional_play' => '',
  300. 'video_controls' => '',
  301. 'video_mute' => '',
  302. 'video_loop' => '',
  303. 'video_autoplay' => '',
  304. 'attachment' => '',
  305. 'attachment_size' => '',
  306.  
  307. ), $atts, $this->config['shortcode'] ) );
  308.  
  309. $custom_class = ! empty( $meta['custom_class'] ) ? $meta['custom_class'] : '';
  310. $style = '';
  311. $html = '';
  312. $fallback_img = "";
  313. $fallback_img_style = "";
  314. $video_html_raw = '';
  315. $video_attributes = array( 'autoplay' => $video_autoplay, 'loop' => $video_loop, 'preload' => '', 'muted' => $video_mute, 'controls' => $video_controls );
  316.  
  317. if( $attachment )
  318. {
  319. $fallback_img = wp_get_attachment_image_src( $attachment, $attachment_size );
  320. $fallback_img = $fallback_img[0];
  321. $style = "background-image:url(\"{$fallback_img}\");";
  322. }
  323.  
  324. if( current_theme_supports( 'avia_template_builder_custom_html5_video_urls' ) )
  325. {
  326. $sources = array();
  327. if(!empty($src)) $sources['src'] = array('url' => $src, 'extension' => substr($src, strrpos($src, '.') + 1));
  328. if(!empty($src_1)) $sources['src_1'] = array('url' => $src_1, 'extension' => substr($src_1, strrpos($src_1, '.') + 1));
  329. if(!empty($src_2)) $sources['src_2'] = array('url' => $src_2, 'extension' => substr($src_2, strrpos($src_2, '.') + 1));
  330.  
  331. $html5 = false;
  332.  
  333. if( ! empty( $sources ) )
  334. {
  335. foreach( $sources as $source )
  336. {
  337. if( in_array( $source['extension'], array( 'ogv','webm','mp4' ) ) ) //check for html 5 video
  338. {
  339. $html5 = true;
  340. }
  341. else
  342. {
  343. $video = $source['url'];
  344. $html5 = false;
  345. break;
  346. }
  347. }
  348. }
  349.  
  350. if( $html5 && ! empty( $sources ) ) //check for html 5 video
  351. {
  352. $poster = '';
  353. if($fallback_img) $poster = "poster='{$fallback_img}'";
  354.  
  355. $video = '';
  356. foreach( $sources as $source )
  357. {
  358. $video .= $source['extension'].'="'.$source['url'].'" ';
  359. }
  360.  
  361. $video_html_raw = do_shortcode( '[video ' . $video . $poster . ']' );
  362.  
  363. $output = $video_html_raw;
  364. $html = "avia-video-html5";
  365. }
  366. else if( ! empty( $video ) )
  367. {
  368. global $wp_embed;
  369.  
  370. $video_html_raw = $wp_embed->run_shortcode( "[embed]" . trim($src) . "[/embed]" );
  371. $output = $video_html_raw;
  372. }
  373. }
  374. else
  375. {
  376. $file_extension = substr( $src, strrpos( $src, '.' ) + 1 );
  377.  
  378. if( in_array( $file_extension, array( 'ogv','webm','mp4' ) ) ) //check for html 5 video
  379. {
  380. $video_type[$file_extension] = 'type="video/'.$file_extension.'"';
  381. $video_html_raw = avia_html5_video_embed( $src, $fallback_img, $video_type, $video_attributes );
  382. $output = $video_html_raw;
  383.  
  384. $html = "avia-video-html5";
  385. }
  386. else
  387. {
  388. global $wp_embed;
  389.  
  390. $video_html_raw = $wp_embed->run_shortcode( "[embed]" . trim($src) . "[/embed]" );
  391. $output = $video_html_raw;
  392.  
  393. if( ! empty( $conditional_play ) )
  394. {
  395. //append autoplay so the user does not need to click 2 times
  396. preg_match( '!src="(.*?)"!', $output, $match );
  397. if( isset($match[1] ) )
  398. {
  399. if( strpos( $match[1], "?" ) === false )
  400. {
  401. $output = str_replace( $match[1], $match[1] . "?autoplay=1", $output );
  402. }
  403. else
  404. {
  405. $output = str_replace( $match[1], $match[1] . "&autoplay=1", $output );
  406. }
  407. }
  408. }
  409. else
  410. {
  411. $custom_class .= " av-lazyload-immediate ";
  412. }
  413.  
  414. $output = "<script type='text/html' class='av-video-tmpl'>{$output}</script>";
  415. $output .= "<div class='av-click-to-play-overlay'>";
  416. $output .= '<div class="avia_playpause_icon">';
  417. $output .= '</div>';
  418. $output .= '</div>';
  419.  
  420. $custom_class .= " av-lazyload-video-embed ";
  421. }
  422. }
  423.  
  424. if($format == 'custom')
  425. {
  426. $height = intval($height);
  427. $width = intval($width);
  428. $ratio = (100 / $width) * $height;
  429. $style .= "padding-bottom:{$ratio}%;";
  430. }
  431.  
  432. if(!empty($style))
  433. {
  434. $style = "style='{$style}'";
  435. }
  436.  
  437. if( ! empty( $output ) )
  438. {
  439. $markup = avia_markup_helper(array('context' => 'video','echo'=>false, 'custom_markup'=>$meta['custom_markup']));
  440. $output = "<div {$meta['custom_el_id']} class='avia-video avia-video-{$format} {$html} {$custom_class} {$av_display_classes}' {$style} {$markup} data-original_url='{$src}' >{$output}</div>";
  441. }
  442.  
  443.  
  444. /**
  445. * Allow plugins to change output in case they want to handle it by themself.
  446. * They must return the complete HTML structure.
  447. *
  448. * @since 4.5.7.2
  449. * @param string $output
  450. * @param array $atts
  451. * @param string $content
  452. * @param string $shortcodename
  453. * @param array|string $meta
  454. * @param string $video_html_raw
  455. * @return string
  456. */
  457. $output = apply_filters( 'avf_sc_video_output', $output, $atts, $content, $shortcodename, $meta, $video_html_raw );
  458.  
  459. return $output;
  460. }
  461.  
  462.  
  463. }
  464. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement