Allow Staging / Development domain activation, regardless of active Instances per License Key

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPrint this page

When setting up the Licensing information for a Product, an Instance per License Key field is available to provide the number of domains that can be assigned towards a license key. This can be from 1 to unlimited.

When using a limited number of active domains, a Staging, Development or a Test may be required to not count towards an active instance. This is achievable programmatically using a filter.
For a domain to be considered as a staging or development, it must match a specific format. The following patterns can be used to determine if applys:

  • dev.
  • .dev
  • staging.
  • .local
  • localhost
  • test.
  • .test

The following code is using the above formats to make a comparison against the provided domain, inquired for a license key activation. The code should be placed within a custom file on /wp-content/mu-plugins/.

    add_filter ('WOOSL/API_call/activate/can_register_domain', '_woosl_api__can_register_domain', 10, 4 );
    function _woosl_api__can_register_domain( $can_register_domain_to_license_key, $domain, $key_instances, $max_instances_per_key )
        {
            //If already allowing more instances, just return
            if ( $can_register_domain_to_license_key )
                return TRUE;
             
            //check if the domain is actually a development
            $is_development_domain  =   TRUE;
            $regex_domain_development_check =   '/(^dev\.|\.dev$|^stage\.|^staging\.|\.local$|localhost|^test\.|\.test$)/i';   
            if ( preg_match( $regex_domain_development_check, $domain ) !== 1 )
                $is_development_domain  =   FALSE;
             
            //count the currently registered instances
            $count_production_instances     =   0;
            $count_dev_instances            =   0;
            
            foreach ( $key_instances    as  $key_instance )
                {
                    if ( preg_match( $regex_domain_development_check, $key_instance->active_domain ) !== 1 )
                        $count_production_instances +=  1;
                        else
                        $count_dev_instances    +=  1;
                }
            
            //We grant a single development one.
            if ( $is_development_domain &&  $count_dev_instances    <   1 )
                return TRUE;  
            
            if (    ! $is_development_domain    &&  $count_production_instances <   $max_instances_per_key )
                return TRUE;                   
                 
            return FALSE;   
        }

Accordingly to the above code logic, it allows a single staging/development domain registration. If already registered such a domain, it will not allow access for another activation.
In case of required unlimited activation, the code can be changed to the following:

    add_filter ('WOOSL/API_call/activate/can_register_domain', '_woosl_api__can_register_domain', 10, 4 );
    function _woosl_api__can_register_domain( $can_register_domain_to_license_key, $domain, $key_instances, $max_instances_per_key )
        {
            //If already allowing more instances, just return
            if ( $can_register_domain_to_license_key )
                return TRUE;
            
            //check if the domain is actually a development
            $regex_domain_development_check =   '/(^dev\.|\.dev$|^stage\.|^staging\.|\.local$|localhost|^test\.|\.test$)/i';   
            if ( preg_match( $regex_domain_development_check, $domain ) !== 1 )
                return FALSE;
            
            return TRUE;  
        }


By woocommerce-sl, posted on September 29, 2022

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments