diff --git a/LICENSE.md b/LICENSE.md index e088db5..26c64ef 100755 --- a/LICENSE.md +++ b/LICENSE.md @@ -652,7 +652,7 @@ Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: - Software License Manager Copyright (C) 2020 Michel Velis + SLM Plus Copyright (C) 2020 Michel Velis This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. diff --git a/README.md b/README.md index 4b3be81..9f56b7e 100755 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ -# Welcome to software license manager 👋 +# Welcome to SLM Plus 👋 ![Version](https://img.shields.io/badge/version-5.6.1-blue.svg?cacheSeconds=2592000) [![Documentation](https://img.shields.io/badge/documentation-yes-brightgreen.svg)](https://documenter.getpostman.com/view/307939/6tjU1FL?version=latest) @@ -78,23 +78,23 @@ Changelog: [View changelog](https://github.com/michelve/software-license-manager ## 🎑 Screenshots -Software License Manager +SLM Plus -Software License Manager +SLM Plus -Software License Manager +SLM Plus -Software License Manager +SLM Plus -Software License Manager +SLM Plus -Software License Manager +SLM Plus -Software License Manager +SLM Plus -Software License Manager +SLM Plus ## 📝 License diff --git a/admin/includes/partials/stats.php b/admin/includes/partials/stats.php index ae67a8b..b78e19a 100644 --- a/admin/includes/partials/stats.php +++ b/admin/includes/partials/stats.php @@ -5,7 +5,7 @@
- +
@@ -15,7 +15,7 @@
- +
@@ -25,7 +25,7 @@
- +
@@ -35,7 +35,7 @@
- +
@@ -45,7 +45,7 @@
- +
@@ -55,7 +55,7 @@
- +
@@ -65,7 +65,7 @@
- +
@@ -75,7 +75,7 @@
- +
@@ -85,7 +85,7 @@
- +
@@ -95,7 +95,7 @@
- +
diff --git a/admin/includes/wpestores/slm-wpestores.php b/admin/includes/wpestores/slm-wpestores.php index 5bbb3dd..5698ff7 100755 --- a/admin/includes/wpestores/slm-wpestores.php +++ b/admin/includes/wpestores/slm-wpestores.php @@ -188,7 +188,7 @@ function slm_estore_product_configuration_html($product_config_html, $prod_id) } } - $product_config_html .= '
Software License Manager Plugin (Click to Expand)
'; + $product_config_html .= '
SLM Plus Plugin (Click to Expand)
'; $product_config_html .= ' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Maximum Allowed Domains'; $product_config_html .= ''; diff --git a/admin/slm-add-licenses.php b/admin/slm-add-licenses.php index 8c2cebe..f7345f7 100755 --- a/admin/slm-add-licenses.php +++ b/admin/slm-add-licenses.php @@ -1,749 +1,300 @@ '; - // echo '

Add/Edit Licenses

'; - echo '
'; - - //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 ''; - } - // new - else { - echo ''; - } - ?> -
-
-
-
-
-
- -
-
-
- -
-
-
- - -
-
- -
- - -
-
-
"> - '; - } else { - if (!isset($editing_record)) { - $editing_record = new stdClass(); - } - $lic_key_prefix = $slm_options['lic_prefix']; - - if (!empty($lic_key_prefix)) { - $license_key = slm_get_license($lic_key_prefix); - } else { - $license_key = slm_get_license($lic_key_prefix); - } - } - ?> -
-
-
- -
- -
-

-
-
- -
- - - -
-
-
- - -
- -
- - - -
-
-
-
-
-
- -
-
- -
- -
-

-
-
- -
-
- - - -
- -
- - - -
-
-
- -
-
- - - -
- - -
- - - - - - - -
-
- -
- - - -
-
-
- -
-
- -
-
-
- -
-

-
-
-
-
-
- - - - -
-
- - - - -
-
-
-
-
-
- -
- -
-
- -
-

-
-
- -
- - - - -
-
-
-
- -
-
-
-
-
-
-
- - - -
-
- -
-
- - - -
-
-
-
-
-
-
- - - - -
-
- - - - - - - - - - - -
- -
- - - -
- -
- - - -
-
-
-
-
-
- -
- -
-
- -
-

-
-
- -
- - - -
- -
-
- - - -
- -
- - - - - - - -
-
-
-
-
- - - -
-
- - - -
-
-
- prefix . 'postmeta'; - $item_ref_meta = '_license_item_reference'; - $sql_prep = $wpdb->prepare("SELECT DISTINCT(meta_value) FROM $post_meta_tbl WHERE meta_key = %s", $item_ref_meta); - $values_item_refs = $wpdb->get_results($sql_prep, OBJECT); - ?> -
-
- - - -
-
- -
-
-
-
- -
- -
- -
-

-
-
- -
-
-
' . json_encode($lic_info, JSON_PRETTY_PRINT) . '
'; - ?> - -
-
-
- -
-
- -
-

-
- -
- -
- -
-
- -
- $id, 'key' => $license_key); - $extra_output = apply_filters('slm_add_edit_interface_above_submit', '', $data); - if (!empty($extra_output)) { - echo $extra_output; - } - ?> -
-
- - - -
-
-
-
- -
-
-
- -
-
-
-
-
-
-
-
-
-
- - - + echo ''; + ?> +
+

+
+ +
+

+
+

+
+

+
+

+
+

+
+

+
+

+
+ + +
'; - echo '

slm logo'. __('Admin Tools', 'softwarelicensemanager').'

'; + echo '

'. __('Admin Tools', 'slmplus') . '

'; echo '
'; - $slm_options = get_option('slm_plugin_options'); - if (isset($_POST['send_deactivation_request'])) { - $postURL = $_POST['slm_deactivation_req_url']; - $secretKeyForVerification = $slm_options['lic_verification_secret']; - $data = array(); - $data['secret_key'] = $secretKeyForVerification; + $postURL = esc_url_raw($_POST['slm_deactivation_req_url']); + $secretKeyForVerification = slm_get_option('lic_verification_secret'); + $data = array('secret_key' => $secretKeyForVerification); $ch = curl_init($postURL); curl_setopt($ch, CURLOPT_POST, true); @@ -27,51 +27,170 @@ function slm_admin_tools_menu() $msg = ""; if ($returnValue == "Success") { - $msg .= "Success message returned from the remote host."; + $msg .= __('Success message returned from the remote host.', 'slmplus'); } echo '

'; - echo 'Request sent to the specified URL!'; - echo '
' . $msg; + echo __('Request sent to the specified URL!', 'slmplus'); + echo '
' . esc_html($msg); echo '

'; } + if (isset($_POST['slm_clear_log'])) { global $wpdb, $slm_debug_logger; $table = SLM_TBL_LIC_LOG; - $slm_log_clean = $wpdb->query("TRUNCATE TABLE $table"); - $slm_logger = new SLM_Debug_Logger(); + $wpdb->query("TRUNCATE TABLE $table"); $slm_debug_logger->reset_log_file("log.txt"); $slm_debug_logger->reset_log_file("log-cron-job.txt"); - echo '

Log was cleared succesfully!

'; + + echo '

' . esc_html__('Log was cleared successfully!', 'slmplus') . '

'; } -?> + + ?>
-

+

-
-

+ +
+ +
+
+
+
- +
+

+
+

+
- +
-

+

-

+

- +
+ + ' . __('Last backup created on: ', 'slmplus') . $backup_date . ' - ' . __('Download Backup', 'slmplus') . '

