Skip Navigation

[Resolved] some custom PHP has stopped working

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 5 years ago.

Assisted by: Christian Cox.

Author
Posts
#1374945

A couple of years ago, I had a very complicated problem of trying to send an email to the author of a grandfather post - and Luo Yang very kindly helped me work it out in this thread:

https://toolset.com/forums/topic/sending-cred-notification-email-to-grandparent-with-category/page/2/

The code he eventually worked out for me went into my functions.php and looked like this:

// Change notification email to evaluator author when submitting assignment
add_filter( 'cred_mail_header', 'notify_grandparent', 10, 4 );
function notify_grandparent( $headers, $formid, $postid, $num_notification ) {
    if($formid != 704 ) return; 
    $num_notification = 0;
    $current_user = get_current_user_id();
    //get the "lab-categories" terms of current "ASSIGNMENTRESULTS" post
    $terms = get_the_terms( $postid, 'lab-categories' );
    $term_slugs = array();
    foreach ( $terms as $key => $taxonomy ){
            $term_slugs[] = $taxonomy->slug ;
    }
    //with above terms, pass it to get_posts function as parameter "tax_query":
    $registrations = get_posts( array(
        'author' => $current_user,
        'post_type' => 'registration',
        'tax_query' => array(
            array(
                'taxonomy' => 'lab-categories',
                'field'    => 'slug',
                'terms'    => $term_slugs,
            ),
        ),
    ));
     
    $evaluator_id = get_post_meta( $registrations[0]->ID, "_wpcf_belongs_evaluator_id", true); // assumes Evaluator CPT slug is 'evaluator'
    $evaluator = get_post( $evaluator_id );
    $evaluator_author = get_user_by( 'ID', $evaluator->post_author );
    $newheaders = array( 'To: ' . $evaluator_author->user_email );
     
    return array_merge($headers, $newheaders);
}

This has been working perfectly for me for the last three years - but it has suddenly stopped working and the CRED form notification is going to the author of the post twice, instead of to the author of the post as well as to the author of the appropriate grandfather post.

Can you see anything in the code that would have stopped working for some reason?

Thanks SO much!

Elise

#1375137

Hello, please go to wp-admin > Toolset > Post Relationships. If you see a list of post relationships here, it means your site has been migrated to the new post relationships system. If that is the case, this line should probably be changed to work with new post relationships:

$evaluator_id = get_post_meta( $registrations[0]->ID, "_wpcf_belongs_evaluator_id", true); // assumes Evaluator CPT slug is 'evaluator'

The old system of relating posts relied on the _wpcf_belongs_posttypeslug_id meta key to create relationships. That is no longer used in the new system. Instead of get_post_meta, this line should use the post relationships API to get the related Evaluator post ID. The post relationships API toolset_get_parent_post_by_type may be appropriate here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_parent_post_by_type
Or maybe toolset_get_related_posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

It depends on the relationship type in the new post relationships system. Can you tell me:
- What type of relationship exists between the Registration and the Evaluator - is it one-to-many, or many-to-many?
- Which post type is the parent and which post type is the child? If you are not sure, edit this relationship in Toolset > Relationships and check the checkbox "I understand that changes to these settings may delete post associations in this Relationship", then click "Edit Settings". You can then see details about the parent and child post types. Do not make any changes in this settings panel.
- What is the post relationship slug? You can see it towards the top of the page.

#1375151

I have NOT migrated my site. I think it's a great idea - but I had written this site a while back with the old set-up, and at the moment I just don't have the time to re-write the whole site for the new structure - so I've left it as is.
I double-checked under Toolset>Relationships and it said:

Your site has relationships that use the old storage.

Registration is a parent to Evaluator, and a Grandparent to AssignmentResults.

I think I detailed all of this when the code was first written for me - I'll try to repost some of the explanation I posted in that older thread. Below is from the older thread:

the user LabTester is the author of two Registration posts:

Post ID 2433 - which is in lab-categories "Book Lab"
and
Post ID 93255 - which is in lab-categories "Lyric Lab"

If you login as that user:

username: labtester
password: wmt

you will automatically be redirected to this Lab home page with this chart at the top:
"Lyric Lab Student Status Chart view"

Scroll down the page to the chart labelled "Lyric Lab ASSIGNMENT SUMMARY"

then click the "REVIEW" link under "# 1: Overview Assignment

this will take you to an on-the-fly page with the cred form for post ID 2706

scroll down to the bottom where there is a drop down box labelled "Ready to Submit Assignment?" and choose "Assignment is ready for evaluation" from the drop-down box and click "Submit"

This post - ID 2706 - is in the lab-categories "Lyric Lab"

So the way the process needs to work is that it needs to find the Registrations authored by the logged in user (Labtester) - and find the one that is also in lab-categories "Lyric Lab" (which is Post ID 93255).

THEN it needs to find out who that post BELONGS to (Post 93255 belongs to user Larry Cousineau) and send an email to THAT user.

What it was doing instead was sending an email to the user who owns the Registration in the lab-categories "Book Lab" (Elise Dewsberry).

The most recent modification you made to the code is making it now send the email to the currently logged in user.

