PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 All 502 releases
jetpack / modules / videopress / shortcode.php

shortcode.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at modules/videopress/shortcode.php

283 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit( 0 );
5 }
6
7 use Automattic\Jetpack\VideoPress\Data as VideoPress_Data;
8
9 /**
10 * VideoPress Shortcode Handler
11 *
12 * This file may or may not be included from the Jetpack VideoPress module.
13 */
14 class VideoPress_Shortcode {
15 /**
16 * Singleton VideoPress_Shortcode instance.
17 *
18 * @var VideoPress_Shortcode
19 */
20 protected static $instance;
21
22 /**
23 * VideoPress_Shortcode constructor.
24 */
25 protected function __construct() {
26 // Only add the shortcode if it hasn't already been added by the standalone VideoPress plugin.
27 if ( ! shortcode_exists( 'videopress' ) ) {
28 add_shortcode( 'videopress', array( $this, 'shortcode_callback' ) );
29 add_shortcode( 'wpvideo', array( $this, 'shortcode_callback' ) );
30
31 add_filter( 'wp_video_shortcode_override', array( $this, 'video_shortcode_override' ), 10, 4 );
32 }
33
34 $this->add_video_embed_hander();
35 }
36
37 /**
38 * VideoPress_Shortcode initialization.
39 *
40 * @return VideoPress_Shortcode
41 */
42 public static function initialize() {
43 if ( ! isset( self::$instance ) ) {
44 self::$instance = new self();
45 }
46
47 return self::$instance;
48 }
49
50 /**
51 * Translate a 'videopress' or 'wpvideo' shortcode and arguments into a video player display.
52 *
53 * Expected input formats:
54 *
55 * [videopress OcobLTqC]
56 * [wpvideo OcobLTqC]
57 *
58 * @link https://codex.wordpress.org/Shortcode_API Shortcode API
59 * @param array $attr shortcode attributes.
60 * @return string HTML markup or blank string on fail
61 */
62 public function shortcode_callback( $attr ) {
63 global $content_width;
64
65 /**
66 * We only accept GUIDs as a first unnamed argument.
67 */
68 $guid = $attr[0] ?? null;
69
70 if ( isset( $attr['postid'] ) ) {
71 $guid = get_post_meta( $attr['postid'], 'videopress_guid', true );
72 }
73
74 /**
75 * Make sure the GUID passed in matches how actual GUIDs are formatted.
76 */
77 if ( ! videopress_is_valid_guid( $guid ) ) {
78 return '';
79 }
80
81 /**
82 * Set the defaults
83 */
84 $defaults = array(
85 'w' => 0, // Width of the video player, in pixels
86 'at' => 0, // How many seconds in to initially seek to
87 'hd' => true, // Whether to display a high definition version
88 'loop' => false, // Whether to loop the video repeatedly
89 'freedom' => false, // Whether to use only free/libre codecs
90 'autoplay' => false, // Whether to autoplay the video on load
91 'permalink' => true, // Whether to display the permalink to the video
92 'flashonly' => false, // Whether to support the Flash player exclusively
93 'defaultlangcode' => false, // Default language code
94 'cover' => true, // Whether to scale the video to its container.
95 'muted' => false, // Whether the video should start without sound.
96 'controls' => true, // Whether the video should display controls.
97 'playsinline' => false, // Whether the video should be allowed to play inline (for browsers that support this).
98 'useaveragecolor' => false, // Whether the video should use the seekbar automatic average color.
99 'preloadcontent' => 'metadata', // Setting for how the browser should preload the video (none, metadata, auto).
100 );
101
102 // Make sure "false" will be actually false.
103 foreach ( $attr as $key => $value ) {
104 if ( is_string( $value ) && 'false' === strtolower( $value ) ) {
105 $attr[ $key ] = false;
106 }
107 }
108
109 if ( isset( $attr['preload'] ) ) {
110 $attr['preloadcontent'] = $attr['preload'];
111 }
112
113 // The site-wide opt-out wins over the shortcode's own preload attribute.
114 if ( VideoPress_Data::get_videopress_player_preload_disabled() ) {
115 $attr['preloadcontent'] = 'none';
116 }
117
118 $attr = shortcode_atts( $defaults, $attr, 'videopress' );
119
120 /**
121 * Cast the attributes, post-input.
122 */
123 $attr['width'] = absint( $attr['w'] );
124 $attr['hd'] = (bool) $attr['hd'];
125 $attr['freedom'] = (bool) $attr['freedom'];
126
127 /**
128 * If the provided width is less than the minimum allowed
129 * width, or greater than `$content_width` ignore.
130 */
131 if ( $attr['width'] < VIDEOPRESS_MIN_WIDTH ) {
132 $attr['width'] = 0;
133 } elseif ( isset( $content_width ) && $content_width > VIDEOPRESS_MIN_WIDTH && $attr['width'] > $content_width ) {
134 $attr['width'] = 0;
135 }
136
137 /**
138 * If there was an invalid or unspecified width, set the width equal to the theme's `$content_width`.
139 */
140 if ( 0 === $attr['width'] && isset( $content_width ) && $content_width >= VIDEOPRESS_MIN_WIDTH ) {
141 $attr['width'] = (int) $content_width;
142 }
143
144 /**
145 * If the width isn't an even number, reduce it by one (making it even).
146 */
147 if ( 1 === ( $attr['width'] % 2 ) ) {
148 --$attr['width'];
149 }
150
151 /**
152 * Filter the default VideoPress shortcode options.
153 *
154 * @module videopress
155 *
156 * @since 2.5.0
157 *
158 * @param array $args Array of VideoPress shortcode options.
159 */
160 $options = apply_filters(
161 'videopress_shortcode_options',
162 array(
163 'at' => (int) $attr['at'],
164 'hd' => $attr['hd'],
165 'cover' => (bool) $attr['cover'],
166 'loop' => $attr['loop'],
167 'freedom' => $attr['freedom'],
168 'autoplay' => $attr['autoplay'],
169 'permalink' => $attr['permalink'],
170 'force_flash' => (bool) $attr['flashonly'],
171 'defaultlangcode' => $attr['defaultlangcode'],
172 'forcestatic' => false, // This used to be a displayed option, but now is only.
173 'muted' => $attr['muted'],
174 'controls' => $attr['controls'],
175 'playsinline' => $attr['playsinline'],
176 'useAverageColor' => (bool) $attr['useaveragecolor'], // The casing is intentional, shortcode params are lowercase, but player expects useAverageColor
177 'preloadContent' => $attr['preloadcontent'], // The casing is intentional, shortcode params are lowercase, but player expects preloadContent
178 // accessible via the `videopress_shortcode_options` filter.
179 )
180 );
181
182 // Register VideoPress scripts
183 wp_register_script( 'videopress', 'https://v0.wordpress.com/js/videopress.js', array( 'jquery', 'swfobject' ), '1.09', false );
184
185 require_once __DIR__ . '/class.videopress-video.php';
186 require_once __DIR__ . '/class.videopress-player.php';
187
188 $player = new VideoPress_Player( $guid, $attr['width'], $options );
189
190 if ( is_feed() ) {
191 return $player->as_xml();
192 } else {
193 return $player->as_html();
194 }
195 }
196
197 /**
198 * Override the standard video short tag to also process videopress files as well.
199 *
200 * This will, parse the src given, and if it is a videopress file, it will parse as the
201 * VideoPress shortcode instead.
202 *
203 * @param string $html Empty variable to be replaced with shortcode markup.
204 * @param array $attr Attributes of the video shortcode.
205 * @param string $content Video shortcode content.
206 * @param int $instance Unique numeric ID of this video shortcode instance.
207 *
208 * @return string
209 */
210 public function video_shortcode_override( $html, $attr, $content, $instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
211 $videopress_guid = null;
212
213 if ( isset( $attr['videopress_guid'] ) ) {
214 $videopress_guid = $attr['videopress_guid'];
215 } else {
216 // Handle the different possible url attributes
217 $url_keys = array( 'src', 'mp4' );
218
219 foreach ( $url_keys as $key ) {
220 if ( isset( $attr[ $key ] ) ) {
221 $url = $attr[ $key ];
222 // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText
223 if ( preg_match( '@videos.(videopress\.com|files\.wordpress\.com)/([a-z0-9]{8})/@i', $url, $matches ) ) {
224 $videopress_guid = $matches[2];
225 }
226
227 // Also test for videopress oembed url, which is used by the Video Media Widget.
228 if ( ! $videopress_guid && preg_match( '@https://videopress.com/v/([a-z0-9]{8})@i', $url, $matches ) ) {
229 $videopress_guid = $matches[1];
230 }
231
232 // Also test for old v.wordpress.com oembed URL.
233 if ( ! $videopress_guid && preg_match( '|^https?://v\.wordpress\.com/([a-zA-Z\d]{8})(.+)?$|i', $url, $matches ) ) { // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText
234 $videopress_guid = $matches[1];
235 }
236
237 break;
238 }
239 }
240 }
241
242 if ( $videopress_guid ) {
243 $videopress_attr = array( $videopress_guid );
244 if ( isset( $attr['width'] ) ) {
245 $videopress_attr['w'] = (int) $attr['width'];
246 }
247 if ( isset( $attr['muted'] ) ) {
248 $videopress_attr['muted'] = $attr['muted'];
249 }
250 if ( isset( $attr['autoplay'] ) ) {
251 $videopress_attr['autoplay'] = $attr['autoplay'];
252 }
253 if ( isset( $attr['loop'] ) ) {
254 $videopress_attr['loop'] = $attr['loop'];
255 }
256 // The core video block doesn't support the cover attribute, setting it to false for consistency.
257 $videopress_attr['cover'] = false;
258
259 // Then display the VideoPress version of the stored GUID!
260 return $this->shortcode_callback( $videopress_attr );
261 }
262
263 return '';
264 }
265
266 /**
267 * Register a VideoPress handler for direct links to .mov files (and potential other non-handled types later).
268 */
269 public function add_video_embed_hander() {
270 // These are the video extensions that VideoPress can transcode and considers video as well (even if core does not).
271 $extensions = array( 'mov' );
272 $override_extensions = implode( '|', $extensions );
273
274 $regex = "#^https?://videos.(videopress.com|files.wordpress.com)/.+?.($override_extensions)$#i";
275
276 /** This filter is already documented in core/wp-includes/embed.php */
277 $filter = apply_filters( 'wp_video_embed_handler', 'wp_embed_handler_video' );
278 wp_embed_register_handler( 'video', $regex, $filter, 10 );
279 }
280 }
281
282 VideoPress_Shortcode::initialize();
283