Skip to content

Instantly share code, notes, and snippets.

@pawelkmpt
Created May 12, 2023 13:45
Show Gist options
  • Save pawelkmpt/29ac23de527bf5306622c58a801cb283 to your computer and use it in GitHub Desktop.
Save pawelkmpt/29ac23de527bf5306622c58a801cb283 to your computer and use it in GitHub Desktop.
<?php
namespace ConversionXL\Institute\Plugin\Integrations\AutomateWoo\Triggers;
use AutomateWoo\Customer_Factory;
use AutomateWoo\Fields\Text;
use AutomateWoo\Trigger;
defined( 'ABSPATH' ) || exit;
class FormEntryCreated extends Trigger {
public $supplied_data_items = [
'customer',
'entry',
'form',
'subscription',
];
public function load_admin_details(): void {
parent::load_admin_details();
$this->title = 'Entry Created';
$this->group = 'Gravity Forms';
}
public function load_fields() {
$subscription_field_id = ( new Text() )
->set_title( 'Form field: Subscription ID' )
->set_name( 'form_field_subscription_id' )
->set_description( 'Provide if form has a field with subscription ID' );
$this->add_field( $subscription_field_id );
parent::load_fields();
}
public function register_hooks() {
add_action( 'gform_entry_created', [ $this, 'catch_hooks' ], 10, 2 );
}
public function catch_hooks( array $entry, array $form ): void {
$data_layer = [
'customer' => Customer_Factory::get_by_user_id( $entry['created_by'] ),
'entry' => $entry,
'form' => $form,
'subscription' => null,
];
/*
* Unfortunately this is empty at the moment of execution.
* It looks like `$options` property is never populated.
*
* Also `Trigger::get_option()` is deprecated.
* Suggestion: `$workflow->get_trigger_option()`
* but we don't have `Workflow` object at this point.
*
* Take a look at `Trigger::maybe_run()`
* and then `Workflow::maybe_run()`.
*
* Workflow will not run without `subscription` in `$data_layer`.
*
* Suggestion: define common GField CSS class instead of field ID
* like `SUBSCRIPTION_ID_FIELD_ID` in `CancelFormEntryCreated`,
* based on the CSS class find the relevant field
* and then get subscription.
*/
$field_subscription_id = $this->options[ 'form_field_subscription_id' ] ?? false;
if ( ! $field_subscription_id ) {
return;
}
if ( $field_subscription_id ) {
$subscription = wcs_get_subscription( $entry[ $field_subscription_id ] );
if ( $subscription ) {
$data_layer['subscription'] = $subscription;
}
}
$this->maybe_run( $data_layer );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment