Skip to content

Commit

Permalink
Add new license renewal workflow and enhanced templates
Browse files Browse the repository at this point in the history
Features:

Added new license renewal workflow to track historical orders and update associated_orders column for licenses.
Introduced improved handling for WooCommerce order IDs during license renewal.
Added new templates for license management, including renewal pages and license product display suggestions for empty carts.
Improvements:

Enhanced wc_slm_create_new_license to manage expiration dates, logging, and associated orders seamlessly.
Refactored license renewal logic to ensure existing orders are appended to the associated_orders column.
Improved database schema management for adding missing columns like associated_orders, ensuring backward compatibility and smoother upgrades.
Introduced structured retrieval of associated orders with the SLM_Utility::slm_get_associated_orders function to return well-formatted data.
Fixes:

Addressed metadata dependency by replacing _slm_lic_key usage with database lookups for better accuracy and reliability.
Fixed database version handling logic to ensure schema updates are properly applied during version upgrades.
Templates:

Added license cart template with suggested license products display for empty cart scenarios.
Included robust template structure to handle renewal messages and new license purchases dynamically.
Notes:

The new renewal workflow improves traceability by appending historical orders to the license's associated_orders.
Templates are designed to integrate seamlessly with WooCommerce workflows, providing a consistent user experience.
  • Loading branch information
michelve committed Nov 25, 2024
1 parent 1ab8114 commit 3d6a5b3
Show file tree
Hide file tree
Showing 20 changed files with 2,070 additions and 1,267 deletions.
1 change: 1 addition & 0 deletions admin/slm-admin-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function slm_admin_tools_menu()
</form>
</div>
</div>


<div class="postbox">
<h3 class="hndle"><label for="title"><?php esc_html_e('Backup Database', 'slm-plus'); ?></label></h3>
Expand Down
95 changes: 87 additions & 8 deletions admin/slm-lic-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,21 @@ function slm_general_settings()
<?php esc_html_e('Enable WooCommerce downloads expiration. Downloads will expire together with corresponding license.', 'slm-plus'); ?>
</td>
</tr>
</table>

<!-- <h3><?php // esc_html_e('WP eStores', 'slm-plus'); ?> </h3>
<table class="form-table">
<tr valign="top">
<th scope="row"> <?php //_e('WP eStores', 'slm-plus'); ?></th>
<tr>
<th scope="row"><?php esc_html_e('License Cart Page', 'slm-plus'); ?></th>
<td>
<input name="slm_wpestores" type="checkbox" <?php //if ($options['slm_wpestores'] != '') echo ' checked="checked"'; ?> value="1" />
<?php //_e('Enable WordPress eStore Plugin Support.', 'slm-plus'); ?>
<form method="post" action="">
<?php wp_nonce_field('slm_create_license_cart', 'slm_create_license_cart_nonce'); ?>
<button type="submit" name="slm_create_license_cart" class="button button-primary">
<?php esc_html_e('Create License Cart Page', 'slm-plus'); ?>
</button>
<p class="description"><?php esc_html_e('Click this button to create the License Cart page and assign the custom template.', 'slm-plus'); ?></p>
</form>
</td>
</tr>
</table> -->

</table>
</div>
</div>

Expand Down Expand Up @@ -342,3 +345,79 @@ function slm_general_settings()
</div>
<?php
}


add_action('admin_init', 'slm_handle_create_license_cart_page');

