| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Frontend |
| 10 |
* Responsible to handle the frontend of the Double OptIn |
| 11 |
* |
| 12 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 13 |
*/ |
| 14 |
class AvadaFrontend |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Admin constructor. |
| 18 |
*/ |
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
add_action('init', array($this, 'validateOptIn')); |
| 22 |
|
| 23 |
if(!isset($_GET['optin'])) { |
| 24 |
add_filter('fusion_form_send_mail_args', array($this, 'onSubmit'), 10, 3); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Store the optin value for the given object by the hash value |
| 30 |
* |
| 31 |
* @param $hash |
| 32 |
* @param $value |
| 33 |
* |
| 34 |
* @return int The number of rows changed. |
| 35 |
*/ |
| 36 |
private function updateOptInByHash($hash, $value) |
| 37 |
{ |
| 38 |
$OptIn = OptIn::get_by_hash($hash); |
| 39 |
if ($OptIn->is_confirmed()) { |
| 40 |
do_action('f12_cf7_doubleoptin_already_confirmed', $hash, $OptIn); |
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
do_action('f12_cf7_doubleoptin_before_confirm', $hash, $OptIn); |
| 45 |
|
| 46 |
$OptIn->set_doubleoptin($value); |
| 47 |
$OptIn->set_updatetime(time()); |
| 48 |
$OptIn->set_ipaddr_confirmation(CF7DoubleOptIn::getInstance()->getIPAdress()); |
| 49 |
|
| 50 |
$result = $OptIn->save(); |
| 51 |
if ($result) { |
| 52 |
do_action('f12_cf7_doubleoptin_after_confirm', $hash, $OptIn); |
| 53 |
} |
| 54 |
|
| 55 |
return $result; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Add Stylesheets |
| 60 |
*/ |
| 61 |
public function validateOptIn() |
| 62 |
{ |
| 63 |
|
| 64 |
if (!isset($_GET['optin'])) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
$hash = sanitize_text_field($_GET['optin']); |
| 69 |
|
| 70 |
$OptIn = OptIn::get_by_hash($hash); |
| 71 |
|
| 72 |
if (null != $OptIn) { |
| 73 |
if ($OptIn->isType('avada')) { |
| 74 |
if($this->updateOptInByHash($hash, 1) > 0) { |
| 75 |
// Setup the contact form 7 which will fire the form send after loading the page) |
| 76 |
$formData = maybe_unserialize($OptIn->get_content()); |
| 77 |
|
| 78 |
if (!isset($formData['fields']) || !isset($formData['sendmail_args'])) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$formData = CF7DoubleOptIn::getInstance()->sanitize_array($formData); |
| 83 |
|
| 84 |
$_POST = array('formData' => implode('&', $formData['fields'])); |
| 85 |
$_POST['field_labels'] = ''; |
| 86 |
$_POST['form_id'] = $OptIn->get_cf_form_id(); |
| 87 |
$_POST['hidden_field_names'] = ''; |
| 88 |
|
| 89 |
require_once('AvadaFormSubmit.class.php'); |
| 90 |
$AvadaFormSubmit = new AvadaFormSubmit(); |
| 91 |
$AvadaFormSubmit->submit($OptIn->get_cf_form_id(), $formData['sendmail_args']); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Add the double opt in link to the body |
| 99 |
* |
| 100 |
* @param string $body |
| 101 |
* @param OptIn $OptIn |
| 102 |
* @param int $page - The Page ID |
| 103 |
* |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
private function addDoubleOptInLink($body, $OptIn) |
| 107 |
{ |
| 108 |
$link = $OptIn->get_link_optin(); |
| 109 |
|
| 110 |
return str_replace('[doubleoptinlink]', $link, $body); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Add additional placeholder like time, date, subject |
| 115 |
* @param $form \WPCF7_ContactForm |
| 116 |
* @param $submission \WPCF7_Submission |
| 117 |
* @param $body |
| 118 |
* @param $OptIn |
| 119 |
* @param $parameter |
| 120 |
*/ |
| 121 |
private function addAdditionalPlaceholder($form_data, $body, $OptIn, $parameter) |
| 122 |
{ |
| 123 |
# set the default timezone |
| 124 |
date_default_timezone_set(get_option('timezone_string')); |
| 125 |
$placeholder = array( |
| 126 |
'doubleoptin_form_url' => $form_data['submission']['source_url]'], |
| 127 |
'doubleoptin_form_subject' => $parameter['subject'], |
| 128 |
'doubleoptin_form_date' => date(get_option('date_format')), |
| 129 |
'doubleoptin_form_time' => date(get_option('time_format')), |
| 130 |
'doubleoptin_form_email' => get_option('admin_email') |
| 131 |
); |
| 132 |
|
| 133 |
foreach ($placeholder as $key => $value) { |
| 134 |
$body = str_replace('[' . $key . ']', $value, $body); |
| 135 |
} |
| 136 |
return $body; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Add formular informaition to the database |
| 141 |
* |
| 142 |
* @param \WPCF7_ContactForm |
| 143 |
* @param array |
| 144 |
* |
| 145 |
* @return OptIn|null |
| 146 |
*/ |
| 147 |
private function addRequest($post_id, $parameter, $sendmail_args, $files = array()) |
| 148 |
{ |
| 149 |
global $wpdb; |
| 150 |
|
| 151 |
if (null == $wpdb) { |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
$copiedFiles = array(); |
| 156 |
|
| 157 |
if (!empty($files)) { |
| 158 |
foreach ($files as $key => $subfiles) { |
| 159 |
foreach ($subfiles as $file) { |
| 160 |
$newFile = explode('/', $file); |
| 161 |
$name = $newFile[count($newFile) - 1]; |
| 162 |
$name = time() . '_' . $name; |
| 163 |
$newFile[count($newFile) - 1] = $name; |
| 164 |
$newFile = implode("/", $newFile); |
| 165 |
|
| 166 |
if (copy($file, $newFile)) { |
| 167 |
$copiedFiles[] = $newFile; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
$parameter = \apply_filters('f12_cf7_doubleoptin_add_request_parameter', $parameter); |
| 174 |
|
| 175 |
$formParameter = CF7DoubleOptIn::getInstance()->getParameter($post_id); |
| 176 |
|
| 177 |
$form = get_post($post_id); |
| 178 |
|
| 179 |
$data = array( |
| 180 |
'cf_form_id' => $post_id, |
| 181 |
'doubleoptin' => 0, |
| 182 |
'createtime' => time(), |
| 183 |
'content' => maybe_serialize(array('fields' => $parameter, 'sendmail_args' => $sendmail_args)), |
| 184 |
'files' => maybe_serialize($copiedFiles), |
| 185 |
'ipaddr_register' => CF7DoubleOptIn::getInstance()->getIPAdress(), |
| 186 |
'category' => (int)$formParameter['category'], |
| 187 |
'form' => do_shortcode($form->post_content), |
| 188 |
'email' => $parameter[$formParameter['recipient']], |
| 189 |
); |
| 190 |
|
| 191 |
$OptIn = new OptIn($data); |
| 192 |
if ($OptIn->save()) { |
| 193 |
return $OptIn; |
| 194 |
} |
| 195 |
return null; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Validate if the optin is enabled. |
| 200 |
*/ |
| 201 |
private function isOptinEnabled($post_id) |
| 202 |
{ |
| 203 |
// Disable optin sending if the optin flag is set. |
| 204 |
if (isset($_GET['optin'])) { |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
$parameter = CF7DoubleOptIn::getInstance()->getParameter($post_id); |
| 209 |
|
| 210 |
if ((int)$parameter['enable'] != 1) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
return true; |
| 215 |
} |
| 216 |
|
| 217 |
private function get_results_from_message($type, $info){ |
| 218 |
return [ |
| 219 |
'status' => $type, |
| 220 |
'info' => $info, |
| 221 |
]; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* On Form Submit add the double optin if enabled |
| 226 |
* @param array $parameter |
| 227 |
* @param int $post_id |
| 228 |
* @param array $data |
| 229 |
*/ |
| 230 |
public function onSubmit($form_parameter, $submission_id, $form_data) |
| 231 |
{ |
| 232 |
$post_id = isset($_POST['form_id']) ? absint(sanitize_text_field(wp_unslash($_POST['form_id']))) : 0; // phpcs:ignore WordPress.Security.NonceVerification |
| 233 |
if (!$post_id) { |
| 234 |
return $form_parameter; |
| 235 |
} |
| 236 |
$parameter = CF7DoubleOptIn::getInstance()->getParameter($post_id); |
| 237 |
|
| 238 |
if ($this->isOptinEnabled($post_id)) { |
| 239 |
|
| 240 |
// Remove Contact Form 7 DB hook to ensure the optin mail is not saved |
| 241 |
$OptIn = $this->addRequest($post_id, $form_data['data'], $form_parameter, $form_parameter['attachments']); |
| 242 |
$body = $this->addDoubleOptInLink($parameter['body'], $OptIn); |
| 243 |
$body = $this->addAdditionalPlaceholder($form_data, $body, $OptIn, $parameter); |
| 244 |
|
| 245 |
// Update the opt in after setting the opt-in content. |
| 246 |
$OptIn->set_mail_optin($body); |
| 247 |
$OptIn->save(); |
| 248 |
|
| 249 |
// Prepare the Opt-In Mail |
| 250 |
$headers = 'MIME-Version: 1.0' . "\r\n"; |
| 251 |
$headers .= 'Content-type: text/html; charset=UTF8' . "\r\n"; |
| 252 |
$headers .= 'From: ' . $parameter['sender'] . "\r\n"; |
| 253 |
|
| 254 |
// $recipient = $parameter[$formParameter['recipient']]; |
| 255 |
|
| 256 |
\wp_mail( |
| 257 |
$form_data['data'][$parameter['recipient']], |
| 258 |
$parameter['subject'], |
| 259 |
$body, |
| 260 |
$headers |
| 261 |
); |
| 262 |
|
| 263 |
do_action('f12_cf7_doubleoptin_sent', $form_data, $post_id); |
| 264 |
|
| 265 |
die( wp_json_encode( $this->get_results_from_message( 'success', 'opt-in send' ) ) ); |
| 266 |
|
| 267 |
//$abort = true; |
| 268 |
} else if (isset($_GET['optin'])) { |
| 269 |
$OptIn = OptIn::get_by_hash(esc_sql($_GET['optin'])); |
| 270 |
} |
| 271 |
return $form_parameter; |
| 272 |
} |
| 273 |
} |
| 274 |
} |