Skip to content

Commit

Permalink
Fix admin notice class.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jrfnl committed Jan 5, 2016
1 parent 15843bc commit 5b057f5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
31 changes: 25 additions & 6 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1180,12 +1180,7 @@ public function notices() {
}

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

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

/**
* Get admin notice class.
*
* Work around all the changes to the various admin notice classes between WP 4.4 and 3.7
* (lowest supported version by TGMPA).
*
* @since 2.x.x
*
* @return string
*/
protected function get_admin_notice_class() {
if ( ! empty( $this->strings['nag_type'] ) ) {
return sanitize_html_class( strtolower( $this->strings['nag_type'] ) );
} else {
if ( version_compare( $this->wp_version, '4.2', '>=' ) ) {
return 'notice-warning';
} elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) {
return 'notice';
} else {
return 'updated';
}
}
}

/**
* Display settings errors and remove those which have been displayed to avoid duplicate messages showing
*
Expand Down
2 changes: 1 addition & 1 deletion example.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function my_theme_register_required_plugins() {
'complete' => __( 'All plugins installed and activated successfully. %1$s', 'theme-slug' ), // %s = dashboard link.
'contact_admin' => __( 'Please contact the administrator of this site for help.', 'tgmpa' ),
'nag_type' => 'updated', // Determines admin notice type - can only be 'updated', 'update-nag' or 'error'.
'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.
),
*/
);
Expand Down

0 comments on commit 5b057f5

Please sign in to comment.