Data objects represent a structured object that is provided by a Trigger
.
After a WordPress action is triggered, parameters will be passed to the Trigger
which will transform them into Data_Object
.
The reason why we’re doing this is to better understand what each trigger provides so we can later use this data to match what actions are compatible.
Data is set inside the process_params
function of the Trigger
through the constructor of the Data_Object
. For special situations, we have the get_value
and set_value
functions which can be overwritten to fit the current structure.
Creating Your First Data Object
To create your own Data_Object
you need to extend ThriveAutomatorItemsData_Object
and implement the required methods.
abstract public static function get_id(): string
– should return a unique identifier that will be used as a key in arrays. To avoid conflicts or overwrites, we suggest using a prefix
public static function get_id(): string {
return 'wp/user-data';
}
abstract public static function get_fields(): array
– returns an array ofData_Field
keys that the currentData_Object
provides. Filters might want to know this in order to provide filtering. AnAction
might want to get those fields so it can know what it can use.
public static function get_fields(): array {
return return [ Username_Data_Field::get_id(), User_Email_Data_Field::get_id() ];
}
public static function get_nice_name(): string
– should return a name that is displayed inside the editor, inside dropdowns when mapping fieldspublic static function get_nice_name() {
return '';
}abstract public static function create_object( $param ): array
– this method is called inside the constructor and it’s used to create the object from the raw data received from theTrigger
. This object should contain information about all the fields mentioned in theget_fields
method from above.
public static function create_object( $user_id ): array {
$user = get_userdata($user_id);
if( $user === null ) {
$user_data = [];
} else {
$user_data = [
'wp/username' => $user->user_login,
'wp/email' => $user->user_email,
];
}
return $user_data;
}
Other Methods
public function replace_dynamic_data( $value )
– on automation execution, this function will replace dynamic data specific toData_Object1
structure.
The$value
parameter can contain one or multiple shortcodes which are replaced with actual values.
Registering the Data Object
To register a data object so it can be used by triggers, actions and filters, we should use the thrive_automator_register_data_object
function which receives as the only parameter, the class name.