Plugin.php
4.91 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace FakerPress;
class Plugin {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 0.5.1
* @var string
*/
const VERSION = '0.5.2';
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 0.1.0
* @deprecated 0.5.1
*
* @var string
*/
const version = self::VERSION;
/**
* A static variable that holds a dynamic instance of the class
*
* @since 0.1.0
*
* @var null|object The dynamic version of this class
*/
public static $instance = null;
/**
* A static variable that holds a dynamic instance of the class of the admin
*
* @since 0.1.0
*
* @var null|object The dynamic version of this class
*/
public static $admin = null;
/**
* A static variable that holds a dynamic instance of the class of the AJAX methods
*
* @since 0.2.0
*
* @var null|object The dynamic version of this class
*/
public static $ajax = null;
/**
* Variable holding the folder name of the plugin
*
* @since 0.1.0
*
* @var string
*/
public static $folder = 'fakerpress';
/**
* Variable holding the slug name of the plugin
*
* @since 0.1.0
*
* @var string
*/
public static $slug = 'fakerpress';
/**
* The __FILE__ that initialized the plugin
*
* @since 0.1.0
*
* @var string
*/
public static $_file = __FP_FILE__;
/**
* The Plugin external website base domain
*
* @since 0.3.2
*
* @var string
*/
public static $_ext_domain = 'http://fakerpress.com';
/**
* Return a Path relative to the plugin root
*
* @since 0.1.0
*
* @uses plugin_dir_path
*
* @param string $append A string to be appended to the root path
*
* @return string The path after been appended by the variable
*/
public static function path( $append = '' ) {
return (string) plugin_dir_path( self::$_file ) . str_replace( '/', DIRECTORY_SEPARATOR, $append );
}
/**
* Return a URL relative to the plugin root
*
* @since 0.1.0
* @param string $file A string to be appended to the root url
* @uses plugins_url
* @return string The url to the file
*/
public static function url( $file = '' ) {
return (string) plugins_url( $file, self::basename() );
}
/**
* Return a URL relative to the plugin's administration page.
*
* @since 0.1.0
*
* @uses admin_url
* @uses wp_parse_args
* @uses add_query_arg
*
* @param string|array $args Arguments for the admin URL
* @param string $hash Hash for the admin URL
*
* @return string The url to the file
*/
public static function admin_url( $args = '', $hash = false ) {
/**
* Define the array of defaults
*/
$defaults = [
'page' => self::$slug,
];
/**
* Parse incoming $args into an array and merge it with $defaults
*/
$args = wp_parse_args( $args, $defaults );
return add_query_arg( $args, admin_url( 'admin.php' ) ) . ( $hash !== false ? "#{$hash}" : '' );
}
/**
* Returns a URL for the external project website
*
* @since 0.3.2
* @param string $path Hash for the admin URL
* @uses esc_url_raw
*
* @return string The url the external website with the appended $path
*/
public static function ext_site_url( $path = '/' ) {
return esc_url_raw( self::$_ext_domain . ( ! empty( $path ) ? $path : '/' ), [ 'http', 'https' ] );
}
/**
* Return the plugin basename.
*
* @since 0.1.0
*
* @uses plugin_basename
*
* @return string plugin_basename from __FILE__
*/
public static function basename() {
$_link = WP_PLUGIN_DIR . '/' . self::$folder;
$_file = self::$_file;
if ( is_link( $_link ) && readlink( $_link ) == dirname( $_file ) ) {
$basename = explode( '/', $_file );
$_file = $_link . '/' . end( $basename );
}
return (string) plugin_basename( $_file );
}
public static function get( $name, $default = false ) {
$options = self::all();
$value = fp_array_get( $options, $name, null, $default );
return $value;
}
public static function update( $name = null, $value = false ) {
$options = self::all();
$opts = [];
foreach ( (array) $name as $k => $index ) {
if ( 0 === $k ) {
$opts[ -1 ] = &$options;
}
if ( count( $name ) - 1 !== $k && ! isset( $opts[ $k - 1 ][ $index ] ) ) {
$opts[ $k - 1 ][ $index ] = [];
}
if ( isset( $opts[ $k - 1 ][ $index ] ) ) {
$opts[ $k ] = &$opts[ $k - 1 ][ $index ];
} else {
$opts[ $k - 1 ][ $index ] = $value;
}
}
$opts[ $k ] = $value;
return update_option( self::$slug . '-plugin-options', $options );
}
public static function remove( $name = null ) {
// @TODO
}
public static function all() {
$defaults = [];
return get_option( self::$slug . '-plugin-options', $defaults );
}
/**
* The initialization of the static and dynamic variables
*
* @since 0.1.0
*/
public function __construct() {
// Setup the global version of the class, this only runs once...
null === self::$instance && self::$instance = &$this;
}
}