Diff: STRATO-apps/wordpress_03/app/wp-content/themes/blocksy/inc/components/media/video.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + if (! function_exists('blocksy_get_video_data')) {
4 + function blocksy_get_video_data($attachment_id) {
5 + $maybe_video = [];
6 +
7 + if (! $attachment_id) {
8 + return $maybe_video;
9 + }
10 +
11 + $maybe_new_video = blocksy_get_post_options($attachment_id);
12 +
13 + if ($maybe_new_video) {
14 + $source = blocksy_akg('media_video_source', $maybe_new_video, 'upload');
15 + $maybe_video = $maybe_new_video;
16 +
17 + if ($source === 'upload') {
18 + $maybeVideoIdOrUrl = blocksy_akg('media_video_upload', $maybe_video, '');
19 +
20 + if (empty($maybeVideoIdOrUrl)) {
21 + return [];
22 + }
23 +
24 + if (is_numeric($maybeVideoIdOrUrl)) {
25 + $video_url = wp_get_attachment_url(
26 + $maybeVideoIdOrUrl
27 + );
28 +
29 + if (! empty($video_url)) {
30 + $maybe_video['url'] = $video_url;
31 + }
32 + } else {
33 + $maybe_video['url'] = $maybeVideoIdOrUrl;
34 + }
35 + }
36 +
37 + if ($source === 'youtube') {
38 + $video_url = blocksy_akg(
39 + 'media_video_youtube_url',
40 + $maybe_new_video,
41 + ''
42 + );
43 +
44 + if ( ! empty($video_url) ) {
45 + if (
46 + strpos($video_url, 'youtube') !== false
47 + ||
48 + strpos($video_url, 'youtu.be') !== false
49 + ) {
50 + $maybe_video['url'] = $video_url;
51 + }
52 + }
53 + }
54 +
55 + if ($source === 'vimeo') {
56 + $video_url = blocksy_akg(
57 + 'media_video_vimeo_url',
58 + $maybe_new_video,
59 + ''
60 + );
61 +
62 + if ( ! empty($video_url) ) {
63 + if (
64 + strpos($video_url, 'vimeo') !== false
65 + ) {
66 + $maybe_video['url'] = $video_url;
67 + }
68 + }
69 + }
70 +
71 + return $maybe_video;
72 + }
73 +
74 + $maybe_old_video = get_post_meta(
75 + $attachment_id,
76 + 'blocksy_media_video',
77 + true
78 + );
79 +
80 + if (! empty($maybe_old_video)) {
81 + $maybe_video = [
82 + 'url' => $maybe_old_video
83 + ];
84 + }
85 +
86 + return $maybe_video;
87 + }
88 + }
89 +
90 + if (! function_exists('blocksy_has_video_element')) {
91 + function blocksy_has_video_element($args) {
92 + $parser = new Blocksy_Attributes_Parser();
93 +
94 + $play_icon = '<span class="ct-video-indicator">
95 + <svg width="40" height="40" viewBox="0 0 40 40" fill="#fff">
96 + <path class="ct-play-path" d="M20,0C8.9,0,0,8.9,0,20s8.9,20,20,20s20-9,20-20S31,0,20,0z M16,29V11l12,9L16,29z"/>
97 +
98 + <path class="ct-pause-path" d="M20 0C8.9 0 0 8.9 0 20s8.9 20 20 20 20-9 20-20S31 0 20 0zm-2.3 28h-4.6V12h4.6v16zm9.2 0h-4.6V12h4.6v16z"/>
99 +
100 + <path class="ct-video-loader" fill="currentColor" opacity="0.2" d="M20,11c-5,0-9,4-9,9c0,5,4,9,9,9s9-4,9-9C29,15,25,11,20,11z M20,27c-3.9,0-7-3.1-7-7c0-3.9,3.1-7,7-7s7,3.1,7,7C27,23.9,23.9,27,20,27z"/>
101 +
102 + <path class="ct-video-loader" fill="currentColor" d="M23.5,13.9l1-1.7C23.1,11.4,21.6,11,20,11v2C21.3,13,22.5,13.3,23.5,13.9z">
103 + <animateTransform attributeName="transform" type="rotate" from="0 20 20" to="360 20 20" dur="0.6s" repeatCount="indefinite"/>
104 + </path>
105 + </svg>
106 + </span>';
107 +
108 + if (! $args['display_video']) {
109 + return null;
110 + }
111 +
112 + $video_data = blocksy_get_video_data($args['attachment_id']);
113 +
114 + if (
115 + ! isset($video_data['url'])
116 + ||
117 + empty($video_data['url'])
118 + ) {
119 + return null;
120 + }
121 +
122 + return array_merge(
123 + $video_data,
124 + [
125 + 'icon' => $play_icon,
126 + ]
127 + );
128 + }
129 + }
130 +
131 + add_action(
132 + 'wp_ajax_blocksy_get_image_video_component',
133 + 'blocksy_get_via_request_image_video_component'
134 + );
135 +
136 + add_action(
137 + 'wp_ajax_nopriv_blocksy_get_image_video_component',
138 + 'blocksy_get_via_request_image_video_component'
139 + );
140 + function blocksy_get_via_request_image_video_component() {
141 + if (! isset($_GET['media'])) {
142 + wp_send_json_error();
143 + }
144 +
145 + $args = [];
146 +
147 + if (isset($_GET['ignore_video_options'])) {
148 + $args['ignore_video_options'] = true;
149 + }
150 +
151 + $result = blocksy_get_image_video_component($_GET['media'], $args);
152 +
153 + if (empty($result)) {
154 + wp_send_json_error();
155 + }
156 +
157 + $video_data = blocksy_get_video_data($_GET['media']);
158 +
159 + $wrapper_attr = [
160 + 'class' => 'ct-video-container',
161 +
162 + 'data-video-source' => blocksy_akg(
163 + 'media_video_source',
164 + $video_data,
165 + 'upload'
166 + ),
167 +
168 + 'data-video-size' => 'contain'
169 + ];
170 +
171 + if (blocksy_akg('media_video_player', $video_data, 'no') === 'yes') {
172 + $wrapper_attr['data-video-size'] = blocksy_akg(
173 + 'media_video_size',
174 + $video_data,
175 + 'contain'
176 + );
177 + }
178 +
179 + if (
180 + isset($video_data['url'])
181 + &&
182 + ! empty($video_data['url'])
183 + &&
184 + strpos($video_data['url'], 'youtube.com/shorts/') !== false
185 + ) {
186 + $wrapper_attr['data-video-source'] = 'youtube-shorts';
187 + }
188 +
189 + wp_send_json_success([
190 + 'html' => blocksy_html_tag(
191 + 'div',
192 + $wrapper_attr,
193 + $result
194 + )
195 + ]);
196 +
197 + return;
198 + }
199 +
200 + function blocksy_get_image_video_component($media_id, $args = []) {
201 + $args = wp_parse_args(
202 + $args,
203 + [
204 + 'ignore_video_options' => false,
205 + ]
206 + );
207 +
208 + if (! isset($media_id)) {
209 + return '';
210 + }
211 +
212 + $video_data = blocksy_get_video_data($media_id);
213 +
214 + if (
215 + ! isset($video_data['url'])
216 + ||
217 + empty($video_data['url'])
218 + ) {
219 + return '';
220 + }
221 +
222 + $use_simple_player = blocksy_akg('media_video_player', $video_data, 'no') === 'yes';
223 +
224 + $new_default_based_on_old_value = blocksy_akg(
225 + 'media_video_autoplay',
226 + $video_data,
227 + 'no'
228 + ) === 'yes' ? 'autoplay' : 'click';
229 +
230 + $play_event = blocksy_akg(
231 + 'media_video_event',
232 + $video_data,
233 + $new_default_based_on_old_value
234 + );
235 +
236 + $use_autoplay = $play_event === 'autoplay' || $play_event === 'hover';
237 +
238 + if ($args['ignore_video_options']) {
239 + $use_autoplay = false;
240 + $use_simple_player = false;
241 + }
242 +
243 + preg_match(
244 + '#^(http|https)://.+\.(mp4|MP4|mpeg4|mov)(?=\?|$)#i',
245 + $video_data['url'],
246 + $matches
247 + );
248 +
249 + $media_video_loop = blocksy_akg('media_video_loop', $video_data, 'no');
250 +
251 + if (isset($matches[0]) && ! empty($matches[0])) {
252 + $poster = wp_get_attachment_url($media_id);
253 +
254 + $video_attr = [
255 + 'poster' => $poster,
256 + 'controls' => !$use_simple_player,
257 + 'playsinline' => 1
258 + ];
259 +
260 + $video_attr['autoplay'] = $use_autoplay;
261 + $video_attr['muted'] = $use_autoplay;
262 + $video_attr['loop'] = $media_video_loop === 'yes' ? 1 : 0;
263 +
264 + $result = blocksy_html_tag(
265 + 'video',
266 + $video_attr,
267 + blocksy_html_tag(
268 + 'source',
269 + [
270 + 'src' => $video_data['url'],
271 + 'type' => 'video/mp4',
272 + ],
273 + false
274 + )
275 + );
276 +
277 + return $result;
278 + }
279 +
280 + $embed = wp_oembed_get($video_data['url']);
281 +
282 + $additional_parmas = [
283 + 'autoplay' => $use_autoplay ? 1 : 0,
284 + 'controls' => ! $use_simple_player ? 1 : 0,
285 + 'loop' => $media_video_loop === 'yes' ? 1 : 0,
286 + 'playsinline' => 1,
287 + ];
288 +
289 + if (
290 + strpos($video_data['url'], 'youtube') !== false
291 + ||
292 + strpos($video_data['url'], 'youtu.be') !== false
293 + ) {
294 + if ($media_video_loop === 'yes') {
295 + $id = explode('?', $video_data['url'])[1];
296 + $id = str_replace('v=', '', array_reverse(explode('/', $id))[0]);
297 +
298 + if (strpos($video_data['url'], 'youtube.com/shorts/') === false) {
299 + $additional_parmas['playlist'] = $id;
300 + }
301 + }
302 +
303 + $additional_parmas = array_merge($additional_parmas, [
304 + 'enablejsapi' => 1,
305 + 'version' => 3,
306 + 'playerapiid' => 'ytplayer',
307 + 'mute' => $use_autoplay ? 1 : 0
308 + ]);
309 + }
310 +
311 + if (strpos($video_data['url'], 'vimeo') !== false) {
312 + $additional_parmas = array_merge(
313 + $additional_parmas,
314 + [
315 + 'autopause' => 0,
316 + 'background' => $use_simple_player ? 1 : 0,
317 + 'muted' => $use_autoplay ? 1 : 0,
318 + 'transparent' => 0
319 + ]
320 + );
321 + }
322 +
323 + $src_param = [];
324 +
325 + preg_match('/src=["](.*?)["]/', $embed, $src_param);
326 +
327 + if (isset($src_param[1]) && ! empty($src_param[1])) {
328 + $embed = str_replace(
329 + $src_param[1],
330 + add_query_arg(
331 + $additional_parmas,
332 + $src_param[1]
333 + ),
334 + $embed
335 + );
336 + }
337 +
338 + return $embed;
339 + }
340 +
341 +