$autoplay ? 1 : 0, 'mute' => $muted ? 1 : 0, 'controls' => $controls ? 1 : 0, 'playsinline' => 1, 'rel' => 0, ]; if ($loop) { // YouTube needs the playlist parameter for a single video to loop. $params['loop'] = 1; $params['playlist'] = $id; } return 'https://' . $host . '/embed/' . rawurlencode($id) . '?' . http_build_query($params); } if ('vimeo' === $source) { $id = self::vimeo_id($url); if ('' === $id) { return ''; } $params = [ 'autoplay' => $autoplay ? 1 : 0, 'muted' => $muted ? 1 : 0, 'loop' => $loop ? 1 : 0, 'playsinline' => 1, ]; if (!$controls) { $params['controls'] = 0; } return 'https://player.vimeo.com/video/' . rawurlencode($id) . '?' . http_build_query($params); } return ''; } /** * Build the wrapper class list. * * PHP twin of src/divi5/modules/sticky-video/wrapper-class.js. * * @param array $advanced The `module.advanced` attrs array. * * @return string */ public static function wrapper_class($advanced) { $pick = function ($key, array $allowed, $fallback) use ($advanced) { $value = $advanced[$key]['desktop']['value'] ?? $fallback; return in_array($value, $allowed, true) ? $value : $fallback; }; $ratio = $pick('aspectRatio', self::RATIOS, '16:9'); $classes = [ 'dtq-module', 'dtq-sticky-video', 'dtq-d5', 'dtq-sticky-video--pos-' . $pick('stickPosition', self::POSITIONS, 'bottom-right'), 'dtq-sticky-video--anim-' . $pick('stickAnimation', self::ANIMATIONS, 'slide'), 'dtq-sticky-video--ratio-' . str_replace(':', '-', $ratio), ]; $disable_on = $pick('disableOn', self::DISABLE_ON, 'phone'); if ('none' !== $disable_on) { $classes[] = 'dtq-sticky-video--off-' . $disable_on; } return implode(' ', $classes); } /** * Server-side render for the Sticky Video module. * * @param array $attrs Block attributes. * @param string $content Block content (unused; no children). * @param WP_Block $block Parsed block. * @param ModuleElements $elements Module elements helper. * * @return string */ public static function render_callback($attrs, $content, $block, $elements) { $advanced = $attrs['module']['advanced'] ?? []; $pick = function ($key, array $allowed, $fallback) use ($advanced) { $value = $advanced[$key]['desktop']['value'] ?? $fallback; return in_array($value, $allowed, true) ? $value : $fallback; }; $on = function ($key, $fallback) use ($advanced) { return ($advanced[$key]['desktop']['value'] ?? $fallback) === 'on'; }; $source = $pick('source', self::SOURCES, 'youtube'); // Autoplay only works muted in every current browser, so a muted-off // autoplay would simply not start. Forcing it is kinder than shipping a // setting that silently does nothing. $autoplay = $on('autoplay', 'off'); $muted = $autoplay ? true : ($advanced['muted']['desktop']['value'] ?? 'on') !== 'off'; $media_html = ''; if ('self' === $source) { $file = self::upload_src($advanced['videoFile']['desktop']['value'] ?? ''); if ('' !== $file) { $media_html = sprintf( '', esc_url($file), ($advanced['controls']['desktop']['value'] ?? 'on') !== 'off' ? ' controls' : '', $muted ? ' muted' : '', $on('loop', 'off') ? ' loop' : '' ); } } else { $embed = self::embed_url($source, $advanced['videoUrl']['desktop']['value'] ?? '', [ 'autoplay' => $autoplay, 'muted' => $muted, 'loop' => $on('loop', 'off'), 'controls' => ($advanced['controls']['desktop']['value'] ?? 'on') !== 'off', 'noCookie' => ($advanced['noCookie']['desktop']['value'] ?? 'on') !== 'off', ]); if ('' !== $embed) { // No src yet. The iframe is created by frontend.js when the // visitor presses play — which keeps the video host out of the // page until then, and is what makes the one-time relocation in // frontend.js safe (reparenting an iframe reloads it). $media_html = sprintf( '
', esc_url($embed), esc_attr($advanced['videoTitle']['desktop']['value'] ?? __('Embedded video', 'addons-for-divi')) ); } } if ('' === $media_html) { $children = sprintf( '%2$s