Skip to content

Instantly share code, notes, and snippets.

@vanbo
Created September 13, 2021 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanbo/0c148bae07e9542554869e1cc31aa3c8 to your computer and use it in GitHub Desktop.
Save vanbo/0c148bae07e9542554869e1cc31aa3c8 to your computer and use it in GitHub Desktop.
wc-trustcommerce-limit-transactions
/**
* IMPORTANT: Add to your 'wp-content/themes/theme-name/functions.php' file
*
* Description: Will limit the trustcommerce requests per interval of time.
*/
add_filter( 'wc_trustcommerce_process_single_payment_request', 'vanbodevelops_limit_transaction_requests', 9999, 3 );
/**
* @param $request
* @param $order
* @param $gateway
*
* @return mixed
* @throws \Exception
*/
function vanbodevelops_limit_transaction_requests( $request, $order, $gateway ) {
// Make sure that we are using the checkout
if ( ! isset( $_POST['payment_method'] ) && 'trustcommerce' != $_POST['payment_method'] ) {
return $request;
}
vanbodevelops_maybe_create_limit_db();
global $wpdb;
$interval = vd_get_sql_interval_string();
$allowed_request = vd_get_allowed_requests();
$query = "SELECT count(*) as the_count
FROM {$wpdb->prefix}vd_limit_transaction_requests
WHERE date_created >= DATE_SUB(NOW(),INTERVAL %s)";
$query = sprintf( $query, $interval );
$count = $wpdb->get_var( $query );
if ( $count > $allowed_request ) {
throw new Exception( vd_get_limit_error_message() );
}
$wpdb->insert( "{$wpdb->prefix}vd_limit_transaction_requests", array( 'date_created' => gmdate( 'Y-m-d H:i:s', time() ) ) );
return $request;
}
function vd_get_allowed_requests() {
// IMPORTANT: Set here the allowed request for the interval of time
return 1000;
}
function vd_get_request_limit_interval() {
// IMPORTANT: Set here your interval from the available intervals below.
// Currently available strings:
// 'hour'
// 'six_hours'
// 'day'
// 'seven_days'
// 'fourteen_days'
// 'thirty_days'
return 'day';
}
function vd_get_limit_error_message() {
// IMPORTANT: Set here the error message displayed to the customer.
return 'Transaction request limit reached. We were notified of the issue and our team is working to fix it.';
}
function vd_get_sql_interval_string() {
$intervals = array(
'hour' => '1 HOUR',
'six_hours' => '6 HOUR',
'day' => '1 DAY',
'seven_days' => '7 DAY',
'fourteen_days' => '14 DAY',
'thirty_days' => '30 DAY',
);
return $intervals[ vd_get_request_limit_interval() ];
}
add_action( 'init', 'vanbodevelops_maybe_clear_db' );
/**
* Clears the database from all
*/
function vanbodevelops_maybe_clear_db() {
$last_clearance = get_option( 'vd_limit_db_requests_clear', time() );
if ( $last_clearance > time() - MINUTE_IN_SECONDS ) {
return;
}
vanbodevelops_maybe_create_limit_db();
global $wpdb;
$interval = vd_get_sql_interval_string();
$query = "DELETE FROM {$wpdb->prefix}vd_limit_transaction_requests WHERE date_created < NOW() - INTERVAL {$interval}";
$wpdb->query( $query );
update_option( 'vd_limit_db_requests_clear', time() );
}
function vanbodevelops_maybe_create_limit_db() {
if ( 'yes' === get_option( 'vanbodevelops_limit_db_created', false ) ) {
return;
}
global $wpdb;
$wpdb->hide_errors();
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$collate = '';
if ( $wpdb->has_cap( 'collation' ) ) {
$collate = $wpdb->get_charset_collate();
}
$tables = "
CREATE TABLE {$wpdb->prefix}vd_limit_transaction_requests (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
date_created datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (id)
) $collate;";
dbDelta( $tables );
update_option( 'vd_limit_db_requests_clear', time() );
update_option( 'vanbodevelops_limit_db_created', 'yes' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment