reated * @access public */ public function init() { $notices = get_option( 'social_warfare_dismissed_notices', false ); if ( false === $notices ) { update_option( 'social_warfare_dismissed_notices', array() ); $notices = array(); } $this->notices = $notices; if ( isset( $notices[ $this->key ] ) ) : $this->data = $notices[ $this->key ]; endif; } /** * A method to determine if this notice should be displayed. * * This method lets the class now if this notice should be displayed or not. It checks * thing like the start date, the end date, the dimissal status if it was temporarily * dismissed versus permanently dismissed and so on. * * @since 3.0.9 | 07 JUN 2018 | Created * @access public * @return bool Default true. */ public function should_display_notice() { $now = new DateTime(); $now = $now->format( 'Y-m-d H:i:s' ); // If the start date has not been reached. if ( isset( $this->start_date ) && $now < $this->start_date ) { return false; } // If the end date has been reached. if ( isset( $this->end_date ) && $now > $this->end_date ) { return false; } // * No dismissal has happened yet. if ( empty( $this->data['timestamp'] ) ) : return true; endif; // * They have dismissed a permadismiss. if ( isset( $this->data['timestamp'] ) && 0 === (int) $this->data['timeframe'] ) { return false; } // * They have dismissed with a temp CTA. if ( isset( $this->data['timeframe'] ) && $this->data['timeframe'] > 0 ) { $expiry = $this->data['timestamp']; return $now > $expiry; } return true; } /** * Processes notice dismissals via ajax. * * This is the method that is added to the WordPress admin-ajax hooks. * * @since 3.0.9 | 07 JUN 2018 | Created * @access public */ public function dismiss() { SWP_Utility::auth(); $key = isset( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; // phpcs:ignore $timeframe = isset( $_POST['timeframe'] ) ? sanitize_text_field( wp_unslash( $_POST['timeframe'] ) ) : ''; // phpcs:ignore $now = new DateTime(); // Further sanitize, validate, and format the timeframe. if ( 0 < $timeframe ) { $timestamp = $now->modify( "+$timeframe days" )->format( 'Y-m-d H:i:s' ); } else { $timestamp = $now->format( 'Y-m-d H:i:s' ); } $this->notices[ $key ]['timestamp'] = $timestamp; $this->notices[ $key ]['timeframe'] = $timeframe; echo wp_json_encode( update_option( 'social_warfare_dismissed_notices', $this->notices ) ); wp_die(); } /** * A method to allow you to set the message text for this notice. * * @since 3.0.9 | 07 JUN 2018 | Created * @access public * @param string $message A string of text for the notices message. * @return object $this Allows for method chaining. * @throws Exception If the provided key is not a string. */ public function set_message( $message ) { if ( ! is_string( $message ) ) : throw('Please provide a string for your database key.'); endif; $this->message = $message; return $this; } /** * A method to allow you to set the unique key for this notice. * * @since 3.0.9 | 07 JUN 2018 | Created * @access protected * @param string $key A string representing this notices unique key. * @return object $this Allows for method chaining. * @throws Exception If the provided key is not a string. */ protected function set_key( $key ) { if ( ! is_string( $key ) ) : throw('Please provide a string for your database key.'); endif; $this->key = $key; return $this; } /** * Set a start date. * * This will allow us to schedule messages to be displayed at a specific date in the * future. For example, before the StumbleUpon service goes away, we may want to post * a notice letting folks know that it WILL BE going away. The day that they actually * go away could be the start date for a notice that says that they HAVE gone away. * * @since 3.0.9 | 07 JUN 2018 | Created * @access public * @param string $start_date A str date formatted to 'Y-m-d H:i:s' * @return $this Allows for method chaining * @TODO Add a type check, if possible, for a properly formatted date string. */ public function set_start_date( $start_date ) { if ( $this->is_date( $start_date ) ) : $this->start_date = $start_date; endif; return $this; } /** * Set an end date. * * This will allow us to schedule messages to stop being displayed at a specific date * in the future. For example, before the StumbleUpon service goes away, we may want * to post a notice letting folks know that it WILL BE going away. The day that they * actually go away could be the end date for that notice and the start date for a * notice that says that they HAVE gone away. Additionally, we may only want to notify * people about StumbleUpon having gone away for 60 days after it happens. After that, * we can just assume that they've probably heard from somewhere else and not worry * about showing a notice message. * * @since 3.0.9 | 07 JUN 2018 | Created * @access public * @param string $end_date A str date formatted to 'Y-m-d H:i:s' * @return $this Allows for method chaining * @TODO Add a type check, if possible, for a properly formatted date string. */ public function set_end_date( $end_date ) { if ( $this->is_date( $end_date ) ) : $this->end_date = $end_date; endif; return $this; } /** * Creates the interactive CTA for the notice. * * @since 3.0.9 | 07 JUN 2018 | Created * @access public * @return $this Allows for method chaining. */ public function add_default_cta() { $cta = array(); $cta['action'] = 'Thanks, I understand.'; $cta['href'] = ''; $cta['target'] = '_self'; $cta['class'] = ''; $cta['timeframe'] = 0; $this->actions[] = $cta; return $this; } /** * Render out the HTML. * * Ideally, everything before this method will create a beautiful data-oriented * object. The only HTML that should be compiled should be inside this method. * * @since 3.0.9 | 07 JUN 2018 | Created * @access public * @return string The compiled HTML of the dashboard notice. */ public function render_HTML() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid if ( empty( $this->actions ) && false === $this->no_cta ) : $this->add_default_cta(); endif; $html = '
' . $this->message . ' - Warfare Plugins Team
'; $html .= '