Skip to content

Commit 5b057f5

Browse files
committed
Fix admin notice class.
Semi-consistent display of the admin notice. Fixes #478, #495. Props @Ninos Work around all the changes to the various admin notice classes between WP 4.4 and 3.7 (lowest WP version currently supported by TGMPA).
1 parent 15843bc commit 5b057f5

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

class-tgm-plugin-activation.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,12 +1180,7 @@ public function notices() {
11801180
}
11811181

11821182
// Register the nag messages and prepare them to be processed.
1183-
if ( ! empty( $this->strings['nag_type'] ) ) {
1184-
add_settings_error( 'tgmpa', 'tgmpa', $rendered, sanitize_html_class( strtolower( $this->strings['nag_type'] ) ) );
1185-
} else {
1186-
$nag_class = version_compare( $this->wp_version, '3.8', '<' ) ? 'updated' : 'update-nag';
1187-
add_settings_error( 'tgmpa', 'tgmpa', $rendered, $nag_class );
1188-
}
1183+
add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() );
11891184
}
11901185

11911186
// Admin options pages already output settings_errors, so this is to avoid duplication.
@@ -1194,6 +1189,30 @@ public function notices() {
11941189
}
11951190
}
11961191

1192+
/**
1193+
* Get admin notice class.
1194+
*
1195+
* Work around all the changes to the various admin notice classes between WP 4.4 and 3.7
1196+
* (lowest supported version by TGMPA).
1197+
*
1198+
* @since 2.x.x
1199+
*
1200+
* @return string
1201+
*/
1202+
protected function get_admin_notice_class() {
1203+
if ( ! empty( $this->strings['nag_type'] ) ) {
1204+
return sanitize_html_class( strtolower( $this->strings['nag_type'] ) );
1205+
} else {
1206+
if ( version_compare( $this->wp_version, '4.2', '>=' ) ) {
1207+
return 'notice-warning';
1208+
} elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) {
1209+
return 'notice';
1210+
} else {
1211+
return 'updated';
1212+
}
1213+
}
1214+
}
1215+
11971216
/**
11981217
* Display settings errors and remove those which have been displayed to avoid duplicate messages showing
11991218
*

example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ function my_theme_register_required_plugins() {
191191
'complete' => __( 'All plugins installed and activated successfully. %1$s', 'theme-slug' ), // %s = dashboard link.
192192
'contact_admin' => __( 'Please contact the administrator of this site for help.', 'tgmpa' ),
193193
194-
'nag_type' => 'updated', // Determines admin notice type - can only be 'updated', 'update-nag' or 'error'.
194+
'nag_type' => '', // Determines admin notice type - can only be one of the typical WP notice classes, such as 'updated', 'update-nag', 'notice-warning', 'notice-info' or 'error'. Some of which may not work as expected in older WP versions.
195195
),
196196
*/
197197
);

0 commit comments

Comments
 (0)