summaryrefslogtreecommitdiff
path: root/Postman/Extensions/License/PostmanLicenseManager.php
blob: a25f092370a416fa0bcd87f18eeae7a8d8acfeaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
if ( ! defined( 'ABSPATH' ) ) exit;

class PostmanLicenseManager {

    const ENDPOINT = 'https://postmansmtpcom-staging.dxpsites.net';

    const CORE_EXTENSIONS = [ 'gmail_api', 'sendgrid_api', 'mandrill_api', 'mailgun_api' ];

    private $extensions;

    private $rand_cache_interval = 12;

    private static $instance;

    public static function get_instance() {
        if ( ! self::$instance ) {
            self::$instance = new static();
        }

        return self::$instance;
    }

    /**
     * PostmanLicenseManager constructor.
     */
    private function __construct()
    {
        $this->includes();
        $this->rand_cache_interval = rand( 1, 24 );

        add_filter( 'extra_plugin_headers', [ $this, 'add_extension_headers' ] );
    }

    public function includes() {
        include_once 'PostmanLicenseHandler.php';

        include_once ABSPATH . '/wp-admin/includes/plugin.php';

    }


    function add_extension_headers($headers) {
        $headers[] = 'Class';
        $headers[] = 'Slug';

        return $headers;
    }

    /**
     * Init
     */
    public function init() {

        $plugins = get_plugins();
        foreach ( $plugins as $plugin_dir_and_filename => $plugin_data ) {

            if ( ! is_plugin_active( $plugin_dir_and_filename ) ) {
                continue;
            }

            if ( false !== strpos( $plugin_dir_and_filename, 'post-smtp-extension' ) ) {
                $slug = $plugin_dir_and_filename;
                $class = $plugin_data['Class'];
                $plugin_path = WP_CONTENT_DIR . '/plugins/' . $plugin_dir_and_filename;

                $this->extensions[$slug]['plugin_data'] = $plugin_data;
                $this->extensions[$slug]['plugin_dir_and_filename'] = $plugin_dir_and_filename;
                $this->extensions[$slug]['license_manager'] = new PostmanLicenseHandler(
                    $plugin_path, $plugin_data['Name'],
                    $plugin_data['Version'], $plugin_data['Author'], null, self::ENDPOINT
                );
                if ( $this->extensions[$slug]['license_manager']->is_licensed() ) {
                    $this->extensions[$slug]['instance'] = new $class;
                }
            }
        }

        if ( ! empty( $this->extensions ) ) {
            new PostmanAdmin();
        }
    }

    public function add_extension($slug) {
        $plugin_path = WP_CONTENT_DIR . '/plugins/' . $this->extensions[$slug]['plugin_dir_and_filename'];
        $class = $this->extensions[$slug]['plugin_data']['Class'];

        include_once $plugin_path;
        $this->extensions[$slug]['instance'] = new $class;
    }

    public function remove_extension($slug) {
        $this->extensions[$slug]['instance'] = null;
        unset($this->extensions[$slug]['instance']);
    }

    public function get_extensions() {
        return $this->extensions;
    }
}