function slm_handle_create_license_cart_page() {
// Verify the nonce for security
if (!isset($_POST['slm_create_license_cart_nonce']) || !wp_verify_nonce($_POST['slm_create_license_cart_nonce'], 'slm_create_license_cart')) {
return;
}

// Check if the user has permissions to manage pages
if (!current_user_can('edit_pages')) {
add_settings_error(
'slm_license_cart_error',
'slm_no_permission',
__('You do not have sufficient permissions to create the License Cart page.', 'slm-plus'),
'error'
);
return;
}

// Use WP_Query to check if the page already exists
$query = new WP_Query(array(
'post_type' => 'page',
'name' => 'license-cart', // Check for the slug
'post_status' => 'publish',
'posts_per_page' => 1,
));

if ($query->have_posts()) {
add_settings_error(
'slm_license_cart_error',
'slm_license_cart_exists',
__('The License Cart page already exists.', 'slm-plus'),
'error'
);
return;
}

// Create the License Cart page
$page_id = wp_insert_post(array(
'post_title' => 'License Cart',
'post_content' => '',
'post_status' => 'publish',
'post_name' => 'license-cart', // Set the slug
'post_type' => 'page',
'meta_input' => array('_wp_page_template' => 'page-license-cart.php'), // Assign the custom template
));

// Check if the page was created successfully
if ($page_id && !is_wp_error($page_id)) {
// Hide the page from menus and navigation
update_post_meta($page_id, '_menu_item_visibility', 'hidden');
add_settings_error(
'slm_license_cart_success',
'slm_license_cart_created',
__('The License Cart page was successfully created.', 'slm-plus'),
'updated'
);
} else {
add_settings_error(
'slm_license_cart_error',
'slm_license_cart_failed',
__('Failed to create the License Cart page. Please try again.', 'slm-plus'),
'error'
);
}
}


add_action('admin_notices', 'slm_license_cart_admin_notices');

function slm_license_cart_admin_notices() {
settings_errors('slm_license_cart_error');
settings_errors('slm_license_cart_success');
}
186 changes: 124 additions & 62 deletions includes/class-slm-installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,104 @@
$lic_emails_table = SLM_TBL_EMAILS;
$lic_status_table = SLM_TBL_LICENSE_STATUS; // New Status Table

// Check the current database version
$used_db_version = get_option('slm_db_version', SLM_DB_VERSION);

// Set charset and collation for database tables
$charset_collate = $wpdb->get_charset_collate();

$used_db_version = get_option('slm_db_version', '5.0.0');

// Check the current database version
$new_db_version = SLM_DB_VERSION;

// Table definitions
$lic_key_table = SLM_TBL_LICENSE_KEYS;

// Ensure backward compatibility updates are applied
if (version_compare($used_db_version, $new_db_version, '<')) {
error_log("SLM: Starting database updates from version $used_db_version to $new_db_version.");

// Check if the 'associated_orders' column exists
$column_exists = $wpdb->get_results("SHOW COLUMNS FROM $lic_key_table LIKE 'associated_orders'");

if (empty($column_exists)) {
error_log("SLM: Adding missing column 'associated_orders' to $lic_key_table.");

// Add missing columns to the license keys table
$lk_tbl_sql_mod = "
ALTER TABLE $lic_key_table
ADD COLUMN associated_orders TEXT DEFAULT NULL;
";

$result = $wpdb->query($lk_tbl_sql_mod);

if ($result === false) {
error_log("SLM: Error adding 'associated_orders' column - " . $wpdb->last_error);
} else {
error_log("SLM: 'associated_orders' column added successfully.");
}
} else {
error_log("SLM: Column 'associated_orders' already exists in $lic_key_table.");
}

// Add other missing columns (if required)
$other_columns = array(
'item_reference' => "VARCHAR(255) NOT NULL",
'slm_billing_length' => "VARCHAR(255) NOT NULL",
'slm_billing_interval' => "ENUM('days', 'months', 'years', 'onetime') NOT NULL DEFAULT 'days'",
'wc_order_id' => "INT DEFAULT NULL",
'payment_status' => "ENUM('pending', 'completed', 'failed') DEFAULT 'pending'",
'renewal_attempts' => "INT DEFAULT 0",
'lic_status' => "ENUM('pending', 'active', 'expired', 'suspended', 'blocked', 'trial') NOT NULL DEFAULT 'pending'"
);

foreach ($other_columns as $column => $definition) {
$column_exists = $wpdb->get_results("SHOW COLUMNS FROM $lic_key_table LIKE '$column'");
if (empty($column_exists)) {
// Add missing column
$alter_query = "ALTER TABLE $lic_key_table ADD COLUMN $column $definition;";
error_log("SLM: Adding missing column '$column' to $lic_key_table.");
$result = $wpdb->query($alter_query);

if ($result === false) {
error_log("SLM: Error adding column '$column' - " . $wpdb->last_error);
} else {
error_log("SLM: Column '$column' added successfully.");
}
} else {
// Check if the column definition needs to be updated (for example, updating lic_status ENUM values)
$column_info = $wpdb->get_row("SHOW COLUMNS FROM $lic_key_table LIKE '$column'");
if ($column === 'lic_status' && strpos($column_info->Type, "'trial'") === false) {
// Update lic_status to include the new ENUM values
$alter_query = "ALTER TABLE $lic_key_table MODIFY COLUMN $column $definition;";
error_log("SLM: Updating column '$column' in $lic_key_table.");
$result = $wpdb->query($alter_query);

if ($result === false) {
error_log("SLM: Error updating column '$column' - " . $wpdb->last_error);
} else {
error_log("SLM: Column '$column' updated successfully.");
}
} else {
error_log("SLM: Column '$column' already exists and does not need updates.");
}
}
}

// Update the database version
update_option("slm_db_version", $new_db_version);
error_log("SLM database updated from version $used_db_version to $new_db_version.");
} else {
error_log("SLM: No updates needed for backward compatibility. Current version: $used_db_version.");
}


