| 1 |
<?php |
| 2 |
// powerpressadmin-notifications.php |
| 3 |
|
| 4 |
// Notice message manager for PowerPress |
| 5 |
|
| 6 |
// Inspired by the notifications in Yoast's SEO |
| 7 |
// Also inspired from https://premium.wpmudev.org/blog/adding-admin-notices/ |
| 8 |
// For the sake of keeping things simple, we will only include this class when we need to display notifications |
| 9 |
|
| 10 |
class PowerPress_Notification { |
| 11 |
|
| 12 |
private $settings = array(); |
| 13 |
|
| 14 |
public function __construct($content, $settings = array() ) |
| 15 |
{ |
| 16 |
$defaults = array(); |
| 17 |
$defaults['content'] = ''; |
| 18 |
$defaults['type'] = 'updated'; |
| 19 |
$defaults['id'] = ''; |
| 20 |
$this->settings = wp_parse_args( $settings, $defaults ); |
| 21 |
if( !empty($content) ) |
| 22 |
$this->settings['content'] = $content; |
| 23 |
if( empty($this->settings['content']) ) |
| 24 |
$this->settings['id'] = crc32($this->settings['content']); // Not ideal, but with no ID we need some unique value |
| 25 |
} |
| 26 |
|
| 27 |
public function get_notification_as_string() |
| 28 |
{ |
| 29 |
$html = '<div class="powerpress-notice notice is-dismissible ' . esc_attr( $this->settings['type'] ) . '" id="powerpress-notice-'. esc_attr( $this->settings['id'] ) .'">'; |
| 30 |
$html .= wpautop( $this->settings['content'] ); |
| 31 |
|
| 32 |
if( version_compare($GLOBALS['wp_version'], '4.2', '<' ) ) { |
| 33 |
$html .= '<p> <a style="float:right;" href="#" class="notice-dismiss-link">'. __('Dismiss', 'powerpress') .'</a></p>'; |
| 34 |
} |
| 35 |
$html .= '</div>' . PHP_EOL; |
| 36 |
return $html; |
| 37 |
} |
| 38 |
|
| 39 |
}; |
| 40 |
|
| 41 |
class PowerPress_Notification_Manager { |
| 42 |
|
| 43 |
private $notifications = array(); |
| 44 |
private $dismissedNotifications = array(); |
| 45 |
|
| 46 |
public function __construct() |
| 47 |
{ |
| 48 |
$this->dismissedNotifications = get_option('powerpress_dismissed_notices'); |
| 49 |
add_action( 'all_admin_notices', array( $this, 'all_admin_notices' ) ); |
| 50 |
add_action('wp_ajax_powerpress_notice_dismiss', array($this, 'wp_ajax_powerpress_notice_dismiss') ); |
| 51 |
add_action('admin_head', array($this, 'admin_head') ); |
| 52 |
} |
| 53 |
|
| 54 |
// this will print the notification as a normal string, rather than a bar at the top of the page |
| 55 |
public function print_one_notice($id, $ignore_dismiss = false) { |
| 56 |
echo "<div class='pp-sidebar-notification'>"; |
| 57 |
if (!empty($this->notifications[$id])) { |
| 58 |
echo str_replace(array('powerpress-notice ', 'notice ', 'updated'), array('', '', ''), $this->notifications[$id]->get_notification_as_string()); |
| 59 |
} else if ($ignore_dismiss) { |
| 60 |
echo str_replace(array('powerpress-notice ', 'notice ', 'updated'), array('', '', ''), $this->dismissedNotifications[$id]); |
| 61 |
} |
| 62 |
echo "</div>"; |
| 63 |
} |
| 64 |
|
| 65 |
public function all_admin_notices() |
| 66 |
{ |
| 67 |
foreach( $this->notifications as $key => $notification ) |
| 68 |
{ |
| 69 |
echo $notification->get_notification_as_string(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function wp_ajax_powerpress_notice_dismiss() |
| 74 |
{ |
| 75 |
// Check for nonce security |
| 76 |
if (!isset($_POST['nonce'])) { |
| 77 |
exit; |
| 78 |
} |
| 79 |
if ( ! wp_verify_nonce( $_POST['nonce'], 'powerpress-notice-dismiss' ) ) { |
| 80 |
exit; |
| 81 |
} |
| 82 |
$dismiss_notice_id = $_POST['dismiss_notice_id']; |
| 83 |
preg_match('/^powerpress-notice-(.*)$/i', $dismiss_notice_id, $match ); |
| 84 |
if( empty($match[1]) ) |
| 85 |
die('-1'); |
| 86 |
|
| 87 |
$DismissedNotifications = get_option('powerpress_dismissed_notices'); |
| 88 |
if( !is_array($DismissedNotifications) ) |
| 89 |
$DismissedNotifications = array(); |
| 90 |
$DismissedNotifications[ $match[1] ] = $this->notifications[$match[1]]->get_notification_as_string(); |
| 91 |
update_option('powerpress_dismissed_notices', $DismissedNotifications); |
| 92 |
die('1'); |
| 93 |
} |
| 94 |
|
| 95 |
function admin_head() |
| 96 |
{ |
| 97 |
if( count($this->notifications) > 0 ) // If there are notices to print, then lets also put in the ajax to clear them |
| 98 |
{ |
| 99 |
if( version_compare($GLOBALS['wp_version'], '4.2', '>=' ) ) { |
| 100 |
?> |
| 101 |
<script type="text/javascript"><!-- |
| 102 |
|
| 103 |
jQuery(document).ready( function() { |
| 104 |
|
| 105 |
jQuery(document).on( 'click', '.powerpress-notice .notice-dismiss', function() { |
| 106 |
|
| 107 |
var dismissId = jQuery(this).closest('.powerpress-notice').attr('id'); |
| 108 |
jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', {action:"powerpress_notice_dismiss", dismiss_notice_id: dismissId, nonce: '<?php echo wp_create_nonce('powerpress-notice-dismiss'); ?>' }); |
| 109 |
}); |
| 110 |
}); |
| 111 |
|
| 112 |
--></script> |
| 113 |
<?php |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
?> |
| 118 |
<script type="text/javascript"><!-- |
| 119 |
|
| 120 |
jQuery(document).ready( function() { |
| 121 |
|
| 122 |
jQuery(document).on( 'click', '.powerpress-notice .notice-dismiss-link', function(e) { |
| 123 |
e.preventDefault(); |
| 124 |
var dismissId = jQuery(this).closest('.powerpress-notice').attr('id'); |
| 125 |
jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', {action:"powerpress_notice_dismiss", dismiss_notice_id: dismissId, nonce: '<?php echo wp_create_nonce('powerpress-notice-dismiss'); ?>' }); |
| 126 |
jQuery(this).closest('.powerpress-notice').hide(); // Hide the div now we dismissed it |
| 127 |
}); |
| 128 |
}); |
| 129 |
|
| 130 |
--></script> |
| 131 |
<?php |
| 132 |
} |
| 133 |
?> |
| 134 |
<style> |
| 135 |
.powerpress-notice a { |
| 136 |
text-decoration: underline; |
| 137 |
} |
| 138 |
</style> |
| 139 |
<?php |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
public function dismissed_status($notification_id) |
| 144 |
{ |
| 145 |
if( !empty($this->dismissedNotifications[ $notification_id ]) ) |
| 146 |
return true; |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
public function add($notification_id, $notification_content) |
| 151 |
{ |
| 152 |
if( !$this->dismissed_status($notification_id) ) { |
| 153 |
$this->notifications[$notification_id] = new PowerPress_Notification($notification_content, array('id'=>$notification_id) ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
}; |
| 158 |
|
| 159 |
function powerpressadmin_notifications_appropriate() |
| 160 |
{ |
| 161 |
// Any powerpress page |
| 162 |
if( preg_match('/wp-admin\/.*powerpress/', $_SERVER['REQUEST_URI']) ) |
| 163 |
return true; |
| 164 |
|
| 165 |
// Dashboard is acceptable: |
| 166 |
if( preg_match('/wp-admin\/(index\.php)?$/', $_SERVER['REQUEST_URI']) ) |
| 167 |
return true; |
| 168 |
|
| 169 |
// edit posts (pages, post types, etc...) |
| 170 |
if( preg_match('/wp-admin\/edit\.php/', $_SERVER['REQUEST_URI']) ) |
| 171 |
return true; |
| 172 |
|
| 173 |
// managing plugins |
| 174 |
if( preg_match('/wp-admin\/plugins\.php/', $_SERVER['REQUEST_URI']) ) |
| 175 |
return true; |
| 176 |
|
| 177 |
// managing themes |
| 178 |
if( preg_match('/wp-admin\/themes\.php/', $_SERVER['REQUEST_URI']) ) |
| 179 |
return true; |
| 180 |
|
| 181 |
// Required so we can dismiss notices |
| 182 |
if( preg_match('/wp-admin\/admin-ajax\.php/', $_SERVER['REQUEST_URI']) ) |
| 183 |
return true; |
| 184 |
|
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
// eof |