PluginProbe
Poptin – Email Marketing Automation, Newsletter & Exit Pop Ups, Email Popups / 1.3.6
Poptin – Email Marketing Automation, Newsletter & Exit Pop Ups, Email Popups v1.3.6
1.3.13 1.3.12 1.3.11 trunk 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2 1.2.1 1.3 1.3.1 1.3.10 1.3.2 1.3.3 1.3.4 All 30 releases
poptin / poptin.php

poptin.php in Poptin – Email Marketing Automation, Newsletter & Exit Pop Ups, Email Popups 1.3.6, at poptin.php

1,066 lines 40.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Poptin
4 Contributors: poptin, galdub, tomeraharon
5 Description: Use Poptin to get more leads, sales, and email subscribers. Create targeted beautiful pop ups and forms in less than 2 minutes with ease.
6 Version: 1.3.6
7 Author: Poptin
8 Author URI: https://www.poptin.com
9 Text Domain: poptin
10 Domain Path: /lang
11 License: GPL2
12 */
13
14 // Prevent direct file access
15 use Hummingbird\Core\Utils;
16
17 if (!defined('ABSPATH')) {
18 exit;
19 }
20
21 define('POPTIN_VERSION', '1.3.6');
22 define('POPTIN_PATH', dirname(__FILE__));
23 define('POPTIN_PATH_INCLUDES', dirname(__FILE__) . '/inc');
24 define('POPTIN_FOLDER', basename(POPTIN_PATH));
25 define('POPTIN_URL', plugins_url() . '/' . POPTIN_FOLDER);
26 define('POPTIN_URL_INCLUDES', POPTIN_URL . '/inc');
27
28 define('POPTIN_ID', get_option('poptin_id'));
29 define('POPTIN_USER_ID', get_option('poptin_user_id'));
30 define('POPTIN_MARKETPLACE_TOKEN', get_option('poptin_marketplace_token'));
31 define('POPTIN_MARKETPLACE_EMAIL_ID', get_option('poptin_marketplace_email_id'));
32 define('POPTIN_FRONT_SITE', 'https://www.poptin.com');
33
34 define('POPTIN_MARKETPLACE', 'Wrdprs');
35 define('POPTIN_APP_BASE_URL', 'https://app.popt.in/');
36 define('POPTIN_MARKETPLACE_LOGIN_URL', POPTIN_APP_BASE_URL . 'api/marketplace/auth');
37 define('POPTIN_MARKETPLACE_REGISTER_URL', POPTIN_APP_BASE_URL . 'api/marketplace/register');
38 define('POPTIN_CACERT_PATH', POPTIN_PATH . "/assets/ca-cert/cacert-2017-06-07.pem");
39
40 if (is_admin()) {
41 include_once "includes/class-affiliate.php";
42 include_once "includes/poptin-functions.php";
43 }
44
45 class POPTIN_Plugin_Base
46 {
47 public function __construct()
48 {
49 /**
50 * Activation Hook
51 * Deactivation Hook
52 */
53 register_activation_hook(__FILE__, 'poptin_activation_hook');
54 register_deactivation_hook(__FILE__, 'poptin_deactivation_hook');
55
56 add_action('admin_enqueue_scripts', array($this, 'poptin_add_admin_javascript'));
57 add_action('admin_enqueue_scripts', array($this, 'poptin_add_admin_css'));
58 // register admin pages for the plugin
59 add_action('admin_menu', array($this, 'poptin_admin_pages_callback'));
60 add_action('admin_init', array($this, 'admin_init'));
61
62 // Translation-ready
63 add_action('plugins_loaded', array($this, 'poptin_add_textdomain'));
64
65 add_filter('plugin_action_links_'.plugin_basename(__FILE__), [$this, 'plugin_action_links']);
66
67 $poptinidcheck = get_option('poptin_id', (isset($myOption_def) ? $myOption_def : false));
68 if ($poptinidcheck) {
69 $poptinid = get_option('poptin_id');
70 if (strlen($poptinid) != 13) {
71 update_option('poptin_id', '');
72 } else {
73 add_action('wp_head', array($this, 'poptin_add_script_frontend'));
74 }
75 }
76 // Add actions for storing value and fetching URL
77 // use the wp_ajax_nopriv_ hook for non-logged users (handle guest actions)
78
79 /**
80 * AJAX Calls registration
81 * Action => name followed after wp_ajax
82 */
83 add_action('wp_ajax_poptin_register', array($this, 'poptin_marketplace_registration'));
84 add_action('wp_ajax_poptin_logmein', array($this, 'poptin_markplace_login'));
85 add_action('wp_ajax_delete-id', array($this, 'delete_poptin_id'));
86 add_action('wp_ajax_add-id', array($this, 'add_poptin_id'));
87 add_action('wp_ajax_poptin_logout', array($this, 'handle_logout_ajax'));
88
89 /**
90 *
91 * Admin Initialization calls registration
92 * We need this to send user to Poptin's Admin page on activation
93 *
94 */
95 add_action('admin_init', 'poptin_plugin_redirect');
96 add_filter('admin_footer_text', array($this, 'replace_footer_text'));
97
98 /**
99 *
100 * Admin Initialization calls registration
101 * We need this to send user to Poptin's Admin page on activation
102 *
103 */
104 if (isset($_GET['poptin_logmein'])) {
105 if (!function_exists('is_user_logged_in')) {
106 require_once(ABSPATH . "wp-includes/pluggable.php");
107 }
108 if (is_user_logged_in()) {
109 if (current_user_can('administrator')) {
110 $after_registration = '';
111 if (isset($_GET['after_registration'])) {
112 $after_registration = $_GET['after_registration'];
113 }
114 $this->poptin_markplace_login_direct($after_registration);
115 } else {
116 echo '<style>html {background: #f1f1f1;}body{margin:0;padding:0;}h1 {background: #fff;font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;padding: 1em 2em;-webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);text-align:center;color: #666;font-size: 24px;margin: 30px auto 0;padding:20px;display:table;border-radius:5px}</style>';
117 echo '<h1>Please login as administrator</h1>';
118 die();
119 }
120 } else {
121 wp_redirect(wp_login_url());
122 }
123 }
124
125 // Add AJAX handlers
126 add_action('wp_ajax_poptin_logout', array($this, 'handle_logout_ajax'));
127
128 /**
129 * Clean Up Of the URL
130 * Not sure why is this here
131 */
132 add_filter('clean_url', 'async_scripts', 11, 1);
133
134 add_action('admin_footer', array($this, 'deactivate_modal'));
135 add_action('wp_ajax_poptin_plugin_deactivate', array($this, 'poptin_plugin_deactivate'));
136 }
137
138 /**
139 * Helper function to check if user is logged in to Poptin
140 */
141 private function poptin_is_logged_in() {
142 $poptin_id = get_option('poptin_id', '');
143 return !empty($poptin_id) && strlen($poptin_id) == 13;
144 }
145
146 /**
147 * Helper function to check if user has full registration (email + token)
148 */
149 private function poptin_has_full_registration() {
150 $token = get_option('poptin_marketplace_token', '');
151 $user_id = get_option('poptin_user_id', '');
152 return !empty($token) && !empty($user_id);
153 }
154
155 /**
156 * Get the iframe URL for full registration users
157 */
158 private function get_iframe_url() {
159 if (!function_exists('poptin_get_iframe_url')) {
160 include_once POPTIN_PATH . '/includes/poptin-functions.php';
161 }
162 return poptin_get_iframe_url();
163 }
164
165 /**
166 * Scope: Public
167 * Function: admin_init
168 * Description: Handles initialization actions for the admin panel. Specifically checks if the current page is the
169 * Poptin support page and redirects to the main Poptin page if applicable.
170 * Return: void | Performs a redirect to the specified admin URL if the condition is met.
171 **/
172 public function admin_init() {
173 if(isset($_GET['page']) && $_GET['page'] == 'Poptin-support') {
174 wp_redirect(admin_url("admin.php?page=poptin"));
175 exit;
176 }
177 }
178
179 /**
180 * Function: plugin_action_links
181 * Description: Adds a custom support link to the array of plugin action links.
182 * This method is generally used to provide quick access to support or documentation for the plugin.
183 *
184 * @param array $links Array of existing action links for the plugin.
185 *
186 * @return array Modified array of action links including the custom support link.
187 */
188 public function plugin_action_links($links) {
189 $links[] = '<a target="_blank" href="'.esc_url($this->poptin_support_link()).'">' . esc_html__( 'Need help?', 'ppbase' ) . '</a>';
190 return $links;
191 }
192
193 /**
194 * Retrieve the original footer text
195 *
196 * @return string
197 */
198 private function get_original_text()
199 {
200 global $wp_version;
201
202 /* The way of determining the default footer text was changed in 3.9 */
203 if (version_compare($wp_version, '3.9', '<')) {
204 $text = __('Thank you for creating with <a href="http://wordpress.org/">WordPress</a>.');
205 } else {
206 /* translators: %s: https://wordpress.org/ */
207 $text = sprintf(__('Thank you for creating with <a href="%s">WordPress</a>.'), __('https://wordpress.org/'));
208 }
209
210 return $text;
211 }
212 /**
213 * Replace the admin footer text
214 *
215 * @param string $footer_text The current footer text
216 *
217 * @return string The new footer text
218 */
219 function replace_footer_text($footer_text)
220 {
221 return str_replace($this->get_original_text(), '', $footer_text);
222 }
223
224 public function poptin_plugin_deactivate()
225 {
226 if (current_user_can('manage_options')) {
227 $postData = $_POST;
228 $errorCounter = 0;
229 $response = array();
230 $response['status'] = 0;
231 $response['message'] = "";
232 $response['valid'] = 1;
233 $reason = filter_input(INPUT_POST, 'reason');
234 $nonce = filter_input(INPUT_POST, 'nonce');
235 if (empty($reason)) {
236 $errorCounter++;
237 $response['message'] = "Please provide reason";
238 } else if (empty($nonce)) {
239 $response['message'] = esc_html__("Your request is not valid", 'poptin');
240 $errorCounter++;
241 $response['valid'] = 0;
242 } else {
243 if (!wp_verify_nonce($nonce, 'poptin_deactivate_nonce')) {
244 $response['message'] = esc_html__("Your request is not valid", 'poptin');
245 $errorCounter++;
246 $response['valid'] = 0;
247 }
248 }
249 if ($errorCounter == 0) {
250 global $current_user;
251 $email = "none@none.none";
252 if (isset($postData['email_id']) && !empty($postData['email_id']) && filter_var($postData['email_id'], FILTER_VALIDATE_EMAIL)) {
253 $email = $postData['email_id'];
254 }
255 $domain = site_url();
256 $user_name = $current_user->first_name . " " . $current_user->last_name;
257 $subject = "Poptin was removed from {$domain}";
258 $headers = "MIME-Version: 1.0\r\n";
259 $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
260 $headers .= 'From: ' . $user_name . ' <' . $email . '>' . PHP_EOL;
261 $headers .= 'Reply-To: ' . $user_name . ' <' . $email . '>' . PHP_EOL;
262 $headers .= 'X-Mailer: PHP/' . phpversion();
263 ob_start();
264 ?>
265 <table border="0" cellspacing="0" cellpadding="5">
266 <tr>
267 <th>Plugin</th>
268 <td>Poptin</td>
269 </tr>
270 <tr>
271 <th>Plugin Version</th>
272 <td><?php echo esc_attr(POPTIN_VERSION) ?></td>
273 </tr>
274 <tr>
275 <th>Domain</th>
276 <td><?php echo esc_url($domain) ?></td>
277 </tr>
278 <tr>
279 <th>Email</th>
280 <td><?php echo esc_attr($email) ?></td>
281 </tr>
282 <tr>
283 <th>Reason</th>
284 <td><?php echo nl2br(esc_attr($reason)) ?></td>
285 </tr>
286 <tr>
287 <th>WordPress Version</th>
288 <td><?php echo esc_attr(get_bloginfo('version')) ?></td>
289 </tr>
290 <tr>
291 <th>PHP Version</th>
292 <td><?php echo esc_attr(PHP_VERSION) ?></td>
293 </tr>
294 </table>
295 <?php
296 $content = ob_get_clean();
297 $email_id = "contact@poptin.on.crisp.email";
298 wp_mail($email_id, $subject, $content, $headers);
299 $response['status'] = 1;
300 }
301 echo json_encode($response);
302 wp_die();
303 }
304 }
305
306 public function deactivate_modal()
307 {
308 if (current_user_can('manage_options')) {
309 global $pagenow;
310
311 if ('plugins.php' !== $pagenow) {
312 return;
313 }
314
315 include 'deactivate-form.php';
316 }
317 }
318
319 /**
320 *
321 *
322 * Scope: Private
323 * Function: poptin_marketplace_registration
324 * Description: Will be called via the Admin Page AJAX
325 * Will be checked with WP nounce.
326 * If the verification is OK, we go ahead and create the account.
327 * Email Address - Would be pre-filled in the form in the Admin Page.
328 * User wants, can change the email id.
329 * We store the registration email ID, to ensure that we do not have issues in future.
330 * Basic wrapper function to the poptin_middleware_registration_curl
331 * Responds in JSON from the cURL call.
332 * Parameters: email (argument)
333 * domain - retrieved from the Wordpress base.
334 * first_name - derived from the nice_name of the existing logged in user.
335 * last_name - derived from the nice_name of the existing logged in user.
336 * Return: Return response is derived directly from the chain function poptin_middleware_registration_curl
337 *
338 *
339 */
340 function poptin_marketplace_registration()
341 {
342 if (!current_user_can('manage_options')) {
343 die('You are now allowed to do this.');
344 }
345
346 $email = sanitize_email($_POST['email']);
347 /**
348 *
349 * We check the sanitization here again for the email id.
350 * This is for AJAX call, hence the double check is required.
351 * If this is okay, we go ahead and send the data poptin_marketplace_registration function.
352 *
353 */
354 if ($email) {
355 } else {
356 $return_array = array();
357 $return_array['success'] = false;
358 $return_array['message'] = 'Invalid Email Address found.';
359 echo json_encode($return_array);
360 exit(0);
361 }
362
363 if (check_ajax_referer('poptin-fe-register', 'security')) {
364 $domain = site_url();
365 if (!function_exists('wp_get_current_user')) {
366 require_once(ABSPATH . "wp-includes/pluggable.php");
367 }
368 $user_data = wp_get_current_user();
369 $user_nice_name = $user_data->data->user_nicename;
370 $user_nice_name_array = explode(" ", $user_nice_name);
371 $first_name = $user_nice_name_array[0];
372 $last_name = "";
373 if (isset($user_nice_name_array[1])) {
374 $last_name = $user_nice_name_array[1];
375 } else {
376 $last_name = $user_nice_name_array[0];
377 }
378 $this->poptin_middleware_registration_curl($first_name, $last_name, $domain, $email);
379 } else {
380 $return_array = array();
381 $return_array['success'] = false;
382 $return_array['message'] = 'Invalid request. Please refresh & try again.';
383 echo json_encode($return_array);
384 exit(0);
385 }
386 }
387
388
389
390 /**
391 *
392 *
393 * Scope: Private
394 * Function: poptin_markplace_login_direct
395 * Description: Based on the Options stored in the WP database
396 * We make request to the marketplace api for login
397 * In simpler terms this is a wrapper function for making AJAX call -> to cURL call -> to respond back.
398 * Parameters: POST Data NOT required
399 * - token [Generated at the time of registration from the marketplace API ONLY]
400 * - email [Email ID used at the time of registration from the marketplace API ONLY]
401 * Return: login_url - if it went successful
402 * success - true/false
403 *
404 *
405 */
406 function poptin_markplace_login_direct($after_registration = '')
407 {
408 $token = POPTIN_MARKETPLACE_TOKEN;
409 $user_id = POPTIN_USER_ID;
410 $curl_URL = POPTIN_MARKETPLACE_LOGIN_URL;
411 $curl_post_array = array(
412 'token' => $token,
413 'user_id' => $user_id
414 );
415 $curl_options = $this->generate_curl_options($curl_URL, $curl_post_array);
416 $curl = curl_init();
417 curl_setopt_array($curl, $curl_options);
418 $response = curl_exec($curl);
419 $err = curl_error($curl);
420 curl_close($curl);
421 $response_return_array = array();
422 if ($err) {
423 $response_return_array['success'] = false;
424 echo json_encode($response_return_array);
425 exit(0);
426 } else {
427 $response_array = json_decode($response);
428 if ($response_array->success) {
429 $login_url = $response_array->login_url;
430 // If user just registered
431 if ($after_registration != '') {
432 $login_url .= '&utm_source=wordpress';
433 }
434 header("Location: " . $login_url);
435 exit(0);
436 } else {
437 exit(wp_redirect(admin_url("admin.php?page=poptin")));
438 }
439 }
440 exit(wp_redirect(admin_url("admin.php?page=poptin")));
441 }
442
443
444 /**
445 * Scope: Public
446 * Function: poptin_add_admin_javascript
447 * Description: Will be called via the Admin Page AJAX
448 * Will be checked with WP nounce.
449 * If the verification is OK, we go ahead and create the account.
450 * Email Address - Would be pre-filled in the form in the Admin Page.
451 * User wants, can change the email id.
452 * We store the registration email ID, to ensure that we do not have issues in future.
453 * Basic wrapper function to the poptin_middleware_registration_curl
454 * Responds in JSON from the cURL call.
455 * Parameters: email (argument)
456 * domain - retrieved from the Wordpress base.
457 * first_name - derived from the nice_name of the existing logged in user.
458 * last_name - derived from the nice_name of the existing logged in user.
459 * Return: Return response is derived directly from the chain function poptin_middleware_registration_curl
460 */
461 public function poptin_add_admin_javascript($hook)
462 {
463 if (strpos($hook, 'poptin') !== false) {
464 wp_enqueue_script('jquery');
465 wp_register_script('poptin-admin', plugins_url('assets/js/poptin-admin.js', __FILE__), array('jquery'), POPTIN_VERSION, true);
466 wp_enqueue_script('poptin-admin');
467
468 // Determine auto-login URL for full registration users
469 $auto_login_url = POPTIN_APP_BASE_URL;
470 if ($this->poptin_has_full_registration()) {
471 // Try to get the login URL from the marketplace API
472 $token = get_option('poptin_marketplace_token', '');
473 $user_id = get_option('poptin_user_id', '');
474
475 if (!empty($token) && !empty($user_id)) {
476 $curl_URL = POPTIN_MARKETPLACE_LOGIN_URL;
477 $curl_post_array = array(
478 'token' => $token,
479 'user_id' => $user_id
480 );
481
482 $curl_options = $this->generate_curl_options($curl_URL, $curl_post_array);
483 $curl = curl_init();
484 curl_setopt_array($curl, $curl_options);
485 $response = curl_exec($curl);
486 $err = curl_error($curl);
487 curl_close($curl);
488
489 if (!$err && $response) {
490 $response_array = json_decode($response);
491 if (isset($response_array->success) && $response_array->success && isset($response_array->login_url)) {
492 $auto_login_url = $response_array->login_url;
493 }
494 }
495 }
496 }
497
498 $settings = [
499 'after_registration_url' => admin_url("admin.php?page=poptin"),
500 'poptin_app_base_url' => POPTIN_APP_BASE_URL,
501 'support_link' => $this->poptin_support_link(),
502 'has_marketplace_token' => $this->poptin_has_full_registration(),
503 'auto_login_url' => $auto_login_url,
504 'iframe_url' => $this->poptin_has_full_registration() ? $this->get_iframe_url() : ''
505 ];
506 wp_localize_script('poptin-admin', 'poptin_settings', $settings);
507 wp_register_script('bootstrap-modal', plugins_url('assets/js/bootstrap.min.js', __FILE__), array('jquery'), POPTIN_VERSION, true);
508 wp_enqueue_script('bootstrap-modal');
509 }
510 }
511
512
513 /**
514 * Scope: Public
515 * Function: poptin_support_link
516 * Description: Returns the URL for the Poptin plugin support page.
517 * Return: String | URL of the Poptin plugin support page.
518 **/
519 public function poptin_support_link() {
520 return 'https://wordpress.org/support/plugin/poptin/';
521 }
522
523
524
525 /**
526 * Scope: Public
527 * Function: poptin_add_script_frontend
528 * Description: Will add the Poptin JS code to the system.
529 * Parameters: None
530 */
531 public function poptin_add_script_frontend()
532 {
533 // Determine script URL based on POPTIN_APP_BASE_URL
534 if (POPTIN_APP_BASE_URL === 'https://app.popt.in/') {
535 $script_url = 'https://cdn.popt.in/pixel.js';
536 } else {
537 // Remove trailing slash if present and add js/pixel.js
538 $base_url = rtrim(POPTIN_APP_BASE_URL, '/');
539 $script_url = $base_url . '/js/pixel.js';
540 }
541
542 echo "<script id='pixel-script-poptin' src='" . esc_url($script_url) . "?id=" . POPTIN_ID . "' async='true'></script> ";
543 }
544
545 /**
546 * Scope: Public
547 * Function: poptin_add_admin_css
548 * Description: Will add the backend CSS required for the display of poptin settings page.
549 * Parameters: hook | Not used.
550 */
551 public function poptin_add_admin_css($hook)
552 {
553 if (strpos($hook, 'poptin') !== false) {
554 wp_register_style('poptin-admin', plugins_url('assets/css/poptin-admin.css', __FILE__), array(), POPTIN_VERSION);
555 wp_enqueue_style('poptin-admin');
556 wp_register_style('bootstrap-modal-css', plugins_url('assets/css/bootstrap.min.css', __FILE__), array(), POPTIN_VERSION);
557 wp_enqueue_style('bootstrap-modal-css');
558 }
559 }
560
561 /**
562 * Scope: Public
563 * Function: poptin_admin_pages_callback
564 * Description: Will add the Poptin Page into the Menu System of Wordpress
565 * Parameters: None
566 */
567 public function poptin_admin_pages_callback()
568 {
569 // Main menu page
570 add_menu_page(
571 __("Poptin", 'ppbase'),
572 __("Poptin", 'ppbase'),
573 'manage_options',
574 'poptin',
575 array($this, 'poptin_admin_view'),
576 POPTIN_URL . '/assets/images/menu-icon.png'
577 );
578
579 // Only show these additional options if user is logged in
580 if ($this->poptin_is_logged_in()) {
581
582 // Dashboard submenu - now handled by main page with iframe overlay
583 add_submenu_page(
584 'poptin',
585 __("Dashboard", 'ppbase'),
586 __("Dashboard", 'ppbase'),
587 'manage_options',
588 'poptin', // Same as main page
589 array($this, 'poptin_admin_view')
590 );
591
592 add_submenu_page(
593 'poptin',
594 __("Poptin Full-Screen View", 'ppbase'),
595 __("Full-Screen View", 'ppbase'),
596 'manage_options',
597 'poptin-full-screen',
598 array($this, 'poptin_fullscreen_view')
599 );
600
601 add_submenu_page(
602 'poptin',
603 esc_html__( "Support", 'ppbase'),
604 esc_html__( "Support", 'ppbase'),
605 'manage_options',
606 'poptin-support',
607 [$this, 'screen']
608 );
609
610 add_submenu_page(
611 'poptin',
612 __("Poptin Logout", 'ppbase'),
613 __("Log Out", 'ppbase'),
614 'manage_options',
615 'poptin-logout',
616 array($this, 'poptin_logout_view')
617 );
618 }
619 }
620
621 /**
622 * Scope: Public
623 * Function: poptin_admin_view
624 * Description: The main admin view - shows login form or success page with iframe overlay
625 * Parameters: None
626 */
627 public function poptin_admin_view() {
628 // Use the original admin view file which handles both states
629 $admin_view_file = POPTIN_PATH . '/views/poptin_admin_view.php';
630
631 if (file_exists($admin_view_file)) {
632 include_once($admin_view_file);
633 }
634
635 // Include modals for logout functionality
636 $modals_file = POPTIN_PATH . '/views/poptin_modals.php';
637 if (file_exists($modals_file)) {
638 include_once($modals_file);
639 }
640 }
641
642 /**
643 * Scope: Public
644 * Function: poptin_add_textdomain
645 * Description: --
646 * Parameters: None
647 */
648 public function poptin_add_textdomain()
649 {
650 load_plugin_textdomain('poptin', false, dirname(plugin_basename(__FILE__)) . '/lang/');
651 }
652
653
654
655 /**
656 * Scope: Public
657 * Function: delete_poptin_id
658 * Description: AJAX wrapper for removing Poptin ID
659 * Parameters: None
660 */
661 function delete_poptin_id()
662 {
663 if (!current_user_can('manage_options')) {
664 die('You now allowed to do this.');
665 }
666
667
668 if (!isset($_POST['data']['nonce']) || !wp_verify_nonce($_POST['data']['nonce'], "ppFormIdDeactivate")) {
669 die('You now allowed to do this.');
670 }
671
672 update_option('poptin_id', '');
673 update_option('poptin_marketplace_token', '');
674 update_option('poptin_marketplace_email_id', '');
675 update_option('poptin_user_id', '');
676 poptin_clear_all_caches();
677 die(json_encode(
678 array(
679 'success' => true,
680 'message' => 'Database updated successfully.'
681 )
682 ));
683 }
684
685 /**
686 * Scope: Public
687 * Function: add_poptin_id
688 * Description: AJAX wrapper for adding Poptin ID, only used when user enters the Poptin ID manually.
689 * Parameters: None
690 */
691 function add_poptin_id()
692 {
693 if (!current_user_can('manage_options')) {
694 die('You now allowed to do this.');
695 }
696 if (!isset($_POST['data']['nonce']) || !wp_verify_nonce($_POST['data']['nonce'], "ppFormIdRegister")) {
697 die('You now allowed to do this.');
698 }
699
700 if (isset($_POST['data']) && isset($_POST['data']['poptin_id'])) {
701 $id = $_POST['data']['poptin_id'];
702 update_option('poptin_id', $id);
703 poptin_clear_all_caches();
704 die(json_encode(
705 array(
706 'success' => true,
707 'message' => 'Database updated successfully.'
708 )
709 ));
710 } else {
711 die(json_encode(
712 array(
713 'success' => false,
714 'message' => 'Wrong id.'
715 )
716 ));
717 }
718 }
719
720
721 /**
722 *
723 *
724 * Scope: Private
725 * Function: poptin_middleware_registration_curl
726 * Description:
727 * Arguments:
728 * Return: JSON Response
729 *
730 *
731 **/
732 private function poptin_middleware_registration_curl($first_name, $last_name, $domain, $email)
733 {
734 /*
735 * Because Wordpress doesn't provide one
736 * We will rely on the Middleware's Country Code for this one
737 * Overriding Country Code
738 */
739 /*
740 * Not sending First + Last Name as per Gal Dubinski
741 * first_name => $first_name,
742 last_name => $last_name,
743 */
744 $curl_URL = POPTIN_MARKETPLACE_REGISTER_URL;
745 $curl_post_array = array(
746 'domain' => $domain,
747 'marketplace' => POPTIN_MARKETPLACE,
748 'email' => $email
749 );
750
751 $curl_options = $this->generate_curl_options($curl_URL, $curl_post_array);
752 $curl = curl_init();
753 curl_setopt_array($curl, $curl_options);
754 $response = curl_exec($curl);
755 $err = curl_error($curl);
756 curl_close($curl);
757 $response_return_array = array();
758 if ($err) {
759 $response_return_array['success'] = false;
760 $response_return_array['message'] = $err;
761 echo json_encode($response_return_array);
762 exit(0);
763 } else {
764 $response_array = json_decode($response);
765 if ($response_array->success) {
766 $response_return_array['success'] = true;
767 $response_return_array['message'] = "Registration successful";
768 $response_return_array['js_client_id'] = $response_array->client_id;
769 $response_return_array['user_token'] = $response_array->token;
770
771 /**
772 * On Success
773 * We setup the update options
774 */
775 update_option("poptin_id", $response_array->client_id);
776 update_option("poptin_user_id", $response_array->user_id);
777 update_option("poptin_marketplace_token", $response_array->token);
778 update_option("poptin_marketplace_email_id", $email);
779 poptin_clear_all_caches();
780 echo json_encode($response_return_array);
781 exit(0);
782 } else {
783 $response_return_array['success'] = false;
784 $response_return_array['message'] = $response_array->message;
785 echo json_encode($response_return_array);
786 exit(0);
787 }
788 }
789 }
790
791 /**
792 * Scope: Private
793 * Function: generate_curl_options
794 * Description: This is Utility Function generates the POST cURL calls options.
795 * Have placed a function to ensure it remains generic and updates do not require many changes.
796 * Uses the CA Cert certificate.
797 * Return: Array | Options Array for cURL Post method call.
798 **/
799 private function generate_curl_options($curl_URL, $curl_post_array)
800 {
801 $curl_options_array = array(
802 CURLOPT_URL => $curl_URL,
803 CURLOPT_RETURNTRANSFER => true,
804 CURLOPT_ENCODING => "",
805 CURLOPT_MAXREDIRS => 20,
806 CURLOPT_TIMEOUT => 120,
807 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
808 CURLOPT_CUSTOMREQUEST => "POST",
809 CURLOPT_POSTFIELDS => $curl_post_array,
810 CURLOPT_CAINFO => POPTIN_CACERT_PATH
811 );
812 return $curl_options_array;
813 }
814
815 public function poptin_fullscreen_view() {
816 // Instead of redirecting, we'll use JavaScript to open in new tab
817 ?>
818 <script type="text/javascript">
819 window.open('<?php echo esc_url(POPTIN_APP_BASE_URL); ?>', '_blank');
820 // Redirect back to main page after opening new tab
821 window.location.href = '<?php echo admin_url("admin.php?page=poptin"); ?>';
822 </script>
823 <?php
824 exit;
825 }
826
827 public function poptin_logout_view() {
828 // Clear Poptin data (similar to your existing delete_poptin_id method)
829 update_option('poptin_id', '');
830 update_option('poptin_marketplace_token', '');
831 update_option('poptin_marketplace_email_id', '');
832 update_option('poptin_user_id', '');
833 poptin_clear_all_caches();
834
835 // Redirect back to main page
836 wp_redirect(admin_url('admin.php?page=poptin'));
837 exit;
838 }
839
840 public function handle_logout_ajax() {
841 // Check if user has permission
842 if (!current_user_can('manage_options')) {
843 wp_send_json_error('Insufficient permissions');
844 return;
845 }
846
847 // Verify nonce
848 if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'poptin_logout_nonce')) {
849 wp_send_json_error('Security check failed');
850 return;
851 }
852
853 // Clear Poptin data (same as your existing delete_poptin_id method)
854 update_option('poptin_id', '');
855 update_option('poptin_marketplace_token', '');
856 update_option('poptin_marketplace_email_id', '');
857 update_option('poptin_user_id', '');
858 poptin_clear_all_caches();
859
860 wp_send_json_success('Logged out successfully');
861 }
862 }
863
864 /**
865 * Scope: Public
866 * Function: poptin_shortcode
867 * Description: Converts the content value to poptin div.
868 * [poptin-embed 123456789] => <div class='poptin-123456789'></div>
869 * Return: Echo of generated <div></div>
870 **/
871 function poptin_shortcode($arguments)
872 {
873 if(isset($arguments[0]) && !empty($arguments[0])) {
874 $poptin_div_id = $arguments[0];
875 return "<div class='poptin-'".esc_attr($poptin_div_id)."'></div>";
876 }
877 }
878 // add_shortcode('poptin-embed', 'poptin_shortcode');
879
880 /**
881 * Scope: Public
882 * Function: poptin_shortcode_form
883 * Description: Converts the content value to poptin form.
884 * [poptin-form 123456789] => <div class="poptin-embedded" data-id="123456789"></div>
885 * Return: Echo of generated <div></div>
886 **/
887 function poptin_shortcode_form($arguments)
888 {
889 if(isset($arguments[0]) && !empty($arguments[0])) {
890 $poptin_div_id = $arguments[0];
891 return "<div class='poptin-embedded' data-id='" . esc_attr($poptin_div_id) . "'></div>";
892 }
893 }
894 add_shortcode('poptin-form', 'poptin_shortcode_form');
895
896 /**
897 * Scope: Public
898 * Function: async_scripts
899 * Description: Async URL for changes from asyncload to async='async'
900 * Return: Async URL
901 **/
902 function async_scripts($url)
903 {
904 if (strpos($url, '#asyncload') === false)
905 return $url;
906 else if (is_admin())
907 return str_replace('#asyncload', '', $url);
908 else
909 return str_replace('#asyncload', '', $url) . "' async='async";
910 }
911
912 /**
913 * Scope: Public
914 * Function: poptin_plugin_redirect
915 * Description: Called via admin_init action in Constructor
916 * Will redirect to the plugin page if the poptin_plugin_redirection is setup.
917 * Once redirection is pushed, the key is removed.
918 * Return: void
919 **/
920 function poptin_plugin_redirect()
921 {
922 if (!defined("DOING_AJAX") && get_option('poptin_plugin_redirection', false)) {
923 delete_option('poptin_plugin_redirection');
924 exit(wp_redirect(admin_url("admin.php?page=poptin")));
925 }
926 }
927
928
929 /**
930 * Scope: Public
931 * Function: poptin_activation_hook
932 * Description: On installation of the plugin this will be called.
933 * We want to setup/update poptin related options at this time.
934 * Options to change to blank
935 * - poptin_id [Poptin ID used for JS injection]
936 * - poptin_marketplace_token [Poptin Marketplace Token | Generated at the time of registration from the plugin ONLY]
937 * - poptin_marketplace_email_id [Poptin Marketplace Email ID | used at the time of registration from the plugin ONLY]
938 * Return: void
939 **/
940 function poptin_activation_hook()
941 {
942 /**
943 * We want these three options to be blank on activation/installation of the plugin
944 */
945 add_option('poptin_id', '', '', 'yes');
946 add_option("poptin_marketplace_token", "", '', 'yes');
947 add_option("poptin_marketplace_email_id", "", '', 'yes');
948 add_option("poptin_user_id", "", '', 'yes');
949
950 /**
951 * We want to take the user to the Plugin Page on installation.
952 * Hence setting up a temporary redirection key.
953 * It gets removed as soon as it's called for the first time.
954 * Ussage at : poptin_plugin_redirect, and called with admin_init
955 */
956 if(!defined("DOING_AJAX")) {
957 add_option('poptin_plugin_redirection', true);
958 }
959 }
960
961
962 /**
963 * Scope: Public
964 * Function: poptin_decativation_hook
965 * Description: On deactivation of the plugin this will be called.
966 * We want to delete poptin related options at this time.
967 * Options to change to remove
968 * - poptin_id [Poptin ID used for JS injection]
969 * - poptin_marketplace_token [Poptin Marketplace Token | Generated at the time of registration from the plugin ONLY]
970 * - poptin_marketplace_email_id [Poptin Marketplace Email ID | used at the time of registration from the plugin ONLY]
971 * Return: void
972 **/
973 function poptin_deactivation_hook()
974 {
975 delete_option('poptin_id');
976 delete_option('poptin_marketplace_token');
977 delete_option('poptin_marketplace_email_id');
978 delete_option('poptin_user_id');
979
980 /**
981 * If At all this was not removed already
982 */
983 delete_option('poptin_plugin_redirection');
984 poptin_clear_all_caches();
985 }
986
987 /**
988 * Scope: Public
989 * Function: poptin_clear_all_caches
990 * Description: Clears caches from below plugins if any
991 * - W3Total Cache
992 * - SuperCache
993 * - WPFastestCache
994 * - WP Engine
995 * Return: void
996 **/
997 function poptin_clear_all_caches()
998 {
999 try {
1000 global $wp_fastest_cache;
1001 // if W3 Total Cache is being used, clear the cache
1002 if (function_exists('w3tc_flush_all')) {
1003 w3tc_flush_all();
1004 }
1005 /* if WP Super Cache is being used, clear the cache */
1006 if (function_exists('wp_cache_clean_cache')) {
1007 global $file_prefix, $supercachedir;
1008 if (empty($supercachedir) && function_exists('get_supercache_dir')) {
1009 $supercachedir = get_supercache_dir();
1010 }
1011 wp_cache_clean_cache($file_prefix);
1012 }
1013
1014 if (class_exists('WpeCommon')) {
1015 //be extra careful, just in case 3rd party changes things on us
1016 if (method_exists('WpeCommon', 'purge_memcached')) {
1017 //WpeCommon::purge_memcached();
1018 }
1019 if (method_exists('WpeCommon', 'clear_maxcdn_cache')) {
1020 //WpeCommon::clear_maxcdn_cache();
1021 }
1022 if (method_exists('WpeCommon', 'purge_varnish_cache')) {
1023 //WpeCommon::purge_varnish_cache();
1024 }
1025 }
1026
1027 if (method_exists('WpFastestCache', 'deleteCache') && !empty($wp_fastest_cache)) {
1028 $wp_fastest_cache->deleteCache();
1029 }
1030 if (function_exists('rocket_clean_domain')) {
1031 rocket_clean_domain();
1032 // Preload cache.
1033 if (function_exists('run_rocket_sitemap_preload')) {
1034 run_rocket_sitemap_preload();
1035 }
1036 }
1037
1038 if (class_exists("autoptimizeCache") && method_exists("autoptimizeCache", "clearall")) {
1039 autoptimizeCache::clearall();
1040 }
1041
1042 if (class_exists("LiteSpeed_Cache_API") && method_exists("autoptimizeCache", "purge_all")) {
1043 LiteSpeed_Cache_API::purge_all();
1044 }
1045
1046 if (class_exists('\Hummingbird\Core\Utils')) {
1047
1048 $modules = Utils::get_active_cache_modules();
1049 foreach ($modules as $module => $name) {
1050 $mod = Utils::get_module($module);
1051
1052 if ($mod->is_active()) {
1053 if ('minify' === $module) {
1054 $mod->clear_files();
1055 } else {
1056 $mod->clear_cache();
1057 }
1058 }
1059 }
1060 }
1061 } catch (Exception $e) {
1062 return 1;
1063 }
1064 }
1065 $poptinBase = new POPTIN_Plugin_Base();
1066