| 1 |
<?php |
| 2 |
/** |
| 3 |
* OptIn Link Generator Service |
| 4 |
* |
| 5 |
* @package Forge12\DoubleOptIn\Service |
| 6 |
* @since 4.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Forge12\DoubleOptIn\Service; |
| 10 |
|
| 11 |
use Forge12\DoubleOptIn\Entity\OptIn; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class OptInLinkGenerator |
| 19 |
* |
| 20 |
* Generates various links related to OptIn operations. |
| 21 |
*/ |
| 22 |
class OptInLinkGenerator { |
| 23 |
|
| 24 |
/** |
| 25 |
* Generate the opt-in confirmation link. |
| 26 |
* |
| 27 |
* @param OptIn $optIn The OptIn entity. |
| 28 |
* @param array $parameter Optional parameters (page, formUrl). |
| 29 |
* |
| 30 |
* @return string The confirmation URL. |
| 31 |
*/ |
| 32 |
public function generateOptInLink( OptIn $optIn, array $parameter = array() ): string { |
| 33 |
if ( empty( $optIn->getHash() ) ) { |
| 34 |
return ''; |
| 35 |
} |
| 36 |
|
| 37 |
$pageId = $parameter['page'] ?? -1; |
| 38 |
|
| 39 |
if ( $pageId > 0 ) { |
| 40 |
$url = get_permalink( $pageId ); |
| 41 |
} elseif ( ! empty( $parameter['formUrl'] ) ) { |
| 42 |
$url = $parameter['formUrl']; |
| 43 |
} else { |
| 44 |
$url = home_url(); |
| 45 |
} |
| 46 |
|
| 47 |
return add_query_arg( 'optin', $optIn->getHash(), $url ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Generate the opt-out link. |
| 52 |
* |
| 53 |
* @param OptIn $optIn The OptIn entity. |
| 54 |
* |
| 55 |
* @return string The opt-out URL. |
| 56 |
*/ |
| 57 |
public function generateOptOutLink( OptIn $optIn ): string { |
| 58 |
if ( empty( $optIn->getHash() ) ) { |
| 59 |
return ''; |
| 60 |
} |
| 61 |
|
| 62 |
$settings = \forge12\contactform7\CF7DoubleOptIn\CF7DoubleOptIn::getInstance()->getSettings(); |
| 63 |
$pageId = $settings['optout_page'] ?? -1; |
| 64 |
|
| 65 |
if ( $pageId > 0 ) { |
| 66 |
$url = get_permalink( $pageId ); |
| 67 |
} else { |
| 68 |
$url = home_url(); |
| 69 |
} |
| 70 |
|
| 71 |
return add_query_arg( 'optout', $optIn->getHash(), $url ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Generate the admin view link. |
| 76 |
* |
| 77 |
* @param OptIn $optIn The OptIn entity. |
| 78 |
* |
| 79 |
* @return string The admin view URL. |
| 80 |
*/ |
| 81 |
public function generateAdminViewLink( OptIn $optIn ): string { |
| 82 |
if ( empty( $optIn->getHash() ) ) { |
| 83 |
return ''; |
| 84 |
} |
| 85 |
|
| 86 |
return admin_url( 'admin.php?page=f12-cf7-doubleoptin_optin_view&hash=' . $optIn->getHash() ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Generate the admin delete link with nonce. |
| 91 |
* |
| 92 |
* @param OptIn $optIn The OptIn entity. |
| 93 |
* |
| 94 |
* @return string The admin delete URL with nonce. |
| 95 |
*/ |
| 96 |
public function generateAdminDeleteLink( OptIn $optIn ): string { |
| 97 |
if ( empty( $optIn->getHash() ) ) { |
| 98 |
return ''; |
| 99 |
} |
| 100 |
|
| 101 |
return wp_nonce_url( |
| 102 |
admin_url( 'admin.php?page=f12-cf7-doubleoptin_optins&option=delete&hash=' . $optIn->getHash() ), |
| 103 |
'doi-delete-optin-' . $optIn->getHash() |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Generate the export link. |
| 109 |
* |
| 110 |
* @param OptIn $optIn The OptIn entity. |
| 111 |
* |
| 112 |
* @return string The export URL. |
| 113 |
*/ |
| 114 |
public function generateExportLink( OptIn $optIn ): string { |
| 115 |
if ( empty( $optIn->getHash() ) ) { |
| 116 |
return ''; |
| 117 |
} |
| 118 |
|
| 119 |
return wp_nonce_url( |
| 120 |
admin_url( 'admin.php?page=f12-cf7-doubleoptin_optins&option=export&hash=' . $optIn->getHash() ), |
| 121 |
'doi-export-optin-' . $optIn->getHash() |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Add placeholder values to text. |
| 127 |
* |
| 128 |
* Replaces [doubleoptinlink], [doubleoptoutlink] etc. in text. |
| 129 |
* |
| 130 |
* @param string $text The text with placeholders. |
| 131 |
* @param OptIn $optIn The OptIn entity. |
| 132 |
* @param array $parameter Optional parameters. |
| 133 |
* |
| 134 |
* @return string The text with replaced placeholders. |
| 135 |
*/ |
| 136 |
public function replacePlaceholders( string $text, OptIn $optIn, array $parameter = array() ): string { |
| 137 |
$replacements = array( |
| 138 |
'[doubleoptinlink]' => $this->generateOptInLink( $optIn, $parameter ), |
| 139 |
'[doubleoptoutlink]' => $this->generateOptOutLink( $optIn ), |
| 140 |
'[doubleoptinhash]' => $optIn->getHash(), |
| 141 |
'[doubleoptin_email]' => $optIn->getEmail(), |
| 142 |
); |
| 143 |
|
| 144 |
return str_replace( |
| 145 |
array_keys( $replacements ), |
| 146 |
array_values( $replacements ), |
| 147 |
$text |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
|