Skip to content

Instantly share code, notes, and snippets.

@bigfather98
Created April 29, 2025 21:49
Show Gist options
  • Save bigfather98/c4b9977ceafff952936315241ef2ca6b to your computer and use it in GitHub Desktop.
Save bigfather98/c4b9977ceafff952936315241ef2ca6b to your computer and use it in GitHub Desktop.
<?php
/**
* Handles announcements management
*/
class Preschool_Announcements {
public function __construct() {
add_action('preschool_render_announcements_page', array($this, 'render_announcements_page'));
add_action('admin_post_preschool_save_announcement', array($this, 'handle_save_announcement'));
add_action('admin_post_preschool_delete_announcement', array($this, 'handle_delete_announcement'));
}
public function render_announcements_page() {
global $wpdb;
// Handle announcement deletion
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['announcement_id'])) {
$this->handle_delete_announcement(absint($_GET['announcement_id']));
}
// Get all announcements
$announcements = $wpdb->get_results("
SELECT a.*, u.display_name AS author
FROM {$wpdb->prefix}preschool_announcements a
LEFT JOIN {$wpdb->users} u ON a.created_by = u.ID
ORDER BY a.date_created DESC
");
include PRESCHOOL_MANAGEMENT_PLUGIN_DIR . 'views/admin/announcements.php';
}
public function handle_save_announcement() {
if (!current_user_can('manage_announcements') || !wp_verify_nonce($_POST['_wpnonce'], 'preschool_save_announcement')) {
wp_die(__('You do not have sufficient permissions to access this page.', 'preschool-management'));
}
global $wpdb;
$title = sanitize_text_field($_POST['title']);
$content = wp_kses_post($_POST['content']);
$audience = sanitize_text_field($_POST['audience']);
$created_by = get_current_user_id();
$wpdb->insert($wpdb->prefix . 'preschool_announcements', array(
'title' => $title,
'content' => $content,
'audience' => $audience,
'created_by' => $created_by,
'date_created' => current_time('mysql')
));
$announcement_id = $wpdb->insert_id;
// Send announcement emails
$this->send_announcement_emails($title, $content, $audience);
wp_redirect(admin_url('admin.php?page=preschool-announcements&announcement_saved=1'));
exit;
}
private function send_announcement_emails($title, $content, $audience) {
$emails = array();
if ($audience === 'all') {
$users = get_users(array(
'role__in' => array('preschool_staff', 'preschool_parent'),
'fields' => array('user_email')
));
$emails = wp_list_pluck($users, 'user_email');
} elseif ($audience === 'staff') {
$users = get_users(array(
'role' => 'preschool_staff',
'fields' => array('user_email')
));
$emails = wp_list_pluck($users, 'user_email');
} elseif ($audience === 'parents') {
$users = get_users(array(
'role' => 'preschool_parent',
'fields' => array('user_email')
));
$emails = wp_list_pluck($users, 'user_email');
}
if (!empty($emails)) {
$subject = $title;
// Prepare HTML email
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'Bcc: ' . implode(',', $emails)
);
$message = '<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px; }
h1 { color: #2b6cb0; font-size: 20px; margin-bottom: 20px; }
.content { margin-bottom: 20px; }
img { max-width: 100%; height: auto; }
.footer { font-size: 12px; color: #718096; margin-top: 30px; border-top: 1px solid #e2e8f0; padding-top: 10px; }
</style>
</head>
<body>
<div class="content">'.wpautop($content).'</div>
<div class="footer">'.__('This is an automated message from the preschool management system.', 'preschool-management').'</div>
</body>
</html>';
// Set content type filter
$content_type_filter = function() { return 'text/html'; };
add_filter('wp_mail_content_type', $content_type_filter);
// Send to admin email with BCC to all recipients
$admin_email = get_option('admin_email');
wp_mail($admin_email, $subject, $message, $headers);
// Remove content type filter
remove_filter('wp_mail_content_type', $content_type_filter);
}
}
public function handle_delete_announcement($announcement_id) {
if (!current_user_can('manage_announcements')) {
wp_die(__('You do not have sufficient permissions to access this page.', 'preschool-management'));
}
global $wpdb;
$wpdb->delete($wpdb->prefix . 'preschool_announcements', array('announcement_id' => $announcement_id));
wp_redirect(admin_url('admin.php?page=preschool-announcements&announcement_deleted=1'));
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment