| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Hide WP Admin Notifications |
| 4 |
* Description: Hides WordPress admin notices until you choose to show them again from Settings. |
| 5 |
* Version: 0.2.3 |
| 6 |
* Author: Yoda Of WP |
| 7 |
* Requires at least: 5.2 |
| 8 |
* Tested up to: 7.0 |
| 9 |
* Requires PHP: 7.0 |
| 10 |
* Text Domain: hide-wp-admin-notifications |
| 11 |
* License: GPLv2 or later |
| 12 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 13 |
*/ |
| 14 |
|
| 15 |
// Prevent direct access |
| 16 |
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' ); |
| 17 |
|
| 18 |
define( 'HWAN_PLUGIN_VERSION', '0.2.3' ); |
| 19 |
define( 'HWAN_OPTION_SHOW_NOTICES', 'hwan_show_dashboard_notices' ); |
| 20 |
define( 'HWAN_OPTION_PLUGIN_VERSION', 'hwan_plugin_version' ); |
| 21 |
define( 'HWAN_SETTINGS_PAGE', 'hide-wp-admin-notifications' ); |
| 22 |
define( 'HWAN_SETTINGS_GROUP', 'hwan_settings' ); |
| 23 |
|
| 24 |
// Ensure option exists for seamless version updates |
| 25 |
function hwan_ensure_option_exists() { |
| 26 |
if ( false === get_option( HWAN_OPTION_SHOW_NOTICES ) ) { |
| 27 |
add_option( HWAN_OPTION_SHOW_NOTICES, 'no', '', false ); |
| 28 |
} |
| 29 |
|
| 30 |
if ( false === get_option( HWAN_OPTION_PLUGIN_VERSION ) ) { |
| 31 |
add_option( HWAN_OPTION_PLUGIN_VERSION, HWAN_PLUGIN_VERSION, '', false ); |
| 32 |
} |
| 33 |
} |
| 34 |
register_activation_hook( __FILE__, 'hwan_ensure_option_exists' ); |
| 35 |
|
| 36 |
// Maintain existing setting during updates |
| 37 |
function hwan_update_check() { |
| 38 |
if ( get_option( HWAN_OPTION_PLUGIN_VERSION ) !== HWAN_PLUGIN_VERSION ) { |
| 39 |
hwan_ensure_option_exists(); |
| 40 |
update_option( HWAN_OPTION_PLUGIN_VERSION, HWAN_PLUGIN_VERSION, false ); |
| 41 |
} |
| 42 |
} |
| 43 |
add_action( 'plugins_loaded', 'hwan_update_check' ); |
| 44 |
|
| 45 |
// Get the current notice visibility setting. |
| 46 |
function hwan_get_show_dashboard_notices() { |
| 47 |
return get_option( HWAN_OPTION_SHOW_NOTICES, 'no' ); |
| 48 |
} |
| 49 |
|
| 50 |
// Remove admin notices based on the saved setting. |
| 51 |
function hwan_remove_admin_notices() { |
| 52 |
$show_notices = hwan_get_show_dashboard_notices(); |
| 53 |
|
| 54 |
if ( hwan_is_settings_page() ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if ( 'yes' !== $show_notices ) { |
| 59 |
global $wp_filter; |
| 60 |
|
| 61 |
foreach ( array( 'user_admin_notices', 'admin_notices', 'all_admin_notices', 'network_admin_notices' ) as $hook_name ) { |
| 62 |
if ( isset( $wp_filter[ $hook_name ] ) && $wp_filter[ $hook_name ] instanceof WP_Hook ) { |
| 63 |
$wp_filter[ $hook_name ]->remove_all_filters(); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
add_action( 'in_admin_header', 'hwan_remove_admin_notices', 0 ); |
| 69 |
|
| 70 |
// Check whether the current admin screen is this plugin's settings page. |
| 71 |
function hwan_is_settings_page() { |
| 72 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; |
| 73 |
|
| 74 |
return $screen && 'settings_page_' . HWAN_SETTINGS_PAGE === $screen->id; |
| 75 |
} |
| 76 |
|
| 77 |
// Add plugin settings page |
| 78 |
function hwan_admin_menu() { |
| 79 |
add_options_page( |
| 80 |
__( 'Hide WP Admin Notifications', 'hide-wp-admin-notifications' ), |
| 81 |
__( 'Hide WP Admin Notifications', 'hide-wp-admin-notifications' ), |
| 82 |
'manage_options', |
| 83 |
HWAN_SETTINGS_PAGE, |
| 84 |
'hwan_options_page' |
| 85 |
); |
| 86 |
} |
| 87 |
add_action( 'admin_menu', 'hwan_admin_menu' ); |
| 88 |
|
| 89 |
// Register settings and fields |
| 90 |
function hwan_settings_init() { |
| 91 |
register_setting( |
| 92 |
HWAN_SETTINGS_GROUP, |
| 93 |
HWAN_OPTION_SHOW_NOTICES, |
| 94 |
array( |
| 95 |
'sanitize_callback' => 'hwan_sanitize_checkbox', |
| 96 |
'default' => 'no', |
| 97 |
) |
| 98 |
); |
| 99 |
|
| 100 |
add_settings_section( |
| 101 |
'hwan_settings_section', |
| 102 |
'', |
| 103 |
null, |
| 104 |
HWAN_SETTINGS_PAGE |
| 105 |
); |
| 106 |
|
| 107 |
add_settings_field( |
| 108 |
HWAN_OPTION_SHOW_NOTICES, |
| 109 |
__( 'Show admin notices', 'hide-wp-admin-notifications' ), |
| 110 |
'hwan_show_dashboard_notices_cb', |
| 111 |
HWAN_SETTINGS_PAGE, |
| 112 |
'hwan_settings_section' |
| 113 |
); |
| 114 |
} |
| 115 |
add_action( 'admin_init', 'hwan_settings_init' ); |
| 116 |
|
| 117 |
// Sanitize checkbox value |
| 118 |
function hwan_sanitize_checkbox( $value ) { |
| 119 |
return ( 'yes' === $value ) ? 'yes' : 'no'; |
| 120 |
} |
| 121 |
|
| 122 |
// Show dashboard notices setting field |
| 123 |
function hwan_show_dashboard_notices_cb() { |
| 124 |
$show_notices = hwan_get_show_dashboard_notices(); |
| 125 |
?> |
| 126 |
<fieldset class="hwan-setting-field"> |
| 127 |
<input type="hidden" name="<?php echo esc_attr( HWAN_OPTION_SHOW_NOTICES ); ?>" value="no" /> |
| 128 |
<label class="hwan-checkbox-label" for="<?php echo esc_attr( HWAN_OPTION_SHOW_NOTICES ); ?>"> |
| 129 |
<input |
| 130 |
id="<?php echo esc_attr( HWAN_OPTION_SHOW_NOTICES ); ?>" |
| 131 |
name="<?php echo esc_attr( HWAN_OPTION_SHOW_NOTICES ); ?>" |
| 132 |
type="checkbox" |
| 133 |
value="yes" |
| 134 |
<?php checked( $show_notices, 'yes' ); ?> |
| 135 |
/> |
| 136 |
<span class="checkmark" aria-hidden="true"></span> |
| 137 |
<?php esc_html_e( 'Show admin notices again', 'hide-wp-admin-notifications' ); ?> |
| 138 |
</label> |
| 139 |
<p class="description"> |
| 140 |
<?php esc_html_e( 'Check this box to show admin notices temporarily. Uncheck it to hide them again.', 'hide-wp-admin-notifications' ); ?> |
| 141 |
</p> |
| 142 |
</fieldset> |
| 143 |
<?php |
| 144 |
} |
| 145 |
|
| 146 |
// Display the plugin settings page |
| 147 |
function hwan_options_page() { |
| 148 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 149 |
wp_die( |
| 150 |
esc_html__( |
| 151 |
'You do not have sufficient permissions to access this page.', |
| 152 |
'hide-wp-admin-notifications' |
| 153 |
) |
| 154 |
); |
| 155 |
} |
| 156 |
?> |
| 157 |
<div class="hwan-settings-container"> |
| 158 |
<div class="hwan-header"> |
| 159 |
<svg class="hwan-logo" width="48" height="48" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"> |
| 160 |
<rect width="256" height="256" rx="52" fill="#111111" /> |
| 161 |
<path d="M58 176 C82 176 78 150 80 120 C82 92 100 60 128 60 C156 60 174 92 176 120 C178 150 174 176 198 176 Z" fill="#ffffff" /> |
| 162 |
<rect x="119" y="42" width="18" height="20" rx="9" fill="#ffffff" /> |
| 163 |
<path d="M112 182 C112 202 144 202 144 182 Z" fill="#ffffff" /> |
| 164 |
<line x1="50" y1="44" x2="210" y2="204" stroke="#111111" stroke-width="34" stroke-linecap="round" /> |
| 165 |
<line x1="50" y1="44" x2="210" y2="204" stroke="#ff5600" stroke-width="16" stroke-linecap="round" /> |
| 166 |
</svg> |
| 167 |
<div> |
| 168 |
<p class="hwan-eyebrow"><?php esc_html_e( 'Plugin settings', 'hide-wp-admin-notifications' ); ?></p> |
| 169 |
<h1><?php esc_html_e( 'Hide WP Admin Notifications', 'hide-wp-admin-notifications' ); ?></h1> |
| 170 |
</div> |
| 171 |
</div> |
| 172 |
<div class="hwan-message"> |
| 173 |
<p> |
| 174 |
<?php esc_html_e( 'It is still a good idea to review admin notices from time to time so you do not miss security, update, or maintenance messages.', 'hide-wp-admin-notifications' ); ?> |
| 175 |
</p> |
| 176 |
</div> |
| 177 |
<form method="post" action="options.php"> |
| 178 |
<?php |
| 179 |
settings_fields( HWAN_SETTINGS_GROUP ); |
| 180 |
do_settings_sections( HWAN_SETTINGS_PAGE ); |
| 181 |
submit_button(); |
| 182 |
?> |
| 183 |
</form> |
| 184 |
</div> |
| 185 |
<?php |
| 186 |
} |
| 187 |
|
| 188 |
// Add settings link to the plugin action links |
| 189 |
function hwan_add_settings_link( $links ) { |
| 190 |
$settings_link = sprintf( |
| 191 |
'<a href="%s">%s</a>', |
| 192 |
esc_url( admin_url( 'options-general.php?page=' . HWAN_SETTINGS_PAGE ) ), |
| 193 |
esc_html__( 'Settings', 'hide-wp-admin-notifications' ) |
| 194 |
); |
| 195 |
|
| 196 |
array_unshift( $links, $settings_link ); |
| 197 |
|
| 198 |
return $links; |
| 199 |
} |
| 200 |
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'hwan_add_settings_link' ); |
| 201 |
|
| 202 |
// Enqueue admin styles for the plugin settings page |
| 203 |
function hwan_enqueue_admin_styles( $hook ) { |
| 204 |
$is_settings_page = 'settings_page_' . HWAN_SETTINGS_PAGE === $hook; |
| 205 |
|
| 206 |
if ( ! $is_settings_page ) { |
| 207 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; |
| 208 |
$is_settings_page = $screen && 'settings_page_' . HWAN_SETTINGS_PAGE === $screen->id; |
| 209 |
} |
| 210 |
|
| 211 |
if ( ! $is_settings_page ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
$stylesheet_path = plugin_dir_path( __FILE__ ) . 'admin.css'; |
| 216 |
$stylesheet_version = file_exists( $stylesheet_path ) ? filemtime( $stylesheet_path ) : HWAN_PLUGIN_VERSION; |
| 217 |
|
| 218 |
wp_enqueue_style( |
| 219 |
'hwan-admin-styles', |
| 220 |
plugins_url( 'admin.css', __FILE__ ), |
| 221 |
array(), |
| 222 |
$stylesheet_version |
| 223 |
); |
| 224 |
} |
| 225 |
add_action( 'admin_enqueue_scripts', 'hwan_enqueue_admin_styles' ); |
| 226 |
|