get_results($wpdb->prepare(" SELECT * FROM $table_name WHERE status = %s AND (start_date IS NULL OR start_date <= %s) AND (end_date IS NULL OR end_date >= %s) ORDER BY created_at DESC ", 'active', $current_time, $current_time)); if ($announcements === null) { return array(); } return $announcements; } /** * Display announcements at the top of pages */ public function display_announcements() { // Don't show on admin pages or in WordPress admin if (is_admin()) { return; } $announcements = $this->get_active_announcements(); if (empty($announcements)) { return; } // Display only the most recent active announcement $announcement = $announcements[0]; $this->render_announcement($announcement); } /** * Render single announcement */ private function render_announcement($announcement) { $title = esc_html($announcement->title); $message = wpautop(wp_kses_post($announcement->message)); $image_url = esc_url($announcement->image_url); $banner_color = esc_attr($announcement->banner_color); $link_url = esc_url($announcement->link_url); $close_text = __('Close', 'pc-announcements-274'); $banner_style = ''; if (!empty($banner_color) && $banner_color !== '#0d47a1') { $banner_style = 'background: ' . $banner_color . ';'; } $has_link = !empty($link_url); $link_attrs = $has_link ? 'href="' . $link_url . '" target="_blank" rel="noopener noreferrer"' : ''; $close_button = $has_link ? '' : ''; ?>
1, 'show_image' => true, 'show_close' => true, 'class' => '' ), $atts, 'pc_announcements_274'); $announcements = $this->get_active_announcements(); if (empty($announcements)) { return ''; } $output = ''; $count = min(intval($atts['count']), count($announcements)); $show_image = filter_var($atts['show_image'], FILTER_VALIDATE_BOOLEAN); $show_close = filter_var($atts['show_close'], FILTER_VALIDATE_BOOLEAN); $custom_class = sanitize_html_class($atts['class']); for ($i = 0; $i < $count; $i++) { $announcement = $announcements[$i]; $output .= $this->render_announcement_html($announcement, $show_image, $show_close, $custom_class); } return $output; } /** * Render announcement as HTML string */ private function render_announcement_html($announcement, $show_image = true, $show_close = true, $custom_class = '') { $title = esc_html($announcement->title); $message = wpautop(wp_kses_post($announcement->message)); $image_url = esc_url($announcement->image_url); $banner_color = esc_attr($announcement->banner_color); $link_url = esc_url($announcement->link_url); $close_text = __('Close', 'pc-announcements-274'); $class_names = array('pc-announcements-274-announcement'); if (!empty($custom_class)) { $class_names[] = $custom_class; } $banner_style = ''; if (!empty($banner_color) && $banner_color !== '#0d47a1') { $banner_style = 'style="background: ' . $banner_color . ';"'; } $has_link = !empty($link_url); $link_attrs = $has_link ? 'href="' . $link_url . '" target="_blank" rel="noopener noreferrer"' : ''; $html = '