Created
September 6, 2016 06:32
-
-
Save mohammadmursaleen/db1449de293a10ab246bac6a6ac6d5df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Mohammad Mursaleen | |
* @usage function to get unique transient key | |
* @return string: transient key | |
*/ | |
function objects_get_transient_identifier( $key = '' ){ | |
return $key . '_' . objects_get_ip_address(); | |
} | |
/** | |
* @author Mohammad Mursaleen | |
* @usage function to get ip of current user | |
* @return string: ip | |
*/ | |
function mm_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); // just to be safe | |
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){ | |
return $ip; | |
} | |
} | |
} | |
} | |
} | |
/** | |
* @author Mohammad Mursaleen | |
* @usage function to get transient value by key | |
* @param string $page | |
*/ | |
function mm_get_transient( $key = '' ){ | |
$transient = mm_get_transient_identifier( $key ); | |
$value = get_transient( $transient ); | |
if( !empty($value) ){ | |
return $value; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* @author Mohammad Mursaleen | |
* @usage function to set transient value by key, value and duration | |
*/ | |
function mm_set_transient( $key = '' , $value = '' , $duration = HOUR_IN_SECONDS ){ | |
$transient = mm_get_transient_identifier( $key ); | |
set_transient( $transient, $value , $duration ); | |
} | |
/** | |
* @author Mohammad Mursaleen | |
* @usage function to delete transient value by key | |
*/ | |
function mm_delete_transient( $key = '' ){ | |
$transient = mm_get_transient_identifier( $key ); | |
delete_transient( $transient ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment