Skip to content

Commit 4f0405b

Browse files
committed
feat: Update log creation to support schema changes and enhance compatibility
- Updated the `create_log` method to handle the new `time` (DATETIME) and `time_only` (TIME) columns. - Ensured sanitized input for `license_key`, `action`, and `origin`. - Improved error handling with detailed success and failure logs. - Streamlined origin determination for better readability. - Added example usage and database insertion validation.
1 parent bf5734b commit 4f0405b

File tree

6 files changed

+196
-102
lines changed

6 files changed

+196
-102
lines changed

admin/slm-add-licenses.php

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -192,45 +192,84 @@ function slm_add_licenses_menu()
192192

193193
case 'activity': ?>
194194

195-
<?php
196-
// Retrieve the license key for the current record
197-
$license_key = esc_attr($data['license_key']);
198195

199-
// Fetch the log data using a utility function to handle the database query
200-
$log_entries = SLM_Helper_Class::get_license_logs($license_key);
201196

202-
// Display the log table if there are any log entries
203-
if ($log_entries) {
204-
?>
205-
<div class="wrap">
206-
<h2><?php esc_html_e('Activity Log', 'slm-plus'); ?></h2>
207-
<table class="widefat striped">
208-
<thead>
209-
<tr>
210-
<th><?php esc_html_e('ID', 'slm-plus'); ?></th>
211-
<th><?php esc_html_e('Action', 'slm-plus'); ?></th>
212-
<th><?php esc_html_e('Time', 'slm-plus'); ?></th>
213-
<th><?php esc_html_e('Source', 'slm-plus'); ?></th>
214-
</tr>
215-
</thead>
216-
<tbody>
217-
<?php foreach ($log_entries as $entry): ?>
218-
<tr>
219-
<td><?php echo esc_html($entry['id']); ?></td>
220-
<td><?php echo esc_html($entry['slm_action']); ?></td>
221-
<td><?php echo esc_html($entry['time']); ?></td>
222-
<td><?php echo esc_html($entry['source']); ?></td>
223-
</tr>
224-
<?php endforeach; ?>
225-
</tbody>
226-
</table>
227-
</div>
197+
<?php
198+
global $wpdb;
199+
$table_name = SLM_TBL_LIC_LOG;
200+
201+
// Preserve existing query parameters
202+
$current_url_params = $_GET;
203+
unset($current_url_params['orderby'], $current_url_params['order']); // Remove old sorting params
204+
$current_url = http_build_query($current_url_params); // Build the remaining query string
205+
206+
// Fetch sorting parameters with defaults
207+
$orderby = isset($_GET['orderby']) ? esc_sql($_GET['orderby']) : 'time';
208+
$order = isset($_GET['order']) && in_array(strtoupper($_GET['order']), ['ASC', 'DESC']) ? esc_sql($_GET['order']) : 'DESC';
209+
210+
// Validate $orderby to ensure it's a valid column
211+
$valid_columns = ['id', 'slm_action', 'time', 'time_only', 'source'];
212+
if (!in_array($orderby, $valid_columns)) {
213+
$orderby = 'time'; // Fallback to default
214+
}
215+
216+
// Fetch data with sorting
217+
$query = $wpdb->prepare(
218+
"SELECT * FROM $table_name WHERE license_key = %s ORDER BY $orderby $order LIMIT 10 OFFSET %d",
219+
esc_sql($data['license_key']),
220+
(isset($_GET['paged']) ? intval($_GET['paged'] - 1) * 10 : 0) // Calculate offset for pagination
221+
);
222+
$log_entries = $wpdb->get_results($query, ARRAY_A);
223+
?>
224+
225+
<div class="wrap">
226+
<h2><?php esc_html_e('Activity Log', 'slm-plus'); ?></h2>
227+
<?php if (!empty($log_entries)): ?>
228+
<table class="widefat striped">
229+
<thead>
230+
<tr>
228231
<?php
229-
} else {
230-
// Show a message if there are no log entries
231-
echo '<p>' . esc_html__('No activity log found for this license.', 'slm-plus') . '</p>';
232-
}
232+
// Define all columns to be displayed and sortable
233+
$columns = [
234+
'id' => __('ID', 'slm-plus'),
235+
'slm_action' => __('Action', 'slm-plus'),
236+
'time' => __('Date & Time', 'slm-plus'),
237+
'source' => __('Source', 'slm-plus'),
238+
];
239+
240+
// Render table headers with sorting links
241+
foreach ($columns as $column_key => $column_name):
242+
$next_order = ($orderby === $column_key && $order === 'ASC') ? 'DESC' : 'ASC';
233243
?>
244+
<th>
245+
<a href="?<?php echo $current_url; ?>&orderby=<?php echo esc_attr($column_key); ?>&order=<?php echo esc_attr($next_order); ?>">
246+
<?php echo esc_html($column_name); ?>
247+
<?php if ($orderby === $column_key): ?>
248+
<?php echo $order === 'ASC' ? '&#9650;' : '&#9660;'; ?> <!-- Arrow for sorting -->
249+
<?php endif; ?>
250+
</a>
251+
</th>
252+
<?php endforeach; ?>
253+
</tr>
254+
</thead>
255+
<tbody>
256+
<?php foreach ($log_entries as $entry): ?>
257+
<tr>
258+
<td><?php echo esc_html($entry['id']); ?></td>
259+
<td><?php echo esc_html($entry['slm_action']); ?></td>
260+
<td><?php echo esc_html($entry['time']); ?></td>
261+
<td><?php echo esc_html($entry['source']); ?></td>
262+
</tr>
263+
<?php endforeach; ?>
264+
</tbody>
265+
</table>
266+
<?php else: ?>
267+
<p><?php esc_html_e('No activity log found for this license.', 'slm-plus'); ?></p>
268+
<?php endif; ?>
269+
</div>
270+
271+
272+
234273

235274
<?php
236275
break;

includes/class-slm-installer.php

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,48 @@
3737
// Check if the 'associated_orders' column exists
3838
$column_exists = $wpdb->get_results("SHOW COLUMNS FROM $lic_key_table LIKE 'associated_orders'");
3939

40+
// Ensure the 'time' column is DATETIME
41+
$check_time_column = $wpdb->get_results("SHOW COLUMNS FROM $lic_log_tbl LIKE 'time'");
42+
if (!empty($check_time_column)) {
43+
$time_column_info = $wpdb->get_row("SHOW COLUMNS FROM $lic_log_tbl WHERE Field = 'time'");
44+
if ($time_column_info->Type !== 'datetime') {
45+
error_log("SLM: Updating 'time' column to DATETIME in $lic_log_tbl.");
46+
$update_time_column_query = "
47+
ALTER TABLE $lic_log_tbl
48+
MODIFY COLUMN time DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00';
49+
";
50+
$update_time_column_result = $wpdb->query($update_time_column_query);
51+
if ($update_time_column_result === false) {
52+
error_log("SLM: Error updating 'time' column - " . $wpdb->last_error);
53+
} else {
54+
error_log("SLM: 'time' column updated successfully to DATETIME.");
55+
}
56+
} else {
57+
error_log("SLM: 'time' column is already DATETIME.");
58+
}
59+
} else {
60+
error_log("SLM: 'time' column does not exist in $lic_log_tbl. Skipping update.");
61+
}
62+
63+
// Add the 'time_only' column if it doesn't exist
64+
$check_time_only_column = $wpdb->get_results("SHOW COLUMNS FROM $lic_log_tbl LIKE 'time_only'");
65+
if (empty($check_time_only_column)) {
66+
error_log("SLM: Adding missing column 'time_only' to $lic_log_tbl.");
67+
$add_time_only_column_query = "
68+
ALTER TABLE $lic_log_tbl
69+
ADD COLUMN time_only TIME NOT NULL DEFAULT '00:00:00';
70+
";
71+
$add_time_only_column_result = $wpdb->query($add_time_only_column_query);
72+
if ($add_time_only_column_result === false) {
73+
error_log("SLM: Error adding 'time_only' column - " . $wpdb->last_error);
74+
} else {
75+
error_log("SLM: 'time_only' column added successfully.");
76+
}
77+
} else {
78+
error_log("SLM: Column 'time_only' already exists in $lic_log_tbl.");
79+
}
80+
81+
4082
if (empty($column_exists)) {
4183
error_log("SLM: Adding missing column 'associated_orders' to $lic_key_table.");
4284

@@ -200,16 +242,18 @@
200242
dbDelta($slm_emails_tbl);
201243

202244
// Create log table if not exists
203-
$log_tbl_sql = "CREATE TABLE IF NOT EXISTS " . $lic_log_tbl . " (
204-
id INT NOT NULL AUTO_INCREMENT,
205-
license_key varchar(255) NOT NULL,
206-
slm_action varchar(255) NOT NULL,
207-
time date NOT NULL DEFAULT '0000-00-00',
208-
source varchar(255) NOT NULL,
209-
PRIMARY KEY (id)
210-
)" . $charset_collate . ";";
245+
$log_tbl_sql = "CREATE TABLE IF NOT EXISTS" . $lic_log_tbl . " (
246+
id INT NOT NULL AUTO_INCREMENT,
247+
license_key VARCHAR(255) NOT NULL,
248+
slm_action VARCHAR(255) NOT NULL,
249+
time DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', -- Store combined date and time
250+
time_only TIME NOT NULL DEFAULT '00:00:00', -- Store time only
251+
source VARCHAR(255) NOT NULL,
252+
PRIMARY KEY (id)
253+
) $charset_collate;";
211254
dbDelta($log_tbl_sql);
212255

