VideoPress_Shortcode

VideoPress Shortcode Handler.

Defined (1)

The class is defined in the following location(s).

/modules/videopress/shortcode.php  
  1. class VideoPress_Shortcode { 
  2. /** @var VideoPress_Shortcode */ 
  3. protected static $instance; 
  4.  
  5. protected function __construct() { 
  6.  
  7. // By explicitly declaring the provider here, we can speed things up by not relying on oEmbed discovery. 
  8. wp_oembed_add_provider( '#^https?://videopress.com/v/.*#', 'http://public-api.wordpress.com/oembed/1.0/', true ); 
  9.  
  10. add_shortcode( 'videopress', array( $this, 'shortcode_callback' ) ); 
  11. add_shortcode( 'wpvideo', array( $this, 'shortcode_callback' ) ); 
  12.  
  13. add_filter('wp_video_shortcode_override', array( $this, 'video_shortcode_override' ), 10, 4); 
  14.  
  15. add_filter( 'oembed_fetch_url', array( $this, 'add_oembed_for_parameter' ) ); 
  16.  
  17. $this->add_video_embed_hander(); 
  18.  
  19. /** 
  20. * @return VideoPress_Shortcode 
  21. */ 
  22. public static function initialize() { 
  23. if ( ! isset ( self::$instance ) ) { 
  24. self::$instance = new self(); 
  25.  
  26. return self::$instance; 
  27.  
  28. /** 
  29. * Translate a 'videopress' or 'wpvideo' shortcode and arguments into a video player display. 
  30. * Expected input formats: 
  31. * [videopress OcobLTqC] 
  32. * [wpvideo OcobLTqC] 
  33. * @link http://codex.wordpress.org/Shortcode_API Shortcode API 
  34. * @param array $attr shortcode attributes 
  35. * @return string HTML markup or blank string on fail 
  36. */ 
  37. public function shortcode_callback( $attr ) { 
  38. global $content_width; 
  39.  
  40. /** 
  41. * We only accept GUIDs as a first unnamed argument. 
  42. */ 
  43. $guid = isset( $attr[0] ) ? $attr[0] : null; 
  44.  
  45. if ( isset( $attr['postid'] ) ) { 
  46. $guid = get_post_meta( $attr['postid'], 'videopress_guid', true ); 
  47.  
  48. /** 
  49. * Make sure the GUID passed in matches how actual GUIDs are formatted. 
  50. */ 
  51. if ( ! videopress_is_valid_guid( $guid ) ) { 
  52. return ''; 
  53.  
  54. /** 
  55. * Set the defaults 
  56. */ 
  57. $defaults = array( 
  58. 'w' => 0, // Width of the video player, in pixels 
  59. 'at' => 0, // How many seconds in to initially seek to 
  60. 'hd' => true, // Whether to display a high definition version 
  61. 'loop' => false, // Whether to loop the video repeatedly 
  62. 'freedom' => false, // Whether to use only free/libre codecs 
  63. 'autoplay' => false, // Whether to autoplay the video on load 
  64. 'permalink' => true, // Whether to display the permalink to the video 
  65. 'flashonly' => false, // Whether to support the Flash player exclusively 
  66. 'defaultlangcode' => false, // Default language code 
  67. ); 
  68.  
  69. $attr = shortcode_atts( $defaults, $attr, 'videopress' ); 
  70.  
  71. /** 
  72. * Cast the attributes, post-input. 
  73. */ 
  74. $attr['width'] = absint( $attr['w'] ); 
  75. $attr['hd'] = (bool) $attr['hd']; 
  76. $attr['freedom'] = (bool) $attr['freedom']; 
  77.  
  78. /** 
  79. * If the provided width is less than the minimum allowed 
  80. * width, or greater than `$content_width` ignore. 
  81. */ 
  82. if ( $attr['width'] < VIDEOPRESS_MIN_WIDTH ) { 
  83. $attr['width'] = 0; 
  84. } elseif ( isset( $content_width ) && $content_width > VIDEOPRESS_MIN_WIDTH && $attr['width'] > $content_width ) { 
  85. $attr['width'] = 0; 
  86.  
  87. /** 
  88. * If there was an invalid or unspecified width, set the width equal to the theme's `$content_width`. 
  89. */ 
  90. if ( 0 === $attr['width'] && isset( $content_width ) && $content_width >= VIDEOPRESS_MIN_WIDTH ) { 
  91. $attr['width'] = $content_width; 
  92.  
  93. /** 
  94. * If the width isn't an even number, reduce it by one (making it even). 
  95. */ 
  96. if ( 1 === ( $attr['width'] % 2 ) ) { 
  97. $attr['width'] --; 
  98.  
  99. /** 
  100. * Filter the default VideoPress shortcode options. 
  101. * @module videopress 
  102. * @since 2.5.0 
  103. * @param array $args Array of VideoPress shortcode options. 
  104. */ 
  105. $options = apply_filters( 'videopress_shortcode_options', array( 
  106. 'at' => (int) $attr['at'],  
  107. 'hd' => $attr['hd'],  
  108. 'loop' => $attr['autoplay'] || $attr['loop'],  
  109. 'freedom' => $attr['freedom'],  
  110. 'autoplay' => $attr['autoplay'],  
  111. 'permalink' => $attr['permalink'],  
  112. 'force_flash' => (bool) $attr['flashonly'],  
  113. 'defaultlangcode' => $attr['defaultlangcode'],  
  114. 'forcestatic' => false, // This used to be a displayed option, but now is only 
  115. // accessible via the `videopress_shortcode_options` filter. 
  116. ) ); 
  117.  
  118. // Register VideoPress scripts 
  119. wp_register_script( 'videopress', 'https://v0.wordpress.com/js/videopress.js', array( 'jquery', 'swfobject' ), '1.09' ); 
  120.  
  121. require_once( dirname( __FILE__ ) . '/class.videopress-video.php' ); 
  122. require_once( dirname( __FILE__ ) . '/class.videopress-player.php' ); 
  123.  
  124. $player = new VideoPress_Player( $guid, $attr['width'], $options ); 
  125.  
  126. if ( is_feed() ) { 
  127. return $player->asXML(); 
  128. } else { 
  129. return $player->asHTML(); 
  130.  
  131. /** 
  132. * Override the standard video short tag to also process videopress files as well. 
  133. * This will, parse the src given, and if it is a videopress file, it will parse as the 
  134. * VideoPress shortcode instead. 
  135. * @param string $html Empty variable to be replaced with shortcode markup. 
  136. * @param array $attr Attributes of the video shortcode. 
  137. * @param string $content Video shortcode content. 
  138. * @param int $instance Unique numeric ID of this video shortcode instance. 
  139. * @return string 
  140. */ 
  141. public function video_shortcode_override($html, $attr, $content, $instance) { 
  142.  
  143. $videopress_guid = null; 
  144.  
  145. if ( isset( $attr['videopress_guid'] ) ) { 
  146. $videopress_guid = $attr['videopress_guid']; 
  147.  
  148. } else { 
  149. // Handle the different possible url attributes 
  150. $url_keys = array( 'src', 'mp4' ); 
  151.  
  152. foreach ( $url_keys as $key ) { 
  153. if ( isset ( $attr[ $key ] ) ) { 
  154. $url = $attr[ $key ]; 
  155.  
  156. if ( preg_match( '@videos.(videopress\.com|files\.wordpress\.com)/([a-z0-9]{8})/@i', $url, $matches ) ) { 
  157. $videopress_guid = $matches[2]; 
  158.  
  159. break; 
  160.  
  161. if ( $videopress_guid ) { 
  162. $videopress_attr = array( $videopress_guid ); 
  163. if ( isset( $attr['width'] ) ) { 
  164. $videopress_attr['w'] = (int) $attr['width']; 
  165. if ( isset( $attr['autoplay'] ) ) { 
  166. $videopress_attr['autoplay'] = $attr['autoplay']; 
  167. if ( isset( $attr['loop'] ) ) { 
  168. $videopress_attr['loop'] = $attr['loop']; 
  169.  
  170. // Then display the VideoPress version of the stored GUID! 
  171. return $this->shortcode_callback( $videopress_attr ); 
  172.  
  173. return ''; 
  174.  
  175. /** 
  176. * Adds a `for` query parameter to the oembed provider request URL. 
  177. * @param String $oembed_provider 
  178. * @return String $ehnanced_oembed_provider 
  179. */ 
  180. public function add_oembed_for_parameter( $oembed_provider ) { 
  181. if ( false === stripos( $oembed_provider, 'videopress.com' ) ) { 
  182. return $oembed_provider; 
  183. return add_query_arg( 'for', parse_url( home_url(), PHP_URL_HOST ), $oembed_provider ); 
  184.  
  185. /** 
  186. * Register a VideoPress handler for direct links to .mov files (and potential other non-handled types later). 
  187. */ 
  188. public function add_video_embed_hander() { 
  189. // These are the video extensions that VideoPress can transcode and considers video as well (even if core does not). 
  190. $extensions = array( 'mov' ); 
  191. $override_extensions = implode( '|', $extensions ); 
  192.  
  193. $regex = "#^https?://videos.(videopress.com|files.wordpress.com)/.+?.($override_extensions)$#i"; 
  194.  
  195. /** This filter is already documented in core/wp-includes/embed.php */ 
  196. $filter = apply_filters( 'wp_video_embed_handler', 'wp_embed_handler_video' ); 
  197. wp_embed_register_handler( 'video', $regex, $filter, 10 );