From 1a19def5d7e551687d36d074215604d4eb5e6b22 Mon Sep 17 00:00:00 2001 From: "H. C. Kruse" Date: Sat, 11 Nov 2023 10:40:58 +0100 Subject: [PATCH] fix: Fix passing css class to local embeds Fixes #74 --- EmbedVideo.i18n.magic.php | 1 + includes/EmbedService/EmbedHtmlFormatter.php | 4 ++++ includes/Media/AudioHandler.php | 6 ++++-- includes/Media/TransformOutput/AudioTransformOutput.php | 2 +- .../Media/TransformOutput/VideoEmbedTransformOutput.php | 1 + includes/Media/TransformOutput/VideoTransformOutput.php | 2 +- includes/Media/VideoHandler.php | 2 +- 7 files changed, 13 insertions(+), 5 deletions(-) diff --git a/EmbedVideo.i18n.magic.php b/EmbedVideo.i18n.magic.php index 9f6629c..0cdead9 100644 --- a/EmbedVideo.i18n.magic.php +++ b/EmbedVideo.i18n.magic.php @@ -29,4 +29,5 @@ 'autoresize' => [ 0, 'autoresize' ], 'title' => [ 0, 'title=$1' ], 'description' => [ 0, 'description=$1' ], + 'class' => [ 0, 'class=$1' ], ]; diff --git a/includes/EmbedService/EmbedHtmlFormatter.php b/includes/EmbedService/EmbedHtmlFormatter.php index 764a4f7..6bb9e4d 100644 --- a/includes/EmbedService/EmbedHtmlFormatter.php +++ b/includes/EmbedService/EmbedHtmlFormatter.php @@ -49,6 +49,10 @@ public static function toHtml( AbstractEmbedService $service, array $config = [] $config ); + if ( !empty( $config['img-class'] ) ) { + $config['class'] .= ' ' . $config['img-class']; + } + $inlineStyles = [ 'container' => $config['style'] ?? '', 'wrapper' => '', diff --git a/includes/Media/AudioHandler.php b/includes/Media/AudioHandler.php index 0ea7134..976dee8 100644 --- a/includes/Media/AudioHandler.php +++ b/includes/Media/AudioHandler.php @@ -42,6 +42,7 @@ public function getParamMap(): array { 'loop' => 'loop', 'nocontrols' => 'nocontrols', 'muted' => 'muted', + 'class' => 'class', ]; } @@ -63,7 +64,7 @@ public function validateParam( $name, $value ): bool { return $this->parseTimeString( $value ) !== false; } - if ( $name === 'autoplay' || $name === 'loop' || $name === 'nocontrols' ) { + if ( in_array( $name, [ 'autoplay', 'loop', 'nocontrols', 'class' ], true ) ) { return true; } @@ -124,7 +125,7 @@ public function parseParamString( $string ): array { * Returns false if the parameters are unacceptable and the transform should fail * * @param stdClass|File $image - * @param array $params + * @param array &$params * @return bool Success */ public function normaliseParams( $image, &$params ): bool { @@ -151,6 +152,7 @@ public function normaliseParams( $image, &$params ): bool { } $params['page'] = 1; + $params['img-class'] = $params['img-class'] ?? $params['class'] ?? null; return true; } diff --git a/includes/Media/TransformOutput/AudioTransformOutput.php b/includes/Media/TransformOutput/AudioTransformOutput.php index 9632260..769c366 100644 --- a/includes/Media/TransformOutput/AudioTransformOutput.php +++ b/includes/Media/TransformOutput/AudioTransformOutput.php @@ -52,7 +52,7 @@ public function toHtml( $options = [] ): string { return Html::rawElement( 'audio', [ 'src' => $this->getSrc(), 'width' => $this->getWidth(), - 'class' => $options['img-class'] ?? false, + 'class' => $options['img-class'] ?? $this->parameters['img-class'] ?? false, 'style' => $this->getStyle( $options ), 'controls' => !isset( $this->parameters['nocontrols'] ), 'autoplay' => isset( $this->parameters['autoplay'] ), diff --git a/includes/Media/TransformOutput/VideoEmbedTransformOutput.php b/includes/Media/TransformOutput/VideoEmbedTransformOutput.php index 1154084..9c3c30b 100644 --- a/includes/Media/TransformOutput/VideoEmbedTransformOutput.php +++ b/includes/Media/TransformOutput/VideoEmbedTransformOutput.php @@ -24,6 +24,7 @@ public function toHtml( $options = [] ): string { 'withConsent' => true, 'autoresize' => ( $this->parameters['autoresize'] ?? false ) === true, 'description' => $this->parameters['description'] ?? null, + 'img-class' => $this->parameters['img-class'] ?? null, ] ); } } diff --git a/includes/Media/TransformOutput/VideoTransformOutput.php b/includes/Media/TransformOutput/VideoTransformOutput.php index 2c01ffb..d2ed9db 100644 --- a/includes/Media/TransformOutput/VideoTransformOutput.php +++ b/includes/Media/TransformOutput/VideoTransformOutput.php @@ -48,7 +48,7 @@ public function toHtml( $options = [] ): string { 'src' => $this->getSrc(), 'width' => $this->getWidth(), 'height' => $this->getHeight(), - 'class' => $options['img-class'] ?? false, + 'class' => $options['img-class'] ?? $this->parameters['img-class'] ?? false, 'style' => $this->getStyle( $options ), 'poster' => $this->parameters['posterUrl'] ?? false, 'controls' => !isset( $this->parameters['nocontrols'] ), diff --git a/includes/Media/VideoHandler.php b/includes/Media/VideoHandler.php index e465c1b..bf227fe 100644 --- a/includes/Media/VideoHandler.php +++ b/includes/Media/VideoHandler.php @@ -45,7 +45,7 @@ public function validateParam( $name, $value ): bool { return $value > 0; } - if ( in_array( $name, [ 'poster', 'gif', 'muted', 'title', 'description', 'lazy', 'autoresize' ] ) ) { + if ( in_array( $name, [ 'poster', 'gif', 'muted', 'title', 'description', 'lazy', 'autoresize' ], true ) ) { return true; }