'; + } + ?>
-
'; echo '
'; } + +/** + * Generates or retrieves a unique hash for the backup directory. + */ +function slm_get_unique_hash() +{ + $hash = slm_get_option('slm_backup_dir_hash'); + + if (!$hash) { + $hash = wp_generate_password(8, false, false); // Generate random 8-character hash + slm_update_option('slm_backup_dir_hash', $hash); + } + + return $hash; +} + +/** + * Saves a backup of the plugin's database tables in a secure folder. + */ +function slm_save_backup_to_uploads() +{ + global $wpdb; + + // Get the upload directory + $upload_dir = wp_upload_dir(); + $unique_hash = slm_get_unique_hash(); // Generate or retrieve the unique hash + $slm_backup_dir = $upload_dir['basedir'] . $unique_hash; + + // Create the slm-plus folder with hash if it doesn't exist + if (!file_exists($slm_backup_dir)) { + wp_mkdir_p($slm_backup_dir); + } + + // Set backup file name and path + $backup_file = $slm_backup_dir . '/slm_plugin_backup_' . date('Y-m-d_H-i-s') . '.sql'; + + // Get plugin tables + $backup_tables = [ + SLM_TBL_LICENSE_KEYS, + SLM_TBL_LIC_DOMAIN, + SLM_TBL_LIC_DEVICES, + SLM_TBL_LIC_LOG, + SLM_TBL_EMAILS, + SLM_TBL_LICENSE_STATUS + ]; + + $sql = ""; + foreach ($backup_tables as $table) { + // Get table structure + $create_table_query = $wpdb->get_results("SHOW CREATE TABLE $table", ARRAY_N); + $sql .= "\n\n" . $create_table_query[0][1] . ";\n\n"; + + // Get table data + $rows = $wpdb->get_results("SELECT * FROM $table", ARRAY_A); + foreach ($rows as $row) { + $values = array_map('esc_sql', array_values($row)); // Use esc_sql to escape the values + $values = "'" . implode("','", $values) . "'"; + $sql .= "INSERT INTO $table VALUES ($values);\n"; + } + } + + // Save the SQL to a file in the slm-plus folder + if (file_put_contents($backup_file, $sql)) { + $backup_url = $upload_dir['baseurl'] . $unique_hash . '/' . basename($backup_file); + + // Save backup info in plugin options + $backup_info = [ + 'url' => $backup_url, + 'date' => date('Y-m-d H:i:s') + ]; + slm_update_option('slm_last_backup_info', $backup_info); + + echo '

' . __('Backup created successfully! Download from: ', 'slmplus') . '' . esc_html(basename($backup_file)) . '

'; + } else { + echo '

' . __('Error: Failed to create the backup file.', 'slmplus') . '

'; + } +} + + +/** + * Retrieves an option from the slm_plugin_options. + */ +function slm_get_option($key) +{ + $options = get_option('slm_plugin_options', []); + return $options[$key] ?? null; +} + +/** + * Updates or adds an option to the slm_plugin_options. + */ +function slm_update_option($key, $value) +{ + $options = get_option('slm_plugin_options', []); + $options[$key] = $value; + update_option('slm_plugin_options', $options); +} diff --git a/admin/slm-dashboard-widgets.php b/admin/slm-dashboard-widgets.php index 9b5c950..3739233 100644 --- a/admin/slm-dashboard-widgets.php +++ b/admin/slm-dashboard-widgets.php @@ -19,7 +19,7 @@ function slm_add_dashboard_widgets() wp_add_dashboard_widget( 'slm_dashboard_widget', // Widget slug. - 'Software license manager', // Title. + 'SLM Plus', // Title. 'slm_dashboard_widget_function' // Display function. ); } @@ -27,7 +27,7 @@ function slm_add_dashboard_widgets() function add_toolbar_items($admin_bar){ $admin_bar->add_menu(array( 'id' => 'slm-menu', - 'title' => '' . __('SLM', 'softwarelicensemanager'), + 'title' => '' . __('SLM', 'slmplus'), 'href' => admin_url('admin.php?page=slm_overview'), 'meta' => array( 'title' => __('SLM'), diff --git a/admin/slm-integration-help-page.php b/admin/slm-integration-help-page.php index c8647a7..2f74473 100755 --- a/admin/slm-integration-help-page.php +++ b/admin/slm-integration-help-page.php @@ -1,14 +1,14 @@ -

License Manager Integration Help v

+

v

+
-

API Settings

+

The License API Query POST URL For Your Installation"; - echo '
'; - echo "
The License Activation or Deactivation API secret key"; - echo '
'; - echo "
The License Creation API secret key"; - echo '
'; - ?> + $slm_options = get_option('slm_plugin_options'); + $slm_creation_secret_key = esc_attr($slm_options['lic_creation_secret']); + $slm_secret_verification_key = esc_attr($slm_options['lic_verification_secret']); + $slm_api_query_post_url = esc_url(SLM_SITE_HOME_URL); + + echo "
" . esc_html__('The License API Query POST URL For Your Installation', 'slmplus') . ""; + echo '
'; + + echo "
" . esc_html__('The License Activation or Deactivation API Secret Key', 'slmplus') . ""; + echo '
'; + echo "
" . esc_html__('The License Creation API Secret Key', 'slmplus') . ""; + echo '
'; + ?>
-

Documentation and guides: check out postman demos

+

+ +

-

Error codes and constants

+

- +
- - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ConstantError code
CREATE_FAILED10
CREATE_KEY_INVALID100
DOMAIN_ALREADY_INACTIVE 80
DOMAIN_MISSING 70
KEY_CANCELED 130
KEY_CANCELED_FAILED 140
KEY_DEACTIVATE_DOMAIN_SUCCESS 360
KEY_DEACTIVATE_SUCCESS 340
KEY_DELETE_FAILED 300
KEY_DELETE_SUCCESS320
KEY_DELETED 130
KEY_UPDATE_FAILED 220
KEY_UPDATE_SUCCESS240
LICENSE_ACTIVATED 380
LICENSE_BLOCKED20
LICENSE_CREATED400
LICENSE_EXIST200
LICENSE_EXPIRED30
LICENSE_IN_USE 40
LICENSE_INVALID60
MISSING_KEY_DELETE_FAILED 280
MISSING_KEY_UPDATE_FAILED 260
REACHED_MAX_DEVICES 120
REACHED_MAX_DOMAINS 50
VERIFY_KEY_INVALID90
CREATE_FAILED10
CREATE_KEY_INVALID100
DOMAIN_ALREADY_INACTIVE80
DOMAIN_MISSING70
KEY_CANCELED130
KEY_CANCELED_FAILED140
KEY_DEACTIVATE_DOMAIN_SUCCESS360
KEY_DEACTIVATE_SUCCESS340
KEY_DELETE_FAILED300
KEY_DELETE_SUCCESS320
KEY_DELETED130
KEY_UPDATE_FAILED220
KEY_UPDATE_SUCCESS240
LICENSE_ACTIVATED380
LICENSE_BLOCKED20
LICENSE_CREATED400
LICENSE_EXIST200
LICENSE_EXPIRED30
LICENSE_IN_USE40
LICENSE_INVALID60
MISSING_KEY_DELETE_FAILED280
MISSING_KEY_UPDATE_FAILED260
REACHED_MAX_DEVICES120
REACHED_MAX_DOMAINS50
VERIFY_KEY_INVALID90
reset_log_file("log.txt"); $slm_debug_logger->reset_log_file("log-cron-job.txt"); - echo '

- ' . __('Debug log files have been reset!', 'softwarelicensemanager') . ' -

'; + + echo '

' . __('Debug log files have been reset!', 'slmplus') . '

'; } if (isset($_POST['slm_save_settings'])) { - - if (!is_numeric($_POST["default_max_domains"])) { - //Set it to one by default if incorrect value is entered - $_POST["default_max_domains"] = '2'; - } - if (!is_numeric($_POST["default_max_devices"])) { - //Set it to one by default if incorrect value is entered - $_POST["default_max_devices"] = '2'; - } + // Sanitize and validate numeric values for default max domains and devices + $default_max_domains = max((int) $_POST['default_max_domains'], 2); + $default_max_devices = max((int) $_POST['default_max_devices'], 2); $options = array( - 'lic_creation_secret' => trim($_POST["lic_creation_secret"]), - 'lic_prefix' => trim($_POST["lic_prefix"]), - 'default_max_domains' => trim($_POST["default_max_domains"]), - 'default_max_devices' => trim($_POST["default_max_devices"]), - 'lic_verification_secret' => trim($_POST["lic_verification_secret"]), - 'enable_auto_key_expiration' => isset($_POST['enable_auto_key_expiration']) ? '1' : '', - 'enable_debug' => isset($_POST['enable_debug']) ? '1' : '', - 'slm_woo' => isset($_POST['slm_woo']) ? '1' : '', - 'slm_woo_downloads' => isset($_POST['slm_woo_downloads']) ? '1' : '', - 'slm_woo_affect_downloads' => isset($_POST['slm_woo_affect_downloads']) ? '1' : '', - 'slm_stats' => isset($_POST['slm_stats']) ? '1' : '', - 'slm_adminbar' => isset($_POST['slm_adminbar']) ? '1' : '', - 'slm_conflictmode' => isset($_POST['slm_conflictmode']) ? '1' : '', - 'slm_front_conflictmode' => isset($_POST['slm_front_conflictmode']) ? '1' : '', - 'slm_wpestores' => isset($_POST['slm_wpestores']) ? '1' : '', - 'slm_dl_manager' => isset($_POST['slm_dl_manager']) ? '1' : '', - 'slm_multiple_items' => isset($_POST['slm_multiple_items']) ? '1' : '', - 'allow_user_activation_removal' => isset($_POST['allow_user_activation_removal']) ? '1' : '', - 'expiration_reminder_text' => sanitize_text_field($_POST['expiration_reminder_text']) + 'lic_creation_secret' => trim($_POST['lic_creation_secret']), + 'lic_prefix' => trim($_POST['lic_prefix']), + 'default_max_domains' => $default_max_domains, + 'default_max_devices' => $default_max_devices, + 'lic_verification_secret' => trim($_POST['lic_verification_secret']), + 'enable_auto_key_expiration' => isset($_POST['enable_auto_key_expiration']), + 'enable_debug' => isset($_POST['enable_debug']), + 'slm_woo' => isset($_POST['slm_woo']), + 'slm_woo_downloads' => isset($_POST['slm_woo_downloads']), + 'slm_woo_affect_downloads' => isset($_POST['slm_woo_affect_downloads']), + 'slm_stats' => isset($_POST['slm_stats']), + 'slm_adminbar' => isset($_POST['slm_adminbar']), + 'slm_conflictmode' => isset($_POST['slm_conflictmode']), + 'slm_front_conflictmode' => isset($_POST['slm_front_conflictmode']), + 'slm_wpestores' => isset($_POST['slm_wpestores']), + 'slm_dl_manager' => isset($_POST['slm_dl_manager']), + 'slm_multiple_items' => isset($_POST['slm_multiple_items']), + 'allow_user_activation_removal' => isset($_POST['allow_user_activation_removal']), + 'expiration_reminder_text' => sanitize_text_field($_POST['expiration_reminder_text']), ); - update_option('slm_plugin_options', $options); - - echo '

' . __('Options updated!', 'softwarelicensemanager') . '

'; - } - - $options = get_option('slm_plugin_options'); - $secret_key = $options['lic_creation_secret']; - if (empty($secret_key)) { - //$secret_key = md5(uniqid('', true)); - $secret_key = SLM_Utility::create_secret_keys(); + update_option('slm_plugin_options', $options); + echo '

' . __('Options updated!', 'slmplus') . '

'; } - $secret_verification_key = $options['lic_verification_secret']; - if (empty($secret_verification_key)) { - //$secret_verification_key = md5(uniqid('', true)); - $secret_verification_key = SLM_Utility::create_secret_keys(); - } - $tab = ""; //Initialization value; - if (isset($_REQUEST['tab'])) { - $tab = $_REQUEST['tab']; - } else { - $tab = 'general_settings'; - } + $options = get_option('slm_plugin_options'); + $secret_key = !empty($options['lic_creation_secret']) ? $options['lic_creation_secret'] : SLM_Utility::create_secret_keys(); + $secret_verification_key = !empty($options['lic_verification_secret']) ? $options['lic_verification_secret'] : SLM_Utility::create_secret_keys(); + $tab = isset($_REQUEST['tab']) ? $_REQUEST['tab'] : 'general_settings'; ?>
-

+

+ + ' . esc_html(get_bloginfo('name')) . ' + + +
+
+ + + + + + +
+ + + + + + + +
+
+
+ +
+

License key was activated successfully!

+

' . wp_kses_post($message) . '

+

Regards,

+
+

+ ' . esc_html(get_bloginfo('name')) . ' +
+ ' . esc_html(get_bloginfo('admin_email')) . ' +

+
+
+
+
+ + +
+
+
+
+ + '; - $template = ' Epikly



License key was activated successfully !

' . $message . '

Regards,

' . get_bloginfo('name') . '
' . get_bloginfo('admin_email') . '

'; return $template; } - static function count_licenses($status) + public static function count_licenses($status) { global $wpdb; $license_table = SLM_TBL_LICENSE_KEYS; - $get_lic_status = $wpdb->get_var("SELECT COUNT(*) FROM $license_table WHERE lic_status = '" . $status . "'"); + + // Sanitize input + $status = sanitize_text_field($status); + + // Prepare the SQL statement + $query = $wpdb->prepare("SELECT COUNT(*) FROM $license_table WHERE lic_status = %s", $status); + $get_lic_status = $wpdb->get_var($query); + return $get_lic_status; } - static function slm_get_icon_url($size, $filename) + + public static function slm_get_icon_url($size, $filename) { + // Sanitize inputs + $size = sanitize_text_field($size); + $filename = sanitize_file_name($filename); + return SLM_ASSETS_URL . 'icons/' . $size . '/' . $filename; } - static function count_logrequest() + public static function count_logrequest() { global $wpdb; $license_table = SLM_TBL_LIC_LOG; + $getlogs = $wpdb->get_var("SELECT COUNT(*) FROM $license_table"); return $getlogs; } - static function count_emailsent() + public static function count_emailsent() { global $wpdb; $license_table = SLM_TBL_EMAILS; + $getlogs = $wpdb->get_var("SELECT COUNT(*) FROM $license_table"); return $getlogs; } - static function getstats_licenses($date_created, $interval) + public static function getstats_licenses($date_created, $interval) { global $wpdb; $license_table = SLM_TBL_LICENSE_KEYS; - $query = $wpdb->get_var("SELECT COUNT(*) FROM $license_table WHERE $date_created >= DATE_ADD(CURDATE(), INTERVAL -" . $interval . " DAY)"); - return $query; + + // Sanitize inputs + $date_created = sanitize_text_field($date_created); + $interval = intval($interval); + + $query = $wpdb->prepare( + "SELECT COUNT(*) FROM $license_table WHERE $date_created >= DATE_ADD(CURDATE(), INTERVAL -%d DAY)", + $interval + ); + + return $wpdb->get_var($query); } - static function get_total_licenses() + public static function get_total_licenses() { global $wpdb; $license_table = SLM_TBL_LICENSE_KEYS; - $license_count = $wpdb->get_var("SELECT COUNT(*) FROM " . $license_table . ""); - return $license_count; + + $license_count = $wpdb->get_var("SELECT COUNT(*) FROM $license_table"); + return $license_count; } - static function get_lic_expiringsoon() + public static function get_lic_expiringsoon() { global $wpdb; $license_table = SLM_TBL_LICENSE_KEYS; - $license_count = $wpdb->get_var("SELECT COUNT(*) FROM $license_table WHERE date_expiry BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 1 MONTH ) AND DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH );"); - return $license_count; + + $license_count = $wpdb->get_var( + "SELECT COUNT(*) FROM $license_table WHERE date_expiry BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND CURDATE()" + ); + + return $license_count; } - static function block_license_key_by_row_id($key_row_id) + public static function block_license_key_by_row_id($key_row_id) { global $wpdb; $license_table = SLM_TBL_LICENSE_KEYS; - //Now, delete the key from the licenses table. + + // Sanitize input + $key_row_id = intval($key_row_id); + $wpdb->update($license_table, array('lic_status' => 'blocked'), array('id' => $key_row_id)); } - static function expire_license_key_by_row_id($key_row_id) + public static function expire_license_key_by_row_id($key_row_id) { global $wpdb; $license_table = SLM_TBL_LICENSE_KEYS; - //Now, delete the key from the licenses table. + // Sanitize input + $key_row_id = intval($key_row_id); + $wpdb->update($license_table, array('lic_status' => 'expired'), array('id' => $key_row_id)); } - static function active_license_key_by_row_id($key_row_id) + public static function active_license_key_by_row_id($key_row_id) { global $wpdb; $license_table = SLM_TBL_LICENSE_KEYS; - $current_date = wp_date('Y/m/d'); - // 'lic_status' => ''. $current_date.'' + $current_date = current_time('Y-m-d'); + + // Sanitize input + $key_row_id = intval($key_row_id); $wpdb->update($license_table, array('lic_status' => 'active'), array('id' => $key_row_id)); - $wpdb->update($license_table, array('date_activated' => '' . $current_date . ''), array('id' => $key_row_id)); + $wpdb->update($license_table, array('date_activated' => $current_date), array('id' => $key_row_id)); } /* @@ -503,7 +774,7 @@ static function get_subscriber_licenses() ' . $slm_user["id"] . ' ' . $slm_user["license_key"] . ' ' . $slm_user["lic_status"] . ' - '. __(' view', 'softwarelicensemanager'). ' + '. __(' view', 'slmplus'). ' '; } } @@ -515,7 +786,7 @@ static function get_lic_activity($license_key) $slm_log_table = SLM_TBL_LIC_LOG; echo ' -
+
'. __('ID', 'softwarelicensemanager'). ' '. __('Request', 'softwarelicensemanager'). '
'; $activity = $wpdb->get_results("SELECT * FROM " . $slm_log_table . " WHERE license_key='" . $license_key . "';"); foreach ($activity as $log) { @@ -523,8 +794,8 @@ static function get_lic_activity($license_key) ' . '' . ' + '

