builders
2 years ago
privacy
2 years ago
class-yit-ajax.php
2 years ago
class-yit-assets.php
2 years ago
class-yit-cpt-unlimited.php
5 years ago
class-yit-gradients.php
2 years ago
class-yit-help-desk.php
2 years ago
class-yit-icons.php
2 years ago
class-yit-metabox.php
2 years ago
class-yit-plugin-common.php
5 years ago
class-yit-plugin-licence.php
2 years ago
class-yit-plugin-panel-woocommerce.php
1 year ago
class-yit-plugin-panel.php
1 year ago
class-yit-plugin-subpanel.php
2 years ago
class-yit-pointers.php
2 years ago
class-yit-theme-licence.php
2 years ago
class-yit-upgrade.php
5 years ago
class-yit-video.php
5 years ago
class-yith-bh-onboarding.php
3 years ago
class-yith-dashboard.php
4 years ago
class-yith-debug.php
2 years ago
class-yith-external-services.php
1 year ago
class-yith-post-type-admin.php
2 years ago
class-yith-system-status.php
1 year ago
class-yit-video.php
193 lines
| 1 | <?php |
| 2 | /** |
| 3 | * YITH Video Class |
| 4 | * manage videos from youtube, vimeo and other services. |
| 5 | * |
| 6 | * @class YIT_Video |
| 7 | * @package YITH\PluginFramework\Classes |
| 8 | */ |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 11 | |
| 12 | if ( ! class_exists( 'YIT_Video' ) ) { |
| 13 | /** |
| 14 | * YIT_Video class. |
| 15 | * |
| 16 | * @deprecated 3.5 |
| 17 | */ |
| 18 | class YIT_Video { |
| 19 | |
| 20 | /** |
| 21 | * Generate the HTML for a youtube video |
| 22 | * |
| 23 | * @param array $args Array of arguments to configure the video to generate. |
| 24 | * |
| 25 | * @return string |
| 26 | */ |
| 27 | public static function youtube( $args = array() ) { |
| 28 | $defaults = array( |
| 29 | 'id' => '', |
| 30 | 'url' => '', |
| 31 | 'width' => 425, |
| 32 | 'height' => 356, |
| 33 | 'echo' => false, |
| 34 | ); |
| 35 | $args = wp_parse_args( $args, $defaults ); |
| 36 | |
| 37 | $id = $args['id']; |
| 38 | $url = $args['url']; |
| 39 | $width = $args['width']; |
| 40 | $height = $args['height']; |
| 41 | $echo = $args['echo']; |
| 42 | $html = ''; |
| 43 | |
| 44 | // Retrieve the video ID if we have only the URL. |
| 45 | if ( ! $id && ! ! $url ) { |
| 46 | $id = self::video_id_by_url( $url ); |
| 47 | } |
| 48 | |
| 49 | if ( $id ) { |
| 50 | $id = preg_replace( '/[&|&]feature=([\w\-]*)/', '', $id ); |
| 51 | $id = preg_replace( '/(youtube|vimeo):/', '', $id ); |
| 52 | $url = "https://www.youtube.com/embed/{$id}?wmode=transparent"; |
| 53 | |
| 54 | $html = '<div class="post_video youtube">' . |
| 55 | '<iframe wmode="transparent" width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" src="' . esc_url( $url ) . '" frameborder="0" allowfullscreen></iframe>' . |
| 56 | '</div>'; |
| 57 | $html = apply_filters( 'yit_video_youtube', $html ); |
| 58 | } |
| 59 | |
| 60 | if ( $echo ) { |
| 61 | echo wp_kses_post( $html ); |
| 62 | } |
| 63 | |
| 64 | return $html; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Generate the HTML for a vimeo video |
| 69 | * |
| 70 | * @param array $args Array of arguments to configure the video to generate. |
| 71 | * |
| 72 | * @return string |
| 73 | */ |
| 74 | public static function vimeo( $args = array() ) { |
| 75 | $defaults = array( |
| 76 | 'id' => '', |
| 77 | 'url' => '', |
| 78 | 'width' => 425, |
| 79 | 'height' => 356, |
| 80 | 'echo' => false, |
| 81 | ); |
| 82 | $args = wp_parse_args( $args, $defaults ); |
| 83 | |
| 84 | $id = $args['id']; |
| 85 | $url = $args['url']; |
| 86 | $width = $args['width']; |
| 87 | $height = $args['height']; |
| 88 | $echo = $args['echo']; |
| 89 | $html = ''; |
| 90 | |
| 91 | // Retrieve the video ID if we have only the URL. |
| 92 | if ( ! $id && ! ! $url ) { |
| 93 | $id = self::video_id_by_url( $url ); |
| 94 | } |
| 95 | |
| 96 | if ( $id ) { |
| 97 | $id = preg_replace( '/[&|&]feature=([\w\-]*)/', '', $id ); |
| 98 | $id = preg_replace( '/(youtube|vimeo):/', '', $id ); |
| 99 | $protocol = is_ssl() ? 'https' : 'http'; |
| 100 | $url = "{$protocol}://player.vimeo.com/video/{$id}?title=0&byline=0&portrait=0"; |
| 101 | |
| 102 | $html = '<div class="post_video youtube">' . |
| 103 | '<iframe wmode="transparent" width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" src="' . esc_url( $url ) . '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>' . |
| 104 | '</div>'; |
| 105 | $html = apply_filters( 'yit_video_vimeo', $html ); |
| 106 | } |
| 107 | |
| 108 | if ( $echo ) { |
| 109 | echo wp_kses_post( $html ); |
| 110 | } |
| 111 | |
| 112 | return $html; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Retrieve video ID from URL |
| 117 | * |
| 118 | * @param string $url URL of the video. |
| 119 | * |
| 120 | * @return bool|string |
| 121 | */ |
| 122 | public static function video_id_by_url( $url ) { |
| 123 | $parsed = wp_parse_url( esc_url( $url ) ); |
| 124 | $host = isset( $parsed['host'] ) ? $parsed['host'] : false; |
| 125 | |
| 126 | switch ( $host ) { |
| 127 | case 'youtube.com': |
| 128 | case 'www.youtube.com': |
| 129 | case 'youtu.be': |
| 130 | case 'www.youtu.be': |
| 131 | $id = self::youtube_id_by_url( $url ); |
| 132 | $video_id = "youtube:$id"; |
| 133 | break; |
| 134 | |
| 135 | case 'www.vimeo.com': |
| 136 | case 'vimeo.com': |
| 137 | preg_match( '/http(s)?:\/\/(\w+.)?vimeo\.com\/(.*\/)?([0-9]+)/', $url, $matches ); |
| 138 | |
| 139 | $id = trim( $matches[4], '/' ); |
| 140 | $video_id = "vimeo:$id"; |
| 141 | break; |
| 142 | |
| 143 | default: |
| 144 | $video_id = false; |
| 145 | |
| 146 | } |
| 147 | |
| 148 | return $video_id; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Retrieve video ID from URL |
| 153 | * |
| 154 | * @param string $url URL of the video. |
| 155 | * |
| 156 | * @return bool|string |
| 157 | */ |
| 158 | protected static function youtube_id_by_url( $url ) { |
| 159 | if ( preg_match( '/http(s)?:\/\/youtu.be/', $url, $matches ) ) { |
| 160 | $url = wp_parse_url( $url, PHP_URL_PATH ); |
| 161 | $url = str_replace( '/', '', $url ); |
| 162 | |
| 163 | return $url; |
| 164 | |
| 165 | } elseif ( preg_match( '/watch/', $url, $matches ) ) { |
| 166 | $arr = wp_parse_url( $url ); |
| 167 | $url = str_replace( 'v=', '', $arr['query'] ); |
| 168 | |
| 169 | return $url; |
| 170 | |
| 171 | } elseif ( preg_match( '/http(s)?:\/\/(\w+.)?youtube.com\/v/', $url, $matches ) ) { |
| 172 | $arr = wp_parse_url( $url ); |
| 173 | $url = str_replace( '/v/', '', $arr['path'] ); |
| 174 | |
| 175 | return $url; |
| 176 | |
| 177 | } elseif ( preg_match( '/http(s)?:\/\/(\w+.)?youtube.com\/embed/', $url, $matches ) ) { |
| 178 | $arr = wp_parse_url( $url ); |
| 179 | $url = str_replace( '/embed/', '', $arr['path'] ); |
| 180 | |
| 181 | return $url; |
| 182 | |
| 183 | } elseif ( preg_match( "#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#", $url, $matches ) ) { |
| 184 | return $matches[0]; |
| 185 | |
| 186 | } else { |
| 187 | return false; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | } |
| 193 |