Markup.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Helper\Image; |
| 6 | |
| 7 | use AC\Helper\Creatable; |
| 8 | use AC\Helper\Html; |
| 9 | use AC\View; |
| 10 | |
| 11 | class Markup extends Creatable |
| 12 | { |
| 13 | public function image(string $src, int $width, int $height, ?int $media_id = null, string $tooltip = ''): string |
| 14 | { |
| 15 | $class = ''; |
| 16 | |
| 17 | if ($media_id && ! wp_attachment_is_image($media_id)) { |
| 18 | $class = ' ac-icon'; |
| 19 | } |
| 20 | |
| 21 | $image_attributes = [ |
| 22 | 'max-width' => esc_attr((string)$width) . 'px', |
| 23 | 'max-height' => esc_attr((string)$height) . 'px', |
| 24 | ]; |
| 25 | |
| 26 | if (pathinfo($src, PATHINFO_EXTENSION) === 'svg') { |
| 27 | $image_attributes['width'] = esc_attr((string)$width) . 'px'; |
| 28 | $image_attributes['height'] = esc_attr((string)$height) . 'px'; |
| 29 | } |
| 30 | |
| 31 | return (new View([ |
| 32 | 'src' => $src, |
| 33 | 'class' => $class, |
| 34 | 'styles' => Html::create()->get_inline_styles($image_attributes), |
| 35 | 'tooltip_attr' => $tooltip, |
| 36 | 'media_id' => $media_id, |
| 37 | ]))->set_template('column/image')->render(); |
| 38 | } |
| 39 | |
| 40 | public function cover(string $src, int $width, int $height, ?int $media_id = null): string |
| 41 | { |
| 42 | return (new View([ |
| 43 | 'src' => $src, |
| 44 | 'width' => $width, |
| 45 | 'height' => $height, |
| 46 | 'media_id' => $media_id, |
| 47 | ]))->set_template('column/image-cover')->render(); |
| 48 | } |
| 49 | |
| 50 | public function file_pill(int $media_id, string $filename): string |
| 51 | { |
| 52 | $extension = (string)pathinfo($filename, PATHINFO_EXTENSION); |
| 53 | |
| 54 | return sprintf( |
| 55 | '<span class="ac-file-pill" data-media-id="%s"><span class="ac-file-type">%s</span><span class="ac-file-name">%s</span></span>', |
| 56 | esc_attr((string)$media_id), |
| 57 | esc_html(strtoupper($extension)), |
| 58 | esc_html($filename) |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | } |
| 63 |