Ei kuvausta

shortcode.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/.*#', 'https://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 https://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(
  107. 'videopress_shortcode_options',
  108. array(
  109. 'at' => (int) $attr['at'],
  110. 'hd' => $attr['hd'],
  111. 'loop' => $attr['loop'],
  112. 'freedom' => $attr['freedom'],
  113. 'autoplay' => $attr['autoplay'],
  114. 'permalink' => $attr['permalink'],
  115. 'force_flash' => (bool) $attr['flashonly'],
  116. 'defaultlangcode' => $attr['defaultlangcode'],
  117. 'forcestatic' => false, // This used to be a displayed option, but now is only
  118. // accessible via the `videopress_shortcode_options` filter.
  119. )
  120. );
  121. // Register VideoPress scripts
  122. wp_register_script( 'videopress', 'https://v0.wordpress.com/js/videopress.js', array( 'jquery', 'swfobject' ), '1.09' );
  123. require_once dirname( __FILE__ ) . '/class.videopress-video.php';
  124. require_once dirname( __FILE__ ) . '/class.videopress-player.php';
  125. $player = new VideoPress_Player( $guid, $attr['width'], $options );
  126. if ( is_feed() ) {
  127. return $player->asXML();
  128. } else {
  129. return $player->asHTML();
  130. }
  131. }
  132. /**
  133. * Override the standard video short tag to also process videopress files as well.
  134. *
  135. * This will, parse the src given, and if it is a videopress file, it will parse as the
  136. * VideoPress shortcode instead.
  137. *
  138. * @param string $html Empty variable to be replaced with shortcode markup.
  139. * @param array $attr Attributes of the video shortcode.
  140. * @param string $content Video shortcode content.
  141. * @param int $instance Unique numeric ID of this video shortcode instance.
  142. *
  143. * @return string
  144. */
  145. public function video_shortcode_override( $html, $attr, $content, $instance ) {
  146. $videopress_guid = null;
  147. if ( isset( $attr['videopress_guid'] ) ) {
  148. $videopress_guid = $attr['videopress_guid'];
  149. } else {
  150. // Handle the different possible url attributes
  151. $url_keys = array( 'src', 'mp4' );
  152. foreach ( $url_keys as $key ) {
  153. if ( isset( $attr[ $key ] ) ) {
  154. $url = $attr[ $key ];
  155. // phpcs:ignore WordPress.WP.CapitalPDangit
  156. if ( preg_match( '@videos.(videopress\.com|files\.wordpress\.com)/([a-z0-9]{8})/@i', $url, $matches ) ) {
  157. $videopress_guid = $matches[2];
  158. }
  159. // Also test for videopress oembed url, which is used by the Video Media Widget.
  160. if ( ! $videopress_guid && preg_match( '@https://videopress.com/v/([a-z0-9]{8})@i', $url, $matches ) ) {
  161. $videopress_guid = $matches[1];
  162. }
  163. break;
  164. }
  165. }
  166. }
  167. if ( $videopress_guid ) {
  168. $videopress_attr = array( $videopress_guid );
  169. if ( isset( $attr['width'] ) ) {
  170. $videopress_attr['w'] = (int) $attr['width'];
  171. }
  172. if ( isset( $attr['autoplay'] ) ) {
  173. $videopress_attr['autoplay'] = $attr['autoplay'];
  174. }
  175. if ( isset( $attr['loop'] ) ) {
  176. $videopress_attr['loop'] = $attr['loop'];
  177. }
  178. // Then display the VideoPress version of the stored GUID!
  179. return $this->shortcode_callback( $videopress_attr );
  180. }
  181. return '';
  182. }
  183. /**
  184. * Adds a `for` query parameter to the oembed provider request URL.
  185. *
  186. * @param String $oembed_provider
  187. * @return String $ehnanced_oembed_provider
  188. */
  189. public function add_oembed_for_parameter( $oembed_provider ) {
  190. if ( false === stripos( $oembed_provider, 'videopress.com' ) ) {
  191. return $oembed_provider;
  192. }
  193. return add_query_arg( 'for', wp_parse_url( home_url(), PHP_URL_HOST ), $oembed_provider );
  194. }
  195. /**
  196. * Register a VideoPress handler for direct links to .mov files (and potential other non-handled types later).
  197. */
  198. public function add_video_embed_hander() {
  199. // These are the video extensions that VideoPress can transcode and considers video as well (even if core does not).
  200. $extensions = array( 'mov' );
  201. $override_extensions = implode( '|', $extensions );
  202. $regex = "#^https?://videos.(videopress.com|files.wordpress.com)/.+?.($override_extensions)$#i";
  203. /** This filter is already documented in core/wp-includes/embed.php */
  204. $filter = apply_filters( 'wp_video_embed_handler', 'wp_embed_handler_video' );
  205. wp_embed_register_handler( 'video', $regex, $filter, 10 );
  206. }
  207. }
  208. VideoPress_Shortcode::initialize();