-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-example.php
More file actions
341 lines (308 loc) · 8.48 KB
/
Copy pathplugin-example.php
File metadata and controls
341 lines (308 loc) · 8.48 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
/**
* Plugin Name: PathOfSettings Example Plugin
* Plugin URI: https://github.com/whaze/path-of-settings
* Description: Example implementation of PathOfSettings package in a WordPress plugin
* Version: 1.0.0
* Author: Jerome Buquet
* Requires PHP: 7.4
* Text Domain: pos-example-plugin
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Example plugin demonstrating PathOfSettings usage.
*
* Shows how to integrate PathOfSettings package into a WordPress plugin,
* including proper initialization, error handling, and settings registration.
*
* @package PathOfSettings\Examples
* @since 1.0.0
*/
class PathOfSettingsExamplePlugin {
/**
* Singleton instance.
*
* @since 1.0.0
* @var self|null
*/
private static $instance = null;
/**
* Get singleton instance.
*
* @since 1.0.0
* @return self
*/
public static function getInstance(): self {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Initialize the example plugin.
*
* @since 1.0.0
*/
private function __construct() {
add_action( 'plugins_loaded', [ $this, 'init' ] );
}
/**
* Initialize plugin components and dependencies.
*
* @since 1.0.0
*/
public function init(): void {
if ( ! $this->loadDependencies() ) {
return;
}
$this->initPathOfSettings();
add_action( 'pos_register_pages', [ $this, 'registerSettingsPages' ] );
}
/**
* Load required dependencies and check for PathOfSettings package.
*
* @since 1.0.0
* @return bool True if dependencies loaded successfully
*/
private function loadDependencies(): bool {
if ( ! file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
add_action( 'admin_notices', [ $this, 'showComposerNotice' ] );
return false;
}
require_once __DIR__ . '/vendor/autoload.php';
if ( ! class_exists( '\PathOfSettings\PathOfSettings' ) ) {
add_action( 'admin_notices', [ $this, 'showPackageNotice' ] );
return false;
}
return true;
}
/**
* Initialize PathOfSettings package.
*
* @since 1.0.0
*/
private function initPathOfSettings(): void {
\PathOfSettings\PathOfSettings::getInstance()->init();
}
/**
* Display notice when Composer dependencies are missing.
*
* @since 1.0.0
*/
public function showComposerNotice(): void {
echo '<div class="notice notice-error"><p>';
echo esc_html__( 'PathOfSettings Example Plugin: Please run "composer install" in the plugin directory.', 'pos-example-plugin' );
echo '</p></div>';
}
/**
* Display notice when PathOfSettings package is missing.
*
* @since 1.0.0
*/
public function showPackageNotice(): void {
echo '<div class="notice notice-error"><p>';
echo esc_html__( 'PathOfSettings Example Plugin: The whaze/path-of-settings package is not installed. Run "composer require whaze/path-of-settings".', 'pos-example-plugin' );
echo '</p></div>';
}
/**
* Register example settings pages and fields.
*
* Demonstrates various field types and configuration options.
*
* @since 1.0.0
*/
public function registerSettingsPages(): void {
$this->registerGeneralSettings();
$this->registerAdvancedSettings();
}
/**
* Register general settings page.
*
* @since 1.0.0
*/
private function registerGeneralSettings(): void {
pos_register_page(
'example-general',
[
'title' => __( 'General Settings', 'pos-example-plugin' ),
'menu_title' => __( 'Example Settings', 'pos-example-plugin' ),
'capability' => 'manage_options',
]
);
pos_add_field(
'example-general',
'text',
'site_name',
[
'label' => __( 'Site Name', 'pos-example-plugin' ),
'description' => __( 'Enter your site name for branding purposes', 'pos-example-plugin' ),
'default' => get_bloginfo( 'name' ),
'required' => true,
'placeholder' => __( 'My Awesome Site', 'pos-example-plugin' ),
]
);
pos_add_field(
'example-general',
'textarea',
'site_description',
[
'label' => __( 'Site Description', 'pos-example-plugin' ),
'description' => __( 'Provide a brief description of your website', 'pos-example-plugin' ),
'default' => get_bloginfo( 'description' ),
'placeholder' => __( 'A fantastic WordPress website...', 'pos-example-plugin' ),
'rows' => 4,
]
);
pos_add_field(
'example-general',
'select',
'theme_style',
[
'label' => __( 'Theme Style', 'pos-example-plugin' ),
'description' => __( 'Choose the visual style for your site', 'pos-example-plugin' ),
'default' => 'modern',
'options' => [
'classic' => __( 'Classic', 'pos-example-plugin' ),
'modern' => __( 'Modern', 'pos-example-plugin' ),
'minimal' => __( 'Minimal', 'pos-example-plugin' ),
'bold' => __( 'Bold', 'pos-example-plugin' ),
],
]
);
pos_add_field(
'example-general',
'checkbox',
'enable_features',
[
'label' => __( 'Enable Advanced Features', 'pos-example-plugin' ),
'description' => __( 'Check to enable advanced functionality', 'pos-example-plugin' ),
'default' => false,
]
);
pos_add_field(
'example-general',
'text',
'api_key',
[
'label' => __( 'API Key', 'pos-example-plugin' ),
'description' => __( 'Enter your API key for external services', 'pos-example-plugin' ),
'placeholder' => __( 'sk-...', 'pos-example-plugin' ),
]
);
}
/**
* Register advanced settings page.
*
* @since 1.0.0
*/
private function registerAdvancedSettings(): void {
pos_register_page(
'example-advanced',
[
'title' => __( 'Advanced Settings', 'pos-example-plugin' ),
'menu_title' => __( 'Advanced', 'pos-example-plugin' ),
'capability' => 'manage_options',
]
);
pos_add_field(
'example-advanced',
'select',
'cache_duration',
[
'label' => __( 'Cache Duration', 'pos-example-plugin' ),
'description' => __( 'Select how long to cache data', 'pos-example-plugin' ),
'default' => '3600',
'options' => [
'300' => __( '5 minutes', 'pos-example-plugin' ),
'1800' => __( '30 minutes', 'pos-example-plugin' ),
'3600' => __( '1 hour', 'pos-example-plugin' ),
'86400' => __( '24 hours', 'pos-example-plugin' ),
],
]
);
pos_add_field(
'example-advanced',
'checkbox',
'debug_mode',
[
'label' => __( 'Debug Mode', 'pos-example-plugin' ),
'description' => __( 'Enable debug mode (not recommended for production)', 'pos-example-plugin' ),
'default' => false,
]
);
}
}
PathOfSettingsExamplePlugin::getInstance();
/**
* Utility functions for retrieving example settings.
*/
/**
* Get the configured site name.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Site name
*/
function pos_example_get_site_name( string $default = '' ): string {
return pos_get_setting( 'example-general', 'site_name', $default );
}
/**
* Get the configured site description.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Site description
*/
function pos_example_get_site_description( string $default = '' ): string {
return pos_get_setting( 'example-general', 'site_description', $default );
}
/**
* Get the selected theme style.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Theme style
*/
function pos_example_get_theme_style( string $default = 'modern' ): string {
return pos_get_setting( 'example-general', 'theme_style', $default );
}
/**
* Check if advanced features are enabled.
*
* @since 1.0.0
* @return bool True if advanced features are enabled
*/
function pos_example_is_features_enabled(): bool {
return (bool) pos_get_setting( 'example-general', 'enable_features', false );
}
/**
* Get the API key.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string API key
*/
function pos_example_get_api_key( string $default = '' ): string {
return pos_get_setting( 'example-general', 'api_key', $default );
}
/**
* Get the cache duration in seconds.
*
* @since 1.0.0
* @param int $default Default value if setting not found
* @return int Cache duration in seconds
*/
function pos_example_get_cache_duration( int $default = 3600 ): int {
return (int) pos_get_setting( 'example-advanced', 'cache_duration', $default );
}
/**
* Check if debug mode is enabled.
*
* @since 1.0.0
* @return bool True if debug mode is enabled
*/
function pos_example_is_debug_enabled(): bool {
return (bool) pos_get_setting( 'example-advanced', 'debug_mode', false );
}