-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
2451 lines (2118 loc) · 86.6 KB
/
functions.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
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// =========================
// ======== BIOSHIP ========
// =========================
// ==== Theme Framework ====
// =========================
// BioShip HomePage: https://bioship.space
// Support and Features: https://wordquest.org/support/bioship/
// GitHub Repository: https://github.com/majick777/bioship/
// -------------------
// Full Documentation!
// -------------------
// To read detailed and complete Documentation for BioShip:
// a) popup via the Docs link on the BioShip Theme Options page
// b) read directly by loading /wp-content/themes/bioship/admin/docs.php
// c) available online at https://bioship.space/documentation/
// -------
// License
// -------
// BioShip Theme Framework, super flexible theme for WordPress
// Copyright (C) 2016-2019 Tony Hayes
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// ---------------
// Reference Links
// ---------------
// * See includes/links.txt for complete list of included resources.
// * See includes/licenses.txt for list of all resource licenses.
// ------------------
// Mini Theme History
// ------------------
// Latest Update News: https://bioship.space/news/
// Known Minimum Requirement: WordPress 3.4 (wp_get_theme)
// Original Development from: WordPress ~3.8
// Public Beta Version Available from: WordPress ~4.0
// Public Release Candidate Available: WordPress ~4.5
// Second Public Beta Available from: WordPress ~4.6
// Hotfixed Public Version: WordPress ~4.7
// First WordPress.Org Submission Version: WordPress ~4.8
// Second WordPress.Org Submission Version: WordPress ~4.9
// Third WordPress.Org Submission Version: ~5.0
// -------------------------------
// === functions.php Structure ===
// -------------------------------
// [optional] WordPress auto-loads Child Theme functions.php
// === Theme Setup ===
// - AutoSet Framework Version
// - Set WordQuest Theme 'Plugin' Info
// - Set Framework URLs
// - Set Global Theme Directories and URLs
// - Set Windows Path Constant
// - Set Global Resource Paths
// - Comprehensive File Hierarchy
// - Load Helper Functions (helpers.php)
// === Load Theme ===
// - Load Theme Backwards Compatiblity (compat.php)
// - Load Current Theme Data
// - Determine Theme for Test Drive Plugin
// - Theme Test Drive Compatibility
// - Load Theme Value Filters (filters.php)
// - Set Site Icon Constant
// - Filter Resource Directories
// - Set Global Theme Name and Parent/Child
// - Set Child Theme Version
// === Debugging ===
// - Set Theme Debug Mode Switch
// - Start Theme Debug Output/Log
// - Set Debug Directory
// - Load Theme Tracer (tracer.php)
// === Theme Settings ===
// - Convert Posted Customizer Preview Options
// - Get Theme Settings Filter - with Fallbacks!
// - Check/Fix Updated Theme Settings (PreSave)
// - Debug Updated Theme Settings
// - On/Off File Switch for Titan Framework
// - Check Options Framework to Load
// -- Titan Framework Load
// -- Options Framework Load
// - Set Framework Flag Constants
// - Map Titan Option values to Theme Settings
// - Load Theme Options and Settings
// - Load Theme Admin Functions (admin/admin.php)
// - Set HTML Comments Constant
// - Set Script Cachebusting
// - Set Stylesheet Cachebusting
// - Declare WooCommerce Support
// - Set Post Format Theme Supports
// - Output Theme Settings Debug Info
// === Customizer ===
// - Customizer Loader (admin/customizer.php)
// - Load Customizer Controls
// - Customizer Preview Window
// - Customizer Loading Image
// === Hybrid Core ===
// - Load Hybrid Core
// - Non Hybrid Fallback Loading
// - Fix to Main Element IDs
// === Skull, Skeleton, and Muscle ===
// - Require Head and Template Setup (skull.php)
// - Require Layout Hooks Definitions (hooks.php)
// - Require Skeleton Templating Functions (skeleton.php)
// - Include Muscle Extended Functions (muscle.php)
// - Grid and Skin Loading Functions (moved to skull.php)
// --- no direct load ---
if ( !defined( 'ABSPATH' ) ) {exit;}
// -------------------
// === Theme Setup ===
// -------------------
// ------------------------------------
// Define DIRECTORY_SEPARATOR Pseudonym
// ------------------------------------
if ( !defined( 'DIRSEP' ) ) {
define( 'DIRSEP', DIRECTORY_SEPARATOR );
}
// ---------------------
// AutoSet Framework Version
// ---------------------
// 2.0.1: get theme version direct from style.css
// 2.0.5: set theme prefix constant
$vthemestylecsspath = dirname( __FILE__ ) . DIRSEP . 'style.css';
$vthemeheaders = get_file_data( $vthemestylecsspath, array( 'Version' => 'Version' ) );
define( 'THEMEVERSION', $vthemeheaders['Version'] );
if ( !defined( 'THEMEPREFIX' ) ) {
define( 'THEMEPREFIX', 'bioship' );
}
// ---------------------------------
// Set WordQuest Theme 'Plugin' Info
// ---------------------------------
// 2.1.0: use 3 letters for settings prefix
global $wordquestplugins;
$slug = THEMEPREFIX;
$wordquestplugins[$slug] = array(
'version' => THEMEVERSION,
'title' => __( 'BioShip Theme', 'bioship' ),
'settings' => substr( THEMEPREFIX, 0, 3 ),
'plan' => 'free',
'hasplans' => false,
);
// --- WordPress.Org values ---
// note: set to false here and rechecked with THEMEWPORG constant
$wordquestplugins[$slug]['wporg'] = false;
// TODO: add this definition if/when in WordPress.Org repo
// $wordquestplugins[$slug]['wporgslug'] = 'bioship';
// ------------------
// Set Framework URLs
// ------------------
// 2.0.0: added BioShip Home URL constant
// 2.0.1: change constant name for consistency
// 2.0.1: added Support Forum Site URL constant
// 2.0.1: set Theme SSL constant here also
// 2.2.0: change links from http to https
define( 'THEMEHOMEURL', 'https://bioship.space' );
define( 'THEMESUPPORT', 'https://wordquest.org' );
if ( !defined( 'THEMESSL' ) ) {
$vthemessl = is_ssl();
define( 'THEMESSL', $vthemessl );
}
// -------------------------------------
// Set Global Theme Directories and URLs
// -------------------------------------
// 1.8.0: use directory separator not trailingslashit for directories
// 1.9.0: added 'theme' prefix to all global names
global $vthemestyledir, $vthemestyleurl, $vthemetemplatedir, $vthemetemplateurl;
$vthemestyledir = get_stylesheet_directory() . DIRSEP;
$vthemestyleurl = trailingslashit( get_stylesheet_directory_uri() );
$vthemetemplatedir = get_template_directory() . DIRSEP;
$vthemetemplateurl = trailingslashit( get_template_directory_uri() );
// 1.8.0: enforce SSL recheck of theme URLs
if ( THEMESSL ) {
$vthemestyleurl = str_replace( 'http://', 'https://', $vthemestyleurl );
$vthemetemplateurl = str_replace( 'http://', 'https://', $vthemetemplateurl );
}
// -------------------------
// Set Windows Path Constant
// -------------------------
// (may help paths on some local dev Windows IIS environments)
// 2.0.9: use operating system check for Windows paths
if ( !defined( 'THEMEWINDOWS' ) ) {
if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ) {
define( 'THEMEWINDOWS', true );
} else {
define( 'THEMEWINDOWS', false );
}
}
if ( THEMEWINDOWS ) {
$vthemetemplatedir = str_replace( '/', '\\', $vthemetemplatedir );
$vthemestyledir = str_replace( '/', '\\', $vthemestyledir );
}
// -------------------------
// Set Global Resource Paths
// -------------------------
// 1.8.0: used for inbuilt file hierarchy calls
// 1.8.5: set defaults and filter later on (to allow for filters.php)
// 1.9.5: add scripts and styles to top of hierarchy
// 2.1.1: added includes theme directory paths
global $vthemedirs;
$vthemedirs = array(
'core' => array(),
'admin' => array( 'admin' ),
'style' => array( 'styles', 'css', 'assets/css' ),
'script' => array( 'scripts', 'javascripts', 'js', 'assets/js' ),
'image' => array( 'images', 'img', 'icons', 'assets/img' ),
'includes' => array( 'includes' ),
);
// ----------------------------
// Comprehensive File Hierarchy
// ----------------------------
// ...a magical fallback mystery tour...
// 1.8.0: use DIRECTORY_SEPARATOR in all paths
// 1.8.0: switch to directory array loop instead of 2 args
// 1.8.0: added optional search roots override argument
// (added for edge cases, ie. the search for /sidebar/page.php should *not* find /page.php)
// 2.0.9: added theme_file_search filter to return results
// 2.1.4: added function_exists wrappers to bioship_debug calls
if ( !function_exists( 'bioship_file_hierarchy' ) ) {
function bioship_file_hierarchy( $returntype, $filename, $dirs = array(), $searchroots = array( 'stylesheet', 'template' ) ) {
// 1.6.0: check if THEMETRACE constant is defined (in this function only!)
if ( defined( 'THEMETRACE' ) && THEMETRACE ) {bioship_trace( 'F', __FUNCTION__, __FILE__, func_get_args() );}
global $vthemestyledir, $vthemestyleurl, $vthemetemplatedir, $vthemetemplateurl;
// 1.8.5: just in case of bad argument fix ("I'm right", "No, I'm right!" ...)
if ( !is_array( $searchroots ) ) {
$searchroots = array( 'stylesheet', 'template' );
}
// --- replacement for windows paths ---
// 1.8.0: just use THEMEWINDOWS constant here instead of everywhere else...
// 2.0.1: use numerical index when looping array
$filedirs = $dirs;
if ( THEMEWINDOWS && ( count( $dirs ) > 0 ) ) {
foreach ( $dirs as $i => $dir ) {
$filedirs[$i] = str_replace( '/', '\\', $dir );
}
}
// --- debug hierarchy call and search directories ---
if ( defined( 'THEMEDEBUG' ) ) {
if ( function_exists( 'bioship_debug' ) ) {
bioship_debug( "File Hierarchy Call for " . $returntype, $filename );
if ( count( $dirs ) > 0 ) {
bioship_debug( "Search Directories", implode( ',', $dirs ) );
}
}
}
// --- set value flags ---
// 2.0.9: set default value flag
// 2.1.0: set default found flag
// 2.1.1: removed unnecessary double flag
$value = false;
// --- check stylesheet subdirectory(s) loop ---
if ( count( $dirs ) > 0 ) {
foreach ( $dirs as $i => $dir ) {
// 2.0.9: check for found value (stored for filtering)
if ( !$value ) {
$file = false;
// 1.8.0: added absolute path override possibility (prototype)
// (can be used via resource directory override filters)
// note: to work full absolute path must start and end with #
if ( strstr( $dir, '#' ) ) {
if ( ( '#' == substr( $dir, 0, 1 ) ) && ( '#' == substr( $dir, -1 ) ) ) {
$absdir = substr( $dir, 1, -1 );
$file = $absdir . $filename;
// TODO: check/improve this URL to path replacement ?
$url = str_replace( ABSPATH, site_url(), $file );
}
} elseif ( '' == $dir ) {
// 1.8.0: allow for root file to take precedence if specified
$file = $vthemestyledir . $filename;
$url = $vthemestyleurl . $filename;
} else {
$file = $vthemestyledir . $filedirs[$i] . DIRSEP . $filename;
$url = trailingslashit( $vthemestyleurl . $dir ) . $filename;
}
// 2.1.1: moved repeated logic out from if statements
if ( $file && file_exists( $file ) ) {
if ( 'file' == $returntype ) {
$value = $file;
} elseif ( 'url' == $returntype ) {
$value = $url;
} elseif ( 'both' == $returntype ) {
$value = array( 'file' => $file, 'url' => $url );
}
}
}
}
}
// --- fallback check of stylesheet base directory ---
if ( !$value && in_array( 'stylesheet', $searchroots ) ) {
$file = $vthemestyledir . $filename;
$url = $vthemestyleurl . $filename;
if ( file_exists( $file ) ) {
if ( 'file' == $returntype ) {
$value = $file;
} elseif ( 'url' == $returntype ) {
$value = $url;
} elseif ( 'both' == $returntype ) {
$value = array( 'file' => $file, 'url' => $url );
}
}
}
// --- check template subdirectory(s) loop ---
// 1.8.0: bug out early if the template and stylesheet directory are the same (no child theme)
// 2.1.1: combined with child theme check conditions to enable later filtering
if ( !$value && ( $vthemestyledir != $vthemetemplatedir ) ) {
if ( count( $dirs ) > 0 ) {
foreach ( $dirs as $i => $dir ) {
// 2.0.9: check for found value (stored for filtering)
if ( !$value ) {
$file = false;
// 1.8.0: allow for root file to take precedence if specified
if ( '' == $dir ) {
$file = $vthemetemplatedir . $filename;
$url = $vthemetemplateurl . $filename;
} else {
$file = $vthemetemplatedir . $filedirs[$i] . DIRSEP . $filename;
$url = trailingslashit( $vthemetemplateurl . $dir ) . $filename;
}
// 2.1.1: moved repeated logic out of if statements
if ( $file && file_exists( $file ) ) {
if ( 'file' == $returntype ) {
$value = $file;
} elseif ( 'url' == $returntype ) {
$value = $url;
} elseif ( 'both' == $returntype ) {
$value = array( 'file' => $file, 'url' => $url );
}
}
}
}
}
}
// --- check template base directory ---
// 2.1.1: combine with child theme check condition to prevent duplicate searches
if ( !$value && ( $vthemestyledir != $vthemetemplatedir ) && in_array( 'template', $searchroots ) ) {
$file = $vthemetemplatedir . $filename;
$url = $vthemetemplateurl . $filename;
if ( file_exists( $file ) ) {
if ( 'file' == $returntype ) {
$value = $file;
} elseif ( 'url' == $returntype ) {
$value = $url;
} elseif ( 'both' == $returntype ) {
$value = array( 'file' => $file, 'url' => $url );
}
}
}
// --- filter the found file path value ---
// 2.1.0: fix to incorrect filter variable name
// 2.1.1: compressed extra filter values to args array
// 2.1.4: added function_exists check for bioship_apply_filters
if ( function_exists( 'bioship_debug' ) ) {
bioship_debug( $filename . " Search File Return Value", $value );
}
$args = array( 'type' => $returntype, 'filename' => $filename, 'dirs' => $dirs, 'searchroots' => $searchroots );
if ( function_exists( 'bioship_apply_filters' ) ) {
$filtered = bioship_apply_filters( 'theme_file_search', $value, $args );
} else {
$filtered = apply_filters( 'bioship_theme_file_search', $value, $args );
}
if ( $filtered != $value ) {
if ( function_exists( 'bioship_debug' ) ) {
bioship_debug( "Filtered Filepath Search Value", $filtered );
}
// 2.1.1: added file exists check for filtered value
if ( file_exists( $filtered ) ) {
$value = $filtered;
} elseif ( function_exists( 'bioship_debug' ) ) {
bioship_debug( "Warning! Filtered Filepath Not Found" );
}
}
// --- return found file path ---
return $value;
}
}
// ---------------------
// Load Helper Functions
// ---------------------
// 2.1.4: moved helper functions to separate file
$helpers = bioship_file_hierarchy( 'file', 'helpers.php' );
require $helpers;
// ------------------
// === Load Theme ===
// ------------------
// ---------------------------------
// Load Theme Backwards Compatiblity
// ---------------------------------
// used (sparsely) for any function/filter name changes
// 1.8.5: load theme backwards compatibility file
// 2.0.5: add ability to turn compat.php loading off with a file (as pre-filters)
// 2.1.1: changed file switch name from compat-off.php to compat.off
$compat = bioship_file_hierarchy( 'file', 'compat.php' );
$compatoff = bioship_file_hierarchy( 'file', 'compat.off' );
if ( $compat && !$compatoff ) {
include_once $compat;
}
// ----------------------
// Get Current Theme Data
// ----------------------
// note: pre-3.4 compatibility function loaded in compat.php
$vtheme = wp_get_theme();
// -------------------------------------
// Determine Theme for Test Drive Plugin
// -------------------------------------
// (as Theme Test Drive plugin functions not loaded yet)
if ( !function_exists( 'bioship_themedrive_determine_theme' ) ) {
function bioship_themedrive_determine_theme() {
// --- get internal test drive value if any ---
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( !isset( $_REQUEST['theme'] ) ) {
// --- get permission level ---
$tdlevel = get_option( 'td_level' );
if ( '' != $tdlevel ) {
$permissions = 'level_' . $tdlevel;
} else {
$permissions = 'level_10';
}
// --- check permissions ---
if ( !current_user_can( $permissions ) ) {
return false;
} else {
// 2.1.1: added check for false value (unset option)
$tdtheme = get_option( 'td_themes' );
if ( !$tdtheme ) {
// 2.2.0: fix to missing return value
return false;
}
$tdtheme = trim( $tdtheme );
if ( empty( $tdtheme ) || ( '' == $tdtheme ) ) {
return false;
}
}
} else {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$tdtheme = $_REQUEST['theme'];
}
// --- get test drive theme data
$themedata = wp_get_theme( $tdtheme );
// -- loop all themes to match ---
// 2.1.1: simplified theme data check logic
if ( empty( $themedata ) ) {
$allthemes = wp_get_themes();
foreach ( $allthemes as $themedata ) {
if ( $themedata['Stylesheet'] == $tdtheme ) {
break;
}
}
}
// --- check for theme data ---
// 2.1.1: simplified theme data check logic
if ( !empty( $themedata ) ) {
// note: we are skipping the 'publish' check here - as may be a theme under development
// if (isset($themedata['Status']) && ($themedata['Status'] != 'publish')) {return false;}
return $themedata;
}
return false;
}
}
// ------------------------------
// Theme Test Drive Compatibility
// ------------------------------
// 1.5.0: added check that this is not a theme editor page
// (as theme editor can set a theme key in the querystring
// and this conflicts with a theme test drive parameter)
// TODO: recheck this behaviour on the Customizer page?
global $pagenow;
if ( ( 'theme-editor.php' != $pagenow ) && ( 'customize.php' != $pagenow ) ) {
$themetestdrive = bioship_themedrive_determine_theme();
if ( $themetestdrive ) {
// --- set theme drive constant ---
define( 'THEMEDRIVE', true );
$vtheme = $themetestdrive;
// --- set theme directories ---
// 1.8.0: override the style and template directory values
$vthemestyledir = get_stylesheet_directory( $vtheme['Stylesheet'] ) . DIRSEP;
$vthemestyleurl = trailingslashit( get_stylesheet_directory_uri( $vtheme['Stylesheet'] ) );
$vthemetemplatedir = get_template_directory( $vtheme['Template'] ) . DIRSEP;
$vthemetemplateurl = trailingslashit( get_template_directory_uri( $vtheme['Template'] ) );
// 1.8.5: re-enforce SSL recheck
if ( is_ssl() ) {
$vthemestyleurl = str_replace( 'http://', 'https://', $vthemestyleurl );
$vthemetemplateurl = str_replace( 'http://', 'https://', $vthemetemplateurl );
}
// --- load Admin Menu Fixes for Theme Test Driving ---
// 2.1.1: added missing function prefix
add_action( 'admin_menu', 'bioship_admin_themetestdrive_options', 12 );
// --- add theme filter for Options Framework ---
// 2.0.1: added missing function prefix
// 2.1.1: adding missing function exists wrapper
// TODO: retest with Options Framework + Customizer combination
// TODO: move to Options Framework integration section ?
if ( !function_exists( 'bioship_optionsframework_themetestdrive' ) ) {
add_filter( 'of_theme_value', 'bioship_optionsframework_themetestdrive' );
function bioship_optionsframework_themetestdrive( $theme ) {
if ( THEMETRACE ) {bioship_trace( 'F', __FUNCTION__, __FILE__, func_get_args() );}
$testdrive = bioship_themedrive_determine_theme();
$thetheme['id'] = preg_replace( "/\W/", "_", strtolower( $testdrive['Name'] ) );
return $theme;
}
}
}
}
// ------------------------
// Load Theme Value Filters
// ------------------------
// check for possible customized filters.php in parent/child theme
// note: *loaded early* as filters are used immediately if present
// 2.0.9: run core directory filter in case override already exists
$vthemedirs['core'] = bioship_apply_filters( 'theme_core_dirs', $vthemedirs['core'] );
$filters = bioship_file_hierarchy( 'file', 'filters.php', $vthemedirs['core'] );
if ( $filters ) {
include_once $filters;
}
// ----------------------
// Set Site Icon Constant
// ----------------------
// 2.0.8: added this to check for global site icon once only
// 2.0.9: make sure function exists and simplify logic (for backwards compatibility)
// 2.1.1: moved down so that any value filters can be loaded and applied
if ( !defined( 'THEMESITEICON' ) ) {
if ( function_exists( 'has_site_icon' ) && has_site_icon() ) {
$siteicon = true;
} else {
$siteicon = false;
}
$siteicon = bioship_apply_filters( 'skeleton_site_icon', $siteicon );
if ( $siteicon ) {
define( 'THEMESITEICON', $siteicon );
} else {
define( 'THEMESITEICON', false );
}
}
// ---------------------------
// Filter Resource Directories
// ---------------------------
// 1.8.5: run all the directory search filters here (as may be in filters.php)
$vthemedirs['core'] = bioship_apply_filters( 'theme_core_dirs', $vthemedirs['core'] );
$vthemedirs['admin'] = bioship_apply_filters( 'theme_admin_dirs', $vthemedirs['admin'] );
$vthemedirs['style'] = $vthemedirs['css'] = bioship_apply_filters( 'theme_style_dirs', $vthemedirs['style'] );
$vthemedirs['script'] = $vthemedirs['js'] = bioship_apply_filters( 'theme_script_dirs', $vthemedirs['script'] );
$vthemedirs['image'] = $vthemedirs['img'] = bioship_apply_filters( 'theme_image_dirs', $vthemedirs['image'] );
// 2.0.9: add a single directory array override filter for easier usage
$vthemedirs = bioship_apply_filters( 'theme_resource_dirs', $vthemedirs );
// --------------------------------------
// Set Global Theme Name and Parent/Child
// --------------------------------------
// 2.0.5: define THEMESLUG (using dashes not underscores)
// 2.0.5: cleaner code logic for child and parent theme constants
$vthemedisplayname = (string) $vtheme['Name'];
define( 'THEMEDISPLAYNAME', $vthemedisplayname );
$vthemename = preg_replace( "/\W/", "-", strtolower( $vthemedisplayname ) );
define( 'THEMESLUG', $vthemename );
if ( !is_child_theme() ) {
define( 'THEMECHILD', false );
define( 'THEMEPARENT', false );
} else {
define( 'THEMECHILD', true );
define( 'THEMEPARENT', (string) $vtheme['Template'] );
}
// --- Set Child Theme Version ---
// 2.0.1: simplify to set child theme version constant
// 2.0.5: cleaner code logic here
if ( !THEMECHILD ) {
define( 'THEMECHILDVERSION', THEMEVERSION );
} else {
define( 'THEMECHILDVERSION', $vtheme['Version'] );
}
// -----------------
// === Debugging ===
// -----------------
// ---------------------------
// Set Theme Debug Mode Switch
// ---------------------------
// 1.8.0: changed this to a constant, allow for switching
// 1.8.5: added all word values and new '3' option
// TODO: maybe move to tracer.php ?
// ?themedebug=0 or ?themedebug=off - switch theme debug mode off
// ?themedebug=1 or ?themedebug=on - switch theme debug mode on (persistant)
// ?themedebug=2 or ?themedebug=yes - debug mode on for this pageload (overrides)
// ?themedebug=3 or ?themedebug=no - debug mode off for this pageload (overrides)
if ( !defined( 'THEMEDEBUG' ) ) {
$vthemekey = preg_replace( "/\W/", "_", strtolower( $vthemedisplayname ) );
$themedebug = get_option( $vthemekey . '_theme_debug' );
if ( '1' == $themedebug ) {
$themedebug = true;
} else {
$themedebug = false;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_REQUEST['themedebug'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$debug = $_REQUEST['themedebug'];
// 1.8.5: authenticate debug capability
// 2.0.9: add filter for debug switching capability
$debugcap = bioship_apply_filters( 'theme_debug_capability', 'edit_theme_options' );
if ( current_user_can( $debugcap ) ) {
if ( ( '2' == $debug ) || ( 'yes' == $debug ) ) {
// --- debug on for this pageload only
$themedebug = true;
} elseif ( ( '3' == $debug ) || ( 'no' == $debug ) ) {
// --- debug off for this pageload only ---
$themedebug = false;
} elseif ( ( '1' == $debug ) || ( 'on' == $debug ) ) {
// --- switch theme debug on ---
$themedebug = true;
delete_option( $vthemekey . '_theme_debug' );
add_option( $vthemekey . '_theme_debug', '1' );
} elseif ( ( '0' == $debug ) || ( 'off' == $debug ) ) {
// --- switch theme debug off ---
$themedebug = false;
delete_option( $vthemekey . '_theme_debug' );
}
}
}
if ( ( '' == $themedebug ) || ( '0' == $themedebug ) ) {
$themedebug = false;
}
// --- filter debug mode ---
$themedebug = bioship_apply_filters( 'theme_debug', $themedebug );
// --- finally define debug constant ---
define( 'THEMEDEBUG', $themedebug );
// 2.2.0: run new standalone checkfunction
bioship_release_type_checker();
}
// ----------------
// Script Debugging
// ----------------
// 2.0.5: use development scripts/styles if theme debugging in admin area
// 2.2.0: moved here for debugging scripts on frontend also
if ( THEMEDEBUG && !defined( 'SCRIPT_DEBUG' ) ) {
define( 'SCRIPT_DEBUG', true );
}
// -------------------
// Set Debug Directory
// -------------------
// 1.9.8: set debug directory global value
// 2.1.1: use existing function for this purpose
// if (THEMECHILD) {global $vthemestyledir; $vthemedebugdir = $vthemestyledir.'debug';}
// else {global $vthemetemplatedir; $vthemedebugdir = $vthemetemplatedir.'debug';}
// $vthemedebugdir = bioship_apply_filters('theme_debug_dirpath', $vthemedebugdir);
// if (!is_dir($vthemedebugdir)) {wp_mkdir_p($vthemedebugdir);}
$vthemedebugdir = bioship_check_create_debug_dir();
// -----------------
// Load Theme Tracer
// -----------------
// 1.8.0: moved here as was loaded too early to work, refined logic
if ( !defined( 'THEMETRACE' ) ) {
// --- check for tracer loading ---
// 1.8.5: added querystring option for high capability
$themetracer = false;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_REQUEST['themetrace'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$themetracer = trim( $_REQUEST['themetrace'] );
if ( current_user_can( 'manage_options' ) ) {
// 2.0.5: make any other value trigger a trace
// 2.2.0: ignore empty value also
if ( ( '' != $themetracer ) && ( '0' != $themetracer ) ) {
$themetracer = true;
}
}
}
// --- filter tracer loading ---
// 1.9.8: fix to filtered value here
$themetracer = bioship_apply_filters( 'theme_tracer', $themetracer );
}
// --- include theme tracer (tracer.php) ---
// 1.9.8: change fixed directory to admin dir global
$tracer = bioship_file_hierarchy( 'file', 'tracer.php', $vthemedirs['admin'] );
// 2.1.1: moved THEMETRACE definition here from tracer.php (must be below above line!)
if ( !defined( 'THEMETRACE' ) ) {
if ( $themetracer ) {
define( 'THEMETRACE', true );
} else {
define( 'THEMETRACE', false );
}
}
if ( $tracer ) {
include $tracer;
}
// --- maybe define dummy function ---
// (dummy function to avoid potential fatal errors in edge cases)
if ( THEMETRACE && !function_exists( 'bioship_trace' ) ) {
function bioship_trace( $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null ) {}
}
// ----------------------
// === Theme Settings ===
// ----------------------
// -----------------------------------------
// Convert Posted Customizer Preview Options
// -----------------------------------------
// 1.8.5: moved here to be available for both options frameworks
if ( !function_exists( 'bioship_customizer_convert_posted' ) ) {
function bioship_customizer_convert_posted( $postedvalues, $optionvalues ) {
if ( THEMETRACE ) {bioship_trace( 'F', __FUNCTION__, __FILE__, func_get_args() );}
global $vthemeoptions;
// 2.2.0: maybe load theme options
if ( !isset( $vthemeoptions ) || !is_array( $vthemeoptions ) ) {
if ( !function_exists( 'bioship_options' ) ) {
$options = bioship_file_hierarchy( 'file', 'options.php' );
include_once $options;
}
$vthemeoptions = bioship_options();
}
foreach ( $vthemeoptions as $optionkey => $optionvalue ) {
// 2.1.1: fix to skip non-options (headings etc)
if ( isset( $optionvalue['id'] ) ) {
// --- set variable for ID and type ---
$id = $optionvalue['id'];
$optiontype = $optionvalue['type'];
$optionkey = THEMEKEY . '[' . $optionvalue['id'] . ']';
$previewkey = str_replace( '_options', '_customize', $optionkey );
if ( array_key_exists( $previewkey, $postedvalues ) ) {
// --- apply sanitization filters to preview values ---
// 2.1.1.: removed unneeded inline {} wrappers
// note: the third argument above could be a Customizer setting object ?
$previewvalue = bioship_apply_filters( 'customize_sanitize_' . $previewkey, $postedvalues[$previewkey], array() );
// !! WARNING: echoing debug output in Customizer prevents saving !!
// (bioship_debug lines are commented out below just in case)
// --- set option value ---
if ( 'checkbox' == $optionvalue['type'] ) {
// 1.8.5: fix for empty checkbox values
if ( '1' == $previewvalue ) {
$optionvalues[$id] = '1';
} else {
$optionvalues[$id] = '0';
}
} else {
if ( !is_array( $previewvalue ) ) {
// --- set string value ---
$optionvalues[$id] = $previewvalue;
// bioship_debug( "Preview Value for '" . $id . "'", $previewvalue, false );
} else {
// TODO: maybe do something else for subarray values ?
// bioship_debug( "Option Type", $optiontype, false );
// --- Customizer Multicheck values ---
// 1.8.5: fix for customizer multicheck arrays
if ( 'multicheck' == $optiontype ) {
foreach ( $optionvalue['options'] as $key => $value ) {
if ( in_array( $key, $previewvalue ) ) {
$valuearray[$key] = '1';
} else {
$valuearray[$key] = '0';
}
}
$optionvalues[$id] = $valuearray;
} else {
// --- set array value ---
$optionvalues[$id] = $previewvalue;
}
// bioship_debug( "Preview Value for '" . $id . "'", $optionvalues[$id], false );
}
}
}
}
}
return $optionvalues;
}
}
// -------------------------------------------
// Get Theme Settings Filter - with Fallbacks!
// -------------------------------------------
// ...this may seem completely arbitrary and unnecessary but it works...
// 1.9.5: get_option filter to help bypass crazy empty settings / saving bug
// 2.0.9: added theme_settings filter to return value
if ( !function_exists( 'bioship_get_theme_settings' ) ) {
function bioship_get_theme_settings( $value, $optionkey = false ) {
if ( THEMETRACE ) {bioship_trace( 'F', __FUNCTION__, __FILE__ );}
global $vthemesettingsfix, $vthemedebugdir;
// --- not while updating settings ---
// 2.1.1: moved this check to update - not get option
// --- check/create debug directory ---
$vthemedebugdir = bioship_check_create_debug_dir();
// --- this is to bypass possible cached value ---
$settings = bioship_get_option( THEMEKEY );
// ---- set to theme key if needed ---
// 1.9.7: fix to missing argument 2 filter warning
if ( !$optionkey ) {
$optionkey = THEMEKEY;
}
// --- set found settings flag ---
// 2.1.1: added this for better conditional logic
$foundsettings = false;
// 2.2.0: check for forced restore from file
$forcerestore = get_stylesheet_directory() . '/debug/force_restore.txt';
if ( !file_exists( $forcerestore ) ) {
// --- unserialize settings check ---
if ( $settings ) {
if ( is_serialized( $settings ) ) {
bioship_debug( "Serialized Theme Settings Found" );
$settings = (string) $settings;
$unserialized = unserialize( $settings );
if ( $unserialized ) {
// 2.1.1: set as found instead of returning
bioship_debug( "Theme Settings Unserialized Successfully" );
$settings = $unserialized;
$foundsettings = true;
} else {
// and here is the problem (read "ghost insane WTF bug")... finally! yeesh.
// ?!sometimes!? the data JUST DOES NOT UNSERIALIZE - FOR NO CLEAR REASON.
// ...it just returns as false even though the serialized data is there
// and does not appear to be at all corrupt - and works in other contexts!
// --- attempt to fix serialized settings ---
// (but sometimes works sometimes not :-/ )
// $repaired = bioship_fix_serialized($settings);
// $fixedsettings = unserialize($repaired);
// if ($fixedsettings) {return $fixedsettings;}
// --- add theme debug warning ---
if ( THEMEDEBUG ) {
bioship_debug( "WARNING: Unserialization Failed for Option Key", $optionkey );
bioship_debug( "Settings", $settings );
bioship_debug( "Maybe Unserialized", maybe_unserialize( $settings ) );
bioship_debug( "Unserialized", $unserialized );
}
// ---- for now, add a filter so users can apply a custom manual fix ---
// TODO: somehow add an admin warning for when this happens ?
$customsettings = bioship_apply_filters( 'theme_settings_fallback', $settings );
if ( is_serialized( $customsettings ) ) {
$unserialized = @unserialize( $customsettings );
if ( $unserialized ) {
bioship_debug( "Unserialized Settings from Custom Override Used" );
$settings = $unserialized;
$foundsettings = true;
}
}
}
}
}
// --- maybe apply force update settings ---
if ( !$foundsettings ) {
$forcesettings = get_transient( 'force_update_' . $optionkey );
if ( $forcesettings ) {
bioship_debug( "Checking Force Update Settings" );
if ( is_serialized( $forcesettings ) ) {
// --- unserialize force updated settings ---
$unserialized = @unserialize( $forcesettings );
if ( !$unserialized ) {
// --- attempt fix of serialized settings ---
$repaired = bioship_fix_serialized( $forcesettings );
$fixedsettings = @unserialize( $repaired );
if ( $fixedsettings ) {
bioship_debug( "Fixed Force Update Settings" );
$unserialized = $fixedsettings;
}
}
if ( $unserialized ) {
// --- write to file backup ---
// 2.1.1: use update_option instead of delete and add
// 2.1.1: remove filter to prevent infinite loop
bioship_write_debug_file( $optionkey . '.txt', $forcesettings );
remove_filter( 'pre_option_' . THEMEKEY, 'bioship_get_theme_settings', 10, 2 );
update_option( $optionkey, $unserialized );
add_filter( 'pre_option_' . THEMEKEY, 'bioship_get_theme_settings', 10, 2 );
// --- add notice for force update settings ---
bioship_debug( "Force Update Settings Used and Restored" );
// 2.0.0: change to skeleton prefix and add translation wrappers
if ( !function_exists( 'bioship_forced_settings_restored' ) ) {
// 2.1.1: move add_action internally for consistency
add_action( 'theme_admin_notices', 'bioship_forced_settings_restored' );
function bioship_forced_settings_restored() {
echo "<div class='message notice notice-warning'><b>" . esc_html( __( 'Warning', 'bioship' ) ) . ":</b> ";
echo esc_html( __( 'Theme Settings from Force Update Used and Restored!', 'bioship' ) ) . "</div>";
}
}
$settings = $unserialized;
$foundsettings = true;
} else {
bioship_debug( "Force Update Settings could not be Unserialized" );