abstract \Groundhogg\Base_Object_With_Meta

📅 Published on

📝 Last updated

Documentation

»

Classes

»

abstract \Groundhogg\Base_Object_With_Meta

extends Base_Object

Extends Base_Object with a meta table — an arbitrary key/value store attached to the object. Contacts, emails, funnels, steps, broadcasts, and submissions all extend this class. Meta values can store any serialisable data and are retrieved or updated using a simple key-based API.

Methods

MethodReturnsDescription
get_meta( $key = false, $single = true )mixedReturns a meta value by key. If $key is omitted, returns all meta as an associative array. If $single is false, returns an array of all values for that key.
update_meta( $key, $value = false )boolUpdates (or creates) a meta key/value pair.
update_meta_if_empty( $key, $value = false )boolUpdates a meta value only if the key does not already have a value.
add_meta( $key, $value = false )boolAdds a new meta entry without overwriting existing values for the same key.
delete_meta( $key )boolDeletes all meta entries for the given key.
get_all_meta()arrayReturns all meta key/value pairs for this object.
schedule_meta_redaction( string $key, int $ttl )boolSchedules a meta key to be automatically deleted after $ttl seconds.

Usage

Here’s a pseudo code example for a hypothetical integration that stores and reads custom meta on a contact.

<?php

$contact = \Groundhogg\get_contactdata( 'jane@example.com' );

// Store a custom value
$contact->update_meta( 'crm_external_id', 'EXT-00123' );

// Only set a default if not already stored
$contact->update_meta_if_empty( 'lead_source', 'organic' );

// Read it back
$external_id = $contact->get_meta( 'crm_external_id' );

// Sync to an external CRM
if ( $external_id ) {
    my_plugin_sync_to_crm( $external_id, [
        'email'      => $contact->get_email(),
        'first_name' => $contact->get_first_name(),
    ] );
}

// Schedule the external ID to be redacted after 90 days
$contact->schedule_meta_redaction( 'crm_external_id', 90 * DAY_IN_SECONDS );

Was this helpful?

Let us know if this document answered your question. That’s the only way we can improve.