| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
|
| 5 |
use Forge12\Shared\LoggerInterface; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Class UIOptinView |
| 13 |
* Show a specific OptIn |
| 14 |
* |
| 15 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 16 |
*/ |
| 17 |
class UIOptinView extends UIPage { |
| 18 |
/** |
| 19 |
* Class constructor. |
| 20 |
* |
| 21 |
* @param TemplateHandler $templateHandler The template handler object. |
| 22 |
* @param string $domain The domain of the class. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function __construct(LoggerInterface $logger, TemplateHandler $templateHandler, string $domain ) { |
| 27 |
parent::__construct($logger, $templateHandler, $domain, 'optin_view', __( 'OptIn View', 'double-opt-in' ) ); |
| 28 |
$this->get_logger()->debug( 'Parent constructor called with slug "optin_view".', [ |
| 29 |
'plugin' => $domain, |
| 30 |
] ); |
| 31 |
|
| 32 |
// Check for status messages in the URL and add corresponding messages. |
| 33 |
if ( isset( $_GET['status'] ) ) { |
| 34 |
$status = sanitize_text_field( $_GET['status'] ); |
| 35 |
$this->get_logger()->info( 'URL status parameter found: ' . $status, [ |
| 36 |
'plugin' => $domain, |
| 37 |
] ); |
| 38 |
|
| 39 |
switch ( $status ) { |
| 40 |
case 'deleted': |
| 41 |
Messages::getInstance()->add( __( 'Opt-In deleted', 'double-opt-in' ), 'success' ); |
| 42 |
$this->get_logger()->notice( 'Added "Opt-In deleted" success message.', [ |
| 43 |
'plugin' => $domain, |
| 44 |
] ); |
| 45 |
break; |
| 46 |
case 'not-deleted': |
| 47 |
Messages::getInstance()->add( __( 'Opt-In not-deleted', 'double-opt-in' ), 'error' ); |
| 48 |
$this->get_logger()->error( 'Added "Opt-In not-deleted" error message.', [ |
| 49 |
'plugin' => $domain, |
| 50 |
] ); |
| 51 |
break; |
| 52 |
default: |
| 53 |
$this->get_logger()->warning( 'Unknown status parameter value: ' . $status, [ |
| 54 |
'plugin' => $domain, |
| 55 |
] ); |
| 56 |
break; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Hide this page in the menu. |
| 63 |
* |
| 64 |
* @return bool |
| 65 |
*/ |
| 66 |
public function hideInMenu() { |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Render the license subpage content |
| 72 |
*/ |
| 73 |
protected function theContent( $slug, $page, $settings ) { |
| 74 |
$this->get_logger()->info( 'Rendering the main content for the "OptIn View" UI page.', [ |
| 75 |
'plugin' => $this->domain, |
| 76 |
'page_slug' => $page, |
| 77 |
] ); |
| 78 |
|
| 79 |
/* |
| 80 |
* Ensure that the hash is given |
| 81 |
*/ |
| 82 |
if ( ! isset( $_GET['hash'] ) ) { |
| 83 |
$this->get_logger()->error( 'OptIn hash not provided in URL. Redirecting to the dashboard.', [ |
| 84 |
'plugin' => $this->domain, |
| 85 |
] ); |
| 86 |
wp_redirect( admin_url( 'admin.php' ) . '?page=f12-cf7-doubleoptin' ); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// Check if there is any POST data to save. |
| 91 |
$this->maybeSave(); |
| 92 |
|
| 93 |
$hash = sanitize_text_field( $_GET['hash'] ); |
| 94 |
$this->get_logger()->debug( 'OptIn hash from URL: ' . $hash, [ |
| 95 |
'plugin' => $this->domain, |
| 96 |
] ); |
| 97 |
|
| 98 |
/* |
| 99 |
* Load the assigned Category |
| 100 |
*/ |
| 101 |
$Optin = Optin::get_by_hash( $hash ); |
| 102 |
|
| 103 |
if ( null === $Optin ) { |
| 104 |
$this->get_logger()->error( 'OptIn with hash ' . $hash . ' not found. Redirecting to the dashboard.', [ |
| 105 |
'plugin' => $this->domain, |
| 106 |
] ); |
| 107 |
wp_redirect( admin_url( 'admin.php' ) . '?page=f12-cf7-doubleoptin' ); |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$this->get_logger()->notice( 'OptIn object loaded successfully for hash: ' . $Optin->get_hash(), [ |
| 112 |
'plugin' => $this->domain, |
| 113 |
] ); |
| 114 |
|
| 115 |
/* |
| 116 |
* Render the Template |
| 117 |
*/ |
| 118 |
?> |
| 119 |
<h2><?php _e( 'Opt-In: ', 'double-opt-in' ) . esc_html( $Optin->get_hash() ); ?></h2> |
| 120 |
<?php |
| 121 |
$this->get_logger()->info( 'Rendering the "view-optin" template.', [ |
| 122 |
'plugin' => $this->domain, |
| 123 |
] ); |
| 124 |
|
| 125 |
$this->TemplateHandler->renderTemplate( 'view-optin', array( |
| 126 |
'domain' => $this->domain, |
| 127 |
'optin' => $Optin |
| 128 |
) ); |
| 129 |
|
| 130 |
$this->get_logger()->notice( 'Content rendering for "OptIn View" completed successfully.', [ |
| 131 |
'plugin' => $this->domain, |
| 132 |
] ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Update the Optin if the category has been submitted. |
| 137 |
* |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
private function maybeSave() { |
| 141 |
$this->get_logger()->info( 'Attempting to save OptIn data based on form submission.', [ |
| 142 |
'plugin' => $this->domain, |
| 143 |
] ); |
| 144 |
|
| 145 |
// Check for required POST data and nonce verification. |
| 146 |
if ( ! isset( $_POST['hash'] ) || ! wp_verify_nonce( $_POST['doi_settings_nonce'], 'doi_settings_action' ) ) { |
| 147 |
$this->get_logger()->error( 'Missing hash or nonce verification failed. Aborting save.', [ |
| 148 |
'plugin' => $this->domain, |
| 149 |
] ); |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
$category = (int) $_POST['category']; |
| 154 |
$hash = sanitize_text_field( $_POST['hash'] ); |
| 155 |
|
| 156 |
$this->get_logger()->debug( 'Form data received: ' . json_encode(['category' => $category, 'hash' => $hash]), [ |
| 157 |
'plugin' => $this->domain, |
| 158 |
] ); |
| 159 |
|
| 160 |
// Validate the category ID. |
| 161 |
if ( ! $category ) { |
| 162 |
$this->get_logger()->warning( 'Category ID is not a valid number. Aborting save.', [ |
| 163 |
'plugin' => $this->domain, |
| 164 |
] ); |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$Optin = OptIn::get_by_hash( $hash ); |
| 169 |
|
| 170 |
if (null === $Optin) { |
| 171 |
$this->get_logger()->error('OptIn with hash ' . $hash . ' not found. Aborting save.', [ |
| 172 |
'plugin' => $this->domain, |
| 173 |
]); |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
$this->get_logger()->notice( 'Updating OptIn with hash "' . $hash . '" to category ID "' . $category . '".', [ |
| 178 |
'plugin' => $this->domain, |
| 179 |
] ); |
| 180 |
|
| 181 |
$Optin->set_category( $category ); |
| 182 |
|
| 183 |
if ($Optin->save()) { |
| 184 |
$this->get_logger()->info('OptIn successfully saved to the database.', [ |
| 185 |
'plugin' => $this->domain, |
| 186 |
]); |
| 187 |
Messages::getInstance()->add(__( 'Opt-In updated successfully.', 'double-opt-in' ), 'success' ); |
| 188 |
} else { |
| 189 |
$this->get_logger()->error('Failed to save OptIn to the database.', [ |
| 190 |
'plugin' => $this->domain, |
| 191 |
]); |
| 192 |
Messages::getInstance()->add(__( 'Opt-In update failed.', 'double-opt-in' ), 'error' ); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
protected function theSidebar( $slug, $page ) { |
| 197 |
$this->get_logger()->info( 'Rendering the sidebar content for the "OptIn View" UI page.', [ |
| 198 |
'plugin' => $this->domain, |
| 199 |
'page_slug' => $page, |
| 200 |
] ); |
| 201 |
|
| 202 |
/* |
| 203 |
* Ensure that the hash is set in the URL |
| 204 |
*/ |
| 205 |
if ( ! isset( $_GET['hash'] ) ) { |
| 206 |
$this->get_logger()->error( 'OptIn hash not provided in URL for sidebar rendering. Redirecting.', [ |
| 207 |
'plugin' => $this->domain, |
| 208 |
] ); |
| 209 |
wp_redirect( admin_url( 'admin.php' ) . '?page=f12-cf7-doubleoptin' ); |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
$hash = sanitize_text_field( $_GET['hash'] ); |
| 214 |
$this->get_logger()->debug( 'OptIn hash from URL for sidebar: ' . $hash, [ |
| 215 |
'plugin' => $this->domain, |
| 216 |
] ); |
| 217 |
|
| 218 |
/* |
| 219 |
* Load the OptIn object |
| 220 |
*/ |
| 221 |
$Optin = OptIn::get_by_hash( $hash ); |
| 222 |
|
| 223 |
if ( ! $Optin ) { |
| 224 |
$this->get_logger()->error( 'OptIn with hash ' . $hash . ' not found for sidebar. Redirecting.', [ |
| 225 |
'plugin' => $this->domain, |
| 226 |
] ); |
| 227 |
wp_redirect( admin_url( 'admin.php' ) . '?page=f12-cf7-doubleoptin' ); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
$this->get_logger()->notice( 'OptIn object loaded for sidebar rendering.', [ |
| 232 |
'plugin' => $this->domain, |
| 233 |
'optin_id' => $Optin->get_id(), |
| 234 |
] ); |
| 235 |
?> |
| 236 |
<div class="box"> |
| 237 |
<h2> |
| 238 |
<?php _e( 'Edit Opt-In:', 'double-opt-in' ); ?> |
| 239 |
</h2> |
| 240 |
|
| 241 |
<form action="" method="post"> |
| 242 |
<div class="option"> |
| 243 |
<div class="label"> |
| 244 |
<label for="category"><?php _e( 'Category:', 'double-opt-in' ); ?></label> |
| 245 |
</div> |
| 246 |
<input type="hidden" value="<?php esc_attr_e( $Optin->get_hash() ); ?>" name="hash"/> |
| 247 |
<div class="input"> |
| 248 |
<?php |
| 249 |
$this->get_logger()->debug( 'Rendering category selection dropdown.', [ |
| 250 |
'plugin' => $this->domain, |
| 251 |
] ); |
| 252 |
// Using wp_kses for sanitization since the output contains a select tag and its options. |
| 253 |
echo wp_kses( CategoryOptions::getInstance()->select_category( $Optin->get_category() ), array( |
| 254 |
'select' => array( 'name' => array() ), |
| 255 |
'option' => array( |
| 256 |
'value' => array(), |
| 257 |
'selected' => array() |
| 258 |
) |
| 259 |
) ); |
| 260 |
?> |
| 261 |
<p> |
| 262 |
<?php _e( 'Select the category to assign the Opt-In.', 'double-opt-in' ); ?> |
| 263 |
</p> |
| 264 |
</div> |
| 265 |
</div> |
| 266 |
<?php |
| 267 |
$this->get_logger()->debug( 'Rendering nonce field and update button.', [ |
| 268 |
'plugin' => $this->domain, |
| 269 |
] ); |
| 270 |
wp_nonce_field( 'doi_settings_action', 'doi_settings_nonce' ); |
| 271 |
?> |
| 272 |
|
| 273 |
<input type="submit" name="doubleoptin-settings-edit-submit" class="button button-primary" |
| 274 |
value=" <?php _e( 'Update', 'double-opt-in' ); ?>"/> |
| 275 |
</form> |
| 276 |
</div> |
| 277 |
|
| 278 |
<div class="box"> |
| 279 |
<h2> |
| 280 |
<?php _e( 'Opt-In:', 'double-opt-in' ); ?> |
| 281 |
</h2> |
| 282 |
|
| 283 |
<div class="option"> |
| 284 |
<div class="label"> |
| 285 |
<label for="category"><?php _e( 'Link:', 'double-opt-in' ); ?></label> |
| 286 |
</div> |
| 287 |
<div class="input copy-to-clipboard"> |
| 288 |
<?php |
| 289 |
echo esc_url( $Optin->get_link_optin() ); |
| 290 |
?> |
| 291 |
</div> |
| 292 |
</div> |
| 293 |
</div> |
| 294 |
<?php |
| 295 |
$this->get_logger()->info( 'Triggering "f12_cf7_doubleoptin_ui_view_optin_sidebar" action.', [ |
| 296 |
'plugin' => $this->domain, |
| 297 |
] ); |
| 298 |
/** |
| 299 |
* Sidebar |
| 300 |
* |
| 301 |
* This Hook allows us to extend the sidebar for the pro version. |
| 302 |
* |
| 303 |
* @param OptIn $Optin |
| 304 |
* |
| 305 |
* @since 3.0.0 |
| 306 |
*/ |
| 307 |
do_action( 'f12_cf7_doubleoptin_ui_view_optin_sidebar', $Optin ); |
| 308 |
|
| 309 |
$this->get_logger()->notice( 'Sidebar content for "OptIn View" page rendered successfully.', [ |
| 310 |
'plugin' => $this->domain, |
| 311 |
] ); |
| 312 |
} |
| 313 |
|
| 314 |
public function getSettings( $settings ) { |
| 315 |
return $settings; |
| 316 |
} |
| 317 |
} |
| 318 |
} |