LLMS_Helper_Keys

LLMS_Helper_Keys


Source Source

File: libraries/lifterlms-helper/includes/class-llms-helper-keys.php

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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
class LLMS_Helper_Keys {
 
    /**
     * Activate LifterLMS License Keys with the remote server.
     *
     * @since 3.0.0
     * @since 3.0.1 Unknown.
     * @since 3.4.2 Removed empty key lines.
     * @since 3.5.0 Caching results. Added `$force` parameter.
     *
     * @param string|array $keys  Array or a white-space separated list of API keys.
     * @param bool         $force Optional. Whether to force a remote check. Default `false`.
     * @return array
     */
    public static function activate_keys( $keys, $force = false ) {
 
        // Sanitize before sending.
        if ( ! is_array( $keys ) ) {
            $keys = explode( PHP_EOL, $keys );
        }
 
        $keys = array_map( 'sanitize_text_field', $keys );
        $keys = array_map( 'trim', $keys );
        $keys = array_unique( $keys );
        $keys = array_filter( $keys ); // Remove empty keys.
 
        $data = array(
            'keys' => $keys,
            'url'  => get_site_url(),
        );
 
        // Check for a cached result based on the keys and url input.
        $cache_hash = md5( wp_json_encode( $data ) );
        if ( $force ) {
            // Delete cache if forcing a remote check.
            delete_site_transient( 'llms_helper_keys_activation_response_' . $cache_hash );
        } else {
            // Use the cached result if present.
            $cached_req_result = get_site_transient( 'llms_helper_keys_activation_response_' . $cache_hash );
            if ( ! empty( $cached_req_result ) ) {
                return $cached_req_result;
            }
        }
 
        $req = new LLMS_Dot_Com_API( '/license/activate', $data );
        set_site_transient( 'llms_helper_keys_activation_response_' . $cache_hash, $req->get_result(), HOUR_IN_SECONDS );
 
        return $req->get_result();
 
    }
 
    /**
     * Add a single license key
     *
     * @since 3.0.0
     *
     * @param string $activation_data  Array of activation details from api call.
     * @return boolean True if option value has changed, false if not or if update failed.
     */
    public static function add_license_key( $activation_data ) {
 
        $keys                                    = llms_helper_options()->get_license_keys();
        $keys[ $activation_data['license_key'] ] = array(
            'product_id'  => $activation_data['id'],
            'status'      => 1,
            'license_key' => $activation_data['license_key'],
            'update_key'  => $activation_data['update_key'],
            'addons'      => $activation_data['addons'],
        );
 
        return llms_helper_options()->set_license_keys( $keys );
 
    }
 
    /**
     * Check all saved keys to ensure they're still active
     *
     * Outputs warnings if the key has expired or the status has changed remotely.
     *
     * Runs on daily cron (`llms_check_license_keys`).
     *
     * Only make api calls to check once / week.
     *
     * @since 3.0.0
     * @since 3.4.0 Use core textdomain.
     *
     * @param bool $force Ignore the once/week setting and force a check.
     * @return void
     */
    public static function check_keys( $force = false ) {
 
        // Don't trigger during AJAX Requests.
        if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
            return;
        }
 
        // Don't proceed if we don't have any keys to check.
        $keys = llms_helper_options()->get_license_keys();
        if ( ! $keys ) {
            return;
        }
 
        if ( ! $force ) {
            // Only check keys once a week.
            $last_send = llms_helper_options()->get_last_keys_cron_check();
            if ( $last_send > apply_filters( 'llms_check_license_keys_interval', strtotime( '-1 week' ) ) ) {
                return;
            }
        }
 
        // Record check time.
        llms_helper_options()->set_last_keys_cron_check( time() );
 
        $data = array(
            'keys' => array(),
            'url'  => get_site_url(),
        );
 
        foreach ( $keys as $key ) {
            $data['keys'][ $key['license_key'] ] = $key['update_key'];
        }
 
        $req = new LLMS_Dot_Com_API( '/license/status', $data );
        if ( ! $req->is_error() ) {
 
            $res = $req->get_result();
            include_once LLMS_PLUGIN_DIR . 'includes/admin/class.llms.admin.notices.php';
 
            /* Translators: %s = License Key */
            $msg = __( 'The license "%s" is no longer valid and was deactivated. Please visit your account dashboard at https://lifterlms.com/my-account for more information.', 'lifterlms' );
 
            // Output error responses.
            if ( isset( $res['data']['errors'] ) ) {
                foreach ( array_keys( $res['data']['errors'] ) as $key ) {
                    self::remove_license_key( $key );
                    LLMS_Admin_Notices::add_notice(
                        'key_check_' . sanitize_text_field( $key ),
                        make_clickable( sprintf( $msg, $key ) ),
                        array(
                            'type'             => 'error',
                            'dismiss_for_days' => 0,
                        )
                    );
                }
            }
 
            // Check status of keys, if the status has changed remove it locally.
            if ( isset( $res['data']['keys'] ) ) {
                foreach ( $res['data']['keys'] as $key => $data ) {
 
                    if ( $data['status'] ) {
                        continue;
                    }
 
                    self::remove_license_key( $key );
                    LLMS_Admin_Notices::add_notice(
                        'key_check_' . sanitize_text_field( $key ),
                        make_clickable( sprintf( $msg, $key ) ),
                        array(
                            'type'             => 'error',
                            'dismiss_for_days' => 0,
                        )
                    );
 
                }
            }
        }
    }
 
    /**
     * Deactivate LifterLMS API keys with remote server
     *
     * @since 3.0.0
     * @since 3.4.1 Ensure key exists before attempting to deactivate it.
     * @since 3.5.0 Deleting any cached activation result.
     *
     * @param array $keys Array of keys.
     * @return array
     */
    public static function deactivate_keys( $keys ) {
 
        $keys = array_map( 'sanitize_text_field', $keys );
        $keys = array_map( 'trim', $keys );
 
        $data = array(
            'keys' => array(),
            'url'  => get_site_url(),
        );
 
        // Delete any cached activation result.
        $cache_hash = md5( wp_json_encode( $data ) );
        delete_site_transient( 'llms_helper_keys_activation_response_' . $cache_hash );
 
        $saved = llms_helper_options()->get_license_keys();
        foreach ( $keys as $key ) {
            if ( isset( $saved[ $key ] ) && $saved[ $key ]['update_key'] ) {
                $data['keys'][ $key ] = $saved[ $key ]['update_key'];
            }
        }
 
        $req = new LLMS_Dot_Com_API( '/license/deactivate', $data );
        return $req->get_result();
 
    }
 
    /**
     * Retrieve stored information about a key by the license key
     *
     * @since 3.3.1
     *
     * @param string $key License key.
     * @return array|false Associative array of license key information. Returns `false` if the provided license key was not found.
     */
    public static function get( $key ) {

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.