Skip Navigation

[Resolved] Conflicting form notifications — cred_notification_recipients & cred_mail_header

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by Minesh 6 months, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2701438

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.

#2701532

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I'm not sure yet where you added the email notification hooks. If you added those hooks to your custom plugin you made, I'm afraid that you will have to deal with that on your own as that is custom code and as per our support policy we do not entertain custom code.

However - I would like to inform you that whatever Toolset hooks you use either you can add it to your theme's functions.php file or to the "Custom Code" section offered by Toolset.

Even using "Custom Code" section - you should keep in the mind the execution timings.
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#snippet-execution-timing

What if you try to add those email notification hooks to your current theme's functions.php file and check if that help you to resolve your issue.

#2701973

I had not considered trying it in the functions.php file. (A principal reason for setting up a plugin to hold the functions we’re using on CRED hooks is to avoid an unwieldy and hard-to-organize functions.php file, of course.)

The result is the same, in any case, whether adding in functions.php or in the Toolset Custom Code section. Only the second of my two alternative functions, the one hooked to cred_mail_header, does not prevent other forms’ notification emails from being generated when it is enabled. And that is true, moreover, only on the staging site. When applied in the same way on the production site, the notification modified by the custom mail header function will be generated when data is saved via its form, but notifications for all other forms throughout the site will then no longer be generated.

I do have a new piece of information — an error I had not noticed before my latest round of testing. This was logged on the production site when I tried submitting another, standard-notification form while I had the custom-notification function in question enabled:

[11-Jun-2024 04:37:50 UTC] PHP Fatal error:  Uncaught TypeError: array_merge(): Argument #1 must be of type array, null given in …/wp-content/plugins/cred-frontend-editor/library/toolset/cred/embedded/classes/common/Mail_Handler.php:150
Stack trace:
#0 …/wp-content/plugins/cred-frontend-editor/library/toolset/cred/embedded/classes/common/Mail_Handler.php(150): array_merge(NULL, Array)
#1 …/wp-content/plugins/cred-frontend-editor/library/toolset/cred/embedded/classes/common/Mail_Handler.php(181): CRED_Mail_Handler->buildMail()
#2 …/wp-content/plugins/cred-frontend-editor/application/controllers/notification_manager/post.php(660): CRED_Mail_Handler->send()
#3 …/wp-content/plugins/cred-frontend-editor/application/controllers/notification_manager/queue.php(80): CRED_Notification_Manager_Post->send_notifications(13913, 3218, Array)
#4 …/wp-includes/class-wp-hook.php(324): CRED_Notification_Manager_Queue->send('')
#5 …/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#6 …/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#7 …/wp-includes/load.php(1270): do_action('shutdown')
#8 [internal function]: shutdown_action_hook()
#9 {main}
  thrown in …/wp-content/plugins/cred-frontend-editor/library/toolset/cred/embedded/classes/common/Mail_Handler.php on line 150

When the same thing is done on the staging site, this error is not thrown .

Can you help me understand whether this error is relevant and what it may indicate if so?

#2701981

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm not sure whats going wrong with your setup.

Have you try to check with minimum setup in order to check possible conflict?

Could you please try to resolve your issue by deactivating all third-party plugins as well as the default theme to check for any possible conflicts with any of the plugins or themes?
- Do you see any difference?

If no:
To check if its server related or there is issue with code:

Here is a sandbox site and you can auto-login to it using the following link:
- hidden link

You may try to add couple of forms and setup couple of notifications for both the forms and then add those hooks to "Custom Code" section and check if those hooks are fired in right manner or do you able to reproduce the issue you are facing currently with email notifications for Toolset Forms.

#2702335

Thank you, Minesh. For reasons unrelated to the project, I’ll have to wait another day or two before resuming testing following your recommendations.

Just for clarity, meanwhile: Is there anything further to be said that might help me understand the possible relevance or irrelevance of the error log item I include in the previous reply?

#2702336

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The fatal error possibly looks related as its from Mail_Handler.php:150 but you will have to check what are the hooks you are using even if you added for other forms and you should make sure it should only run when required.