| 1 |
<?php |
| 2 |
/** |
| 3 |
* Follow-up adapter for Contact Form 7. |
| 4 |
* |
| 5 |
* @package Forge12\DoubleOptIn\Integration |
| 6 |
* @since 5.6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare( strict_types=1 ); |
| 10 |
|
| 11 |
namespace Forge12\DoubleOptIn\Integration; |
| 12 |
|
| 13 |
use Forge12\DoubleOptIn\FollowUp\FollowUpAction; |
| 14 |
use Forge12\DoubleOptIn\FollowUp\FollowUpAdapterInterface; |
| 15 |
use Forge12\DoubleOptIn\FollowUp\FollowUpAttempt; |
| 16 |
use Forge12\DoubleOptIn\FollowUp\FollowUpResult; |
| 17 |
use Forge12\DoubleOptIn\FollowUp\FollowUpStatus; |
| 18 |
use forge12\contactform7\CF7DoubleOptIn\OptIn; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* CF7 has one observable result per submission: `get_status()` of the |
| 26 |
* WPCF7_Submission. Mail 2 and storage extensions (Flamingo, CFDB7) run |
| 27 |
* inside that same submission and report nothing of their own, so the |
| 28 |
* follow-up is one action, `cf7:submission`. |
| 29 |
* |
| 30 |
* CF7 itself stores no submissions — no entry is promised here. |
| 31 |
*/ |
| 32 |
final class CF7FollowUpAdapter implements FollowUpAdapterInterface { |
| 33 |
|
| 34 |
public const ACTION_ID = 'cf7:submission'; |
| 35 |
|
| 36 |
/** @var CF7Integration */ |
| 37 |
private $integration; |
| 38 |
|
| 39 |
public function __construct( CF7Integration $integration ) { |
| 40 |
$this->integration = $integration; |
| 41 |
} |
| 42 |
|
| 43 |
public function getIntegration(): string { |
| 44 |
return 'cf7'; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* {@inheritdoc} |
| 49 |
*/ |
| 50 |
public function planActions( OptIn $optIn ): array { |
| 51 |
return array( |
| 52 |
new FollowUpAction( self::ACTION_ID, FollowUpAction::KIND_MAIL, 'Contact Form 7 mail' ), |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* {@inheritdoc} |
| 58 |
*/ |
| 59 |
public function execute( OptIn $optIn, array $actions, FollowUpAttempt $attempt ): array { |
| 60 |
$result = AbstractFormIntegration::runAsReplay( |
| 61 |
function () use ( $optIn ) { |
| 62 |
return $this->integration->replaySubmission( $optIn ); |
| 63 |
} |
| 64 |
); |
| 65 |
|
| 66 |
$out = array(); |
| 67 |
foreach ( $actions as $action ) { |
| 68 |
$out[ $action->getId() ] = $result; |
| 69 |
} |
| 70 |
return $out; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Map CF7's submission status. Pure — unit-tested directly. |
| 75 |
*/ |
| 76 |
public static function mapStatus( string $status ): FollowUpResult { |
| 77 |
switch ( $status ) { |
| 78 |
case 'mail_sent': |
| 79 |
return FollowUpResult::succeeded( 'cf7_mail_sent' ); |
| 80 |
case 'mail_failed': |
| 81 |
// Mail 1 was not handed over (CF7 sends Mail 2 only after |
| 82 |
// Mail 1 succeeded). Storage extensions on wpcf7_submit may |
| 83 |
// still have written a record, so no automatic retry. |
| 84 |
return FollowUpResult::failedPermanent( 'mail_handoff_failed' ); |
| 85 |
case 'spam': |
| 86 |
return FollowUpResult::failedPermanent( 'spam_rejected' ); |
| 87 |
case 'validation_failed': |
| 88 |
case 'acceptance_missing': |
| 89 |
return FollowUpResult::failedPermanent( 'validation_failed' ); |
| 90 |
case 'aborted': |
| 91 |
return FollowUpResult::failedPermanent( 'submission_aborted' ); |
| 92 |
default: |
| 93 |
return FollowUpResult::unknown( 'action_outcome_unknown' ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* {@inheritdoc} |
| 99 |
* |
| 100 |
* Pending attachment copies go only after the mail that attaches |
| 101 |
* them was handed over. |
| 102 |
*/ |
| 103 |
public function onSettled( OptIn $optIn, array $statusByAction ): void { |
| 104 |
if ( ( $statusByAction[ self::ACTION_ID ] ?? '' ) === FollowUpStatus::SUCCEEDED ) { |
| 105 |
$this->integration->processFilesOnConfirm( '', $optIn ); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|