-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWordPress_Helpers.php
81 lines (73 loc) · 2.06 KB
/
WordPress_Helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Your_Namespace\Core\Traits;
use Your_Namespace\Core\Config;
trait WordPress_Helpers {
/**
* Usage: `$script_url = h::plugin_url( 'assets/js/app.js' );`
*
* @param string $path
* @return string the link
*/
public static function plugin_url ( $path = '' ) {
return \plugins_url( $path, Config::get( 'FILE' ) );
}
/**
* @param boolean $raw
* @return string The plugin version
*/
public static function get_plugin_version ( $raw = false ) {
$version = Config::get( 'VERSION' );
return $raw ? $version : preg_replace( '/[^0-9.]/', '', $version );
}
/**
* Saves a WordPress transient prefixed with the plugin slug.
*
* @see https://developer.wordpress.org/apis/transients/
* @see https://codex.wordpress.org/Easier_Expression_of_Time_Constants
* @param string $transient
* @param mixed $value
* @param integer $duration
* @return mixed
*/
public static function set_transient ( $transient, $value, $duration = 0 ) {
if ( is_callable( $value ) ) {
$value = \call_user_func( $value );
}
if ( Config::get( 'CACHE_ENABLED', true ) ) {
$key = self::get_transient_key( $transient );
if ( ! self::filled( $value ) ) {
return \delete_transient( $key );
}
else {
$duration = \absint( $duration );
$duration = $duration !== 0 ? $duration : \apply_filters(
self::prefix( 'transient_max_duration' ),
3 * MONTH_IN_SECONDS, // by default, max is 3 months
$transient
);
\set_transient( $key, $value, $duration );
}
}
return $value;
}
/**
* @param string $transient
* @param mixed $default
* @return mixed
*/
public static function get_transient ( $transient, $default = false ) {
$value = false;
if ( Config::get( 'CACHE_ENABLED', true ) ) {
$key = self::get_transient_key( $transient );
$value = \get_transient( $key );
}
return false !== $value ? $value : $default;
}
/**
* @param string $transient
* @return string
*/
public static function get_transient_key ( $transient ) {
return self::prefix( $transient ) . '_' . self::get_plugin_version();
}
}