';
-
- //If product is being edited, grab current product info
- if (isset($_GET['edit_record'])) {
- $errors = '';
- $id = $_GET['edit_record'];
- $lk_table = SLM_TBL_LICENSE_KEYS;
- $sql_prep = $wpdb->prepare("SELECT * FROM $lk_table WHERE id = %s", $id);
- $record = $wpdb->get_row($sql_prep, OBJECT);
- $license_key = $record->license_key;
- $max_domains = $record->max_allowed_domains;
- $max_devices = $record->max_allowed_devices;
- $license_status = $record->lic_status;
- $first_name = $record->first_name;
- $last_name = $record->last_name;
- $email = $record->email;
- $company_name = $record->company_name;
- $txn_id = $record->txn_id;
- $reset_count = $record->manual_reset_count;
- $purchase_id_ = $record->purchase_id_;
- $created_date = $record->date_created;
- $renewed_date = $record->date_renewed;
- $date_activated = $record->date_activated;
- $product_ref = $record->product_ref;
- $until = $record->until;
- $current_ver = $record->current_ver;
- $subscr_id = $record->subscr_id;
- $lic_type = $record->lic_type;
- $expiry_date = $record->date_expiry;
- $lic_item_ref = $record->item_reference;
- $slm_billing_length = $record->slm_billing_length;
- $slm_billing_interval = $record->slm_billing_interval;
- }
- if (isset($_POST['save_record'])) {
-
- //Check nonce
- if (!isset($_POST['slm_add_edit_nonce_val']) || !wp_verify_nonce($_POST['slm_add_edit_nonce_val'], 'slm_add_edit_nonce_action')) {
- //Nonce check failed.
- wp_die("Error! Nonce verification failed for license save action.");
+ // Get the WordPress date format
+ $slm_wp_date_format = get_option('date_format');
+ $lic_status_table = SLM_TBL_LICENSE_STATUS;
+
+ $id = !empty($_GET['edit_record']) ? intval($_GET['edit_record']) : 0; // Check for 'edit_record' parameter in the URL
+ $slm_options = get_option('slm_options');
+
+ // Check if the form is submitted
+ if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['slm_save_license'])) {
+ // Verify the nonce before processing the form
+ if (!isset($_POST['slm_nonce']) || !wp_verify_nonce($_POST['slm_nonce'], 'slm_save_license')) {
+ die(__('Security check failed', 'slmplus'));
}
- do_action('slm_add_edit_interface_save_submission');
-
- //TODO - do some validation
- $license_key = $_POST['license_key'];
- $max_domains = $_POST['max_allowed_domains'];
- $max_devices = $_POST['max_allowed_devices'];
- $license_status = $_POST['lic_status'];
- $first_name = $_POST['first_name'];
- $last_name = $_POST['last_name'];
- $email = $_POST['email'];
- $company_name = $_POST['company_name'];
- $txn_id = $_POST['txn_id'];
- $reset_count = $_POST['manual_reset_count'];
- $purchase_id_ = $_POST['purchase_id_'];
- $created_date = $_POST['date_created'];
- $renewed_date = $_POST['date_renewed'];
- $date_activated = $_POST['date_activated'];
- // $expiry_date = $_POST['date_expiry'];
- $product_ref = $_POST['product_ref'];
- $until = $_POST['until'];
- $current_ver = $_POST['current_ver'];
- $subscr_id = $_POST['subscr_id'];
- $lic_type = $_POST['lic_type'];
-
- if ("" == trim($_POST['item_reference'])) {
- $lic_item_ref = 'default';
- } else {
- $lic_item_ref = trim($_POST['item_reference']);
- }
-
-
- $slm_billing_length = trim($_POST['slm_billing_length']);
- $slm_billing_interval = trim($_POST['slm_billing_interval']);
-
- $expiry_date = '';
- if ($_POST['lic_type'] == 'lifetime') {
- $expiry_date = '0000-00-00';
+ // Sanitize and validate the input data
+ $data = [
+ 'license_key' => sanitize_text_field($_POST['license_key']),
+ 'max_allowed_domains' => intval($_POST['max_allowed_domains']),
+ 'max_allowed_devices' => intval($_POST['max_allowed_devices']),
+ 'lic_status' => sanitize_text_field($_POST['lic_status']),
+ 'first_name' => sanitize_text_field($_POST['first_name']),
+ 'last_name' => sanitize_text_field($_POST['last_name']),
+
+ // Validate email
+ 'email' => is_email($_POST['email']) ? sanitize_email($_POST['email']) : '',
+
+ 'company_name' => sanitize_text_field($_POST['company_name']),
+ 'txn_id' => sanitize_text_field($_POST['txn_id']),
+ 'manual_reset_count' => intval($_POST['manual_reset_count']),
+ 'purchase_id_' => sanitize_text_field($_POST['purchase_id_']),
+
+ // Date validation
+ 'date_created' => SLM_API_Utility::slm_validate_date($_POST['date_created']),
+ 'date_renewed' => SLM_API_Utility::slm_validate_date($_POST['date_renewed']),
+ 'date_activated' => SLM_API_Utility::slm_validate_date($_POST['date_activated']),
+
+ 'product_ref' => sanitize_text_field($_POST['product_ref']),
+ 'until' => sanitize_text_field($_POST['until']),
+ 'current_ver' => sanitize_text_field($_POST['current_ver']),
+ 'subscr_id' => sanitize_text_field($_POST['subscr_id']),
+ 'lic_type' => sanitize_text_field($_POST['lic_type']),
+
+ // Handle 'lifetime' license expiration properly
+ 'date_expiry' => ($_POST['lic_type'] == 'lifetime') ? '0000-00-00' : SLM_API_Utility::slm_validate_date($_POST['date_expiry']),
+
+ 'item_reference' => sanitize_text_field($_POST['item_reference']),
+ 'slm_billing_length' => sanitize_text_field($_POST['slm_billing_length']),
+ 'slm_billing_interval' => sanitize_text_field($_POST['slm_billing_interval']),
+ 'reminder_sent' => intval($_POST['reminder_sent']),
+
+ // Reminder date validation
+ 'reminder_sent_date' => SLM_API_Utility::slm_validate_date($_POST['reminder_sent_date'])
+ ];
+
+
+ // Check for required fields
+ if (empty($data['email']) || empty($data['date_created']) || ($data['lic_type'] != 'lifetime' && empty($data['date_expiry'])) || empty($data['lic_type'])) {
+ echo '
' . __('Required fields are missing.', 'slmplus') . '
';
} else {
- $expiry_date = $_POST['date_expiry'];
- }
-
- if (empty($created_date)) {
- $created_date = $current_date;
- }
- if (empty($renewed_date)) {
- $renewed_date = $current_date;
- }
- if (empty($expiry_date) && $lic_type !== 'lifetime') {
- $expiry_date = $current_date_plus_1year;
+ // Insert or update the data in the database
+ if ($id) {
+ $wpdb->update(SLM_TBL_LICENSE_KEYS, $data, ['id' => $id]);
+ echo '
' . __('License updated successfully.', 'slmplus') . '
';
+ } else {
+ $wpdb->insert(SLM_TBL_LICENSE_KEYS, $data);
+ echo '
' . __('License created successfully.', 'slmplus') . '
';
+ }
}
-
- //Save the entry to the database
- $fields = array();
- $fields['license_key'] = $license_key;
- $fields['max_allowed_domains'] = $max_domains;
- $fields['max_allowed_devices'] = $max_devices;
- $fields['lic_status'] = $license_status;
- $fields['first_name'] = $first_name;
- $fields['last_name'] = $last_name;
- $fields['email'] = $email;
- $fields['company_name'] = $company_name;
- $fields['txn_id'] = $txn_id;
- $fields['manual_reset_count'] = $reset_count;
- $fields['purchase_id_'] = $purchase_id_;
- $fields['date_created'] = $created_date;
- $fields['date_renewed'] = $renewed_date;
- $fields['date_activated'] = $date_activated;
- $fields['date_expiry'] = $expiry_date;
- $fields['product_ref'] = $product_ref;
- $fields['until'] = $until;
- $fields['current_ver'] = $current_ver;
- $fields['subscr_id'] = $subscr_id;
- $fields['lic_type'] = $lic_type;
- $fields['item_reference'] = $lic_item_ref;
- $fields['slm_billing_length'] = $slm_billing_length;
- $fields['slm_billing_interval'] = $slm_billing_interval;
- $id = isset($_POST['edit_record']) ? $_POST['edit_record'] : '';
- $lk_table = SLM_TBL_LICENSE_KEYS;
-
- if (empty($id)) {
- //Insert into database
- $result = $wpdb->insert($lk_table, $fields);
- $id = $wpdb->insert_id;
- if ($result === false) {
- $errors .= __('Record could not be inserted into the database!', 'softwarelicensemanager');
+ } else {
+ // If editing, load existing data
+ // Ensure the correct data types and default values for new records
+ if ($id) {
+ $license = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . SLM_TBL_LICENSE_KEYS . " WHERE id = %d", $id));
+ if ($license) {
+ $data = (array) $license;
}
} else {
- //Update record
- $where = array('id' => $id);
- $updated = $wpdb->update($lk_table, $fields, $where);
- if ($updated === false) {
- //TODO - log error
- $errors .= __('Update of the license key table failed!', 'softwarelicensemanager');
+
+ // Prepare empty data for new record
+ $data = [
+ 'license_key' => '',
+ 'max_allowed_domains' => SLM_DEFAULT_MAX_DOMAINS,
+ 'max_allowed_devices' => SLM_DEFAULT_MAX_DEVICES,
+ 'lic_status' => 'pending', // Default value
+ 'first_name' => '',
+ 'last_name' => '',
+ 'email' => '',
+ 'company_name' => '',
+ 'txn_id' => '',
+ 'manual_reset_count' => '',
+ 'purchase_id_' => '',
+ 'date_created' => date_i18n($slm_wp_date_format, strtotime('now')), // Use WP date format
+ 'date_renewed' => '0000-00-00',
+ 'date_activated' => '0000-00-00',
+ 'product_ref' => '',
+ 'until' => '',
+ 'current_ver' => '',
+ 'subscr_id' => '',
+ 'lic_type' => 'subscription',
+ 'date_expiry' => date_i18n($slm_wp_date_format, strtotime('+1 year')), // Use WP date format
+ 'item_reference' => '',
+ 'slm_billing_length' => '',
+ 'slm_billing_interval' => 'days', // Default value
+ 'reminder_sent' => '0',
+ 'reminder_sent_date' => '0000-00-00'
+ ];
+
+
+ // Generate a license key if it's a new record
+ if (!isset($editing_record)) {
+ $editing_record = new stdClass();
}
+
+ $lic_key_prefix = isset($slm_options['lic_prefix']) ? $slm_options['lic_prefix'] : '';
+ $data['license_key'] = slm_get_license($lic_key_prefix);
}
- $data = array('row_id' => $id, 'key' => $license_key);
- do_action('slm_add_edit_interface_save_record_processed', $data);
- }
-?>
-
-
- ';
}
- ?>
-
-
-
-
-
-
-
-
-
-
-
-
-
'.__('Done!', 'softwarelicensemanager').' ' .__('License was successfully generated', 'softwarelicensemanager'). '
× ';
- }
- //edit
- elseif (isset($_GET['edit_record'])) {
- echo '
'. __('Edit the information below to update your license key','softwarelicensemanager').'
';
- }
- // new
- else {
- echo '
'. __('Fill the information below to generate your license key','softwarelicensemanager').'
';
- }
- ?>
-
-
-