Skip to content

Instantly share code, notes, and snippets.

@shanejones
Created February 5, 2018 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanejones/df662462fa23f4514bbd501980db568f to your computer and use it in GitHub Desktop.
Save shanejones/df662462fa23f4514bbd501980db568f to your computer and use it in GitHub Desktop.
<?php
/**
* Bootstrap Alert Dialog
*
* @link http://v4-alpha.getbootstrap.com/components/alerts/
*
* @param $a array containing the options for this function. Expected array keys are below
* html - string - this is the HTML that will be outputted
* type - string - type of alert info(default), success, warning, danger
* close_button - bool- true or false to display
* extra_classes - string - if the user needs any other classes in here
* attrs - string - if the user needs any other attributes and values to the tag
*
* @author ShaneJones
* @since 2.0.0
*
*/
static function alert($a) {
$html = (isset($a['html'])?$a['html']:null);
$type = (isset($a['type'])?$a['type']:'info');
$close_button = (isset($a['close_button'])?$a['close_button']:null);
$extra_classes = (isset($a['extra_classes'])?$a['extra_classes']:null);
$extra_ids = (isset($a['extra_ids'])?$a['extra_ids']:null);
$attrs = (isset($a['attrs'])?$a['attrs']:null);
/** check for ahrefs in $html */
preg_match_all('/(<a.*?>)/', $html, $links);
$links = $links[0];
if(sizeof($links)>0){
$links = array_unique($links);
foreach($links as $link){
$current_link = $link;
$updated_link = $link;
if(strpos($updated_link, 'alert-link') == true){
// this link is fine so bail
break;
} else if(strpos($updated_link, 'class=') == true){
// this needs the class name appending to the current class
preg_match('/(class=".*?")/', $link, $class);
$class = array_unique($class);
$current_class = $class[0];
$updated_class = $class[0];
$updated_class = substr($updated_class, 0, -1);
$updated_class .= ' alert-link"';
$html = str_replace($current_class, $updated_class, $html);
} else {
// this needs the class attribute adding
$updated_link = substr($updated_link, 0, -1);
if(substr($updated_link, -1) == ' '){
$updated_link .= 'class="alert-link">';
} else {
$updated_link .= ' class="alert-link">';
}
$html = str_replace($current_link, $updated_link, $html);
}
}
}
if($extra_ids){
$extra_ids = 'id="' . $extra_ids . '"';
}
/** Output the alert with all of the options */
?>
<div class="alert alert-<?php echo $type ?> <?php echo $extra_classes; ?>" <?php echo $extra_ids ?> role="alert" <?php echo $attrs ?>>
<?php if($close_button): ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<? endif; ?>
<?php echo $html; ?>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment