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