| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* Enqueues/displays administrative notices. |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @package s2Member\Admin_Notices |
| 16 |
* @since 3.5 |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit('Do not access this file directly.'); |
| 20 |
|
| 21 |
if(!class_exists('c_ws_plugin__s2member_admin_notices')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Enqueues/displays administrative notices. |
| 25 |
* |
| 26 |
* @package s2Member\Admin_Notices |
| 27 |
* @since 3.5 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_admin_notices |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Enqueues administrative notices. |
| 33 |
* |
| 34 |
* @package s2Member\Admin_Notices |
| 35 |
* @since 3.5 |
| 36 |
* |
| 37 |
* @param string $notice String value of actual notice *(i.e., the message)*. |
| 38 |
* @param string|array $on_pages Optional. Defaults to any page. String or array of pages to display this notice on. |
| 39 |
* @param bool $error Optional. True if this notice is regarding an error. Defaults to false. |
| 40 |
* @param int $time Optional. Unix timestamp indicating when this notice will be displayed. |
| 41 |
* @param bool $dismiss Optional. If true, the notice will remain persistent, until dismissed. Defaults to false. |
| 42 |
*/ |
| 43 |
public static function enqueue_admin_notice($notice = '', $on_pages = array(), $error = FALSE, $time = 0, $dismiss = FALSE) |
| 44 |
{ |
| 45 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 46 |
do_action('ws_plugin__s2member_before_enqueue_admin_notice', get_defined_vars()); |
| 47 |
unset($__refs, $__v); // Allow variables to be modified by reference. |
| 48 |
|
| 49 |
if($notice && is_string($notice))// Have a valid string. |
| 50 |
{ |
| 51 |
$notices = (array)get_option('ws_plugin__s2member_notices'); |
| 52 |
array_push($notices, array('notice' => $notice, 'on_pages' => $on_pages, 'error' => $error, 'time' => $time, 'dismiss' => $dismiss)); |
| 53 |
|
| 54 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 55 |
do_action('ws_plugin__s2member_during_enqueue_admin_notice', get_defined_vars()); |
| 56 |
unset($__refs, $__v); // Allow variables to be modified by reference. |
| 57 |
|
| 58 |
update_option('ws_plugin__s2member_notices', c_ws_plugin__s2member_utils_arrays::array_unique($notices)); |
| 59 |
} |
| 60 |
do_action('ws_plugin__s2member_after_enqueue_admin_notice', get_defined_vars()); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Displays an administrative notice. |
| 65 |
* |
| 66 |
* @package s2Member\Admin_Notices |
| 67 |
* @since 3.5 |
| 68 |
* |
| 69 |
* @param string $notice String value of actual notice *(i.e., the message)*. |
| 70 |
* @param bool $error Optional. True if this notice is regarding an error. Defaults to false. |
| 71 |
* @param bool $dismiss Optional. If true, the notice will be displayed with a dismissal link. Defaults to false. |
| 72 |
*/ |
| 73 |
public static function display_admin_notice($notice = '', $error = FALSE, $dismiss = FALSE) |
| 74 |
{ |
| 75 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 76 |
do_action('ws_plugin__s2member_before_display_admin_notice', get_defined_vars()); |
| 77 |
unset($__refs, $__v); // Allow variables to be modified by reference. |
| 78 |
|
| 79 |
if($dismiss) $dismissal_link = '<div style="float:right; margin:0 0 0 1em; font-weight:bold;">'. |
| 80 |
'[ <a href="'.esc_attr(add_query_arg('ws-plugin--s2member-dismiss-admin-notice', urlencode(md5($notice)), $_SERVER['REQUEST_URI'])).'">dismiss</a> ]'. |
| 81 |
'</div>'; |
| 82 |
if($notice && is_string($notice) && $error) |
| 83 |
{ |
| 84 |
if($dismiss && !empty($dismissal_link)) |
| 85 |
$notice = $dismissal_link.$notice; |
| 86 |
echo '<div class="notice notice-error"><p>'.wp_kses_post($notice).'</p></div>'; |
| 87 |
} |
| 88 |
else if($notice && is_string($notice)) |
| 89 |
{ |
| 90 |
if($dismiss && !empty($dismissal_link)) |
| 91 |
$notice = $dismissal_link.$notice; |
| 92 |
echo '<div class="notice notice-info"><p>'.wp_kses_post($notice).'</p></div>'; |
| 93 |
} |
| 94 |
do_action('ws_plugin__s2member_after_display_admin_notice', get_defined_vars()); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Processes all administrative notices. |
| 99 |
* |
| 100 |
* @package s2Member\Admin_Notices |
| 101 |
* @since 3.5 |
| 102 |
* |
| 103 |
* @attaches-to ``add_action('admin_notices');`` |
| 104 |
* @attaches-to ``add_action('user_admin_notices');`` |
| 105 |
* @attaches-to ``add_action('network_admin_notices');`` |
| 106 |
* @todo Update to ``add_action('all_admin_notices');``. |
| 107 |
*/ |
| 108 |
public static function admin_notices() |
| 109 |
{ |
| 110 |
global $pagenow; // This holds the current page filename. |
| 111 |
|
| 112 |
do_action('ws_plugin__s2member_before_admin_notices', get_defined_vars()); |
| 113 |
|
| 114 |
if(is_admin() && is_array($notices = get_option('ws_plugin__s2member_notices')) && !empty($notices)) |
| 115 |
{ |
| 116 |
$a = (is_blog_admin()) ? 'blog' : ''; |
| 117 |
$a = (is_user_admin()) ? 'user' : $a; |
| 118 |
$a = (is_network_admin()) ? 'network' : $a; |
| 119 |
$a = (!$a) ? 'blog' : $a; // Default blog admin. |
| 120 |
|
| 121 |
foreach($notices as $i => $notice) // Check several things about each notice. |
| 122 |
{ |
| 123 |
//250510 Fixed for PHP 8.1+: safely normalize on_pages before foreach |
| 124 |
$notice = (array)$notice; |
| 125 |
$notice['on_pages'] = empty($notice['on_pages']) ? array('*') : (array)$notice['on_pages']; |
| 126 |
foreach($notice['on_pages'] as $page) |
| 127 |
{ |
| 128 |
if(!preg_match('/^(.+?)\:/', $page)) // NO prefix? |
| 129 |
$page = 'blog:'.ltrim($page, ':'); // `blog:` |
| 130 |
|
| 131 |
$adms = preg_split('/\|/', preg_replace('/\:(.*)$/i', '', $page)); |
| 132 |
$page = preg_replace('/^([^\:]*)\:/i', '', $page); |
| 133 |
|
| 134 |
if(empty($adms) || in_array('*', $adms) || in_array($a, $adms)) |
| 135 |
if(!$page || '*' === $page || $pagenow === $page || @$_GET['page'] === $page) |
| 136 |
{ |
| 137 |
if(strtotime('now') >= (int)$notice['time']) // Time to show it? |
| 138 |
{ |
| 139 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 140 |
do_action('ws_plugin__s2member_during_admin_notices_before_display', get_defined_vars()); |
| 141 |
unset($__refs, $__v); // Allow variables to be modified by reference. |
| 142 |
|
| 143 |
if(!$notice['dismiss'] || (!empty($_GET['ws-plugin--s2member-dismiss-admin-notice']) && $_GET['ws-plugin--s2member-dismiss-admin-notice'] === md5($notice['notice']))) |
| 144 |
unset($notices[$i]); // Clear this administrative notice now? |
| 145 |
|
| 146 |
if(!$notice['dismiss'] || empty($_GET['ws-plugin--s2member-dismiss-admin-notice']) || $_GET['ws-plugin--s2member-dismiss-admin-notice'] !== md5($notice['notice'])) |
| 147 |
c_ws_plugin__s2member_admin_notices::display_admin_notice($notice['notice'], $notice['error'], $notice['dismiss']); |
| 148 |
|
| 149 |
do_action('ws_plugin__s2member_during_admin_notices_after_display', get_defined_vars()); |
| 150 |
} |
| 151 |
continue 2; // This notice processed; continue. |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
$notices = array_merge($notices); // Re-index array. |
| 156 |
|
| 157 |
foreach(array_keys(get_defined_vars()) as $__v) $__refs[$__v] =& $$__v; |
| 158 |
do_action('ws_plugin__s2member_during_admin_notices', get_defined_vars()); |
| 159 |
unset($__refs, $__v); // Allow variables to be modified by reference. |
| 160 |
|
| 161 |
update_option('ws_plugin__s2member_notices', $notices); |
| 162 |
} |
| 163 |
do_action('ws_plugin__s2member_after_admin_notices', get_defined_vars()); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|