| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
|
| 5 |
use Forge12\Shared\Logger; |
| 6 |
use Forge12\Shared\LoggerInterface; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Ajax |
| 14 |
* Responsible to handle the admin settings for the double opt-in field |
| 15 |
* |
| 16 |
* @package forge12\contactform7\CF7OptIn |
| 17 |
*/ |
| 18 |
class Ajax { |
| 19 |
private LoggerInterface $logger; |
| 20 |
|
| 21 |
/** |
| 22 |
* Admin constructor. |
| 23 |
*/ |
| 24 |
public function __construct( LoggerInterface $logger ) { |
| 25 |
$this->logger = $logger; |
| 26 |
|
| 27 |
$this->get_logger()->debug( 'Initializing AJAX handlers', [ |
| 28 |
'plugin' => 'double-opt-in', |
| 29 |
'class' => __CLASS__, |
| 30 |
'method' => __METHOD__, |
| 31 |
] ); |
| 32 |
|
| 33 |
add_action( 'wp_ajax_f12_doi_details', [ $this, 'getDetails' ] ); |
| 34 |
add_action( 'wp_ajax_f12_doi_templateloader', [ $this, 'getTemplate' ] ); |
| 35 |
|
| 36 |
$this->get_logger()->info( 'AJAX handlers registered', [ |
| 37 |
'plugin' => 'double-opt-in', |
| 38 |
'class' => __CLASS__, |
| 39 |
'method' => __METHOD__, |
| 40 |
] ); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
public function get_logger() { |
| 45 |
return $this->logger; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Load and get the template we need. |
| 50 |
*/ |
| 51 |
public function getTemplate() { |
| 52 |
$this->get_logger()->debug( 'getTemplate called', [ |
| 53 |
'plugin' => 'double-opt-in', |
| 54 |
'class' => __CLASS__, |
| 55 |
'method' => __METHOD__, |
| 56 |
'post' => $_POST, |
| 57 |
] ); |
| 58 |
|
| 59 |
$content = ''; |
| 60 |
if ( isset( $_POST['template'] ) && wp_verify_nonce( $_POST['nonce'], 'f12_doi_templateloader' ) ) { |
| 61 |
$template_path = plugin_dir_path( dirname( __FILE__ ) ) . 'mails/' . esc_attr( sanitize_text_field( $_POST['template'] ) ) . '.html'; |
| 62 |
|
| 63 |
if ( file_exists( $template_path ) ) { |
| 64 |
$this->get_logger()->debug( 'Template file found, loading content', [ |
| 65 |
'plugin' => 'double-opt-in', |
| 66 |
'class' => __CLASS__, |
| 67 |
'method' => __METHOD__, |
| 68 |
'template_path' => $template_path, |
| 69 |
] ); |
| 70 |
$content = file_get_contents( $template_path ); |
| 71 |
} else { |
| 72 |
$this->get_logger()->warning( 'Template file not found', [ |
| 73 |
'plugin' => 'double-opt-in', |
| 74 |
'class' => __CLASS__, |
| 75 |
'method' => __METHOD__, |
| 76 |
'template_path' => $template_path, |
| 77 |
] ); |
| 78 |
} |
| 79 |
} else { |
| 80 |
$this->get_logger()->warning( 'Invalid or missing nonce/template parameter', [ |
| 81 |
'plugin' => 'double-opt-in', |
| 82 |
'class' => __CLASS__, |
| 83 |
'method' => __METHOD__, |
| 84 |
'post' => $_POST, |
| 85 |
] ); |
| 86 |
} |
| 87 |
|
| 88 |
echo wp_json_encode( [ 'status' => 200, 'content' => $content ] ); |
| 89 |
wp_die(); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* Return the Popup for the HASH DOI |
| 95 |
*/ |
| 96 |
public function getDetails() { |
| 97 |
$this->get_logger()->debug( 'getDetails called', [ |
| 98 |
'plugin' => 'double-opt-in', |
| 99 |
'class' => __CLASS__, |
| 100 |
'method' => __METHOD__, |
| 101 |
'post' => $_POST, |
| 102 |
] ); |
| 103 |
|
| 104 |
if ( isset( $_POST['hash'] ) && wp_verify_nonce( $_POST['nonce'], 'f12_doi_details' ) ) { |
| 105 |
global $wpdb; |
| 106 |
$tableName = $wpdb->prefix . 'f12_cf7_doubleoptin'; |
| 107 |
$hash = sanitize_text_field( $_POST['hash'] ); |
| 108 |
|
| 109 |
$this->get_logger()->debug( 'Looking up OptIn by hash', [ |
| 110 |
'plugin' => 'double-opt-in', |
| 111 |
'class' => __CLASS__, |
| 112 |
'method' => __METHOD__, |
| 113 |
'hash' => $hash, |
| 114 |
] ); |
| 115 |
|
| 116 |
$OptIn = OptIn::get_by_hash( $hash ); |
| 117 |
|
| 118 |
if ( null == $OptIn ) { |
| 119 |
$this->get_logger()->warning( 'OptIn not found for hash', [ |
| 120 |
'plugin' => 'double-opt-in', |
| 121 |
'class' => __CLASS__, |
| 122 |
'method' => __METHOD__, |
| 123 |
'hash' => $hash, |
| 124 |
] ); |
| 125 |
|
| 126 |
ob_start(); |
| 127 |
?> |
| 128 |
<h2><?php _e( 'Ooops!', 'double-opt-in' ); ?></h2> |
| 129 |
<p> |
| 130 |
<?php _e( 'Something went wrong. The given DOI wasn\'t found. Maybe it got removed?', 'double-opt-in' ); ?> |
| 131 |
</p> |
| 132 |
<?php |
| 133 |
$content = ob_get_contents(); |
| 134 |
ob_end_clean(); |
| 135 |
echo wp_json_encode( [ 'status' => 200, 'content' => $content ] ); |
| 136 |
wp_die(); |
| 137 |
} |
| 138 |
|
| 139 |
$this->get_logger()->info( 'OptIn found', [ |
| 140 |
'plugin' => 'double-opt-in', |
| 141 |
'class' => __CLASS__, |
| 142 |
'method' => __METHOD__, |
| 143 |
'optin' => [ |
| 144 |
'id' => $OptIn->get_id(), |
| 145 |
'hash' => $OptIn->get_hash(), |
| 146 |
'email' => $OptIn->get_email(), |
| 147 |
], |
| 148 |
] ); |
| 149 |
|
| 150 |
$formfields = maybe_unserialize( $OptIn->get_content() ); |
| 151 |
ob_start(); |
| 152 |
?> |
| 153 |
<h2><?php echo esc_html( $OptIn->get_hash() ); ?></h2> |
| 154 |
<?php if ( current_user_can( 'manage_options' ) ): ?> |
| 155 |
<div class="options"> |
| 156 |
<?php |
| 157 |
do_action( 'f12_cf7_doubleoptin_ui_view_optin_options', $OptIn ); |
| 158 |
?> |
| 159 |
<a class="button" |
| 160 |
href="<?php echo esc_url( $OptIn->get_link_delete() ); ?>"><?php _e( 'Delete DOI', 'double-opt-in' ); ?></a> |
| 161 |
<a class="button" |
| 162 |
href="<?php echo esc_url( $OptIn->get_link_ui() ); ?>"><?php _e( 'Details', 'double-opt-in' ); ?></a> |
| 163 |
</div> |
| 164 |
<?php endif; ?> |
| 165 |
<table> |
| 166 |
<tr> |
| 167 |
<td><?php _e( 'Key', 'double-opt-in' ); ?></td> |
| 168 |
<td><?php _e( 'Value', 'double-opt-in' ); ?></td> |
| 169 |
</tr> |
| 170 |
<tr> |
| 171 |
<td><?php _e( 'ID', 'f12-cf7-doupleoptin' ); ?></td> |
| 172 |
<td><?php echo esc_html( $OptIn->get_id() ); ?></td> |
| 173 |
</tr> |
| 174 |
<tr> |
| 175 |
<td><?php _e( 'CF7 Form ID', 'double-opt-in' ); ?></td> |
| 176 |
<td><?php echo esc_html( $OptIn->get_cf_form_id() ); ?></td> |
| 177 |
</tr> |
| 178 |
<tr> |
| 179 |
<td><?php _e( 'Registration Date', 'double-opt-in' ); ?></td> |
| 180 |
<td><?php echo esc_html( $OptIn->get_createtime( 'formatted' ) ); ?></td> |
| 181 |
</tr> |
| 182 |
<tr> |
| 183 |
<td><?php _e( 'Registration IP', 'double-opt-in' ); ?></td> |
| 184 |
<td><?php echo esc_html( $OptIn->get_ipaddr_register() ); ?></td> |
| 185 |
</tr> |
| 186 |
<tr> |
| 187 |
<td><?php _e( 'Confirmation Date', 'double-opt-in' ); ?></td> |
| 188 |
<td><?php if ( $OptIn->is_confirmed() ) { |
| 189 |
echo esc_html( $OptIn->get_updatetime( 'formatted' ) ); |
| 190 |
} ?></td> |
| 191 |
</tr> |
| 192 |
<tr> |
| 193 |
<td><?php _e( 'Confirmation IP', 'double-opt-in' ); ?></td> |
| 194 |
<td><?php echo esc_html( $OptIn->get_ipaddr_confirmation() ); ?></td> |
| 195 |
</tr> |
| 196 |
</table> |
| 197 |
|
| 198 |
<h3><?php _e( 'Form Fields', 'double-opt-in' ); ?></h3> |
| 199 |
<table> |
| 200 |
<tr> |
| 201 |
<td><?php _e( 'Key', 'double-opt-in' ); ?></td> |
| 202 |
<td><?php _e( 'Value', 'double-opt-in' ); ?></td> |
| 203 |
</tr> |
| 204 |
<?php if ( isset( $formfields['fields'] ) ): ?> |
| 205 |
<?php foreach ( $formfields['fields'] as $key => $value ): ?> |
| 206 |
<tr> |
| 207 |
<td><?php esc_attr_e( $key ); ?></td> |
| 208 |
<td><?php echo is_array( $value ) ? esc_html( implode( ',', $value ) ) : esc_html( $value ); ?></td> |
| 209 |
</tr> |
| 210 |
<?php endforeach; ?> |
| 211 |
<?php else: ?> |
| 212 |
<?php foreach ( $formfields as $key => $value ): ?> |
| 213 |
<tr> |
| 214 |
<td><?php esc_attr_e( $key ); ?></td> |
| 215 |
<td><?php echo is_array( $value ) ? esc_html( implode( ',', $value ) ) : esc_html( $value ); ?></td> |
| 216 |
</tr> |
| 217 |
<?php endforeach; ?> |
| 218 |
<?php endif; ?> |
| 219 |
</table> |
| 220 |
<?php |
| 221 |
$content = ob_get_contents(); |
| 222 |
ob_end_clean(); |
| 223 |
echo wp_json_encode( [ 'status' => 200, 'content' => $content ] ); |
| 224 |
exit; |
| 225 |
} |
| 226 |
|
| 227 |
$this->get_logger()->warning( 'Invalid request for getDetails (missing or invalid nonce/hash)', [ |
| 228 |
'plugin' => 'double-opt-in', |
| 229 |
'class' => __CLASS__, |
| 230 |
'method' => __METHOD__, |
| 231 |
'post' => $_POST, |
| 232 |
] ); |
| 233 |
|
| 234 |
wp_die( 0 ); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
* Add the styles for the form |
| 240 |
*/ |
| 241 |
public function addStyles( $hook ) { |
| 242 |
$this->get_logger()->debug( 'addStyles called', [ |
| 243 |
'plugin' => 'double-opt-in', |
| 244 |
'class' => __CLASS__, |
| 245 |
'method' => __METHOD__, |
| 246 |
'hook' => $hook, |
| 247 |
] ); |
| 248 |
|
| 249 |
if ( $hook == 'tools_page_f12doubleoptin' ) { |
| 250 |
wp_enqueue_style( 'f12-cf7-doubleoptin-admin', plugins_url( 'assets/admin-style.css', __FILE__ ) ); |
| 251 |
wp_enqueue_script( 'f12-cf7-doubleoptin-admin', plugins_url( 'assets/f12-cf7-popup.js', __FILE__ ), [ 'jquery' ] ); |
| 252 |
|
| 253 |
$this->get_logger()->info( 'Admin styles and scripts enqueued for DOI tools page', [ |
| 254 |
'plugin' => 'double-opt-in', |
| 255 |
'class' => __CLASS__, |
| 256 |
'method' => __METHOD__, |
| 257 |
'hook' => $hook, |
| 258 |
] ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
new Ajax( Logger::getInstance() ); |
| 265 |
} |