'. __('Source:', 'slmplus'). ' ' . $log->source . + '

'. __('Time:', 'slmplus'). ' ' . $log->time . '

'; } echo ' @@ -570,7 +841,7 @@ static function get_license_activation($license_key, $tablename, $item_name, $ac
'. __('ID', 'slmplus'). ' '. __('Request', 'slmplus'). '
' . $log->id . ' ' . $log->slm_action . '' . - '

'. __('Source:', 'softwarelicensemanager'). ' ' . $log->source . - '

'. __('Time:', 'softwarelicensemanager'). ' ' . $log->time . '

- '.__('Not registered yet', 'softwarelicensemanager').'
'; ?> + '.__('Not registered yet', 'slmplus').''; ?> is_type('slm_license')) { $tabs['shipping'] = array( - 'title' => __('License information', 'softwarelicensemanager'), + 'title' => __('License information', 'slmplus'), 'priority' => 50, 'callback' => 'slm_woo_tab_lic_info' ); @@ -600,11 +871,11 @@ function slm_woo_tab_lic_info() { global $product; // The new tab content - echo '

'.__('License information', 'softwarelicensemanager') .'

'; - echo __('License type: ', 'softwarelicensemanager') . get_post_meta($product->get_id(), '_license_type', true) . '
'; - echo __('Domains allowed: ', 'softwarelicensemanager') . get_post_meta($product->get_id(), '_domain_licenses', true) . '
'; - echo __('Devices allowed: ', 'softwarelicensemanager') . get_post_meta($product->get_id(), '_devices_licenses', true) . '
'; - echo __('Renews every ', 'softwarelicensemanager') . get_post_meta($product->get_id(), '_license_renewal_period', true) . ' ' . get_post_meta($product->get_id(), '_license_renewal_period_term', true) . '
'; + echo '

'.__('License information', 'slmplus') .'

'; + echo __('License type: ', 'slmplus') . get_post_meta($product->get_id(), '_license_type', true) . '
'; + echo __('Domains allowed: ', 'slmplus') . get_post_meta($product->get_id(), '_domain_licenses', true) . '
'; + echo __('Devices allowed: ', 'slmplus') . get_post_meta($product->get_id(), '_devices_licenses', true) . '
'; + echo __('Renews every ', 'slmplus') . get_post_meta($product->get_id(), '_license_renewal_period_lenght', true) . ' ' . get_post_meta($product->get_id(), '_license_renewal_period_term', true) . '
'; } } } diff --git a/readme.txt b/readme.txt index 2ac7ee1..0e5023c 100644 --- a/readme.txt +++ b/readme.txt @@ -1,49 +1,54 @@ -=== Software license management === -Contributors: Michel Velis +=== SLM Plus === +Contributors: Michel Velis, Tips and Tricks HQ Donate link: http://paypal.me/mvelis -Tags: license, software license -Requires at least: 4.1 +Tags: license, software license, woocommerce, license management +Requires at least: 5.6 Tested up to: 6.1.0 -Stable tag: 5.6.2 -Requires PHP: 5.2.4 +Stable tag: 6.1.0 +Requires PHP: 7.4 License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html -Software license management solution for your web applications. Supports WooCommerce. +A comprehensive software license management solution for web applications including WordPress plugins, themes, and PHP-based software. Seamlessly integrates with WooCommerce to offer license key generation, management, and validation. Ideal for developers managing software licenses across multiple platforms with built-in multilingual support and performance optimization. == Description == -Software license management solution for your web applications (WordPress plugins, Themes, Applications, PHP based membership script etc.). Supports WooCommerce. +SLM Plus provides a powerful solution for managing software licenses for WordPress plugins, themes, and other web applications. With WooCommerce integration, the plugin allows you to generate, activate, and manage licenses for your customers seamlessly. ## 📦 Features: -- Create license keys -- Check the status of a license key from from your application (remotely) -- Activate a license key from your application (remotely) -- Deactivate a license key (remotely) -- Delete a license key (remotely) -- Update a license key (remotely) -- Check a license key (remotely) -- Track where the license key is being used. -- Track activation date -- You can also create license keys manually from the admin dashboard of this plugin. -- Bulk actions are now available -- Users and admins can export licenses (new) +- Generate and manage license keys for your products +- Remote license key validation and activation +- Track license key usage across different domains and devices +- Manually create licenses from the admin dashboard +- Bulk actions for managing licenses +- Export licenses for both users and admins (new) - View licenses by subscribers (new) -- License info is now included inside woocommerce order (new) -- License activity log, track requests and activations per license (new) -- Admin stats widgets (new) -- Send emails on expiration and activation -- Language support (spanish and english) -- Woocommerce product type -- Pass license info to woocommerce order -- Expiration reminder (Emails the user when the license expires) -- User can manager and view licenses from my account's page +- License info is now included inside WooCommerce order details (new) +- License activity logs to track key usage and activations (new) +- Admin stats widgets to visualize license usage (new) +- Automated emails for license expiration and activation reminders +- Multilingual support (English, Spanish) +- WooCommerce product type integration to manage licenses through orders +- Manage licenses directly from users' "My Account" page +- License expiration reminders and notification emails == Installation == This section describes how to install the plugin and get it working. 1. Go to the Add New plugins screen in your WordPress admin area -2. Click the upload tab +2. Click the Upload tab 3. Browse for the plugin file (software-license-manager.zip) -4. Click Install Now and then activate the plugin \ No newline at end of file +4. Click Install Now and then activate the plugin + +== Changelog == += 6.1.0 = +- Improved compatibility with WordPress 6.1 and WooCommerce +- Enhanced performance for large license databases +- Added multilingual support for Spanish +- New admin widgets for tracking licenses +- Improved license information in WooCommerce order details + +== Upgrade Notice == += 6.1.0 = +- Please ensure your WordPress version is at least 5.6 and PHP version is 7.4 or higher before updating. diff --git a/slm-plus.php b/slm-plus.php new file mode 100644 index 0000000..c3df75e --- /dev/null +++ b/slm-plus.php @@ -0,0 +1,83 @@ +prefix . "lic_key_tbl"); +define('SLM_TBL_EMAILS', $wpdb->prefix . "lic_emails_tbl"); +define('SLM_TBL_LIC_DOMAIN', $wpdb->prefix . "lic_reg_domain_tbl"); +define('SLM_TBL_LIC_DEVICES', $wpdb->prefix . "lic_reg_devices_tbl"); +define('SLM_TBL_LIC_LOG', $wpdb->prefix . "lic_log_tbl"); +define('SLM_TBL_LICENSE_STATUS', $wpdb->prefix . "lic_status_tbl"); +define('SLM_MANAGEMENT_PERMISSION', 'manage_options'); +define('SLM_MAIN_MENU_SLUG', 'slm_overview'); +define('SLM_MENU_ICON', 'dashicons-lock'); + +// Load core plugin functionalities +if (file_exists(SLM_LIB . 'slm-plugin-core.php')) { + require_once SLM_LIB . 'slm-plugin-core.php'; +} + +define('SLM_DEFAULT_MAX_DOMAINS', SLM_API_Utility::get_slm_option('default_max_domains')); +define('SLM_DEFAULT_MAX_DEVICES', SLM_API_Utility::get_slm_option('default_max_devices')); + +// Use native WordPress function for setting options +define('WOO_SLM_API_SECRET', SLM_API_Utility::get_slm_option('lic_creation_secret')); +define('KEY_API', SLM_API_Utility::get_slm_option('lic_creation_secret')); +define('KEY_API_PREFIX', SLM_API_Utility::get_slm_option('lic_prefix')); + +// Auto-updater integration for GitHub updates +if (file_exists('plugin-update-checker/plugin-update-checker.php')) { + require 'plugin-update-checker/plugin-update-checker.php'; + $myUpdateChecker = Puc_v4_Factory::buildUpdateChecker( + 'https://github.com/michelve/software-license-manager', + __FILE__, + 'software-license-manager' + ); +} diff --git a/software-license-manager.php b/software-license-manager.php deleted file mode 100755 index a3b6e1a..0000000 --- a/software-license-manager.php +++ /dev/null @@ -1,64 +0,0 @@ -Tips and Tricks HQ -Text Domain: softwarelicensemanager -Domain Path: /i18n/languages/ -WC tested up to: 6.1.0 -*/ - -// If this file is called directly, abort. -if (!defined('WPINC')) { - die(); -} -global $wpdb, $slm_debug_logger; - -//Short name/slug "SLM" or "slm" -define('SLM_VERSION', '5.6.2'); -define('SLM_DB_VERSION', '4.3.5'); -define('SLM_REWRITE_VERSION', '2.4.5'); -define('SLM_FOLDER', dirname(plugin_basename(__FILE__))); -define('SLM_URL', plugins_url('', __FILE__)); -define('SLM_ASSETS_URL', SLM_URL . '/public/assets/'); -define('SLM_PATH', plugin_dir_path(__FILE__)); -define('SLM_LIB', SLM_PATH . 'includes/'); -define('SLM_WOO', SLM_PATH . 'woocommerce/'); -define('SLM_ADDONS', SLM_PATH . 'addons/'); -define('SLM_ADMIN', SLM_PATH . 'admin/'); -define('SLM_ADMIN_ADDONS', SLM_ADMIN . 'includes/'); -define('SLM_CRONS', SLM_ADMIN_ADDONS . 'cronjobs/'); -define('SLM_PUBLIC', SLM_PATH . 'public/'); -define('SLM_TEAMPLATES', SLM_PATH . 'templates/'); -define('SLM_SITE_HOME_URL', get_home_url()); -define('SLM_SITE_URL', get_site_url() . '/'); -define('SLM_TBL_LICENSE_KEYS', $wpdb->prefix . "lic_key_tbl"); -define('SLM_TBL_EMAILS', $wpdb->prefix . "lic_emails_tbl"); -define('SLM_TBL_LIC_DOMAIN', $wpdb->prefix . "lic_reg_domain_tbl"); -define('SLM_TBL_LIC_DEVICES', $wpdb->prefix . "lic_reg_devices_tbl"); -define('SLM_TBL_LIC_LOG', $wpdb->prefix . "lic_log_tbl"); -define('SLM_MANAGEMENT_PERMISSION', 'manage_options'); -define('SLM_MAIN_MENU_SLUG', 'slm_overview'); -define('SLM_MENU_ICON', 'dashicons-lock'); - - -if (file_exists(SLM_LIB . 'slm-plugin-core.php')) { - include_once SLM_LIB . 'slm-plugin-core.php'; -} - -// Options and filters -define('WOO_SLM_API_SECRET', SLM_Helper_Class::slm_get_option('lic_creation_secret')); -define('KEY_API', SLM_Helper_Class::slm_get_option('lic_creation_secret')); -define('KEY_API_PREFIX', SLM_Helper_Class::slm_get_option('lic_prefix')); - -add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'slm_settings_link'); - -// plugin auto updater helper -if (file_exists('plugin-update-checker/plugin-update-checker.php')) { - require 'plugin-update-checker/plugin-update-checker.php'; - $myUpdateChecker = Puc_v4_Factory::buildUpdateChecker('https://github.com/michelve/software-license-manager', __FILE__, '/software-license-manager'); -} diff --git a/uninstall.php b/uninstall.php index fcd0176..ee99be5 100755 --- a/uninstall.php +++ b/uninstall.php @@ -1,8 +1,8 @@ -
- +function slm_display_nice_item_meta($item_id, $item, $product) { + // Fetch the metadata associated with the license key + if ($meta_data = wc_get_order_item_meta($item_id, '_slm_lic_key', false)) { + ?> +
' . $lic_key . ''; + $cur_link = sprintf( + '%s', + esc_url($admin_link . $lic_id), + esc_html($lic_key) + ); } else { - $cur_link = $lic_key . ' - Licence not exists anymore'; + $cur_link = sprintf( + '%s - %s', + esc_html($lic_key), + esc_html__('License no longer exists', 'slmplus') + ); } ?> - +
- -
- + get_id(); + if (!$order) { + SLM_Helper_Class::write_log('Order ID ' . $order_id . ' not found.'); + return; // Stop if the order does not exist + } - // SLM_Helper_Class::write_log('purchase_id_ -- '.$purchase_id_ ); - // SLM_Helper_Class::write_log('purchase_id_ -- '.$user_id ); + $purchase_id_ = $order->get_id(); + SLM_Helper_Class::write_log('Purchase ID: ' . $purchase_id_); global $user_id; - $user_id = $order->get_user_id(); - $user_info = get_userdata($user_id); - $get_user_meta = get_user_meta($user_id); - $payment_meta['user_info']['first_name'] = $get_user_meta['billing_first_name'][0]; - $payment_meta['user_info']['last_name'] = $get_user_meta['billing_last_name'][0]; - $payment_meta['user_info']['email'] = $get_user_meta['billing_email'][0]; - $payment_meta['user_info']['company'] = $get_user_meta['billing_company'][0]; + SLM_Helper_Class::write_log('User ID: ' . $user_id); + + if (!$user_id) { + SLM_Helper_Class::write_log('User ID not found for Order ID: ' . $order_id); + return; // Stop if user ID cannot be found + } - // SLM_Helper_Class::write_log('user_id -- '.$user_id ); + // Get user details + $user_meta = get_user_meta($user_id); + $payment_meta = array( + 'user_info' => array( + 'first_name' => isset($user_meta['billing_first_name'][0]) ? sanitize_text_field($user_meta['billing_first_name'][0]) : '', + 'last_name' => isset($user_meta['billing_last_name'][0]) ? sanitize_text_field($user_meta['billing_last_name'][0]) : '', + 'email' => isset($user_meta['billing_email'][0]) ? sanitize_email($user_meta['billing_email'][0]) : '', + 'company' => isset($user_meta['billing_company'][0]) ? sanitize_text_field($user_meta['billing_company'][0]) : '' + ) + ); - // Collect license keys + + // Initialize licenses array $licenses = array(); $items = $order->get_items(); + foreach ($items as $item_key => $values) { - foreach ($items as $item => $values) { - $download_id = $product_id = $values['product_id']; + $product_id = $values->get_product_id(); $product = $values->get_product(); if ($product->is_type('slm_license')) { - $download_quantity = absint($values['qty']); - //Get all existing licence keys of the product - $order_item_lic_key = $values->get_meta('_slm_lic_key', false); - $lic_to_add = $download_quantity - count($order_item_lic_key); - //Create keys only if there are not keys created already + $download_quantity = absint($values->get_quantity()); + $order_item_lic_keys = $values->get_meta('_slm_lic_key', false); + $lic_to_add = $download_quantity - count($order_item_lic_keys); + + // Generate license keys only if necessary for ($i = 1; $i <= $lic_to_add; $i++) { - /** - * Calculate Expire date - * @since 1.0.3 - */ $expiration = ''; - - $renewal_period = (int)wc_slm_get_licensing_renewal_period($product_id); + $renewal_period = wc_slm_get_licensing_renewal_period($product_id); $renewal_term = wc_slm_get_licensing_renewal_period_term($product_id); - $slm_billing_length = $renewal_period; - $slm_billing_interval = $renewal_term; - - if ($renewal_period == 'onetime') { + // Set expiration date + if ($renewal_term === 'onetime') { $expiration = '0000-00-00'; + } else { + $expiration = date('Y-m-d', strtotime('+' . $renewal_period . ' ' . sanitize_text_field($renewal_term))); } - // elseif ($renewal_period == 30) { - // $renewal_period = date('Y-m-d', strtotime('+' . 31 . ' days')); - // } - else { - $expiration = date('Y-m-d', strtotime('+' . $renewal_period . ' ' . $renewal_term)); - } - // SLM_Helper_Class::write_log('renewal_period -- '.$renewal_period ); - // SLM_Helper_Class::write_log('exp -- ' . $expiration); - // SLM_Helper_Class::write_log('term -- ' . $renewal_term); - // Sites allowed get license meta from variation - $sites_allowed = wc_slm_get_sites_allowed($product_id); + // Log renewal details + SLM_Helper_Class::write_log('Renewal Period: ' . $renewal_period); + SLM_Helper_Class::write_log('Expiration Date: ' . $expiration); + SLM_Helper_Class::write_log('Renewal Term: ' . $renewal_term); + // Get allowed sites/devices and verify them + $sites_allowed = wc_slm_get_sites_allowed($product_id); if (!$sites_allowed) { - $sites_allowed_error = __('License could not be created: Invalid sites allowed number.', 'softwarelicensemanager'); - $int = wc_insert_payment_note($purchase_id_, $sites_allowed_error); + wc_insert_payment_note($purchase_id_, __('License could not be created: Invalid sites allowed number.', 'slmplus')); + SLM_Helper_Class::write_log('License could not be created: Invalid sites allowed number'); break; } - // Get the custumer ID - // $user_id = $order->get_user_id(); - $order_data = $order->get_data(); // The Order data - - ## Access Order Items data properties (in an array of values) ## + // Collect product details $item_data = $values->get_data(); $product_name = $item_data['name']; - $product_id = $item_data['product_id']; $_license_current_version = get_post_meta($product_id, '_license_current_version', true); $_license_until_version = get_post_meta($product_id, '_license_until_version', true); $amount_of_licenses_devices = wc_slm_get_devices_allowed($product_id); - $current_version = (int)get_post_meta($product_id, '_license_current_version', true); $license_type = get_post_meta($product_id, '_license_type', true); $lic_item_ref = get_post_meta($product_id, '_license_item_reference', true); - - // Transaction id - $transaction_id = wc_get_payment_transaction_id($product_id); - - // Build item name - $item_name = $product->get_title(); - - // Build parameters - $api_params = array(); - $api_params['slm_action'] = 'slm_create_new'; - $api_params['secret_key'] = KEY_API; - $api_params['first_name'] = (isset($payment_meta['user_info']['first_name'])) ? $payment_meta['user_info']['first_name'] : ''; - $api_params['last_name'] = (isset($payment_meta['user_info']['last_name'])) ? $payment_meta['user_info']['last_name'] : ''; - $api_params['email'] = (isset($payment_meta['user_info']['email'])) ? $payment_meta['user_info']['email'] : ''; - $api_params['company_name'] = $payment_meta['user_info']['company']; - $api_params['purchase_id_'] = $purchase_id_; - $api_params['product_ref'] = $product_id; // TODO: get product id - $api_params['txn_id'] = $purchase_id_; - $api_params['max_allowed_domains'] = $sites_allowed; - $api_params['max_allowed_devices'] = $amount_of_licenses_devices; - $api_params['date_created'] = date('Y-m-d'); - $api_params['date_expiry'] = $expiration; - $api_params['slm_billing_length'] = $slm_billing_length; - $api_params['slm_billing_interval'] = $slm_billing_interval; - $api_params['until'] = $_license_until_version; - $api_params['current_ver'] = $_license_current_version; - $api_params['subscr_id'] = $order->get_customer_id(); - $api_params['lic_type'] = $license_type; - $api_params['item_reference'] = $lic_item_ref; - - //access_expires - //SLM_Helper_Class::write_log('license_type -- ' . $license_type ); - // Send query to the license manager server - $url = SLM_SITE_HOME_URL . '?' . http_build_query($api_params); - $url = str_replace(array('http://', 'https://'), '', $url); - $url = 'http://' . $url; - $response = wp_remote_get($url, array('timeout' => 20, 'sslverify' => false)); + $transaction_id = wc_get_payment_transaction_id($order_id); + + // Prepare API parameters for license creation + $api_params = array( + 'slm_action' => 'slm_create_new', + 'secret_key' => KEY_API, + 'first_name' => $payment_meta['user_info']['first_name'], + 'last_name' => $payment_meta['user_info']['last_name'], + 'email' => $payment_meta['user_info']['email'], + 'company_name' => $payment_meta['user_info']['company'], + 'purchase_id_' => $purchase_id_, + 'product_ref' => $product_id, + 'txn_id' => $transaction_id, + 'max_allowed_domains' => $sites_allowed, + 'max_allowed_devices' => $amount_of_licenses_devices, + 'date_created' => current_time('Y-m-d'), + 'date_expiry' => $expiration, + 'slm_billing_length' => $renewal_period, + 'slm_billing_interval' => $renewal_term, + 'until' => $_license_until_version, + 'current_ver' => $_license_current_version, + 'subscr_id' => $order->get_customer_id(), + 'lic_type' => $license_type, + 'item_reference' => $lic_item_ref, + ); + + // Send the request to create a license key + $url = esc_url_raw(SLM_SITE_HOME_URL) . '?' . http_build_query($api_params); + + SLM_Helper_Class::write_log('URL: ' . $url); + + $response = wp_safe_remote_get($url, array('timeout' => 20, 'sslverify' => false)); $license_key = wc_slm_get_license_key($response); - // Collect license keys + // If a license key is generated, save it if ($license_key) { $licenses[] = array( - 'item' => $item_name, - 'key' => $license_key, + 'item' => sanitize_text_field($product_name), + 'key' => sanitize_text_field($license_key), 'expires' => $expiration, - 'type' => $license_type, + 'type' => sanitize_text_field($license_type), 'item_ref' => $lic_item_ref, - 'slm_billing_length' => $slm_billing_length, - 'slm_billing_interval' => $slm_billing_interval, + 'slm_billing_length' => $renewal_period, + 'slm_billing_interval' => $renewal_term, 'status' => 'pending', 'version' => $_license_current_version, 'until' => $_license_until_version @@ -234,407 +263,583 @@ function wc_slm_create_license_keys($order_id) } } + // If licenses were successfully generated, add a payment note if (count($licenses) > 0) { - // Payment note wc_slm_payment_note($order_id, $licenses); - - // Assign licenses - - //What does this do? The meta is not used in the plugin anywhere - //wc_slm_assign_licenses($order_id, $licenses); } } -function wc_slm_get_license_key($response) + + +function wc_slm_get_license_key($response) { // Check for error in the response if (is_wp_error($response)) { return false; } - // Get License data - $json = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', utf8_encode(wp_remote_retrieve_body($response))); - $license_data = json_decode($json); - if (!isset($license_data->key)) { + // Retrieve response body + $response_body = wp_remote_retrieve_body($response); + + if (empty($response_body)) { + return false; // If response body is empty, return false + } + + // Decode JSON while handling potential errors + $decoded_data = json_decode($response_body, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + // Handle JSON decoding error appropriately, e.g., log the error + error_log('Failed to decode JSON response: ' . json_last_error_msg()); + return false; + } + + // Remove invalid control characters from response data (control chars except line feeds, tabs, etc.) + $cleaned_data = preg_replace('/[\x00-\x1F\x7F]/', '', json_encode($decoded_data, JSON_UNESCAPED_UNICODE)); + + if ($cleaned_data === false) { + // If the cleaning fails, return false + error_log('Failed to clean the JSON response body.'); return false; } - // Prepare note text + + // Decode cleaned JSON back to PHP associative array + $license_data = json_decode($cleaned_data); + + if (!isset($license_data->key)) { + return false; // Key is not set + } + + // Return the license key return $license_data->key; } -function wc_slm_get_license_id($license) + +function wc_slm_get_license_id($license) { global $wpdb; - $license_id = $wpdb->get_row("SELECT ID, license_key FROM " . $wpdb->prefix . "lic_key_tbl" . " WHERE license_key = '" . $license . "' ORDER BY id DESC LIMIT 0,1"); - return $license_id->ID; + + // Prepare the SQL statement to prevent SQL injection + $table_name = $wpdb->prefix . 'lic_key_tbl'; + $query = $wpdb->prepare( + "SELECT ID FROM {$table_name} WHERE license_key = %s ORDER BY id DESC LIMIT 1", + $license + ); + + // Fetch the result as an object + $license_id = $wpdb->get_var($query); + + // Return the license ID if found, otherwise return false + return $license_id ? intval($license_id) : false; } function wc_slm_payment_note($order_id, $licenses) { - if ($licenses && count($licenses) != 0) { - $message = __('License Key(s) generated', 'softwarelicensemanager'); - - foreach ($licenses as $license) { - $license_key = $license['key']; - $message .= '
' . $license['item'] . ': ' . $license_key . ''; + // Get the order object + $order = wc_get_order($order_id); - //These data are irrelevant - they work only when the order is completed and just for one licence key + // Check if the order is valid and licenses are provided + if ($order && !empty($licenses) && is_array($licenses)) { + $message = __('License Key(s) generated:', 'slmplus'); - /* add_post_meta($order_id, 'slm_wc_license_order_key', $license_key); - add_post_meta($order_id, 'slm_wc_license_expires', $license[ 'expires']); - add_post_meta($order_id, 'slm_wc_license_type', $license[ 'type']); - add_post_meta($order_id, 'slm_wc_license_item_ref', $license[ 'item_ref']); - add_post_meta($order_id, 'slm_wc_license_status', $license['status']); - add_post_meta($order_id, 'slm_wc_license_version', $license[ 'version']); - add_post_meta($order_id, 'slm_wc_until_version', $license['until']); */ + foreach ($licenses as $license) { + // Check if required license information is available + if (!isset($license['key']) || !isset($license['item'])) { + continue; // Skip if data is incomplete + } - //SLM_Helper_Class::write_log($license_key); + $license_key = sanitize_text_field($license['key']); + $item_name = sanitize_text_field($license['item']); + + // Fetch the license ID securely + $license_id = wc_slm_get_license_id($license_key); + + // Construct the link to the license edit page + $license_link = esc_url( + add_query_arg( + array( + 'page' => 'slm_manage_license', + 'edit_record' => $license_id, + ), + admin_url('admin.php') + ) + ); + + // Add to the message + $message .= '
' . esc_html($item_name) . ': ' . esc_html($license_key) . ''; } } else { - $message = __('License Key(s) could not be created.', 'softwarelicensemanager'); + $message = __('License Key(s) could not be created.', 'slmplus'); } - // Save note - $int = wc_insert_payment_note($order_id, $message); + // Add the message as an order note using the correct method + if ($order) { + $order->add_order_note( wp_kses_post($message) ); + } } + function wc_slm_access_expiration($order_id, $lic_expiry = '') { global $wpdb; + // Fetch the WooCommerce order object $order = wc_get_order($order_id); - $items = $order->get_items(); - foreach ($items as $item_key => $item_details) { - $product_id = $item_details['product_id']; + if (!$order) { + return; // If the order doesn't exist, return early + } + + // Loop through each item in the order + foreach ($order->get_items() as $item_key => $item_details) { + $product_id = $item_details->get_product_id(); $product = wc_get_product($product_id); - if ($product->is_type('slm_license')) { - //Get any existing licence key + + // Check if the product is of type 'slm_license' + if ($product && $product->is_type('slm_license')) { + + // Get the existing license key attached to the order item $order_item_lic_key = $item_details->get_meta('_slm_lic_key', true); if (!empty($order_item_lic_key)) { + + // Fetch license data using a custom helper function $licence = get_licence_by_key($order_item_lic_key); if (!empty($licence)) { + // Retrieve and format the license expiry date $lic_expiry = $licence['date_expiry']; - if ($lic_expiry == '0000-00-00') { + if ($lic_expiry === '0000-00-00') { $lic_expiry = 'NULL'; } else { - $lic_expiry = "'" . $lic_expiry . "'"; + $lic_expiry = $wpdb->prepare('%s', $lic_expiry); } - $query = "UPDATE " . $wpdb->prefix . "woocommerce_downloadable_product_permissions SET access_expires = " . $lic_expiry . " WHERE order_id = " . $order_id . " AND product_id = " . $product_id . ";"; + + // Prepare the SQL query using placeholders + $table_name = $wpdb->prefix . 'woocommerce_downloadable_product_permissions'; + $query = $wpdb->prepare( + "UPDATE {$table_name} + SET access_expires = {$lic_expiry} + WHERE order_id = %d AND product_id = %d", + $order_id, + $product_id + ); + + // Execute the query $wpdb->query($query); } } } } - //SLM_Helper_Class::write_log('log:' . $query ); + + // Optionally, log the query for debugging (commented out by default) + //SLM_Helper_Class::write_log('log:' . $query); } + /** - * Get licence info from given it's key - * @param array $licence_key - licence key for which to retrieve licence - * @return array all the licence fields - * @since 4.5.5 + * Get License by Key + * + * @param string $licence_key License key to fetch the record for. + * @return array|false Returns license data array if found, false otherwise. */ function get_licence_by_key($licence_key) { global $wpdb; + // Check if license key is empty and sanitize input if (empty($licence_key)) { return false; - } else { - $licence_key = esc_attr($licence_key); } + $licence_key = esc_attr($licence_key); + + // Prepare and execute the SQL query $lic_keys_table = SLM_TBL_LICENSE_KEYS; - $sql_prep = $wpdb->prepare("SELECT * FROM $lic_keys_table WHERE license_key = %s ORDER BY id DESC LIMIT 0,1", $licence_key); - $record = $wpdb->get_row($sql_prep, ARRAY_A, 0); - return $record; + $sql_prep = $wpdb->prepare( + "SELECT * FROM {$lic_keys_table} WHERE license_key = %s ORDER BY id DESC LIMIT 1", + $licence_key + ); + + // Fetch the record and return as an associative array + $record = $wpdb->get_row($sql_prep, ARRAY_A); + return $record ? $record : false; } +/** + * Assign Licenses to Order + * + * @param int $order_id WooCommerce Order ID to assign licenses to. + * @param array $licenses An array of licenses to be assigned. + * @return void + */ function wc_slm_assign_licenses($order_id, $licenses) { - if (count($licenses) != 0) { + // Check if licenses are available and valid + if (!empty($licenses) && is_array($licenses)) { add_post_meta($order_id, '_wc_slm_payment_licenses', $licenses); } } - +/** + * Get Allowed Number of Sites for a Product + * + * @param int $product_id WooCommerce Product ID. + * @return int|false Returns the number of allowed domains if set, false otherwise. + */ function wc_slm_get_sites_allowed($product_id) { + // Get the number of allowed domains for a product $wc_slm_sites_allowed = absint(get_post_meta($product_id, '_domain_licenses', true)); - if (empty($wc_slm_sites_allowed)) { - return false; - } - return $wc_slm_sites_allowed; + + // Return false if no value is set, otherwise return the value + return !empty($wc_slm_sites_allowed) ? $wc_slm_sites_allowed : false; } +/** + * Get License Type + * + * @param int $product_id Product ID. + * @return string|false License type or false if not found. + */ function wc_slm_get_lic_type($product_id) { - $_license_type = absint(get_post_meta($product_id, '_license_type', true)); - if (empty($_license_type)) { - return false; - } - return $_license_type; + $_license_type = get_post_meta($product_id, '_license_type', true); + return !empty($_license_type) ? sanitize_text_field($_license_type) : false; } +/** + * Get Number of Allowed Devices + * + * @param int $product_id Product ID. + * @return int|false Number of allowed devices or false if not set. + */ function wc_slm_get_devices_allowed($product_id) { $_devices_licenses = absint(get_post_meta($product_id, '_devices_licenses', true)); - if (empty($_devices_licenses)) { - return false; - } - return $_devices_licenses; + return !empty($_devices_licenses) ? $_devices_licenses : false; } +/** + * Get Quantity of Licenses + * + * @param int $product_id Product ID. + * @return int|false Quantity of licenses or false if not set. + */ function wc_slm_get_licenses_qty($product_id) { $amount_of_licenses = absint(get_post_meta($product_id, '_amount_of_licenses', true)); - if (empty($amount_of_licenses)) { - return false; - } - return $amount_of_licenses; + return !empty($amount_of_licenses) ? $amount_of_licenses : false; } +/** + * Get Licensing Renewal Period + * + * @param int $product_id Product ID. + * @return int Licensing renewal period in days or 0 if not set. + */ function wc_slm_get_licensing_renewal_period($product_id) { - $_license_renewal_period = absint(get_post_meta($product_id, '_license_renewal_period', true)); - if (empty($_license_renewal_period)) { - return 0; - } - return $_license_renewal_period; + $_license_renewal_period_lenght = absint(get_post_meta($product_id, '_license_renewal_period_lenght', true)); + return !empty($_license_renewal_period_lenght) ? $_license_renewal_period_lenght : 0; } -//_license_renewal_period_term - +/** + * Get Licensing Renewal Period Term + * + * @param int $product_id Product ID. + * @return string Renewal period term (e.g., days, months, years) or empty string if not set. + */ function wc_slm_get_licensing_renewal_period_term($product_id) { $term = get_post_meta($product_id, '_license_renewal_period_term', true); - return $term; + return !empty($term) ? sanitize_text_field($term) : ''; } +/** + * Check if Licensing is Enabled + * + * @param int $download_id Downloadable product ID. + * @return bool True if licensing is enabled, false otherwise. + */ function wc_slm_is_licensing_enabled($download_id) { $licensing_enabled = absint(get_post_meta($download_id, '_wc_slm_licensing_enabled', true)); - // Set defaults - if ($licensing_enabled) { - return true; - } else { - return false; - } + return $licensing_enabled === 1; } +/** + * Insert a Payment Note to Order + * + * @param int $order_id WooCommerce Order ID. + * @param string $msg Note to be added to the order. + * @return void + */ function wc_insert_payment_note($order_id, $msg) { - $order = new WC_Order($order_id); - $order->add_order_note($msg); + if (!empty($order_id) && !empty($msg)) { + $order = wc_get_order($order_id); + if ($order) { + $order->add_order_note(esc_html($msg)); + } + } } +/** + * Get Payment Transaction ID + * + * @param int $order_id WooCommerce Order ID. + * @return string|null Transaction ID or null if not found. + */ function wc_get_payment_transaction_id($order_id) { return get_post_meta($order_id, '_transaction_id', true); } +/** + * Handle Order Completion Actions + * + * @param int $order_id WooCommerce Order ID. + * @return void + */ function slm_order_completed($order_id) { - - global $user_id, $wpdb; - $get_user_info = ''; - $order = wc_get_order($order_id); - $purchase_id_ = $order->get_id(); - $order_data = $order->get_data(); // The Order data - $order_billing_email = $order_data['billing']['email']; - - // if wp billing is empty - if ($order_billing_email == '') { - $get_user_info = get_userdata(get_current_user_id()); - $order_billing_email = $get_user_info->user_email; + if (!$order_id) { + return; } - $billing_address = $order_billing_email; - - // The text for the note - $note = __("Order confirmation email sent to: " . $billing_address . ""); - // Add the note - $order->add_order_note($note); - // Save the data - $order->save(); - //SLM_Helper_Class::write_log($to_email . 'License details'. $message . $headers ); -} - -function slm_show_msg($order_id) -{ - $order_id = new WC_Order($order_id); - $purchase_id_ = $order_id->get_id(); $order = wc_get_order($order_id); - $items = $order->get_items(); - - foreach ($items as $item) { - $product_name = $item->get_name(); - $product_id = $item->get_product_id(); - $product_variation_id = $item->get_variation_id(); - $amount_of_licenses = wc_slm_get_sites_allowed($product_id); + if (!$order) { + return; + } - // is a licensed product - //var_dump(get_post_meta($product_id)); + $order_billing_email = $order->get_billing_email(); - if ($amount_of_licenses) { - echo '

' . __('My subscriptions', 'softwarelicensemanager') . '

' . __('You can see and manage your licenses inside your account', 'softwarelicensemanager') . ' ' . __('Manage Licenses', 'softwarelicensemanager') . '
'; + // If billing email is empty, use current user's email + if (empty($order_billing_email)) { + $current_user = wp_get_current_user(); + if ($current_user->exists()) { + $order_billing_email = $current_user->user_email; } } + + // Create the note text + if (!empty($order_billing_email)) { + $note = sprintf( + __("Order confirmation email sent to: %s", 'slmplus'), + esc_attr($order_billing_email), + esc_html($order_billing_email) + ); + + // Add the note to the order and save + $order->add_order_note($note); + $order->save(); + } } /** - * Update the order meta with field value + * Update the Order Meta with Custom License Fields + * + * @param int $order_id The WooCommerce Order ID. + * @return void */ - -function slm_add_lic_key_meta_update($order_id) -{ - if (!empty($_POST['slm_wc_license_order_key'])) { - update_post_meta($order_id, 'slm_wc_license_order_key', sanitize_text_field($_POST['slm_wc_license_order_key'])); - } - if (!empty($_POST['slm_wc_license_expires'])) { - update_post_meta($order_id, 'slm_wc_license_expires', sanitize_text_field($_POST['slm_wc_license_expires'])); - } - if (!empty($_POST['slm_wc_license_type'])) { - update_post_meta($order_id, 'slm_wc_license_type', sanitize_text_field($_POST['slm_wc_license_type'])); - } - - if (!empty($_POST['slm_wc_license_status'])) { - update_post_meta($order_id, 'slm_wc_license_status', sanitize_text_field($_POST['slm_wc_license_status'])); - } - - if (!empty($_POST['slm_wc_license_item_ref'])) { - update_post_meta($order_id, 'slm_wc_license_item_ref', sanitize_text_field($_POST['slm_wc_license_item_ref'])); +function slm_add_lic_key_meta_update($order_id) { + // Get the order object from the order ID + $order = wc_get_order($order_id); + + if (!$order) { + SLM_Helper_Class::write_log('Order not found for order ID: ' . $order_id); + return; } - if (!empty($_POST['slm_wc_license_version'])) { - update_post_meta($order_id, 'slm_wc_license_version', sanitize_text_field($_POST['slm_wc_license_version'])); + // Debug to check if the function is called + SLM_Helper_Class::write_log('slm_add_lic_key_meta_update called for order ID: ' . $order->get_id()); + + $fields = [ + 'slm_wc_license_order_key', + 'slm_wc_license_expires', + 'slm_wc_license_type', + 'slm_wc_license_status', + 'slm_wc_license_item_ref', + 'slm_wc_license_version', + ]; + + foreach ($fields as $field) { + if (!empty($_POST[$field])) { + $order->update_meta_data($field, sanitize_text_field($_POST[$field])); + SLM_Helper_Class::write_log('Added meta: ' . $field . ' with value: ' . sanitize_text_field($_POST[$field])); + } else { + SLM_Helper_Class::write_log('Field ' . $field . ' not found in POST data.'); + } } + + // Save the order metadata changes + $order->save(); } +add_action('woocommerce_payment_complete', 'slm_add_lic_key_meta_update', 10, 1); + /** - * Display field value on the order edit page + * Display License Key Information on the Admin Order Edit Page + * + * @param WC_Order $order The WooCommerce Order object. + * @return void */ - function slm_add_lic_key_meta_display($order) { - if (!empty(get_post_meta($order->get_id(), 'slm_wc_license_order_key', true))) { - echo '

' . __('License key', 'softwarelicensemanager') . ':
' . get_post_meta($order->get_id(), 'slm_wc_license_order_key', true) . '

'; - echo '

' . __('License expiration', 'softwarelicensemanager') . ':
' . get_post_meta($order->get_id(), 'slm_wc_license_expires', true) . '

'; - echo '

' . __('License type', 'softwarelicensemanager') . ':
' . get_post_meta($order->get_id(), 'slm_wc_license_type', true) . '

'; - echo '

' . __('License item reference', 'softwarelicensemanager') . ':
' . get_post_meta($order->get_id(), 'slm_wc_license_item_ref', true) . '

'; - echo '

' . __('License status', 'softwarelicensemanager') . ':
' . get_post_meta($order->get_id(), 'slm_wc_license_status', true) . '

'; - echo '

' . __('License current version', 'softwarelicensemanager') . ':
' . get_post_meta($order->get_id(), 'slm_wc_license_version', true) . '

'; - echo '

' . __('Supported until version', 'softwarelicensemanager') . ':
' . get_post_meta($order->get_id(), 'slm_wc_until_version', true) . '

'; + // Meta keys for License Information + $meta_fields = [ + 'slm_wc_license_order_key' => __('License Key', 'slmplus'), + 'slm_wc_license_expires' => __('License Expiration', 'slmplus'), + 'slm_wc_license_type' => __('License Type', 'slmplus'), + 'slm_wc_license_item_ref' => __('License Item Reference', 'slmplus'), + 'slm_wc_license_status' => __('License Status', 'slmplus'), + 'slm_wc_license_version' => __('License Current Version', 'slmplus'), + 'slm_wc_until_version' => __('Supported Until Version', 'slmplus'), + ]; + + // Display license details if they are available + foreach ($meta_fields as $meta_key => $label) { + $meta_value = get_post_meta($order->get_id(), $meta_key, true); + if (!empty($meta_value)) { + echo '

' . esc_html($label) . ':
' . esc_html($meta_value) . '

'; + } } } +add_action('woocommerce_admin_order_data_after_order_details', 'slm_add_lic_key_meta_display'); /** - * Display values on the order details page + * Display License Details on the Order Details Page for Customers + * + * @param WC_Order $order The WooCommerce Order object. + * @return void */ - function slm_order_details($order) { - global $wpdb; - + // Get all the items from the order $items = $order->get_items(); - $licences = array(); + $licences = []; + foreach ($items as $item_key => $item_details) { $product = $item_details->get_product(); + // Check if product is of type 'slm_license' if ($product->is_type('slm_license')) { - if ($lic_keys = wc_get_order_item_meta($item_details->get_id(), '_slm_lic_key', false)) { - $lic_types = wc_get_order_item_meta($item_details->get_id(), '_slm_lic_type', false); - $licences = array_map(function ($keys, $types) { - return array( - 'lic_key' => $keys, - 'lic_type' => $types - ); + // Retrieve license keys and types from the order item meta + $lic_keys = wc_get_order_item_meta($item_details->get_id(), '_slm_lic_key', false); + $lic_types = wc_get_order_item_meta($item_details->get_id(), '_slm_lic_type', false); + + if ($lic_keys && $lic_types) { + $licenses_data = array_map(function ($keys, $types) { + return [ + 'lic_key' => sanitize_text_field($keys), + 'lic_type' => sanitize_text_field($types), + ]; }, $lic_keys, $lic_types); + + $licences = array_merge($licences, $licenses_data); } } } - if ($licences) { + + // Display license details if available + if (!empty($licences)) { echo ' -

' . __('License details', 'softwarelicensemanager') . '

- - - - - - - - - '; +

' . esc_html__('License Details', 'slmplus') . '

+
' . __('License key', 'softwarelicensemanager') . '' . __('Type', 'softwarelicensemanager') . '
+ + + + + + + + '; foreach ($licences as $lic_row) { echo ' - - - - - '; + + + + + '; } echo ' - -
' . esc_html__('License Key', 'slmplus') . '' . esc_html__('Type', 'slmplus') . '
- ' . $lic_row['lic_key'] . ' - ' . __('view my licenses', 'softwarelicensemanager') . ' - - ' . $lic_row['lic_type'] . ' -
+ ' . esc_html($lic_row['lic_key']) . ' - + ' . esc_html__('View My Licenses', 'slmplus') . ' + + ' . esc_html($lic_row['lic_type']) . ' +
- '; + + + '; } } +add_action('woocommerce_order_details_after_order_table', 'slm_order_details'); + -/** - * @snippet Add Content to the Customer Processing Order Email - WooCommerce - * https://businessbloomer.com/woocommerce-add-extra-content-order-email/ - */ add_action('woocommerce_email_before_order_table', 'slm_add_license_to_order_confirmation', 20, 4); +/** + * Adds license key information to the order confirmation email. + * + * @param WC_Order $order Order object. + * @param bool $sent_to_admin Whether the email is sent to the admin. + * @param bool $plain_text Whether the email is in plain text. + * @param WC_Email $email Email object. + */ function slm_add_license_to_order_confirmation($order, $sent_to_admin, $plain_text, $email) { - if ($email->id == 'customer_completed_order') { - $items = $order->get_items(); - $licences = array(); - foreach ($items as $item_key => $item_details) { - $product = $item_details->get_product(); - if ($product->is_type('slm_license')) { - $meta_data = wc_get_order_item_meta($item_details->get_id(), '_slm_lic_key', false); - foreach ($meta_data as $meta_row) { - $licences[] = array( - 'product' => $product->get_name(), - 'lic_key' => $meta_row, - ); - } - } - } - if ($licences) { - echo ' - - - - - - - - '; - foreach ($licences as $lic_row) { - echo ' - - - - - '; + // Only display the license information in customer completed order emails + if ($email->id !== 'customer_completed_order') { + return; + } + + // Fetch the order items + $items = $order->get_items(); + $licenses = []; + + // Iterate through items to find licenses + foreach ($items as $item_key => $item_details) { + $product = $item_details->get_product(); + // Check if the product type is 'slm_license' + if ($product && $product->is_type('slm_license')) { + $meta_data = wc_get_order_item_meta($item_details->get_id(), '_slm_lic_key', false); + + // Store license information in an array + foreach ($meta_data as $meta_row) { + $licenses[] = [ + 'product' => sanitize_text_field($product->get_name()), + 'lic_key' => sanitize_text_field($meta_row), + ]; } - echo ' - -
- ' . __('License keys', 'softwarelicensemanager') . ' -
- ' . $lic_row['product'] . ' - - ' . $lic_row['lic_key'] . ' -
-

- '; } } + + // If there are licenses, add them to the email + if (!empty($licenses)) { + ?> +

+ + + + + + + + + + + + + + + +
+ + + +
+ + + +
+

+ get_type()) { + + // Ensure $product is a valid WooCommerce product object + if (!$product || !is_a($product, 'WC_Product')) { + return; + } + + // Get the product type with backward compatibility handling + $product_type = ''; + if (method_exists($product, 'get_type')) { + $product_type = $product->get_type(); + } else { + $product_type = $product->product_type; // For older versions + } + + // Check if the product type is 'slm_license' + if ($product_type === 'slm_license') { $template_path = SLM_WOO . 'templates/'; + + // Ensure the template path exists, if not fall back to plugin's default template directory + if (!file_exists($template_path . 'single-product/add-to-cart/slm_license.php')) { + // If template is not found, provide a fallback to default WooCommerce template directory or a custom one + $template_path = plugin_dir_path(__FILE__) . 'templates/'; + } + // Load the template wc_get_template( 'single-product/add-to-cart/slm_license.php', - '', + array(), '', trailingslashit($template_path) ); } -} \ No newline at end of file +} diff --git a/woocommerce/includes/slm-meta-boxes.php b/woocommerce/includes/slm-meta-boxes.php index 3dcebff..8d935de 100644 --- a/woocommerce/includes/slm-meta-boxes.php +++ b/woocommerce/includes/slm-meta-boxes.php @@ -1,367 +1,358 @@ version, '3.0.0', '>=' ) ) { + class WC_Product_SLM_License extends WC_Product_Simple { + protected $product_type = 'slm_license'; -add_filter('woocommerce_product_data_tabs', 'wc_slm_add_tab'); -add_action('woocommerce_process_product_meta', 'wc_slm_save_data'); -add_action('woocommerce_product_data_panels', 'wc_slm_data_panel'); -//add_filter('product_type_options', 'add_wc_slm_data_tab_enabled_product_option'); //legacy + public function __construct( $product = 0 ) { + parent::__construct( $product ); + } + + public function get_type() { + return 'slm_license'; + } + } + } else { + // Older versions use WC_Product as the base class + class WC_Product_SLM_License extends WC_Product { + protected $product_type = 'slm_license'; + + public function __construct( $product = 0 ) { + parent::__construct( $product ); + } + + public function get_type() { + return 'slm_license'; + } + } + } + } +} add_action('init', 'slm_register_product_type'); -add_filter( 'woocommerce_product_class', 'slm_register_product_class', 10, 2 ); + + +function slm_register_product_class($classname, $product_type) { + if ($product_type == 'slm_license') { + $classname = 'WC_Product_SLM_License'; + } + return $classname; +} +add_filter('woocommerce_product_class', 'slm_register_product_class', 10, 2); + + +function slm_add_product_type($types) { + $types['slm_license'] = __('License product', 'slmplus'); + return $types; + error_log("Saving product type for Product ID: " . $types); + +} add_filter('product_type_selector', 'slm_add_product_type'); -add_action('admin_footer', 'slm_license_admin_custom_js'); + + /** - * Add 'License' product option + * Add 'License Manager' product option. */ -function add_wc_slm_data_tab_enabled_product_option($product_type_options) -{ - $product_type_options['wc_slm_data_tab_enabled'] = array( - 'id' => '_wc_slm_data_tab_enabled', - 'wrapper_class' => 'show_if_slm_license', - 'label' => __('License Manager', 'softwarelicensemanager'), - 'default' => 'no', - 'description' => __('Enables the license creation api.', 'softwarelicensemanager') - ); +function add_wc_slm_data_tab_enabled_product_option($product_type_options) { + // Check if the current product type is the custom license product type + if (isset($_GET['product_type']) && $_GET['product_type'] === 'slm_license') { + $product_type_options['wc_slm_data_tab_enabled'] = array( + 'id' => '_wc_slm_data_tab_enabled', + 'wrapper_class' => 'show_if_slm_license', + 'label' => __('License Manager', 'slmplus'), + 'default' => 'no', + 'description' => __('Enables the license creation API.', 'slmplus'), + 'type' => 'checkbox' + ); + } return $product_type_options; } +add_filter('product_type_options', 'add_wc_slm_data_tab_enabled_product_option', 10); -/** CSS To Add Custom tab Icon */ -function wcpp_custom_style() -{ - ?> - __('Licensing', 'softwarelicensemanager'), - 'target' => 'wc_slm_meta', - 'class' => array('show_if_slm_license', 'show_if_wc_slm_data_tab_enabled'), - ); - return $wc_slm_data_tabs; - } +} +add_action('admin_head', 'wcpp_custom_style'); + +/** + * Add Custom WooCommerce Product Data Tab + */ +function wc_slm_add_tab($wc_slm_data_tabs) { + $wc_slm_data_tabs['wc_slm_data_tab'] = array( + 'label' => __('License Info', 'slmplus'), + 'target' => 'wc_slm_meta', + 'class' => array('show_if_slm_license', 'show_if_wc_slm_data_tab_enabled'), + ); + return $wc_slm_data_tabs; +} +add_filter('woocommerce_product_data_tabs', 'wc_slm_add_tab'); - function wc_slm_data_panel(){ - global $post; - $product_id=get_the_ID(); - $slm_options = get_option('slm_plugin_options'); +/** + * Custom WooCommerce Data Panel + */ +function wc_slm_data_panel() { + global $post; + $product_id = get_the_ID(); + $slm_options = get_option('slm_plugin_options'); ?>
-
'_domain_licenses', - 'label' => __('Domain Licenses', 'softwarelicensemanager'), - 'placeholder' => SLM_Helper_Class::slm_get_option('default_max_domains'), - 'desc_tip' => 'true', - 'value' => $value, - 'type' => 'number', - 'custom_attributes' => array( - 'step' => 'any', - 'min' => 0, - ), - 'description' => __('Enter the allowed amount of domains this license can have (websites).', 'softwarelicensemanager') - ) - ); - $value = get_post_meta($product_id, '_devices_licenses',true); - if($value === ''){ - $value = SLM_Helper_Class::slm_get_option('default_max_devices'); - } - woocommerce_wp_text_input( - array( - 'id' => '_devices_licenses', - 'label' => __('Devices Licenses', 'softwarelicensemanager'), - 'placeholder' => SLM_Helper_Class::slm_get_option('default_max_devices'), - 'desc_tip' => 'true', - 'value' => $value, - 'type' => 'number', - 'custom_attributes' => array( - 'step' => 'any', - 'min' => 0, - ), - 'description' => __('Enter the allowed amount of devices this license can have (computers, mobile, etc).', 'softwarelicensemanager') - ) - ); - if ($slm_options['slm_multiple_items']==1){ - woocommerce_wp_text_input( - array( - 'id' => '_license_item_reference', - 'label' => __('Item reference', 'softwarelicensemanager'), - 'placeholder' => "Software's item reference", - 'desc_tip' => 'true', - 'description' => __('Enter the item reference of your application, theme, or plug-in. The licence will be then bound to this exact software.', 'softwarelicensemanager') - ) - ); - } - woocommerce_wp_select( - array( - 'id' => '_license_type', - 'label' => __('License Type', 'softwarelicensemanager'), - 'placeholder' => 'Select one', - 'desc_tip' => 'true', - 'description' => __('type of license: subscription base or lifetime', 'softwarelicensemanager'), - 'options' => array( - 'none' => __('Select one', 'softwarelicensemanager'), - 'subscription' => __('subscription', 'softwarelicensemanager'), - 'lifetime' => __('lifetime', 'softwarelicensemanager'), - ) - ) - ); - - echo '
'; - woocommerce_wp_text_input( - array( - 'id' => '_license_renewal_period', - 'label' => __('Renewal period lenght ', 'softwarelicensemanager'), - 'placeholder' => '0', - 'description' => __('Amount of days or months or years', 'softwarelicensemanager'), - ) - ); - - woocommerce_wp_select( - array( - 'id' => '_license_renewal_period_term', - 'label' => __('Expiration term', 'softwarelicensemanager'), - 'placeholder' => 'days', - 'description' => __('Choose between days or months or years', 'softwarelicensemanager'), - 'options' => array( - 'days' => __('Day(s)', 'softwarelicensemanager'), - 'months' => __('Month(s)', 'softwarelicensemanager'), - 'years' => __('Year(s)', 'softwarelicensemanager'), - 'onetime' => __('One Time', 'softwarelicensemanager'), - ) - ) - ); - echo '

'; - - woocommerce_wp_text_input( - array( - 'id' => '_license_current_version', - 'label' => __('Current Version', 'softwarelicensemanager'), - 'placeholder' => '0.0.0', - 'desc_tip' => 'true', - 'description' => __('Enter the current version of your application, theme, or plug-in', 'softwarelicensemanager') - ) - ); - - woocommerce_wp_text_input( - array( - 'id' => '_license_until_version', - 'label' => __('Until Version', 'softwarelicensemanager'), - 'placeholder' => '0.0.0', - 'desc_tip' => 'true', - 'description' => __('Enter the version until support expires.', 'softwarelicensemanager') - ) - ); - ?> -
-
'_domain_licenses', + 'label' => __('Domain Licenses', 'slmplus'), + 'placeholder' => SLM_Helper_Class::slm_get_option('default_max_domains'), + 'desc_tip' => 'true', + 'value' => $value, + 'type' => 'number', + 'custom_attributes' => array('step' => 'any', 'min' => 0), + 'description' => __('Enter the allowed number of domains this license can have (websites).', 'slmplus') + )); + + // Device Licenses Input + $value = get_post_meta($product_id, '_devices_licenses', true); + $value = ($value === '') ? SLM_Helper_Class::slm_get_option('default_max_devices') : $value; + + woocommerce_wp_text_input(array( + 'id' => '_devices_licenses', + 'label' => __('Devices Licenses', 'slmplus'), + 'placeholder' => SLM_Helper_Class::slm_get_option('default_max_devices'), + 'desc_tip' => 'true', + 'value' => $value, + 'type' => 'number', + 'custom_attributes' => array('step' => 'any', 'min' => 0), + 'description' => __('Enter the allowed number of devices this license can have (computers, mobile, etc).', 'slmplus') + )); + + // Item Reference Field (if enabled) + if (!empty($slm_options['slm_multiple_items']) && $slm_options['slm_multiple_items'] == 1) { + woocommerce_wp_text_input(array( + 'id' => '_license_item_reference', + 'label' => __('Item Reference', 'slmplus'), + 'placeholder' => __("Software's item reference"), + 'desc_tip' => 'true', + 'description' => __('Enter the item reference of your application, theme, or plug-in. The license will be then bound to this exact software.', 'slmplus') + )); } - /** Hook callback function to save custom fields information */ - function wc_slm_save_data($post_id) - { - // _domain_licenses - // _devices_licenses - // _license_type - // _license_current_version - // _license_until_version + // License Type Dropdown + woocommerce_wp_select(array( + 'id' => '_license_type', + 'label' => __('License Type', 'slmplus'), + 'desc_tip' => 'true', + 'description' => __('Type of license: subscription base or lifetime', 'slmplus'), + 'options' => array( + 'none' => __('Select one', 'slmplus'), + 'subscription' => __('Subscription', 'slmplus'), + 'lifetime' => __('Lifetime', 'slmplus'), + ) + )); - $_domain_licenses = $_POST['_domain_licenses']; - update_post_meta($post_id, '_domain_licenses', esc_attr($_domain_licenses)); + // License Renewal Period Length + woocommerce_wp_text_input(array( + 'id' => '_license_renewal_period_lenght', + 'label' => __('Renewal Period Length', 'slmplus'), + 'description' => __('Amount of days, months, or years.', 'slmplus'), + 'type' => 'text', // Change 'number' to 'text' + 'value' => get_post_meta($product_id, '_license_renewal_period_lenght', true) ?: '1', // Fallback to '1' if empty + )); - $is_wc_slm_data_tab_enabled = isset($_POST['_wc_slm_data_tab_enabled']) ? 'yes' : 'no'; - update_post_meta($post_id, '_wc_slm_data_tab_enabled', $is_wc_slm_data_tab_enabled); + - $_license_item_reference = $_POST['_license_item_reference']; - if (!empty($_license_item_reference)) { - update_post_meta($post_id, '_license_item_reference', esc_attr($_license_item_reference)); - } - else { - update_post_meta($post_id, '_license_item_reference', esc_attr('default')); - } + // License Renewal Period Term Dropdown + woocommerce_wp_select(array( + 'id' => '_license_renewal_period_term', + 'label' => __('Expiration Term', 'slmplus'), + 'placeholder' => 'select time frame', + 'description' => __('Choose between days, months, or years', 'slmplus'), + 'options' => array( + 'days' => __('Day(s)', 'slmplus'), + 'months' => __('Month(s)', 'slmplus'), + 'years' => __('Year(s)', 'slmplus'), + 'onetime' => __('One Time', 'slmplus'), + ), + 'value' => get_post_meta($product_id, '_license_renewal_period_term', true) ?: 'years', // Ensure default value is set to 'years' if empty + )); - $_license_type = $_POST['_license_type']; - if (!empty($_license_type)) { - update_post_meta($post_id, '_license_type', esc_attr($_license_type)); - } + echo '

