| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form Submission Event |
| 4 |
* |
| 5 |
* @package Forge12\DoubleOptIn\Events\Integration |
| 6 |
* @since 4.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Forge12\DoubleOptIn\Events\Integration; |
| 10 |
|
| 11 |
use Forge12\DoubleOptIn\EventSystem\Event; |
| 12 |
use Forge12\DoubleOptIn\Integration\FormDataInterface; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Class FormSubmissionEvent |
| 20 |
* |
| 21 |
* Dispatched before an OptIn record is created for a form submission. |
| 22 |
* Listeners can modify the form data or skip the opt-in process entirely. |
| 23 |
*/ |
| 24 |
class FormSubmissionEvent extends Event { |
| 25 |
|
| 26 |
/** |
| 27 |
* The form data. |
| 28 |
* |
| 29 |
* @var FormDataInterface |
| 30 |
*/ |
| 31 |
private FormDataInterface $formData; |
| 32 |
|
| 33 |
/** |
| 34 |
* The integration identifier. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
private string $integrationId; |
| 39 |
|
| 40 |
/** |
| 41 |
* Whether to skip opt-in creation. |
| 42 |
* |
| 43 |
* @var bool |
| 44 |
*/ |
| 45 |
private bool $skipOptIn = false; |
| 46 |
|
| 47 |
/** |
| 48 |
* Reason for skipping opt-in. |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
private string $skipReason = ''; |
| 53 |
|
| 54 |
/** |
| 55 |
* Constructor. |
| 56 |
* |
| 57 |
* @param FormDataInterface $formData The normalized form data. |
| 58 |
* @param string $integrationId The integration identifier (e.g., 'cf7', 'avada'). |
| 59 |
*/ |
| 60 |
public function __construct( FormDataInterface $formData, string $integrationId ) { |
| 61 |
parent::__construct(); |
| 62 |
$this->formData = $formData; |
| 63 |
$this->integrationId = $integrationId; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* {@inheritdoc} |
| 68 |
*/ |
| 69 |
public static function getWordPressHookName(): string { |
| 70 |
return 'f12_cf7_doubleoptin_form_submission'; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get the form data. |
| 75 |
* |
| 76 |
* @return FormDataInterface |
| 77 |
*/ |
| 78 |
public function getFormData(): FormDataInterface { |
| 79 |
return $this->formData; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Set modified form data. |
| 84 |
* |
| 85 |
* @param FormDataInterface $formData The modified form data. |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function setFormData( FormDataInterface $formData ): void { |
| 90 |
$this->formData = $formData; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the integration identifier. |
| 95 |
* |
| 96 |
* @return string |
| 97 |
*/ |
| 98 |
public function getIntegrationId(): string { |
| 99 |
return $this->integrationId; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get the form ID. |
| 104 |
* |
| 105 |
* @return int |
| 106 |
*/ |
| 107 |
public function getFormId(): int { |
| 108 |
return $this->formData->getFormId(); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the form type. |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
public function getFormType(): string { |
| 117 |
return $this->formData->getFormType(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Skip the opt-in creation for this submission. |
| 122 |
* |
| 123 |
* @param string $reason Optional reason for skipping. |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
public function skipOptIn( string $reason = '' ): void { |
| 128 |
$this->skipOptIn = true; |
| 129 |
$this->skipReason = $reason; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Check if opt-in should be skipped. |
| 134 |
* |
| 135 |
* @return bool True if opt-in should be skipped. |
| 136 |
*/ |
| 137 |
public function shouldSkipOptIn(): bool { |
| 138 |
return $this->skipOptIn; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get the reason for skipping opt-in. |
| 143 |
* |
| 144 |
* @return string The skip reason. |
| 145 |
*/ |
| 146 |
public function getSkipReason(): string { |
| 147 |
return $this->skipReason; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get a specific form field value. |
| 152 |
* |
| 153 |
* @param string $key The field name. |
| 154 |
* @param mixed $default Default value if field doesn't exist. |
| 155 |
* |
| 156 |
* @return mixed The field value. |
| 157 |
*/ |
| 158 |
public function getField( string $key, $default = null ) { |
| 159 |
return $this->formData->getField( $key, $default ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Check if a form field exists. |
| 164 |
* |
| 165 |
* @param string $key The field name. |
| 166 |
* |
| 167 |
* @return bool True if field exists. |
| 168 |
*/ |
| 169 |
public function hasField( string $key ): bool { |
| 170 |
return $this->formData->hasField( $key ); |
| 171 |
} |
| 172 |
} |
| 173 |
|