Plugin Directory

source: poptin/tags/1.3/poptin.php

Last change on this file was 2602192, checked in by galdub, 4 years ago

stable tag 1.3

File size: 30.4 KB
Line 
1<?php
2/*
3Plugin Name: Poptin
4Contributors: galdub, tomeraharon
5Description: Use Poptin to get more leads, sales, and email subscribers. Create targeted beautiful pop ups and forms in less than 2 minutes with ease.
6Version: 1.3
7Author: Poptin
8Author URI: https://www.poptin.com
9Text Domain: poptin
10Domain Path: /lang
11License: GPL2
12*/
13
14// Prevent direct file access
15if (!defined('ABSPATH')) {
16    exit;
17}
18
19define('POPTIN_VERSION', '1.6');
20define('POPTIN_PATH', dirname(__FILE__));
21define('POPTIN_PATH_INCLUDES', dirname(__FILE__) . '/inc');
22define('POPTIN_FOLDER', basename(POPTIN_PATH));
23define('POPTIN_URL', plugins_url() . '/' . POPTIN_FOLDER);
24define('POPTIN_URL_INCLUDES', POPTIN_URL . '/inc');
25
26define('POPTIN_ID', get_option('poptin_id'));
27define('POPTIN_USER_ID', get_option('poptin_user_id'));
28define('POPTIN_MARKETPLACE_TOKEN', get_option('poptin_marketplace_token'));
29define('POPTIN_MARKETPLACE_EMAIL_ID', get_option('poptin_marketplace_email_id'));
30define('POPTIN_FRONT_SITE', 'https://www.poptin.com');
31
32define('POPTIN_MARKETPLACE', 'Wrdprs');
33define('POPTIN_MARKETPLACE_LOGIN_URL', 'https://app.popt.in/api/marketplace/auth');
34define('POPTIN_MARKETPLACE_REGISTER_URL', 'https://app.popt.in/api/marketplace/register');
35define('POPTIN_CACERT_PATH', POPTIN_PATH . "/assets/ca-cert/cacert-2017-06-07.pem");
36
37
38if (is_admin()) {
39    include_once "class-affiliate.php";
40}
41class POPTIN_Plugin_Base
42{
43    public function __construct()
44    {
45        /**
46         * Activation Hook
47         * Deactivation Hook
48         */
49        register_activation_hook(__FILE__, 'poptin_activation_hook');
50        register_deactivation_hook(__FILE__, 'poptin_deactivation_hook');
51
52        add_action('admin_enqueue_scripts', array($this, 'poptin_add_admin_javascript'));
53        add_action('admin_enqueue_scripts', array($this, 'poptin_add_admin_css'));
54        // register admin pages for the plugin
55        add_action('admin_menu', array($this, 'poptin_admin_pages_callback'));
56
57        // Translation-ready
58        add_action('plugins_loaded', array($this, 'poptin_add_textdomain'));
59
60        $poptinidcheck = get_option('poptin_id', (isset($myOption_def) ? $myOption_def : false));
61        if ($poptinidcheck) {
62            $poptinid = get_option('poptin_id');
63            if (strlen($poptinid) != 13) {
64                update_option('poptin_id', '');
65            } else {
66                add_action('wp_head', array($this, 'poptin_add_script_frontend'));
67            }
68        }
69        // Add actions for storing value and fetching URL
70        // use the wp_ajax_nopriv_ hook for non-logged users (handle guest actions)
71
72        /**
73         * AJAX Calls registration
74         * Action => name followed after wp_ajax
75         */
76        add_action('wp_ajax_poptin_register', array($this, 'poptin_marketplace_registration'));
77        add_action('wp_ajax_poptin_logmein', array($this, 'poptin_markplace_login'));
78        add_action('wp_ajax_delete-id', array($this, 'delete_poptin_id'));
79        add_action('wp_ajax_add-id', array($this, 'add_poptin_id'));
80
81        /**
82         *
83         * Admin Initialization calls registration
84         * We need this to send user to Poptin's Admin page on activation
85         *
86         */
87        add_action('admin_init', 'poptin_plugin_redirect');
88        add_filter('admin_footer_text', array($this, 'replace_footer_text'));
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        if (isset($_GET['poptin_logmein'])) {
97            if (!function_exists('is_user_logged_in')) {
98                require_once(ABSPATH . "wp-includes/pluggable.php");
99            }
100            if (is_user_logged_in()) {
101                if (current_user_can('administrator')) {
102                    $after_registration = '';
103                    if (isset($_GET['after_registration'])) {
104                        $after_registration = $_GET['after_registration'];
105                    }
106                    $this->poptin_markplace_login_direct($after_registration);
107                } else {
108                    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>';
109                    echo '<h1>Please login as administrator</h1>';
110                    die();
111                }
112            } else {
113                wp_redirect(wp_login_url());
114            }
115        }
116
117        /**
118         * Clean Up Of the URL
119         * Not sure why is this here
120         */
121        add_filter('clean_url', 'async_scripts', 11, 1);
122
123        add_action('admin_footer', array($this, 'deactivate_modal'));
124        add_action('wp_ajax_poptin_plugin_deactivate', array($this, 'poptin_plugin_deactivate'));
125    }
126
127    /**
128     * Retrieve the original footer text
129     *
130     * @return string
131     */
132    private function get_original_text()
133    {
134        global $wp_version;
135
136        /* The way of determining the default footer text was changed in 3.9 */
137        if (version_compare($wp_version, '3.9', '<')) {
138            $text = __('Thank you for creating with <a href="http://wordpress.org/">WordPress</a>.');
139        } else {
140            /* translators: %s: https://wordpress.org/ */
141            $text = sprintf(__('Thank you for creating with <a href="%s">WordPress</a>.'), __('https://wordpress.org/'));
142        }
143
144        return $text;
145    }
146    /**
147     * Replace the admin footer text
148     *
149     * @param string $footer_text The current footer text
150     *
151     * @return string The new footer text
152     */
153    function replace_footer_text($footer_text)
154    {
155        return str_replace($this->get_original_text(), '', $footer_text);
156    }
157
158    public function poptin_plugin_deactivate()
159    {
160        if (current_user_can('manage_options')) {
161            $postData = $_POST;
162            $errorCounter = 0;
163            $response = array();
164            $response['status'] = 0;
165            $response['message'] = "";
166            $response['valid'] = 1;
167            $reason = filter_input(INPUT_POST, 'reason', FILTER_SANITIZE_STRING);
168            $nonce = filter_input(INPUT_POST, 'nonce', FILTER_SANITIZE_STRING);
169            if (empty($reason)) {
170                $errorCounter++;
171                $response['message'] = "Please provide reason";
172            } else if (empty($nonce)) {
173                $response['message'] = esc_attr__("Your request is not valid", CHT_OPT);
174                $errorCounter++;
175                $response['valid'] = 0;
176            } else {
177                if (!wp_verify_nonce($nonce, 'poptin_deactivate_nonce')) {
178                    $response['message'] = esc_attr__("Your request is not valid", CHT_OPT);
179                    $errorCounter++;
180                    $response['valid'] = 0;
181                }
182            }
183            if ($errorCounter == 0) {
184                global $current_user;
185                $email = "none@none.none";
186                if (isset($postData['email_id']) && !empty($postData['email_id']) && filter_var($postData['email_id'], FILTER_VALIDATE_EMAIL)) {
187                    $email = $postData['email_id'];
188                }
189                $domain = site_url();
190                $user_name = $current_user->first_name . " " . $current_user->last_name;
191                $subject = "Poptin was removed from {$domain}";
192                $headers = "MIME-Version: 1.0\r\n";
193                $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
194                $headers .= 'From: ' . $user_name . ' <' . $email . '>' . PHP_EOL;
195                $headers .= 'Reply-To: ' . $user_name . ' <' . $email . '>' . PHP_EOL;
196                $headers .= 'X-Mailer: PHP/' . phpversion();
197                ob_start();
198?>
199                <table border="0" cellspacing="0" cellpadding="5">
200                    <tr>
201                        <th>Plugin</th>
202                        <td>Poptin</td>
203                    </tr>
204                    <tr>
205                        <th>Plugin Version</th>
206                        <td><?php esc_attr_e(POPTIN_VERSION) ?></td>
207                    </tr>
208                    <tr>
209                        <th>Domain</th>
210                        <td><?php echo esc_url($domain) ?></td>
211                    </tr>
212                    <tr>
213                        <th>Email</th>
214                        <td><?php esc_attr_e($email) ?></td>
215                    </tr>
216                    <tr>
217                        <th>Reason</th>
218                        <td><?php esc_attr_e(nl2br($reason)) ?></td>
219                    </tr>
220                    <tr>
221                        <th>WordPress Version</th>
222                        <td><?php esc_attr_e(get_bloginfo('version')) ?></td>
223                    </tr>
224                    <tr>
225                        <th>PHP Version</th>
226                        <td><?php esc_attr_e(PHP_VERSION) ?></td>
227                    </tr>
228                </table>
229<?php
230                $content = ob_get_clean();
231                $email_id = "contact@poptin.on.crisp.email";
232                wp_mail($email_id, $subject, $content, $headers);
233                $response['status'] = 1;
234            }
235            echo json_encode($response);
236            wp_die();
237        }
238    }
239
240    public function deactivate_modal()
241    {
242        if (current_user_can('manage_options')) {
243            global $pagenow;
244
245            if ('plugins.php' !== $pagenow) {
246                return;
247            }
248
249            include 'deactivate-form.php';
250        }
251    }
252
253    /**
254     *
255     *
256     * Scope:       Private
257     * Function:    poptin_marketplace_registration
258     * Description: Will be called via the Admin Page AJAX
259     *              Will be checked with WP nounce.
260     *              If the verification is OK, we go ahead and create the account.
261     *              Email Address - Would be pre-filled in the form in the Admin Page.
262     *              User wants, can change the email id.
263     *              We store the registration email ID, to ensure that we do not have issues in future.
264     *              Basic wrapper function to the poptin_middleware_registration_curl
265     *              Responds in JSON from the cURL call.
266     * Parameters:  email   (argument)
267     *              domain      -   retrieved from the Wordpress base.
268     *              first_name  -   derived from the nice_name of the existing logged in user.
269     *              last_name  -   derived from the nice_name of the existing logged in user.
270     * Return:      Return response is derived directly from the chain function poptin_middleware_registration_curl
271     *
272     *
273     */
274    function poptin_marketplace_registration()
275    {
276        if (!current_user_can('manage_options')) {
277            die('You are now allowed to do this.');
278        }
279
280        $email = sanitize_email($_POST['email']);
281        /**
282         *
283         * We check the sanitization here again for the email id.
284         * This is for AJAX call, hence the double check is required.
285         * If this is okay, we go ahead and send the data poptin_marketplace_registration function.
286         *
287         */
288        if ($email) {
289        } else {
290            $return_array = array();
291            $return_array['success'] = false;
292            $return_array['message'] = 'Invalid Email Address found.';
293            echo json_encode($return_array);
294            exit(0);
295        }
296
297        if (check_ajax_referer('poptin-fe-register', 'security')) {
298            $domain = site_url();
299            if (!function_exists('wp_get_current_user')) {
300                require_once(ABSPATH . "wp-includes/pluggable.php");
301            }
302            $user_data = wp_get_current_user();
303            $user_nice_name = $user_data->data->user_nicename;
304            $user_nice_name_array = explode(" ", $user_nice_name);
305            $first_name = $user_nice_name_array[0];
306            $last_name = "";
307            if (isset($user_nice_name_array[1])) {
308                $last_name = $user_nice_name_array[1];
309            } else {
310                $last_name = $user_nice_name_array[0];
311            }
312            $this->poptin_middleware_registration_curl($first_name, $last_name, $domain, $email);
313        } else {
314            $return_array = array();
315            $return_array['success'] = false;
316            $return_array['message'] = 'Invalid request. Please refresh & try again.';
317            echo json_encode($return_array);
318            exit(0);
319        }
320    }
321
322
323
324    /**
325     *
326     *
327     * Scope:       Private
328     * Function:    poptin_markplace_login_direct
329     * Description: Based on the Options stored in the WP database
330     *              We make request to the marketplace api for login
331     *              In simpler terms this is a wrapper function for making AJAX call -> to cURL call -> to respond back.
332     * Parameters:  POST Data NOT required
333     *              - token [Generated at the time of registration from the marketplace API ONLY]
334     *              - email [Email ID used at the time of registration from the marketplace API ONLY]
335     * Return:      login_url   -   if it went successful
336     *              success     -   true/false
337     *
338     *
339     */
340    function poptin_markplace_login_direct($after_registration = '')
341    {
342        $token = POPTIN_MARKETPLACE_TOKEN;
343        $user_id = POPTIN_USER_ID;
344        $curl_URL = POPTIN_MARKETPLACE_LOGIN_URL;
345        $curl_post_array = array(
346            'token' => $token,
347            'user_id' => $user_id
348        );
349        $curl_options = $this->generate_curl_options($curl_URL, $curl_post_array);
350        $curl = curl_init();
351        curl_setopt_array($curl, $curl_options);
352        $response = curl_exec($curl);
353        $err = curl_error($curl);
354        curl_close($curl);
355        $response_return_array = array();
356        if ($err) {
357            $response_return_array['success'] = false;
358            echo json_encode($response_return_array);
359            exit(0);
360        } else {
361            $response_array = json_decode($response);
362            if ($response_array->success) {
363                $login_url = $response_array->login_url;
364                // If user just registered
365                if ($after_registration != '') {
366                    $login_url .= '&utm_source=wordpress';
367                }
368                header("Location: " . $login_url);
369                exit(0);
370            } else {
371                exit(wp_redirect("admin.php?page=Poptin"));
372            }
373        }
374        exit(wp_redirect("admin.php?page=Poptin"));
375    }
376
377
378    /**
379     * Scope:       Public
380     * Function:    poptin_add_admin_javascript
381     * Description: Will be called via the Admin Page AJAX
382     *              Will be checked with WP nounce.
383     *              If the verification is OK, we go ahead and create the account.
384     *              Email Address - Would be pre-filled in the form in the Admin Page.
385     *              User wants, can change the email id.
386     *              We store the registration email ID, to ensure that we do not have issues in future.
387     *              Basic wrapper function to the poptin_middleware_registration_curl
388     *              Responds in JSON from the cURL call.
389     * Parameters:  email   (argument)
390     *              domain      -   retrieved from the Wordpress base.
391     *              first_name  -   derived from the nice_name of the existing logged in user.
392     *              last_name  -   derived from the nice_name of the existing logged in user.
393     * Return:      Return response is derived directly from the chain function poptin_middleware_registration_curl
394     */
395    public function poptin_add_admin_javascript($hook)
396    {
397        if ('toplevel_page_Poptin' === $hook) {
398            wp_enqueue_script('jquery');
399            wp_register_script('poptin-admin', plugins_url('assets/js/poptin-admin.js', __FILE__), array('jquery'), '1.0.6', true);
400            wp_enqueue_script('poptin-admin');
401            wp_register_script('bootstrap-modal', plugins_url('assets/js/bootstrap.min.js', __FILE__), array('jquery'), '1.0', true);
402            wp_enqueue_script('bootstrap-modal');
403        }
404    }
405
406
407
408    /**
409     * Scope:       Public
410     * Function:    poptin_add_script_frontend
411     * Description: Will add the Poptin JS code to the system.
412     * Parameters:  None
413     */
414    public function poptin_add_script_frontend()
415    {
416        echo "<script id='pixel-script-poptin' src='https://cdn.popt.in/pixel.js?id=" . POPTIN_ID . "' async='true'></script> ";
417    }
418
419    /**
420     * Scope:       Public
421     * Function:    poptin_add_admin_css
422     * Description: Will add the backend CSS required for the display of poptin settings page.
423     * Parameters:  hook | Not used.
424     */
425    public function poptin_add_admin_css($hook)
426    {
427        if ('toplevel_page_Poptin' === $hook) {
428            wp_register_style('poptin-admin', plugins_url('assets/css/poptin-admin.css', __FILE__), array(), '1.1');
429            wp_enqueue_style('poptin-admin');
430            wp_register_style('bootstrap-modal-css', plugins_url('assets/css/bootstrap.min.css', __FILE__), array(), '1.0');
431            wp_enqueue_style('bootstrap-modal-css');
432        }
433    }
434
435    /**
436     * Scope:       Public
437     * Function:    poptin_admin_pages_callback
438     * Description: Will add the Poptin Page into the Menu System of Wordpress
439     * Parameters:  None
440     */
441    public function poptin_admin_pages_callback()
442    {
443        //$this->check_if_poptin_is_connected();
444        add_menu_page(__("Poptin", 'ppbase'), __("Poptin", 'ppbase'), 'manage_options', 'Poptin', array($this, 'poptin_admin_view'), POPTIN_URL . '/assets/images/menu-icon.png');
445    }
446
447
448    /**
449     * Scope:       Public
450     * Function:    poptin_admin_view
451     * Description: The URL link is added to render the view setup as per the function
452     * Parameters:  None
453     */
454    public function poptin_admin_view()
455    {
456        include_once(POPTIN_PATH . '/views/poptin_admin_view.php');
457    }
458
459
460    /**
461     * Scope:       Public
462     * Function:    poptin_add_textdomain
463     * Description: --
464     * Parameters:  None
465     */
466    public function poptin_add_textdomain()
467    {
468        load_plugin_textdomain('poptin', false, dirname(plugin_basename(__FILE__)) . '/lang/');
469    }
470
471
472
473    /**
474     * Scope:       Public
475     * Function:    delete_poptin_id
476     * Description: AJAX wrapper for removing Poptin ID
477     * Parameters:  None
478     */
479    function delete_poptin_id()
480    {
481        if (!current_user_can('manage_options')) {
482            die('You now allowed to do this.');
483        }
484
485
486        if (!isset($_POST['data']['nonce']) || !wp_verify_nonce($_POST['data']['nonce'], "ppFormIdDeactivate")) {
487            die('You now allowed to do this.');
488        }
489
490        update_option('poptin_id', '');
491        update_option('poptin_marketplace_token', '');
492        update_option('poptin_marketplace_email_id', '');
493        update_option('poptin_user_id', '');
494        poptin_clear_all_caches();
495        die(json_encode(
496            array(
497                'success' => true,
498                'message' => 'Database updated successfully.'
499            )
500        ));
501    }
502
503    /**
504     * Scope:       Public
505     * Function:    add_poptin_id
506     * Description: AJAX wrapper for adding Poptin ID, only used when user enters the Poptin ID manually.
507     * Parameters:  None
508     */
509    function add_poptin_id()
510    {
511        if (!current_user_can('manage_options')) {
512            die('You now allowed to do this.');
513        }
514        if (!isset($_POST['data']['nonce']) || !wp_verify_nonce($_POST['data']['nonce'], "ppFormIdRegister")) {
515            die('You now allowed to do this.');
516        }
517
518        if (isset($_POST['data']) && isset($_POST['data']['poptin_id'])) {
519            $id = $_POST['data']['poptin_id'];
520            update_option('poptin_id', $id);
521            poptin_clear_all_caches();
522            die(json_encode(
523                array(
524                    'success' => true,
525                    'message' => 'Database updated successfully.'
526                )
527            ));
528        } else {
529            die(json_encode(
530                array(
531                    'success' => false,
532                    'message' => 'Wrong id.'
533                )
534            ));
535        }
536    }
537
538
539    /**
540     *
541     *
542     * Scope:       Private
543     * Function:    poptin_middleware_registration_curl
544     * Description:
545     * Arguments:
546     * Return:      JSON Response
547     *
548     *
549     **/
550    private function poptin_middleware_registration_curl($first_name, $last_name, $domain, $email)
551    {
552        /*
553         * Because Wordpress doesn't provide one
554         * We will rely on the Middleware's Country Code for this one
555         * Overriding Country Code
556        */
557        /*
558         * Not sending First + Last Name as per Gal Dubinski
559         * first_name => $first_name,
560            last_name => $last_name,
561         */
562        $curl_URL = POPTIN_MARKETPLACE_REGISTER_URL;
563        $curl_post_array = array(
564            'domain' => $domain,
565            'marketplace' => POPTIN_MARKETPLACE,
566            'email' => $email
567        );
568
569        $curl_options = $this->generate_curl_options($curl_URL, $curl_post_array);
570        $curl = curl_init();
571        curl_setopt_array($curl, $curl_options);
572        $response = curl_exec($curl);
573        $err = curl_error($curl);
574        curl_close($curl);
575        $response_return_array = array();
576        if ($err) {
577            $response_return_array['success'] = false;
578            $response_return_array['message'] = "Internal error occurred. Please try again later.";
579            echo json_encode($response_return_array);
580            exit(0);
581        } else {
582            $response_array = json_decode($response);
583            if ($response_array->success) {
584                $response_return_array['success'] = true;
585                $response_return_array['message'] = "Registration successful";
586                $response_return_array['js_client_id'] = $response_array->client_id;
587                $response_return_array['user_token'] = $response_array->token;
588
589
590                /**
591                 * On Success
592                 * We setup the update options
593                 */
594                update_option("poptin_id", $response_array->client_id);
595                update_option("poptin_user_id", $response_array->user_id);
596                update_option("poptin_marketplace_token", $response_array->token);
597                update_option("poptin_marketplace_email_id", $email);
598                poptin_clear_all_caches();
599                echo json_encode($response_return_array);
600                exit(0);
601            } else {
602                $response_return_array['success'] = false;
603                $response_return_array['message'] = $response_array->message;
604                echo json_encode($response_return_array);
605                exit(0);
606            }
607        }
608    }
609
610    /**
611     * Scope:       Private
612     * Function:    generate_curl_options
613     * Description: This is Utility Function generates the POST cURL calls options.
614     *              Have placed a function to ensure it remains generic and updates do not require many changes.
615     *              Uses the CA Cert certificate.
616     * Return:      Array | Options Array for cURL Post method call.
617     **/
618    private function generate_curl_options($curl_URL, $curl_post_array)
619    {
620        $curl_options_array = array(
621            CURLOPT_URL => $curl_URL,
622            CURLOPT_RETURNTRANSFER => true,
623            CURLOPT_ENCODING => "",
624            CURLOPT_MAXREDIRS => 20,
625            CURLOPT_TIMEOUT => 120,
626            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
627            CURLOPT_CUSTOMREQUEST => "POST",
628            CURLOPT_POSTFIELDS => $curl_post_array,
629            CURLOPT_CAINFO => POPTIN_CACERT_PATH
630        );
631        return $curl_options_array;
632    }
633}
634
635/**
636 * Scope:       Public
637 * Function:    poptin_shortcode
638 * Description: Converts the content value to poptin div.
639 *              [poptin-embed 123456789] => <div class='poptin-123456789'></div>
640 * Return:      Echo of generated <div></div>
641 **/
642function poptin_shortcode($arguments)
643{
644    $poptin_div_id = $arguments[0];
645    echo "<div class='poptin-{$poptin_div_id}'></div>";
646}
647// add_shortcode('poptin-embed', 'poptin_shortcode');
648
649/**
650 * Scope:       Public
651 * Function:    poptin_shortcode_form
652 * Description: Converts the content value to poptin form.
653 *              [poptin-form 123456789] => <div class="poptin-embedded" data-id="123456789"></div>
654 * Return:      Echo of generated <div></div>
655 **/
656function poptin_shortcode_form($arguments)
657{
658    $poptin_div_id = $arguments[0];
659    echo "<div class='poptin-embedded' data-id='{$poptin_div_id}'></div>";
660}
661add_shortcode('poptin-form', 'poptin_shortcode_form');
662
663/**
664 * Scope:       Public
665 * Function:    async_scripts
666 * Description: Async URL for changes from asyncload to async='async'
667 * Return:      Async URL
668 **/
669function async_scripts($url)
670{
671    if (strpos($url, '#asyncload') === false)
672        return $url;
673    else if (is_admin())
674        return str_replace('#asyncload', '', $url);
675    else
676        return str_replace('#asyncload', '', $url) . "' async='async";
677}
678
679/**
680 * Scope:       Public
681 * Function:    poptin_plugin_redirect
682 * Description: Called via admin_init action in Constructor
683 *              Will redirect to the plugin page if the poptin_plugin_redirection is setup.
684 *              Once redirection is pushed, the key is removed.
685 * Return:      void
686 **/
687function poptin_plugin_redirect()
688{
689    if (get_option('poptin_plugin_redirection', false)) {
690        delete_option('poptin_plugin_redirection');
691        exit(wp_redirect("admin.php?page=Poptin"));
692    }
693}
694
695
696/**
697 * Scope:       Public
698 * Function:    poptin_activation_hook
699 * Description: On installation of the plugin this will be called.
700 *              We want to setup/update poptin related options at this time.
701 *              Options to change to blank
702 *                  - poptin_id                     [Poptin ID used for JS injection]
703 *                  - poptin_marketplace_token      [Poptin Marketplace Token | Generated at the time of registration from the plugin ONLY]
704 *                  - poptin_marketplace_email_id   [Poptin Marketplace Email ID | used at the time of registration from the plugin ONLY]
705 * Return:      void
706 **/
707function poptin_activation_hook()
708{
709    /**
710     * We want these three options to be blank on activation/installation of the plugin
711     */
712    add_option('poptin_id', '', '', 'yes');
713    add_option("poptin_marketplace_token", "", '', 'yes');
714    add_option("poptin_marketplace_email_id", "", '', 'yes');
715    add_option("poptin_user_id", "", '', 'yes');
716
717    /**
718     * We want to take the user to the Plugin Page on installation.
719     * Hence setting up a temporary redirection key.
720     * It gets removed as soon as it's called for the first time.
721     * Ussage at : poptin_plugin_redirect, and called with admin_init
722     */
723    add_option('poptin_plugin_redirection', true);
724}
725
726
727/**
728 * Scope:       Public
729 * Function:    poptin_decativation_hook
730 * Description: On deactivation of the plugin this will be called.
731 *              We want to delete poptin related options at this time.
732 *              Options to change to remove
733 *                  - poptin_id                     [Poptin ID used for JS injection]
734 *                  - poptin_marketplace_token      [Poptin Marketplace Token | Generated at the time of registration from the plugin ONLY]
735 *                  - poptin_marketplace_email_id   [Poptin Marketplace Email ID | used at the time of registration from the plugin ONLY]
736 * Return:      void
737 **/
738function poptin_deactivation_hook()
739{
740    delete_option('poptin_id');
741    delete_option('poptin_marketplace_token');
742    delete_option('poptin_marketplace_email_id');
743    delete_option('poptin_user_id');
744
745    /**
746     * If At all this was not removed already
747     */
748    delete_option('poptin_plugin_redirection');
749    poptin_clear_all_caches();
750}
751
752/**
753 * Scope:       Public
754 * Function:    poptin_clear_all_caches
755 * Description: Clears caches from below plugins if any
756 *              - W3Total Cache
757 *              - SuperCache
758 *              - WPFastestCache
759 *              - WP Engine
760 * Return:      void
761 **/
762function poptin_clear_all_caches()
763{
764    try {
765        global $wp_fastest_cache;
766        // if W3 Total Cache is being used, clear the cache
767        if (function_exists('w3tc_flush_all')) {
768            w3tc_flush_all();
769        }
770        /* if WP Super Cache is being used, clear the cache */
771        if (function_exists('wp_cache_clean_cache')) {
772            global $file_prefix, $supercachedir;
773            if (empty($supercachedir) && function_exists('get_supercache_dir')) {
774                $supercachedir = get_supercache_dir();
775            }
776            wp_cache_clean_cache($file_prefix);
777        }
778
779        if (class_exists('WpeCommon')) {
780            //be extra careful, just in case 3rd party changes things on us
781            if (method_exists('WpeCommon', 'purge_memcached')) {
782                //WpeCommon::purge_memcached();
783            }
784            if (method_exists('WpeCommon', 'clear_maxcdn_cache')) {
785                //WpeCommon::clear_maxcdn_cache();
786            }
787            if (method_exists('WpeCommon', 'purge_varnish_cache')) {
788                //WpeCommon::purge_varnish_cache();
789            }
790        }
791
792        if (method_exists('WpFastestCache', 'deleteCache') && !empty($wp_fastest_cache)) {
793            $wp_fastest_cache->deleteCache();
794        }
795        if (function_exists('rocket_clean_domain')) {
796            rocket_clean_domain();
797            // Preload cache.
798            if (function_exists('run_rocket_sitemap_preload')) {
799                run_rocket_sitemap_preload();
800            }
801        }
802
803        if (class_exists("autoptimizeCache") && method_exists("autoptimizeCache", "clearall")) {
804            autoptimizeCache::clearall();
805        }
806
807        if (class_exists("LiteSpeed_Cache_API") && method_exists("autoptimizeCache", "purge_all")) {
808            LiteSpeed_Cache_API::purge_all();
809        }
810
811        if (class_exists('\Hummingbird\Core\Utils')) {
812
813            $modules   = \Hummingbird\Core\Utils::get_active_cache_modules();
814            foreach ($modules as $module => $name) {
815                $mod = \Hummingbird\Core\Utils::get_module($module);
816
817                if ($mod->is_active()) {
818                    if ('minify' === $module) {
819                        $mod->clear_files();
820                    } else {
821                        $mod->clear_cache();
822                    }
823                }
824            }
825        }
826    } catch (Exception $e) {
827        return 1;
828    }
829}
830$poptinBase = new POPTIN_Plugin_Base();
831?>
Note: See TracBrowser for help on using the repository browser.