Created
June 3, 2020 16:25
-
-
Save tobeyadr/1d93b8e53a6ebb3b75591074afe4b04a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Demonstrates... | |
* | |
* Create a contact record | |
* Add a tag | |
* Remove a tag | |
* | |
* @param $email | |
* @param $first | |
* @param $last | |
*/ | |
function create_contact_record( $email, $first, $last ) { | |
// Check if contact exists... if not, we can create a new one! | |
$contact = \Groundhogg\get_contactdata( $email ); | |
if ( ! $contact ) { | |
// This will provide you with a usable object and add the contact to the DB at the same time... | |
$contact = new \Groundhogg\Contact( [ | |
'first_name' => $first, | |
'last_name' => $last, | |
'email' => $email | |
] ); | |
} else { | |
// If the contact exists, we should update it | |
$contact->update( [ | |
'first_name' => $first, | |
'last_name' => $last, | |
] ); | |
} | |
// Custom fields are stored in meta data, so add relevant custom info via the `add_meta()` method. | |
$contact->add_meta( 'custom_field_key', 'Custom Field Value' ); | |
// To add a tag use the `add_tag()` method, you can pass a string name or an exisitng tag ID, both are valid. | |
$contact->add_tag( 1234 ); | |
$contact->add_tag( 'My Tag' ); | |
// There is also the `apply_tag()` method which does the same thing as `add_tag()`... | |
$contact->apply_tag( 1234 ); | |
$contact->apply_tag( 'My Tag' ); | |
// Remove tag works similarly | |
$contact->remove_tag( 1234 ); | |
$contact->remove_tag( 'My Tag' ); | |
} | |
/** | |
* This hook passes the contact object when useing the `add_tag()` method as well as the tag_id | |
* | |
* @param $contact \Groundhogg\Contact the modified contact record | |
* @param $tag_id int the ID of the tag | |
*/ | |
function do_something_when_tag_applied( $contact, $tag_id ){ | |
// do stuff | |
$contact->add_note( 'Got tag ' . $tag_id ); | |
} | |
add_action( 'groundhogg/contact/tag_applied', 'do_something_when_tag_applied', 10, 2 ); | |
/** | |
* This hook passes the contact object when using the `remove_tag()` method as well as the tag_id | |
* | |
* @param $contact \Groundhogg\Contact the modified contact record | |
* @param $tag_id int the ID of the tag | |
*/ | |
function do_something_when_tag_removed( $contact, $tag_id ){ | |
// do stuff | |
$contact->add_note( 'Removed tag ' . $tag_id ); | |
} | |
add_action( 'groundhogg/contact/tag_removed', 'do_something_when_tag_removed', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment