update
This commit is contained in:
@@ -0,0 +1,241 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
if ( ! class_exists( 'Password_Protected_Activity_Logs' ) ) {
|
||||
class Password_Protected_Activity_Logs {
|
||||
/**
|
||||
* table
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
private static $table;
|
||||
/**
|
||||
* now
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public static $now = 0;
|
||||
/**
|
||||
* Method __construct
|
||||
*/
|
||||
public function __construct() {
|
||||
self::$now = current_time( 'timestamp' );
|
||||
self::$table = 'pp_activity_logs';
|
||||
$database_updated = get_option( 'pp_activity_logs_db_updated' );
|
||||
if ( ! $database_updated ) {
|
||||
if ( ! function_exists( 'maybe_add_column' ) ) {
|
||||
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
|
||||
}
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . self::$table;
|
||||
$used_password_col = maybe_add_column(
|
||||
$table_name,
|
||||
'used_password',
|
||||
'ALTER TABLE ' . $table_name . ' ADD `used_password` VARCHAR ( 255 ) NULL AFTER `status`'
|
||||
);
|
||||
$password_id_col = maybe_add_column(
|
||||
$table_name,
|
||||
'password_id',
|
||||
'ALTER TABLE ' . $table_name . ' ADD `password_id` VARCHAR( 255 ) NULL AFTER `id`'
|
||||
);
|
||||
$object_type_col = maybe_add_column(
|
||||
$table_name,
|
||||
'object_type',
|
||||
'ALTER TABLE ' . $table_name . ' ADD `object_type` VARCHAR( 255 ) NULL AFTER `status`'
|
||||
);
|
||||
|
||||
if ( $used_password_col && $password_id_col && $object_type_col ) {
|
||||
update_option( 'pp_activity_logs_db_updated', true );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get_items
|
||||
*
|
||||
* @return array|object|stdClass[]|null
|
||||
*/
|
||||
public static function get_items() {
|
||||
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . self::$table;
|
||||
|
||||
$search_term = self::check_for_search();
|
||||
$filter = self::check_for_filter();
|
||||
$timestamp = self::get_time_from_keyword( $filter );
|
||||
|
||||
|
||||
if( $filter != NULL && !empty( $timestamp ) )
|
||||
$query = " WHERE `created_at` BETWEEN " . reset( $timestamp ) . " AND " . end( $timestamp );
|
||||
else
|
||||
$query = "";
|
||||
|
||||
if( $search_term == NULL ) {
|
||||
$search = $query . " ORDER BY `id` DESC";
|
||||
} else {
|
||||
$query = empty($query) ? '' : str_replace( "WHERE", "and", $query );
|
||||
$search = "WHERE CONCAT_WS( ' ', `ip`, `browser`, `status` ) LIKE '%$search_term%'" . $query;
|
||||
}
|
||||
|
||||
$logs = $wpdb->get_results(
|
||||
" SELECT * FROM $table_name " . $search,
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
if( !is_wp_error( $logs ) && count( $logs ) > 0 )
|
||||
return $logs;
|
||||
else
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method add_item
|
||||
*
|
||||
* @param $request $request
|
||||
*
|
||||
* @return bool|int|mysqli_result|null
|
||||
*/
|
||||
public static function add_item( $request ) {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . self::$table;
|
||||
|
||||
$data = array(
|
||||
'ip' => $request['ip'],
|
||||
'browser' => $request['browser'],
|
||||
'status' => $request['status'],
|
||||
'created_at' => $request['created_at'],
|
||||
'used_password' => $request['used_password'],
|
||||
'password_id' => $request['password_id'],
|
||||
'object_type' => $request['object_type'],
|
||||
);
|
||||
$format = array( '%s', '%s', '%s', '%s' );
|
||||
return $wpdb->insert( $table_name, $data, $format );
|
||||
}
|
||||
|
||||
/**
|
||||
* delete_item
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return bool|int|mysqli_result|null
|
||||
*/
|
||||
public static function delete_item( $id ) {
|
||||
global $wpdb;
|
||||
|
||||
return $wpdb->delete(
|
||||
$wpdb->prefix . self::$table,
|
||||
['id' => $id],
|
||||
['%d']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* delete_items
|
||||
*
|
||||
* @param mixed $ids
|
||||
*/
|
||||
public static function delete_items( $ids ) {
|
||||
global $wpdb;
|
||||
|
||||
$table_name = $wpdb->prefix . self::$table;
|
||||
$wpdb->query( "DELETE FROM `{$table_name}` WHERE ID IN( $ids )" );
|
||||
}
|
||||
|
||||
/**
|
||||
* delete_all_items
|
||||
*/
|
||||
public static function delete_all_items() {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . self::$table;
|
||||
$wpdb->query("TRUNCATE TABLE $table_name");
|
||||
}
|
||||
|
||||
/**
|
||||
* check_for_search
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public static function check_for_search() {
|
||||
if( isset( $_POST['s'] ) ) {
|
||||
if ( ! isset( $_POST['search_activity_logs_nonce'] ) || ! wp_verify_nonce( $_POST['search_activity_logs_nonce'], 'password_protected_search_activity_logs' ) ) {
|
||||
wp_die('Sorry, your nonce did not verify.');
|
||||
}
|
||||
return trim( sanitize_text_field( $_POST['s'] ) );
|
||||
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* check_for_filter
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public static function check_for_filter() {
|
||||
if( isset( $_GET['show_logs'] ) ) {
|
||||
|
||||
$nonce = sanitize_text_field( $_GET['_wpnonce'] );
|
||||
if( ! wp_verify_nonce( $nonce, 'activity-logs-filter' ) ) {
|
||||
wp_die( __( 'Security check: Your nonce did not verify!', 'password-protected-pro' ) );
|
||||
} else {
|
||||
return sanitize_text_field( $_GET['show_logs'] );
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* get_time_from_keyword
|
||||
*
|
||||
* @param mixed $keyword
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_time_from_keyword( $keyword = '' ) {
|
||||
|
||||
$today = strtotime( date( 'Y-m-d' ) . ' midnight', self::$now );
|
||||
$todays_date = date( 'd', self::$now );
|
||||
$weekday = date( 'w', self::$now );
|
||||
$result = array();
|
||||
|
||||
$keyword = strtolower( (string) $keyword );
|
||||
|
||||
// Today
|
||||
if ( $keyword === 'today' ) {
|
||||
$result[] = $today;
|
||||
$result[] = self::$now;
|
||||
}
|
||||
|
||||
// Yesterday
|
||||
elseif ( $keyword === 'yesterday' ) {
|
||||
$result[] = strtotime( '-1 day midnight', self::$now );
|
||||
$result[] = strtotime( 'today midnight', self::$now );
|
||||
}
|
||||
|
||||
// This week
|
||||
elseif ( $keyword === 'thisweek' ) {
|
||||
|
||||
$thisweek = strtotime( '-' . ( $weekday+1 ) . ' days midnight', self::$now );
|
||||
if ( get_option( 'start_of_week' ) == $weekday )
|
||||
$thisweek = $today;
|
||||
|
||||
$result[] = $thisweek;
|
||||
$result[] = self::$now;
|
||||
|
||||
}
|
||||
|
||||
// This month
|
||||
elseif ( $keyword === 'thismonth' ) {
|
||||
$result[] = strtotime( date( 'Y-m-01' ) . ' midnight', self::$now );
|
||||
$result[] = self::$now;
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new Password_Protected_Activity_Logs();
|
||||
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Password Protected Activity Report Settings
|
||||
*
|
||||
* @package Password Protected Pro
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
if ( ! class_exists( 'Password_protected_Activity_Report_Settings' ) ) {
|
||||
class Password_protected_Activity_Report_Settings {
|
||||
private static $instance;
|
||||
|
||||
private function __construct() {
|
||||
add_action( 'admin_init', array( $this, 'update_database' ), -1 );
|
||||
add_action( 'admin_init', array( $this, 'settings_fields' ), 10 );
|
||||
add_action( 'password_protected_subtab_activity-report_content', array( $this, 'activity_report' ) );
|
||||
|
||||
if ( self::is_activity_report_enabled() ) {
|
||||
if ( ! class_exists( 'Password_Protected_Pro' ) ) {
|
||||
require_once PASSWORD_PROTECTED_DIR . 'includes/activity-report-email/class-password-protected-activity-logs.php';
|
||||
|
||||
add_action( 'password_protected_success_login_attempt', array( $this, 'success_attempt' ), 10, 3 );
|
||||
add_action( 'password_protected_failure_login_attempt', array( $this, 'failure_attempt' ), 10, 3 );
|
||||
add_action( 'password_protected_after_login_form', array( $this, 'login_enqueue_scripts' ) );
|
||||
add_action( 'password_protected_below_password_field', array( $this, 'add_new_field_after_password_field' ) );
|
||||
}
|
||||
|
||||
require_once PASSWORD_PROTECTED_DIR . 'includes/activity-report-email/class-password-protected-send-email-notification.php';
|
||||
}
|
||||
}
|
||||
|
||||
public function update_database() {
|
||||
$plugin_updated = get_option( 'password_protected_1.5_update_database', false );
|
||||
if ( ! $plugin_updated ) {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'pp_activity_logs';
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
if ( ! function_exists( 'maybe_create_table' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
}
|
||||
|
||||
$sql = 'CREATE TABLE `' . $table_name . '` (
|
||||
`id` MEDIUMINT ( 9 ) NOT NULL AUTO_INCREMENT,
|
||||
`ip` VARCHAR ( 55 ) NOT NULL,
|
||||
`browser` TEXT NOT NULL,
|
||||
`status` TINYTEXT NOT NULL,
|
||||
`created_at` VARCHAR ( 55 ) NOT NULL,
|
||||
PRIMARY KEY ( `id` )
|
||||
) ' . $charset_collate . ';';
|
||||
|
||||
maybe_create_table( $table_name, $sql );
|
||||
update_option( 'password_protected_1.5_update_database', true );
|
||||
}
|
||||
}
|
||||
|
||||
public function settings_fields() {
|
||||
register_setting(
|
||||
'password_protected_activity_report',
|
||||
'password_protected_activity_report_enable',
|
||||
array(
|
||||
'sanitize_callback' => array( $this, 'sanitize_report_fields' ),
|
||||
)
|
||||
);
|
||||
|
||||
add_settings_section(
|
||||
'password_protected_activity_report',
|
||||
__( 'Password Activity Report via Email', 'password-protected' ),
|
||||
'__return_null',
|
||||
'admin.php?page=password-protected&tab=activity-report',
|
||||
array()
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
'password_protected_activity_report',
|
||||
__( 'Enable Activity Report', 'password-protected' ),
|
||||
array( $this, 'activity_report_field_callback' ),
|
||||
'admin.php?page=password-protected&tab=activity-report',
|
||||
'password_protected_activity_report',
|
||||
array(
|
||||
'label_for' => 'password_protected_activity_report',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function sanitize_report_fields( $fields ) {
|
||||
if ( empty( $fields ) ) {
|
||||
return 'no';
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function activity_report_field_callback( $args ) {
|
||||
$checked = get_option( 'password_protected_activity_report_enable', 'no' );
|
||||
$checked = 'yes' === $checked ? 'checked' : '';
|
||||
echo '<div class="pp-toggle-wrapper">
|
||||
<input id="' . esc_attr( $args['label_for'] ) . '" value="yes" name="password_protected_activity_report_enable" type="checkbox" ' . $checked . ' />
|
||||
<label for="' . esc_attr( $args['label_for'] ) . '" class="pp-toggle">
|
||||
<span class="pp-toggle-slider"></span>
|
||||
</label>
|
||||
</div>
|
||||
<p class="description">
|
||||
<label for="' . esc_attr( $args['label_for'] ) . '">
|
||||
' . __( 'Enable this option to receive weekly activity report on your email.', 'password-protected' ) . '
|
||||
</label>
|
||||
</p>';
|
||||
}
|
||||
|
||||
public function add_activity_report_tab( $tabs ) {
|
||||
$tabs['activity-report'] = __( 'Activity Report', 'password-protected' );
|
||||
return $tabs;
|
||||
}
|
||||
|
||||
public function activity_report() {
|
||||
echo '<form action="options.php" method="post" enctype="multipart/form-data">';
|
||||
|
||||
settings_fields( 'password_protected_activity_report' );
|
||||
do_settings_sections( 'admin.php?page=password-protected&tab=activity-report' );
|
||||
|
||||
submit_button();
|
||||
|
||||
echo '</form>';
|
||||
}
|
||||
|
||||
public function success_attempt( $form_type, $password, $password_id ) {
|
||||
$this->log_password_attempt( "Success", $form_type, $password, $password_id );
|
||||
}
|
||||
|
||||
public function failure_attempt( $form_type, $password, $password_id ) {
|
||||
$this->log_password_attempt( "Failure", $form_type, $password, $password_id );
|
||||
}
|
||||
|
||||
public function login_enqueue_scripts() {
|
||||
global $Password_Protected;
|
||||
wp_enqueue_script( 'password-protected-detect', PASSWORD_PROTECTED_URL . 'assets/js/detect.min.js', array( 'jquery' ), $Password_Protected->version, true );
|
||||
wp_enqueue_script( 'password-protected-compatibility', PASSWORD_PROTECTED_URL . 'assets/js/compatibility.js', array( 'password-protected-detect' ), $Password_Protected->version, true );
|
||||
}
|
||||
|
||||
public function add_new_field_after_password_field() {
|
||||
echo '<input type="hidden" name="password_protected_user_agent" value="" />';
|
||||
}
|
||||
|
||||
private function log_password_attempt( $success_or_failure, $form_type, $password, $password_id ) {
|
||||
$log = $this->prepare_entry_log();
|
||||
extract( $log );
|
||||
Password_Protected_Activity_Logs::add_item(
|
||||
array(
|
||||
'ip' => $IP,
|
||||
'browser' => $browser,
|
||||
'status' => $success_or_failure,
|
||||
'created_at' => current_time( "timestamp" ),
|
||||
'password_id' => $password_id,
|
||||
'object_type' => $form_type,
|
||||
'used_password' => $password,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function prepare_entry_log() {
|
||||
$IP = self::get_client_ip();
|
||||
$browser = self::get_browser();
|
||||
return compact( 'IP', 'browser' );
|
||||
}
|
||||
|
||||
public static function get_client_ip() {
|
||||
$ipaddress = 'UNKNOWN';
|
||||
$keys = array(
|
||||
'HTTP_CLIENT_IP',
|
||||
'HTTP_X_FORWARDED_FOR',
|
||||
'HTTP_X_FORWARDED',
|
||||
'HTTP_FORWARDED_FOR',
|
||||
'HTTP_FORWARDED',
|
||||
'REMOTE_ADDR',
|
||||
);
|
||||
|
||||
foreach ( $keys as $key ) {
|
||||
if ( isset( $_SERVER[ $key ] ) ) {
|
||||
$ipaddress = sanitize_text_field( wp_unslash( $_SERVER[ $key ] ) );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( '::1' === $ipaddress ) {
|
||||
$ipaddress = '127.0.1.6';
|
||||
}
|
||||
|
||||
return $ipaddress;
|
||||
}
|
||||
|
||||
public static function get_browser() {
|
||||
if ( isset( $_POST['password_protected_user_agent'] ) ) {
|
||||
return sanitize_text_field( wp_unslash( $_POST['password_protected_user_agent'] ) );
|
||||
}
|
||||
|
||||
return 'UNKNOWN';
|
||||
}
|
||||
|
||||
public static function is_activity_report_enabled() {
|
||||
return 'yes' === get_option( 'password_protected_activity_report_enable', 'no' );
|
||||
}
|
||||
|
||||
public static function get_report_interval() {
|
||||
$interval = apply_filters( 'password_protected_activity_report_interval', 7 );
|
||||
if ( ! $interval ) {
|
||||
$interval = 7;
|
||||
}
|
||||
|
||||
$day_into_seconds = 60*60*24;
|
||||
return $day_into_seconds * $interval;
|
||||
}
|
||||
|
||||
public static function get_instance() {
|
||||
if ( is_null( self::$instance ) ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Password_protected_Activity_Report_Settings::get_instance();
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
if ( ! class_exists( 'Password_Protected_Send_Email_Notification' ) ) {
|
||||
class Password_Protected_Send_Email_Notification {
|
||||
private static $instance;
|
||||
|
||||
private function __construct() {
|
||||
add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) );
|
||||
add_action( 'init', array( $this, 'init_cron' ) );
|
||||
add_action( 'password_protected_email_notification_hook', array( $this, 'send_email_notification' ) );
|
||||
}
|
||||
|
||||
public function cron_schedules( $schedules ) {
|
||||
$schedules['password_protected_email_notification'] = array(
|
||||
'interval' => Password_protected_Activity_Report_Settings::get_report_interval(),
|
||||
'display' => __( 'Password Protected Email Notification Interval', 'password-protected' ),
|
||||
);
|
||||
return $schedules;
|
||||
}
|
||||
|
||||
public function init_cron() {
|
||||
if ( ! wp_next_scheduled( 'password_protected_email_notification_hook' ) ) {
|
||||
wp_schedule_event( time(), 'password_protected_email_notification', 'password_protected_email_notification_hook' );
|
||||
}
|
||||
}
|
||||
|
||||
public function send_email_notification() {
|
||||
global $wpdb;
|
||||
$timestamps = Password_Protected_Activity_Logs::get_time_from_keyword( 'thisweek' );
|
||||
$sql = 'SELECT
|
||||
SUM( IF ( `status` = %s, 1, 0 ) ) as success,
|
||||
SUM( IF ( `status` = %s, 1, 0 ) ) as failed
|
||||
FROM %i WHERE created_at between %d and %d;';
|
||||
$sql = $wpdb->prepare( $sql, 'Success', 'Failure', $wpdb->prefix . 'pp_activity_logs', $timestamps[0], $timestamps[1] );
|
||||
$results = $wpdb->get_row( $sql, ARRAY_A );
|
||||
|
||||
$success_attempts =
|
||||
$failed_attempts = 0;
|
||||
if ( is_array( $results ) ) {
|
||||
if ( isset( $results['success'] ) ) {
|
||||
$success_attempts = absint( $results['success'] );
|
||||
}
|
||||
|
||||
if ( isset( $results['failed'] ) ) {
|
||||
$failed_attempts = absint( $results['failed'] );
|
||||
}
|
||||
}
|
||||
|
||||
$total_attempts = $success_attempts + $failed_attempts;
|
||||
|
||||
$template = $this->get_template( $success_attempts, $failed_attempts, $total_attempts, $timestamps );
|
||||
$headers = $this->get_html_headers();
|
||||
$subject = sprintf(
|
||||
'[ %s, %s ]',
|
||||
get_bloginfo( 'name' ),
|
||||
__( 'Password Protected Activity Log Notification', 'password-protected' )
|
||||
);
|
||||
|
||||
return wp_mail( get_option( 'admin_email' ), $subject, $template, $headers );
|
||||
}
|
||||
|
||||
private function get_template( $success, $failed, $total, $time ) {
|
||||
ob_start();
|
||||
require_once PASSWORD_PROTECTED_DIR . 'templates/emails/activity-notification.php';
|
||||
return ob_get_clean();
|
||||
}
|
||||
private function get_html_headers() {
|
||||
$headers = array(
|
||||
'Content-type: text/html',
|
||||
);
|
||||
|
||||
return implode( "\r\n", apply_filters( 'password_protected_email_headers', $headers ) );
|
||||
}
|
||||
|
||||
public static function get_instance() {
|
||||
if ( is_null( self::$instance ) ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Password_Protected_Send_Email_Notification::get_instance();
|
||||
|
||||
#F03B3E, #04AA5E, #4685EC, #CA1329, #FBBA40
|
||||
46
wp-content/plugins/password-protected/includes/freemius.php
Normal file
46
wp-content/plugins/password-protected/includes/freemius.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Freemius integration
|
||||
*
|
||||
* @package Password Protected Pro
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
if ( ! function_exists( 'pp_free_fs' ) ) {
|
||||
/**
|
||||
* Freemius integration
|
||||
*
|
||||
* @return Freemius Freemius instance.
|
||||
* @throws Freemius_Exception Throws Freemius exception.
|
||||
*/
|
||||
function pp_free_fs() {
|
||||
global $pp_free_fs;
|
||||
|
||||
if ( ! isset( $pp_free_fs ) ) {
|
||||
require_once dirname( __DIR__ ) . '/freemius/start.php';
|
||||
|
||||
$pp_free_fs = fs_dynamic_init(
|
||||
array(
|
||||
'id' => '12503',
|
||||
'slug' => 'password-protected-free',
|
||||
'premium_slug' => 'password-protected-premium-premium',
|
||||
'type' => 'plugin',
|
||||
'public_key' => 'pk_e9210517721d27b5112fa7773a600',
|
||||
'is_premium' => false,
|
||||
// 'has_addons' => true,
|
||||
'has_paid_plans' => false,
|
||||
'menu' => array(
|
||||
'slug' => 'password-protected',
|
||||
'support' => false,
|
||||
'contact' => false,
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $pp_free_fs;
|
||||
}
|
||||
}
|
||||
|
||||
pp_free_fs();
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* Transient functions.
|
||||
*
|
||||
* @usage Transient functions to get, set and delete transient values
|
||||
* @description Use transient instead of cookies.
|
||||
* @package Password Protected
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
if ( ! function_exists( 'pp_get_transient_identifier' ) ) {
|
||||
/**
|
||||
* Get identifier
|
||||
*
|
||||
* @param string $key transient key.
|
||||
*
|
||||
* @usage function to get unique transient key
|
||||
* @return string: transient key
|
||||
*/
|
||||
function pp_get_transient_identifier( $key = '' ) {
|
||||
return $key . '_' . pp_get_ip_address();
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'pp_get_ip_address' ) ) {
|
||||
/**
|
||||
* Get IP Address
|
||||
*
|
||||
* @usage function to get ip of current user
|
||||
* @return string: ip
|
||||
*/
|
||||
function pp_get_ip_address() {
|
||||
foreach ( array( 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR' ) as $key ) {
|
||||
if ( array_key_exists( $key, $_SERVER ) === true ) {
|
||||
foreach ( explode(',', $_SERVER[ $key ] ) as $ip ) {
|
||||
$ip = trim( $ip );
|
||||
|
||||
if ( filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) !== false ){
|
||||
return $ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'pp_get_transient' ) ) {
|
||||
/**
|
||||
* Get Transient
|
||||
*
|
||||
* @usage function to get transient value by key
|
||||
*
|
||||
* @param string $key transient key.
|
||||
*
|
||||
* @return bool|mixed
|
||||
*/
|
||||
function pp_get_transient( $key = '' ) {
|
||||
$transient = pp_get_transient_identifier( $key );
|
||||
$value = get_transient( $transient );
|
||||
if ( ! empty( $value ) ) {
|
||||
return $value;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'pp_set_transient' ) ) {
|
||||
/**
|
||||
* Set Transient
|
||||
*
|
||||
* @param string $key transient key.
|
||||
* @param string $value transient value.
|
||||
* @param int $duration transient duration.
|
||||
*
|
||||
* @usage function to set transient value by key, value and duration
|
||||
*/
|
||||
function pp_set_transient( $key = '', $value = '', $duration = HOUR_IN_SECONDS ) {
|
||||
$transient = pp_get_transient_identifier( $key );
|
||||
set_transient( $transient, $value, $duration );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'pp_delete_transient' ) ) {
|
||||
/**
|
||||
* Delete transient
|
||||
*
|
||||
* @param string $key transient key.
|
||||
*
|
||||
* @usage function to delete transient value by key
|
||||
*/
|
||||
function pp_delete_transient( $key = '' ) {
|
||||
$transient = pp_get_transient_identifier( $key );
|
||||
delete_transient( $transient );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user