• Resolved Paul Gaugris

    (@bul0u)


    Hello !

    I try to use the hook in your documentation to set remote addr in activation request:

    function lmfwc_tutorial_modify_response($method, $route, $data) {
        // First check if we are on the correct route
        if ($route !== 'v2/licenses/activate/{license_key}') {
            return $data;
        }
    ?
        // Now we will save an activation object for this license/activation. The
        // activation object will be an array consisting of an ID, the proxy (if
        // The remote party is using a proxy), and the user's remote address (IP).
        // Please note that we're just using the PHP "uniqid()" method for the ID,
        // you can of course use something more appropriate.
        $activation = array(
            'id' => uniqid(),
            'http_x_forwarded_for' => $_SERVER['HTTP_X_FORWARDED_FOR'],
            'remote_addr' => $_SERVER['REMOTE_ADDR']
        );
    ?
        // Save to the license meta
        lmfwc_add_license_meta($data['id'], 'activation', $activation);
    ?
        // Add the $activation variable to the response
        $data['activation'] = $activation;
    ?
        // Return the result
        return $data;
    }
    add_filter('lmfwc_rest_api_pre_response', 'lmfwc_tutorial_modify_response', 10);

    when I fetch the endpoint i get an error 500 :

    {
        "code": "internal_server_error",
        "message": "<p>Il y a eu une erreur critique sur ce site.</p><p><a href=\"https://fr.www.ads-software.com/support/article/debugging-in-wordpress/\">En apprendre plus sur le débogage de WordPress.</a></p>",
        "data": {
            "status": 500
        },
        "additional_errors": []
    }

    Obviously the problem comes from the arguments passed to the function, can you help me?

    Thank you so much !

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Mirza Hamza

    (@hamza1010)

    Hello @bul0u,

    Thanks for contacting us,

    Hope you are doing well, We’ve informed our technical team about your issue, and they will work on it promptly. When we receive their response, we will get back to you. Our team is here to assist you.

    Thanks & Regards
    WP Experts Support Team

    Plugin Support Mirza Hamza

    (@hamza1010)

    Hello @bul0u,

    You haven’t passed the parameters correctly. Please remove the previous code and then add this code.

    Here is the code:

    function lmfwc_tutorial_modify_response($data, $method, $route) {
        // First check if we are on the correct route
        if ($route !== 'v2/licenses/activate/{license_key}') {
            return $data;
        }
    ?
        // Now we will save an activation object for this license/activation. The
        // activation object will be an array consisting of an ID, the proxy (if
        // The remote party is using a proxy), and the user's remote address (IP).
        // Please note that we're just using the PHP "uniqid()" method for the ID,
        // you can of course use something more appropriate.
        $activation = array(
            'id' => uniqid(),
            'http_x_forwarded_for' => $_SERVER['HTTP_X_FORWARDED_FOR'],
            'remote_addr' => $_SERVER['REMOTE_ADDR']
        );
    ?
        // Save to the license meta
        lmfwc_add_license_meta($data['id'], 'activation', $activation);
    ?
        // Add the $activation variable to the response
        $data['activation'] = $activation;
    ?
        // Return the result
        return $data;
    }
    add_filter('lmfwc_rest_api_pre_response', 'lmfwc_tutorial_modify_response', 10);

    If you have any questions, feel free to reach out. We’re here to assist you.

    Thank you

    Thread Starter Paul Gaugris

    (@bul0u)

    Hello !
    I tried but I still get the same 500 error in the API response.

    Plugin Support Mirza Hamza

    (@hamza1010)

    Hello @bul0u,

    We have fixed this issue. Please have a look at the screencast video.

    function lmfwc_tutorial_modify_response($data, $method, $route) {
    	
        if ( $route !== 'v2/licenses/activate/{license_key}') {
            return $data;
        }
       
        $activation = array(
            'id' => uniqid(),
            'http_x_forwarded_for' => $_SERVER['HTTP_X_FORWARDED_FOR'],
            'remote_addr' => $_SERVER['REMOTE_ADDR']
        );
    
        lmfwc_add_license_meta($data['id'], 'activation', $activation);
        $data['activation'] = $activation;
        return $data;
    }
    add_filter('lmfwc_rest_api_pre_response', 'lmfwc_tutorial_modify_response', 10, 3);
    

    The Corrected function: You have to made a change in the licenseManager\includes\Abstracts\Restcontroller.php file on line 25 in the response function.

    protected function response( $success, $data, $code, $route ) {
    	   $request_method = isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : '';
    
    	   return new WP_REST_Response(
    		  array(
    			 'success' => $success,
    			 /**
    			  * Filter lmfwc_rest_api_pre_response
    			  *
    			  * @since 1.0
    			  **/
    			 'data'    => apply_filters( 'lmfwc_rest_api_pre_response', $data, $request_method, $route ),
    		  ),
    		  $code
    	   );
    	}
    

    Here is the updated plugin: Download Plugin.

    If you have any questions, feel free to reach out. We’re here to assist you.

    Thank you

    • This reply was modified 9 months, 4 weeks ago by Mirza Hamza.
    Thread Starter Paul Gaugris

    (@bul0u)

    Perfect, it works well. Thanks for your help.

    Have a nice day !

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘lmfwc_rest_api_pre_response return 500 error’ is closed to new replies.