// Create license statuses table if it doesn't exist
$status_table_sql = "CREATE TABLE IF NOT EXISTS " . $lic_status_table . " (
id INT NOT NULL AUTO_INCREMENT,
status_key VARCHAR(255) NOT NULL,
status_label VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
)" . $charset_collate . ";";
dbDelta($status_table_sql);
id INT(11) NOT NULL AUTO_INCREMENT,
status_key VARCHAR(255) NOT NULL,
status_label VARCHAR(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY unique_status_key (status_key)
) " . $charset_collate . ";";

// Insert default statuses if table is empty
$status_count = $wpdb->get_var("SELECT COUNT(*) FROM $lic_status_table");
Expand All @@ -41,6 +125,7 @@
array('status_key' => 'pending', 'status_label' => __('Pending', 'slm-plus')),
array('status_key' => 'active', 'status_label' => __('Active', 'slm-plus')),
array('status_key' => 'blocked', 'status_label' => __('Blocked', 'slm-plus')),
array('status_key' => 'trial', 'status_label' => __('Trial', 'slm-plus')),
array('status_key' => 'expired', 'status_label' => __('Expired', 'slm-plus'))
);

Expand All @@ -51,62 +136,41 @@

// Create or update the license keys table structure
$lk_tbl_sql = "CREATE TABLE IF NOT EXISTS " . $lic_key_table . " (
id int(12) NOT NULL AUTO_INCREMENT,
license_key varchar(255) NOT NULL,
max_allowed_domains int(40) NOT NULL,
max_allowed_devices int(40) NOT NULL,
lic_status varchar(255) NOT NULL DEFAULT 'pending', /* Store status_key here */
id INT(12) NOT NULL AUTO_INCREMENT,
license_key VARCHAR(255) NOT NULL,
item_reference VARCHAR(255) NOT NULL,
product_ref VARCHAR(255) NOT NULL DEFAULT '',
subscr_id VARCHAR(128) NOT NULL DEFAULT '',
txn_id VARCHAR(64) NOT NULL DEFAULT '',
purchase_id_ VARCHAR(255) NOT NULL DEFAULT '',
wc_order_id INT DEFAULT NULL,
associated_orders TEXT DEFAULT NULL,
first_name VARCHAR(32) NOT NULL DEFAULT '',
last_name VARCHAR(32) NOT NULL DEFAULT '',
email VARCHAR(64) NOT NULL,
company_name VARCHAR(100) NOT NULL DEFAULT '',
lic_status ENUM('pending', 'active', 'expired', 'suspended', 'blocked', 'trial') NOT NULL DEFAULT 'pending',
lic_type ENUM('none', 'subscription', 'lifetime') NOT NULL DEFAULT 'subscription',
first_name varchar(32) NOT NULL DEFAULT '',
last_name varchar(32) NOT NULL DEFAULT '',
email varchar(64) NOT NULL,
item_reference varchar(255) NOT NULL,
company_name varchar(100) NOT NULL DEFAULT '',
txn_id varchar(64) NOT NULL DEFAULT '',
manual_reset_count varchar(128) NOT NULL DEFAULT '',
purchase_id_ varchar(255) NOT NULL DEFAULT '',
date_created date NOT NULL DEFAULT '0000-00-00',
date_activated date NOT NULL DEFAULT '0000-00-00',
date_renewed date NOT NULL DEFAULT '0000-00-00',
date_expiry date NOT NULL DEFAULT '0000-00-00',
reminder_sent varchar(255) NOT NULL DEFAULT '0',
reminder_sent_date date NOT NULL DEFAULT '0000-00-00',
product_ref varchar(255) NOT NULL DEFAULT '',
until varchar(255) NOT NULL DEFAULT '',
current_ver varchar(255) NOT NULL DEFAULT '',
subscr_id varchar(128) NOT NULL DEFAULT '',
slm_billing_length varchar(255) NOT NULL,
max_allowed_domains INT NOT NULL,
max_allowed_devices INT NOT NULL,
manual_reset_count VARCHAR(128) NOT NULL DEFAULT '',
current_ver VARCHAR(255) NOT NULL DEFAULT '',
until VARCHAR(255) NOT NULL DEFAULT '',
slm_billing_length VARCHAR(255) NOT NULL,
slm_billing_interval ENUM('days', 'months', 'years', 'onetime') NOT NULL DEFAULT 'days',
payment_status ENUM('pending', 'completed', 'failed') DEFAULT 'pending',
renewal_attempts INT DEFAULT 0,
date_created DATE NOT NULL DEFAULT '0000-00-00',
date_activated DATE NOT NULL DEFAULT '0000-00-00',
date_renewed DATE NOT NULL DEFAULT '0000-00-00',
date_expiry DATE NOT NULL DEFAULT '0000-00-00',
reminder_sent_date DATE NOT NULL DEFAULT '0000-00-00',
reminder_sent VARCHAR(255) NOT NULL DEFAULT '0',
PRIMARY KEY (id)
)" . $charset_collate . ";";
) " . $charset_collate . ";";

