class-auxels-system-check.php
6 months ago
class-auxin-cli-commands.php
5 months ago
class-auxin-license-activation.php
6 months ago
class-auxin-notices.php
3 years ago
class-auxin-upgrader-ajax-handlers.php
1 year ago
class-auxin-upgrader-plugin.php
2 years ago
class-auxin-upgrader-prepare.php
3 years ago
class-auxin-upgrader-theme.php
4 years ago
class-auxin-license-activation.php
243 lines
| 1 | <?php |
| 2 | |
| 3 | // no direct access allowed |
| 4 | if ( ! defined('ABSPATH') ) { |
| 5 | die(); |
| 6 | } |
| 7 | |
| 8 | class Auxin_License_Activation { |
| 9 | |
| 10 | /** |
| 11 | * Instance of this class. |
| 12 | * |
| 13 | * @since 1.0.0 |
| 14 | * |
| 15 | * @var object |
| 16 | */ |
| 17 | protected static $instance = null; |
| 18 | |
| 19 | protected $api = 'https://env.averta.net/en/api/?branch=envato&group=items&cat=verify-purchase'; |
| 20 | protected $usermail = ''; |
| 21 | protected $purchase_code = ''; |
| 22 | protected $option_prefix = ''; |
| 23 | protected $action = 'activate'; |
| 24 | |
| 25 | |
| 26 | public function __construct(){ |
| 27 | $this->option_prefix = AUXELS_PURCHASE_KEY; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Return an instance of this class. |
| 32 | * |
| 33 | * @since 1.0.0 |
| 34 | * |
| 35 | * @return object A single instance of this class. |
| 36 | */ |
| 37 | public static function get_instance() { |
| 38 | |
| 39 | // If the single instance hasn't been set, set it now. |
| 40 | if ( null == self::$instance ) { |
| 41 | self::$instance = new self; |
| 42 | } |
| 43 | |
| 44 | return self::$instance; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * The result of license validation request |
| 49 | * |
| 50 | * @return string|array the server response |
| 51 | */ |
| 52 | private function get_license_result() { |
| 53 | |
| 54 | if( empty( $this->usermail ) || empty( $this->purchase_code ) ) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | global $wp_version; |
| 59 | |
| 60 | $action = ( 'activate' === $this->action ) ? 'activate' : 'deactivate'; |
| 61 | $token = $this->get_license_info( 'token' ); |
| 62 | |
| 63 | $args = array( |
| 64 | 'user-agent' => 'WordPress/'.$wp_version.'; ' . get_site_url(), |
| 65 | 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3), |
| 66 | 'body' => array( |
| 67 | 'action' => $action, |
| 68 | 'key' => $this->purchase_code, |
| 69 | 'email' => $this->usermail, |
| 70 | 'token' => $token, |
| 71 | 'url' => get_site_url(), |
| 72 | 'item_id' => '3909293', |
| 73 | 'ip' => isset( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : '' |
| 74 | ) |
| 75 | ); |
| 76 | |
| 77 | $args = apply_filters( 'auxels_get_license_result_args', $args ); |
| 78 | |
| 79 | $request = wp_remote_post( $this->api, $args ); |
| 80 | |
| 81 | if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) !== 200 ) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | return json_decode( $request['body'], true ); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * Activate or deactivate license if license info is correct |
| 91 | * |
| 92 | * @param string $usermail envato usermail |
| 93 | * @param string $purchase_code item purchase code |
| 94 | * @param string $action activate or deactivate license |
| 95 | * |
| 96 | * @return array An array containing result of activation or deactivation |
| 97 | */ |
| 98 | public function license_action( $usermail, $purchase_code, $action = 'activate' ){ |
| 99 | |
| 100 | $output = array( |
| 101 | 'success' => 0, |
| 102 | 'status' => '', |
| 103 | 'message' => '', |
| 104 | 'error' => '' |
| 105 | ); |
| 106 | |
| 107 | if( empty( $usermail ) ){ |
| 108 | $output['message'] = __( 'Email address is required.', 'auxin-elements' ); |
| 109 | return $output; |
| 110 | } elseif( empty( $purchase_code ) ){ |
| 111 | $output['message'] = __( 'Purchase key is required.', 'auxin-elements' ); |
| 112 | return $output; |
| 113 | } |
| 114 | |
| 115 | // Check email validation |
| 116 | if( ! is_email( $usermail ) ){ |
| 117 | $output['message'] = __( 'Please enter a valid email address.', 'auxin-elements' ); |
| 118 | return $output; |
| 119 | } |
| 120 | |
| 121 | // get previous license info |
| 122 | $license_info = $this->get_license_info(); |
| 123 | |
| 124 | $this->usermail = $usermail; |
| 125 | $this->purchase_code = $purchase_code; |
| 126 | $this->action = $action; |
| 127 | // fetch license info |
| 128 | $response = $this->get_license_result(); |
| 129 | |
| 130 | if( false !== $response ){ |
| 131 | |
| 132 | if( empty( $response['result'] ) ){ |
| 133 | $output['message'] = __( 'Bad request with wrong header ..', 'auxin-elements' ); |
| 134 | return $output; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | if( 'success' == $response['result'] ){ |
| 139 | // Remove token transient |
| 140 | auxin_delete_transient( 'auxin_check_token_validation_status' ); |
| 141 | |
| 142 | if( 'active' == $response['status'] ){ |
| 143 | |
| 144 | $token = isset( $response['token'] ) ? $response['token'] : ''; |
| 145 | $license_info['usermail'] = $usermail; |
| 146 | $license_info['purchase_code'] = $purchase_code; |
| 147 | $license_info['token'] = $token; |
| 148 | update_site_option( $this->option_prefix, $license_info ); |
| 149 | |
| 150 | } elseif( 'deactive' == $response['status'] ){ |
| 151 | |
| 152 | $license_info['token'] = ''; |
| 153 | update_site_option( $this->option_prefix, $license_info ); |
| 154 | |
| 155 | } |
| 156 | |
| 157 | $output['success'] = 1; |
| 158 | $output['status' ] = $response['status']; |
| 159 | |
| 160 | // if an error occurred |
| 161 | } else { |
| 162 | $output['success'] = 0; |
| 163 | $output['status' ] = $response['status']; |
| 164 | } |
| 165 | |
| 166 | $output['message'] = $response['msg'] . sprintf( ' <sub>[%s]</sub>', $response['code'] ); |
| 167 | |
| 168 | } else { |
| 169 | $output['message'] = sprintf( "%s <a href='http://avt.li/phmli'>%s</a>", __( 'Connection error...', 'auxin-elements' ), __( 'Learn more', 'auxin-elements' ) ); |
| 170 | $output['success'] = 0; |
| 171 | } |
| 172 | |
| 173 | do_action( 'auxin_on_license_action', $action, $output ); |
| 174 | |
| 175 | return $output; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | private function get_token_validation_status() { |
| 180 | |
| 181 | global $wp_version; |
| 182 | |
| 183 | $token = $this->get_license_info( 'token' ); |
| 184 | |
| 185 | $args = array( |
| 186 | 'user-agent' => 'WordPress/'.$wp_version.'; ' . get_site_url(), |
| 187 | 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3), |
| 188 | 'body' => array( |
| 189 | 'action' => 'token', |
| 190 | 'token' => $token, |
| 191 | 'url' => get_site_url() |
| 192 | ) |
| 193 | ); |
| 194 | |
| 195 | $request = wp_remote_post( $this->api, $args ); |
| 196 | |
| 197 | |
| 198 | if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) !== 200 ) { |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | $response = json_decode( $request['body'], true ); |
| 203 | $response['token'] = $token; |
| 204 | |
| 205 | return $response; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | public function maybe_invalid_token(){ |
| 210 | |
| 211 | $status = $this->get_token_validation_status(); |
| 212 | |
| 213 | if( false !== $status && isset( $status['allowed'] ) ){ |
| 214 | // if token is no longer valid to be used on this domain |
| 215 | if ( ! $status['allowed'] ){ |
| 216 | $license_info = get_site_option( $this->option_prefix, array() ); |
| 217 | $license_info['token'] = ''; |
| 218 | update_site_option( $this->option_prefix, $license_info ); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return $status; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | |
| 227 | /** |
| 228 | * Retrieves license info or a specific license field |
| 229 | * |
| 230 | * @param string $field Specific license field or empty string |
| 231 | * @return array|string Returns all license info in array or a string containing license field value |
| 232 | */ |
| 233 | private function get_license_info( $field = '', $default = '' ){ |
| 234 | $license_info = get_site_option( $this->option_prefix, array() ); |
| 235 | |
| 236 | if( empty( $field ) ) |
| 237 | return $license_info; |
| 238 | |
| 239 | return isset( $license_info[ $field ] ) ? $license_info[ $field ] : $default; |
| 240 | } |
| 241 | |
| 242 | } |
| 243 |