shortcode.php 7.6 KB

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