array( 'key' => $key, 'domain' => wp_parse_url( site_url(), PHP_URL_HOST ), ), ); $response = wp_remote_post( self::REMOTE_LICENSES, $args ); if ( is_wp_error( $response ) ) { wp_send_json_error( __( 'Remote request error: ', 'sharing-image' ) . $response->get_error_message(), 400 ); } $answer = json_decode( $response['body'], true ); if ( ! isset( $answer['success'] ) ) { wp_send_json_error( __( 'Invalid response received from the verification server.', 'sharing-image' ), 400 ); } wp_unschedule_hook( self::EVENT_PREMIUM ); if ( true === $answer['success'] ) { $license = self::update_license( true, $key ); self::schedule_verification(); wp_send_json_success( $license ); } $error = array( 'success' => false, 'data' => __( 'Verification unsuccessful.', 'sharing-image' ), ); if ( isset( $answer['result'] ) ) { $error['code'] = $answer['result']; } self::update_license( false, $key ); wp_send_json( $error, 403 ); } /** * Revoke Premium access from AJAX request. */ public static function revoke_premium_access() { $check = check_ajax_referer( Settings::SETTINGS_NONCE, 'sharing_image_nonce', false ); if ( false === $check ) { wp_send_json_error( __( 'Invalid security token. Please reload the page and try again.', 'sharing-image' ), 403 ); } // Remove license verification event. wp_unschedule_hook( self::EVENT_PREMIUM ); // Disable Premium license. $license = self::update_license( false ); wp_send_json_success( $license ); } /** * Get plugin license settings. * * @return array List of plugin license settings. */ public static function get_license() { $license = get_option( self::OPTION_LICENSE, array() ); /** * Check if the plugin in development mode. * * @param bool Current development state. Disabled by default. */ $develop = apply_filters( 'sharing_image_develop', false ); if ( $develop ) { $license['develop'] = true; } /** * Filters license settings. * * @param array List of plugin license settings. */ return apply_filters( 'sharing_image_get_license', $license ); } /** * Set license options. * * @param bool $premium Premium status. * @param string $key License key. * @param string $error Verification error code. * @return array License options. */ public static function update_license( $premium, $key = '', $error = '' ) { $license = get_option( self::OPTION_LICENSE, array() ); if ( ! empty( $key ) ) { $license['key'] = $key; } unset( $license['error'] ); if ( ! empty( $error ) ) { $license['error'] = $error; } $license['premium'] = $premium; // Save updated license settings in database. update_option( self::OPTION_LICENSE, $license ); return $license; } /** * Launch scheduled license verification event. * Do not disable Premium if the verification server does not respond. * * @param string|null $key Optional. Legacy license key. */ public static function launch_verification_event( $key = null ) { if ( ! is_null( $key ) ) { self::reschedule_verification(); } $license = get_option( self::OPTION_LICENSE, array() ); if ( empty( $license['key'] ) ) { return self::update_license( false, '' ); } $args = array( 'body' => array( 'key' => $license['key'], 'domain' => wp_parse_url( site_url(), PHP_URL_HOST ), ), ); $response = wp_remote_post( self::REMOTE_LICENSES, $args ); if ( is_wp_error( $response ) ) { return; } $answer = json_decode( $response['body'], true ); if ( ! isset( $answer['success'] ) ) { return; } if ( true === $answer['success'] ) { return self::update_license( true, $license['key'] ); } if ( ! isset( $answer['result'] ) ) { return self::update_license( false, $license['key'] ); } self::update_license( false, $license['key'], $answer['result'] ); } /** * Check if Premium features availible. * * @return bool Whether premium features are enabled. */ public static function is_premium_features() { $license = self::get_license(); if ( ! empty( $license['premium'] ) || ! empty( $license['develop'] ) ) { return true; } return false; } /** * Reschedule license verification with current Premium key. */ public static function check_verification_event() { $license = self::get_license(); if ( empty( $license['premium'] ) ) { return; } self::schedule_verification(); } /** * Schedule license verification. */ public static function schedule_verification() { if ( wp_next_scheduled( self::EVENT_PREMIUM ) ) { return; } wp_schedule_event( time() + DAY_IN_SECONDS / 2, 'twicedaily', self::EVENT_PREMIUM ); } /** * Delete all events with the same name and schedule again. */ public static function reschedule_verification() { wp_unschedule_hook( self::EVENT_PREMIUM ); self::schedule_verification(); } }