Skip to content

Commit

Permalink
added a new action hook
Browse files Browse the repository at this point in the history
  • Loading branch information
michelve committed Jul 13, 2018
1 parent 165b3a7 commit 26635a1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGELOG
All notable changes to this project will be documented in this file.

#### 4.1
- Added: New action hookFadded a new action hook

#### 4.0
- Fixed a typo with the slm_api_response_args filter
Expand Down
39 changes: 8 additions & 31 deletions software-license-manager/includes/slm-third-party-integration.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<?php

/* * ********************************* */
/* * * WP eStore Plugin Integration ** */
/* * ********************************* */
add_filter('eStore_notification_email_body_filter', 'slm_handle_estore_email_body_filter', 10, 3); //Standard sale notification email
add_filter('eStore_squeeze_form_email_body_filter', 'slm_handle_estore_email_body_filter', 10, 3); //Squeeze form email

function slm_handle_estore_email_body_filter($body, $payment_data, $cart_items) {
global $slm_debug_logger, $wpdb;
$slm_debug_logger->log_debug("WP eStore integration - checking if a license key needs to be created for this transaction.");
$products_table_name = $wpdb->prefix . "wp_eStore_tbl";
$slm_data = "";

foreach ($cart_items as $current_cart_item) {
$prod_id = $current_cart_item['item_number'];
$item_name = $current_cart_item['item_name'];
$retrieved_product = $wpdb->get_row("SELECT * FROM $products_table_name WHERE id = '$prod_id'", OBJECT);
$package_product = eStore_is_package_product($retrieved_product);
if ($package_product) {
Expand All @@ -22,33 +20,29 @@ function slm_handle_estore_email_body_filter($body, $payment_data, $cart_items)
foreach ($product_ids as $id) {
$id = trim($id);
$retrieved_product_for_specific_id = $wpdb->get_row("SELECT * FROM $products_table_name WHERE id = '$id'", OBJECT);
$slm_data .= slm_estore_check_and_generate_key($retrieved_product_for_specific_id, $payment_data, $cart_items);
$slm_data .= slm_estore_check_and_generate_key($retrieved_product_for_specific_id, $payment_data, $cart_items, $item_name);
}
} else {
$slm_debug_logger->log_debug('Checking license key generation for single item product.');
$slm_data .= slm_estore_check_and_generate_key($retrieved_product, $payment_data, $cart_items);
$slm_data .= slm_estore_check_and_generate_key($retrieved_product, $payment_data, $cart_items, $item_name);
}
}

$body = str_replace("{slm_data}", $slm_data, $body);
return $body;
}

