AP_License()

Description #

AnsPress license

Source #

File: admin/license.php

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
* @ignore
 */
class AP_License {
 
    /**
     * Initialize class.
     */
    public function __construct() {
        add_action( 'ap_admin_menu', array( $this, 'menu' ) );
        add_action( 'admin_init', array( $this, 'ap_plugin_updater' ), 0 );
    }
 
    /**
     * Show license menu if license field is registered.
     */
    public function menu() {
        $fields = ap_product_license_fields();
        if ( ! empty( $fields ) ) {
            $count = ' <span class="update-plugins count"><span class="plugin-count">' . number_format_i18n( count( $fields ) ) . '</span></span>';
            add_submenu_page( 'anspress', __( 'Licenses', 'anspress-question-answer' ), __( 'Licenses', 'anspress-question-answer' ) . $count, 'manage_options', 'anspress_licenses', array( $this, 'display_plugin_licenses' ) );
        }
    }
 
    /**
     * Display license page.
     */
    public function display_plugin_licenses() {
        include_once( 'views/licenses.php' );
    }
 
    /**
     * AnsPress license form.
     */
    public static function ap_product_license() {
        if ( ! current_user_can( 'manage_options' ) || ! ap_verify_nonce( 'ap_licenses_nonce' ) ) {
            return;
        }
 
        $licenses = get_option( 'anspress_license', array() );
        $fields = ap_product_license_fields();
 
        if ( empty( $fields ) ) {
            return;
        }
 
        if ( ap_isset_post_value( 'save_licenses' ) ) {
            foreach ( (array) $fields as $slug => $prod ) {
                $prod_license = ap_isset_post_value( 'ap_license_' . $slug, '' );
                if ( ! empty( $prod_license ) && ! isset( $licenses[ $slug ] ) || $prod_license !== $licenses[ $slug ]['key'] ) {
                    $licenses[ $slug ] = array(
                        'key'    => trim( ap_sanitize_unslash( 'ap_license_' . $slug, 'g', '' ) ),
                        'status' => false,
                    );
 
                    update_option( 'anspress_license', $licenses );
                }
            }
        }
 
        foreach ( (array) $fields as $slug => $prod ) {
 
            // Data to send in our API request.
            $api_params = array(
                'license'   => $licenses[ $slug ]['key'],
                'item_name' => rawurlencode( $prod['name'] ),
                'url'       => home_url(),
                'anspress_ver' => AP_VERSION,
            );
 
            // Check if activate is clicked.
            if ( ap_isset_post_value( 'ap_license_activate_' . $slug ) ) {
                $api_params['edd_action'] = 'activate_license';
 
                // Call the custom API.
                $response = wp_remote_post( 'https://anspress.io', array( 'timeout' => 15, 'sslverify' => true, 'body' => $api_params ) );
 
                // Make sure the response came back okay.
                if ( ! is_wp_error( $response ) ) {
                    // Decode the license data.
                    $license_data = json_decode( wp_remote_retrieve_body( $response ) );
 
                    $licenses[ $slug ]['status'] = sanitize_text_field( $license_data->license );
                    update_option( 'anspress_license', $licenses );
                }
            }
 
            // Check if deactivate is clicked.
            if ( ap_isset_post_value( 'ap_license_deactivate_' . $slug ) ) {
                $api_params['edd_action'] = 'deactivate_license';
 
                // Call the custom API.
                $response = wp_remote_post( 'https://anspress.io', array( 'timeout' => 15, 'sslverify' => true, 'body' => $api_params ) );
 
                // Make sure the response came back okay.
                if ( ! is_wp_error( $response ) ) {
                    // Decode the license data.
                    $license_data = json_decode( wp_remote_retrieve_body( $response ) );
                    $licenses[ $slug ]['status'] = sanitize_text_field( $license_data->license );
                    update_option( 'anspress_license', $licenses );
                }
            }
        }
    }
 
    /**
     * Initiate product updater.
     */
    public function ap_plugin_updater() {
        $fields = ap_product_license_fields();
        $licenses = get_option( 'anspress_license', array() );
 
        if ( ! empty( $fields ) ) {
            foreach ( $fields as $slug => $prod ) {
                if ( isset( $licenses[ $slug ] ) && ! empty( $licenses[ $slug ]['key'] ) ) {
                    new AnsPress_Prod_Updater( $prod['file'], array(
                            'version'   => ! empty( $prod['version'] ) ? $prod['version']: '',
                            'license'   => $licenses[ $slug ]['key'],
                            'item_name' => ! empty( $prod['name'] ) ? $prod['name']:       '',
                            'author'      => ! empty( $prod['author'] ) ? $prod['author']:   '',
                            'slug'      => $slug,
                        ),
                        isset( $prod['is_plugin'] ) ? $prod['is_plugin'] : true
                    );
                }
            }
        }
    }

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment