Initial commit.
This commit is contained in:
372
inc/alert.php
Normal file
372
inc/alert.php
Normal file
@@ -0,0 +1,372 @@
|
||||
<?php
|
||||
/**
|
||||
* @package CleverOgre
|
||||
* @subpackage OgreAlert
|
||||
* @since OgreAlert 0.1.7
|
||||
*/
|
||||
|
||||
namespace OgreAlert;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Alert {
|
||||
|
||||
public static function load() {
|
||||
add_action('init', array(__CLASS__, 'init'));
|
||||
add_filter('manage_alert_posts_columns', array(__CLASS__, 'register_columns'));
|
||||
add_action('manage_alert_posts_custom_column', array(__CLASS__, 'display_column'), 10, 2);
|
||||
|
||||
// OgreAlert Content Filters
|
||||
add_filter('ogrealert/content', 'wptexturize');
|
||||
add_filter('ogrealert/content', 'convert_smilies', 20);
|
||||
add_filter('ogrealert/content', 'wpautop');
|
||||
add_filter('ogrealert/content', 'shortcode_unautop');
|
||||
add_filter('ogrealert/content', 'prepend_attachment');
|
||||
add_filter('ogrealert/content', 'wp_make_content_images_responsive');
|
||||
add_filter('ogrealert/content', 'do_shortcode', 11); // After wpautop
|
||||
}
|
||||
|
||||
static function init() {
|
||||
$post_type_args = array(
|
||||
'supports' => array('title', 'editor', 'revisions'),
|
||||
'public' => true,
|
||||
'exclude_from_search' => true,
|
||||
'publicly_queryable' => false,
|
||||
'show_in_nav_menus' => false,
|
||||
'show_ui' => true,
|
||||
'has_archive' => false,
|
||||
'show_in_admin_bar' => true,
|
||||
'menu_position' => apply_filters('ogrealert/menu_position', 20),
|
||||
'menu_icon' => 'dashicons-format-status',
|
||||
);
|
||||
|
||||
if (class_exists('Ogre')) {
|
||||
$post_type_args['labels'] = \Ogre::get_labels(__('Alerts', 'ogrealert'), __('Alert', 'ogrealert'));
|
||||
} else {
|
||||
$post_type_args['labels'] = array(
|
||||
'name' => __('Alerts', 'ogrealert'),
|
||||
'singular_name' => __('Alert', 'ogrealert'),
|
||||
);
|
||||
}
|
||||
|
||||
register_post_type('alert', apply_filters('ogrealert/post_type_args', $post_type_args));
|
||||
}
|
||||
|
||||
static function register_columns($columns) {
|
||||
$_columns = array();
|
||||
|
||||
foreach ($columns as $key => $label) {
|
||||
if ($key == 'date') {
|
||||
$_columns['priority'] = __('Priority', 'ogrealert');
|
||||
$_columns['expiration'] = __('Expires On', 'ogrealert');
|
||||
}
|
||||
$_columns[$key] = $label;
|
||||
}
|
||||
|
||||
return $_columns;
|
||||
}
|
||||
|
||||
static function display_column($column, $post_id) {
|
||||
switch ($column) {
|
||||
case 'priority':
|
||||
esc_html_e(ucfirst(get_post_meta($post_id, '_ogrealert_priority', true)));
|
||||
break;
|
||||
case 'expiration':
|
||||
$exp_time = \OgreAlert\Expiration::get_timestamp($post_id);
|
||||
if (!!$exp_time) esc_html_e(date(get_option('date_format'), $exp_time));
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static function valid($id) {
|
||||
if (is_a($id, 'WP_Post')) $id = $id->ID;
|
||||
|
||||
$return = true;
|
||||
|
||||
if (get_post_type($id) != 'alert' || get_post_status($id) != 'publish') $return = false;
|
||||
if (\OgreAlert\Settings::get('message_enabled') != '1') $return = false;
|
||||
if (empty(trim(self::get_content($id, false)))) $return = false;
|
||||
//if (isset($_COOKIE['ogrealert_' . strval($id)])) $return = false;
|
||||
|
||||
return apply_filters('ogrealert/valid_id', $return, $id);
|
||||
}
|
||||
|
||||
private static function validate_ids(&$ids) {
|
||||
if (!is_array($ids) || empty($ids)) return false;
|
||||
|
||||
$ids = array_map('intval', $ids);
|
||||
|
||||
$remove_keys = array();
|
||||
foreach ($ids as $key => $id) {
|
||||
if (!self::valid($id)) $remove_keys[] = $key;
|
||||
}
|
||||
foreach ($remove_keys as $key) {
|
||||
unset($ids[$key]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function get_content($id, $echo = false) {
|
||||
if (is_a($id, 'WP_Post')) $id = $id->ID;
|
||||
|
||||
$_post = get_post($id);
|
||||
if (is_wp_error($_post) || !is_a($_post, 'WP_Post')) return false;
|
||||
|
||||
$content = apply_filters('ogrealert/content', $_post->post_content);
|
||||
|
||||
if ($echo == true) {
|
||||
echo $content;
|
||||
return true;
|
||||
} else {
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
public static function get_active() {
|
||||
$alerts = get_posts(apply_filters('ogrealert/get_active_args', array(
|
||||
'posts_per_page' => -1,
|
||||
'offset' => 0,
|
||||
'orderby' => 'rand',
|
||||
'order' => 'ASC',
|
||||
'post_type' => 'alert',
|
||||
'post_stauts' => 'publish',
|
||||
'fields' => 'ids',
|
||||
)));
|
||||
$alerts = !is_wp_error($alerts) && !empty($alerts) ? $alerts : false;
|
||||
|
||||
self::validate_ids($alerts);
|
||||
|
||||
return apply_filters('ogrealert/get_active', $alerts);
|
||||
}
|
||||
|
||||
public static function get_random() {
|
||||
$alerts = self::get_active();
|
||||
|
||||
$return = false;
|
||||
if (!empty($alerts)) $return = array_values($alerts)[0];
|
||||
|
||||
return apply_filters('ogrealert/get_random', $return);
|
||||
}
|
||||
|
||||
public static function is_active($id = false) {
|
||||
if ($id === false) $id = self::get_random();
|
||||
if ($id === false) return false;
|
||||
|
||||
$alert = get_post($id);
|
||||
if (is_wp_error($alert) || $alert === false) return false;
|
||||
|
||||
if (!self::valid($alert)) return false;
|
||||
|
||||
return apply_filters('ogrealert/is_active', true, $id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
\OgreAlert\Alert::load();
|
||||
|
||||
class AlertMetaBox {
|
||||
|
||||
public static function load() {
|
||||
if (is_admin()) {
|
||||
add_action('load-post.php', array(__ClASS__, 'init'));
|
||||
add_action('load-post-new.php', array(__ClASS__, 'init'));
|
||||
}
|
||||
}
|
||||
|
||||
public static function init() {
|
||||
add_action('add_meta_boxes_alert', array(__CLASS__, 'add'));
|
||||
add_action('save_post', array(__CLASS__, 'save'), 10, 2);
|
||||
add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'));
|
||||
}
|
||||
|
||||
static function enqueue_scripts() {
|
||||
if (!wp_style_is('jquery-ui-datepicker', 'enqueued')) {
|
||||
wp_enqueue_style('jquery-ui-datepicker');
|
||||
}
|
||||
wp_enqueue_script('ogrealert-post', \OgreAlert\Settings::get('dir') . 'assets/js/post.js', array('jquery', 'jquery-ui-core', 'jquery-ui-datepicker'), \OgreAlert\Settings::get('version'), true);
|
||||
}
|
||||
|
||||
static function add() {
|
||||
add_meta_box(
|
||||
'ogrealert_post_settings',
|
||||
__('Message Settings', 'ogrealert'),
|
||||
array(__CLASS__, 'render'),
|
||||
'alert',
|
||||
'side',
|
||||
'default'
|
||||
);
|
||||
}
|
||||
|
||||
static function render($post) {
|
||||
wp_nonce_field('post_settings', '_ogrealert_nonce');
|
||||
|
||||
echo '<p>';
|
||||
self::field_select(array(
|
||||
'id' => 'ogrealert_post_settings_priority',
|
||||
'name' => '_ogrealert_priority',
|
||||
'label' => __('Priority', 'ogrealert'),
|
||||
'options' => array(
|
||||
'high' => __('High', 'ogrealert'),
|
||||
'normal' => __('Normal', 'ogrealert'),
|
||||
'low' => __('Low', 'ogrealert'),
|
||||
),
|
||||
'default' => 'normal',
|
||||
'class' => 'widefat',
|
||||
));
|
||||
echo '</p>';
|
||||
|
||||
echo '<p>';
|
||||
self::field_date(array(
|
||||
'id' => 'ogrealert_post_settings_expiration',
|
||||
'name' => '_ogrealert_expiration',
|
||||
'label' => __('Expires On', 'ogrealer'),
|
||||
'class' => 'widefat',
|
||||
));
|
||||
echo '</p>';
|
||||
|
||||
echo '<p>';
|
||||
self::field_select(array(
|
||||
'id' => 'ogrealert_post_settings_duration',
|
||||
'name' => '_ogrealert_duration',
|
||||
'label' => __('Dismiss Duration', 'ogrealert'),
|
||||
'options' => array(
|
||||
'none' => __('None', 'ogrealert'),
|
||||
'page' => __('Page', 'ogrealert'),
|
||||
'minute' => __('Minute', 'ogrealert'),
|
||||
'hour' => __('Hour', 'ogrealert'),
|
||||
'day' => __('Day', 'ogrealert'),
|
||||
'week' => __('Week', 'ogrealert'),
|
||||
'month' => __('Month', 'ogrealert'),
|
||||
'year' => __('Year', 'ogrealert'),
|
||||
),
|
||||
'default' => \OgreAlert\Settings::get('message_dismiss_duration'),
|
||||
'class' => 'widefat',
|
||||
'description' => __('If "None" is selected, the notice will not be able to be dismissed.', 'ogrealert'),
|
||||
));
|
||||
echo '</p>';
|
||||
}
|
||||
|
||||
static function save($post_id, $post) {
|
||||
// Check if nonce is valid
|
||||
if (!wp_verify_nonce(isset($_POST['_ogrealert_nonce']) ? $_POST['_ogrealert_nonce'] : '', 'post_settings')) return;
|
||||
|
||||
// Check if user is capable
|
||||
if (!current_user_can(\OgreAlert\Settings::get('capability'), $post_id)) return;
|
||||
|
||||
// Check if not an autosave or revision
|
||||
if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) return;
|
||||
|
||||
$keys = array(
|
||||
'_ogrealert_priority',
|
||||
'_ogrealert_expiration',
|
||||
'_ogrealert_duration',
|
||||
);
|
||||
foreach ($keys as $key) {
|
||||
if (isset($_POST[$key])) update_post_meta($post_id, $key, $_POST[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function field_select($args) {
|
||||
if (!isset($args['id']) || empty($args['id'])) return;
|
||||
|
||||
if (isset($args['label']) && !empty($args['label'])) {
|
||||
self::field_label(array(
|
||||
'title' => $args['label'],
|
||||
'required' => isset($args['required']) && $args['required'] == true,
|
||||
));
|
||||
}
|
||||
|
||||
$selected = '';
|
||||
$value = get_post_meta(get_the_ID(), $args['name'], true);
|
||||
if (!empty($value) && in_array($value, array_keys($args['options']))) {
|
||||
$selected = $value;
|
||||
} else if (isset($args['default'])) {
|
||||
$selected = $args['default'];
|
||||
}
|
||||
|
||||
echo '<select id="' . esc_attr($args['id']) . '"';
|
||||
|
||||
if (isset($args['name'])) {
|
||||
echo ' name="' . esc_attr($args['name']) . '"';
|
||||
} else {
|
||||
echo ' name="' . esc_attr($args['id']) . '"';
|
||||
}
|
||||
|
||||
if (isset($args['class']) && !empty($args['class'])) echo ' class="' . esc_attr($args['class']) . '"';
|
||||
|
||||
if (isset($args['required']) && $args['required'] == true) echo ' required="required"';
|
||||
|
||||
echo '>';
|
||||
|
||||
if (isset($args['placeholder']) && !empty($args['placeholder'])) {
|
||||
echo '<option value="">' . esc_html($args['placeholder']) . '</option>';
|
||||
}
|
||||
|
||||
foreach ($args['options'] as $value => $title) {
|
||||
echo '<option value="' . esc_attr($value) . '" ' . selected($selected, $value, false) . '>' . esc_html($title) . '</option>';
|
||||
}
|
||||
|
||||
echo '</select>';
|
||||
|
||||
if (isset($args['description']) && !empty($args['description'])) {
|
||||
echo '<p class="description">';
|
||||
_e($args['description'], 'ogrealert');
|
||||
echo '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
public static function field_date($args) {
|
||||
if (!isset($args['id']) || empty($args['id'])) return;
|
||||
|
||||
if (isset($args['label']) && !empty($args['label'])) {
|
||||
self::field_label(array(
|
||||
'title' => $args['label'],
|
||||
'required' => isset($args['required']) && $args['required'] == true,
|
||||
));
|
||||
}
|
||||
|
||||
$value = get_post_meta(get_the_ID(), $args['name'], true);
|
||||
if (empty($value) && isset($args['default'])) $value = $args['default'];
|
||||
|
||||
echo '<input type="date" class="ogrealert-date-picker ' . esc_attr($args['class']) . '" value="' . esc_attr($value) . '"';
|
||||
|
||||
if (isset($args['id']) && !empty($args['id'])) {
|
||||
echo ' id="' . esc_attr($args['id']) . '"';
|
||||
if (isset($args['name'])) {
|
||||
echo ' name="' . esc_attr($args['name']) . '"';
|
||||
} else {
|
||||
echo ' name="' . esc_attr($args['id']) . '"';
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($args['placeholder']) && !empty($args['placeholder'])) {
|
||||
echo ' placeholder="' . esc_attr($args['placeholder']) . '"';
|
||||
}
|
||||
|
||||
if (isset($args['required']) && $args['required'] == true) {
|
||||
echo ' required="required"';
|
||||
}
|
||||
|
||||
echo ' />';
|
||||
|
||||
if (isset($args['description']) && !empty($args['description'])) {
|
||||
echo '<p class="description">';
|
||||
_e($args['description'], 'ogrealert');
|
||||
echo '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
public static function field_label($args) {
|
||||
if (!isset($args['title'])) return false;
|
||||
|
||||
echo '<label class="' . esc_attr($args['class']) . '" for="' . esc_attr($args['id']) . '">';
|
||||
esc_html_e($args['title']);
|
||||
if (isset($args['required']) && $args['required'] == true) echo ' <span class="required req">*</span>';
|
||||
echo '</label>';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
\OgreAlert\AlertMetaBox::load();
|
||||
90
inc/display.php
Normal file
90
inc/display.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* @package CleverOgre
|
||||
* @subpackage OgreAlert
|
||||
* @since OgreAlert 0.1.7
|
||||
*/
|
||||
|
||||
namespace OgreAlert;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Display {
|
||||
|
||||
public static function load() {
|
||||
if (is_admin()) return;
|
||||
|
||||
add_action('wp', array(__CLASS__, 'init'));
|
||||
}
|
||||
|
||||
static function init() {
|
||||
if (empty(\OgreAlert\Alert::get_active())) return;
|
||||
|
||||
add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'));
|
||||
|
||||
$position = \OgreAlert\Settings::get('message_position');
|
||||
switch ($position) {
|
||||
case 'top':
|
||||
case 'bottom':
|
||||
case 'footer':
|
||||
add_action('wp_footer', array(__CLASS__, 'display_alerts'));
|
||||
break;
|
||||
case 'custom':
|
||||
add_action('display_alerts', array(__CLASS__, 'display_alerts'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static function enqueue_scripts() {
|
||||
wp_enqueue_style('ogrealert', \OgreAlert\Settings::get('dir') . 'assets/scss/style.css', array(), \OgreAlert\Settings::get('version'), 'all');
|
||||
wp_enqueue_script('ogrealert', \OgreAlert\Settings::get('dir') . 'assets/js/display.js', array('jquery'), \OgreAlert\Settings::get('version'), true);
|
||||
wp_localize_script('ogrealert', 'ogrealert', array(
|
||||
'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() {
|
||||
$ids = \OgreAlert\Alert::get_active();
|
||||
if (empty($ids)) return;
|
||||
|
||||
self::load_template('loop-start');
|
||||
foreach ($ids as $id) {
|
||||
self::load_template('alert', \OgreAlert\Priority::get($id), true, array('id' => $id));
|
||||
}
|
||||
self::load_template('loop-end');
|
||||
}
|
||||
|
||||
private static function load_template($filename, $filepart = '', $echo = true, $vars = array()) {
|
||||
$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();
|
||||
47
inc/expiration.php
Normal file
47
inc/expiration.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* @package CleverOgre
|
||||
* @subpackage OgreAlert
|
||||
* @since OgreAlert 0.1.7
|
||||
*/
|
||||
|
||||
namespace OgreAlert;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Expiration {
|
||||
|
||||
public static function load() {
|
||||
add_filter('ogrealert/valid_id', array(__CLASS__, 'check'), 10, 2);
|
||||
}
|
||||
|
||||
static function check($valid, $id) {
|
||||
if (!is_int($id)) return $valid;
|
||||
|
||||
$exp_time = self::get_timestamp($id);
|
||||
if (!$exp_time) return $valid;
|
||||
|
||||
$remaining = $exp_time - time();
|
||||
if ($remaining <= 0) {
|
||||
wp_trash_post($id);
|
||||
$valid = false;
|
||||
}
|
||||
|
||||
return $valid;
|
||||
}
|
||||
|
||||
public static function get_timestamp($id) {
|
||||
$meta_value = get_post_meta($id, '_ogrealert_expiration', true);
|
||||
if (empty($meta_value)) return false;
|
||||
|
||||
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $meta_value) !== 1) return false;
|
||||
|
||||
$exp_time = strtotime($meta_value);
|
||||
if (!$exp_time) return false;
|
||||
|
||||
return $exp_time;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
\OgreAlert\Expiration::load();
|
||||
55
inc/priority.php
Normal file
55
inc/priority.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* @package CleverOgre
|
||||
* @subpackage OgreAlert
|
||||
* @since OgreAlert 0.1.7
|
||||
*/
|
||||
|
||||
namespace OgreAlert;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Priority {
|
||||
|
||||
private static $priorities = array(
|
||||
'high',
|
||||
'normal',
|
||||
'low',
|
||||
);
|
||||
|
||||
public static function load() {
|
||||
add_filter('ogrealert/get_active', array(__CLASS__, 'order'), 10, 1);
|
||||
}
|
||||
|
||||
static function order($alerts) {
|
||||
if (!is_array($alerts) || empty($alerts)) return $alerts;
|
||||
|
||||
$_alerts = array();
|
||||
foreach ($alerts as $id) {
|
||||
$_alerts[] = array(
|
||||
'id' => $id,
|
||||
'priority' => self::get($id),
|
||||
);
|
||||
}
|
||||
|
||||
usort($_alerts, function ($a, $b) {
|
||||
return array_search($a['priority'], self::$priorities) > array_search($b['priority'], self::$priorities);
|
||||
});
|
||||
|
||||
return wp_list_pluck($_alerts, 'id');
|
||||
}
|
||||
|
||||
public static function get($id) {
|
||||
if (!\OgreAlert\Alert::valid($id)) return false;
|
||||
|
||||
$value = get_post_meta($id, '_ogrealert_priority', true);
|
||||
if (!$value || !in_array($value, self::$priorities)) {
|
||||
$value = 'normal';
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
\OgreAlert\Priority::load();
|
||||
452
inc/settings.php
Normal file
452
inc/settings.php
Normal file
@@ -0,0 +1,452 @@
|
||||
<?php
|
||||
/**
|
||||
* @package CleverOgre
|
||||
* @subpackage OgreAlert
|
||||
* @since OgreAlert 0.1.7
|
||||
*/
|
||||
|
||||
namespace OgreAlert;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Settings {
|
||||
|
||||
private static $defaults;
|
||||
|
||||
// Plugin Settings
|
||||
public static $name;
|
||||
public static $version;
|
||||
public static $capability;
|
||||
public static $path;
|
||||
public static $dir;
|
||||
public static $hook;
|
||||
|
||||
// Message Settings
|
||||
public static $message_enabled;
|
||||
public static $message_position;
|
||||
public static $message_container;
|
||||
public static $message_transition_duration;
|
||||
public static $message_transition_animation;
|
||||
public static $message_dismiss_duration;
|
||||
|
||||
public static $message_high_text_color;
|
||||
public static $message_high_background_color;
|
||||
|
||||
public static $message_normal_text_color;
|
||||
public static $message_normal_background_color;
|
||||
|
||||
public static $message_low_text_color;
|
||||
public static $message_low_background_color;
|
||||
|
||||
public static function init() {
|
||||
self::$defaults = array(
|
||||
'message_enabled' => '1',
|
||||
'message_position' => 'bottom',
|
||||
'message_container' => '1200',
|
||||
'message_transition_duration' => '250',
|
||||
'message_dismiss_duration' => 'day',
|
||||
'message_animation' => 'slide',
|
||||
|
||||
'message_high_text_color' => 'rgba(255, 255, 255, 1)',
|
||||
'message_high_background_color' => 'rgba(220, 53, 69, 1)',
|
||||
|
||||
'message_normal_text_color' => 'rgba(52, 58, 64, 1)',
|
||||
'message_normal_background_color' => 'rgba(255, 255, 255, 1)',
|
||||
|
||||
'message_low_text_color' => 'rgba(255, 255, 255, 1)',
|
||||
'message_low_background_color' => 'rgba(23, 162, 184, 1)',
|
||||
);
|
||||
}
|
||||
|
||||
public static function get($key) {
|
||||
if (!property_exists(__CLASS__, $key)) return false;
|
||||
|
||||
switch ($key) {
|
||||
/* // Constant properties
|
||||
case 'readonly':
|
||||
return false;
|
||||
*/
|
||||
|
||||
default:
|
||||
$options = get_option('ogrealert_options');
|
||||
|
||||
if (isset($options["ogrealert_settings_{$key}"])) {
|
||||
return $options["ogrealert_settings_{$key}"];
|
||||
} else if (isset(self::$defaults[$key])) {
|
||||
return self::$defaults[$key];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function set($key, $value) {
|
||||
if (!property_exists(__CLASS__, $key)) return false;
|
||||
|
||||
switch ($key) {
|
||||
/* // Read-only properties
|
||||
case 'readonly':
|
||||
break;
|
||||
*/
|
||||
|
||||
default:
|
||||
$options = get_option('ogrealert_options');
|
||||
if (!is_array($options)) $options = array();
|
||||
$options["ogrealert_settings_{$key}"] = $value;
|
||||
update_option('ogrealert_options', $options);
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function get_default($key) {
|
||||
if (!property_exists(__CLASS__, $key)) return null;
|
||||
|
||||
if (!array_key_exists($key, self::$defaults)) return null;
|
||||
|
||||
return self::$defaults[$key];
|
||||
}
|
||||
|
||||
public static function parse_hex_color($hex) {
|
||||
// Check if empty
|
||||
if (!isset($hex) || trim($hex) == '') return false;
|
||||
|
||||
// Validate with Regex
|
||||
$regex = '/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/';
|
||||
if (preg_match($regex, $hex) != 1) return false;
|
||||
|
||||
$dec = array('r' => 0, 'g' => 0, 'b' => 0);
|
||||
|
||||
if (strlen($hex) == strlen('#000')) {
|
||||
$dec['r'] = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
|
||||
$dec['g'] = hexdec(substr($hex, 2, 1) . substr($hex, 1, 1));
|
||||
$dec['b'] = hexdec(substr($hex, 3, 1) . substr($hex, 1, 1));
|
||||
} else {
|
||||
$dec['r'] = hexdec(substr($hex, 1, 2));
|
||||
$dec['g'] = hexdec(substr($hex, 3, 2));
|
||||
$dec['b'] = hexdec(substr($hex, 5, 2));
|
||||
}
|
||||
|
||||
return $dec;
|
||||
}
|
||||
|
||||
public static function parse_dismiss_duration($duration) {
|
||||
if (is_numeric($duration)) return intval($duration);
|
||||
|
||||
if (!is_string($duration)) $duration = self::get_default('message_dismiss_duration');
|
||||
|
||||
switch ($duration) {
|
||||
case 'none':
|
||||
return -1;
|
||||
case 'page':
|
||||
return 0;
|
||||
case 'minute':
|
||||
return MINUTE_IN_SECONDS;
|
||||
case 'hour':
|
||||
return HOUR_IN_SECONDS;
|
||||
case 'day':
|
||||
return DAY_IN_SECONDS;
|
||||
case 'week':
|
||||
return WEEK_IN_SECONDS;
|
||||
case 'month':
|
||||
return MONTH_IN_SECONDS;
|
||||
case 'year':
|
||||
return YEAR_IN_SECONDS;
|
||||
default:
|
||||
return HOUR_IN_SECONDS;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class SettingsPage {
|
||||
|
||||
public static function init() {
|
||||
add_action('admin_init', array(__CLASS__, 'register_settings'));
|
||||
add_action('admin_menu', array(__CLASS__, 'menu'));
|
||||
}
|
||||
|
||||
static function menu() {
|
||||
if (!current_user_can(\OgreAlert\Settings::get('capability'))) return;
|
||||
|
||||
$page = add_submenu_page('edit.php?post_type=alert', __('OgreAlert Settings', 'ogrealert'), __('Settings', 'ogrealert'), \OgreAlert\Settings::get('capability'), 'ogrealert_settings', array(__CLASS__, 'page'));
|
||||
add_action('load-' . $page, array(__CLASS__, 'load_page'));
|
||||
}
|
||||
|
||||
static function load_page() {
|
||||
if (!current_user_can(\OgreAlert\Settings::get('capability'))) return;
|
||||
|
||||
add_action('admin_enqueue_scripts', array(__CLASS__, 'admin_enqueue_scripts'));
|
||||
}
|
||||
|
||||
static function admin_enqueue_scripts() {
|
||||
if (!wp_style_is('wp-color-picker', 'enqueued')) {
|
||||
wp_enqueue_style('wp-color-picker');
|
||||
}
|
||||
wp_enqueue_script('ogrealert-settings', \OgreAlert\Settings::get('dir') . 'assets/js/settings.js', array('jquery', 'wp-color-picker'), \OgreAlert\Settings::get('version'), true);
|
||||
}
|
||||
|
||||
static function register_settings() {
|
||||
register_setting('ogrealert', 'ogrealert_options');
|
||||
|
||||
// Message Settings
|
||||
|
||||
add_settings_section('ogrealert_section_general', __('General Settings', 'ogrealert'), array(__CLASS__, 'section'), 'ogrealert');
|
||||
|
||||
add_settings_field('ogrealert_message_enabled', __('Enable Alerts', 'ogrealert'), array(__CLASS__, 'field_checkbox'), 'ogrealert', 'ogrealert_section_general', array(
|
||||
'id' => 'ogrealert_settings_message_enabled',
|
||||
'default' => \OgreAlert\Settings::get_default('message_enabled'),
|
||||
));
|
||||
|
||||
add_settings_field('ogrealert_message_position', __('Position', 'ogrealert'), array(__CLASS__, 'field_select'), 'ogrealert', 'ogrealert_section_general', array(
|
||||
'id' => 'ogrealert_settings_message_position',
|
||||
'options' => array(
|
||||
'top' => __('Fixed Top', 'ogrealert'),
|
||||
'bottom' => __('Fixed Bottom', 'ogrealert'),
|
||||
'footer' => __('After Footer', 'ogrealert'),
|
||||
'custom' => __('Custom', 'ogrealert'),
|
||||
),
|
||||
'default' => \OgreAlert\Settings::get_default('message_position'),
|
||||
'description' => __('If you choose Custom, you must add "do_action(\'display_alerts\');" to a suitable location in your template files.', 'ogrealert'),
|
||||
));
|
||||
|
||||
add_settings_field('ogrealert_message_container', __('Container Width', 'ogrealert'), array(__CLASS__, 'field_text'), 'ogrealert', 'ogrealert_section_general', array(
|
||||
'id' => 'ogrealert_settings_message_container',
|
||||
'description' => 'A positive integer in pixels.',
|
||||
'placeholder' => \OgreAlert\Settings::get_default('message_container'),
|
||||
'default' => \OgreAlert\Settings::get_default('message_container'),
|
||||
));
|
||||
|
||||
add_settings_field('ogrealert_message_transition_duration', __('Transition Duration', 'ogrealert'), array(__CLASS__, 'field_text'), 'ogrealert', 'ogrealert_section_general', array(
|
||||
'id' => 'ogrealert_settings_message_transition_duration',
|
||||
'description' => 'A positive integer in milliseconds.',
|
||||
'placeholder' => \OgreAlert\Settings::get_default('message_transition_duration'),
|
||||
'default' => \OgreAlert\Settings::get_default('message_transition_duration'),
|
||||
));
|
||||
|
||||
add_settings_field('ogrealert_message_transition_animation', __('Transition Animation', 'ogrealert'), array(__CLASS__, 'field_select'), 'ogrealert', 'ogrealert_section_general', array(
|
||||
'id' => 'ogrealert_settings_message_transition_animation',
|
||||
'options' => array(
|
||||
'slide' => __('Slide', 'ogrealert'),
|
||||
'fade' => __('Fade', 'ogrealert'),
|
||||
'custom' => __('Custom', 'ogrealert'),
|
||||
),
|
||||
'default' => \OgreAlert\Settings::get_default('message_transition_animation'),
|
||||
'description' => __('If you choose Custom, the animation must be handled with the "ogrealert-message-transition-custom" class in css.', 'ogrealert'),
|
||||
));
|
||||
|
||||
add_settings_field('ogrealert_message_dismiss_duration', __('Default Dismiss Duration', 'ogrealert'), array(__CLASS__, 'field_select'), 'ogrealert', 'ogrealert_section_general', array(
|
||||
'id' => 'ogrealert_settings_message_dismiss_duration',
|
||||
'options' => array(
|
||||
'none' => __('None', 'ogrealert'),
|
||||
'page' => __('Page', 'ogrealert'),
|
||||
'minute' => __('Minute', 'ogrealert'),
|
||||
'hour' => __('Hour', 'ogrealert'),
|
||||
'day' => __('Day', 'ogrealert'),
|
||||
'week' => __('Week', 'ogrealert'),
|
||||
'month' => __('Month', 'ogrealert'),
|
||||
'year' => __('Year', 'ogrealert'),
|
||||
),
|
||||
'default' => \OgreAlert\Settings::get_default('message_dismiss_duration'),
|
||||
));
|
||||
|
||||
add_settings_section('ogrealert_section_high', __('High Priority', 'ogrealert'), array(__CLASS__, 'section'), 'ogrealert');
|
||||
|
||||
add_settings_field('ogrealert_message_high_text_color', __('Text Color', 'ogrealert'), array(__CLASS__, 'field_color'), 'ogrealert', 'ogrealert_section_high', array(
|
||||
'id' => 'ogrealert_settings_message_high_text_color',
|
||||
'default' => \OgreAlert\Settings::get_default('message_high_text_color'),
|
||||
));
|
||||
|
||||
add_settings_field('ogrealert_message_high_background_color', __('Background Color', 'ogrealert'), array(__CLASS__, 'field_color'), 'ogrealert', 'ogrealert_section_high', array(
|
||||
'id' => 'ogrealert_settings_message_high_background_color',
|
||||
'default' => \OgreAlert\Settings::get_default('message_high_background_color'),
|
||||
));
|
||||
|
||||
add_settings_section('ogrealert_section_normal', __('Normal Priority', 'ogrealert'), array(__CLASS__, 'section'), 'ogrealert');
|
||||
|
||||
add_settings_field('ogrealert_message_normal_text_color', __('Text Color', 'ogrealert'), array(__CLASS__, 'field_color'), 'ogrealert', 'ogrealert_section_normal', array(
|
||||
'id' => 'ogrealert_settings_message_normal_text_color',
|
||||
'default' => \OgreAlert\Settings::get_default('message_normal_text_color'),
|
||||
));
|
||||
|
||||
add_settings_field('ogrealert_message_normal_background_color', __('Background Color', 'ogrealert'), array(__CLASS__, 'field_color'), 'ogrealert', 'ogrealert_section_normal', array(
|
||||
'id' => 'ogrealert_settings_message_normal_background_color',
|
||||
'default' => \OgreAlert\Settings::get_default('message_normal_background_color'),
|
||||
));
|
||||
|
||||
add_settings_section('ogrealert_section_low', __('Low Priority', 'ogrealert'), array(__CLASS__, 'section'), 'ogrealert');
|
||||
|
||||
add_settings_field('ogrealert_message_low_text_color', __('Text Color', 'ogrealert'), array(__CLASS__, 'field_color'), 'ogrealert', 'ogrealert_section_low', array(
|
||||
'id' => 'ogrealert_settings_message_low_text_color',
|
||||
'default' => \OgreAlert\Settings::get_default('message_low_text_color'),
|
||||
));
|
||||
|
||||
add_settings_field('ogrealert_message_low_background_color', __('Background Color', 'ogrealert'), array(__CLASS__, 'field_color'), 'ogrealert', 'ogrealert_section_low', array(
|
||||
'id' => 'ogrealert_settings_message_low_background_color',
|
||||
'default' => \OgreAlert\Settings::get_default('message_low_background_color'),
|
||||
));
|
||||
}
|
||||
|
||||
static function page() {
|
||||
if (!current_user_can(\OgreAlert\Settings::get('capability'))) return;
|
||||
|
||||
// Show error/update messages
|
||||
settings_errors('ogrealert_messages');
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1><?php esc_html_e(get_admin_page_title()); ?></h1>
|
||||
<form action="options.php" method="post"><?php
|
||||
settings_fields('ogrealert');
|
||||
do_settings_sections('ogrealert');
|
||||
submit_button(__('Save Settings', 'ogrealert'));
|
||||
?></form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
// Sections
|
||||
|
||||
public static function section($args) {
|
||||
// <p>Description</p>
|
||||
}
|
||||
|
||||
// Fields
|
||||
|
||||
public static function field_text($args) {
|
||||
$value = '';
|
||||
$options = get_option('ogrealert_options');
|
||||
if (isset($args['default'])) $value = $args['default'];
|
||||
if (isset($args['id']) && !empty($args['id']) && isset($options[$args['id']]) && !empty($options[$args['id']])) $value = $options[$args['id']];
|
||||
|
||||
$type = 'text';
|
||||
if (isset($args['type']) && !empty($args['type'])) {
|
||||
$type = $args['type'];
|
||||
}
|
||||
|
||||
echo '<input class="regular-text" type="' . $type . '" value="' . esc_attr($value) . '"';
|
||||
|
||||
if (isset($args['id']) && !empty($args['id'])) {
|
||||
echo ' id="' . esc_attr($args['id']) . '"';
|
||||
echo ' name="ogrealert_options[' . esc_attr($args['id']) . ']"';
|
||||
}
|
||||
if (isset($args['placeholder']) && !empty($args['placeholder'])) {
|
||||
echo ' placeholder="' . esc_attr($args['placeholder']) . '"';
|
||||
}
|
||||
|
||||
echo ' />';
|
||||
|
||||
if (isset($args['description']) && !empty($args['description'])) {
|
||||
echo '<p class="description">';
|
||||
_e($args['description'], 'ogrealert');
|
||||
echo '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
public static function field_checkbox($args) {
|
||||
$value = false;
|
||||
$options = get_option('ogrealert_options');
|
||||
if (isset($args['default'])) $value = $args['default'];
|
||||
if (isset($args['id']) && !empty($args['id']) && isset($options[$args['id']]) && !empty($options[$args['id']])) $value = $options[$args['id']];
|
||||
|
||||
echo '<input type="checkbox" value="1" type="checkbox" ' . checked($value, 1, false);
|
||||
|
||||
if (isset($args['id']) && !empty($args['id'])) {
|
||||
echo ' id="' . esc_attr($args['id']) . '"';
|
||||
echo ' name="ogrealert_options[' . esc_attr($args['id']) . ']"';
|
||||
}
|
||||
echo ' />';
|
||||
|
||||
if (isset($args['description']) && !empty($args['description'])) {
|
||||
echo '<p class="description">';
|
||||
esc_html_e($args['description'], 'ogrealert');
|
||||
echo '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
public static function field_wysiwyg($args) {
|
||||
if (!isset($args['id']) || empty($args['id'])) return;
|
||||
|
||||
$value = '';
|
||||
$options = get_option('ogrealert_options');
|
||||
if (isset($args['default'])) $value = $args['default'];
|
||||
if (isset($options[$args['id']]) && !empty($options[$args['id']])) $value = $options[$args['id']];
|
||||
|
||||
$settings = array(
|
||||
'tinymce' => true,
|
||||
'textarea_name' => 'ogrealert_options[' . $args['id'] . ']',
|
||||
'textarea_rows' => 15,
|
||||
'tabindex' => 1,
|
||||
);
|
||||
wp_editor($value, $args['id'], $settings);
|
||||
|
||||
if (isset($args['description']) && !empty($args['description'])) {
|
||||
echo '<p class="description">';
|
||||
esc_html_e($args['description'], 'ogrealert');
|
||||
echo '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
public static function field_select($args) {
|
||||
if (!isset($args['id']) || empty($args['id'])) return;
|
||||
|
||||
$selected = '';
|
||||
$options = get_option('ogrealert_options');
|
||||
if (isset($args['default'])) $selected = $args['default'];
|
||||
if (isset($options[$args['id']]) && !empty($options[$args['id']])) $selected = $options[$args['id']];
|
||||
|
||||
echo '<select id="' . esc_attr($args['id']) . '" name="ogrealert_options[' . esc_attr($args['id']) . ']"';
|
||||
if (isset($args['class']) && !empty($args['class'])) echo ' class="' . esc_attr($args['class']) . '"';
|
||||
if (isset($args['required']) && $args['required'] == true) echo ' required="required"';
|
||||
echo '>';
|
||||
|
||||
if (isset($args['placeholder']) && !empty($args['placeholder'])) {
|
||||
echo '<option value="">' . esc_html($args['placeholder']) . '</option>';
|
||||
}
|
||||
|
||||
foreach ($args['options'] as $value => $title) {
|
||||
echo '<option value="' . esc_attr($value) . '" ' . selected($selected, $value, false) . '>' . esc_html($title) . '</option>';
|
||||
}
|
||||
|
||||
echo '</select>';
|
||||
|
||||
if (isset($args['description']) && !empty($args['description'])) {
|
||||
echo '<p class="description">';
|
||||
_e($args['description'], 'ogrealert');
|
||||
echo '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
public static function field_color($args) {
|
||||
$value = '';
|
||||
$options = get_option('ogrealert_options');
|
||||
if (isset($args['default'])) $value = $args['default'];
|
||||
if (isset($args['id']) && !empty($args['id']) && isset($options[$args['id']])) $value = $options[$args['id']];
|
||||
|
||||
echo '<input class="ogrealert-color-picker" type="text" value="' . esc_attr($value) . '"';
|
||||
|
||||
if (isset($args['id']) && !empty($args['id'])) {
|
||||
echo ' id="' . esc_attr($args['id']) . '"';
|
||||
echo ' name="ogrealert_options[' . esc_attr($args['id']) . ']"';
|
||||
}
|
||||
|
||||
if (isset($args['placeholder']) && !empty($args['placeholder'])) {
|
||||
echo ' placeholder="' . esc_attr($args['placeholder']) . '"';
|
||||
}
|
||||
|
||||
echo ' />';
|
||||
|
||||
if (isset($args['description']) && !empty($args['description'])) {
|
||||
echo '<p class="description">';
|
||||
_e($args['description'], 'ogrealert');
|
||||
echo '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
\OgreAlert\SettingsPage::init();
|
||||
Reference in New Issue
Block a user