'; - $_devices_licenses = $_POST['_devices_licenses']; - update_post_meta($post_id, '_devices_licenses', esc_attr($_devices_licenses)); + // Current Version Input + woocommerce_wp_text_input(array( + 'id' => '_license_current_version', + 'label' => __('Current Version', 'slmplus'), + 'placeholder' => '0.0.0', + 'desc_tip' => 'true', + 'description' => __('Enter the current version of your application, theme, or plug-in', 'slmplus') + )); - $_license_renewal_period = $_POST['_license_renewal_period']; - if (!empty($_license_renewal_period) && $_license_type == 'lifetime') { - update_post_meta($post_id, '_license_renewal_period', esc_attr('0')); - } - else { - update_post_meta($post_id, '_license_renewal_period', esc_attr($_license_renewal_period)); - } + // Until Version Input + woocommerce_wp_text_input(array( + 'id' => '_license_until_version', + 'label' => __('Until Version', 'slmplus'), + 'placeholder' => '0.0.0', + 'desc_tip' => 'true', + 'description' => __('Enter the version until support expires.', 'slmplus') + )); + ?> +
+ + product_type = 'slm_license'; - parent::__construct($product); - } - } - } - - function slm_register_product_class( $classname, $product_type ) { - if ( $product_type == 'slm_license' ) { - $classname = 'WC_Product_SLM_License'; - } - return $classname; - } + // Save the tab enable option + $is_wc_slm_data_tab_enabled = isset($_POST['_wc_slm_data_tab_enabled']) ? 'yes' : 'no'; + update_post_meta($post_id, '_wc_slm_data_tab_enabled', $is_wc_slm_data_tab_enabled); - function slm_add_product_type($types){ - $types['slm_license'] = __('License product', 'softwarelicensemanager'); - return $types; - } - function slm_license_admin_custom_js(){ - if ('product' != get_post_type()) : - return; - endif; - $slm_options = get_option('slm_plugin_options'); - $affect_downloads = $slm_options['slm_woo_affect_downloads']==1 ? true : false; - ?> + // Save the item reference + $_license_item_reference = isset($_POST['_license_item_reference']) ? sanitize_text_field($_POST['_license_item_reference']) : ''; + update_post_meta($post_id, '_license_item_reference', empty($_license_item_reference) ? 'default' : $_license_item_reference); + + // Save license type + $_license_type = isset($_POST['_license_type']) ? sanitize_text_field($_POST['_license_type']) : ''; + update_post_meta($post_id, '_license_type', $_license_type); + + // Sanitize and save device licenses + $_devices_licenses = isset($_POST['_devices_licenses']) ? intval($_POST['_devices_licenses']) : 0; + update_post_meta($post_id, '_devices_licenses', $_devices_licenses); + + // Handle license renewal period + + $_license_renewal_period_lenght = isset($_POST['_license_renewal_period_lenght']) ? sanitize_text_field($_POST['_license_renewal_period_lenght']) : ''; + update_post_meta($post_id, '_license_renewal_period_lenght', $_license_renewal_period_lenght); + + // Handle license renewal period term + if (isset($_POST['_license_renewal_period_term'])) { + $_license_renewal_period_term = sanitize_text_field($_POST['_license_renewal_period_term']); + if ($_license_type == 'lifetime' && $_license_renewal_period_term !== 'onetime') { + $_license_renewal_period_term = 'onetime'; + } + update_post_meta($post_id, '_license_renewal_period_term', $_license_renewal_period_term); + } + + // Save current version + $_license_current_version = isset($_POST['_license_current_version']) ? sanitize_text_field($_POST['_license_current_version']) : ''; + update_post_meta($post_id, '_license_current_version', $_license_current_version); + + // Save until version + $_license_until_version = isset($_POST['_license_until_version']) ? sanitize_text_field($_POST['_license_until_version']) : ''; + update_post_meta($post_id, '_license_until_version', $_license_until_version); +} + +add_action('woocommerce_process_product_meta', 'wc_slm_save_data'); + +function slm_license_admin_custom_js() { + if ('product' !== get_post_type()) { + return; + } + + $slm_options = get_option('slm_plugin_options', array()); + $affect_downloads = isset($slm_options['slm_woo_affect_downloads']) && $slm_options['slm_woo_affect_downloads'] === '1'; + + ?> -includes(); - } - return self::$instance; - } - - private function includes() { - require_once SLM_WOO . 'includes/register-template.php'; - require_once SLM_WOO . 'includes/purchase.php'; - } - } - return WOO_SLM::instance(); \ No newline at end of file diff --git a/woocommerce/includes/wc_licenses_class.php b/woocommerce/includes/wc_licenses_class.php index 3d9eb86..1be3d2b 100755 --- a/woocommerce/includes/wc_licenses_class.php +++ b/woocommerce/includes/wc_licenses_class.php @@ -8,8 +8,33 @@ // If this file is called directly, abort. if (!defined('WPINC')) { die(); + } +//slm_woo_downloads +function slm_remove_downloads_from_account_menu($items) { + // Remove "Downloads" menu item. + unset($items['downloads']); + return $items; +} + +function slm_disable_downloads_endpoint_redirect() { + // Check if the current endpoint is "downloads" and if it's part of the My Account page. + if (is_wc_endpoint_url('downloads')) { + // Redirect to the My Account dashboard. + wp_safe_redirect(wc_get_page_permalink('myaccount')); + exit; + } +} + +$enable_downloads_page = SLM_API_Utility::get_slm_option('slm_woo_downloads'); + // Check if the 'enable_downloads_page' option is enabled. + if ($enable_downloads_page == 1) { + // If the option is set and enabled, trigger the action. + add_action('template_redirect', 'slm_disable_downloads_endpoint_redirect'); + add_filter('woocommerce_account_menu_items', 'slm_remove_downloads_from_account_menu', 10); + } + class SLM_Woo_Account { public static $endpoint = 'my-licenses'; @@ -55,7 +80,7 @@ public function endpoint_title($title) $is_endpoint = isset($wp_query->query_vars[self::$endpoint]); if ($is_endpoint && !is_admin() && is_main_query() && in_the_loop() && is_account_page()) { // New page title. - $title = __('My Licenses', 'softwarelicensemanager'); + $title = __('My Licenses', 'slmplus'); remove_filter('the_title', array($this, 'endpoint_title')); } return $title; @@ -67,7 +92,7 @@ public function slm_woo_menu_list($items) $logout = $items['customer-logout']; unset($items['customer-logout']); // Insert your custom endpoint. - $items[self::$endpoint] = __('My Licenses', 'softwarelicensemanager'); + $items[self::$endpoint] = __('My Licenses', 'slmplus'); // Insert back the logout item. $items['customer-logout'] = $logout; return $items; @@ -94,9 +119,9 @@ public function endpoint_content() if (empty($result)) { ?>
- + - +
- - - - - - + + + + + + - #purchase_id_; ?> + #purchase_id_; ?> - + lic_status; ?>
- + product_ref; $product_name = get_the_title($product_id); @@ -138,9 +163,9 @@ public function endpoint_content() ?> - license_key; ?> + license_key; ?> - + date_expiry); $today = new DateTime(); @@ -156,7 +181,7 @@ public function endpoint_content() } ?> - + @@ -190,15 +215,15 @@ public function endpoint_content() @@ -207,15 +232,15 @@ public function endpoint_content()
- +
@@ -287,9 +312,9 @@ public function endpoint_content() jQuery.get('' + 'wp-admin/admin-ajax.php?action=del_activation&id=' + id + '&activation_type=' + activation_type, function(data) { if (data == 'success') { jQuery(class_name).remove(); - jQuery('.slm_ajax_msg').html(''); + jQuery('.slm_ajax_msg').html(''); } else { - jQuery('.slm_ajax_msg').html(''); + jQuery('.slm_ajax_msg').html(''); } }); });