WOOSL/API_call/early_response

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

Type
Filter

Arguments
(array) $response
(array) $args
(class) $api_object

Description
Allows filtering the API response by adding a custom response item on top of everything else.

Observation
The cache module need to be off for the filter to trigger.

Example of usage
Return additional response with a timestamp call, on top of the other API messages.

    add_filter( 'WOOSL/API_call/early_response', 'WOOSL_API_call_early_response', 10, 3 );
    function WOOSL_API_call_early_response ( $response, $args, $object )
        {
            
            $response[] =   array ( 
                                    'status'    =>  'success',
                                    'timestamp' =>  time(),
                                    'message'   =>  'API call time'
                                        );
               
            return $response;   
        }

The code should be inserted within the /wp-content/mu-plugins/woosl-custom.php

Example of usage
The following code example overwrite the default API ‘deactivate’ method with a custom one. That removes all instances for a license key, if no domain argument is provided through the API call.

    add_filter( 'WOOSL/API_call/early_response', 'my_custom_deactivate', 10, 3 );
    add_filter( 'WOOSL/API_call/do_action', 'my_custom_do_action', 1, 4 );
    function my_custom_deactivate( $response, $args, $api_object )
        {
            $action = isset ( $args['woo_sl_action'] ) ? preg_replace("/[^a-zA-Z0-9-_]/i", "", $args['woo_sl_action'] ) : '';
            if ( $action !== 'deactivate' )
                return FALSE;
            
            $product_unique_id  = isset($args['product_unique_id']) ?   preg_replace("/[^a-zA-Z0-9.\-\_ ]/", "", $args['product_unique_id'] )       : '';
            $licence_key        = isset($args['licence_key'])       ?   preg_replace("/[^a-zA-Z0-9.\-\_\: ]/", "", $args['licence_key'] )             : '';
            $domain             = isset($args['domain']) ? $api_object->clean_machine_hash( preg_replace("/[^a-zA-Z0-9.\-\_\/ ]/", "", $args['domain'] ) )   : '';
            
            $product_id = $api_object->product_validation($licence_key, $product_unique_id);
            if($product_id === FALSE)
                return FALSE;
                
            if ( ! $api_object->order_status_validation ( $licence_key, $product_unique_id ) )
                return FALSE;
            
            $response   =   [];
            
            //check against the licence key validation
            if ( ! empty ( $domain ) )
                {
                    $licence_key_data = $api_object->functions->get_licence_key_data_by_licence_key( $licence_key, array ( 'domain'  =>  $domain ) );
                                                
                    if (    !   isset($licence_key_data->id))
                        {
                            $response[]   =   array(
                                                            'status'       => 'error',
                                                            'status_code'  => 'e110',
                                                            'message'      => 'Invalid Licence Key or Licence Key not Active for Domain'
                                                                );
                            return $response;
                        }
                                   
                    global $wpdb;
                    
                    $api_object->functions->key_domain_remove($licence_key_data);
                    $response[]   =   array(
                                                            'status'       => 'success',
                                                            'status_code'  => 's201',
                                                            'message'      => 'Licence Key Successfully Unassigned'
                                                                );
                }
                else
                {
                    $licence_key_data           =   $api_object->functions->get_licence_key_data_by_licence_key( $licence_key, array ( ) );
                    $license_key_instances      =   $api_object->functions->get_license_key_active_instances( $licence_key_data->licence, $licence_key_data->order_item_id );
                    
                    foreach ( $license_key_instances    as  $license_key_instance )
                        {
                            $api_object->functions->key_domain_remove( $license_key_instance );    
                        }
                        
                    $response[]   =   array(
                                                            'status'       => 'success',
                                                            'status_code'  => 's201',
                                                            'message'      => 'Licence Key Successfully Unassigned'
                                                                );
                }
            
            return $response;
       
        }


    function my_custom_do_action ( $status, $response, $args, $api_object )
        {
            $action = isset ( $args['woo_sl_action'] ) ? preg_replace("/[^a-zA-Z0-9-_]/i", "", $args['woo_sl_action'] ) : '';
            if ( $action === 'deactivate' )
                return FALSE;
            
            return $status;    
        }

The code should be inserted within the /wp-content/mu-plugins/woosl-custom.php


By woocommerce-sl, posted on January 27, 2021

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