-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplate.php
More file actions
1515 lines (1318 loc) · 46.9 KB
/
Template.php
File metadata and controls
1515 lines (1318 loc) · 46.9 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
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
/**
* Handles all functionality for templates.
*
* @since 1.0.0
*
* @package StellarWP\Templates
*/
namespace StellarWP\Templates;
use InvalidArgumentException;
use StellarWP\Arrays\Arr;
use StellarWP\Templates\Utils\Conditions;
use StellarWP\Templates\Utils\Paths;
use StellarWP\Templates\Utils\Strings;
/**
* Handles all functionality for templates.
*
* @since 1.0.0
*
* @package StellarWP\Templates
*/
class Template {
/**
* The origin class for the plugin where the template lives
*
* @since 1.0.0
*
* @var object
*/
public $origin;
/**
* The folders into which we will look for the template.
*
* @since 1.0.0
*
* @var array
*/
protected array $folder = [];
/**
* The local context for templates, mutable on every self::template() call
*
* @since 1.0.0
*
* @var array
*/
protected array $context = [];
/**
* The global context for this instance of templates
*
* @since 1.0.0
*
* @var array
*/
protected array $global = [];
/**
* Used for finding templates for public templates on themes inside of a folder.
*
* @since 1.0.0
*
* @var string[]
*/
protected array $template_origin_base_folder = [ 'src', 'views' ];
/**
* Allow changing if class will extract data from the local context
*
* @since 1.0.0
*
* @var boolean
*/
protected bool $template_context_extract = false;
/**
* Current template hook name.
*
* @since 1.0.0
*
* @var string
*/
protected string $template_current_hook_name = '';
/**
* Base template for where to look for template
*
* @since 1.0.0
*
* @var array
*/
protected array $template_base_path = [];
/**
* Should we use a lookup into the list of folders to try to find the file
*
* @since 1.0.0
*
* @var bool
*/
protected bool $template_folder_lookup = false;
/**
* Create a class variable for the include path, to avoid conflicting with extract.
*
* @since 1.0.0
*
* @var string
*/
protected string $template_current_file_path = '';
/**
* A map of aliases to add a rewritten version of the paths to the template lists.
* The map has format `original => alias`.
*
* @since 1.0.0
*
* @var array<string,string>
*/
protected array $aliases = [];
/**
* Configures the class origin plugin path
*
* @since 1.0.0
*
* @param object|string $origin The base origin for the templates.
*
* @throws InvalidArgumentException When an origin class is not valid.
*
* @return static
*/
public function set_template_origin( $origin = null ): self {
if ( empty( $origin ) ) {
$origin = $this->origin;
}
if ( is_string( $origin ) ) {
// Origin needs to be a class with a `instance` method.
if ( class_exists( $origin ) && method_exists( $origin, 'instance' ) ) {
$origin = call_user_func( [ $origin, 'instance' ] ); // @phpstan-ignore-line If we get here, we know the object is an object.
}
}
if (
empty( $origin->plugin_path )
&& empty( $origin->pluginPath ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
&& ! is_dir( $origin )
) {
throw new InvalidArgumentException( 'Invalid Origin Class for Template Instance' );
}
if ( is_string( $origin ) ) {
$this->template_base_path = array_filter(
(array) explode(
'/',
untrailingslashit( $origin )
)
);
} else {
$this->origin = $origin;
$this->template_base_path = [ untrailingslashit( Config::get_path() ) ];
}
return $this;
}
/**
* Configures the class with the base folder in relation to the Origin
*
* @since 1.0.0
*
* @param array|string $folder Which folder we are going to look for templates in.
*
* @return self
*/
public function set_template_folder( $folder = null ): self {
// Allows configuring a already set class.
if ( ! isset( $folder ) ) {
$folder = $this->folder;
}
// If Folder is String make it an Array.
if ( is_string( $folder ) ) {
$folder = explode( '/', $folder );
}
// Cast as Array and save.
$this->folder = Arr::wrap( $folder );
return $this;
}
/**
* Returns the array for which folder this template instance is looking into.
*
* @since 1.0.0
*
* @return array Current folder we are looking for templates.
*/
public function get_template_folder() {
return $this->folder;
}
/**
* Configures the class with the base folder in relation to the Origin
*
* @since 1.0.0
*
* @param mixed $value Should we look for template files in the list of folders.
*
* @return self
*/
public function set_template_folder_lookup( $value = true ): self {
$this->template_folder_lookup = Conditions::is_truthy( $value );
return $this;
}
/**
* Gets in this instance of the template engine whether we are looking public folders like themes.
*
* @since 1.0.0
*
* @return bool Whether we are looking into theme folders.
*/
public function get_template_folder_lookup() {
return $this->template_folder_lookup;
}
/**
* Configures the class global context
*
* @since 1.0.0
*
* @param array $context The default global context.
*
* @return self
*/
public function add_template_globals( $context = [] ) {
// Cast as Array merge and save.
$this->global = wp_parse_args( Arr::wrap( $context ), $this->global );
return $this;
}
/**
* Configures if the class will extract context for template
*
* @since 1.0.0
*
* @param bool $value Should we extract context for templates.
*
* @return self
*/
public function set_template_context_extract( $value = false ) {
// Cast as bool and save.
$this->template_context_extract = Conditions::is_truthy( $value );
return $this;
}
/**
* Set the current hook name for the template include.
*
* @since 1.0.0
*
* @param string $value Which value will be saved as the current hook name.
*
* @return self Allow daisy-chaining.
*/
public function set_template_current_hook_name( $value ) {
$this->template_current_hook_name = (string) $value;
return $this;
}
/**
* Gets the hook name for the current template setup.
*
* @since 1.0.0
*
* @return string Hook name currently set on the class.
*/
public function get_template_current_hook_name() {
return $this->template_current_hook_name;
}
/**
* Sets an Index inside of the global or local context.
* Final to prevent extending the class when the `get` already exists on the child class.
*
* @see Arr::set()
*
* @since 1.0.0
*
* @param array|string $index Specify each nested index in order (e.g. [ 'lvl1', 'lvl2' ]).
* @param mixed $default Default value if the search finds nothing.
* @param boolean $is_local Use the Local or Global context.
*
* @return mixed The value of the specified index or the default if not found.
*/
final public function get( $index, $default = null, $is_local = true ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.defaultFound
$hook_prefix = Config::get_hook_prefix();
$context = $this->get_global_values();
if ( true === $is_local ) {
$context = $this->get_local_values();
}
/**
* Allows filtering the the getting of Context variables, also short circuiting
* Following the same structure as WP Core
*
* @since 1.0.0
*
* @param mixed $value The value that will be filtered.
* @param array|string $index Specify each nested index in order (e.g. [ 'lvl1', 'lvl2' ]).
* @param mixed $default Default value if the search finds nothing.
* @param boolean $is_local Use the Local or Global context.
* @param self $template Current instance of the self.
*/
$value = apply_filters( "stellarwp/templates/{$hook_prefix}/template_context_get", null, $index, $default, $is_local, $this );
if ( null !== $value ) {
return $value;
}
return Arr::get( $context, $index, $default );
}
/**
* Sets a Index inside of the global or local context
* Final to prevent extending the class when the `set` already exists on the child class
*
* @see Arr::set
*
* @since 1.0.0
*
* @param string|array $index To set a key nested multiple levels deep pass an array specifying each key in order as a value (e.g. [ 'lvl1', 'lvl2', 'lvl3' ]).
* @param mixed $value The value.
* @param boolean $is_local Use the Local or Global context.
*
* @return array Full array with the key set to the specified value.
*/
final public function set( $index, $value = null, $is_local = true ) {
if ( true === $is_local ) {
$this->context = Arr::set( $this->context, $index, $value );
return $this->context;
}
$this->global = Arr::set( $this->global, $index, $value );
return $this->global;
}
/**
* Merges local and global context, and saves it locally.
*
* @since 1.0.0
*
* @param array|null $context Local Context array of data.
* @param string $file Complete path to include the PHP File.
* @param array $name Template name.
*
* @return array
*/
public function merge_context( $context = [], $file = null, $name = null ) {
$hook_prefix = Config::get_hook_prefix();
// Allow for simple null usage as well as array() for nothing.
if ( is_null( $context ) ) {
$context = [];
}
// Applies new local context on top of Global + Previous local.
$context = wp_parse_args( Arr::wrap( $context ), $this->get_values() );
/**
* Allows filtering the Local context.
*
* @since 1.0.0
*
* @param array $context Local Context array of data.
* @param string $file Complete path to include the PHP File.
* @param array $name Template name.
* @param self $template Current instance of the self.
*/
$this->context = apply_filters( "stellarwp/templates/{$hook_prefix}/template_context", $context, $file, $name, $this );
$hook_name = $this->get_template_current_hook_name();
/**
* Allows filtering the Local context specifically to the template with the hook name passed to the method.
*
* @since 1.0.0
*
* @param array $context Local Context array of data.
* @param string $file Complete path to include the PHP File.
* @param array $name Template name.
* @param self $template Current instance of the self.
*/
$this->context = apply_filters( "stellarwp/templates/{$hook_prefix}/template_context:{$hook_name}", $this->context, $file, $name, $this );
return $this->context;
}
/**
* Fetches which base folder we look for templates in the origin plugin.
*
* @since 1.0.0
*
* @return array The base folders we look for templates in the origin plugin.
*/
public function get_template_origin_base_folder() {
$hook_prefix = Config::get_hook_prefix();
/**
* Allows filtering of the base path for templates.
*
* @since 1.0.0
*
* @param array $namespace Which is the base folder we will look for files in the plugin.
* @param self $template Current instance of the self.
*/
return apply_filters( "stellarwp/templates/{$hook_prefix}/template_origin_base_folder", $this->template_origin_base_folder, $this );
}
/**
* Tries to locate the correct file we want to load based on the Template class
* configuration and it's list of folders
*
* @since 1.0.0
*
* @param mixed $name File name we are looking for.
*
* @return string|false
*/
public function get_template_file( $name ) {
$hook_prefix = Config::get_hook_prefix();
// If name is String make it an Array.
if ( is_string( $name ) ) {
$name = (array) explode( '/', $name );
}
$folders = $this->get_template_path_list();
$found_file = false;
$namespace = false;
foreach ( $folders as $folder ) {
if ( empty( $folder['path'] ) ) {
continue;
}
// Build the File Path.
$file = Paths::merge( $folder['path'], $name );
// Append the Extension to the file path.
$file .= '.php';
// Skip non-existent files.
if ( file_exists( $file ) ) {
$found_file = $file;
$namespace = ! empty( $folder['namespace'] ) ? $folder['namespace'] : false;
break;
}
}
if ( $this->get_template_folder_lookup() ) {
$theme_folders = $this->get_template_theme_path_list( $namespace );
foreach ( $theme_folders as $folder ) {
if ( empty( $folder['path'] ) ) {
continue;
}
// Build the File Path.
$file = implode( DIRECTORY_SEPARATOR, array_merge( Arr::wrap( $folder['path'] ), $name ) );
// Append the Extension to the file path.
$file .= '.php';
// Skip non-existent files.
if ( file_exists( $file ) ) {
$found_file = $file;
break;
}
}
}
if ( $found_file ) {
/**
* A more Specific Filter that will include the template name
*
* @since 1.0.0
* @since 1.0.0
*
* @param string $file Complete path to include the PHP File
* @param array $name Template name
* @param self $template Current instance of the self.
*/
return apply_filters( "stellarwp/templates/{$hook_prefix}/template_file", $found_file, $name, $this );
}
// Couldn't find a template on the Stack.
return false;
}
/**
* Runs the entry point hooks and filters.
*
* @param string $entry_point_name The name of the entry point.
* @param boolean $echo If we should also print the entry point content.
*
* @return null|string `null` if an entry point is disabled or the entry point HTML.
*/
public function do_entry_point( $entry_point_name, $echo = true ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.echoFound
$hook_prefix = Config::get_hook_prefix();
$hook_name = $this->get_template_current_hook_name();
/**
* Filter if the entry points are enabled.
*
* @since 1.0.0
*
* @param boolean $is_enabled Is entry_point enabled.
* @param string $hook_name For which template include this entry point belongs.
* @param string $entry_point_name Which entry point specifically we are triggering.
* @param self $template Current instance of the template class doing this entry point.
*/
$is_entry_point_enabled = apply_filters( "stellarwp/templates/{$hook_prefix}/template_entry_point_is_enabled", true, $hook_name, $entry_point_name, $this );
if ( ! $is_entry_point_enabled ) {
return null;
}
ob_start();
if ( has_action( "stellarwp/templates/{$hook_prefix}/template_entry_point:{$hook_name}" ) ) {
/**
* Generic entry point action for the current template.
*
* @since 1.0.0
*
* @param string $hook_name For which template include this entry point belongs.
* @param string $entry_point_name Which entry point specifically we are triggering.
* @param self $template Current instance of the template class doing this entry point.
*/
do_action( "stellarwp/templates/{$hook_prefix}/template_entry_point:{$hook_name}", $hook_name, $entry_point_name, $this );
}
if ( has_action( "stellarwp/templates/{$hook_prefix}/template_entry_point:{$hook_name}:{$entry_point_name}" ) ) {
/**
* Specific named entry point action called.
*
* @since 1.0.0
*
* @param string $hook_name For which template include this entry point belongs.
* @param string $entry_point_name Which entry point specifically we are triggering.
* @param self $template Current instance of the template class doing this entry point.
*/
do_action( "stellarwp/templates/{$hook_prefix}/template_entry_point:{$hook_name}:{$entry_point_name}", $hook_name, $entry_point_name, $this );
}
$html = ob_get_clean();
if ( has_filter( "stellarwp/templates/{$hook_prefix}/template_entry_point_html:{$hook_name}" ) ) {
/**
* Generic entry point action for the current template.
*
* @since 1.0.0
*
* @param string $html HTML returned and/or echoed for this for this entry point.
* @param string $hook_name For which template include this entry point belongs.
* @param string $entry_point_name Which entry point specifically we are triggering.
* @param self $template Current instance of the template class doing this entry point.
*/
$html = apply_filters( "stellarwp/templates/{$hook_prefix}/template_entry_point_html:{$hook_name}", $html, $hook_name, $entry_point_name, $this );
}
if ( has_filter( "stellarwp/templates/{$hook_prefix}/template_entry_point_html:{$hook_name}:{$entry_point_name}" ) ) {
/**
* Specific named entry point action called.
*
* @since 1.0.0
*
* @param string $html HTML returned and/or echoed for this for this entry point.
* @param string $hook_name For which template include this entry point belongs.
* @param string $entry_point_name Which entry point specifically we are triggering.
* @param self $template Current instance of the template class doing this entry point.
*/
$html = apply_filters( "stellarwp/templates/{$hook_prefix}/template_entry_point_html:{$hook_name}:{$entry_point_name}", $html, $hook_name, $entry_point_name, $this );
}
if ( $echo ) {
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped,StellarWP.XSS.EscapeOutput.OutputNotEscaped
}
return $html;
}
/**
* A very simple method to include a Template, allowing filtering and additions using hooks.
*
* @since 1.0.0
*
* @param string|array $name Which file we are talking about including.
* If an array, each item will add a directory separator to get to the single template.
* @param array $context Any context data you need to expose to this file.
* @param boolean $echo If we should also print the Template.
*
* @return string|false Either the final content HTML or `false` if no template could be found.
*/
public function template( $name, $context = [], $echo = true ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.echoFound,Generic.NamingConventions.ConstructorName.OldStyle
static $file_exists = [];
static $files = [];
static $template_names = [];
$hook_prefix = Config::get_hook_prefix();
/**
* Allow users to disable templates before rendering it by returning empty string.
*
* @since 1.0.0
*
* @param string|null $content Whether to continue displaying the template or not.
* @param array $name Template name.
* @param array $context Any context data you need to expose to this file.
* @param boolean $echo If we should also print the Template.
*/
$done = apply_filters( "stellarwp/templates/{$hook_prefix}/template_done", null, $name, $context, $echo );
if ( null !== $done ) {
return false;
}
// Key we'll use for in-memory caching of expensive operations.
$cache_name_key = is_array( $name ) ? implode( '/', $name ) : $name;
// Cache template name massaging so we don't have to repeat these actions.
if ( ! isset( $template_names[ $cache_name_key ] ) ) {
// If name is String make it an Array.
if ( is_string( $name ) ) {
$name = (array) explode( '/', $name );
}
// Clean this Variable.
$name = array_map( 'sanitize_title_with_dashes', $name );
$template_names[ $cache_name_key ] = $name;
}
// Cache file location and existence.
if (
! isset( $file_exists[ $cache_name_key ] )
|| ! isset( $files[ $cache_name_key ] )
) {
// Check if the file exists.
$file = $this->get_template_file( $name );
$files[ $cache_name_key ] = $this->get_template_file( $name );
// Check if it's a valid variable.
if ( ! $file ) {
$file_exists[ $cache_name_key ] = false;
return $file_exists[ $cache_name_key ];
}
// Before we load the file we check if it exists.
if ( ! file_exists( $file ) ) {
$file_exists[ $cache_name_key ] = false;
return $file_exists[ $cache_name_key ];
}
$file_exists[ $cache_name_key ] = true;
}
// If the file doesn't exist, bail.
if ( ! $file_exists[ $cache_name_key ] ) {
return false;
}
// Use filename stored in cache.
$file = $files[ $cache_name_key ];
$name = $template_names[ $cache_name_key ];
$origin_folder_appendix = array_diff( $this->folder, $this->template_origin_base_folder );
$origin_namespace = $this->template_get_origin_namespace( $file );
if ( $origin_namespace ) {
$legacy_namespace = array_merge( Arr::wrap( $origin_namespace ), $name );
$namespace = array_merge( Arr::wrap( $origin_namespace ), $origin_folder_appendix, $name );
} else {
$legacy_namespace = $name;
$namespace = array_merge( $origin_folder_appendix, $legacy_namespace );
}
// Setup the Hook name.
$legacy_hook_name = implode( '/', $legacy_namespace );
$hook_name = implode( '/', $namespace );
$prev_hook_name = $this->get_template_current_hook_name();
// Store the current hook name for the purposes of entry-points.
$this->set_template_current_hook_name( $hook_name );
/**
* Allow users to filter the HTML before rendering
*
* @since 1.0.0
*
* @param string|null $html The initial HTML
* @param string $file Complete path to include the PHP File
* @param array $name Template name
* @param self $template Current instance of the self.
*/
$pre_html = apply_filters( "stellarwp/templates/{$hook_prefix}/template_pre_html", null, $file, $name, $this );
/**
* Allow users to filter the HTML by the name before rendering
*
* E.g.:
* `stellarwp/templates/{$hook_prefix}/template_pre_html:events/blocks/parts/details`
* `stellarwp/templates/{$hook_prefix}/template_pre_html:events/embed`
* `stellarwp/templates/{$hook_prefix}/template_pre_html:tickets/login-to-purchase`
*
* @since 1.0.0
*
* @param string|null $html The initial HTML
* @param string $file Complete path to include the PHP File
* @param array $name Template name
* @param self $template Current instance of the self.
*/
$pre_html = apply_filters( "stellarwp/templates/{$hook_prefix}template_pre_html:{$hook_name}", $pre_html, $file, $name, $this );
if ( null !== $pre_html ) {
return $pre_html;
}
// Merges the local data passed to template to the global scope.
$this->merge_context( $context, $file, $name );
$before_include_html = $this->actions_before_template( $file, $name, $hook_name );
$before_include_html = $this->filter_template_before_include_html( $before_include_html, $file, $name, $hook_name );
$include_html = $this->template_safe_include( $file );
$include_html = $this->filter_template_include_html( $include_html, $file, $name, $hook_name );
$after_include_html = $this->actions_after_template( $file, $name, $hook_name );
$after_include_html = $this->filter_template_after_include_html( $after_include_html, $file, $name, $hook_name );
// Only fetch the contents after the action.
$html = $before_include_html . $include_html . $after_include_html;
$html = $this->filter_template_html( $html, $file, $name, $hook_name );
// Tries to hook container entry points in the HTML.
$html = $this->template_hook_container_entry_points( $html );
if ( $echo ) {
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped,StellarWP.XSS.EscapeOutput.OutputNotEscaped
}
// Revert the current hook name.
$this->set_template_current_hook_name( $prev_hook_name );
return $html;
}
/**
* Based on a path it determines what is the namespace that should be used.
*
* @since 1.0.0
*
* @param string $path Which file we are going to load.
*
* @return string|false The found namespace for that path or false.
*/
public function template_get_origin_namespace( $path ) {
$hook_prefix = Config::get_hook_prefix();
$matching_namespace = false;
/**
* Allows more namespaces to be added based on the path of the file we are loading.
*
* @since 1.0.0
*
* @param array $namespace_map Indexed array containing the namespace as the key and path to `strpos`.
* @param string $path Path we will do the `strpos` to validate a given namespace.
* @param self $template Current instance of the template class.
*/
$namespace_map = (array) apply_filters( "stellarwp/templates/{$hook_prefix}/template_origin_namespace_map", [], $path, $this );
foreach ( $namespace_map as $namespace => $contains_string ) {
// Normalize the trailing slash to the current OS directory separator.
$contains_string = rtrim( $contains_string, '\\/' ) . DIRECTORY_SEPARATOR;
// Skip when we don't have the namespace path.
if ( false === strpos( $path, $contains_string ) ) {
continue;
}
$matching_namespace = $namespace;
// Once the first namespace is found it breaks out.
break;
}
if ( empty( $matching_namespace ) && ! empty( $this->origin->template_namespace ) ) {
$matching_namespace = $this->origin->template_namespace;
}
return $matching_namespace;
}
/**
* Includes a give PHP inside of a safe context.
*
* This method is required to prevent template files messing with local variables used inside of the
* `self::template` method. Also shelters the template loading from any possible variables that could
* be overwritten by the context.
*
* @since 1.0.0
*
* @param string $file Which file will be included with safe context.
*
* @return string Contents of the included file.
*/
public function template_safe_include( $file ) {
ob_start();
// We use this instance variable to prevent collisions.
$this->template_current_file_path = $file;
unset( $file );
// Only do this if really needed (by default it won't).
if ( true === $this->template_context_extract && ! empty( $this->context ) ) {
// Make any provided variables available in the template variable scope.
extract( $this->context ); // @phpcs:ignore
}
include $this->template_current_file_path;
// After the include we reset the variable.
unset( $this->template_current_file_path );
return ob_get_clean();
}
/**
* Sets a number of values at the same time.
*
* @see static::set()
* @since 1.0.0
*
* @param array $values An associative key/value array of the values to set.
* @param bool $is_local Whether to set the values as global or local (defaults to local as the `set` method does).
*
* @return void
*/
public function set_values( array $values = [], $is_local = true ) {
foreach ( $values as $key => $value ) {
$this->set( $key, $value, $is_local );
}
}
/**
* Returns the Template global context.
*
* @since 1.0.0
*
* @return array An associative key/value array of the Template global context.
*/
public function get_global_values() {
return $this->global;
}
/**
* Returns the Template local context.
*
* @since 1.0.0
*
* @return array An associative key/value array of the Template local context.
*/
public function get_local_values() {
return $this->context;
}
/**
* Returns the Template global and local context values.
*
* Local values will override the template global context values.
*
* @since 1.0.0
*
* @return array An associative key/value array of the Template global and local context.
*/
public function get_values() {
return array_merge( $this->get_global_values(), $this->get_local_values() );
}
/**
* Sets the aliases the template should use.
*
* @since 1.0.0
*
* @param array<string,string> $aliases A map of aliases that should be used to add lookup locations, in the format [ original => alias ].
*
* @return static This instance, for method chaining.
*/
public function set_aliases( array $aliases = [] ) {
$this->aliases = $aliases;
return $this;
}
/**
* Fetches the path for locating files in the Plugin Folder
*
* @since 1.0.0
*
* @return string
*/
protected function get_template_plugin_path() {
$hook_prefix = Config::get_hook_prefix();
// Craft the plugin Path.
$path = array_merge( $this->template_base_path, $this->folder );
// Implode to avoid Window Problems.
$path = implode( DIRECTORY_SEPARATOR, $path );
/**
* Allows filtering of the base path for templates
*
* @since 1.0.0
*
* @param string $path Complete path to include the base plugin folder
* @param self $template Current instance of the self.
*/
return apply_filters( "stellarwp/templates/{$hook_prefix}/template_plugin_path", $path, $this );
}
/**
* Fetches the Namespace for the public paths, normally folders to look for
* in the theme's directory.
*
* @since 1.0.0
* @since 1.0.0
*
* @param string $plugin_namespace Overwrite the origin namespace with a given one.
*
* @return array Namespace where we to look for templates.
*/
protected function get_template_public_namespace( $plugin_namespace ) {
$hook_prefix = Config::get_hook_prefix();
$namespace = [
$hook_prefix,
];
if ( ! empty( $plugin_namespace ) ) {
$namespace[] = $plugin_namespace;
} elseif ( ! empty( $this->origin->template_namespace ) ) {
$namespace[] = $this->origin->template_namespace;
}
/**
* Allows filtering of the base path for templates
*
* @since 1.0.0
*
* @param array $namespace Which is the namespace we will look for files in the theme
* @param self $template Current instance of the self.
*/
return apply_filters( "stellarwp/templates/{$hook_prefix}/template_public_namespace", $namespace, $this );
}
/**
* Fetches the path for locating files given a base folder normally theme related.
*
* @since 1.0.0
* @since 1.0.0
*
* @param mixed $base Base path to look into.
* @param string $namespace Adds the plugin namespace to the path returned.
*
* @return string The public path for a given base.˙˙
*/
protected function get_template_public_path( $base, $namespace ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.namespaceFound
$hook_prefix = Config::get_hook_prefix();
// Craft the plugin Path.
$path = array_merge( Arr::wrap( $base ), $this->get_template_public_namespace( $namespace ) );
// Pick up if the folder needs to be added to the public template path.
$folder = array_diff( $this->folder, $this->get_template_origin_base_folder() );
if ( ! empty( $folder ) ) {