dbDelta($lk_tbl_sql);

// Handle backward compatibility for version 6.1.5 or earlier
if (version_compare($used_db_version, '5.1.1', '<=')) {
// Update $lic_key_table if necessary
$lk_tbl_sql = "
ALTER TABLE $lic_key_table
ADD COLUMN IF NOT EXISTS item_reference varchar(255) NOT NULL,
ADD COLUMN IF NOT EXISTS slm_billing_length varchar(255) NOT NULL,
ADD COLUMN IF NOT EXISTS slm_billing_interval ENUM('days', 'months', 'years', 'onetime') NOT NULL DEFAULT 'days';
";
dbDelta($lk_tbl_sql);

// Remove column 'registered_devices' from $lic_domain_table if it exists
$domain_table_sql = "
ALTER TABLE $lic_domain_table
DROP COLUMN IF EXISTS registered_devices;
";
$wpdb->query($domain_table_sql);

// Remove column 'registered_domain' from $lic_devices_table if it exists
$devices_table_sql = "
ALTER TABLE $lic_devices_table
DROP COLUMN IF EXISTS registered_domain;
";
$wpdb->query($devices_table_sql);
}
dbDelta($lk_tbl_sql);

// Create domains table if not exists
$ld_tbl_sql = "CREATE TABLE IF NOT EXISTS " . $lic_domain_table . " (
Expand Down Expand Up @@ -201,5 +265,3 @@
wp_mkdir_p($backup_dir_path);
}

// Update the database version
update_option("slm_db_version", SLM_DB_VERSION);
Loading

0 comments on commit 3d6a5b3

Please sign in to comment.