And i don’t understand something about the code https://www.licensemanager.at/docs/tutorials-how-to/rest-api/validating-custom-request-data
Where i will add the UUID in the database? where i will call the lmfwc_add_license_meta ?
In your example you added the UUID, but when you saved the UUID? If i made at the verification phase i will have some problems, because if i’m at the max of my activation size, and i added in the verification, i will get fail, but if i check the licence it will be good.
This is the code i’m trying to do, but with URL and not with UUID (but is the same problem, where you call the save meta function?)
<?php
/**
* Plugin Name: licence manager url validator
* Description: licence manager url validator
*/
function lmfwc_add_activate_check_url($result, $server, $request)
{
// Not our route, nothing to do...
if (strpos($request->get_route(), '/lmfwc/v2/licenses/activate') === false) {
return true;
}
// The request body was empty, or the "uuid" property is missing.
if (!$request->params || !array_key_exists('url', $request->params)) {
return new WP_Error(
'lmfwc_rest_data_error',
'The URL is missing from the request.',
array('status' => 400)
);
}
// Obtain the license key from the request URL
$licenseKey = explode('/lmfwc/v2/licenses/activate/', $request->get_route())[1];
// Retrieve the license object
$license = lmfwc_get_license($licenseKey);
// The license was not found
if (!$license) {
return new WP_Error(
'lmfwc_rest_validation_error',
'The license was not found.',
array('status' => 404)
);
}
// Check if the license key already has this UUID
$previousActivation = lmfwc_get_license_meta($license->getId(), 'activation_url');
$url = $request->params['url'];
// Throw an error if that's the case
if ($previousActivation && in_array($url, $previousActivation)) {
return new WP_Error(
'lmfwc_rest_validation_error',
'The license was already activated using this URL.',
array('status' => 403)
);
}
// TODO: validate number of licence available
lmfwc_add_license_meta($license->getId(), 'activation_url', $url);
// Hasn't been used before, proceed
return true;
}
add_filter('lmfwc_rest_api_validation', 'lmfwc_add_activate_check_url', 11, 3);
function lmfwc_validate_check_url($result, $server, $request)
{
if (strpos($request->get_route(), '/lmfwc/v2/licenses/validate') === false) {
return true;
}
if (!$request->params || !array_key_exists('url', $request->params)) {
return new WP_Error(
'lmfwc_rest_data_error',
'The URL is missing from the request.',
array('status' => 400)
);
}
$url = $request->params['url'];
// Obtain the license key from the request URL
$licenseKey = explode('/lmfwc/v2/licenses/validate/', $request->get_route())[1];
$license = lmfwc_get_license($licenseKey);
// The license was not found
if (!$license) {
return new WP_Error(
'lmfwc_rest_validation_error',
'The license was not found.',
array('status' => 404)
);
}
$previousActivation = lmfwc_get_license_meta($license->getId(), 'activation_url', true);
if ($previousActivation && $previousActivation !== $url) {
return new WP_Error(
'lmfwc_rest_validation_error',
'The license is not for this site URL.',
array('status' => 403)
);
}
return true;
}
add_filter('lmfwc_rest_api_validation', 'lmfwc_tutorial_check_url', 10, 3);
-
This reply was modified 3 years, 4 months ago by nicearma.