get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type IN ('post', 'page') AND post_status = 'publish' AND post_content LIKE %s LIMIT 1", $query_str));
if (!$page_id) {
$query_str_unq = '%[alba_board%id=' . intval($board_id) . '%';
$page_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type IN ('post', 'page') AND post_status = 'publish' AND post_content LIKE %s LIMIT 1", $query_str_unq));
}
if ($page_id) {
$public_url = get_permalink($page_id);
update_post_meta($board_id, 'alba_board_public_url', $public_url);
}
}
return $public_url;
}
/**
* CORE UTILITY: Smart Routing Context Builder
* Context-aware URL generation (Guests vs WP Users)
*/
function alba_get_smart_card_url($card_id, $context = 'wp_user') {
$list_id = get_post_meta($card_id, 'alba_list_parent', true);
$board_id = get_post_meta($list_id, 'alba_board_parent', true);
$public_url = alba_board_get_public_url($board_id);
if (!empty($public_url)) {
return add_query_arg('alba_card', $card_id, $public_url);
} else {
// Fallback if no public page exists
if ($context === 'guest') {
// Guests must NEVER be directed to the wp-admin login screen
return home_url();
}
// WP Users can safely fall back to the backend visual board
return add_query_arg(['page' => 'alba-board-visual', 'board_id' => $board_id, 'alba_card' => $card_id], admin_url('admin.php'));
}
}
/**
* 1. THE ENGINE: Replaces variables and relies strictly on wp_mail()
*/
function alba_board_send_notification($to_email, $subject, $user_name, $card_title, $card_content, $card_link, $template_type, $comment_text = '') {
$options = get_option('alba_board_notifications', []);
$defaults = [];
$defaults['new_card'] = '
ALBA BOARD
Hello {user_name},
You have a new task waiting on your board. Here are the main details:
{card_title}
{card_content}
Notification automatically generated by Alba Board
';
$defaults['new_comment'] = '
ALBA BOARD
Hello {user_name},
Someone left a new comment on the task {card_title}:
Notification automatically generated by Alba Board
';
$html_template = !empty($options['template_' . $template_type]) ? $options['template_' . $template_type] : $defaults[$template_type];
$search = ['{user_name}', '{card_title}', '{card_content}', '{card_link}', '{comment_text}'];
$replace = [
esc_html($user_name),
esc_html($card_title),
wp_kses_post($card_content),
esc_url($card_link),
esc_html($comment_text)
];
$final_html_message = str_replace($search, $replace, $html_template);
$process_native_mail = apply_filters('alba_board_pre_send_notification', true, $to_email, $subject, $final_html_message);
if ($process_native_mail) {
$headers = ['Content-Type: text/html; charset=UTF-8'];
wp_mail($to_email, $subject, $final_html_message, $headers);
}
}
/**
* 2. TRIGGER 1: New Card Created/Assigned (WP Users)
*/
function alba_board_notify_on_new_card( $post_id, $post, $update ) {
if ( get_post_type( $post_id ) !== 'alba_card' || $update ) return;
$options = get_option( 'alba_board_notifications' );
if ( empty( $options['notify_on_card'] ) ) return;
$assignee_id = get_post_meta( $post_id, '_alba_assignee', true );
$user_to_notify = $assignee_id ? get_userdata( $assignee_id ) : get_userdata( $post->post_author );
if ( ! $user_to_notify || empty( $user_to_notify->user_email ) || ! is_email( $user_to_notify->user_email ) ) return;
/* translators: Card title. */
$subject = sprintf( __( 'New task assigned: %s', 'alba-board' ), $post->post_title );
$subject = apply_filters( 'alba_board_notification_subject_new_card', $subject, $post_id, $post );
$card_link = alba_get_smart_card_url($post_id, 'wp_user');
alba_board_send_notification(
$user_to_notify->user_email,
$subject,
$user_to_notify->display_name,
$post->post_title,
$post->post_content,
$card_link,
'new_card'
);
}
add_action( 'wp_insert_post', 'alba_board_notify_on_new_card', 10, 3 );
/**
* 3. TRIGGER 2: New Comment Added to a Card
*/
function alba_board_notify_on_new_comment( $comment_id, $comment_approved, $commentdata ) {
if ( $comment_approved !== 1 ) return;
$post_id = $commentdata['comment_post_ID'];
if ( get_post_type( $post_id ) !== 'alba_card' ) return;
$options = get_option( 'alba_board_notifications' );
if ( empty( $options['notify_on_comment'] ) ) return;
$post = get_post( $post_id );
$assignee_id = get_post_meta( $post_id, '_alba_assignee', true );
$comment_author_id = $commentdata['user_id'];
$notify_user_id = ($assignee_id && $assignee_id != $comment_author_id) ? $assignee_id : $post->post_author;
if ($notify_user_id == $comment_author_id) return;
$user_to_notify = get_userdata( $notify_user_id );
if ( ! $user_to_notify || empty( $user_to_notify->user_email ) || ! is_email( $user_to_notify->user_email ) ) return;
/* translators: Card title. */
$subject = sprintf( __( 'New comment on task: %s', 'alba-board' ), $post->post_title );
$subject = apply_filters( 'alba_board_notification_subject_new_comment', $subject, $post_id, $comment_id );
// Determine the context based on whether the recipient is the guest assignee or a WP user
$guest_assignee = get_post_meta($post_id, 'alba_guest_assignee', true);
$context = (strpos($guest_assignee, $user_to_notify->user_email) !== false) ? 'guest' : 'wp_user';
$base_link = alba_get_smart_card_url($post_id, $context);
alba_board_send_notification(
$user_to_notify->user_email,
$subject,
$user_to_notify->display_name,
$post->post_title,
$post->post_content,
$base_link . '#comment-' . $comment_id,
'new_comment',
$commentdata['comment_content']
);
}
add_action( 'comment_post', 'alba_board_notify_on_new_comment', 10, 3 );
/**
* 4. TRIGGER 3: Guest Assignee Update
*/
function alba_board_notify_guest_assignee($card_id, $new_guests, $old_guests) {
$options = get_option('alba_board_notifications');
if (empty($options['notify_on_card'])) return;
$post = get_post($card_id);
if (!$post || $post->post_type !== 'alba_card') return;
$new_emails = array_filter(array_map('trim', explode(',', $new_guests)));
$old_emails = array_filter(array_map('trim', explode(',', (string)$old_guests)));
$emails_to_notify = array_diff($new_emails, $old_emails);
if (empty($emails_to_notify)) return;
/* translators: Card title. */
$subject = sprintf(__('New task assigned: %s', 'alba-board'), $post->post_title);
$subject = apply_filters('alba_board_notification_subject_guest_card', $subject, $card_id, $post);
// Context MUST be 'guest' to prevent sending them admin login URLs
$card_link = alba_get_smart_card_url($card_id, 'guest');
foreach ($emails_to_notify as $email) {
if (is_email($email)) {
$name_parts = explode('@', $email);
$user_name = ucfirst($name_parts[0]);
alba_board_send_notification(
$email,
$subject,
$user_name,
$post->post_title,
$post->post_content,
$card_link,
'new_card'
);
}
}
}
add_action('alba_board_guest_assigned', 'alba_board_notify_guest_assignee', 10, 3);