| 1 |
<?php |
| 2 |
/** |
| 3 |
* Submit Form |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\Thrive_Leads\Triggers\Submit_Form |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_Thrive_Leads_Submit_Form extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'thrive_leads'; |
| 15 |
public $trigger = 'thrive_leads_submit_form'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the trigger |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
|
| 24 |
automatorwp_register_trigger( $this->trigger, array( |
| 25 |
'integration' => $this->integration, |
| 26 |
'label' => __( 'User submits a form', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User submits <strong>a form</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Post title. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User submits %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ), |
| 30 |
/* translators: %1$s: Post title. */ |
| 31 |
'log_label' => sprintf( __( 'User submits %1$s', 'automatorwp' ), '{post}' ), |
| 32 |
'action' => 'tcb_api_form_submit', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 1, |
| 36 |
'options' => array( |
| 37 |
'post' => automatorwp_utilities_ajax_selector_option( array( |
| 38 |
'field' => 'post', |
| 39 |
'name' => __( 'Form:', 'automatorwp' ), |
| 40 |
'option_none_value' => 'any', |
| 41 |
'option_none_label' => __( 'any form', 'automatorwp' ), |
| 42 |
'action_cb' => 'automatorwp_thrive_leads_get_forms', |
| 43 |
'options_cb' => 'automatorwp_thrive_leads_options_cb_form', |
| 44 |
'default' => 'any' |
| 45 |
) ), |
| 46 |
'times' => automatorwp_utilities_times_option(), |
| 47 |
), |
| 48 |
'tags' => array_merge( |
| 49 |
automatorwp_thrive_leads_tags(), |
| 50 |
automatorwp_utilities_times_tag() |
| 51 |
) |
| 52 |
) ); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Trigger listener |
| 58 |
* |
| 59 |
* @since 1.0.0 |
| 60 |
* |
| 61 |
* @param array $post |
| 62 |
*/ |
| 63 |
public function listener( $post ) { |
| 64 |
|
| 65 |
// Login is required |
| 66 |
if ( ! is_user_logged_in() ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$user_id = get_current_user_id(); |
| 71 |
|
| 72 |
$form_id = $post['thrive_leads']['tl_data']['_key']; |
| 73 |
$form_name = $post['thrive_leads']['tl_data']['form_name']; |
| 74 |
$group_id = $post['thrive_leads']['tl_data']['main_group_id']; |
| 75 |
$group_name = $post['thrive_leads']['tl_data']['main_group_name']; |
| 76 |
|
| 77 |
// Trigger submit form event |
| 78 |
automatorwp_trigger_event( array( |
| 79 |
'trigger' => $this->trigger, |
| 80 |
'user_id' => $user_id, |
| 81 |
'form_id' => $form_id, |
| 82 |
'form_name' => $form_name, |
| 83 |
'group_id' => $group_id, |
| 84 |
'group_name' => $group_name, |
| 85 |
) ); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* User deserves check |
| 91 |
* |
| 92 |
* @since 1.0.0 |
| 93 |
* |
| 94 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 95 |
* @param stdClass $trigger The trigger object |
| 96 |
* @param int $user_id The user ID |
| 97 |
* @param array $event Event information |
| 98 |
* @param array $trigger_options The trigger's stored options |
| 99 |
* @param stdClass $automation The trigger's automation object |
| 100 |
* |
| 101 |
* @return bool True if user deserves trigger, false otherwise |
| 102 |
*/ |
| 103 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 104 |
|
| 105 |
// Don't deserve if post is not received |
| 106 |
if( ! isset( $event['form_id'] ) ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
// Bail if post doesn't match with the trigger option |
| 111 |
if( $trigger_options['post'] !== 'any' && absint( $event['form_id'] ) !== absint( $trigger_options['post'] ) ) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
return $deserves_trigger; |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Register the required hooks |
| 121 |
* |
| 122 |
* @since 1.0.0 |
| 123 |
*/ |
| 124 |
public function hooks() { |
| 125 |
|
| 126 |
// Log meta data |
| 127 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 128 |
|
| 129 |
parent::hooks(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Trigger custom log meta |
| 134 |
* |
| 135 |
* @since 1.0.0 |
| 136 |
* |
| 137 |
* @param array $log_meta Log meta data |
| 138 |
* @param stdClass $trigger The trigger object |
| 139 |
* @param int $user_id The user ID |
| 140 |
* @param array $event Event information |
| 141 |
* @param array $trigger_options The trigger's stored options |
| 142 |
* @param stdClass $automation The trigger's automation object |
| 143 |
* |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 147 |
|
| 148 |
// Bail if action type don't match this action |
| 149 |
if( $trigger->type !== $this->trigger ) { |
| 150 |
return $log_meta; |
| 151 |
} |
| 152 |
|
| 153 |
$log_meta['form_id'] = ( isset( $event['form_id'] ) ? $event['form_id'] : 0 ); |
| 154 |
$log_meta['form_name'] = ( isset( $event['form_name'] ) ? $event['form_name'] : '' ); |
| 155 |
$log_meta['group_id'] = ( isset( $event['group_id'] ) ? $event['group_id'] : 0 ); |
| 156 |
$log_meta['group_name'] = ( isset( $event['group_name'] ) ? $event['group_name'] : '' ); |
| 157 |
|
| 158 |
|
| 159 |
return $log_meta; |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
new AutomatorWP_Thrive_Leads_Submit_Form(); |