PageRenderTime 36ms CodeModel.GetById 13ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/wp-content/plugins/google-analytics-for-wordpress/includes/admin/licensing/license-actions.php

https://bitbucket.org/carloskikea/helpet
PHP | 371 lines | 191 code | 59 blank | 121 comment | 39 complexity | 80b7494497542a563b0eeb282ad2106b MD5 | raw file
  1<?php
  2/**
  3 * License Actions class.
  4 *
  5 * @since 6.0.0
  6 *
  7 * @package MonsterInsights
  8 * @author  Chris Christoff
  9 */
 10
 11// Exit if accessed directly
 12if ( ! defined( 'ABSPATH' ) ) {
 13    exit;
 14}
 15
 16final class MonsterInsights_License_Actions {
 17    /**
 18     * Holds the license key.
 19     *
 20     * @since 6.0.0
 21     *
 22     * @var string
 23     */
 24    public $key;
 25
 26    /**
 27     * Holds any license error messages.
 28     *
 29     * @since 6.0.0
 30     *
 31     * @var array
 32     */
 33    public $errors = array();
 34
 35    /**
 36     * Holds any license success messages.
 37     *
 38     * @since 6.0.0
 39     *
 40     * @var array
 41     */
 42    public $success = array();
 43
 44    /**
 45     * Primary class constructor.
 46     *
 47     * @since 6.0.0
 48     */
 49    public function __construct() {
 50        add_action( 'admin_init', array( $this, 'admin_init' ) );
 51    }
 52    
 53    public function admin_init() {
 54        // Possibly verify the key.
 55        $this->maybe_verify_key();
 56
 57        // Add potential admin notices for actions around the admin.
 58        add_action( 'admin_notices', array( $this, 'monsterinsights_notices' ), 11 );
 59        add_action( 'network_admin_notices', array( $this, 'monsterinsights_notices' ), 11 );
 60
 61        // Grab the license key. If it is not set (even after verification), return early.
 62        $this->key = is_network_admin() ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key();
 63        if ( ! $this->key ) {
 64            return;
 65        }
 66
 67        // Possibly handle validating, deactivating and refreshing license keys.
 68        $this->maybe_validate_key();
 69        $this->maybe_deactivate_key();
 70        $this->maybe_refresh_key();
 71    }
 72
 73    /**
 74     * Maybe verifies a license key entered by the user.
 75     *
 76     * @since 6.0.0
 77     *
 78     * @return null Return early if the key fails to be verified.
 79     */
 80    public function maybe_verify_key() {
 81
 82        if ( empty( $_POST['monsterinsights-license-key'] ) || strlen( $_POST['monsterinsights-license-key'] ) < 10 || empty( $_POST['monsterinsights-verify-submit'] ) ) {
 83            return;
 84        }
 85
 86        if ( ! wp_verify_nonce( $_POST['monsterinsights-key-nonce'], 'monsterinsights-key-nonce' ) ) {
 87            return;
 88        }
 89
 90        if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
 91            return;
 92        }
 93
 94        $this->verify_key();
 95
 96    }
 97
 98    /**
 99     * Verifies a license key entered by the user.
100     *
101     * @since 6.0.0
102     */
103    public function verify_key() {
104
105        // Perform a request to verify the key.
106        $verify = $this->perform_remote_request( 'verify-key', array( 'tgm-updater-key' => trim( $_POST['monsterinsights-license-key'] ) ) );
107
108        // If it returns false, send back a generic error message and return.
109        if ( ! $verify ) {
110            $this->errors[] = esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' );
111            return;
112        }
113
114        // If an error is returned, set the error and return.
115        if ( ! empty( $verify->error ) ) {
116            $this->errors[] = $verify->error;
117            return;
118        }
119
120        // Otherwise, our request has been done successfully. Update the option and set the success message.
121        $option                = is_network_admin() ? MonsterInsights()->license->get_network_license() : MonsterInsights()->license->get_site_license();
122        $option['key']         = trim( $_POST['monsterinsights-license-key'] );
123        $option['type']        = isset( $verify->type ) ? $verify->type : $option['type'];
124        $option['is_expired']  = false;
125        $option['is_disabled'] = false;
126        $option['is_invalid']  = false;
127        $this->success[]       = isset( $verify->success ) ? $verify->success : esc_html__( 'Congratulations! This site is now receiving automatic updates.', 'google-analytics-for-wordpress' );
128        is_network_admin() ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option );
129        delete_transient( '_monsterinsights_addons' );
130        monsterinsights_get_addons_data( $option['key'] );
131    }
132
133    /**
134     * Maybe validates a license key entered by the user.
135     *
136     * @since 6.0.0
137     *
138     * @return null Return early if the transient has not expired yet.
139     */
140    public function maybe_validate_key() {
141        $check = is_network_admin() ? MonsterInsights()->license->time_to_check_network_license() : MonsterInsights()->license->time_to_check_site_license();
142        if ( $check ) {
143            $this->validate_key();
144        }
145    }
146
147    /**
148     * Validates a license key entered by the user.
149     *
150     * @since 6.0.0
151     *
152     * @param bool $forced Force to set contextual messages (false by default).
153     */
154    public function validate_key( $forced = false ) {
155
156        $validate = $this->perform_remote_request( 'validate-key', array( 'tgm-updater-key' => $this->key ) );
157
158        // If there was a basic API error in validation, only set the transient for 10 minutes before retrying.
159        if ( ! $validate ) {
160            // If forced, set contextual success message.
161            if ( $forced ) {
162                $this->errors[] = esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' );
163            }
164            return;
165        }
166
167        $option = is_network_admin() ? MonsterInsights()->license->get_network_license() : MonsterInsights()->license->get_site_license();
168        $option['is_expired']  = false;
169        $option['is_disabled'] = false;
170        $option['is_invalid']  = false;
171
172        // If a key or author error is returned, the license no longer exists or the user has been deleted, so reset license.
173        if ( isset( $validate->key ) || isset( $validate->author ) ) {
174            $option['is_invalid']  = true;
175            is_network_admin() ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option );
176            return;
177        }
178
179        // If the license has expired, set the transient and expired flag and return.
180        if ( isset( $validate->expired ) ) {
181            $option['is_expired']  = true;
182            is_network_admin() ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option );
183            return;
184        }
185
186        // If the license is disabled, set the transient and disabled flag and return.
187        if ( isset( $validate->disabled ) ) {
188            $option['is_disabled'] = true;
189            is_network_admin() ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option );
190            return;
191        }
192
193        // If forced, set contextual success message.
194        if ( ( ! empty( $validate->key ) || ! empty( $this->key ) ) && $forced ) {
195            $key = ! empty( $validate->key ) ? $validate->key : $this->key;
196            delete_transient( '_monsterinsights_addons' );
197            monsterinsights_get_addons_data( $key );
198            $this->success[] = esc_html__( 'Congratulations! Your key has been refreshed successfully.', 'google-analytics-for-wordpress' );
199        }
200
201        $option = array();
202        $option = is_network_admin() ? MonsterInsights()->license->get_network_license() : MonsterInsights()->license->get_site_license();
203        $option['type']        = isset( $validate->type ) ? $validate->type : $option['type'];
204        $option['is_expired']  = false;
205        $option['is_disabled'] = false;
206        $option['is_invalid']  = false;
207        is_network_admin() ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option );
208
209    }
210
211    /**
212     * Maybe deactivates a license key entered by the user.
213     *
214     * @since 6.0.0
215     *
216     * @return null Return early if the key fails to be deactivated.
217     */
218    public function maybe_deactivate_key() {
219
220        if ( empty( $_POST['monsterinsights-license-key'] ) || empty( $_POST['monsterinsights-deactivate-submit'] ) ) {
221            return;
222        }
223
224        if ( ! wp_verify_nonce( $_POST['monsterinsights-key-nonce'], 'monsterinsights-key-nonce' ) ) {
225            return;
226        }
227
228        if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
229            return;
230        }
231
232        $this->deactivate_key();
233
234    }
235
236    /**
237     * Deactivates a license key entered by the user.
238     *
239     * @since 6.0.0
240     */
241    public function deactivate_key() {
242
243        // Perform a request to deactivate the key.
244        $deactivate = $this->perform_remote_request( 'deactivate-key', array( 'tgm-updater-key' => $_POST['monsterinsights-license-key'] ) );
245
246        // If it returns false, send back a generic error message and return.
247        if ( ! $deactivate ) {
248            $this->errors[] = esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' );
249            return;
250        }
251
252        // If an error is returned, set the error and return.
253        if ( ! empty( $deactivate->error ) && ! monsterinsights_is_debug_mode() ) {
254            $this->errors[] = $deactivate->error;
255            return;
256        }
257
258        // Otherwise, our request has been done successfully. Reset the option and set the success message.
259        $this->success[] = isset( $deactivate->success ) ? $deactivate->success : esc_html__( 'Congratulations! You have deactivated the key from this site successfully.', 'google-analytics-for-wordpress' );
260        is_network_admin() ? MonsterInsights()->license->delete_network_license() : MonsterInsights()->license->delete_site_license();
261
262    }
263
264    /**
265     * Maybe refreshes a license key.
266     *
267     * @since 6.0.0
268     *
269     * @return null Return early if the key fails to be refreshed.
270     */
271    public function maybe_refresh_key() {
272
273        if ( empty( $_POST['monsterinsights-license-key'] ) || empty( $_POST['monsterinsights-refresh-submit'] ) ) {
274            return;
275        }
276
277        if ( ! wp_verify_nonce( $_POST['monsterinsights-key-nonce'], 'monsterinsights-key-nonce' ) ) {
278            return;
279        }
280
281        if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
282            return;
283        }
284
285        // Refreshing is simply a word alias for validating a key. Force true to set contextual messages.
286        $this->validate_key( true );
287
288    }
289
290    /**
291     * Outputs any notices generated by the class.
292     *
293     * @since 7.0.0
294     */
295    public function monsterinsights_notices() {
296        if ( ! monsterinsights_is_pro_version() ) {
297            return;
298        }
299
300        $screen = get_current_screen();
301        if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) {
302            return;
303        }
304
305        if ( ! empty( $this->errors ) ) { ?>
306            <div class="error">
307                <p><?php echo implode( '<br>', $this->errors ); ?></p>
308            </div>
309        <?php } else if ( ! empty( $this->success ) ) { ?>
310            <div class="updated">
311                <p><?php echo implode( '<br>', $this->success ); ?></p>
312            </div>
313        <?php }
314    }
315
316    /**
317     * Queries the remote URL via wp_remote_post and returns a json decoded response.
318     *
319     * @since 6.0.0
320     *
321     * @param string $action        The name of the $_POST action var.
322     * @param array $body           The content to retrieve from the remote URL.
323     * @param array $headers        The headers to send to the remote URL.
324     * @param string $return_format The format for returning content from the remote URL.
325     * @return string|bool          Json decoded response on success, false on failure.
326     */
327    public function perform_remote_request( $action, $body = array(), $headers = array(), $return_format = 'json' ) {
328
329        // Build the body of the request.
330        $body = wp_parse_args(
331            $body,
332            array(
333                'tgm-updater-action'     => $action,
334                'tgm-updater-key'        => $this->key,
335                'tgm-updater-wp-version' => get_bloginfo( 'version' ),
336                'tgm-updater-referer'    => site_url(),
337                'tgm-updater-mi-version' => MONSTERINSIGHTS_VERSION,
338                'tgm-updater-is-pro'     => monsterinsights_is_pro_version(),
339            )
340        );
341        $body = http_build_query( $body, '', '&' );
342
343        // Build the headers of the request.
344        $headers = wp_parse_args(
345            $headers,
346            array(
347                'Content-Type'   => 'application/x-www-form-urlencoded',
348                'Content-Length' => strlen( $body )
349            )
350        );
351
352        // Setup variable for wp_remote_post.
353        $post = array(
354            'headers' => $headers,
355            'body'    => $body
356        );
357
358        // Perform the query and retrieve the response.
359        $response      = wp_remote_post( monsterinsights_get_licensing_url(), $post );
360        $response_code = wp_remote_retrieve_response_code( $response );
361        $response_body = wp_remote_retrieve_body( $response );
362
363        // Bail out early if there are any errors.
364        if ( 200 != $response_code || is_wp_error( $response_body ) ) {
365            return false;
366        }
367
368        // Return the json decoded content.
369        return json_decode( $response_body );
370    }
371}