assets.php
1 month ago
color.php
1 month ago
countries.php
1 month ago
index.html
1 month ago
media.php
1 month ago
site.php
1 month ago
sitescripts.php
1 month ago
status.php
1 month ago
vikappointments.php
1 month ago
media.php
114 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * VikAppointments HTML media helper. |
| 16 | * |
| 17 | * @since 1.7.2 |
| 18 | */ |
| 19 | abstract class VAPHtmlMedia |
| 20 | { |
| 21 | /** |
| 22 | * Displays an image tag for the specified media file. |
| 23 | * |
| 24 | * @param string $image Either the image name or a complete URI. |
| 25 | * @param array $attrs An array of attributes to be used. |
| 26 | * - loading The loading mode ("lazy" or default "eager"); |
| 27 | * - alt The default alternate text; |
| 28 | * - title The default title; |
| 29 | * - caption The default caption; |
| 30 | * - small True to display the image thumbnail (false by default); |
| 31 | * - relative True to use a relative path (false by default). |
| 32 | * |
| 33 | * @return string The resulting img tag. |
| 34 | */ |
| 35 | public static function display($image, array $attrs = []) |
| 36 | { |
| 37 | // check if we have a URL |
| 38 | if (strpos($image, '/') === false) |
| 39 | { |
| 40 | // nope, just the image name... |
| 41 | $imageName = $image; |
| 42 | |
| 43 | // Detect the type of image to use. |
| 44 | if (empty($attrs['small'])) |
| 45 | { |
| 46 | // use original image |
| 47 | $image = VAPMEDIA_URI . $imageName; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | // use thumbnail image |
| 52 | $image = VAPMEDIA_SMALL_URI . $imageName; |
| 53 | } |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | // extract base name from URL |
| 58 | $imageName = basename($image); |
| 59 | } |
| 60 | |
| 61 | if (!empty($attrs['relative'])) |
| 62 | { |
| 63 | // remove root from given URL |
| 64 | $image = str_replace(JUri::root(), '', $image); |
| 65 | } |
| 66 | |
| 67 | // inject source image |
| 68 | $attrs['src'] = $image; |
| 69 | |
| 70 | $translator = VAPFactory::getTranslator(); |
| 71 | // translate the specified option |
| 72 | $media = $translator->translate('media', $imageName); |
| 73 | |
| 74 | // check whether the alternate text has been specified |
| 75 | if (!empty($media->alt)) |
| 76 | { |
| 77 | // inject alternate text |
| 78 | $attrs['alt'] = $media->alt; |
| 79 | } |
| 80 | |
| 81 | // check whether the title has been specified |
| 82 | if (!empty($media->title)) |
| 83 | { |
| 84 | // inject media title |
| 85 | $attrs['title'] = $media->title; |
| 86 | } |
| 87 | |
| 88 | // check whether the caption has been specified |
| 89 | if (!empty($media->caption)) |
| 90 | { |
| 91 | // inject caption |
| 92 | $attrs['caption'] = $media->caption; |
| 93 | } |
| 94 | |
| 95 | if (!empty($attrs['caption'])) |
| 96 | { |
| 97 | $attrs['data-caption'] = $attrs['caption']; |
| 98 | } |
| 99 | |
| 100 | // unset attributes that shouldn't be appended into the HTML tag |
| 101 | unset($attrs['small'], $attrs['caption'], $attrs['relative']); |
| 102 | |
| 103 | $attrs_str = ''; |
| 104 | |
| 105 | // build HTML attributes |
| 106 | foreach ($attrs as $k => $v) |
| 107 | { |
| 108 | $attrs_str .= ' ' . $k . '="' . htmlspecialchars($v, ENT_QUOTES, 'UTF-8') . '"'; |
| 109 | } |
| 110 | |
| 111 | return "<img{$attrs_str} />"; |
| 112 | } |
| 113 | } |
| 114 |