Getting notice functionality in the admin panel

This commit is contained in:
Brian 2021-10-06 12:09:37 -06:00
parent af75b9544a
commit fa1807f5fc
Signed by: brian
GPG Key ID: DE1A5390A3B84CD8
5 changed files with 61 additions and 9 deletions

View File

@ -66,7 +66,7 @@ class AdminSettings {
public function register_scripts(): void {
wp_enqueue_style( 'nmsp-plugin-name-admin-css', plugins_url( '../assets/css/nmsp-plugin-name-admin.css', __FILE__ ), array(), '1.0.0', 'screen' );
wp_register_script( 'nmsp-plugin-name-admin-js', plugins_url( '../assets/js/nmsp-plugin-name-admin.js', __FILE__ ), array( 'jquery' ), '1.0.0', false );
wp_register_script( 'nmsp-plugin-name-admin-js', plugins_url( '../assets/js/nmsp-plugin-name-admin.js', __FILE__ ), array(), '1.0.0', false );
}
/**

View File

@ -0,0 +1,48 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Show a notice on the admin panel.
*
* @since 1.0.0
*
* @param string $message The message to show in the notice
* @param string $type The 'type' of notice, e.g. info (default), success, warning, or error
* @param bool $dismissible Whether the notice should be dismissible
*
* @return string
*/
function nmsp_admin_notice( string $message, string $type = 'info', bool $dismissible = true ) {
$classes = 'notice';
switch ($type) {
case 'success':
$classes .= ' notice-success';
break;
case 'warning':
$classes .= ' notice-warning';
break;
case 'error':
$classes .= ' notice-error';
break;
case 'info':
default:
$classes .= ' notice-info';
}
if ( $dismissible ) {
$classes .= ' is-dismissible';
}
add_action( 'admin_notices', function () use ( $classes, $message ) {
ob_start();
?>
<div class="<?php echo $classes; ?>">
<p><?php _e( $message, 'nmsp-plugin-name-text-domain' ); ?></p>
</div>
<?php
echo ob_get_clean();
});
}

View File

@ -11,12 +11,6 @@ if ( ! defined( 'ABSPATH' ) ) {
class App {
/** @var string */
public $version = '1.0.0';
/** @var string */
public $db_version = '1.0.0';
/** @var \PluginNamespace\App */
public static $instance;
@ -32,6 +26,12 @@ class App {
*/
public static function plugin_activation(): void {
self::ensure_template_cache_dir_exists();
$plugin_metadata = array(
'plugin_version' => PLUGIN_NAME_VERSION,
'db_version' => PLUGIN_NAME_DB_VERSION,
);
add_option( 'nmsp_plugin_metadata', $plugin_metadata );
}
/**
@ -151,9 +151,10 @@ class App {
public function includes(): void {
// Files to include on the admin.
if ( is_admin() ) {
require_once NMSP_PLUGIN_BASE_DIR . '/admin/nmsp-admin-functions.php';
require_once NMSP_PLUGIN_BASE_DIR . '/admin/nmsp-update-functions.php';
require_once NMSP_PLUGIN_BASE_DIR . '/admin/class-adminsettings.php';
}
//
// Files to include on every request.
//

View File

@ -29,7 +29,7 @@ class CustomPage {
public function enqueue_scripts(): void {
wp_enqueue_style( 'nmsp-plugin-name-css', plugins_url( '../../assets/css/nmsp-plugin-name.css', __FILE__ ), array(), '1.0.0', 'screen' );
wp_register_script( 'nmsp-plugin-name-js', plugins_url( '../../assets/js/nmsp-plugin-name.js', __FILE__ ), array( 'jquery' ), '1.0.0', false );
wp_register_script( 'nmsp-plugin-name-js', plugins_url( '../../assets/js/nmsp-plugin-name.js', __FILE__ ), array(), '1.0.0', false );
wp_enqueue_script( 'nmsp-plugin-name-js' );
$payload = array(

View File

@ -40,6 +40,9 @@ if ( ! defined( 'ABSPATH' ) ) {
if ( ! defined( 'PLUGIN_NAME_VERSION' ) ) {
define( 'PLUGIN_NAME_VERSION', '1.0.0' );
}
if ( ! defined( 'PLUGIN_NAME_DB_VERSION' ) ) {
define( 'PLUGIN_NAME_DB_VERSION', '1.0.0' );
}
if ( ! defined('NMSP_PLUGIN_BASE_DIR') ) {
define( 'NMSP_PLUGIN_BASE_DIR', __DIR__ );