I have what appears to be a complicated problem with custom email notifications, where the issue is centered on a single form in the project under development but not on a single custom notification hook among the options offered within Toolset. That is, I have hooked into both cred_notification_recipients and cred_mail_header in attempt to accomplish what is needed for the form in question, and have had partial success with both.
In the present ticket I’m going to refer to my most recent previous ticket, ‘Several cred_success_redirect instances in project worked at first but don’t now’, which was resolved yesterday. I brought the custom redirects problem first, on the hunch that what I could learn from it might help me understand this other, possibly more difficult custom notifications problem. And it has helped in that way. I’m not yet at a full solution, though.
A factor in the complexity of the question is that there are actually two installations involved, one the development site (‘staging’) and the other the production site. I am finding that not every change made to both has the same effect in both — which seems to reflect that differing site usage and data are factors potentially affecting script execution here. Although the working solution needs to carry over to the production site, finally, my principal concern here will be with the development site. (I recognize that there may be a point where a further ticket needs to be opened.)
The result needed is a single notification sent to multiple email addresses that will be selected via checkboxes in the form. At the core of the problem is not that this does not work. I have the multiple-recipients notification working both via the cred_notification_recipients hook and the cred_mail_header hook. The core problem has been that with either hook, when this one custom-scripted notification is active, all other notifications for all CRED forms throughout the project are interrupted somehow. I could have either the custom notification active or all the rest of the site’s non-custom form notifications operational, but not both.
The partial solution I now have has come with adopting the method recommended by Minesh in the previous ticket — giving a function at issue a different location within the site files. All of my notifications, custom and standard, will work at the same time in one arrangement and (so far) in one arrangement only: when the custom notification uses cred_mail_header, and when the function is relocated from its original plugin file to Toolset’s Custom Functions in the site files. This is complicated by the fact that it is only so when applied on the development site. The same solution doesn’t yet work when applied on the production site. But I want to bracket that fact and set it to the side for now, because I think the thing I still need to understand better is why and how a conflict can show up between the custom notification and standard CRED notifications in the first place.
As I noted, the partial solution is only effective for the version of the custom notification that uses cred_mail_header. The version that uses cred_notification_recipients seems, from what I can tell, not to fire at all when moved to the Toolset Custom Functions files. I may not be interpreting correctly the error log information I know how to gather, though.
For reference, here are the two functions:
function ic_task_record_new_assignable_workers_notif_recip( $recipients, $notification, $form_id, $post_id ) {
if ( in_array( $form_id, [ ###0, ###2 ] ) ) {
if ( isset( $notification[ 'name' ] ) && $notification[ 'name' ] == 'multiple worker task availability' ) {
if ( $form_id == ###0 && isset( $_POST[ 'assignable-workers' ] ) ) {
$selected_wrkrs = $_POST[ 'assignable-workers' ];
} elseif ( $form_id == ###2 && isset( $_POST[ 'assignable-workers-supplemental' ] ) ) {
$selected_wrkrs = $_POST[ 'assignable-workers-supplemental' ];
}
$active_wrkrs = explode( ',', render_view( [ 'name' => 'user-listing-active-worker' ] ) );
$recipients = array_intersect( $active_wrkrs, $selected_wrkrs );
foreach ( $recipients as &$recipient ) {
$email = types_render_field( 'worker-email', [ 'item' => $recipient, 'output' => 'raw' ] );
$recipient = [
'to' => 'to',
'address' => $email,
'name' => get_the_title( $recipient )
];
}
unset( $recipient );
return $recipients;
}
}
}
add_filter( 'cred_notification_recipients', 'ic_task_record_new_assignable_workers_notif_recip', 10, 4 );
and
function ic_task_record_new_assignable_workers_notif_recip( $headers, $form_id, $post_id, $notif_name, $notif_num ) {
if ( in_array( $form_id, [ ###0, ###2 ] ) && $notif_name == 'multiple worker task availability' ) {
$header_add = [];
if ( $form_id == ###0 && isset( $_POST[ 'assignable-workers' ] ) ) {
$selected_wrkrs = $_POST[ 'assignable-workers' ];
} elseif ( $form_id == ###2 && isset( $_POST[ 'assignable-workers-supplemental' ] ) ) {
$selected_wrkrs = $_POST[ 'assignable-workers-supplemental' ];
}
$active_wrkrs = explode( ',', render_view( [ 'name' => 'user-listing-active-worker' ] ) );
$recipients = array_intersect( $active_wrkrs, $selected_wrkrs );
foreach ( $recipients as &$item ) {
$email = types_render_field( 'worker-email', [ 'item' => $item, 'output' => 'raw' ] );
$item = $email;
}
unset( $item );
$list = implode( ', ', $recipients );
$header_add = [ 'To: ' . $list ];
return array_merge( $headers, $header_add );
}
}
add_filter( 'cred_mail_header', 'ic_task_record_new_assignable_workers_notif_recip', 8, 5 );
I can provide login credentials.