So what I want is:
- when updating post # 2706 through Cred Form #704
- determine the logged in user (LabTester) and the lab-categories of post #93255 (which is "Lyric Lab")
- go to Registration posts authored by Lab Tester and find the one that is in the same lab-categories (that would be post #93255)
- find out who that post (#93255) belongs to (not is authored by - but BELONGS TO as in PARENT) - who is Larry Cousineau
- send an email to Larry Cousineau

Is that more clear?

Thanks.

Elise

#1375157

Okay thanks for the explanation, I'll shift over to the old relationship mindset. I have a few questions about the setup, and need some clarification. You said:
Registration is a parent to Evaluator, and a Grandparent to AssignmentResults.

In the code I see:

 $evaluator_id = get_post_meta( $registrations[0]->ID, "_wpcf_belongs_evaluator_id", true); // assumes Evaluator CPT slug is 'evaluator'
    $evaluator = get_post( $evaluator_id );
    $evaluator_author = get_user_by( 'ID', $evaluator->post_author );

This code seems to indicate that Evaluator is a parent to Registration, not the other way around. Please double-check and provide the confirmed hierarchy of relationships between Evaluator, Registration, and AssignmentResults.

What it was doing instead was sending an email to the user who owns the Registration in the lab-categories "Book Lab" (Elise Dewsberry)...The most recent modification you made to the code is making it now send the email to the currently logged in user.
This isn't quite clear to me, let me restate it to be sure we're on the same page. Luo had it working originally, but then it stopped working correctly. The first sign it stopped working correctly was it sent this notification email to Elise Dewsberry instead of Larry Cousineau. Then after updating Toolset plugins, it started sending the email to the currently logged-in user instead of Elise Dewsberry. So the notification broke, then it broke in a different way after plugin update(s). Do I understand correctly?

- find out who that post (#93255) belongs to (not is authored by - but BELONGS TO as in PARENT) - who is Larry Cousineau
"Larry Cousineau" is the title of an Evaluator post, and the Larry Cousineau Evaluator post is a parent to the Registration post #93255. Am I understanding correctly?


- send an email to Larry Cousineau

So the Evaluator post Larry Cousineau is authored by the WordPress User Larry Cousineau, and you want to send the email to that WordPress User's email address. Correct?

#1375181

Sorry for the confusion - this happened at two separate times ...I'll try to clarify.

When the thread first started three years ago, Luo sent me code that ALMOST worked, but didn't allow for the fact that a Student might be signed up for more than one Lab - so although it would send a notification to the Evaluator of a Lab, it wasn't necessarily sending it to the Evaluator of the SPECIFIC Lab.

So - if Student A was signed up for Book Lab (Evaluator Elise) and Lyric Lab (evaluator Larry) - and they were submitting an assignment to the Lyric Lab, the code Luo first sent me would sent an email to Elise instead of Larry because the Book Lab post was the first one that came up in the loop.
Luo immediately modified the code to first check the CATEGORY of the assignment being submitted, and look for the Registration for the Lab with the same CATEGORY designation - thus making sure to send an email to Larry (and not Elise) when submitting an assignment for the Lyric Lab.

That new code worked perfectly - for the last three years.

About a month ago, I noticed that all notifications had stopped going out ... the first reason was because the password for the email address had changed and I had to update it on the website. That fixed the first part of the problem and notifications started going out again - but instead here is what is happening now:

When Student A submits an assignment for the Lyric Lab - the notification email goes to:
- the student (the author of the AssignmentResult post)
- the administrator (info@writingmusicaltheatre.com)
- the STUDENT again - so the student gets TWO emails - instead of the third email going to the EVALUATOR of the Lab.
I think this must be because the code is now finding the author of the AssignmentResult post - instead of finding the CATEGORY of the AssignmentResult post (Lyric Lab); and then finding the REGISTRATION that has the same category - and sending the email to the Evaluator who BELONGS to that Registration.

Does that make more sense, or have I just made it worse? 🙂

If it helps - here is how the CPTs function:

REGISTRATION: this is the post that the student creates when they register for a specific Lab. This post clarifies which Lab it belongs to through the taxonomy CATEGORY (Lyric Lab); but it also identifies which Evaluator BELONGS to this Registration. (For Lyric Lab - that is Larry Cousineau). (separately, the AUTHOR of the Registration post would be the person who actually designed the lab course - which in this case is Scott Guy, not Larry Cousineau).

ASSIGNMENTRESULT: when a student completes an assignment for a lab, they make a post here. Which Lab this belongs to is identified through the same taxonomy CATEGORY.

So - when a student submits a new ASSIGNMENTRESULT post - it is necessary to check the taxonomy category (Lyric Lab) and then check the REGISTRATION for that student that has the SAME taxonomy category (Lyric Lab) to see which Evaluator BELONGS to that Registration post (in the case of the Lyric Lab, that is Larry Cousineau) - so that a notification email can be sent to Larry Cousineau (as well as the student)

Elise

#1375813

Okay I can see from your other ticket that Duplicator was causing problems. How would you like to try to transfer a copy of the site so I can run some additional tests locally without breaking the live site? I can try to log in and use Duplicator Pro, which has some improvements over the basic software, or I can work with a clone from the All-in-one WP Migration tool, or I can work with a SQL dump of your database. I'll open private reply fields here so you can share login credentials, or let me know how you would like to proceed.