Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions includes/Framework/AdminNoticeHandler.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php
// phpcs:ignoreFile
/**
* Facebook for WooCommerce.
*/

namespace WooCommerce\Facebook\Framework;

defined( 'ABSPATH' ) or exit;
defined( 'ABSPATH' ) || exit;

/**
* Admin Notice Handler Class
Expand All @@ -24,25 +23,26 @@ class AdminNoticeHandler {
private $admin_notices = [];

/** @var boolean static member to enforce a single rendering of the admin notice placeholder element */
static private $admin_notice_placeholder_rendered = false;
private static $admin_notice_placeholder_rendered = false;

/** @var boolean static member to enforce a single rendering of the admin notice javascript */
static private $admin_notice_js_rendered = false;
private static $admin_notice_js_rendered = false;


/**
* Initialize and setup the Admin Notice Handler
*
* @since 3.0.0
* @param Plugin $plugin The plugin instance.
*/
public function __construct( $plugin ) {

$this->plugin = $plugin;
$this->plugin = $plugin;

// render any admin notices, delayed notices, and
add_action( 'admin_notices', array( $this, 'render_admin_notices' ), 15 );
add_action( 'admin_footer', array( $this, 'render_delayed_admin_notices' ), 15 );
add_action( 'admin_footer', array( $this, 'render_admin_notice_js' ), 20 );
add_action( 'admin_notices', array( $this, 'render_admin_notices' ), 15 );
add_action( 'admin_footer', array( $this, 'render_delayed_admin_notices' ), 15 );
add_action( 'admin_footer', array( $this, 'render_admin_notice_js' ), 20 );

// AJAX handler to dismiss any warning/error notices
add_action( 'wp_ajax_wc_plugin_framework_' . $this->get_plugin()->get_id() . '_dismiss_notice', array( $this, 'handle_dismiss_notice' ) );
Expand All @@ -56,7 +56,7 @@ public function __construct( $plugin ) {
* @since 3.0.0
* @param string $message the notice message to display
* @param string $message_id the message id
* @param array $params {
* @param array $params {
* Optional parameters.
*
* @type bool $dismissible If the notice should be dismissible
Expand All @@ -67,11 +67,14 @@ public function __construct( $plugin ) {
*/
public function add_admin_notice( $message, $message_id, $params = [] ) {

$params = wp_parse_args( $params, array(
'dismissible' => true,
'always_show_on_settings' => true,
'notice_class' => 'updated',
) );
$params = wp_parse_args(
$params,
array(
'dismissible' => true,
'always_show_on_settings' => true,
'notice_class' => 'updated',
)
);

if ( $this->should_display_notice( $message_id, $params ) ) {
$this->admin_notices[ $message_id ] = array(
Expand All @@ -89,7 +92,7 @@ public function add_admin_notice( $message, $message_id, $params = [] ) {
*
* @since 3.0.0
* @param string $message_id the message id
* @param array $params {
* @param array $params {
* Optional parameters.
*
* @type bool $dismissible If the notice should be dismissible
Expand All @@ -105,10 +108,13 @@ public function should_display_notice( $message_id, $params = [] ) {
return false;
}

$params = wp_parse_args( $params, array(
'dismissible' => true,
'always_show_on_settings' => true,
) );
$params = wp_parse_args(
$params,
array(
'dismissible' => true,
'always_show_on_settings' => true,
)
);

// if the notice is always shown on the settings page, and we're on the settings page
if ( $params['always_show_on_settings'] && $this->get_plugin()->is_plugin_settings() ) {
Expand Down Expand Up @@ -151,7 +157,6 @@ public function render_admin_notices( $is_visible = true ) {
echo '<div class="js-wc-' . esc_attr( $this->get_plugin()->get_id_dasherized() ) . '-admin-notice-placeholder"></div>';
self::$admin_notice_placeholder_rendered = true;
}

}


Expand All @@ -171,7 +176,7 @@ public function render_delayed_admin_notices() {
* @since 3.0.0
* @param string $message the notice message to display
* @param string $message_id the message id
* @param array $params {
* @param array $params {
* Optional parameters.
*
* @type bool $dismissible If the notice should be dismissible
Expand All @@ -183,12 +188,15 @@ public function render_delayed_admin_notices() {
*/
public function render_admin_notice( $message, $message_id, $params = [] ) {

$params = wp_parse_args( $params, array(
'dismissible' => true,
'is_visible' => true,
'always_show_on_settings' => true,
'notice_class' => 'updated',
) );
$params = wp_parse_args(
$params,
array(
'dismissible' => true,
'is_visible' => true,
'always_show_on_settings' => true,
'notice_class' => 'updated',
)
);

$classes = array(
'notice',
Expand All @@ -202,7 +210,7 @@ public function render_admin_notice( $message, $message_id, $params = [] ) {
$classes[] = 'is-dismissible';
}

echo sprintf(
printf(
'<div class="%1$s" data-plugin-id="%2$s" data-message-id="%3$s" %4$s><p>%5$s</p></div>',
esc_attr( implode( ' ', $classes ) ),
esc_attr( $this->get_plugin()->get_id() ),
Expand Down Expand Up @@ -285,7 +293,7 @@ function log_dismissed_notice( pluginID, messageID ) {
*
* @since 3.0.0
* @param string $message_id the message identifier
* @param int $user_id optional user identifier, defaults to current user
* @param int $user_id optional user identifier, defaults to current user
*/
public function dismiss_notice( $message_id, $user_id = null ) {
if ( is_null( $user_id ) ) {
Expand All @@ -303,7 +311,7 @@ public function dismiss_notice( $message_id, $user_id = null ) {
* @param string $message_id notice identifier
* @param string|int $user_id
*/
do_action( 'wc_' . $this->get_plugin()->get_id(). '_dismiss_notice', $message_id, $user_id );
do_action( 'wc_' . $this->get_plugin()->get_id() . '_dismiss_notice', $message_id, $user_id );
}


Expand All @@ -312,7 +320,7 @@ public function dismiss_notice( $message_id, $user_id = null ) {
*
* @since 3.0.0
* @param string $message_id the message identifier
* @param int $user_id optional user identifier, defaults to current user
* @param int $user_id optional user identifier, defaults to current user
*/
public function undismiss_notice( $message_id, $user_id = null ) {
if ( is_null( $user_id ) ) {
Expand All @@ -330,7 +338,7 @@ public function undismiss_notice( $message_id, $user_id = null ) {
*
* @since 3.0.0
* @param string $message_id the message identifier
* @param int $user_id optional user identifier, defaults to current user
* @param int $user_id optional user identifier, defaults to current user
* @return boolean true if the message has been dismissed by the admin user
*/
public function is_notice_dismissed( $message_id, $user_id = null ) {
Expand Down Expand Up @@ -386,6 +394,4 @@ public function handle_dismiss_notice() {
protected function get_plugin() {
return $this->plugin;
}


}