Skip to content

Commit

Permalink
fix: make the success notice temporary
Browse files Browse the repository at this point in the history
When the plugin is activated, I print a message to inform users about
settings. However, even if an user dismissed it, the notice is printed
on each page.
By using the transient API with the activation hook, the notice is now
only printed after activation.
  • Loading branch information
ArmandPhilippot committed Dec 6, 2021
1 parent 13a7282 commit ab7625d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
15 changes: 14 additions & 1 deletion includes/class-wp-graphql-gravatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ public function check_plugin_dependencies() {
* @since 1.1.0
*/
public function print_plugin_activation_success() {
include_once 'partials/activation/wp-graphql-gravatar-success.php';
if ( get_transient( 'wp-graphql-gravatar-admin-notice' ) ) {
include_once 'partials/activation/wp-graphql-gravatar-success.php';

delete_transient( 'wp-graphql-gravatar-admin-notice' );
}
}

/**
Expand Down Expand Up @@ -175,6 +179,15 @@ public function add_gravatar_url_field() {
);
}

/**
* Set a transient to display a temporary notice on activation.
*
* This function is fired during plugin activation.
*/
public static function activate() {
set_transient( 'wp-graphql-gravatar-admin-notice', true, 5 );
}

/**
* Execute the plugin.
*
Expand Down
10 changes: 10 additions & 0 deletions wp-graphql-gravatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
*/
define( 'WP_GRAPHQL_GRAVATAR_VERSION', '1.1.0' );

/**
* The code that runs during plugin activation.
*/
function activate_wp_graphql_gravatar() {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-wp-graphql-gravatar.php';
WP_GraphQL_Gravatar::activate();
}

register_activation_hook( __FILE__, 'WP_GraphQL_Gravatar\activate_wp_graphql_gravatar' );

/**
* Initialize the plugin.
*
Expand Down

0 comments on commit ab7625d

Please sign in to comment.