Files
Hi-School-Slide-Manager/includes/class-hssm-notifications.php

275 lines
10 KiB
PHP

<?php
/**
* HSSM Notifications Class
*
* Handles all email notifications for the slide manager
*
* @package Hi_School_Slide_Manager
* @since 1.0.0
*/
// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit( 'Direct access forbidden.' );
}
/**
* HSSM Notifications Class
*/
class HSSM_Notifications {
/**
* Constructor
*
* @since 1.0.0
*/
public function __construct() {
// We'll hook into actions that trigger notifications
}
/**
* Get notification settings
*
* @return array Array of email addresses for different roles
*/
public static function get_settings() {
$defaults = array(
'editor_emails' => '',
'itc_emails' => ''
);
$settings = get_option( 'hssm_notification_settings', $defaults );
// Ensure we have an array and it contains our expected keys
if ( ! is_array( $settings ) ) {
return $defaults;
}
return wp_parse_args( $settings, $defaults );
}
/**
* Save notification settings
*
* @param array $settings Array of settings
* @return bool True if saved successfully
*/
public static function save_settings( $settings ) {
$clean_settings = array(
'editor_emails' => sanitize_textarea_field( $settings['editor_emails'] ),
'itc_emails' => sanitize_textarea_field( $settings['itc_emails'] )
);
return update_option( 'hssm_notification_settings', $clean_settings );
}
/**
* Helper to get emails as array from string
*
* @param string $emails_string Comma or newline separated emails
* @return array Array of valid email addresses
*/
private function get_emails_array( $emails_string ) {
if ( empty( $emails_string ) ) {
return array();
}
// Replace newlines with commas
$emails_string = str_replace( array( "\r", "\n" ), ',', $emails_string );
$emails = explode( ',', $emails_string );
$clean_emails = array();
foreach ( $emails as $email ) {
$email = sanitize_email( trim( $email ) );
if ( is_email( $email ) ) {
$clean_emails[] = $email;
}
}
return array_unique( $clean_emails );
}
/**
* Send notification to Editor when slide is Scheduled or Published Immediately
*
* @param int $slide_id Slide ID
* @param string $status 'scheduled' or 'published'
*/
public function notify_editor_slide_created( $slide_id, $status ) {
$settings = self::get_settings();
$emails = $this->get_emails_array( $settings['editor_emails'] );
if ( empty( $emails ) ) {
error_log( 'HSSM: No Editor emails configured for notification.' );
return;
}
$slide = get_post( $slide_id );
if ( ! $slide ) return;
$target_sites = wp_get_post_terms( $slide_id, 'hssm_site', array( 'fields' => 'slugs' ) );
$site_slugs = ! empty( $target_sites ) ? implode( ',', $target_sites ) : '';
// Determine publish date
$publish_date = ( $status === 'scheduled' )
? get_post_meta( $slide_id, '_hssm_start_date', true )
: current_time( 'Y-m-d' );
// Construct preview link
// page-slide-preview.php logic usually takes 'date' and 'site' params
// We'll link to the preview page. Assuming there is a page with the template, or we use a custom URL structure if defined.
// The instructions mentioned: "generated link to the slide-preview page with params containing the slide-title, the publish-date... and the site slugs"
$preview_page_url = home_url( '/slide-preview/' ); // Basic assumption, adjust if needed based on setup
// Better: find the page using the template
$pages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'page-slide-preview.php'
));
if ( ! empty( $pages ) ) {
$preview_page_url = get_permalink( $pages[0]->ID );
}
$preview_link = add_query_arg( array(
'preview_title' => urlencode( $slide->post_title ),
'date' => $publish_date,
'sites' => $site_slugs,
'preview_mode' => 'editor_check'
), $preview_page_url );
$subject = sprintf( 'New Slide %s: "%s"', ucfirst( $status ), $slide->post_title );
$message = "A new slide has been " . ( $status === 'scheduled' ? 'Scheduled' : 'Published Immediately' ) . ".\n\n";
$message .= "Title: " . $slide->post_title . "\n";
$message .= "Target Date: " . $publish_date . "\n";
$message .= "Target Sites: " . implode( ', ', $target_sites ) . "\n\n";
$message .= "Preview Link: " . $preview_link . "\n\n";
$message .= "Please review the slide.";
foreach ( $emails as $email ) {
wp_mail( $email, $subject, $message );
}
}
/**
* Send notification to Creator (Author) when slide is Approved
*
* @param int $slide_id Slide ID
*/
public function notify_creator_slide_approved( $slide_id ) {
$slide = get_post( $slide_id );
if ( ! $slide ) return;
$author_email = get_the_author_meta( 'user_email', $slide->post_author );
if ( ! is_email( $author_email ) ) return;
$subject = 'Your Slide Has Been Approved: "' . $slide->post_title . '"';
$message = "Good news! Your slide has been approved.\n\n";
$message .= "Title: " . $slide->post_title . "\n";
$message .= "It is now scheduled/published according to your settings.";
wp_mail( $author_email, $subject, $message );
}
/**
* Send notification to ITCGuys when slide goes LIVE
*
* @param int $slide_id Slide ID
*/
public function notify_itc_slide_live( $slide_id ) {
$settings = self::get_settings();
$emails = $this->get_emails_array( $settings['itc_emails'] );
if ( empty( $emails ) ) return;
$slide = get_post( $slide_id );
if ( ! $slide ) return;
// Generate Confirmation Link
// This link needs to trigger the "Slide is Live" confirmation page
// We probably need a specialized page or endpoint for this.
// Let's assume we use the Dashboard page with a specific action param or a separate page.
// User requested: "We need a page 'Slide is Live' ... with a green confirmation button"
// Let's use the preview page or dashboard page with a specific query arg to show this UI.
// Or better, a dedicated endpoint. For now, let's point to the dashboard with a "confirm_live" param.
$confirm_url = home_url( '/slide-live-confirmation/' ); // Ideally we create this page or rewrite rule
// For now, let's use the dashboard url with arguments, and we'll handle the UI there or create a rewrite.
// Actually, creating a specific page 'Slide is Live' was requested.
// I'll create a new template `page-slide-confirmation.php` later.
// Find confirmation page if exists, or construct URL
$pages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'page-slide-confirmation.php'
));
$confirmation_page_url = !empty($pages) ? get_permalink($pages[0]->ID) : home_url('/?hssm_action=confirm_live');
$confirm_link = add_query_arg( array(
'slide_id' => $slide_id,
'token' => wp_create_nonce( 'hssm_confirm_live_' . $slide_id ) // Note: Nonces depend on user session, might be tricky for email links if user isn't logged in.
// Better to use a hash that doesn't expire or relies on a shared secret if ITC guys are not logged in.
// Assuming ITC guys are logged in users for now or we just use a hash.
), $confirmation_page_url );
// Since email links are clicked outside sessions often, maybe just a hash of slide ID + secret salt.
$token = hash( 'sha256', $slide_id . 'hssm_secret_salt' );
$confirm_link = add_query_arg( array(
'slide_id' => $slide_id,
'hssm_token' => $token
), $confirmation_page_url );
$target_sites = wp_get_post_terms( $slide_id, 'hssm_site', array( 'fields' => 'slugs' ) );
$subject = 'ACTION REQUIRED: Slide is LIVE - "' . $slide->post_title . '"';
$message = "A slide has just gone LIVE.\n\n";
$message .= "Title: " . $slide->post_title . "\n";
$message .= "Sites: " . implode( ', ', $target_sites ) . "\n\n";
$message .= "Please check the sites to ensure it is displaying correctly.\n";
$message .= "Once verified, click the link below to confirm to the Editor and Author:\n\n";
$message .= $confirm_link;
foreach ( $emails as $email ) {
wp_mail( $email, $subject, $message );
}
}
/**
* Send FINAL notification to Editor and Author (triggered by ITC confirmation)
*
* @param int $slide_id Slide ID
*/
public function notify_all_confirmed_live( $slide_id ) {
$slide = get_post( $slide_id );
if ( ! $slide ) return;
// Editor Emails
$settings = self::get_settings();
$editor_emails = $this->get_emails_array( $settings['editor_emails'] );
// Author Email
$author_email = get_the_author_meta( 'user_email', $slide->post_author );
$recipients = array_merge( $editor_emails, array( $author_email ) );
$recipients = array_unique( array_filter( $recipients ) );
if ( empty( $recipients ) ) return;
$subject = 'CONFIRMED: Slide is Live and Verified - "' . $slide->post_title . '"';
$message = "The slide has been verified as LIVE by the ITC team.\n\n";
$message .= "Title: " . $slide->post_title . "\n";
$message .= "Status: Live & Verified\n\n";
$message .= "You can view the slide on the live sites.";
foreach ( $recipients as $email ) {
wp_mail( $email, $subject, $message );
}
}
}