-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-example.php
More file actions
638 lines (576 loc) · 16.1 KB
/
Copy paththeme-example.php
File metadata and controls
638 lines (576 loc) · 16.1 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
<?php
/**
* PathOfSettings Theme Integration Example
*
* This file demonstrates how to integrate PathOfSettings package into a WordPress theme.
* Include this file in your theme's functions.php:
* require_once get_template_directory() . '/examples/theme-example.php';
*
* Or copy the relevant parts directly into your functions.php file.
*
* @package PathOfSettings\Examples
* @since 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Theme options manager using PathOfSettings.
*
* Demonstrates how to create comprehensive theme options with PathOfSettings,
* including appearance settings, typography, and advanced customization options.
*
* @since 1.0.0
*/
class PathOfSettingsThemeExample {
/**
* 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 theme options.
*
* @since 1.0.0
*/
private function __construct() {
add_action( 'after_setup_theme', [ $this, 'init' ] );
}
/**
* Initialize theme integration.
*
* @since 1.0.0
*/
public function init(): void {
if ( ! $this->loadDependencies() ) {
return;
}
$this->initPathOfSettings();
$this->registerHooks();
}
/**
* Load dependencies and check for PathOfSettings package.
*
* @since 1.0.0
* @return bool True if dependencies loaded successfully
*/
private function loadDependencies(): bool {
$autoloader = get_template_directory() . '/vendor/autoload.php';
if ( ! file_exists( $autoloader ) ) {
add_action( 'admin_notices', [ $this, 'showComposerNotice' ] );
return false;
}
require_once $autoloader;
if ( ! class_exists( '\PathOfSettings\PathOfSettings' ) ) {
add_action( 'admin_notices', [ $this, 'showPackageNotice' ] );
return false;
}
return true;
}
/**
* Initialize PathOfSettings for theme usage.
*
* @since 1.0.0
*/
private function initPathOfSettings(): void {
\PathOfSettings\PathOfSettings::getInstance()->init();
}
/**
* Register WordPress hooks.
*
* @since 1.0.0
*/
private function registerHooks(): void {
add_action( 'pos_register_pages', [ $this, 'registerThemePages' ] );
add_action( 'wp_head', [ $this, 'outputCustomCSS' ] );
add_action( 'wp_footer', [ $this, 'outputCustomJS' ] );
}
/**
* Show notice when Composer is not installed.
*
* @since 1.0.0
*/
public function showComposerNotice(): void {
if ( current_user_can( 'edit_theme_options' ) ) {
echo '<div class="notice notice-error"><p>';
echo esc_html__( 'Theme: Please run "composer install" in the theme directory to use PathOfSettings.', 'textdomain' );
echo '</p></div>';
}
}
/**
* Show notice when PathOfSettings package is missing.
*
* @since 1.0.0
*/
public function showPackageNotice(): void {
if ( current_user_can( 'edit_theme_options' ) ) {
echo '<div class="notice notice-error"><p>';
echo esc_html__( 'Theme: The whaze/path-of-settings package is not installed. Run "composer require whaze/path-of-settings".', 'textdomain' );
echo '</p></div>';
}
}
/**
* Register theme options pages.
*
* @since 1.0.0
*/
public function registerThemePages(): void {
$this->registerAppearanceSettings();
$this->registerAdvancedSettings();
}
/**
* Register appearance settings page.
*
* @since 1.0.0
*/
private function registerAppearanceSettings(): void {
pos_register_page(
'theme-appearance',
[
'title' => __( 'Theme Appearance', 'textdomain' ),
'menu_title' => __( 'Theme Options', 'textdomain' ),
'capability' => 'edit_theme_options',
]
);
// Layout Settings
pos_add_field(
'theme-appearance',
'select',
'layout',
[
'label' => __( 'Default Layout', 'textdomain' ),
'description' => __( 'Choose the default page layout for your site', 'textdomain' ),
'default' => 'full-width',
'options' => [
'full-width' => __( 'Full Width', 'textdomain' ),
'boxed' => __( 'Boxed', 'textdomain' ),
'sidebar-left' => __( 'Left Sidebar', 'textdomain' ),
'sidebar-right' => __( 'Right Sidebar', 'textdomain' ),
],
]
);
// Color Scheme
pos_add_field(
'theme-appearance',
'select',
'color_scheme',
[
'label' => __( 'Color Scheme', 'textdomain' ),
'description' => __( 'Select the color scheme for your theme', 'textdomain' ),
'default' => 'default',
'options' => [
'default' => __( 'Default', 'textdomain' ),
'dark' => __( 'Dark Mode', 'textdomain' ),
'light' => __( 'Light Mode', 'textdomain' ),
'blue' => __( 'Blue Theme', 'textdomain' ),
'green' => __( 'Green Theme', 'textdomain' ),
'custom' => __( 'Custom Colors', 'textdomain' ),
],
]
);
// Custom Colors
pos_add_field(
'theme-appearance',
'text',
'primary_color',
[
'label' => __( 'Primary Color', 'textdomain' ),
'description' => __( 'Hexadecimal color code for the primary theme color (e.g., #ff0000)', 'textdomain' ),
'placeholder' => '#007cba',
]
);
pos_add_field(
'theme-appearance',
'text',
'secondary_color',
[
'label' => __( 'Secondary Color', 'textdomain' ),
'description' => __( 'Hexadecimal color code for the secondary theme color', 'textdomain' ),
'placeholder' => '#ffffff',
]
);
// Typography
pos_add_field(
'theme-appearance',
'select',
'font_family',
[
'label' => __( 'Font Family', 'textdomain' ),
'description' => __( 'Choose the primary font for your site', 'textdomain' ),
'default' => 'system',
'options' => [
'system' => __( 'System Font', 'textdomain' ),
'arial' => 'Arial, sans-serif',
'helvetica' => 'Helvetica, sans-serif',
'georgia' => 'Georgia, serif',
'times' => 'Times New Roman, serif',
'roboto' => 'Roboto (Google Fonts)',
'open-sans' => 'Open Sans (Google Fonts)',
],
]
);
pos_add_field(
'theme-appearance',
'select',
'font_size',
[
'label' => __( 'Base Font Size', 'textdomain' ),
'description' => __( 'Default font size for body text', 'textdomain' ),
'default' => '16',
'options' => [
'14' => '14px',
'15' => '15px',
'16' => '16px',
'17' => '17px',
'18' => '18px',
'20' => '20px',
],
]
);
// Header/Footer Options
pos_add_field(
'theme-appearance',
'checkbox',
'show_search',
[
'label' => __( 'Show Search in Header', 'textdomain' ),
'description' => __( 'Display search form in the site header', 'textdomain' ),
'default' => true,
]
);
pos_add_field(
'theme-appearance',
'textarea',
'footer_text',
[
'label' => __( 'Footer Text', 'textdomain' ),
'description' => __( 'Custom text to display in the site footer', 'textdomain' ),
'default' => sprintf( __( '© %s - All rights reserved', 'textdomain' ), date( 'Y' ) ),
'rows' => 3,
]
);
}
/**
* Register advanced settings page.
*
* @since 1.0.0
*/
private function registerAdvancedSettings(): void {
pos_register_page(
'theme-advanced',
[
'title' => __( 'Advanced Theme Settings', 'textdomain' ),
'menu_title' => __( 'Advanced', 'textdomain' ),
'capability' => 'edit_theme_options',
]
);
// Custom Code
pos_add_field(
'theme-advanced',
'textarea',
'custom_css',
[
'label' => __( 'Custom CSS', 'textdomain' ),
'description' => __( 'Add custom CSS styles here', 'textdomain' ),
'placeholder' => '/* Your custom CSS here */',
'rows' => 10,
]
);
pos_add_field(
'theme-advanced',
'textarea',
'custom_js',
[
'label' => __( 'Custom JavaScript', 'textdomain' ),
'description' => __( 'Add custom JavaScript code here (without script tags)', 'textdomain' ),
'placeholder' => '// Your custom JavaScript here',
'rows' => 10,
]
);
// Analytics
pos_add_field(
'theme-advanced',
'text',
'google_analytics',
[
'label' => __( 'Google Analytics ID', 'textdomain' ),
'description' => __( 'Enter your Google Analytics tracking ID (e.g., G-XXXXXXXXXX)', 'textdomain' ),
'placeholder' => 'G-XXXXXXXXXX',
]
);
// Performance
pos_add_field(
'theme-advanced',
'checkbox',
'minify_css',
[
'label' => __( 'Minify CSS', 'textdomain' ),
'description' => __( 'Enable CSS minification for better performance', 'textdomain' ),
'default' => false,
]
);
pos_add_field(
'theme-advanced',
'checkbox',
'enable_lazy_loading',
[
'label' => __( 'Lazy Load Images', 'textdomain' ),
'description' => __( 'Enable lazy loading for images to improve page load speed', 'textdomain' ),
'default' => true,
]
);
}
/**
* Output custom CSS in the document head.
*
* @since 1.0.0
*/
public function outputCustomCSS(): void {
$custom_css = pos_get_setting( 'theme-advanced', 'custom_css', '' );
$primary_color = pos_get_setting( 'theme-appearance', 'primary_color', '' );
$secondary_color = pos_get_setting( 'theme-appearance', 'secondary_color', '' );
$font_family = pos_get_setting( 'theme-appearance', 'font_family', 'system' );
$font_size = pos_get_setting( 'theme-appearance', 'font_size', '16' );
if ( $custom_css || $primary_color || $secondary_color || $font_family !== 'system' ) {
echo '<style id="theme-custom-css">';
// CSS Custom Properties
if ( $primary_color || $secondary_color ) {
echo ':root {';
if ( $primary_color ) {
echo '--theme-primary-color: ' . esc_attr( $primary_color ) . ';';
}
if ( $secondary_color ) {
echo '--theme-secondary-color: ' . esc_attr( $secondary_color ) . ';';
}
echo '}';
}
// Typography
if ( $font_family !== 'system' ) {
$font_stack = $this->getFontStack( $font_family );
echo 'body { font-family: ' . $font_stack . '; font-size: ' . intval( $font_size ) . 'px; }';
}
// Custom CSS
if ( $custom_css ) {
echo wp_strip_all_tags( $custom_css );
}
echo '</style>';
}
$this->outputGoogleAnalytics();
}
/**
* Output Google Analytics tracking code.
*
* @since 1.0.0
*/
private function outputGoogleAnalytics(): void {
$ga_id = pos_get_setting( 'theme-advanced', 'google_analytics', '' );
if ( $ga_id && ! is_admin() ) {
echo "<!-- Google Analytics -->\n";
echo "<script async src='https://www.googletagmanager.com/gtag/js?id=" . esc_attr( $ga_id ) . "'></script>\n";
echo "<script>\n";
echo "window.dataLayer = window.dataLayer || [];\n";
echo "function gtag(){dataLayer.push(arguments);}\n";
echo "gtag('js', new Date());\n";
echo "gtag('config', '" . esc_attr( $ga_id ) . "');\n";
echo "</script>\n";
}
}
/**
* Output custom JavaScript in the document footer.
*
* @since 1.0.0
*/
public function outputCustomJS(): void {
$custom_js = pos_get_setting( 'theme-advanced', 'custom_js', '' );
if ( $custom_js && ! is_admin() ) {
echo '<script id="theme-custom-js">';
echo wp_strip_all_tags( $custom_js );
echo '</script>';
}
}
/**
* Get font stack for the specified font family.
*
* @since 1.0.0
* @param string $font_family Font family identifier
* @return string CSS font stack
*/
private function getFontStack( string $font_family ): string {
$fonts = [
'system' => '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
'arial' => 'Arial, Helvetica, sans-serif',
'helvetica' => 'Helvetica, Arial, sans-serif',
'georgia' => 'Georgia, "Times New Roman", serif',
'times' => '"Times New Roman", Times, serif',
'roboto' => 'Roboto, Arial, sans-serif',
'open-sans' => '"Open Sans", Arial, sans-serif',
];
return $fonts[ $font_family ] ?? $fonts['system'];
}
}
PathOfSettingsThemeExample::getInstance();
/**
* Theme utility functions for retrieving settings.
*/
/**
* Get the theme layout setting.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Layout setting
*/
function theme_get_layout( string $default = 'full-width' ): string {
return pos_get_setting( 'theme-appearance', 'layout', $default );
}
/**
* Get the color scheme setting.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Color scheme setting
*/
function theme_get_color_scheme( string $default = 'default' ): string {
return pos_get_setting( 'theme-appearance', 'color_scheme', $default );
}
/**
* Get the primary color setting.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Primary color hex code
*/
function theme_get_primary_color( string $default = '' ): string {
return pos_get_setting( 'theme-appearance', 'primary_color', $default );
}
/**
* Get the secondary color setting.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Secondary color hex code
*/
function theme_get_secondary_color( string $default = '' ): string {
return pos_get_setting( 'theme-appearance', 'secondary_color', $default );
}
/**
* Check if search should be shown in header.
*
* @since 1.0.0
* @return bool True if search should be displayed
*/
function theme_should_show_search(): bool {
return (bool) pos_get_setting( 'theme-appearance', 'show_search', true );
}
/**
* Get the footer text setting.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Footer text
*/
function theme_get_footer_text( string $default = '' ): string {
if ( empty( $default ) ) {
$default = sprintf( __( '© %s - All rights reserved', 'textdomain' ), date( 'Y' ) );
}
return pos_get_setting( 'theme-appearance', 'footer_text', $default );
}
/**
* Get the font family setting.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Font family setting
*/
function theme_get_font_family( string $default = 'system' ): string {
return pos_get_setting( 'theme-appearance', 'font_family', $default );
}
/**
* Get the font size setting.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Font size in pixels
*/
function theme_get_font_size( string $default = '16' ): string {
return pos_get_setting( 'theme-appearance', 'font_size', $default );
}
/**
* Get the custom CSS setting.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Custom CSS code
*/
function theme_get_custom_css( string $default = '' ): string {
return pos_get_setting( 'theme-advanced', 'custom_css', $default );
}
/**
* Get the custom JavaScript setting.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Custom JavaScript code
*/
function theme_get_custom_js( string $default = '' ): string {
return pos_get_setting( 'theme-advanced', 'custom_js', $default );
}
/**
* Get the Google Analytics ID setting.
*
* @since 1.0.0
* @param string $default Default value if setting not found
* @return string Google Analytics tracking ID
*/
function theme_get_google_analytics( string $default = '' ): string {
return pos_get_setting( 'theme-advanced', 'google_analytics', $default );
}
/**
* Check if CSS minification is enabled.
*
* @since 1.0.0
* @return bool True if CSS minification is enabled
*/
function theme_is_css_minification_enabled(): bool {
return (bool) pos_get_setting( 'theme-advanced', 'minify_css', false );
}
/**
* Check if lazy loading is enabled.
*
* @since 1.0.0
* @return bool True if lazy loading is enabled
*/
function theme_is_lazy_loading_enabled(): bool {
return (bool) pos_get_setting( 'theme-advanced', 'enable_lazy_loading', true );
}
/**
* Get all theme appearance settings.
*
* @since 1.0.0
* @return array All appearance settings
*/
function theme_get_appearance_settings(): array {
return pos_get_settings( 'theme-appearance' );
}
/**
* Get all theme advanced settings.
*
* @since 1.0.0
* @return array All advanced settings
*/
function theme_get_advanced_settings(): array {
return pos_get_settings( 'theme-advanced' );
}