256+
213257
// Create devices table if not exists
214258
$ldv_tbl_sql = "CREATE TABLE IF NOT EXISTS " . $lic_devices_table . " (
215259
id INT NOT NULL AUTO_INCREMENT,

includes/slm-plugin-core.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function slmplus_init_handler()
140140
function slmplus_plugins_loaded_handler()
141141
{
142142
if (is_admin() && get_option('slm_db_version') != SLM_DB_VERSION) {
143-
require_once(SLM_LIB . 'class-slm-installer.php');
143+
require_once SLM_LIB . 'class-slm-installer.php';
144144
// TODO - Implement DB update logic here
145145
}
146146
}
@@ -208,8 +208,8 @@ function wc_log($msg)
208208

209209
// WooCommerce integration
210210
if (SLM_Helper_Class::slm_get_option('slm_woo') == 1 && is_plugin_active('woocommerce/woocommerce.php')) {
211-
require_once(SLM_WOO . 'includes/wc_licenses_class.php');
212-
require_once(SLM_WOO . 'includes/slm-meta-boxes.php');
211+
require_once SLM_WOO . 'includes/wc_licenses_class.php';
212+
require_once SLM_WOO . 'includes/slm-meta-boxes.php';
213213
require_once SLM_WOO . 'includes/register-template.php';
214214
require_once SLM_WOO . 'includes/purchase.php';
215215
require_once SLM_WOO . 'includes/create-license-orders.php';

includes/slm-utility.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,36 +1182,47 @@ public static function create_log($license_key, $action)
11821182
{
11831183
global $wpdb;
11841184
$slm_log_table = SLM_TBL_LIC_LOG;
1185-
1185+
11861186
// Sanitize inputs
11871187
$license_key = sanitize_text_field($license_key);
11881188
$action = sanitize_text_field($action);
1189-
1189+
11901190
// Determine the request origin
1191+
$origin = '';
11911192
if (!empty($_SERVER['HTTP_ORIGIN'])) {
11921193
$origin = sanitize_text_field($_SERVER['HTTP_ORIGIN']);
11931194
} elseif (!empty($_SERVER['HTTP_REFERER'])) {
11941195
$origin = sanitize_text_field($_SERVER['HTTP_REFERER']);
1195-
} else {
1196+
} elseif (!empty($_SERVER['REMOTE_ADDR'])) {
11961197
$origin = sanitize_text_field($_SERVER['REMOTE_ADDR']);
11971198
}
1198-
1199+
1200+
// Get current date and time
1201+
$current_date_time = current_time('mysql'); // Returns 'Y-m-d H:i:s' in WordPress timezone
1202+
$current_time_only = date('H:i:s', strtotime($current_date_time)); // Extract time portion
1203+
11991204
// Prepare log data
1200-
$log_data = array(
1205+
$log_data = [
12011206
'license_key' => $license_key,
12021207
'slm_action' => $action,
1203-
'time' => current_time('mysql'), // Standardized date-time format
1208+
'time' => $current_date_time, // Combined date and time
1209+
'time_only' => $current_time_only, // Time only
12041210
'source' => $origin,
1205-
);
1206-
1211+
];
1212+
12071213
// Insert log data into the database
12081214
$inserted = $wpdb->insert($slm_log_table, $log_data);
1209-
1215+
12101216
// Check for insertion errors
12111217
if ($inserted === false) {
12121218
error_log("Failed to insert log for license key: $license_key, action: $action. Error: " . $wpdb->last_error);
1219+
} else {
1220+
error_log("Log inserted successfully for license key: $license_key, action: $action.");
12131221
}
12141222
}
1223+
1224+
1225+
12151226

12161227

12171228
public static function create_email_log($lic_key, $sent_to, $status, $sent, $date_sent = null)
@@ -1420,3 +1431,4 @@ function slm_woo_tab_lic_info()
14201431
}
14211432
}
14221433
}
1434+

