1313
1414// Exit if accessed directly.
1515use ThemeisleSDK \Common \Abstract_Module ;
16+ use ThemeisleSDK \Loader ;
1617use ThemeisleSDK \Product ;
1718
1819if ( ! defined ( 'ABSPATH ' ) ) {
@@ -435,7 +436,9 @@ public function get_error() {
435436 * @return bool|\WP_Error
436437 */
437438 private function do_license_process ( $ license , $ action = 'toggle ' ) {
438-
439+ if ( strlen ( $ license ) < 10 ) {
440+ return new \WP_Error ( 'themeisle-license-invalid-format ' , 'Invalid license. ' );
441+ }
439442 $ status = $ this ->get_license_status ();
440443
441444 if ( 'valid ' === $ status && 'activate ' === $ action ) {
@@ -451,25 +454,27 @@ private function do_license_process( $license, $action = 'toggle' ) {
451454 'item_name ' => rawurlencode ( $ this ->product ->get_name () ),
452455 'url ' => rawurlencode ( home_url () ),
453456 );
454-
455- switch ( $ action ) {
456- case 'activate ' :
457- $ api_params ['edd_action ' ] = 'activate_license ' ;
458- break ;
459- case 'deactivate ' :
460- $ api_params ['edd_action ' ] = 'deactivate_license ' ;
461- break ;
462- case 'check ' :
463- $ api_params ['edd_action ' ] = 'check_license ' ;
464- break ;
465- case 'toggle ' :
466- case 'default ' :
467- $ api_params ['edd_action ' ] = ( 'valid ' !== $ status ? ( 'activate_license ' ) : ( 'deactivate_license ' ) );
468- break ;
457+ if ( 'toggle ' === $ action ) {
458+ $ action = ( 'valid ' !== $ status ? ( 'activate ' ) : ( 'deactivate ' ) );
469459 }
470460
471461 // Call the custom API.
472- $ response = wp_remote_get ( add_query_arg ( $ api_params , $ this ->get_api_url () ) );
462+ if ( 'check ' === $ action ) {
463+ $ response = wp_remote_get ( sprintf ( '%slicense/check/%s/%s/%s/%s ' , Product::API_URL , rawurlencode ( $ this ->product ->get_name () ), $ license , rawurlencode ( home_url () ), Loader::get_cache_token () ) );
464+ } else {
465+ $ response = wp_remote_post (
466+ sprintf ( '%slicense/%s/%s/%s/%s/%s ' , Product::API_URL , $ action , rawurlencode ( $ this ->product ->get_name () ), $ license ),
467+ array (
468+ 'body ' => array (
469+ 'url ' => rawurlencode ( home_url () ),
470+ ),
471+ 'headers ' => array (
472+ 'Content-Type ' => 'application/json ' ,
473+ ),
474+ )
475+ );
476+ }
477+
473478 // make sure the response came back okay.
474479 if ( is_wp_error ( $ response ) ) {
475480 return new \WP_Error ( 'themeisle-license-500 ' , sprintf ( 'ERROR: Failed to connect to the license service. Please try again later. Reason: %s ' , $ response ->get_error_message () ) );
@@ -483,6 +488,9 @@ private function do_license_process( $license, $action = 'toggle' ) {
483488 if ( 'check ' === $ action ) {
484489 return $ license_data ;
485490 }
491+
492+ Loader::clear_cache_token ();
493+
486494 if ( ! isset ( $ license_data ->license ) ) {
487495 $ license_data ->license = 'invalid ' ;
488496 }
0 commit comments