PluginProbe
FakerPress / 0.8.0
FakerPress v0.8.0
0.9.2 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.5.2 0.5.3 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.7.0 0.7.1 0.7.2 0.8.0 0.9.0 0.9.1
fakerpress / src / FakerPress / Plugin.php

Plugin.php in FakerPress 0.8.0, at src/FakerPress/Plugin.php

302 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin main class.
4 *
5 * @since 0.1.0
6 *
7 * @package FakerPress
8 * @subpackage Main
9 * @copyright Copyright (c) 2014-2025, Gustavo Bordoni
10 * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
11 */
12
13 namespace FakerPress;
14
15 /**
16 * Main plugin class used to setup all the necessary components.
17 *
18 * @since 0.1.0
19 */
20 class Plugin {
21 /**
22 * Plugin version, used for cache-busting of style and script file references.
23 *
24 * @since 0.5.1
25 *
26 * @var string
27 */
28 public const VERSION = '0.8.0';
29
30 /**
31 * @since 0.6.0
32 *
33 * @var string Plugin Directory.
34 */
35 public $plugin_dir;
36
37 /**
38 * @since 0.6.0
39 *
40 * @var string Plugin path.
41 */
42 public $plugin_path;
43
44 /**
45 * @since 0.6.0
46 *
47 * @var string Plugin URL.
48 */
49 public $plugin_url;
50
51 /**
52 * Variable holding the folder name of the plugin
53 *
54 * @since 0.1.0
55 *
56 * @var string
57 */
58 public static $folder = 'fakerpress';
59
60 /**
61 * Variable holding the slug name of the plugin
62 *
63 * @since 0.1.0
64 *
65 * @var string
66 */
67 public static $slug = 'fakerpress';
68
69 /**
70 * The __FILE__ that initialized the plugin
71 *
72 * @since 0.1.0
73 *
74 * @var string
75 */
76 public static $_file = __FP_FILE__;
77
78 /**
79 * The Plugin external website base domain
80 *
81 * @since 0.3.2
82 *
83 * @var string
84 */
85 public static $_ext_domain = 'https://fakerpress.com';
86
87 /**
88 * Store if the plugin has been booted or not.
89 *
90 * @since 0.6.2
91 *
92 * @var bool Whether the plugin has been booted or not.
93 */
94 protected static bool $booted = false;
95
96 /**
97 * Boots the plugin.
98 *
99 * @since 0.6.2
100 *
101 * @return Plugin
102 */
103 public static function boot(): Plugin {
104 // We assume that if we have the booted variable autoloading is complete.
105 if ( static::$booted ) {
106 return make( static::class );
107 }
108
109 static::$booted = true;
110
111 $plugin = new static();
112 $plugin->register();
113
114 return $plugin;
115 }
116
117 /**
118 * Plugin constructor.
119 *
120 * This is intentionally empty and protected to prevent direct instantiation, please use the `boot` method.
121 *
122 * @since 0.6.2
123 */
124 protected function __construct() {
125 // Intentionally empty.
126 }
127
128 /**
129 * Registers the plugin.
130 *
131 * Do not attempt to interact with FakerPress before this method is called.
132 *
133 * @since 0.6.2
134 */
135 protected function register(): void {
136 $this->plugin_path = trailingslashit( dirname( static::$_file ) );
137 $this->plugin_dir = trailingslashit( basename( $this->plugin_path ) );
138 $this->plugin_url = plugins_url( $this->plugin_dir, $this->plugin_path );
139
140 $this->autoload();
141
142 // Register this as a singleton on the container.
143 singleton( static::class, $this );
144
145 $this->bind_implementations();
146
147 /**
148 * Triggers an action for loading of functionality.
149 *
150 * @since 0.6.0
151 */
152 do_action( 'fakerpress.plugin_loaded' );
153 }
154
155 /**
156 * Autoload the classes for the plugin via Composer.
157 *
158 * @since 0.6.2
159 *
160 * @return void
161 */
162 protected function autoload(): void {
163 // Load Composer Vendor Modules
164 require_once $this->plugin_path . 'vendor-prefixed' . DIRECTORY_SEPARATOR . 'autoload.php';
165 }
166
167 /**
168 * Register the implementations of the plugin with the container.
169 *
170 * @since 0.6.2
171 */
172 protected function bind_implementations(): void {
173 singleton( Admin::class, Admin::class );
174 singleton( Admin\Menu::class, Admin\Menu::class );
175 singleton( Ajax::class, Ajax::class );
176 singleton( Utils\Assets::class, Utils\Assets::class );
177 singleton( Utils::class, Utils::class );
178
179 // Register all the Service Providers.
180 register( Assets::class );
181 register( Hooks::class );
182
183 register( Module\Factory::class );
184 register( Admin\View\Factory::class );
185 register( Fields\Factory::class );
186 }
187
188 /**
189 * Return a Path relative to the plugin root
190 *
191 * @since 0.1.0
192 *
193 * @uses plugin_dir_path
194 *
195 * @param string $append A string to be appended to the root path
196 *
197 * @return string The path after being appended by the variable
198 */
199 public static function path( string $append = '' ): string {
200 return (string) make( static::class )->plugin_path . str_replace( '/', DIRECTORY_SEPARATOR, $append );
201 }
202
203 /**
204 * Return a URL relative to the plugin root
205 *
206 * @since 0.1.0
207 * @uses plugins_url
208 *
209 * @param string $file A string to be appended to the root url
210 *
211 * @return string The url to the file
212 */
213 public static function url( string $file = '' ): string {
214 return (string) make( static::class )->plugin_url . $file;
215 }
216
217 /**
218 * Return a URL relative to the plugin's administration page.
219 *
220 * @since 0.1.0
221 *
222 * @uses admin_url
223 * @uses wp_parse_args
224 * @uses add_query_arg
225 *
226 * @param string|array $args Arguments for the admin URL
227 * @param string $hash Hash for the admin URL
228 *
229 * @return string The url to the file
230 */
231 public static function admin_url( $args = '', $hash = false ): string {
232 /**
233 * Define the array of defaults
234 */
235 $defaults = [
236 'page' => static::$slug,
237 ];
238
239 /**
240 * Parse incoming $args into an array and merge it with $defaults
241 */
242 $args = wp_parse_args( $args, $defaults );
243
244 return add_query_arg( $args, admin_url( 'admin.php' ) ) . ( $hash !== false ? "#{$hash}" : '' );
245 }
246
247 /**
248 * Returns a URL for the external project website
249 *
250 * @since 0.3.2
251 * @uses esc_url_raw
252 *
253 * @param string $path Hash for the admin URL
254 *
255 * @return string The url the external website with the appended $path
256 */
257 public static function ext_site_url( string $path = '/' ): string {
258 return esc_url_raw( static::$_ext_domain . ( ! empty( $path ) ? $path : '/' ), [ 'http', 'https' ] );
259 }
260
261 public static function get( $name, $default = false ) {
262 $options = static::all();
263 $value = get( $options, $name, $default );
264
265 return $value;
266 }
267
268 public static function update( $name = null, $value = false ) {
269 $options = static::all();
270 $opts = [];
271
272 foreach ( (array) $name as $k => $index ) {
273 if ( 0 === $k ) {
274 $opts[ - 1 ] = &$options;
275 }
276
277 if ( count( $name ) - 1 !== $k && ! isset( $opts[ $k - 1 ][ $index ] ) ) {
278 $opts[ $k - 1 ][ $index ] = [];
279 }
280
281 if ( isset( $opts[ $k - 1 ][ $index ] ) ) {
282 $opts[ $k ] = &$opts[ $k - 1 ][ $index ];
283 } else {
284 $opts[ $k - 1 ][ $index ] = $value;
285 }
286 }
287 $opts[ $k ] = $value;
288
289 return update_option( static::$slug . '-plugin-options', $options );
290 }
291
292 public static function remove( $name = null ) {
293 // @TODO
294 }
295
296 public static function all() {
297 $defaults = [];
298
299 return get_option( static::$slug . '-plugin-options', $defaults );
300 }
301 }
302