| 1 |
<?php |
| 2 |
/** |
| 3 |
* A place to put something on the page a subscriber lands on after confirming. |
| 4 |
* |
| 5 |
* The plugin had none. Confirmation happens on an ordinary WordPress page via |
| 6 |
* `?optin=<hash>`; there is no endpoint of our own, no redirect, and on success |
| 7 |
* nothing at all is printed — deliberately, so the site owner's page is left |
| 8 |
* alone. That is a reasonable default and it stays the default: this file only |
| 9 |
* offers a hook, and prints nothing unless something is attached to it. |
| 10 |
* |
| 11 |
* Why a hook rather than direct output: the credit link is the first consumer, |
| 12 |
* but "the visitor just confirmed" is generally useful — a thank-you note, a |
| 13 |
* tracking pixel, a coupon. Anyone can attach to it without patching the |
| 14 |
* confirmation path, which is exactly what we had to avoid doing ourselves. |
| 15 |
*/ |
| 16 |
|
| 17 |
namespace forge12\contactform7\CF7DoubleOptIn; |
| 18 |
|
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Whether a confirmation happened in this request. |
| 25 |
* |
| 26 |
* A function-static rather than a global: the value is meaningless outside this |
| 27 |
* request and nothing else has any business writing it. |
| 28 |
* |
| 29 |
* @param bool|null $set Pass true to record a confirmation; null to read. |
| 30 |
* |
| 31 |
* @return bool |
| 32 |
*/ |
| 33 |
function confirmation_happened( ?bool $set = null ): bool { |
| 34 |
static $confirmed = false; |
| 35 |
|
| 36 |
if ( $set === true ) { |
| 37 |
$confirmed = true; |
| 38 |
} |
| 39 |
|
| 40 |
return $confirmed; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Remember the confirmation. |
| 45 |
* |
| 46 |
* `f12_cf7_doubleoptin_after_confirm` is the right signal because it is the one |
| 47 |
* both paths agree on: the modern integrations fire it in |
| 48 |
* AbstractFormIntegration::validateOptIn(), and the legacy OptInFrontend fires |
| 49 |
* it too. Hooking the integrations individually would have missed whichever one |
| 50 |
* is active on any given site — and CF7/Avada run the new path while Elementor |
| 51 |
* still runs the old one. |
| 52 |
* |
| 53 |
* It also only fires on an actual state change: an already-confirmed or expired |
| 54 |
* link does not reach it, so nothing is printed for those. |
| 55 |
*/ |
| 56 |
function note_confirmation(): void { |
| 57 |
confirmation_happened( true ); |
| 58 |
} |
| 59 |
|
| 60 |
add_action( 'f12_cf7_doubleoptin_after_confirm', __NAMESPACE__ . '\note_confirmation' ); |
| 61 |
|
| 62 |
/** |
| 63 |
* Print whatever is attached, at the end of the page. |
| 64 |
* |
| 65 |
* Runs on `wp_footer`, which is safely after the confirmation: every |
| 66 |
* integration handles `?optin=` on `init`. |
| 67 |
*/ |
| 68 |
function render_confirmation_output(): void { |
| 69 |
if ( ! confirmation_happened() ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Filter the markup shown after a successful opt-in confirmation. |
| 75 |
* |
| 76 |
* Returning an empty string — the default — prints nothing at all. |
| 77 |
* |
| 78 |
* @param string $html Markup to print. Empty by default. |
| 79 |
* |
| 80 |
* @since 5.2.0 |
| 81 |
*/ |
| 82 |
$html = (string) apply_filters( 'f12_doi_confirmation_output', '' ); |
| 83 |
|
| 84 |
if ( $html === '' ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
// Escaped here as well as at the source. The filter is public, so this |
| 89 |
// output point is only as safe as the least careful thing hooked to it. |
| 90 |
echo wp_kses_post( $html ); |
| 91 |
} |
| 92 |
|
| 93 |
add_action( 'wp_footer', __NAMESPACE__ . '\render_confirmation_output', 20 ); |
| 94 |
|