Image
2 days ago
Select
2 days ago
Arrays.php
2 days ago
AvailableTranslations.php
2 days ago
Creatable.php
2 days ago
Dashicon.php
2 days ago
Date.php
2 days ago
Html.php
2 days ago
Icon.php
2 days ago
Image.php
2 days ago
LocalFile.php
2 days ago
Mbstring.php
2 days ago
MediaIdResolver.php
2 days ago
Menu.php
2 days ago
Network.php
2 days ago
Post.php
2 days ago
Strings.php
2 days ago
Taxonomy.php
2 days ago
User.php
2 days ago
UserRoles.php
2 days ago
WpDateFormat.php
2 days ago
Html.php
211 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Helper; |
| 6 | |
| 7 | use AC\Type\Link; |
| 8 | use WP_HTML_Tag_Processor; |
| 9 | |
| 10 | class Html extends Creatable |
| 11 | { |
| 12 | public function get_attribute_as_string(string $key, ?string $value = null): string |
| 13 | { |
| 14 | return Strings::create()->is_not_empty($value) |
| 15 | ? sprintf('%s="%s"', $key, esc_attr(trim((string)$value))) |
| 16 | : $key; |
| 17 | } |
| 18 | |
| 19 | public function get_inline_styles(array $attributes): string |
| 20 | { |
| 21 | $style = ''; |
| 22 | |
| 23 | foreach ($attributes as $key => $value) { |
| 24 | $style .= $key . ':' . $value . '; '; |
| 25 | } |
| 26 | |
| 27 | return $style; |
| 28 | } |
| 29 | |
| 30 | public function link(string $url, ?string $label = null, array $attributes = []): string |
| 31 | { |
| 32 | if (! $url) { |
| 33 | return $label ?? ''; |
| 34 | } |
| 35 | |
| 36 | if (null === $label) { |
| 37 | $label = urldecode($url); |
| 38 | } |
| 39 | |
| 40 | if (! $label) { |
| 41 | return ''; |
| 42 | } |
| 43 | |
| 44 | if (! $this->contains_html($label)) { |
| 45 | $label = esc_html($label); |
| 46 | } |
| 47 | |
| 48 | $tooltip = null; |
| 49 | |
| 50 | if (array_key_exists('tooltip', $attributes)) { |
| 51 | $tooltip = (string)$attributes['tooltip']; |
| 52 | |
| 53 | unset($attributes['tooltip']); |
| 54 | } |
| 55 | |
| 56 | $protocols = wp_allowed_protocols(); |
| 57 | $protocols[] = 'skype'; |
| 58 | $protocols[] = 'call'; |
| 59 | |
| 60 | $link = new Link( |
| 61 | $url, |
| 62 | $label, |
| 63 | $this->filter_attributes($attributes), |
| 64 | $protocols |
| 65 | ); |
| 66 | |
| 67 | if (null !== $tooltip) { |
| 68 | $link = $link->with_tooltip($tooltip); |
| 69 | } |
| 70 | |
| 71 | return $link->render(); |
| 72 | } |
| 73 | |
| 74 | public function get_tooltip_attr(string $content): string |
| 75 | { |
| 76 | if (! $content) { |
| 77 | return ''; |
| 78 | } |
| 79 | |
| 80 | return 'data-ac-tip="' . esc_attr($content) . '"'; |
| 81 | } |
| 82 | |
| 83 | public function tooltip(string $label, string $tooltip): string |
| 84 | { |
| 85 | if (Strings::create()->is_not_empty($label) && $tooltip) { |
| 86 | $label = sprintf( |
| 87 | '<span %s>%s</span>', |
| 88 | $this->get_tooltip_attr($tooltip), |
| 89 | $label |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | return $label; |
| 94 | } |
| 95 | |
| 96 | public function codearea(string $string, int $max_chars = 1000): string |
| 97 | { |
| 98 | if (! $string) { |
| 99 | return ''; |
| 100 | } |
| 101 | |
| 102 | $contents = Mbstring::substr( |
| 103 | $string, |
| 104 | 0, |
| 105 | $max_chars |
| 106 | ); |
| 107 | |
| 108 | return '<textarea style="color: #808080; width: 100%; min-height: 60px;" readonly>' . esc_textarea($contents) . '</textarea>'; |
| 109 | } |
| 110 | |
| 111 | private function filter_attributes(array $attributes): array |
| 112 | { |
| 113 | $filtered = []; |
| 114 | |
| 115 | foreach ($attributes as $attribute => $value) { |
| 116 | if ( |
| 117 | in_array($attribute, ['title', 'id', 'class', 'style', 'target', 'rel', 'download'], true) || |
| 118 | 'data-' === substr($attribute, 0, 5)) { |
| 119 | $filtered[$attribute] = $value; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return $filtered; |
| 124 | } |
| 125 | |
| 126 | public function get_links(string $string): ?array |
| 127 | { |
| 128 | // Just do a very simple check to check for possible links |
| 129 | if (false === strpos($string, '<a')) { |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | $hrefs = []; |
| 134 | |
| 135 | $processor = new WP_HTML_Tag_Processor($string); |
| 136 | |
| 137 | while ($processor->next_tag(['tag_name' => 'a'])) { |
| 138 | $href = $processor->get_attribute('href'); |
| 139 | |
| 140 | if (! is_string($href) || 0 === strpos($href, '#')) { |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | $hrefs[] = $href; |
| 145 | } |
| 146 | |
| 147 | return $hrefs; |
| 148 | } |
| 149 | |
| 150 | private function contains_html(string $string): bool |
| 151 | { |
| 152 | return $string && $string !== strip_tags($string); |
| 153 | } |
| 154 | |
| 155 | public function divided(array $array): string |
| 156 | { |
| 157 | $array = Arrays::create()->filter($array); |
| 158 | |
| 159 | return implode('<span class="ac-divider"></span>', $array); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Small HTML block with grey background and rounded corners |
| 164 | */ |
| 165 | public function small_block(array $items): string |
| 166 | { |
| 167 | $blocks = []; |
| 168 | |
| 169 | foreach ($items as $item) { |
| 170 | if ($item && is_scalar($item)) { |
| 171 | $blocks[] = sprintf('<span class="ac-small-block">%s</span>', $item); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return implode('', $blocks); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Return round HTML span |
| 180 | */ |
| 181 | public function rounded(string $string): string |
| 182 | { |
| 183 | return sprintf('<span class="ac-rounded">%s</span>', $string); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @deprecated 7.0.9 |
| 188 | */ |
| 189 | public function get_internal_external_links(): ?array |
| 190 | { |
| 191 | _deprecated_function(__METHOD__, '7.0.9'); |
| 192 | |
| 193 | return []; |
| 194 | } |
| 195 | |
| 196 | public function strip_attributes(string $html): string |
| 197 | { |
| 198 | _deprecated_function(__METHOD__, '7.0.9'); |
| 199 | |
| 200 | return $html; |
| 201 | } |
| 202 | |
| 203 | public function more(array $array, int $limit = 10, string $glue = ', '): string |
| 204 | { |
| 205 | _deprecated_function(__METHOD__, '7.0.11'); |
| 206 | |
| 207 | return implode($glue, $array); |
| 208 | } |
| 209 | |
| 210 | } |
| 211 |