slm-plus.php

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/*
33
Plugin Name: SLM Plus
4-
Version: 6.3.5
4+
Version: 6.3.6
55
Plugin URI: https://github.com/michelve/software-license-manager/
66
Author: Michel Velis
77
Author URI: https://github.com/michelve/
@@ -23,8 +23,7 @@
2323
}
2424

2525
// Load plugin textdomain for multilingual support
26-
function slmplus_load_textdomain()
27-
{
26+
function slmplus_load_textdomain() {
2827
load_plugin_textdomain('slm-plus', false, dirname(plugin_basename(__FILE__)) . '/i18n/languages');
2928
}
3029
add_action('plugins_loaded', 'slmplus_load_textdomain');
@@ -33,53 +32,57 @@ function slmplus_load_textdomain()
3332
global $wpdb, $slm_debug_logger;
3433

3534
// Define constants for plugin paths, URLs, and database tables
36-
define('SLM_VERSION', '6.3.5');
37-
define('SLM_DB_VERSION', '5.8.10');
38-
define('SLM_REWRITE_VERSION', '3.1.3');
39-
define('SLM_FOLDER', dirname(plugin_basename(__FILE__)));
40-
define('SLM_URL', plugins_url('', __FILE__));
41-
define('SLM_ASSETS_URL', SLM_URL . '/public/assets/');
42-
define('SLM_PATH', plugin_dir_path(__FILE__));
43-
define('SLM_LIB', SLM_PATH . 'includes/');
44-
define('SLM_WOO', SLM_PATH . 'woocommerce/');
45-
define('SLM_ADDONS', SLM_PATH . 'addons/');
46-
define('SLM_ADMIN', SLM_PATH . 'admin/');
47-
define('SLM_ADMIN_ADDONS', SLM_ADMIN . 'includes/');
48-
define('SLM_CRONS', SLM_ADMIN_ADDONS . 'cronjobs/');
49-
define('SLM_PUBLIC', SLM_PATH . 'public/');
50-
define('SLM_TEMPLATES', SLM_PATH . 'templates/');
51-
define('SLM_SITE_HOME_URL', get_home_url());
52-
define('SLM_SITE_URL', get_site_url() . '/');
53-
define('SLM_TBL_LICENSE_KEYS', $wpdb->prefix . "lic_key_tbl");
54-
define('SLM_TBL_EMAILS', $wpdb->prefix . "lic_emails_tbl");
55-
define('SLM_TBL_LIC_DOMAIN', $wpdb->prefix . "lic_reg_domain_tbl");
56-
define('SLM_TBL_LIC_DEVICES', $wpdb->prefix . "lic_reg_devices_tbl");
57-
define('SLM_TBL_LIC_LOG', $wpdb->prefix . "lic_log_tbl");
58-
define('SLM_TBL_LICENSE_STATUS', $wpdb->prefix . "lic_status_tbl");
35+
define('SLM_VERSION', '6.3.6');
36+
define('SLM_DB_VERSION', '5.9.1');
37+
define('SLM_REWRITE_VERSION', '3.1.3');
38+
39+
define('SLM_FOLDER', dirname(plugin_basename(__FILE__)));
40+
define('SLM_URL', plugins_url('', __FILE__));
41+
define('SLM_ASSETS_URL', SLM_URL . '/public/assets/');
42+
define('SLM_PATH', plugin_dir_path(__FILE__));
43+
define('SLM_LIB', SLM_PATH . 'includes/');
44+
define('SLM_WOO', SLM_PATH . 'woocommerce/');
45+
define('SLM_ADDONS', SLM_PATH . 'addons/');
46+
define('SLM_ADMIN', SLM_PATH . 'admin/');
47+
define('SLM_ADMIN_ADDONS', SLM_ADMIN . 'includes/');
48+
define('SLM_CRONS', SLM_ADMIN_ADDONS . 'cronjobs/');
49+
define('SLM_PUBLIC', SLM_PATH . 'public/');
50+
define('SLM_TEMPLATES', SLM_PATH . 'templates/');
51+
52+
define('SLM_SITE_HOME_URL', get_home_url());
53+
define('SLM_SITE_URL', get_site_url() . '/');
54+
55+
define('SLM_TBL_LICENSE_KEYS', $wpdb->prefix . "lic_key_tbl");
56+
define('SLM_TBL_EMAILS', $wpdb->prefix . "lic_emails_tbl");
57+
define('SLM_TBL_LIC_DOMAIN', $wpdb->prefix . "lic_reg_domain_tbl");
58+
define('SLM_TBL_LIC_DEVICES', $wpdb->prefix . "lic_reg_devices_tbl");
59+
define('SLM_TBL_LIC_LOG', $wpdb->prefix . "lic_log_tbl");
60+
define('SLM_TBL_LICENSE_STATUS', $wpdb->prefix . "lic_status_tbl");
61+
5962
define('SLM_MANAGEMENT_PERMISSION', 'manage_options');
60-
define('SLM_MAIN_MENU_SLUG', 'slm_overview');
61-
define('SLM_MENU_ICON', 'dashicons-lock');
62-
define('SLM_API_URL', SLM_SITE_URL);
63+
define('SLM_MAIN_MENU_SLUG', 'slm_overview');
64+
define('SLM_MENU_ICON', 'dashicons-lock');
65+
define('SLM_API_URL', SLM_SITE_URL);
6366

6467
// Load core plugin functionalities
6568
if (file_exists(SLM_LIB . 'slm-plugin-core.php')) {
6669
require_once SLM_LIB . 'slm-plugin-core.php';
6770
}
6871

69-
function slm_settings_link($links)
70-
{
72+
// Add settings link to plugin action links
73+
function slm_settings_link($links) {
7174
$settings_link = '<a href="' . esc_url(admin_url('admin.php?page=slm_settings')) . '">' . __('Settings', 'slm-plus') . '</a>';
7275
$links[] = $settings_link;
7376
return $links;
7477
}
7578
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'slm_settings_link');
7679

77-
80+
// Define default max domains and devices
7881
define('SLM_DEFAULT_MAX_DOMAINS', SLM_API_Utility::get_slm_option('default_max_domains'));
7982
define('SLM_DEFAULT_MAX_DEVICES', SLM_API_Utility::get_slm_option('default_max_devices'));
8083

8184
// Use native WordPress function for setting options
82-
define('WOO_SLM_API_SECRET', SLM_API_Utility::get_slm_option('lic_creation_secret'));
83-
define('KEY_API', SLM_API_Utility::get_slm_option('lic_creation_secret'));
84-
define('VERIFY_KEY_API', SLM_API_Utility::get_slm_option('lic_verification_secret'));
85-
define('KEY_API_PREFIX', SLM_API_Utility::get_slm_option('lic_prefix'));
85+
define('WOO_SLM_API_SECRET', SLM_API_Utility::get_slm_option('lic_creation_secret'));
86+
define('KEY_API', SLM_API_Utility::get_slm_option('lic_creation_secret'));
87+
define('VERIFY_KEY_API', SLM_API_Utility::get_slm_option('lic_verification_secret'));
88+
define('KEY_API_PREFIX', SLM_API_Utility::get_slm_option('lic_prefix'));

0 commit comments

Comments
 (0)