| @@ -1,587 +1,372 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * MainWP API Manager | |
| 4 | - * | |
| 5 | - * This class handles MainWP Extensions API licensing. | |
| 6 | - * | |
| 7 | - * @package MainWP/MainWP API Manager | |
| 8 | - * @author Todd Lahman LLC | |
| 9 | - * @copyright Copyright (c) Todd Lahman LLC | |
| 10 | - * @since 1.0.0 | |
| 11 | - */ | |
| 12 | 2 | |
| 13 | -namespace MainWP\Dashboard; | |
| 3 | +class MainWP_Api_Manager { | |
| 14 | 4 | |
| 15 | -/** | |
| 16 | - * Class MainWP_Api_Manager | |
| 17 | - * | |
| 18 | - * @package MainWP\Dashboard | |
| 19 | - */ | |
| 20 | -class MainWP_Api_Manager { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. | |
| 5 | + private $upgrade_url = 'https://mainwp.com/'; | |
| 6 | + private $renew_license_url = 'https://mainwp.com/my-account'; | |
| 7 | + public $domain = ''; | |
| 21 | 8 | |
| 22 | - /** | |
| 23 | - * Extensions upgrade URL. | |
| 24 | - * | |
| 25 | - * @var string Default URL 'https://mainwp.com/' | |
| 26 | - */ | |
| 27 | - private $upgrade_url = 'https://mainwp.com/'; | |
| 9 | + /** | |
| 10 | + * @var The single instance of the class | |
| 11 | + */ | |
| 12 | + protected static $_instance = null; | |
| 28 | 13 | |
| 29 | - /** | |
| 30 | - * Extensions license renewal URL. | |
| 31 | - * | |
| 32 | - * @var string Default 'https://mainwp.com/my-account' | |
| 33 | - */ | |
| 34 | - private $renew_license_url = 'https://mainwp.com/my-account'; | |
| 14 | + public static function instance() { | |
| 35 | 15 | |
| 36 | - /** | |
| 37 | - * Protected static variable to hold the single instance of the class. | |
| 38 | - * | |
| 39 | - * @var mixed Default null | |
| 40 | - */ | |
| 41 | - protected static $instance = null; | |
| 16 | + if ( is_null( self::$_instance ) ) { | |
| 17 | + self::$_instance = new self(); | |
| 18 | + } | |
| 42 | 19 | |
| 43 | - /** | |
| 44 | - * MainWP installation domain name. | |
| 45 | - * | |
| 46 | - * @var string $domain Empty by default. | |
| 47 | - */ | |
| 48 | - public $domain = ''; | |
| 20 | + return self::$_instance; | |
| 21 | + } | |
| 49 | 22 | |
| 50 | - /** | |
| 51 | - * Return the single instance of the class. | |
| 52 | - * | |
| 53 | - * @return mixed $instance The single instance of the class. | |
| 54 | - */ | |
| 55 | - public static function instance() { | |
| 23 | + /** | |
| 24 | + * Cloning is forbidden. | |
| 25 | + * | |
| 26 | + * @since 1.2 | |
| 27 | + */ | |
| 28 | + private function __clone() { | |
| 56 | 29 | |
| 57 | - if ( is_null( static::$instance ) ) { | |
| 58 | - static::$instance = new self(); | |
| 59 | - } | |
| 30 | + } | |
| 60 | 31 | |
| 61 | - return static::$instance; | |
| 62 | - } | |
| 32 | + /** | |
| 33 | + * Unserializing instances of this class is forbidden. | |
| 34 | + * | |
| 35 | + * @since 1.2 | |
| 36 | + */ | |
| 37 | + private function __wakeup() { | |
| 63 | 38 | |
| 64 | - /** | |
| 65 | - * Cloning is forbidden. | |
| 66 | - * | |
| 67 | - * @since 1.2 | |
| 68 | - */ | |
| 69 | - public function __clone() { | |
| 70 | - } | |
| 39 | + } | |
| 71 | 40 | |
| 72 | - /** | |
| 73 | - * Un-serializing instances of this class is forbidden. | |
| 74 | - * | |
| 75 | - * @since 1.2 | |
| 76 | - */ | |
| 77 | - public function __wakeup() { | |
| 78 | - } | |
| 41 | + public function __construct() { | |
| 42 | + $this->domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); | |
| 43 | + } | |
| 79 | 44 | |
| 80 | - /** | |
| 81 | - * MainWP_Api_Manager constructor. | |
| 82 | - * | |
| 83 | - * Run each time the class is called. | |
| 84 | - * | |
| 85 | - * Replace HTTP protocol to HTTPS. | |
| 86 | - */ | |
| 87 | - public function __construct() { | |
| 88 | - $this->domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); | |
| 89 | - } | |
| 45 | + public function get_domain() { | |
| 46 | + return $this->domain; | |
| 47 | + } | |
| 90 | 48 | |
| 91 | - /** | |
| 92 | - * Get Upgrade URL. | |
| 93 | - * | |
| 94 | - * @return string Activation upgrade URL. | |
| 95 | - */ | |
| 96 | - public function get_upgrade_url() { | |
| 97 | - return apply_filters( 'mainwp_api_manager_upgrade_url', $this->upgrade_url ); | |
| 98 | - } | |
| 49 | + public function getUpgradeUrl() { | |
| 50 | + $url = apply_filters( 'mainwp_api_manager_upgrade_url', $this->upgrade_url ); | |
| 51 | + return $url; | |
| 52 | + } | |
| 99 | 53 | |
| 100 | - /** | |
| 101 | - * Get domain URL. | |
| 102 | - * | |
| 103 | - * @return string domain URL. | |
| 104 | - */ | |
| 105 | - public function get_domain() { | |
| 106 | - return $this->domain; | |
| 107 | - } | |
| 108 | 54 | |
| 109 | - /** | |
| 110 | - * Get activation info. | |
| 111 | - * | |
| 112 | - * @param mixed $ext_key extension key. | |
| 113 | - * | |
| 114 | - * @return mixed get_option() get activation information. | |
| 115 | - */ | |
| 116 | 55 | public function get_activation_info( $ext_key ) { |
| 117 | - if ( empty( $ext_key ) ) { | |
| 56 | + if (empty($ext_key)) | |
| 118 | 57 | return array(); |
| 119 | - } | |
| 120 | 58 | |
| 121 | 59 | return get_option( $ext_key . '_APIManAdder' ); |
| 122 | - } | |
| 60 | + } | |
| 123 | 61 | |
| 124 | - /** | |
| 125 | - * Store activation info. | |
| 126 | - * | |
| 127 | - * @param mixed $ext_key Extension key. | |
| 128 | - * @param mixed $info Activation information. | |
| 129 | - * | |
| 130 | - * @return mixed Set activation info. | |
| 131 | - * @uses \MainWP\Dashboard\MainWP_Utility::update_option() | |
| 132 | - */ | |
| 133 | 62 | public function set_activation_info( $ext_key, $info ) { |
| 134 | 63 | |
| 135 | - if ( empty( $ext_key ) ) { | |
| 64 | + if (empty($ext_key)) | |
| 136 | 65 | return false; |
| 137 | - } | |
| 138 | 66 | |
| 139 | - // Clear cached of all activations to reload for next loading. | |
| 140 | - update_option( 'mainwp_extensions_all_activation_cached', '' ); | |
| 67 | + update_option('mainwp_extensions_all_activation_cached', ''); // clear cached of all activations to reload for next loading | |
| 141 | 68 | |
| 142 | 69 | return MainWP_Utility::update_option( $ext_key . '_APIManAdder', $info ); |
| 143 | - } | |
| 70 | + } | |
| 144 | 71 | |
| 145 | - /** | |
| 146 | - * Remove activation info. | |
| 147 | - * | |
| 148 | - * @param mixed $ext_key Extension key. | |
| 149 | - * | |
| 150 | - * @return mixed Remove activation info. | |
| 151 | - */ | |
| 152 | - public function remove_activation_info( $ext_key ) { | |
| 153 | - if ( empty( $ext_key ) ) { | |
| 154 | - return; | |
| 155 | - } | |
| 72 | + public function license_key_activation( $api, $api_key, $api_email ) { | |
| 156 | 73 | |
| 157 | - $options = $this->get_activation_info( $ext_key ); | |
| 158 | - if ( ! is_array( $options ) ) { | |
| 159 | - $options = array(); | |
| 160 | - } | |
| 74 | + $options = $this->get_activation_info( $api ); | |
| 161 | 75 | |
| 162 | - if ( isset( $options['api_key'] ) ) { | |
| 163 | - $options['api_key'] = ''; | |
| 164 | - } | |
| 165 | - if ( isset( $options['activated_key'] ) ) { | |
| 166 | - $options['activated_key'] = 'Deactivated'; | |
| 167 | - } | |
| 168 | - $this->set_activation_info( $ext_key, $options ); | |
| 169 | - } | |
| 76 | + if ( !is_array( $options ) ) { | |
| 77 | + $options = array(); | |
| 78 | + } | |
| 79 | + $current_api_key = isset( $options[ 'api_key' ] ) ? $options[ 'api_key' ] : ''; | |
| 80 | + $current_activation_email = isset( $options[ 'activation_email' ] ) ? $options[ 'activation_email' ] : ''; | |
| 81 | + $activation_status = isset( $options[ 'activated_key' ] ) ? $options[ 'activated_key' ] : ''; | |
| 170 | 82 | |
| 171 | - /** | |
| 172 | - * Check API Key & API Email again MainWP Servers. | |
| 173 | - * | |
| 174 | - * @param array $api_slug Extension activation info. | |
| 175 | - * @param string $api_key API license key. | |
| 176 | - * | |
| 177 | - * @return array Activation info. | |
| 178 | - * | |
| 179 | - * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::activate() | |
| 180 | - * @uses \MainWP\Dashboard\MainWP_Utility::update_option() | |
| 181 | - */ | |
| 182 | - public function license_key_activation( $api_slug, $api_key ) { // phpcs:ignore -- NOSONAR - complex. | |
| 83 | + if ( $activation_status == 'Deactivated' || $activation_status == '' || $api_key == '' || $api_email == '' || $current_api_key != $api_key ) { | |
| 84 | + if ( $current_api_key != $api_key ) { | |
| 85 | + $reset = $this->replace_license_key( array( | |
| 86 | + 'email' => $current_activation_email, | |
| 87 | + 'licence_key' => $current_api_key, | |
| 88 | + 'product_id' => $options[ 'product_id' ], | |
| 89 | + 'instance' => $options[ 'instance_id' ], | |
| 90 | + 'platform' => $this->domain, | |
| 91 | + ) ); | |
| 92 | + if ( !$reset ) { | |
| 93 | + return array( 'error' => __( 'The license could not be deactivated.', 'mainwp' ) ); | |
| 94 | + } | |
| 95 | + } | |
| 183 | 96 | |
| 184 | - $options = $this->get_activation_info( $api_slug ); | |
| 97 | + $return = array(); | |
| 185 | 98 | |
| 186 | - if ( ! is_array( $options ) ) { | |
| 187 | - $options = array(); | |
| 188 | - } | |
| 189 | - $current_api_key = isset( $options['api_key'] ) ? $options['api_key'] : ''; | |
| 190 | - $activation_status = isset( $options['activated_key'] ) ? $options['activated_key'] : ''; | |
| 99 | + $args = array( | |
| 100 | + 'email' => $api_email, | |
| 101 | + 'licence_key' => $api_key, | |
| 102 | + 'product_id' => $options[ 'product_id' ], | |
| 103 | + 'instance' => $options[ 'instance_id' ], | |
| 104 | + 'software_version' => $options[ 'software_version' ], | |
| 105 | + 'platform' => $this->domain, | |
| 106 | + ); | |
| 191 | 107 | |
| 192 | - if ( 'Deactivated' === $activation_status || '' === $activation_status || '' === $api_key || $current_api_key !== $api_key ) { | |
| 193 | - if ( $current_api_key !== $api_key ) { | |
| 194 | - $reset = $this->replace_license_key( | |
| 195 | - array( | |
| 196 | - 'api_key' => $api_key, | |
| 197 | - 'product_id' => $options['product_id'], | |
| 198 | - 'instance' => $options['instance_id'], | |
| 199 | - 'software_version' => $options['software_version'], | |
| 200 | - 'software_slug' => $api_slug, | |
| 201 | - ) | |
| 202 | - ); | |
| 203 | - if ( ! $reset ) { | |
| 204 | - return array( 'error' => esc_html__( 'The license could not be deactivated.', 'mainwp' ) ); | |
| 205 | - } | |
| 206 | - } | |
| 108 | + $activate_results = json_decode( MainWP_Api_Manager_Key::instance()->activate( $args ), true ); | |
| 207 | 109 | |
| 208 | - $return = array(); | |
| 110 | + if ( $activate_results[ 'activated' ] == true ) { | |
| 111 | + $return[ 'result' ] = 'SUCCESS'; | |
| 112 | + $mess = isset( $activate_results[ 'message' ] ) ? $activate_results[ 'message' ] : ''; | |
| 113 | + $return[ 'message' ] = __( 'The extension has been activated. ', 'mainwp' ) . $mess; | |
| 114 | + $options[ 'api_key' ] = $api_key; | |
| 115 | + $options[ 'activation_email' ] = $api_email; | |
| 116 | + $options[ 'activated_key' ] = 'Activated'; | |
| 117 | + $options[ 'deactivate_checkbox' ] = 'off'; | |
| 118 | + } | |
| 209 | 119 | |
| 210 | - $args = array( | |
| 211 | - 'api_key' => $api_key, | |
| 212 | - 'product_id' => $options['product_id'], | |
| 213 | - 'instance' => $options['instance_id'], | |
| 214 | - 'software_version' => $options['software_version'], | |
| 215 | - 'object' => $this->domain, | |
| 216 | - 'software_slug' => $api_slug, | |
| 217 | - ); | |
| 120 | + if ( $activate_results == false ) { | |
| 121 | + $apisslverify = get_option( 'mainwp_api_sslVerifyCertificate' ); | |
| 122 | + if ( $apisslverify == 1 ) { | |
| 123 | + MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', 0 ); | |
| 124 | + $return[ 'retry_action' ] = 1; | |
| 125 | + } else { | |
| 126 | + $return[ 'error' ] = __( 'Connection failed to the License Key API server. Try again later.', 'mainwp' ); | |
| 127 | + } | |
| 128 | + $options[ 'api_key' ] = ''; | |
| 129 | + $options[ 'activation_email' ] = ''; | |
| 130 | + $options[ 'activated_key' ] = 'Deactivated'; | |
| 131 | + } | |
| 218 | 132 | |
| 219 | - $activate_results = MainWP_Api_Manager_Key::instance()->activate( $args ); | |
| 220 | - if ( ! empty( $activate_results ) ) { | |
| 221 | - $activate_results = json_decode( $activate_results, true ); | |
| 222 | - } else { | |
| 223 | - $activate_results = array(); | |
| 224 | - } | |
| 133 | + $error = $this->check_response_for_api_errors( $activate_results ); | |
| 134 | + if ( !empty( $error ) ) | |
| 135 | + $return[ 'error' ] = $error; | |
| 225 | 136 | |
| 226 | - if ( true === $activate_results['activated'] ) { | |
| 227 | - $return['result'] = 'SUCCESS'; | |
| 228 | - $mess = isset( $activate_results['message'] ) ? $activate_results['message'] : ''; | |
| 229 | - $return['message'] = esc_html__( 'The extension has been activated. ', 'mainwp' ) . $mess; | |
| 230 | - $options['api_key'] = $api_key; | |
| 231 | - $options['activated_key'] = 'Activated'; | |
| 232 | - $options['deactivate_checkbox'] = 'off'; | |
| 233 | - $options['mainwp_version'] = MainWP_System::instance()->get_dashboard_version(); | |
| 234 | - MainWP_Utility::instance()->get_set_deactivated_licenses_alerted( $api_slug, 0, 'set' ); // so next time deactived it will alerted. | |
| 235 | - } | |
| 137 | + $this->set_activation_info( $api, $options ); | |
| 236 | 138 | |
| 237 | - if ( false === $activate_results ) { | |
| 238 | - $apisslverify = get_option( 'mainwp_api_sslVerifyCertificate' ); | |
| 239 | - if ( 1 === $apisslverify ) { | |
| 240 | - MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', 0 ); | |
| 241 | - $return['retry_action'] = 1; | |
| 242 | - } else { | |
| 243 | - $return['error'] = esc_html__( 'Connection failed to the License Key API server. Try again later.', 'mainwp' ); | |
| 244 | - } | |
| 245 | - $options['api_key'] = ''; | |
| 246 | - $options['activated_key'] = 'Deactivated'; | |
| 247 | - } | |
| 139 | + return $return; | |
| 140 | + } else { | |
| 141 | + return array( 'result' => 'SUCCESS' ); | |
| 142 | + } | |
| 143 | + } | |
| 248 | 144 | |
| 249 | - $error = $this->check_response_for_api_errors( $activate_results ); | |
| 250 | - if ( ! empty( $error ) ) { | |
| 251 | - $return['error'] = $error; | |
| 252 | - } | |
| 145 | + // Deactivate the current license key before activating the new license key | |
| 146 | + private function replace_license_key( $args ) { | |
| 147 | + $reset = MainWP_Api_Manager_Key::instance()->deactivate( $args ); // reset license key activation | |
| 253 | 148 | |
| 254 | - $this->set_activation_info( $api_slug, $options ); | |
| 149 | + if ( $reset == true ) { | |
| 150 | + return true; | |
| 151 | + } | |
| 152 | + } | |
| 255 | 153 | |
| 256 | - return $return; | |
| 257 | - } else { | |
| 258 | - return array( 'result' => 'SUCCESS' ); | |
| 259 | - } | |
| 260 | - } | |
| 154 | + public function license_key_deactivation( $api ) { | |
| 261 | 155 | |
| 262 | - /** | |
| 263 | - * Deactivate the current license key before activating the new license key. | |
| 264 | - * | |
| 265 | - * @param array $args Request arguments. | |
| 266 | - * | |
| 267 | - * @return bool True on success, false on failure. | |
| 268 | - * | |
| 269 | - * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::deactivate() | |
| 270 | - */ | |
| 271 | - private function replace_license_key( $args ) { | |
| 272 | - $reset = MainWP_Api_Manager_Key::instance()->deactivate( $args ); // reset license key activation. | |
| 156 | + $options = $this->get_activation_info( $api ); | |
| 157 | + if ( !is_array( $options ) ) { | |
| 158 | + $options = array(); | |
| 159 | + } | |
| 273 | 160 | |
| 274 | - if ( true === $reset ) { | |
| 275 | - return true; | |
| 276 | - } | |
| 277 | - } | |
| 161 | + $activation_status = isset( $options[ 'activated_key' ] ) ? $options[ 'activated_key' ] : ''; | |
| 162 | + $current_api_key = isset( $options[ 'api_key' ] ) ? $options[ 'api_key' ] : ''; | |
| 163 | + $current_activation_email = isset( $options[ 'activation_email' ] ) ? $options[ 'activation_email' ] : ''; | |
| 278 | 164 | |
| 279 | - /** | |
| 280 | - * Deactivate license Key. | |
| 281 | - * | |
| 282 | - * @param array $api_slug Extension activation info. | |
| 283 | - * @param array $api_key Extension activation api key. | |
| 284 | - * | |
| 285 | - * @return array Deactivation info. | |
| 286 | - * | |
| 287 | - * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::deactivate() | |
| 288 | - */ | |
| 289 | - public function license_key_deactivation( $api_slug, $api_key ) { | |
| 165 | + $return = array(); | |
| 290 | 166 | |
| 291 | - $options = $this->get_activation_info( $api_slug ); | |
| 292 | - if ( ! is_array( $options ) ) { | |
| 293 | - $options = array(); | |
| 294 | - } | |
| 167 | + if ( $activation_status == 'Activated' && $current_api_key != '' && $current_activation_email != '' ) { | |
| 168 | + $activate_results = MainWP_Api_Manager_Key::instance()->deactivate( array( | |
| 169 | + 'email' => $current_activation_email, | |
| 170 | + 'licence_key' => $current_api_key, | |
| 171 | + 'product_id' => $options[ 'product_id' ], | |
| 172 | + 'instance' => $options[ 'instance_id' ], | |
| 173 | + 'platform' => $this->domain, | |
| 174 | + ) ); // reset license key activation | |
| 295 | 175 | |
| 296 | - $activation_status = isset( $options['activated_key'] ) ? $options['activated_key'] : ''; | |
| 176 | + $activate_results = json_decode( $activate_results, true ); | |
| 177 | + if ( $activate_results['deactivated'] == true || (isset( $activate_results['activated'] ) && $activate_results['activated'] == 'inactive')) { | |
| 178 | + $options[ 'api_key' ] = ''; | |
| 179 | + $options[ 'activation_email' ] = ''; | |
| 180 | + $options[ 'activated_key' ] = 'Deactivated'; | |
| 181 | + $options[ 'deactivate_checkbox' ] = 'on'; | |
| 182 | + $return[ 'result' ] = 'SUCCESS'; | |
| 183 | + $return[ 'activations_remaining' ] = $activate_results[ 'activations_remaining' ]; | |
| 184 | + } | |
| 297 | 185 | |
| 298 | - if ( empty( $api_key ) ) { | |
| 299 | - $options['api_key'] = ''; | |
| 300 | - $this->set_activation_info( $api_slug, $options ); | |
| 301 | - } | |
| 186 | + $error = $this->check_response_for_api_errors( $activate_results ); | |
| 187 | + if ( !empty( $error ) ) | |
| 188 | + $return[ 'error' ] = $error; | |
| 302 | 189 | |
| 303 | - $return = array(); | |
| 190 | + $this->set_activation_info( $api, $options ); | |
| 304 | 191 | |
| 305 | - if ( 'Activated' === $activation_status && '' !== $api_key ) { | |
| 306 | - $activate_results = MainWP_Api_Manager_Key::instance()->deactivate( | |
| 307 | - array( | |
| 308 | - 'product_id' => $options['product_id'], | |
| 309 | - 'instance' => $options['instance_id'], | |
| 310 | - 'api_key' => $api_key, | |
| 311 | - 'object' => $this->domain, | |
| 312 | - 'software_version' => $options['software_version'], | |
| 313 | - 'software_slug' => $api_slug, | |
| 314 | - ) | |
| 315 | - ); // reset license key activation. | |
| 192 | + return $return; | |
| 193 | + } | |
| 194 | + update_option('mainwp_extensions_all_activation_cached', ''); // to fix: clear cached of all activations to reload for next loading | |
| 195 | + return array( 'result' => 'SUCCESS' ); | |
| 196 | + } | |
| 316 | 197 | |
| 317 | - $activate_results = json_decode( $activate_results, true ); | |
| 318 | - if ( isset( $activate_results['deactivated'] ) && true === $activate_results['deactivated'] ) { | |
| 319 | - $options['api_key'] = ''; | |
| 320 | - $options['activated_key'] = 'Deactivated'; | |
| 321 | - $options['deactivate_checkbox'] = 'on'; | |
| 322 | - $return['result'] = 'SUCCESS'; | |
| 323 | - $return['activations_remaining'] = $activate_results['activations_remaining']; | |
| 324 | - } | |
| 198 | + public function test_login_api( $username, $password ) { | |
| 199 | + if ( empty( $username ) || empty( $password ) ) { | |
| 200 | + return false; | |
| 201 | + } | |
| 325 | 202 | |
| 326 | - $error = $this->check_response_for_api_errors( $activate_results ); | |
| 327 | - if ( ! empty( $error ) ) { | |
| 328 | - $return['error'] = $error; | |
| 329 | - $options['api_key'] = ''; | |
| 330 | - $options['activated_key'] = 'Deactivated'; | |
| 331 | - } | |
| 203 | + return MainWP_Api_Manager_Key::instance()->testloginapi( array( | |
| 204 | + 'username' => $username, | |
| 205 | + 'password' => $password, | |
| 206 | + ) ); | |
| 207 | + } | |
| 332 | 208 | |
| 333 | - $this->set_activation_info( $api_slug, $options ); | |
| 209 | + public function purchase_software( $username, $password, $productId ) { | |
| 210 | + if ( empty( $username ) || empty( $password ) ) { | |
| 211 | + return false; | |
| 212 | + } | |
| 334 | 213 | |
| 335 | - return $return; | |
| 336 | - } | |
| 337 | - // to fix: clear cached of all activations to reload for next loading. | |
| 338 | - update_option( 'mainwp_extensions_all_activation_cached', '' ); | |
| 339 | - return array( 'result' => 'SUCCESS' ); | |
| 214 | + return MainWP_Api_Manager_Key::instance()->purchasesoftware( array( | |
| 215 | + 'username' => $username, | |
| 216 | + 'password' => $password, | |
| 217 | + 'product_id' => $productId | |
| 218 | + ) ); | |
| 340 | 219 | } |
| 341 | 220 | |
| 342 | - /** | |
| 343 | - * Test the users MainWP.com Login details against MainWP Server. | |
| 344 | - * | |
| 345 | - * @param string $api_key MainWP api key. | |
| 346 | - * | |
| 347 | - * @return mixed Login test result. | |
| 348 | - * | |
| 349 | - * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::verify_api_key() | |
| 350 | - */ | |
| 351 | - public function verify_mainwp_api( $api_key ) { | |
| 352 | - if ( empty( $api_key ) ) { | |
| 353 | - return false; | |
| 354 | - } | |
| 221 | + public function get_purchased_software( $username, $password, $productId = "", $no_register = false ) { | |
| 222 | + if ( empty( $username ) || empty( $password ) ) { | |
| 223 | + return false; | |
| 224 | + } | |
| 355 | 225 | |
| 356 | - return MainWP_Api_Manager_Key::instance()->verify_api_key( | |
| 357 | - array( | |
| 358 | - 'api_key' => $api_key, | |
| 359 | - ) | |
| 360 | - ); | |
| 361 | - } | |
| 226 | + return MainWP_Api_Manager_Key::instance()->getpurchasedsoftware( array( | |
| 227 | + 'username' => $username, | |
| 228 | + 'password' => $password, | |
| 229 | + 'product_id' => $productId, | |
| 230 | + 'noauth' => $no_register ? 1 : 0 | |
| 231 | + ) ); | |
| 232 | + } | |
| 362 | 233 | |
| 234 | + public function grab_license_key( $api, $username, $password ) { | |
| 363 | 235 | |
| 364 | - /** | |
| 365 | - * Check if the user purchased the software. | |
| 366 | - * | |
| 367 | - * @param string $productId extension (product) ID. | |
| 368 | - * | |
| 369 | - * @return mixed purchase_software() purchase extensions. | |
| 370 | - * | |
| 371 | - * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::purchase_software() | |
| 372 | - */ | |
| 373 | - public function purchase_software( $productId ) { | |
| 374 | - return MainWP_Api_Manager_Key::instance()->purchase_software( | |
| 375 | - array( | |
| 376 | - 'product_id' => $productId, | |
| 377 | - ) | |
| 378 | - ); | |
| 379 | - } | |
| 236 | + $options = $this->get_activation_info( $api ); | |
| 237 | + if ( !is_array( $options ) ) { | |
| 238 | + $options = array(); | |
| 239 | + } | |
| 240 | + $activation_status = isset( $options[ 'activated_key' ] ) ? $options[ 'activated_key' ] : ''; | |
| 380 | 241 | |
| 381 | - /** | |
| 382 | - * Get users purchased extensions. | |
| 383 | - * | |
| 384 | - * @param string $api_key api key. | |
| 385 | - * @param string $productId extension (product) ID. | |
| 386 | - * @param bool $no_register registration request. | |
| 387 | - * | |
| 388 | - * @return array Purchased extensions. | |
| 389 | - * | |
| 390 | - * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::get_purchased_software() | |
| 391 | - */ | |
| 392 | - public function get_purchased_extension( $api_key, $productId = '', $no_register = false ) { | |
| 393 | - if ( empty( $api_key ) ) { | |
| 394 | - return false; | |
| 395 | - } | |
| 242 | + $api_key = isset( $options[ 'api_key' ] ) ? $options[ 'api_key' ] : ''; | |
| 243 | + $api_email = isset( $options[ 'activation_email' ] ) ? $options[ 'activation_email' ] : ''; | |
| 396 | 244 | |
| 397 | - return MainWP_Api_Manager_Key::instance()->get_purchased_software( | |
| 398 | - array( | |
| 399 | - 'product_id' => $productId, | |
| 400 | - 'api_key' => $api_key, | |
| 401 | - 'noauth' => $no_register ? 1 : 0, | |
| 402 | - ) | |
| 403 | - ); | |
| 404 | - } | |
| 245 | + if ( $activation_status == 'Deactivated' || $activation_status == '' || $api_key == '' || $api_email == '' ) { | |
| 246 | + $return = array(); | |
| 247 | + if ( $username != '' && $password != '' ) { | |
| 405 | 248 | |
| 406 | - /** | |
| 407 | - * Grab users associate MainWP License key for the selected Extension. | |
| 408 | - * | |
| 409 | - * @param array $api_slug Extension activation info. | |
| 410 | - * @param string $master_api_key MainWP master api key. | |
| 411 | - * | |
| 412 | - * @return mixed Activation info. | |
| 413 | - * | |
| 414 | - * @uses \MainWP\Dashboard\MainWP_Api_Manager_Key::grab_api_key() | |
| 415 | - */ | |
| 416 | - public function grab_license_key( $api_slug, $master_api_key ) { // phpcs:ignore -- NOSONAR - Current complexity is the only way to achieve desired results, pull request solutions appreciated. | |
| 249 | + $args = array( | |
| 250 | + 'username' => $username, | |
| 251 | + 'password' => $password, | |
| 252 | + 'product_id' => isset( $options[ 'product_id' ] ) ? $options[ 'product_id' ] : '', | |
| 253 | + 'instance' => isset( $options[ 'instance_id' ] ) ? $options[ 'instance_id' ] : '', | |
| 254 | + 'software_version' => isset( $options[ 'software_version' ] ) ? $options[ 'software_version' ] : '', | |
| 255 | + 'platform' => $this->domain, | |
| 256 | + ); | |
| 417 | 257 | |
| 418 | - $options = $this->get_activation_info( $api_slug ); | |
| 419 | - if ( ! is_array( $options ) ) { | |
| 420 | - $options = array(); | |
| 421 | - } | |
| 422 | - $activation_status = isset( $options['activated_key'] ) ? $options['activated_key'] : ''; | |
| 423 | - $api_key = isset( $options['api_key'] ) ? $options['api_key'] : ''; | |
| 258 | + $activate_results = json_decode( MainWP_Api_Manager_Key::instance()->grabapikey( $args ), true ); | |
| 259 | + $options[ 'api_key' ] = ''; | |
| 260 | + $options[ 'activation_email' ] = ''; | |
| 261 | + $options[ 'activated_key' ] = 'Deactivated'; | |
| 424 | 262 | |
| 425 | - if ( ! isset( $options['product_item_id'] ) ) { | |
| 426 | - $options['product_item_id'] = 0; // to compatible. | |
| 427 | - } | |
| 263 | + if ( is_array( $activate_results ) && isset( $activate_results[ 'activated' ] ) && ( $activate_results[ 'activated' ] == true ) && !empty( $activate_results[ 'api_key' ] ) ) { | |
| 264 | + $return[ 'result' ] = 'SUCCESS'; | |
| 265 | + $mess = isset( $activate_results[ 'message' ] ) ? $activate_results[ 'message' ] : ''; | |
| 266 | + $return[ 'message' ] = __( 'Extension activated. ', 'mainwp' ) . $mess; | |
| 267 | + $options[ 'api_key' ] = $return[ 'api_key' ] = $activate_results[ 'api_key' ]; | |
| 268 | + $options[ 'activation_email' ] = $return[ 'activation_email' ] = $activate_results[ 'activation_email' ]; | |
| 269 | + $options[ 'activated_key' ] = 'Activated'; | |
| 270 | + $options[ 'deactivate_checkbox' ] = 'off'; | |
| 271 | + } else { | |
| 428 | 272 | |
| 429 | - if ( 'Deactivated' === $activation_status || '' === $activation_status || '' === $api_key || empty( $options['product_item_id'] ) ) { | |
| 430 | - $return = array(); | |
| 431 | - if ( ! empty( $master_api_key ) ) { | |
| 432 | - $args = array( | |
| 433 | - 'api_key' => $master_api_key, | |
| 434 | - 'product_id' => isset( $options['product_id'] ) ? $options['product_id'] : '', | |
| 435 | - 'instance' => isset( $options['instance_id'] ) ? $options['instance_id'] : '', | |
| 436 | - 'software_version' => isset( $options['software_version'] ) ? $options['software_version'] : '', | |
| 437 | - 'object' => $this->domain, | |
| 438 | - 'software_slug' => $api_slug, | |
| 439 | - ); | |
| 273 | + if ( $activate_results == false ) { | |
| 274 | + $return[ 'error' ] = __( 'Connection with the API license server could not be established. Please, try again later.', "mainwp" ); | |
| 275 | + } else if ( isset( $activate_results[ 'error' ] ) ) { | |
| 276 | + $return[ 'error' ] = $activate_results[ 'error' ]; | |
| 277 | + } else if ( empty( $activate_results[ 'api_key' ] ) ) { | |
| 278 | + $return[ 'error' ] = __( 'License key could not be found.', 'mainwp' ); | |
| 279 | + } else { | |
| 280 | + $return[ 'error' ] = __( 'An undefined error occurred. Please try again later or contact MainWP Support.', 'mainwp' ); | |
| 281 | + } | |
| 282 | + } | |
| 440 | 283 | |
| 441 | - $activate_results = MainWP_Api_Manager_Key::instance()->grab_api_key( $args ); | |
| 442 | - if ( ! empty( $activate_results ) ) { | |
| 443 | - $activate_results = json_decode( $activate_results, true ); | |
| 444 | - } else { | |
| 445 | - $activate_results = array(); | |
| 446 | - } | |
| 284 | + $error = $this->check_response_for_api_errors( $activate_results ); | |
| 285 | + if ( !empty( $error ) ) | |
| 286 | + $return[ 'error' ] = $error; | |
| 447 | 287 | |
| 448 | - $options['api_key'] = ''; | |
| 449 | - $options['activated_key'] = 'Deactivated'; | |
| 288 | + $this->set_activation_info( $api, $options ); | |
| 450 | 289 | |
| 451 | - if ( ! isset( $options['product_item_id'] ) ) { | |
| 452 | - $options['product_item_id'] = 0; // to compatible. | |
| 453 | - } | |
| 290 | + return $return; | |
| 291 | + } else { | |
| 292 | + return array( 'error' => __( 'Username and Password are required in order to grab extensions API keys.', 'mainwp' ) ); | |
| 293 | + } | |
| 294 | + } | |
| 454 | 295 | |
| 455 | - if ( is_array( $activate_results ) && isset( $activate_results['activated'] ) && ( true === $activate_results['activated'] ) && ! empty( $activate_results['api_key'] ) ) { | |
| 456 | - $return['result'] = 'SUCCESS'; | |
| 457 | - $mess = isset( $activate_results['message'] ) ? $activate_results['message'] : ''; | |
| 458 | - $return['message'] = esc_html__( 'Extension activated. ', 'mainwp' ) . $mess; | |
| 459 | - $options['api_key'] = $activate_results['api_key']; | |
| 460 | - $return['api_key'] = $activate_results['api_key']; | |
| 461 | - $options['activated_key'] = 'Activated'; | |
| 462 | - $options['product_item_id'] = isset( $activate_results['product_item_id'] ) ? intval( $activate_results['product_item_id'] ) : 0; | |
| 463 | - $options['deactivate_checkbox'] = 'off'; | |
| 464 | - } elseif ( false === $activate_results ) { | |
| 465 | - $return['error'] = esc_html__( 'Connection with the API license server could not be established. Please, try again later.', 'mainwp' ); | |
| 466 | - } elseif ( isset( $activate_results['error'] ) ) { | |
| 467 | - $return['error'] = $activate_results['error']; | |
| 468 | - } elseif ( empty( $activate_results['api_key'] ) ) { | |
| 469 | - $return['error'] = esc_html__( 'License key could not be found.', 'mainwp' ); | |
| 470 | - } else { | |
| 471 | - $return['error'] = esc_html__( 'An undefined error occurred. Please try again later.', 'mainwp' ); | |
| 472 | - } | |
| 296 | + return array( 'result' => 'SUCCESS' ); | |
| 297 | + } | |
| 473 | 298 | |
| 474 | - $error = $this->check_response_for_api_errors( $activate_results ); | |
| 475 | - if ( ! empty( $error ) ) { | |
| 476 | - $return['error'] = $error; | |
| 299 | + public function check_response_for_api_errors( $response ) { | |
| 300 | + if ( !is_array( $response ) || !isset( $response[ 'code' ] ) ) | |
| 301 | + return false; | |
| 302 | + | |
| 303 | + $error = ''; | |
| 304 | + switch ( $response[ 'code' ] ) { | |
| 305 | + case '100': | |
| 306 | + $error = __( 'Invalid request! Please try to deactivate and re-activate the extension on the WP > Plugins page and try to activate API key again.', 'mainwp' ); | |
| 307 | + break; | |
| 308 | + case '102': | |
| 309 | + $error = __( 'Activation error! Download permission for this product could not be found.', 'mainwp' ); | |
| 310 | + break; | |
| 311 | + case '101': | |
| 312 | + $error = __( 'Activation error! Matching API key could not be found.', 'mainwp' ); | |
| 313 | + break; | |
| 314 | + case '103': | |
| 315 | + case '104': | |
| 316 | + $error = __( 'Invalid Instance ID! Please try to deactivate and re-activate the extension on the WP > Plugins page and try to activate API key again.', 'mainwp' ); | |
| 317 | + break; | |
| 318 | + case '105': | |
| 319 | + case '106': | |
| 320 | + $error = isset( $response[ 'error' ] ) ? $response[ 'error' ] : ''; | |
| 321 | + $info = isset( $response[ 'additional info' ] ) ? ' ' . $response[ 'additional info' ] : ''; | |
| 322 | + $error = $error . $info; | |
| 323 | + break; | |
| 324 | + case '900' : | |
| 325 | + $error = __( 'Your membership is on hold. Reactivate your membership to activate MainWP extensions', 'mainwp' ); | |
| 326 | + break; | |
| 327 | + case '901' : | |
| 328 | + $error = __( 'Your membership has been canceled. Reactivate your membership to activate MainWP extensions', 'mainwp' ); | |
| 329 | + break; | |
| 330 | + case '902' : | |
| 331 | + $error = __( 'Your membership has expired. Reactivate your membership to activate MainWP extensions', 'mainwp' ); | |
| 332 | + break; | |
| 477 | 333 | } |
| 478 | - | |
| 479 | - $this->set_activation_info( $api_slug, $options ); | |
| 480 | - | |
| 481 | - return $return; | |
| 482 | - } else { | |
| 483 | - return array( 'error' => esc_html__( 'MainWP API key are required in order to grab extensions API keys.', 'mainwp' ) ); | |
| 334 | + return $error; | |
| 484 | 335 | } |
| 485 | - } | |
| 486 | 336 | |
| 487 | - return array( 'result' => 'SUCCESS' ); | |
| 488 | - } | |
| 337 | + public function check_response_for_intall_errors( $response, $software_title = "" ) { | |
| 338 | + if ( !is_array( $response ) || !isset( $response[ 'error' ] ) ) | |
| 339 | + return false; | |
| 489 | 340 | |
| 490 | - /** | |
| 491 | - * Check if $response contains any api errors. | |
| 492 | - * | |
| 493 | - * @param array $response Response array. | |
| 494 | - * | |
| 495 | - * @return string $error Error message. | |
| 496 | - */ | |
| 497 | - public function check_response_for_api_errors( $response ) { //phpcs:ignore -- complex. | |
| 498 | - if ( ! is_array( $response ) || ! isset( $response['code'] ) ) { | |
| 499 | - return false; | |
| 500 | - } | |
| 341 | + switch ( $response[ 'error' ] ) { | |
| 342 | + case 'subscription_on_hold': | |
| 343 | + return __( 'Your membership is on hold. Reactivate your membership to install MainWP extensions.', 'mainwp' ); | |
| 344 | + break; | |
| 345 | + case 'subscription_cancelled': | |
| 346 | + return __( 'Your membership has been canceled. Reactivate your membership to install MainWP extensions.', 'mainwp' ); | |
| 347 | + break; | |
| 348 | + case 'subscription_expired': | |
| 349 | + return __( 'Your membership has expired. Reactivate your membership to install MainWP extensions.', 'mainwp' ); | |
| 350 | + break; | |
| 351 | + default : //download_revoked | |
| 352 | + return sprintf( __( 'Download permission for %s has been revoked possibly due to a license key or membership expiring. You can reactivate or purchase a license key from your account <a href="%s" target="_blank">dashboard</a>.', 'mainwp' ), $software_title, $this->renew_license_url ); | |
| 353 | + break; | |
| 354 | + } | |
| 355 | + return false; | |
| 356 | + } | |
| 501 | 357 | |
| 502 | - if ( isset( $response['error'] ) ) { | |
| 503 | - return $response['error']; | |
| 504 | - } | |
| 358 | + public function update_check( $args ) { | |
| 359 | + $args[ 'domain' ] = $this->domain; | |
| 505 | 360 | |
| 506 | - $error = ''; | |
| 507 | - switch ( $response['code'] ) { | |
| 508 | - case '100': | |
| 509 | - $error = esc_html__( 'Activation error! Please try to deactivate and re-activate the extension on the WP > Plugins page and try to activate API key again.', 'mainwp' ); | |
| 510 | - break; | |
| 511 | - case '102': | |
| 512 | - $error = esc_html__( 'Activation error! Download permission for this product could not be found.', 'mainwp' ); | |
| 513 | - break; | |
| 514 | - case '101': | |
| 515 | - $error = esc_html__( 'Activation error! Matching API key could not be found.', 'mainwp' ); | |
| 516 | - break; | |
| 517 | - case '103': | |
| 518 | - case '104': | |
| 519 | - $error = esc_html__( 'Invalid Instance ID! Please try to deactivate and re-activate the extension on the WP > Plugins page and try to activate API key again.', 'mainwp' ); | |
| 520 | - break; | |
| 521 | - case '105': | |
| 522 | - case '106': | |
| 523 | - $error = isset( $response['error'] ) ? $response['error'] : ''; | |
| 524 | - $info = isset( $response['additional info'] ) ? ' ' . $response['additional info'] : ''; | |
| 525 | - $error = $error . $info; | |
| 526 | - break; | |
| 527 | - case '900': | |
| 528 | - $error = esc_html__( 'Your membership is on hold. Reactivate your membership to activate MainWP extensions', 'mainwp' ); | |
| 529 | - break; | |
| 530 | - case '901': | |
| 531 | - $error = esc_html__( 'Your membership has been canceled. Reactivate your membership to activate MainWP extensions', 'mainwp' ); | |
| 532 | - break; | |
| 533 | - case '902': | |
| 534 | - $error = esc_html__( 'Your membership has expired. Reactivate your membership to activate MainWP extensions', 'mainwp' ); | |
| 535 | - break; | |
| 536 | - default: | |
| 537 | - break; | |
| 538 | - } | |
| 539 | - return $error; | |
| 540 | - } | |
| 361 | + return MainWP_Api_Manager_Plugin_Update::instance()->update_check( $args ); | |
| 362 | + } | |
| 541 | 363 | |
| 542 | - /** | |
| 543 | - * Check if $response contains any install errors. | |
| 544 | - * | |
| 545 | - * @param array $response Response array. | |
| 546 | - * @param string $software_title Extension title. | |
| 547 | - * | |
| 548 | - * @return string $return Installation Error messages. | |
| 549 | - */ | |
| 550 | - public function check_response_for_intall_errors( $response, $software_title = '' ) { | |
| 551 | - if ( ! is_array( $response ) || ! isset( $response['error'] ) ) { | |
| 552 | - return false; | |
| 553 | - } | |
| 364 | + public function request_plugin_information( $args ) { | |
| 365 | + $args[ 'domain' ] = $this->domain; | |
| 554 | 366 | |
| 555 | - $return = false; | |
| 556 | - switch ( $response['error'] ) { | |
| 557 | - case 'subscription_on_hold': | |
| 558 | - $return = esc_html__( 'Your membership is on hold. Reactivate your membership to install MainWP extensions.', 'mainwp' ); | |
| 559 | - break; | |
| 560 | - case 'subscription_cancelled': | |
| 561 | - $return = esc_html__( 'Your membership has been canceled. Reactivate your membership to install MainWP extensions.', 'mainwp' ); | |
| 562 | - break; | |
| 563 | - case 'subscription_expired': | |
| 564 | - $return = esc_html__( 'Your membership has expired. Reactivate your membership to install MainWP extensions.', 'mainwp' ); | |
| 565 | - break; | |
| 566 | - default: // download_revoked. | |
| 567 | - $return = sprintf( esc_html__( 'Download permission for %1$s has been revoked possibly due to a license key or membership expiring. You can reactivate or purchase a license key from your account <a href="%2$s" target="_blank">dashboard</a>.', 'mainwp' ), $software_title, $this->renew_license_url ); | |
| 568 | - break; | |
| 569 | - } | |
| 570 | - return $return; | |
| 571 | - } | |
| 367 | + return MainWP_Api_Manager_Plugin_Update::instance()->request( $args ); | |
| 368 | + } | |
| 572 | 369 | |
| 573 | - /** | |
| 574 | - * Request plugin information | |
| 575 | - * | |
| 576 | - * @param array $args Request arguments. | |
| 577 | - * | |
| 578 | - * @return array Plugin info. | |
| 579 | - * | |
| 580 | - * @uses MainWP_Api_Manager_Plugin_Update::request() | |
| 581 | - */ | |
| 582 | - public function request_extension_information( $args ) { | |
| 583 | - return MainWP_Api_Manager_Plugin_Update::instance()->request( $args ); | |
| 584 | - } | |
| 585 | 370 | } |
| 586 | 371 | |
| 587 | -// End of class. | |
| 372 | +// End of class | |