| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* collect and display admin notices for failed prerequisites |
| 9 |
*/ |
| 10 |
class DisableEmailsRequires { |
| 11 |
|
| 12 |
private $notices; |
| 13 |
|
| 14 |
/** |
| 15 |
* set up the notices container, and hook actions for displaying notices |
| 16 |
* only called when a notice is being added |
| 17 |
*/ |
| 18 |
protected function init() { |
| 19 |
if (!is_array($this->notices)) { |
| 20 |
$this->notices = array(); |
| 21 |
|
| 22 |
// hook admin_notices, again! so need to hook later than 10 |
| 23 |
add_action('admin_notices', array($this, 'maybeShowAdminNotices'), 20); |
| 24 |
|
| 25 |
// show Requires notices before update information, so hook earlier than 10 |
| 26 |
add_action('after_plugin_row_' . DISABLE_EMAILS_PLUGIN_NAME, array($this, 'showPluginRowNotices'), 9, 2); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* add a Requires notices |
| 32 |
* @param string $notice |
| 33 |
*/ |
| 34 |
public function addNotice($notice) { |
| 35 |
$this->init(); |
| 36 |
$this->notices[] = $notice; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* maybe show admin notices, if on an appropriate admin page with admin or similar logged in |
| 41 |
*/ |
| 42 |
public function maybeShowAdminNotices() { |
| 43 |
if ($this->canShowAdminNotices()) { |
| 44 |
$notices = $this->notices; |
| 45 |
require DISABLE_EMAILS_PLUGIN_ROOT . 'views/requires-admin-notice.php'; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* show plugin page row with requires notices |
| 51 |
*/ |
| 52 |
public function showPluginRowNotices() { |
| 53 |
global $wp_list_table; |
| 54 |
|
| 55 |
if (empty($wp_list_table)) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$notices = $this->notices; |
| 60 |
require DISABLE_EMAILS_PLUGIN_ROOT . 'views/requires-plugin-notice.php'; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* test whether we can show admin-related notices |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
protected function canShowAdminNotices() { |
| 68 |
$current_screen = get_current_screen(); |
| 69 |
|
| 70 |
// only on specific pages |
| 71 |
if (!$current_screen || $current_screen->id !== 'dashboard') { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
// only bother admins / plugin installers / option setters with this stuff |
| 76 |
if (!current_user_can('activate_plugins') && !current_user_can('manage_options')) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|