PluginProbe
Poptin – Email Marketing Automation, Newsletter & Exit Pop Ups, Email Popups / 1.3.12
Poptin – Email Marketing Automation, Newsletter & Exit Pop Ups, Email Popups v1.3.12
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.12, at poptin.php

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