Skip to content

Commit

Permalink
added support for devices, where you can specified the amount of lice…
Browse files Browse the repository at this point in the history
…nses per device or domain.

added custom meta-boxes for product variations.
expanded API when verifying the license, it will return extra information.
added an extra column for devices in db
  • Loading branch information
michelve committed Jul 19, 2017
1 parent 82df429 commit b378b0a
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 78 deletions.
75 changes: 60 additions & 15 deletions software-license-manager/includes/slm-api-listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ function creation_api_listener() {
SLM_API_Utility::verify_secret_key_for_creation(); //Verify the secret key first.

$slm_debug_logger->log_debug("API - license creation (slm_create_new) request received.");

//Action hook
do_action('slm_api_listener_slm_create_new');
do_action('slm_api_listener_slm_create_new');

$fields = array();
if (isset($_REQUEST['license_key']) && !empty($_REQUEST['license_key'])){
Expand All @@ -49,11 +49,17 @@ function creation_api_listener() {
$fields['email'] = strip_tags($_REQUEST['email']);
$fields['company_name'] = wp_unslash(strip_tags($_REQUEST['company_name']));
$fields['txn_id'] = strip_tags($_REQUEST['txn_id']);

if (empty($_REQUEST['max_allowed_domains'])) {
$fields['max_allowed_domains'] = $options['default_max_domains'];
} else {
$fields['max_allowed_domains'] = strip_tags($_REQUEST['max_allowed_domains']);
}
if (empty($_REQUEST['max_allowed_devices'])) {
$fields['max_allowed_devices'] = $options['default_max_devices'];
} else {
$fields['max_allowed_devices'] = strip_tags($_REQUEST['max_allowed_devices']);
}
$fields['date_created'] = isset($_REQUEST['date_created'])?strip_tags($_REQUEST['date_created']):date("Y-m-d");
$fields['date_expiry'] = isset($_REQUEST['date_expiry'])?strip_tags($_REQUEST['date_expiry']):'';

Expand Down Expand Up @@ -87,9 +93,9 @@ function activation_api_listener() {
SLM_API_Utility::verify_secret_key(); //Verify the secret key first.

$slm_debug_logger->log_debug("API - license activation (slm_activate) request received.");

//Action hook
do_action('slm_api_listener_slm_activate');
do_action('slm_api_listener_slm_activate');

$fields = array();
$fields['lic_key'] = trim(strip_tags($_REQUEST['license_key']));
Expand All @@ -106,6 +112,7 @@ function activation_api_listener() {

$sql_prep2 = $wpdb->prepare("SELECT * FROM $reg_table WHERE lic_key = %s", $key);
$reg_domains = $wpdb->get_results($sql_prep2, OBJECT);

if ($retLic) {
if ($retLic->lic_status == 'blocked') {
$args = (array('result' => 'error', 'message' => 'Your License key is blocked', 'error_code' => SLM_Error_Codes::LICENSE_BLOCKED));
Expand All @@ -127,20 +134,52 @@ function activation_api_listener() {
SLM_API_Utility::output_api_response($args);
}
}

$fields['lic_key_id'] = $retLic->id;
$wpdb->insert($reg_table, $fields);

$slm_debug_logger->log_debug("Updating license key status to active.");
$data = array('lic_status' => 'active');
$where = array('id' => $retLic->id);
$updated = $wpdb->update($tbl_name, $data, $where);

$args = (array('result' => 'success', 'message' => 'License key activated'));
SLM_API_Utility::output_api_response($args);
} else {
}
else {
$args = (array('result' => 'error', 'message' => 'Reached maximum allowable domains', 'error_code' => SLM_Error_Codes::REACHED_MAX_DOMAINS));
SLM_API_Utility::output_api_response($args);
}

if (count($reg_domains) < floor($retLic->max_allowed_domains)) {
foreach ($reg_domains as $reg_domain) {
if (isset($_REQUEST['migrate_from']) && (trim($_REQUEST['migrate_from']) == $reg_domain->registered_domain)) {
$wpdb->update($reg_table, array('registered_domain' => $fields['registered_domain']), array('registered_domain' => trim(strip_tags($_REQUEST['migrate_from']))));
$args = (array('result' => 'success', 'message' => 'Registered domain has been updated'));
SLM_API_Utility::output_api_response($args);
}
if ($fields['registered_domain'] == $reg_domain->registered_domain) {
$args = (array('result' => 'error', 'message' => 'License key already in use on ' . $reg_domain->registered_domain, 'error_code' => SLM_Error_Codes::LICENSE_IN_USE));
SLM_API_Utility::output_api_response($args);
}
}

$fields['lic_key_id'] = $retLic->id;
$wpdb->insert($reg_table, $fields);

$slm_debug_logger->log_debug("Updating license key status to active.");
$data = array('lic_status' => 'active');
$where = array('id' => $retLic->id);
$updated = $wpdb->update($tbl_name, $data, $where);

$args = (array('result' => 'success', 'message' => 'License key activated'));
SLM_API_Utility::output_api_response($args);
}
else {
$args = (array('result' => 'error', 'message' => 'Reached maximum allowable devices', 'error_code' => SLM_Error_Codes::REACHED_MAX_DOMAINS));
SLM_API_Utility::output_api_response($args);
}

} else {
$args = (array('result' => 'error', 'message' => 'Invalid license key', 'error_code' => SLM_Error_Codes::LICENSE_INVALID));
SLM_API_Utility::output_api_response($args);
Expand All @@ -156,9 +195,9 @@ function deactivation_api_listener() {
SLM_API_Utility::verify_secret_key(); //Verify the secret key first.

$slm_debug_logger->log_debug("API - license deactivation (slm_deactivate) request received.");

//Action hook
do_action('slm_api_listener_slm_deactivate');
do_action('slm_api_listener_slm_deactivate');

if (empty($_REQUEST['registered_domain'])) {
$args = (array('result' => 'error', 'message' => 'Registered domain information is missing', 'error_code' => SLM_Error_Codes::DOMAIN_MISSING));
Expand Down Expand Up @@ -192,14 +231,14 @@ function check_api_listener() {
SLM_API_Utility::verify_secret_key(); //Verify the secret key first.

$slm_debug_logger->log_debug("API - license check (slm_check) request received.");

$fields = array();
$fields['lic_key'] = trim(strip_tags($_REQUEST['license_key']));
$slm_debug_logger->log_debug("License key: " . $fields['lic_key']);

//Action hook
do_action('slm_api_listener_slm_check');

global $wpdb;
$tbl_name = SLM_TBL_LICENSE_KEYS;
$reg_table = SLM_TBL_LIC_DOMAIN;
Expand All @@ -211,12 +250,18 @@ function check_api_listener() {
$reg_domains = $wpdb->get_results($sql_prep2, OBJECT);
if ($retLic) {//A license key exists
$args = (array(
'result' => 'success',
'message' => 'License key details retrieved.',
'status' => $retLic->lic_status,
'result' => 'success',
'code' => '200',
'message' => 'License key details retrieved.',
'status' => $retLic->lic_status,
'max_allowed_domains' => $retLic->max_allowed_domains,
'max_allowed_devices' => $retLic->max_allowed_devices,
'email' => $retLic->email,
'first_name' => $retLic->first_name,
'last_name' => $retLic->last_name,
// 'until' => $retLic->until, //until what version license is supported
'registered_domains' => $reg_domains,
'license_key' => $retLic->license_key,
'date_created' => $retLic->date_created,
'date_renewed' => $retLic->date_renewed,
'date_expiry' => $retLic->date_expiry,
Expand All @@ -226,7 +271,7 @@ function check_api_listener() {
} else {
$args = (array('result' => 'error', 'message' => 'Invalid license key', 'error_code' => SLM_Error_Codes::LICENSE_INVALID));
SLM_API_Utility::output_api_response($args);
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions software-license-manager/includes/slm-api-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static function verify_secret_key() {
SLM_API_Utility::output_api_response($args);
}
}

static function verify_secret_key_for_creation() {
$slm_options = get_option('slm_plugin_options');
$right_secret_key = $slm_options['lic_creation_secret'];
Expand All @@ -35,7 +35,7 @@ static function verify_secret_key_for_creation() {
$args = (array('result' => 'error', 'message' => 'License Creation API secret key is invalid', 'error_code' => SLM_Error_Codes::CREATE_KEY_INVALID));
SLM_API_Utility::output_api_response($args);
}
}
}

static function insert_license_data_internal($fields) {
/* The fields array should have values for the following keys
Expand Down
64 changes: 64 additions & 0 deletions software-license-manager/includes/slm-meta-boxes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

// Author: Michel Velis
// Author URI: http://pilotkit.co
// Since: 3.0.0

// Add Variation Custom fields

//Display Fields in admin on product edit screen
add_action( 'woocommerce_product_after_variable_attributes', 'woo_variable_fields', 10, 3 );

//Save variation fields values
add_action( 'woocommerce_save_product_variation', 'save_variation_fields', 10, 2 );

// Create new fields for variations
function woo_variable_fields( $loop, $variation_data, $variation ) {

echo '<div class="variation-custom-fields">';

// License Field
woocommerce_wp_text_input(
array(
'id' => 'amount_of_licenses['. $loop .']',
'label' => __( 'Number of Licenses (domain)', 'woocommerce' ),
'placeholder' => '1-20',
'desc_tip' => true,
'wrapper_class' => 'form-row form-row-first',
'description' => __( 'Ideal for themes, plugins, and websites', 'woocommerce' ),
'value' => get_post_meta($variation->ID, 'amount_of_licenses', true)
)
);

echo "</div>";

echo '<div class="variation-custom-fields">';

// License Field
woocommerce_wp_text_input(
array(
'id' => 'amount_of_licenses_devices['. $loop .']',
'label' => __( 'Number of Licenses (devices)', 'woocommerce' ),
'placeholder' => '1-20',
'desc_tip' => true,
'wrapper_class' => 'form-row form-row-first',
'description' => __( 'Ideal for software and apps.', 'woocommerce' ),
'value' => get_post_meta($variation->ID, 'amount_of_licenses_devices', true)
)
);

echo "</div>";

}

/** Save new fields for variations */
function save_variation_fields( $variation_id, $i) {

// License Field
$text_field = stripslashes( $_POST['amount_of_licenses'][$i] );
update_post_meta( $variation_id, 'amount_of_licenses', esc_attr( $text_field ) );

$text_field = stripslashes( $_POST['amount_of_licenses_devices'][$i] );
update_post_meta( $variation_id, 'amount_of_licenses_devices', esc_attr( $text_field ) );

}
Loading

0 comments on commit b378b0a

Please sign in to comment.