' . $count . '' ); ?>
$nonce,
'code' => $alert['code'],
'pum_dismiss_alert' => 'dismiss',
'expires' => $expires,
]
);
?>
' . esc_html( $action['text'] ) . '' : esc_html( $action['text'] );
if ( 'link' === $action['type'] ) {
$url = $action['href'];
$attributes = 'target="_blank" rel="noreferrer noopener"';
} else {
$url = add_query_arg(
[
'nonce' => $nonce,
'code' => $alert['code'],
'pum_dismiss_alert' => $action['action'],
'expires' => $expires,
]
);
$attributes = 'class="pum-dismiss"';
}
?>
- >
'default',
'priority' => 10,
'message' => '',
'type' => 'info',
'html' => '',
'dismissible' => true,
'global' => false,
]
);
}
// Sort alerts by priority, highest to lowest.
$alerts = PUM_Utils_Array::sort( $alerts, 'priority', true );
return $alerts;
}
/**
* Handles if alert was dismissed AJAX
*/
public static function ajax_handler() {
$args = wp_parse_args(
$_REQUEST,
[
'code' => '',
'expires' => '',
'pum_dismiss_alert' => '',
]
);
if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'pum_alerts_action' ) ) {
wp_send_json_error();
}
$results = self::action_handler( $args['code'], $args['pum_dismiss_alert'], $args['expires'] );
if ( true === $results ) {
wp_send_json_success();
} else {
wp_send_json_error();
}
}
/**
* Handles if alert was dismissed by page reload instead of AJAX
*
* @since 1.11.0
*/
public static function php_handler() {
if ( ! isset( $_REQUEST['pum_dismiss_alert'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'pum_alerts_action' ) ) {
return;
}
$args = wp_parse_args(
$_REQUEST,
[
'code' => '',
'expires' => '',
'pum_dismiss_alert' => '',
]
);
self::action_handler( $args['code'], $args['pum_dismiss_alert'], $args['expires'] );
}
/**
* Handles the action taken on the alert.
*
* @param string $code The specific alert.
* @param string $action Which action was taken
* @param string $expires When the dismissal expires, if any.
*
* @return bool
* @uses PUM_Utils_Logging::instance
* @uses PUM_Utils_Logging::log
* @since 1.11.0
*/
public static function action_handler( $code, $action, $expires ) {
if ( empty( $action ) || 'dismiss' === $action ) {
try {
$dismissed_alerts = self::dismissed_alerts();
$dismissed_alerts[ $code ] = ! empty( $expires ) ? strtotime( '+' . $expires ) : true;
$user_id = get_current_user_id();
update_user_meta( $user_id, '_pum_dismissed_alerts', $dismissed_alerts );
return true;
} catch ( Exception $e ) {
PUM_Utils_Logging::instance()->log( 'Error dismissing alert. Exception: ' . $e->getMessage() );
return false;
}
}
do_action( 'pum_alert_dismissed', $code, $action );
}
/**
* @param string $code
*
* @return bool
*/
public static function has_dismissed_alert( $code = '' ) {
$dimissed_alerts = self::dismissed_alerts();
$alert_dismissed = array_key_exists( $code, $dimissed_alerts );
// If the alert was dismissed and has a non true type value, it is an expiry time.
if ( $alert_dismissed && true !== $dimissed_alerts[ $code ] ) {
return strtotime( 'now' ) < $dimissed_alerts[ $code ];
}
return $alert_dismissed;
}
/**
* Returns an array of dismissed alert groups.
*
* @return array
*/
public static function dismissed_alerts() {
$user_id = get_current_user_id();
$dismissed_alerts = get_user_meta( $user_id, '_pum_dismissed_alerts', true );
if ( ! is_array( $dismissed_alerts ) ) {
$dismissed_alerts = [];
update_user_meta( $user_id, '_pum_dismissed_alerts', $dismissed_alerts );
}
return $dismissed_alerts;
}
}