Select
4 years ago
Arrays.php
4 years ago
Date.php
4 years ago
File.php
4 years ago
Html.php
4 years ago
Icon.php
4 years ago
Image.php
4 years ago
Media.php
4 years ago
Menu.php
4 years ago
Network.php
4 years ago
Post.php
4 years ago
Strings.php
4 years ago
Taxonomy.php
4 years ago
User.php
4 years ago
Html.php
510 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Helper; |
| 4 | |
| 5 | use DOMDocument; |
| 6 | use DOMElement; |
| 7 | |
| 8 | class Html { |
| 9 | |
| 10 | /** |
| 11 | * @param string $key |
| 12 | * @param string $value |
| 13 | * |
| 14 | * @return string |
| 15 | * @since 3.0 |
| 16 | */ |
| 17 | public function get_attribute_as_string( $key, $value ) { |
| 18 | return sprintf( '%s="%s"', $key, esc_attr( trim( $value ) ) ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @param array $attributes |
| 23 | * |
| 24 | * @return string |
| 25 | * @since 3.0 |
| 26 | */ |
| 27 | public function get_attributes_as_string( array $attributes ) { |
| 28 | $output = []; |
| 29 | |
| 30 | foreach ( $attributes as $key => $value ) { |
| 31 | $output[] = $this->get_attribute_as_string( $key, $value ); |
| 32 | } |
| 33 | |
| 34 | return implode( ' ', $output ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param string $url |
| 39 | * @param string $label |
| 40 | * @param array $attributes |
| 41 | * |
| 42 | * @return string|false HTML Anchor element |
| 43 | */ |
| 44 | public function link( $url, $label = null, $attributes = [] ) { |
| 45 | if ( false === $label ) { |
| 46 | return $label; |
| 47 | } |
| 48 | |
| 49 | if ( ! $url ) { |
| 50 | return $label; |
| 51 | } |
| 52 | |
| 53 | if ( null === $label ) { |
| 54 | $label = urldecode( $url ); |
| 55 | } |
| 56 | |
| 57 | if ( ! $label ) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | if ( ! $this->contains_html( $label ) ) { |
| 62 | $label = esc_html( $label ); |
| 63 | } |
| 64 | |
| 65 | if ( array_key_exists( 'tooltip', $attributes ) ) { |
| 66 | $attributes['data-ac-tip'] = $attributes['tooltip']; |
| 67 | unset( $attributes['tooltip'] ); |
| 68 | } |
| 69 | |
| 70 | $allowed = wp_allowed_protocols(); |
| 71 | $allowed[] = 'skype'; |
| 72 | $allowed[] = 'call'; |
| 73 | |
| 74 | return '<a href="' . esc_url( $url, $allowed ) . '" ' . $this->get_attributes( $attributes ) . '>' . $label . '</a>'; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @since 2.5 |
| 79 | */ |
| 80 | public function divider() { |
| 81 | return '<span class="ac-divider"></span>'; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param string $content |
| 86 | * |
| 87 | * @return string |
| 88 | */ |
| 89 | public function get_tooltip_attr( $content ) { |
| 90 | if ( ! $content ) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | return 'data-ac-tip="' . esc_attr( $content ) . '"'; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param $label |
| 99 | * @param $tooltip |
| 100 | * @param array $attributes |
| 101 | * |
| 102 | * @return string |
| 103 | */ |
| 104 | public function tooltip( $label, $tooltip, $attributes = [] ) { |
| 105 | if ( ac_helper()->string->is_not_empty( $label ) && $tooltip ) { |
| 106 | $label = '<span ' . $this->get_tooltip_attr( $tooltip ) . $this->get_attributes( $attributes ) . '>' . $label . '</span>'; |
| 107 | } |
| 108 | |
| 109 | return $label; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Displays a toggle Box. |
| 114 | * |
| 115 | * @param string $label |
| 116 | * @param string $contents |
| 117 | */ |
| 118 | public function toggle_box( $label, $contents ) { |
| 119 | if ( ! $label ) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if ( $contents ) : ?> |
| 124 | <a class="ac-toggle-box-link" href="#"><?php echo $label; ?></a> |
| 125 | <div class="ac-toggle-box-contents"><?php echo $contents; ?></div> |
| 126 | <?php |
| 127 | else : |
| 128 | echo $label; |
| 129 | endif; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Display a toggle box which trigger an ajax event on click. The ajax callback calls AC\Column::get_ajax_value. |
| 134 | * |
| 135 | * @param int $id |
| 136 | * @param string $label |
| 137 | * @param string $column_name |
| 138 | * |
| 139 | * @return string |
| 140 | */ |
| 141 | public function get_ajax_toggle_box_link( $id, $label, $column_name, $label_close = null ) { |
| 142 | return ac_helper()->html->link( '#', $label . '<div class="spinner"></div>', [ |
| 143 | 'class' => 'ac-toggle-box-link', |
| 144 | 'data-column' => $column_name, |
| 145 | 'data-item-id' => $id, |
| 146 | 'data-ajax-populate' => 1, |
| 147 | 'data-label' => $label, |
| 148 | 'data-label-close' => $label_close, |
| 149 | ] ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Display a modal which trigger an ajax event on click. The ajax callback calls AC\Column::get_ajax_value. |
| 154 | * |
| 155 | * @param int $item_id |
| 156 | * @param string $label |
| 157 | * @param string $column_name |
| 158 | * |
| 159 | * @return string |
| 160 | */ |
| 161 | public function get_ajax_modal_link( $item_id, $label, $column_name ) { |
| 162 | return ac_helper()->html->link( '#', $label, [ |
| 163 | 'class' => 'ac-modal-box-link', |
| 164 | 'data-column' => $column_name, |
| 165 | 'data-item-id' => $item_id, |
| 166 | 'data-ajax-populate' => 1, |
| 167 | 'data-label' => $label, |
| 168 | ] ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @param string $string |
| 173 | * @param int $max_chars |
| 174 | * |
| 175 | * @return string |
| 176 | */ |
| 177 | public function codearea( $string, $max_chars = 1000 ) { |
| 178 | if ( ! $string ) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | return '<textarea style="color: #808080; width: 100%; min-height: 60px;" readonly>' . substr( $string, 0, $max_chars ) . '</textarea>'; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @param array $attributes |
| 187 | * |
| 188 | * @return string |
| 189 | */ |
| 190 | private function get_attributes( $attributes ) { |
| 191 | $_attributes = []; |
| 192 | |
| 193 | foreach ( $attributes as $attribute => $value ) { |
| 194 | if ( in_array( $attribute, [ 'title', 'id', 'class', 'style', 'target', 'rel', 'download' ] ) || 'data-' === substr( $attribute, 0, 5 ) ) { |
| 195 | $_attributes[] = $this->get_attribute_as_string( $attribute, $value ); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return ' ' . implode( ' ', $_attributes ); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns an array with internal / external links |
| 204 | * |
| 205 | * @param string $string |
| 206 | * @param array $internal_domains Domains which determine internal links. Default is home_url(). |
| 207 | * |
| 208 | * @return false|array [ internal | external ] |
| 209 | */ |
| 210 | public function get_internal_external_links( $string, $internal_domains = [] ) { |
| 211 | if ( ! class_exists( 'DOMDocument' ) ) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | // Just do a very simple check to check for possible links |
| 216 | if ( false === strpos( $string, '<a' ) ) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | if ( ! $internal_domains ) { |
| 221 | $internal_domains = [ home_url() ]; |
| 222 | } |
| 223 | |
| 224 | $internal_links = []; |
| 225 | $external_links = []; |
| 226 | |
| 227 | $dom = new DOMDocument(); |
| 228 | @$dom->loadHTML( $string ); |
| 229 | |
| 230 | $links = $dom->getElementsByTagName( 'a' ); |
| 231 | |
| 232 | foreach ( $links as $link ) { |
| 233 | /** @var DOMElement $link */ |
| 234 | $href = $link->getAttribute( 'href' ); |
| 235 | |
| 236 | if ( 0 === strpos( $href, '#' ) ) { |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | $internal = false; |
| 241 | |
| 242 | foreach ( (array) $internal_domains as $domain ) { |
| 243 | if ( false !== strpos( $href, $domain ) ) { |
| 244 | $internal = true; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if ( $internal ) { |
| 249 | $internal_links[] = $href; |
| 250 | } else { |
| 251 | $external_links[] = $href; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if ( empty( $internal_links ) && empty( $external_links ) ) { |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | return [ |
| 260 | $internal_links, $external_links, |
| 261 | ]; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @param string $string |
| 266 | * |
| 267 | * @return bool |
| 268 | */ |
| 269 | private function contains_html( $string ) { |
| 270 | return $string && is_string( $string ) && $string !== strip_tags( $string ); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Display indicator icon in the column settings header |
| 275 | * |
| 276 | * @param $class |
| 277 | * @param $id |
| 278 | * @param bool $title |
| 279 | */ |
| 280 | public function indicator( $class, $id, $title = false ) { ?> |
| 281 | <span class="indicator-<?php echo esc_attr( $class ); ?>" data-indicator-id="<?php echo esc_attr( $id ); ?>" title="<?php echo esc_attr( $title ); ?>"></span> |
| 282 | <?php |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Adds a divider to the implode |
| 287 | * |
| 288 | * @param $array |
| 289 | * @param bool $divider |
| 290 | * |
| 291 | * @return string |
| 292 | */ |
| 293 | public function implode( $array, $divider = true ) { |
| 294 | if ( ! is_array( $array ) ) { |
| 295 | return $array; |
| 296 | } |
| 297 | |
| 298 | // Remove empty values |
| 299 | $array = $this->remove_empty( $array ); |
| 300 | |
| 301 | if ( true === $divider ) { |
| 302 | $divider = $this->divider(); |
| 303 | } |
| 304 | |
| 305 | return implode( $divider, $array ); |
| 306 | } |
| 307 | |
| 308 | public function remove_empty( $array ) { |
| 309 | return array_filter( $array, [ ac_helper()->string, 'is_not_empty' ] ); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Remove attribute from an html tag |
| 314 | * |
| 315 | * @param string $html HTML tag |
| 316 | * @param $attributes |
| 317 | * |
| 318 | * @return mixed |
| 319 | */ |
| 320 | public function strip_attributes( $html, $attributes ) { |
| 321 | if ( $this->contains_html( $html ) ) { |
| 322 | foreach ( (array) $attributes as $attribute ) { |
| 323 | $html = preg_replace( '/(<[^>]+) ' . $attribute . '=".*?"/i', '$1', $html ); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return $html; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Small HTML block with grey background and rounded corners |
| 332 | * |
| 333 | * @param string|array $items |
| 334 | * |
| 335 | * @return string |
| 336 | */ |
| 337 | public function small_block( $items ) { |
| 338 | $blocks = []; |
| 339 | |
| 340 | foreach ( (array) $items as $item ) { |
| 341 | if ( $item && is_string( $item ) ) { |
| 342 | $blocks[] = '<span class="ac-small-block">' . $item . '</span>'; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | return implode( $blocks ); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * @param array $args |
| 351 | * |
| 352 | * @return string |
| 353 | */ |
| 354 | public function progress_bar( $args = [] ) { |
| 355 | $defaults = [ |
| 356 | 'current' => 0, |
| 357 | 'total' => 100, // -1 is infinitive |
| 358 | 'label_left' => '', |
| 359 | 'label_right' => '', |
| 360 | 'label_main' => '', |
| 361 | ]; |
| 362 | |
| 363 | $args = wp_parse_args( $args, $defaults ); |
| 364 | |
| 365 | if ( -1 === $args['total'] ) { |
| 366 | $args['current'] = 0; |
| 367 | $args['total'] = 100; |
| 368 | $args['label_right'] = '∞'; |
| 369 | } |
| 370 | |
| 371 | $args['current'] = absint( $args['current'] ); |
| 372 | $args['total'] = absint( $args['total'] ); |
| 373 | |
| 374 | if ( $args['total'] < 0 ) { |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | $percentage = 0; |
| 379 | |
| 380 | if ( $args['total'] > 0 ) { |
| 381 | $percentage = round( ( $args['current'] / $args['total'] ) * 100 ); |
| 382 | } |
| 383 | |
| 384 | // Allowed size is zero, but current has a value |
| 385 | if ( 0 === $args['total'] && $args['current'] > 0 ) { |
| 386 | $percentage = 101; |
| 387 | } |
| 388 | |
| 389 | $class = ''; |
| 390 | if ( $percentage > 100 ) { |
| 391 | $percentage = 100; |
| 392 | $class = ' full'; |
| 393 | } |
| 394 | |
| 395 | ob_start(); |
| 396 | ?> |
| 397 | <div class="ac-progress-bar<?php echo esc_attr( $class ); ?>"> |
| 398 | <?php if ( $args['label_main'] ) : ?> |
| 399 | <span class="ac-label-main"><?php echo esc_html( $args['label_main'] ); ?></span> |
| 400 | <?php endif; ?> |
| 401 | <div class="ac-bar-container"> |
| 402 | <span class="ac-label-left"><?php echo esc_html( $args['label_left'] ); ?></span> |
| 403 | <span class="ac-label-right"><?php echo esc_html( $args['label_right'] ); ?></span> |
| 404 | <?php if ( $percentage ) : ?> |
| 405 | <div class="ac-bar" style="width:<?php echo esc_attr( $percentage ); ?>%"></div> |
| 406 | <?php endif; ?> |
| 407 | </div> |
| 408 | </div> |
| 409 | <?php |
| 410 | |
| 411 | return ob_get_clean(); |
| 412 | } |
| 413 | |
| 414 | public function more( $array, $number = 10, $glue = ', ' ) { |
| 415 | if ( ! $number ) { |
| 416 | return implode( $glue, $array ); |
| 417 | } |
| 418 | |
| 419 | $first_set = array_slice( $array, 0, $number ); |
| 420 | $last_set = array_slice( $array, $number ); |
| 421 | |
| 422 | ob_start(); |
| 423 | |
| 424 | if ( $first_set ) { |
| 425 | $first = sprintf( '<span class="ac-show-more__part -first">%s</span>', implode( $glue, $first_set ) ); |
| 426 | $more = $last_set ? sprintf( '<span class="ac-show-more__part -more">%s%s</span>', $glue, implode( $glue, $last_set ) ) : ''; |
| 427 | $content = sprintf( '<span class="ac-show-more__content">%s%s</span>', $first, $more ); |
| 428 | $toggler = $last_set ? sprintf( '<span class="ac-show-more__divider">|</span><a class="ac-show-more__toggle" data-show-more-toggle data-more="%1$s" data-less="%2$s">%1$s</a>', sprintf( __( '%s more', 'codepress-admin-columns' ), count( $last_set ) ), strtolower( __( 'Hide', 'codepress-admin-columns' ) ) ) : ''; |
| 429 | |
| 430 | echo sprintf( '<span class="ac-show-more">%s</span>', $content . $toggler ); |
| 431 | } |
| 432 | |
| 433 | return ob_get_clean(); |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Return round HTML span |
| 438 | * |
| 439 | * @param $string |
| 440 | * |
| 441 | * @return string |
| 442 | */ |
| 443 | public function rounded( $string ) { |
| 444 | return '<span class="ac-rounded">' . $string . '</span>'; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Returns star rating based on X start from $max count. Does support decimals. |
| 449 | * |
| 450 | * @param int $count |
| 451 | * @param int $max |
| 452 | * |
| 453 | * @return string |
| 454 | */ |
| 455 | public function stars( $count, $max = 0 ) { |
| 456 | $stars = [ |
| 457 | 'filled' => floor( $count ), |
| 458 | 'half' => floor( round( ( $count * 2 ) ) - ( floor( $count ) * 2 ) ) ? 1 : 0, |
| 459 | 'empty' => 0, |
| 460 | ]; |
| 461 | |
| 462 | $max = absint( $max ); |
| 463 | |
| 464 | if ( $max > 0 ) { |
| 465 | $star_count = $stars['filled'] + $stars['half']; |
| 466 | |
| 467 | $stars['empty'] = $max - $star_count; |
| 468 | |
| 469 | if ( $star_count > $max ) { |
| 470 | $stars['filled'] = $max; |
| 471 | $stars['half'] = 0; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | $icons = []; |
| 476 | |
| 477 | foreach ( $stars as $type => $_count ) { |
| 478 | for ( $i = 1; $i <= $_count; $i++ ) { |
| 479 | $icons[] = ac_helper()->icon->dashicon( [ 'icon' => 'star-' . $type, 'class' => 'ac-value-star' ] ); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | ob_start(); |
| 484 | ?> |
| 485 | <span class="ac-value-stars"> |
| 486 | <?php echo implode( ' ', $icons ); ?> |
| 487 | </span> |
| 488 | <?php |
| 489 | return ob_get_clean(); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * @param string $value HTML |
| 494 | * @param bool $removed |
| 495 | * |
| 496 | * @return string |
| 497 | */ |
| 498 | public function images( $value, $removed = false ) { |
| 499 | if ( ! $value ) { |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | if ( $removed ) { |
| 504 | $value .= ac_helper()->html->rounded( '+' . $removed ); |
| 505 | } |
| 506 | |
| 507 | return '<div class="ac-image-container">' . $value . '</div>'; |
| 508 | } |
| 509 | |
| 510 | } |