1: <?php
2:
3: /**
4: * Delete transient by key.
5: *
6: * @param string $key Transient name.
7: *
8: * @return bool
9: */
10: function xu_delete_transient( $key ) {
11: return delete_transient( $key );
12: }
13:
14: /**
15: * Delete site transient by key.
16: *
17: * @param string $key Transient name.
18: *
19: * @return bool
20: */
21: function xu_delete_site_transient( $key ) {
22: return delete_site_transient( $key );
23: }
24:
25: /**
26: * Get/Set transient.
27: *
28: * @param string $key Transient name. Should be 172 (40 on site) characters or less since WordPress will prefix the name.
29: * @param mixed $value Transient value. Expected to not be SQL-escaped. Can be callable value. Optional.
30: * @param int $expiration Time until expiration in seconds from now, or 0 for never expires. Default 0.
31: * @param bool $site Set to true if site transient. Default false.
32: *
33: * @return mixed
34: */
35: function xu_get_transient( $key, $value = null, $expiration = 0, $site = false ) {
36: if ( ! is_string( $key ) ) {
37: return false;
38: }
39:
40: $transient_value = ( $site ? get_site_transient( $key ) : get_transient( $key ) );
41:
42: // Return transient value if it exists or the return value if given value is null.
43: if ( $transient_value || is_null( $value ) ) {
44: return $transient_value;
45: }
46:
47: // Support callable values.
48: if ( is_callable( $value ) ) {
49: $value = call_user_func( $value );
50:
51: // Return false if null value.
52: if ( is_null( $value ) ) {
53: return false;
54: }
55: }
56:
57: if ( $site ) {
58: set_site_transient( $key, $value, $expiration );
59: } else {
60: set_transient( $key, $value, $expiration );
61: }
62:
63: return $value;
64: }
65:
66: /**
67: * Get/Set site transient.
68: *
69: * @param string $key Transient name. Should be 40 characters or less since WordPress will prefix the name.
70: * @param mixed $value Transient value. Expected to not be SQL-escaped. Can be callable value. Optional.
71: * @param int $expiration Time until expiration in seconds from now, or 0 for never expires. Default 0.
72: *
73: * @return mixed
74: */
75: function xu_get_site_transient( $key, $value = null, $expiration = 0 ) {
76: return xu_get_transient( $key, $value, $expiration, true );
77: }
78: