| 1 |
<?php |
| 2 |
/** |
| 3 |
* The optional "Double Opt-In by Forge12" credit on the confirmation page. |
| 4 |
* |
| 5 |
* Off by default, and it has to stay that way. WordPress.org's plugin guidelines |
| 6 |
* are explicit: "All 'Powered By' or credit displays and links included in the |
| 7 |
* plugin code must be optional and default to *not* show on users' front-facing |
| 8 |
* websites", and the choice has to be made through "clearly stated and |
| 9 |
* understandable choices, not buried in the terms of use or documentation". A |
| 10 |
* pre-enabled credit is grounds for removal from the directory, so the default |
| 11 |
* below is not a preference — it is the condition for being listed at all. |
| 12 |
* |
| 13 |
* Kept in its own file rather than woven into the confirmation path: the feature |
| 14 |
* is one setting, one URL and one line of markup, and holding it together makes |
| 15 |
* it as easy to remove as it was to add. It attaches through the same public |
| 16 |
* filter a third party would use. |
| 17 |
*/ |
| 18 |
|
| 19 |
namespace forge12\contactform7\CF7DoubleOptIn; |
| 20 |
|
| 21 |
if ( ! defined( 'ABSPATH' ) ) { |
| 22 |
exit; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Setting key inside `f12-doi-settings`. |
| 27 |
*/ |
| 28 |
const CREDIT_SETTING_KEY = 'credit_link'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Whether the site owner has asked for the credit to be shown. |
| 32 |
* |
| 33 |
* Absent means off. Only an explicit 1 turns it on — the reverse of the |
| 34 |
* plugin's other defaults, deliberately. |
| 35 |
*/ |
| 36 |
function is_credit_enabled(): bool { |
| 37 |
$settings = get_option( 'f12-doi-settings', array() ); |
| 38 |
|
| 39 |
if ( ! is_array( $settings ) || ! isset( $settings[ CREDIT_SETTING_KEY ] ) ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
return (int) $settings[ CREDIT_SETTING_KEY ] === 1; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Where the credit points. |
| 48 |
*/ |
| 49 |
function get_credit_url(): string { |
| 50 |
/** |
| 51 |
* Filter the destination of the credit link specifically. |
| 52 |
* |
| 53 |
* Kept alongside the general f12_doi_product_url filter, which has already |
| 54 |
* run by this point: someone overriding where the credit on their |
| 55 |
* confirmation page points should not have to special-case every other link |
| 56 |
* to the product site as well. |
| 57 |
* |
| 58 |
* @param string $url The full URL including its query arguments. |
| 59 |
* |
| 60 |
* @since 5.2.0 |
| 61 |
*/ |
| 62 |
return (string) apply_filters( 'f12_doi_credit_url', get_product_url( 'confirmation-credit' ) ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* The credit markup, or an empty string when it is switched off. |
| 67 |
* |
| 68 |
* `rel="nofollow"` on purpose. The point of the link is that a curious site |
| 69 |
* owner can follow it, not that it passes ranking signals from sites whose |
| 70 |
* owners agreed to a small thank-you — claiming the latter would make every |
| 71 |
* installation look like a paid link scheme, which is precisely how search |
| 72 |
* engines describe links distributed through plugins. |
| 73 |
* |
| 74 |
* @param bool $force Render even when the setting is off. Only for the preview |
| 75 |
* in the admin notice: someone deciding whether to switch |
| 76 |
* this on is really asking "will it make my page look |
| 77 |
* cluttered", and the honest answer is to show them the |
| 78 |
* exact thing rather than describe it. |
| 79 |
* |
| 80 |
* @return string |
| 81 |
*/ |
| 82 |
function get_credit_markup( bool $force = false ): string { |
| 83 |
if ( ! $force && ! is_credit_enabled() ) { |
| 84 |
return ''; |
| 85 |
} |
| 86 |
|
| 87 |
$markup = sprintf( |
| 88 |
'<p class="f12-doi-credit"><a href="%s" target="_blank" rel="nofollow noopener">%s</a></p>', |
| 89 |
esc_url( get_credit_url() ), |
| 90 |
esc_html__( 'Double Opt-In by Forge12', 'double-opt-in' ) |
| 91 |
); |
| 92 |
|
| 93 |
/** |
| 94 |
* Filter the credit markup. |
| 95 |
* |
| 96 |
* @param string $markup The rendered credit, or '' when switched off. |
| 97 |
* @param bool $force Whether rendering was forced for a preview. |
| 98 |
* |
| 99 |
* @since 5.2.0 |
| 100 |
*/ |
| 101 |
return (string) apply_filters( 'f12_doi_credit_markup', $markup, $force ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Attach the credit to the confirmation page. |
| 106 |
* |
| 107 |
* Typed as mixed rather than string on purpose: this hangs on a public filter, |
| 108 |
* so another plugin earlier in the chain can hand over anything at all. Passing |
| 109 |
* that through untouched is better than breaking someone's page over a credit. |
| 110 |
* |
| 111 |
* @param mixed $html Whatever the confirmation output point has collected. |
| 112 |
* |
| 113 |
* @return mixed |
| 114 |
*/ |
| 115 |
function append_credit( $html ) { |
| 116 |
if ( ! is_string( $html ) ) { |
| 117 |
return $html; |
| 118 |
} |
| 119 |
|
| 120 |
$credit = get_credit_markup(); |
| 121 |
|
| 122 |
return $credit === '' ? $html : $html . $credit; |
| 123 |
} |
| 124 |
|
| 125 |
add_filter( 'f12_doi_confirmation_output', __NAMESPACE__ . '\append_credit', 20 ); |
| 126 |
|