| 1 |
<?php |
| 2 |
/** |
| 3 |
* Registry of follow-up adapters. |
| 4 |
* |
| 5 |
* @package Forge12\DoubleOptIn\FollowUp |
| 6 |
* @since 5.6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare( strict_types=1 ); |
| 10 |
|
| 11 |
namespace Forge12\DoubleOptIn\FollowUp; |
| 12 |
|
| 13 |
use forge12\contactform7\CF7DoubleOptIn\OptIn; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Maps an integration identifier to its adapter. |
| 21 |
* |
| 22 |
* Core and the form addons register directly (container lookup in |
| 23 |
* `boot()`). Third parties can use the `f12_doi_register_follow_up_adapters` |
| 24 |
* action, which fires once, lazily, on first lookup. |
| 25 |
*/ |
| 26 |
final class FollowUpAdapterRegistry { |
| 27 |
|
| 28 |
/** @var array<string, FollowUpAdapterInterface> */ |
| 29 |
private $adapters = array(); |
| 30 |
|
| 31 |
/** @var array<string, GlobalFollowUpAdapterInterface> */ |
| 32 |
private $globals = array(); |
| 33 |
|
| 34 |
/** @var bool */ |
| 35 |
private $hookFired = false; |
| 36 |
|
| 37 |
public function register( FollowUpAdapterInterface $adapter ): void { |
| 38 |
if ( $adapter instanceof GlobalFollowUpAdapterInterface ) { |
| 39 |
$this->globals[ $adapter->getIntegration() ] = $adapter; |
| 40 |
return; |
| 41 |
} |
| 42 |
$this->adapters[ $adapter->getIntegration() ] = $adapter; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Form or global adapter by the integration stored on a row. |
| 47 |
*/ |
| 48 |
public function get( string $integration ): ?FollowUpAdapterInterface { |
| 49 |
$this->fireHookOnce(); |
| 50 |
return $this->adapters[ $integration ] ?? $this->globals[ $integration ] ?? null; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Adapters that follow up every opt-in (since 5.8.0). |
| 55 |
* |
| 56 |
* @return array<string, GlobalFollowUpAdapterInterface> |
| 57 |
*/ |
| 58 |
public function globals(): array { |
| 59 |
$this->fireHookOnce(); |
| 60 |
return $this->globals; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Find the adapter responsible for an opt-in. |
| 65 |
* |
| 66 |
* `OptIn::isType()` is a lookup per call (post type / post meta), so |
| 67 |
* each registered integration is asked at most once. |
| 68 |
*/ |
| 69 |
public function forOptIn( OptIn $optIn ): ?FollowUpAdapterInterface { |
| 70 |
$this->fireHookOnce(); |
| 71 |
foreach ( $this->adapters as $integration => $adapter ) { |
| 72 |
if ( $optIn->isType( $integration ) ) { |
| 73 |
return $adapter; |
| 74 |
} |
| 75 |
} |
| 76 |
return null; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @return string[] |
| 81 |
*/ |
| 82 |
public function integrations(): array { |
| 83 |
$this->fireHookOnce(); |
| 84 |
return array_keys( $this->adapters ); |
| 85 |
} |
| 86 |
|
| 87 |
private function fireHookOnce(): void { |
| 88 |
if ( $this->hookFired ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
$this->hookFired = true; |
| 92 |
|
| 93 |
if ( function_exists( 'do_action' ) ) { |
| 94 |
/** |
| 95 |
* Register additional follow-up adapters. |
| 96 |
* |
| 97 |
* @param FollowUpAdapterRegistry $registry |
| 98 |
* |
| 99 |
* @since 5.6.0 |
| 100 |
*/ |
| 101 |
do_action( 'f12_doi_register_follow_up_adapters', $this ); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|