function slm_estore_check_and_generate_key($retrieved_product, $payment_data, $cart_items) {
function slm_estore_check_and_generate_key($retrieved_product, $payment_data, $cart_items, $item_name) {
global $slm_debug_logger;
$license_data = '';

if ($retrieved_product->create_license == 1) {
$slm_debug_logger->log_debug('Need to create a license key for this product (' . $retrieved_product->id . ')');
$slm_key = slm_estore_create_license($retrieved_product, $payment_data, $cart_items);
$slm_key = slm_estore_create_license($retrieved_product, $payment_data, $cart_items, $item_name);
$license_data = "\n" . __('Item Name: ', 'slm') . $retrieved_product->name . " - " . __('License Key: ', 'slm') . $slm_key;
$slm_debug_logger->log_debug('Liense data: ' . $license_data);
$license_data = apply_filters('slm_estore_item_license_data', $license_data);
}
return $license_data;
}

function slm_estore_create_license($retrieved_product, $payment_data, $cart_items) {
function slm_estore_create_license($retrieved_product, $payment_data, $cart_items, $item_name) {
global $slm_debug_logger;
global $wpdb;
$product_meta_table_name = WP_ESTORE_PRODUCTS_META_TABLE_NAME;
Expand All @@ -57,7 +51,6 @@ function slm_estore_create_license($retrieved_product, $payment_data, $cart_item
$options = get_option('slm_plugin_options');
$lic_key_prefix = $options['lic_prefix'];
$max_domains = $options['default_max_domains'];

//Lets check any product specific configuration.
$prod_id = $retrieved_product->id;
$product_meta = $wpdb->get_row("SELECT * FROM $product_meta_table_name WHERE prod_id = '$prod_id' AND meta_key='slm_max_allowed_domains'", OBJECT);
Expand Down Expand Up @@ -92,11 +85,8 @@ function slm_estore_create_license($retrieved_product, $payment_data, $cart_item
$fields['date_created'] = date("Y-m-d"); //Today's date
$fields['date_expiry'] = $slm_date_of_expiry;
$fields['product_ref'] = $prod_id;//WP eStore product ID

$slm_debug_logger->log_debug('Inserting license data into the license manager DB table.');
$fields = array_filter($fields); //Remove any null values.


$tbl_name = SLM_TBL_LICENSE_KEYS;
$result = $wpdb->insert($tbl_name, $fields);
if (!$result) {
Expand All @@ -108,23 +98,22 @@ function slm_estore_create_license($retrieved_product, $payment_data, $cart_item
$fields['last_name'] = esc_sql($last_name);
$company_name = mb_convert_encoding($fields['company_name'], "UTF-8", "windows-1252");
$fields['company_name'] = esc_sql($company_name);

$result = $wpdb->insert($tbl_name, $fields);
if (!$result) {
$slm_debug_logger->log_debug('Error! Failed to update license key table. DB insert query failed.', false);
}
}
//SLM_API_Utility::insert_license_data_internal($fields);
$prod_args = array('estore_prod_id' => $prod_id, 'estore_item_name' => $item_name);
do_action('slm_estore_license_created', $prod_args, $payment_data, $cart_items, $fields);

return $fields['license_key'];
}

/* Code to handle the eStore's product add/edit interface for SLM specific product configuration */
add_filter('eStore_addon_product_settings_filter', 'slm_estore_product_configuration_html', 10, 2); //Render the product add/edit HTML
add_action('eStore_new_product_added', 'slm_estore_new_product_added', 10, 2); //Handle the DB insert after a product add.
add_action('eStore_product_updated', 'slm_estore_product_updated', 10, 2); //Handle the DB update after a product edit.
add_action('eStore_product_deleted', 'slm_estore_product_deleted'); //Handle the DB delete after a product delete.

function slm_estore_product_configuration_html($product_config_html, $prod_id) {
global $wpdb;
$product_meta_table_name = WP_ESTORE_PRODUCTS_META_TABLE_NAME;
Expand All @@ -151,30 +140,22 @@ function slm_estore_product_configuration_html($product_config_html, $prod_id) {
} else {
$slm_date_of_expiry = "";
}

}

$product_config_html .= '<div class="msg_head">Software License Manager Plugin (Click to Expand)</div><div class="msg_body"><table class="form-table">';

$product_config_html .= '<tr valign="top"><th scope="row">Maximum Allowed Domains</th><td>';
$product_config_html .= '<input name="slm_max_allowed_domains" type="text" id="slm_max_allowed_domains" value="' . $slm_max_allowed_domains . '" size="10" />';
$product_config_html .= '<p class="description">Number of domains/installs in which this license can be used. Leave blank if you wish to use the default value set in the license manager plugin settings.</p>';
$product_config_html .= '</td></tr>';

$product_config_html .= '<tr valign="top"><th scope="row">Number of Days before Expiry</th><td>';
$product_config_html .= '<input name="slm_date_of_expiry" type="text" id="slm_date_of_expiry" value="' . $slm_date_of_expiry . '" size="10" /> Days';
$product_config_html .= '<p class="description">Number of days before expiry. The expiry date of the license will be set based on this value. For example, if you want the key to expire in 6 months then enter a value of 180.</p>';
$product_config_html .= '</td></tr>';

$product_config_html .= '</table></div>';

return $product_config_html;
}

function slm_estore_new_product_added($prod_dat_array, $prod_id) {
global $wpdb;
$product_meta_table_name = WP_ESTORE_PRODUCTS_META_TABLE_NAME;

//Save max domain value
$fields = array();
$fields['prod_id'] = $prod_id;
Expand All @@ -196,11 +177,9 @@ function slm_estore_new_product_added($prod_dat_array, $prod_id) {
}

}

function slm_estore_product_updated($prod_dat_array, $prod_id) {
global $wpdb;
$product_meta_table_name = WP_ESTORE_PRODUCTS_META_TABLE_NAME;

//Find the existing value for the max domains field (for the given product)
$product_meta = $wpdb->get_row("SELECT * FROM $product_meta_table_name WHERE prod_id = '$prod_id' AND meta_key='slm_max_allowed_domains'", OBJECT);
if ($product_meta) {
Expand Down Expand Up @@ -238,15 +217,13 @@ function slm_estore_product_updated($prod_dat_array, $prod_id) {
}

}

function slm_estore_product_deleted($prod_id) {
global $wpdb;
$product_meta_table_name = WP_ESTORE_PRODUCTS_META_TABLE_NAME;

$result = $wpdb->delete($product_meta_table_name, array('prod_id' => $prod_id, 'meta_key' => 'slm_max_allowed_domains'));
$result = $wpdb->delete($product_meta_table_name, array('prod_id' => $prod_id, 'meta_key' => 'slm_date_of_expiry'));
}

/************************************/
/*** End of WP eStore integration ***/
/************************************/
4 changes: 2 additions & 2 deletions software-license-manager/software-license-manager.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
Plugin Name: Software License Manager
Version: 4.0
Version: 4.1
Plugin URI: https://www.tipsandtricks-hq.com/software-license-manager-plugin-for-wordpress
Author: Tips and Tricks HQ
Author URI: https://www.tipsandtricks-hq.com/
Expand All @@ -15,7 +15,7 @@
}

//Short name/slug "SLM" or "slm"
define('SLM_VERSION', "4.0");
define('SLM_VERSION', "4.1");
define('SLM_DB_VERSION', '1.7');
define('SLM_FOLDER', dirname(plugin_basename(__FILE__)));
define('SLM_URL', plugins_url('',__FILE__));
Expand Down

0 comments on commit 26635a1

Please sign in to comment.