| 1 |
<?php |
| 2 |
/** |
| 3 |
* A subscription group: several forms presented as one subscription. |
| 4 |
* |
| 5 |
* @package Forge12\DoubleOptIn\Subscription |
| 6 |
* @since 5.12.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare( strict_types=1 ); |
| 10 |
|
| 11 |
namespace Forge12\DoubleOptIn\Subscription; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Immutable value object. A group only changes how records are presented |
| 19 |
* and selected; the opt-in records themselves are never merged. |
| 20 |
* |
| 21 |
* A member names a form (`form_id`) and optionally one instance of it |
| 22 |
* (`form_ref`, e.g. an Elementor widget id). Without a reference the member |
| 23 |
* covers every record of that form, including records stored before |
| 24 |
* `form_ref` existed. |
| 25 |
*/ |
| 26 |
final class SubscriptionGroup { |
| 27 |
|
| 28 |
/** @var string */ |
| 29 |
private $key; |
| 30 |
|
| 31 |
/** @var string */ |
| 32 |
private $label; |
| 33 |
|
| 34 |
/** @var array<int, array{form_id:int, form_ref:string}> */ |
| 35 |
private $members; |
| 36 |
|
| 37 |
/** @var bool */ |
| 38 |
private $mergeInFrontend; |
| 39 |
|
| 40 |
/** @var bool */ |
| 41 |
private $showDetails; |
| 42 |
|
| 43 |
/** |
| 44 |
* @param string $key Stable identifier. |
| 45 |
* @param string $label Name shown to visitors and admins. |
| 46 |
* @param array<int, mixed> $members Members, each with `form_id` and optional `form_ref`. |
| 47 |
* @param bool $mergeInFrontend One row per address in the consent center. |
| 48 |
* @param bool $showDetails Let visitors expand the single sign-ups. |
| 49 |
*/ |
| 50 |
public function __construct( string $key, string $label, array $members, bool $mergeInFrontend, bool $showDetails ) { |
| 51 |
$this->key = $key; |
| 52 |
$this->label = $label; |
| 53 |
$this->members = self::normaliseMembers( $members ); |
| 54 |
$this->mergeInFrontend = $mergeInFrontend; |
| 55 |
$this->showDetails = $showDetails; |
| 56 |
} |
| 57 |
|
| 58 |
public function getKey(): string { |
| 59 |
return $this->key; |
| 60 |
} |
| 61 |
|
| 62 |
public function getLabel(): string { |
| 63 |
return $this->label; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @return array<int, array{form_id:int, form_ref:string}> |
| 68 |
*/ |
| 69 |
public function getMembers(): array { |
| 70 |
return $this->members; |
| 71 |
} |
| 72 |
|
| 73 |
public function mergesInFrontend(): bool { |
| 74 |
return $this->mergeInFrontend; |
| 75 |
} |
| 76 |
|
| 77 |
public function showsDetails(): bool { |
| 78 |
return $this->showDetails; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Whether a record belongs to this group. |
| 83 |
* |
| 84 |
* @param int $formId The record's form id. |
| 85 |
* @param string $formRef The record's form instance reference, if any. |
| 86 |
*/ |
| 87 |
public function matches( int $formId, string $formRef = '' ): bool { |
| 88 |
foreach ( $this->members as $member ) { |
| 89 |
if ( $member['form_id'] !== $formId ) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
if ( $member['form_ref'] === '' || $member['form_ref'] === $formRef ) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* A WHERE fragment selecting this group's records, with `%d` / `%s` |
| 102 |
* placeholders for `$wpdb->prepare()`. |
| 103 |
* |
| 104 |
* @param string $alias Optional table alias including the dot, e.g. `o.`. |
| 105 |
* |
| 106 |
* @return array{0:string, 1:array<int, int|string>} Fragment and its parameters. |
| 107 |
*/ |
| 108 |
public function toSqlCondition( string $alias = '' ): array { |
| 109 |
if ( array() === $this->members ) { |
| 110 |
return array( '1 = 0', array() ); |
| 111 |
} |
| 112 |
|
| 113 |
$parts = array(); |
| 114 |
$params = array(); |
| 115 |
foreach ( $this->members as $member ) { |
| 116 |
if ( $member['form_ref'] === '' ) { |
| 117 |
$parts[] = $alias . 'cf_form_id = %d'; |
| 118 |
$params[] = $member['form_id']; |
| 119 |
continue; |
| 120 |
} |
| 121 |
$parts[] = '(' . $alias . 'cf_form_id = %d AND ' . $alias . 'form_ref = %s)'; |
| 122 |
$params[] = $member['form_id']; |
| 123 |
$params[] = $member['form_ref']; |
| 124 |
} |
| 125 |
|
| 126 |
return array( '(' . implode( ' OR ', $parts ) . ')', $params ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @param array<int, mixed> $members Raw members. |
| 131 |
* |
| 132 |
* @return array<int, array{form_id:int, form_ref:string}> |
| 133 |
*/ |
| 134 |
private static function normaliseMembers( array $members ): array { |
| 135 |
$clean = array(); |
| 136 |
foreach ( $members as $member ) { |
| 137 |
if ( ! is_array( $member ) ) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
$formId = (int) ( $member['form_id'] ?? 0 ); |
| 141 |
if ( $formId <= 0 ) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
$clean[] = array( |
| 145 |
'form_id' => $formId, |
| 146 |
'form_ref' => (string) ( $member['form_ref'] ?? '' ), |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
return $clean; |
| 151 |
} |
| 152 |
} |
| 153 |
|