_name = TINVWL_PREFIX; $this->table = $wpdb->prefix . $this->_name . '_lists'; $this->activation_date = get_option( 'tinvwl_activation_date' ) ?: $this->get_first_wishlist_date(); add_action( 'admin_notices', [ $this, 'add_notices' ] ); add_action( 'wp_ajax_tinvwl_admin_dismiss_notice', [ $this, 'ajax_dismiss_notice' ] ); $this->postpone_notifications(); } /** * Dismiss admin notice. * * @return void */ public function ajax_dismiss_notice(): void { check_admin_referer( 'tinvwl_admin_dismiss_notice', 'nonce' ) && isset( $_REQUEST['tinvwl_type'] ) ? $this->update_notice_status( sanitize_key( $_REQUEST['tinvwl_type'] ) ) : wp_die(); } /** * Update the status of a notice. * * @param string $notice_type Notice type. * * @return void */ private function update_notice_status( string $notice_type ): void { update_user_meta( get_current_user_id(), $notice_type, true ); set_transient( 'tinvwl-admin-notice-delay', true, 14 * DAY_IN_SECONDS ); wp_send_json( $notice_type ); } /** * Get plugin activation data. * * @return false|int */ private function get_first_wishlist_date() { global $wpdb; $date = $wpdb->get_var( "SELECT `date` FROM `{$this->table}` ORDER BY `ID` ASC" ); $timestamp = $date ? strtotime( $date ) : strtotime( 'now' ); add_option( 'tinvwl_activation_date', $timestamp ); return $timestamp; } /** * Postpone notifications for 14 days. * * @return void */ private function postpone_notifications(): void { if ( ! get_option( 'tinvwl_notifications_postponed' ) ) { $timestamp_in_14_days = time() + ( 14 * DAY_IN_SECONDS ); add_option( 'tinvwl_notifications_postponed', '1' ); // Schedule the event to call the disable_notifications method wp_schedule_single_event( $timestamp_in_14_days, 'tinvwl_disable_notifications_event' ); } } /** * Disable notifications. * * @return void */ public static function disable_notifications(): void { delete_option( 'tinvwl_notifications_postponed' ); // get_transient( 'tinvwl-admin-notice-delay' ); tinv_update_option( 'chat', 'enabled', true ); } /** * Check if it's not the time to display the notice. * * @return bool */ private function is_not_display_time(): bool { return strtotime( '14 days', $this->activation_date ) > strtotime( 'now' ) || get_transient( 'tinvwl-admin-notice-delay' ); } /** * Check if current date is during the Black Friday period. * * The Black Friday period is defined as the last Friday of November * and the two following days (Saturday and Sunday). * * @return bool True if it's Black Friday period, false otherwise. */ private function is_black_friday_period(): bool { $year = date( 'Y' ); $blackFriday = date( 'Y-m-d', strtotime( "last friday of November $year" ) ); return $this->is_date_between( date( 'Y-m-d' ), $blackFriday, date( 'Y-m-d', strtotime( "$blackFriday +2 days" ) ) ); } /** * Check if current date is during the Cyber Monday event. * * Cyber Monday is defined as the next Monday following the Black Friday weekend. * * @return bool True if it's Cyber Monday period, false otherwise. */ private function is_cyber_monday_period(): bool { $year = date( 'Y' ); $cyberMonday = date( 'Y-m-d', strtotime( "last friday of November $year +3 days" ) ); return $this->is_date_between( date( 'Y-m-d' ), $cyberMonday, date( 'Y-m-d', strtotime( "$cyberMonday +6 days" ) ) ); } /** * Check if current date is during the Winter Holidays. * * @return bool True if it's Winter Holiday period, false otherwise. */ private function is_winter_holiday_period(): bool { $year = (int) date( 'Y' ); return $this->is_date_between( date( 'Y-m-d' ), $year . "-12-15", ( $year + 1 ) . "-01-15" ); } /** * Check if a date is between two dates. * * @param string $date The date to check. * @param string $start Start date. * @param string $end End date. * * @return bool True if date is between start and end, false otherwise. */ private function is_date_between( string $date, string $start, string $end ): bool { return $date >= $start && $date <= $end; } /** * Check if it's not the time to display the regular notices. * * @return bool True if it's an event period, false otherwise. */ private function is_event_period(): bool { return $this->is_black_friday_period() || $this->is_cyber_monday_period() || $this->is_winter_holiday_period(); } /** * Print admin notice. * * @return void */ public function add_notices(): void { if ( $this->is_event_period() ) { $this->display_notice_script(); $this->display_event_notices(); return; } if ( $this->is_not_display_time() ) { return; } $this->display_notice_script(); $this->display_user_notices(); } /** * Display the notice JavaScript. * * @return void */ private function display_notice_script(): void { ?>
is_black_friday_period() && ! $this->has_user_seen_notice( $current_user_id, 'tinvwl-user-black-friday' ) ) { $this->display_black_friday_notice(); } elseif ( $this->is_cyber_monday_period() && ! $this->has_user_seen_notice( $current_user_id, 'tinvwl-user-cyber-monday' ) ) { $this->display_cyber_monday_notice(); } elseif ( $this->is_winter_holiday_period() && ! $this->has_user_seen_notice( $current_user_id, 'tinvwl-user-winter-holiday' ) ) { $this->display_winter_holiday_notice(); } } /** * Check if a user has already seen a specific notice. * * @param int $user_id User ID. * @param string $meta_key Meta key to check. * * @return bool True if user has seen the notice, false otherwise. */ private function has_user_seen_notice( int $user_id, string $meta_key ): bool { return (bool) get_user_meta( $user_id, $meta_key, true ); } /** * Display the Black Friday notice. * * @return void */ private function display_black_friday_notice(): void { ?>
30% OFF',
TINVWL_NAME . ' Premium'
); ?>
%s ', __( 'Use code:', 'ti-woocommerce-wishlist' ) ); ?>
BLACKFRIDAY
30% OFF',
TINVWL_NAME . ' Premium'
); ?>
%s ', __( 'Use code:', 'ti-woocommerce-wishlist' ) ); ?>
CYBERMONDAY
30% OFF',
TINVWL_NAME . ' Premium'
); ?>
%s ', __( 'Use code:', 'ti-woocommerce-wishlist' ) ); ?>
WINTER30