Files
ogre-alert/inc/display.php
Cooper Dalrymple e7f5108b76 Add shortcode
2025-12-22 14:59:32 -06:00

112 lines
3.4 KiB
PHP

<?php
/**
* @package CleverOgre
* @subpackage OgreAlert
* @version 0.2.2
* @since 0.1.9
*/
namespace OgreAlert;
if (!defined('ABSPATH')) exit;
class Display {
public static function load() {
if (is_admin()) return;
add_action('wp', [__CLASS__, 'init']);
add_shortcode('ogrealert', [__CLASS__, 'shortcode']);
}
static function init() {
if (!\OgreAlert\Alert::has_active()) return;
add_action('wp_enqueue_scripts', [__CLASS__, 'enqueue_scripts']);
$position = \OgreAlert\Settings::get('message_position');
switch ($position) {
case 'top':
case 'bottom':
case 'footer':
add_action('wp_footer', [__CLASS__, 'display_alerts']);
break;
case 'custom':
add_action('display_alerts', [__CLASS__, 'display_alerts']);
break;
}
add_filter('body_class', [__CLASS__, 'body_class'], 10, 1);
}
static function enqueue_scripts() {
wp_enqueue_style('ogrealert', \OgreAlert\Settings::get('dir') . 'assets/css/style.css', [], \OgreAlert\Settings::get('version'), 'all');
wp_enqueue_script('ogrealert', \OgreAlert\Settings::get('dir') . 'assets/js/frontend.js', ['jquery'], \OgreAlert\Settings::get('version'), true);
wp_localize_script('ogrealert', 'ogrealert', [
'text_color' => \OgreAlert\Settings::get('message_text_color'),
'background_color' => \OgreAlert\Settings::get('message_background_color'),
'transition_duration' => intval(\OgreAlert\Settings::get('message_transition_duration')),
'transition_animation' => \OgreAlert\Settings::get('message_transition_animation'),
]);
}
static function display_alerts() {
if (!\OgreAlert\Alert::has_active()) return;
self::load_template('loop-start');
foreach (\OgreAlert\Alert::get_active() as $id) {
self::load_template('alert', \OgreAlert\Priority::get($id), true, ['id' => $id]);
}
self::load_template('loop-end');
}
static function body_class($classes) {
if (is_admin() || !\OgreAlert\Alert::has_active()) return $classes;
$classes[] = 'has-alert';
foreach (\OgreAlert\Alert::get_active() as $id) {
$classes[] = sprintf('has-alert-%d', $id);
}
return $classes;
}
static function shortcode($atts) {
$atts = shortcode_atts([], $atts, 'ogrealert');
ob_start();
do_action('display_alerts');
return ob_get_clean();
}
private static function load_template($filename, $filepart = '', $echo = true, $vars = []) {
$path = \OgreAlert\Settings::get('path') . 'templates/' . $filename . '-' . $filepart . '.php';
if (!file_exists($path)) {
$path = \OgreAlert\Settings::get('path') . 'templates/' . $filename . '.php';
if (!file_exists($path)) {
if ($echo) {
echo '';
return false;
} else {
return '';
}
}
}
ob_start();
extract($vars);
include($path);
$html = ob_get_contents();
ob_end_clean();
if ($echo) {
echo $html;
return true;
} else {
return $html;
}
}
}
\OgreAlert\Display::load();