Skip to content

Thrive Automator Data Objects

popmihaiadr edited this page Aug 16, 2021 · 5 revisions

About

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.

Creating your first Data_Object

In order to create your own Data_Object you need to extend Thrive\Automator\Items\Data_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 of Data_Field keys that the current Data_Object provides. Filters might want to know this in order to provide filtering. An Action might want to get those fields so it can know what it can use.
	public static function get_fields(): array {
		return ['wp/username', 'wp/email'];
	}
  • 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 the Trigger. This object should contain information about all the fields mentioned in the get_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;
	}

Registering the data object

In order 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.