| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* A component class for rendering a bootstrap alert. |
| 9 |
* |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
class AUI_Component_Alert { |
| 13 |
|
| 14 |
/** |
| 15 |
* Build the component. |
| 16 |
* |
| 17 |
* @param array $args |
| 18 |
* |
| 19 |
* @return string The rendered component. |
| 20 |
*/ |
| 21 |
public static function get($args = array()){ |
| 22 |
global $aui_bs5; |
| 23 |
$defaults = array( |
| 24 |
'type' => 'info', |
| 25 |
'class' => '', |
| 26 |
'icon' => '', |
| 27 |
'heading' => '', |
| 28 |
'content' => '', |
| 29 |
'footer' => '', |
| 30 |
'dismissible'=> false, |
| 31 |
'data' => '', |
| 32 |
); |
| 33 |
|
| 34 |
/** |
| 35 |
* Parse incoming $args into an array and merge it with $defaults |
| 36 |
*/ |
| 37 |
$args = wp_parse_args( $args, $defaults ); |
| 38 |
$output = ''; |
| 39 |
if ( ! empty( $args['content'] ) ) { |
| 40 |
$type = sanitize_html_class( $args['type'] ); |
| 41 |
if($type=='error'){$type='danger';} |
| 42 |
$icon = !empty($args['icon']) ? "<i class='".esc_attr($args['icon'])."'></i>" : ''; |
| 43 |
|
| 44 |
// set default icon |
| 45 |
if(!$icon && $args['icon']!==false && $type){ |
| 46 |
if($type=='danger'){$icon = '<i class="fas fa-exclamation-circle"></i>';} |
| 47 |
elseif($type=='warning'){$icon = '<i class="fas fa-exclamation-triangle"></i>';} |
| 48 |
elseif($type=='success'){$icon = '<i class="fas fa-check-circle"></i>';} |
| 49 |
elseif($type=='info'){$icon = '<i class="fas fa-info-circle"></i>';} |
| 50 |
} |
| 51 |
|
| 52 |
$data = ''; |
| 53 |
$class = !empty($args['class']) ? esc_attr($args['class']) : ''; |
| 54 |
if($args['dismissible']){$class .= " alert-dismissible fade show";} |
| 55 |
|
| 56 |
// open |
| 57 |
$output .= '<div class="alert alert-' . $type . ' '.$class.'" role="alert" '.$data.'>'; |
| 58 |
|
| 59 |
// heading |
| 60 |
if ( ! empty( $args['heading'] ) ) { |
| 61 |
$output .= '<h4 class="alert-heading">' . $args['heading'] . '</h4>'; |
| 62 |
} |
| 63 |
|
| 64 |
// icon |
| 65 |
if ( ! empty( $icon) ) { |
| 66 |
$output .= $icon." "; |
| 67 |
} |
| 68 |
|
| 69 |
// content |
| 70 |
$output .= $args['content']; |
| 71 |
|
| 72 |
// dismissible |
| 73 |
if($args['dismissible']){ |
| 74 |
|
| 75 |
if ( $aui_bs5 ) { |
| 76 |
$output .= '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>'; |
| 77 |
}else{ |
| 78 |
$output .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close">'; |
| 79 |
$output .= '<span aria-hidden="true">×</span>'; |
| 80 |
$output .= '</button>'; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// footer |
| 85 |
if ( ! empty( $args['footer'] ) ) { |
| 86 |
$output .= '<hr>'; |
| 87 |
$output .= '<p class="mb-0">' . $args['footer'] . '</p>'; |
| 88 |
} |
| 89 |
|
| 90 |
// close |
| 91 |
$output .= '</div>'; |
| 92 |
} |
| 93 |
|
| 94 |
return $output; |
| 95 |
} |
| 96 |
|
| 97 |
} |