Skip to content

Instantly share code, notes, and snippets.

@wpeasy
Last active February 6, 2024 05:40
Show Gist options
  • Save wpeasy/57e226dd1ccd38918e9b81c1b779bbcc to your computer and use it in GitHub Desktop.
Save wpeasy/57e226dd1ccd38918e9b81c1b779bbcc to your computer and use it in GitHub Desktop.
Bricks Builder: Restrict content based on Password Levels
<?php
define("WPE_AUTH_TRACKER_TABLE_NAME", "wpe_ip_tracker");
define("WPE_AUTH_TRACKER_MAX_AGE", 14400); /* 4 hours */
function maybe_create_ip_tracking_table()
{
/* ensure called only once per load */
static $initialised;
if ($initialised) {
return false;
}
$initialised = true;
global $wpdb;
$table_name = $wpdb->prefix . WPE_AUTH_TRACKER_TABLE_NAME;
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$charset_collate = $wpdb->get_charset_collate();
$current_time = current_time("mysql");
$sql = "CREATE TABLE $table_name (
id INT(11) NOT NULL AUTO_INCREMENT,
create_time TIMESTAMP DEFAULT '$current_time' NOT NULL,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
IP varchar(45) NOT NULL UNIQUE,
auth_level INT(11) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once ABSPATH . "wp-admin/includes/upgrade.php";
dbDelta($sql);
}
}
function insert_or_update_wpe_ip_tracker($auth_level)
{
global $wpdb;
$table_name = $wpdb->prefix . WPE_AUTH_TRACKER_TABLE_NAME;
$referrer_ip = $_SERVER["REMOTE_ADDR"];
$current_time = current_time("mysql");
maybe_create_ip_tracking_table();
$wpdb->replace(
$table_name,
[
"IP" => $referrer_ip,
"auth_level" => $auth_level,
],
[
"%s", // IP
"%s", // auth_level
]
);
}
function get_auth_level_by_ip($ip)
{
global $wpdb;
$table_name = $wpdb->prefix . WPE_AUTH_TRACKER_TABLE_NAME;
maybe_create_ip_tracking_table();
$result = $wpdb->get_row(
$wpdb->prepare(
"SELECT auth_level, UNIX_TIMESTAMP(update_time) AS update_timestamp FROM $table_name WHERE IP = %s",
$ip
)
);
$auth_level = 0;
if ($result !== null && $result->update_timestamp !== null) {
/* Check for expiry */
$current_timestamp = time();
$update_timestamp = intval($result->update_timestamp);
$time_difference = $current_timestamp - $update_timestamp;
if ($time_difference > WPE_AUTH_TRACKER_MAX_AGE) {
/* Expire */
$auth_level = 0;
insert_or_update_wpe_ip_tracker(0);
} else {
$auth_level = $result->auth_level;
}
}
return $auth_level;
}
/**********************************
CUSTOM FORM ACTION
**********************************/
function wpe_get_passwords()
{
/*
$passwords = [
1 => 'password1',
2 => 'password2',
3 => 'password3',
];
return $passwords;
*/
/* MetaBox Settings Page */
$passwords = [];
$groups = rwmb_meta(
"password_levels",
["object_type" => "setting"],
"access-control"
);
foreach ($groups as $group) {
$passwords[$group["level_number"]] = $group["level_password"];
}
return $passwords;
}
function wpe_password_check_action($form)
{
$passwords = wpe_get_passwords();
// Perform some logic here...
$fields = $form->get_fields();
if (!empty($fields["protect_content"])) {
$provided_password = $fields["password"];
$auth_level = 0;
foreach ($passwords as $level => $password) {
if ($provided_password === $password) {
$auth_level = $level;
break;
}
}
} else {
return false;
}
insert_or_update_wpe_ip_tracker($auth_level);
if ($auth_level > 0) {
$form->set_result([
"action" => "password_check_action",
"type" => "success", // or 'error' or 'info'
"message" => esc_html__("Good job: Auth Level - " . $auth_level),
]);
} else {
$form->set_result([
"action" => "password_check_action",
"type" => "success", // or 'error' or 'info'
"message" => esc_html__("Sorry, wrong password"),
]);
}
}
add_action("bricks/form/custom_action", "wpe_password_check_action", 10, 1);
/**********************************
FUCNTIONS TO GET SESSION VARIABLES
**********************************/
function wpe_get_content_protection_level()
{
/* ensure called only once per load */
static $auth_level;
if ($auth_level) {
return $auth_level;
}
$referrer_ip = $_SERVER["REMOTE_ADDR"];
$auth_level = get_auth_level_by_ip($referrer_ip);
return $auth_level;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment