PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 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 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-videopress / src / class-block-editor-content.php

class-block-editor-content.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at jetpack_vendor/automattic/jetpack-videopress/src/class-block-editor-content.php

255 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * VideoPress Block Editor Content
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8 namespace Automattic\Jetpack\VideoPress;
9
10 use WP_Post;
11
12 /**
13 * VideoPress block editor class for content generation
14 */
15 class Block_Editor_Content {
16 /**
17 * Initializer
18 *
19 * This method should be called only once by the Initializer class. Do not call this method again.
20 */
21 public static function init() {
22 if ( Status::is_standalone_plugin_active() ) {
23 // Remove the videopress shortcodes added by the Jetpack plugin.
24 if ( shortcode_exists( 'videopress' ) ) {
25 remove_shortcode( 'videopress' );
26 }
27 if ( shortcode_exists( 'wpvideo' ) ) {
28 remove_shortcode( 'wpvideo' );
29 }
30
31 add_shortcode( 'videopress', array( static::class, 'videopress_embed_shortcode' ) );
32 add_shortcode( 'wpvideo', array( static::class, 'videopress_embed_shortcode' ) );
33
34 add_filter( 'wp_video_shortcode_override', array( static::class, 'video_shortcode_override' ), 10, 4 );
35 }
36
37 add_filter( 'default_content', array( static::class, 'videopress_video_block_by_guid' ), 10, 2 );
38 }
39
40 /**
41 * VideoPress embed shortcode
42 *
43 * Expected input format:
44 * [videopress tLvEwHYZ]
45 *
46 * @param array $atts Shortcode attributes.
47 *
48 * @return string html
49 */
50 public static function videopress_embed_shortcode( $atts ) {
51 /**
52 * We only accept GUIDs as a first unnamed argument.
53 */
54 $guid = $atts[0] ?? null;
55
56 /**
57 * Make sure the GUID passed in matches how actual GUIDs are formatted.
58 */
59 if ( ! videopress_is_valid_guid( $guid ) ) {
60 return '<!-- error: missing or invalid VideoPress video ID -->';
61 }
62
63 /**
64 * Set the defaults
65 */
66 $defaults = array(
67 'w' => 640, // Width of the video player, in pixels
68 'h' => 0, // Height of the video player, in pixels
69 'at' => 0, // How many seconds in to initially seek to
70 'loop' => false, // Whether to loop the video repeatedly
71 'autoplay' => false, // Whether to autoplay the video on load
72 'cover' => true, // Whether to scale the video to its container
73 'muted' => false, // Whether the video should start without sound
74 'controls' => true, // Whether the video should display controls
75 'playsinline' => false, // Whether the video should be allowed to play inline (for browsers that support this)
76 'useaveragecolor' => false, // Whether the video should use the seekbar automatic average color
77 'preloadcontent' => 'metadata',
78 );
79
80 // Make sure "false" will be actually false.
81 foreach ( $atts as $key => $value ) {
82 if ( is_string( $value ) && 'false' === strtolower( $value ) ) {
83 $atts[ $key ] = 0;
84 }
85 }
86
87 if ( isset( $atts['preload'] ) && videopress_is_valid_preload( $atts['preload'] ) ) {
88 $atts['preloadcontent'] = $atts['preload'];
89 }
90
91 if ( isset( $atts['preloadcontent'] ) && ! videopress_is_valid_preload( $atts['preloadcontent'] ) ) {
92 unset( $atts['preloadcontent'] );
93 }
94
95 // The site-wide opt-out wins over the shortcode's own preload attribute.
96 if ( Data::get_videopress_player_preload_disabled() ) {
97 $atts['preloadcontent'] = 'none';
98 }
99
100 $atts = shortcode_atts( $defaults, $atts, 'videopress' );
101
102 $base_url = 'https://videopress.com/embed/' . $guid;
103 $query_params = array(
104 'at' => $atts['at'],
105 'loop' => $atts['loop'],
106 'autoplay' => $atts['autoplay'],
107 'muted' => $atts['muted'],
108 'controls' => $atts['controls'],
109 'playsinline' => $atts['playsinline'],
110 'useAverageColor' => $atts['useaveragecolor'], // The casing is intentional, shortcode params are lowercase, but player expects useAverageColor
111 'preloadContent' => $atts['preloadcontent'], // The casing is intentional, shortcode params are lowercase, but player expects preloadContent
112 );
113 $src = esc_url( add_query_arg( $query_params, $base_url ) );
114
115 $width = absint( $atts['w'] );
116 if ( ! $atts['h'] ) {
117 $aspect_ratio = 16 / 9; // TODO: Get the correct aspect ratio for the video.
118 $height = $width / $aspect_ratio;
119 } else {
120 $height = absint( $atts['h'] );
121 }
122
123 $cover = $atts['cover'] ? ' data-resize-to-parent="true"' : '';
124
125 $block_template =
126 '<figure class="wp-block-videopress-video wp-block-jetpack-videopress jetpack-videopress-player">' .
127 '<div class="jetpack-videopress-player__wrapper">' .
128 '<iframe ' .
129 'title="' . __( 'VideoPress Video Player', 'jetpack-videopress-pkg' ) . '" ' .
130 'aria-label="' . __( 'VideoPress Video Player', 'jetpack-videopress-pkg' ) . '" ' .
131 'src="%s" ' .
132 'width="%s" ' .
133 'height="%s" ' .
134 'frameborder="0" ' .
135 'allowfullscreen%s allow="clipboard-write; presentation">' .
136 '</iframe>' .
137 '</div>' .
138 '</figure>';
139
140 $version = Package_Version::PACKAGE_VERSION;
141 Jwt_Token_Bridge::enqueue_jwt_token_bridge();
142 wp_enqueue_script( 'videopress-iframe', 'https://videopress.com/videopress-iframe.js', array(), $version, true );
143
144 return sprintf( $block_template, $src, $width, $height, $cover );
145 }
146
147 /**
148 * Generates a VideoPress video block content with the given guid
149 *
150 * @param string $content Post content.
151 * @param WP_Post $post Post.
152 * @return string
153 */
154 public static function videopress_video_block_by_guid( $content, $post ) {
155 if ( isset( $_GET['videopress_guid'] ) && isset( $_GET['_wpnonce'] )
156 && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'videopress-content-nonce' )
157 && current_user_can( 'edit_post', $post->ID )
158 && '' === $content
159 ) {
160 $guid = sanitize_text_field( wp_unslash( $_GET['videopress_guid'] ) );
161
162 $base_url = 'https://videopress.com/v/' . $guid;
163 $query_params = array(
164 'resizeToParent' => 'true',
165 'cover' => 'true',
166 'preloadContent' => 'metadata',
167 'useAverageColor' => 'true',
168 );
169 $url = esc_url( add_query_arg( $query_params, $base_url ) );
170
171 if ( ! empty( $guid ) ) {
172 // ref /client/lib/url/index.ts
173 $content = '<!-- wp:videopress/video {"guid":"' . $guid . '"} -->
174 <figure class="wp-block-videopress-video wp-block-jetpack-videopress jetpack-videopress-player">
175 <div class="jetpack-videopress-player__wrapper">' . $url . '</div>
176 </figure>
177 <!-- /wp:videopress/video -->';
178 }
179 }
180
181 return $content;
182 }
183
184 /**
185 * Override the standard video short tag to also process videopress files as well.
186 *
187 * This will parse the given src and, if it is a videopress file, parse as the
188 * VideoPress shortcode instead.
189 *
190 * @param string $html Empty variable to be replaced with shortcode markup.
191 * @param array $attr Attributes of the video shortcode.
192 * @param string $content Video shortcode content.
193 * @param int $instance Unique numeric ID of this video shortcode instance.
194 *
195 * @return string
196 */
197 public static function video_shortcode_override( $html, $attr, $content, $instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
198 $videopress_guid = null;
199
200 if ( isset( $attr['videopress_guid'] ) ) {
201 $videopress_guid = $attr['videopress_guid'];
202 } else {
203 // Handle the different possible url attributes
204 $url_keys = array( 'src', 'mp4' );
205
206 foreach ( $url_keys as $key ) {
207 if ( isset( $attr[ $key ] ) ) {
208 $url = $attr[ $key ];
209 // phpcs:ignore WordPress.WP.CapitalPDangit
210 if ( preg_match( '@videos.(videopress\.com|files\.wordpress\.com)/([a-z0-9]{8})/@i', $url, $matches ) ) {
211 $videopress_guid = $matches[2];
212 }
213
214 // Also test for videopress oembed url, which is used by the Video Media Widget.
215 if ( ! $videopress_guid && preg_match( '@https://videopress.com/v/([a-z0-9]{8})@i', $url, $matches ) ) {
216 $videopress_guid = $matches[1];
217 }
218
219 // Also test for old v.wordpress.com oembed URL.
220 if ( ! $videopress_guid && preg_match( '|^https?://v\.wordpress\.com/([a-zA-Z\d]{8})(.+)?$|i', $url, $matches ) ) { // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText
221 $videopress_guid = $matches[1];
222 }
223
224 break;
225 }
226 }
227 }
228
229 if ( $videopress_guid ) {
230 $videopress_atts = array( $videopress_guid );
231
232 // height is ignored on jetpack video block, so we don't pass it for consistency.
233 if ( isset( $attr['width'] ) ) {
234 $videopress_atts['w'] = (int) $attr['width'];
235 }
236 if ( isset( $attr['muted'] ) ) {
237 $videopress_atts['muted'] = $attr['muted'];
238 }
239 if ( isset( $attr['autoplay'] ) ) {
240 $videopress_atts['autoplay'] = $attr['autoplay'];
241 }
242 if ( isset( $attr['loop'] ) ) {
243 $videopress_atts['loop'] = $attr['loop'];
244 }
245 // The core video block doesn't support the cover attribute, setting it to false for consistency.
246 $videopress_atts['cover'] = false;
247
248 // Then display the VideoPress version of the stored GUID!
249 return self::videopress_embed_shortcode( $videopress_atts );
250 }
251
252 return '';
253 }
254 }
255