registry()->get( 'admin_notices' );
}
/**
* Create class object. A private constructor, so this is used in a singleton context.
*
* @since 1.4.6
* @since 1.5.4 Access changed to public.
*/
public function __construct() {
$this->load_notices();
add_action( 'admin_notices', array( $this, 'render_upgrade_banner' ) );
add_action( 'admin_notices', array( $this, 'render_five_star_rating' ) );
}
/**
* Adds a notice message.
*
* @since 1.4.6
*
* @param string $message
* @param string $type
* @param string $key Optional. If not set, next numeric key is used.
* @return void
*/
public function add_notice( $message, $type, $key = false, $dismissible = false ) {
if ( false === $key ) {
$this->notices[ $type ][] = array(
'message' => $message,
'dismissible' => $dismissible,
);
} else {
$this->notices[ $type ][ $key ] = array(
'message' => $message,
'dismissible' => $dismissible,
);
}
}
/**
* Adds an error message.
*
* @since 1.4.6
*
* @param string $message
* @param string $key Optional. If not set, next numeric key is used.
* @return void
*/
public function add_error( $message, $key = false, $dismissible = false ) {
$this->add_notice( $message, 'error', $key, $dismissible );
}
/**
* Adds a warning message.
*
* @since 1.4.6
*
* @param string $message
* @param string $key Optional. If not set, next numeric key is used.
* @return void
*/
public function add_warning( $message, $key = false, $dismissible = false ) {
$this->add_notice( $message, 'warning', $key, $dismissible );
}
/**
* Adds a success message.
*
* @since 1.4.6
*
* @param string $message
* @param string $key Optional. If not set, next numeric key is used.
* @return void
*/
public function add_success( $message, $key = false, $dismissible = false ) {
$this->add_notice( $message, 'success', $key, $dismissible );
}
/**
* Adds an info message.
*
* @since 1.4.6
*
* @param string $message
* @param string $key Optional. If not set, next numeric key is used.
* @return void
*/
public function add_info( $message, $key = false, $dismissible = false ) {
$this->add_notice( $message, 'info', $key, $dismissible );
}
/**
* Adds a version update message.
*
* @since 1.4.6
*
* @param string $message
* @param string $key Optional. If not set, next numeric key is used.
* @param boolean $dismissible Optional. Set to true by default.
* @return void
*/
public function add_version_update( $message, $key = false, $dismissible = true ) {
$this->add_notice( $message, 'version', $key, $dismissible );
}
/**
* Render notices.
*
* @since 1.4.6
*
* @return void
*/
public function render() {
foreach ( charitable_get_admin_notices()->get_notices() as $type => $notices ) {
foreach ( $notices as $key => $notice ) {
$this->render_notice( $notice['message'], $type, $notice['dismissible'], $key );
}
}
}
/**
* Render a notice.
*
* @since 1.4.6
*
* @param string $notice
* @param string $type
* @param boolean $dismissible
* @param string $notice_key
* @return void
*/
public function render_notice( $notice, $type, $dismissible = false, $notice_key = '', $paragraph_tags = true ) {
if ( ! isset( $this->script_enqueued ) ) {
if ( ! wp_script_is( 'charitable-admin-notice' ) ) {
wp_enqueue_script( 'charitable-admin-notice' );
}
$this->script_enqueued = true;
}
$class = 'notice charitable-notice';
switch ( $type ) {
case 'error':
$class .= ' notice-error';
break;
case 'warning':
$class .= ' notice-warning';
break;
case 'success':
$class .= ' updated';
break;
case 'info':
$class .= ' notice-info';
break;
case 'five-star-review':
$class .= ' notice-info notice-five-star-review';
break;
case 'version':
$class .= ' charitable-upgrade-notice';
break;
}
if ( $dismissible ) {
$class .= ' is-dismissible';
}
$body_text = ( $paragraph_tags ) ? '
%s
': '%s';
printf(
'' . $body_text . '
',
esc_attr( $class ),
strlen( $notice_key ) ? 'data-notice="' . esc_attr( $notice_key ) . '"' : '',
$notice
);
if ( strlen( $notice_key ) ) {
unset( $this->notices[ $type ][ $notice_key ] );
}
}
/**
* Render a lite to pro banner
*
* @since 1.7.0
*
*/
public function render_upgrade_banner() {
if ( charitable_is_pro() ) {
return;
}
$screen = get_current_screen();
if ( ! is_null( $screen ) && ( in_array( $screen->id, $this->get_charitable_screens() ) || ( isset( $screen->taxonomy ) && 'campaign_category' === $screen->taxonomy ) || ( isset( $screen->taxonomy ) && 'campaign_tag' === $screen->taxonomy ) ) ) {
$banner = get_transient( 'charitable_charitablelitetopro_banner' );
if ( ! $banner ) {
$utm_link = charitable_pro_upgrade_url( 'Upgrade From Lite Top Banner Link', 'To unlock more features consider upgrading to Pro.' );
$this->render_banner('You\'re using WP Charitable Lite. To unlock more features consider upgrading to Pro.
','top-of-page', true);
}
}
}
/**
* Render a lite to pro banner
*
* @since 1.7.0
*
*/
public function render_five_star_rating() {
$screen = get_current_screen();
// determine if we are on the current screen.
if ( ! is_null( $screen ) && ( in_array( $screen->id, $this->get_charitable_screens() ) || ( isset( $screen->taxonomy ) && 'campaign_category' === $screen->taxonomy ) || ( isset( $screen->taxonomy ) && 'campaign_tag' === $screen->taxonomy ) ) ) {
$slug = 'five-star-review';
// determine when to display this message. for now, there should be some sensible boundaries before showing the notification: a minimum of 14 days of use, created one donation form and received at least one donation.
$activated_datetime = ( false !== get_option( 'wpcharitable_activated_datetime' ) ) ? get_option( 'wpcharitable_activated_datetime' ) : false;
$days = 0;
if ( $activated_datetime ) {
$diff = current_time( 'timestamp' ) - $activated_datetime;
$days = abs( round( $diff / 86400 ) );
}
$count_campaigns = wp_count_posts( 'campaign' );
$total_campaigns = isset( $count_campaigns->publish ) ? $count_campaigns->publish : 0;
$count_donations = wp_count_posts( 'donation' );
$total_donations = isset( $count_donations->{'charitable-completed'} ) ? $count_donations->{'charitable-completed'} : 0;
if ( $days >= apply_filters( 'charitable_days_since_activated', 14 ) && $total_campaigns >= 1 && $total_donations >= 1 ) {
// check transient
$star_review = get_transient( 'charitable_' . $slug . '_banner' );
// render five star rating banner/notice
if ( ! $star_review ) {
$message = charitable_admin_view('notices/admin-notice-five-star-review', array(), true );
$key = 'five-star-review';
$this->render_notice( $message, 'five-star-review', true, $key, false );
}
}
}
}
/**
* Render a banner.
*
* @since 1.7.0
*
*/
public function render_banner( $message, $type = 'top-of-page', $dismissible = false, $data_nonce = 'charitablelitetopro', $data_id = 'charitablelitetopro', $data_lifespan = false ) {
if ( ! isset( $this->script_enqueued ) ) {
if ( ! wp_script_is( 'charitable-admin-notice' ) ) {
wp_enqueue_script( 'charitable-admin-notice' );
}
$this->script_enqueued = true;
}
$class = 'charitable-banner';
switch ( $type ) {
case 'top-of-page':
$class .= ' charitable-admin-banner-top-of-page';
break;
}
if ( $dismissible ) {
// $class .= ' is-dismissible';
}
printf(
'%s
',
esc_attr( $class ),
strlen( $data_nonce ) ? 'data-notice="' . esc_attr( $data_nonce ) . '"' : '',
strlen( $data_id ) ? 'data-id="' . esc_attr( $data_id ) . '"' : '',
strlen( $data_lifespan ) ? 'data-lifespan="' . esc_attr( $data_lifespan ) . '"' : '',
$message
);
}
/**
* When PHP finishes executing, stash any notices that haven't been rendered yet.
*
* @since 1.4.13
*
* @return void
*/
public function shutdown() {
set_transient( 'charitable_notices', $this->notices );
}
/**
* Load the notices array.
*
* If there are any stuffed in a transient, pull those out. Otherwise, reset a clear array.
*
* @since 1.4.13
*
* @return void
*/
public function load_notices() {
$this->notices = get_transient( 'charitable_notices' );
if ( ! is_array( $this->notices ) ) {
$this->clear();
}
}
/**
* Fill admin notices from the front-end notices array.
*
* @since 1.6.24
*
* @return void
*/
public function fill_notices_from_frontend() {
$notices = charitable_get_notices();
foreach ( $notices->get_notices() as $type => $type_notices ) {
foreach ( $type_notices as $notice ) {
$this->add_notice( $notice, $type );
}
}
$notices->clear();
}
/**
* Clear out all existing notices.
*
* @since 1.4.6
*
* @return void
*/
public function clear() {
$clear = array(
'error' => array(),
'warning' => array(),
'success' => array(),
'info' => array(),
'version' => array(),
);
$this->notices = $clear;
}
/**
* Returns an array of screen IDs where the Charitable notices should be displayed.
*
* @uses charitable_admin_screens
*
* @since 1.7.0
*
* @return array
*/
public function get_charitable_screens() {
/**
* Filter admin screens where Charitable styles & scripts should be loaded.
*
* @since 1.7.0
*
* @param string[] $screens List of screen ids.
*/
return apply_filters( 'charitable_admin_notice_screens', array(
'campaign',
'donation',
'charitable_page_charitable-settings',
'edit-campaign',
'edit-donation',
'toplevel_page_charitable'
) );
}
}
endif;