-
Notifications
You must be signed in to change notification settings - Fork 22
/
dustpress.php
2484 lines (2053 loc) · 85.6 KB
/
dustpress.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
/*
Plugin Name: DustPress
Plugin URI: http://www.geniem.com
Description: Dust.js templating system for WordPress
Author: Miika Arponen & Ville Siltala / Geniem Oy
Author URI: http://www.geniem.com
License: GPLv3
Version: 1.36.5
*/
final class DustPress {
// Singleton DustPress instance
private static $instance;
// Instance of DustPHP
public $dust;
// Main model
private $model;
// This is where the data will be stored
private $data;
// DustPress settings
private $settings;
// Is DustPress disabled?
public $disabled;
// Paths for locating files
private $paths;
// Paths for template files
private $templates;
// Are we on an activation page
private $activate;
// Registered custom ajax functions
private $ajax_functions;
// Custom routes
private $custom_routes = [];
private $request_data;
private $autoload_paths;
/**
* Should performance metrics be collected? Defaults to false.
* Using DustPress Debugger (while not using WP_CLI) sets this true.
*
* @var bool
*/
public $performance_enabled = false;
// Runtime performance is stored here.
// DustPress Debugger will read it (via $this->get_performance_data) after the page has been rendered.
private $performance = [];
// Multiple microtime(true) values stored here for execution time calculations.
private $performance_timers = [];
private $dustpress_performance_timers = [];
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new DustPress();
}
return self::$instance;
}
/**
* Constructor for DustPress core class.
*
* @type function
* @date 10/8/2015
* @since 0.2.0
*/
protected function __construct() {
$self = $this;
$self->maybe_enable_performance_metrics();
$self->save_performance( 'Before DustPress', $_SERVER['REQUEST_TIME_FLOAT'] );
$performance_measure_id = $this->start_dustpress_performance( __FUNCTION__ );
$self->measure_hooks_performance();
// Autoload paths will be stored here so the filesystem has to be scanned only once.
$this->autoload_paths = [];
$this->register_autoloaders();
// Create a DustPHP instance
$this->dust = new Dust\Dust();
// Dust template paths will be stored here so the filesystem has to be scanned only once.
$this->templates = [];
$this->add_theme_paths();
$this->add_core_paths();
// Add the fetched paths to the Dust instance.
$this->dust->includedDirectories = $this->get_template_paths( 'partials' );
// Find and include Dust helpers and filters from DustPress plugin
$paths = [
__DIR__ . '/helpers',
__DIR__ . '/filters',
];
foreach( $paths as $path ) {
if ( is_readable( $path ) ) {
foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path, RecursiveDirectoryIterator::SKIP_DOTS ) ) as $file ) {
if ( is_readable( $file ) && '.php' === substr( $file, -4, 4 ) ) {
require_once( $file );
}
}
}
}
// Add create_instance to right action hook if we are not on the admin side
if ( $this->want_autoload() ) {
add_filter( 'template_include', [ $this, 'create_instance' ] );
// If we are on wp-activate.php hook into activate_header
if ( strpos( $_SERVER['REQUEST_URI'], 'wp-activate.php' ) !== false ) {
$this->activate = true;
// Run create_instance for use partial and model
add_action( 'activate_header', [ $this, 'create_instance' ] );
// Kill original wp-activate.php execution
add_action( 'activate_header', function() {
die();
});
}
}
else if ( $this->is_dustpress_ajax() ) {
$this->parse_request_data();
// Optimize the run if we don't want to run the main WP_Query at all.
if ( ( is_object( $this->request_data ) && ! empty( $this->request_data->bypassMainQuery ) ) ||
( is_array( $this->request_data ) && ! empty( $this->request_data['bypassMainQuery'] ) ) ) {
// DustPress.js request is never 404.
add_filter( 'status_header', function( $status, $header ) {
return 'status: 200';
}, 10, 2 );
// Make the main query to be as fast as possible within DustPress.js requests.
add_filter( 'posts_request', function( $request, \WP_Query $query ) {
// Target main home query
if ( $query->is_main_query() ) {
$request = str_replace( '1=1', '0=1', $request );
}
return $request;
}, PHP_INT_MAX, 2 );
// Populate the main query posts variable with a dummy post to prevent errors and notices.
add_filter( 'posts_pre_query', function( $posts, \WP_Query $query ) {
if( $query->is_main_query() ){
$posts = [ new WP_Post( (object) [] ) ];
$query->found_posts = 1;
}
return $posts;
}, 10, 2 );
}
add_filter( 'template_include', [ $this, 'create_ajax_instance' ] );
}
// Initialize settings
add_action( 'init', [ $this, 'init_settings' ] );
// Register custom route rewrite tag
add_action( 'after_setup_theme', [ $this, 'rewrite_tags' ], 20 );
$this->save_dustpress_performance( $performance_measure_id );
}
/**
* Should performance tracking be enabled.
*
* @return bool
*/
public function maybe_enable_performance_metrics() {
if ( defined( 'WP_CLI' ) ) {
$this->performance_enabled = false;
return false;
}
// DustPress has probably been initialized before WP_IMPORTING
// has been defined. This might make this check useless, but
// the default is already false, so it shouldn't matter.
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
$this->performance_enabled = false;
return false;
}
if ( $this->debugger_is_active() ) {
$this->performance_enabled = true;
return true;
}
return false;
}
/**
* Register custom route rewrite tags
*
* @type function
* @date 19/3/2019
* @since 1.13.1
*/
public function rewrite_tags() {
$performance_measure_id = $this->start_dustpress_performance( __FUNCTION__ );
// Register custom route rewrite tag
add_rewrite_tag( '%dustpress_custom_route%', '([^\/]+)' );
add_rewrite_tag( '%dustpress_custom_route_route%', '(.+)' );
add_rewrite_tag( '%dustpress_custom_route_parameters%', '(.+)' );
add_rewrite_tag( '%dustpress_custom_route_render%', '(.+)' );
$this->save_dustpress_performance( $performance_measure_id );
}
/**
* This function creates the instance of the main model that is defined by the WordPress template
*
* @type function
* @date 19/3/2015
* @since 0.0.1
*/
public function create_instance() {
$performance_measure_id = $this->start_dustpress_performance( __FUNCTION__ );
global $post, $wp_query;
// Initialize an array for debugging.
$debugs = [];
if ( ! $this->activate ) {
if ( is_object( $post ) && isset( $post->ID ) ) {
$post_id = $post->ID;
}
else {
$post_id = null;
}
// Filter for wanted post ID
$new_post = apply_filters( 'dustpress/router', $post_id );
// If developer wanted a post by post ID.
if ( $new_post !== $post_id ) {
$post = get_post( $new_post );
setup_postdata( $post );
}
// Get current template name tidied up a bit.
$template = $this->get_template_filename( $debugs );
}
else {
// Use user-activate.php and user-activate.dust to replace wp-activate.php
$template = apply_filters( 'dustpress/template/useractivate', 'UserActivate' );
$debugs[] = $template;
// Prevent 404 on multisite sub pages.
$wp_query->is_404 = false;
}
$custom_route_args = [];
$custom_route = $wp_query->get( 'dustpress_custom_route' );
$custom_route_parameters = $wp_query->get( 'dustpress_custom_route_parameters' );
// Handle registered DustPress custom routes
if ( ! empty( $custom_route ) ) {
$template = $custom_route;
$custom_route_args[ 'route' ] = $wp_query->get( 'dustpress_custom_route_route' );
$custom_route_args[ 'render' ] = $wp_query->get( 'dustpress_custom_route_render' );
if ( ! empty( $custom_route_parameters ) ) {
$custom_route_args['params'] = explode( DIRECTORY_SEPARATOR, $custom_route_parameters );
}
$type = $custom_route_args['render'] ?: 'default';
}
$template = apply_filters( 'dustpress/template', $template, $custom_route_args );
if ( ! defined( 'DOING_AJAX' ) && ! $this->disabled ) {
// If class exists with the template's name, create new instance with it.
// We do not throw error if the class does not exist, to ensure that you can still create
// templates in traditional style if needed.
if ( class_exists ( $template ) ) {
$this->start_performance( 'Models total' );
$this->model = new $template( $custom_route_args );
$this->model->fetch_data();
$this->save_performance( 'Models total' );
do_action( 'dustpress/model_list', array_keys( (array) $this->model->get_submodels() ) );
$this->start_performance( 'Templates total' );
$template_override = $this->model->get_template();
$this->save_performance( 'Templates total' );
$partial = $template_override
? $template_override
: strtolower( $this->camelcase_to_dashed( $template ) );
$this->render([
'partial' => $partial,
'main' => true,
'type' => $type ?? 'default'
]);
}
else {
http_response_code(500);
die( 'DustPress error: No suitable model found. One of these is required: '. implode( ', ', $debugs ) );
}
}
}
/**
* This function returns the model name of current custom route, or false if we are not on a custom route.
*
* @type function
* @date 8/1/2019
* @since 1.20.0
*
* @return string|boolean
*/
public function get_custom_route() {
$performance_measure_id = $this->start_dustpress_performance( __FUNCTION__ );
global $wp_query;
$custom_route = $wp_query->get( 'dustpress_custom_route' );
if ( ! empty( $custom_route ) ) {
if ( isset( $this->custom_routes[ $custom_route ] ) ) {
return [
'template' => $custom_route,
'route' => $this->custom_routes[ $custom_route ],
];
}
}
$this->save_dustpress_performance( $performance_measure_id );
return false;
}
/**
* This function gets current template's filename and returns without extension or WP-template prefixes such as page- or single-.
*
* @type function
* @date 19/3/2015
* @since 0.0.1
*
* @return string
*/
private function get_template_filename( &$debugs = array() ) {
$performance_measure_id = $this->start_dustpress_performance( __FUNCTION__ );
global $post;
if ( is_object( $post ) && isset( $post->ID ) ) {
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
if ( $page_template ) {
$array = explode( DIRECTORY_SEPARATOR, $page_template );
$template = array_pop( $array );
// strip out .php
$template = str_replace( '.php', '', $template );
// strip out page-, single-
$template = str_replace( 'page-', '', $template );
$template = str_replace( 'single-', '', $template );
if ( $template == 'default' ) $template = 'page';
}
else {
$template = 'default';
}
}
else {
$template = 'default';
}
$hierarchy = [];
if ( is_front_page() ) {
$hierarchy[ 'is_front_page' ] = [
'FrontPage'
];
}
if ( is_search() ) {
$hierarchy[ 'is_search' ] = [
'Search'
];
}
if ( is_home() ) {
$hierarchy['is_home'] = [
'Home'
];
}
if ( is_page() ) {
$hierarchy[ 'is_page' ] = [
'Page' . $this->dashed_to_camelcase( $template, '_' ),
'Page' . $this->dashed_to_camelcase( $template ),
$this->dashed_to_camelcase( $template ),
'Page' . $this->dashed_to_camelcase( $post->post_name, '_' ),
'Page' . $this->dashed_to_camelcase( $post->post_name ),
'Page' . $post->ID,
'Page'
];
}
if ( is_category() ) {
$cat = get_category( get_query_var( 'cat' ) );
$hierarchy[ 'is_category' ] = [
'Category' . $this->dashed_to_camelcase( $cat->slug, '_' ),
'Category' . $this->dashed_to_camelcase( $cat->slug ),
'Category' . $cat->term_id,
'Category',
'Archive'
];
}
if ( is_tag() ) {
$term_id = get_queried_object()->term_id;
$term = get_term_by( 'id', $term_id, 'post_tag' );
$hierarchy[ 'is_tag' ] = [
'Tag' . $this->dashed_to_camelcase( $term->slug. '_' ),
'Tag' . $this->dashed_to_camelcase( $term->slug ),
'Tag',
'Archive'
];
}
if ( is_tax() ) {
$term = get_queried_object();
$hierarchy[ 'is_tax' ] = [
'Taxonomy' . $this->dashed_to_camelcase( $term->taxonomy, '_' ) . $this->dashed_to_camelcase( $term->slug ),
'Taxonomy' . $this->dashed_to_camelcase( $term->taxonomy ) . $this->dashed_to_camelcase( $term->slug ),
'Taxonomy' . $this->dashed_to_camelcase( $term->taxonomy, '_' ),
'Taxonomy' . $this->dashed_to_camelcase( $term->taxonomy ),
'Taxonomy',
'Archive'
];
}
if ( is_author() ) {
$author = get_user_by( 'slug', get_query_var( 'author_name' ) );
$hierarchy[ 'is_author' ] = [
'Author' . $this->dashed_to_camelcase( $author->user_nicename, '_' ),
'Author' . $this->dashed_to_camelcase( $author->user_nicename ),
'Author' . $author->ID,
'Author',
'Archive'
];
}
$hierarchy[ 'is_404' ] = [
'Error404'
];
if ( is_attachment() ) {
$mime_type = get_post_mime_type( get_the_ID() );
$hierarchy[ 'is_attachment' ] = [
function() use ( $mime_type ) {
if ( preg_match( '/^image/', $mime_type ) && class_exists( 'Image' ) ) {
return 'Image';
}
else {
return false;
}
},
function() use ( $mime_type ) {
if ( preg_match( '/^video/', $mime_type ) && class_exists( 'Video' ) ) {
return 'Video';
}
else {
return false;
}
},
function() use ( $mime_type ) {
if ( preg_match( '/^application/', $mime_type ) && class_exists( 'Application' ) ) {
return 'Application';
}
else {
return false;
}
},
function() use ( $mime_type ) {
if ( $mime_type == 'text/plain' ) {
if ( class_exists( 'Text' ) ) {
return 'Text';
}
else if ( class_exists( 'Plain' ) ) {
return 'Plain';
}
else if ( class_exists( 'TextPlain' ) ) {
return 'TextPlain';
}
else {
return false;
}
}
else {
return false;
}
},
'Attachment',
'SingleAttachment',
'Single'
];
}
if ( is_single() ) {
$type = get_post_type();
$hierarchy[ 'is_single' ] = [
'Single' . $this->dashed_to_camelcase( $template, '_' ),
'Single' . $this->dashed_to_camelcase( $template ),
'Single' . $this->dashed_to_camelcase( $type, '_' ),
'Single' . $this->dashed_to_camelcase( $type ),
$this->dashed_to_camelcase( $template ),
'Single'
];
}
if ( is_archive() ) {
// Double check just to keep the function structure.
$hierarchy[ 'is_archive' ] = [
function() {
$post_types = get_post_types();
foreach ( $post_types as $type ) {
if ( is_post_type_archive( $type ) ) {
if ( class_exists( 'Archive' . $this->dashed_to_camelcase( $type ) ) ) {
return 'Archive' . $this->dashed_to_camelcase( $type );
}
else if ( class_exists( 'Archive' ) ) {
return 'Archive';
}
else {
return false;
}
}
}
return false;
}
];
}
if ( is_date() ) {
// Double check just to keep the function structure.
$hierarchy[ 'is_date' ] = [
function() {
if ( class_exists( 'Date' ) ) {
return 'Date';
}
else if ( class_exists( 'Archive' ) ) {
return 'Archive';
}
else {
return false;
}
}
];
}
// I don't think you really want to do this.
$hierarchy = apply_filters( 'dustpress/template_hierarchy', $hierarchy );
foreach( $hierarchy as $level => $keys ) {
if ( true === call_user_func ( $level ) ) {
foreach( $keys as $key => $value ) {
if ( is_integer( $key ) ) {
if ( is_string( $value ) ) {
$debugs[] = $value;
if ( class_exists( $value ) ) {
return $value;
}
}
else if ( is_callable( $value ) ) {
$value = call_user_func( $value );
$debugs[] = $value;
if( is_string( $value ) && class_exists( $value ) ) {
return $value;
}
}
}
else if ( is_string( $key ) ) {
if ( class_exists( $key ) ) {
if( is_string( $value ) ) {
$debugs[] = $value;
if ( class_exists( $value ) ) {
return $value;
}
}
else if ( is_callable( $value ) ) {
$debugs[] = $value;
return call_user_func( $value );
}
}
}
else if ( true === $key or is_callable( $key ) ) {
if ( true === call_user_func( $key ) ) {
if( is_string( $value ) ) {
$debugs[] = $value;
if ( class_exists( $value ) ) {
return $value;
}
}
else if ( is_callable( $value ) ) {
$debugs[] = $value;
return call_user_func( $value );
}
}
}
}
}
}
$debugs[] = 'Index';
$this->save_dustpress_performance( $performance_measure_id );
return 'Index';
}
/**
* This function populates the data collection with essential data
*
* @type function
* @date 17/3/2015
* @since 0.0.1
*/
private function populate_data_collection() {
$performance_measure_id = $this->start_dustpress_performance( __FUNCTION__ );
$wp_data = array();
// Insert Wordpress blog info data to collection
$infos = [
'name',
'description',
'wpurl',
'url',
'admin_email',
'charset',
'version',
'html_type',
'is_rtl',
'language',
'stylesheet_url',
'stylesheet_directory',
'template_url',
'template_directory',
'pingback_url',
'atom_url',
'rdf_url',
'rss_url',
'rss2_url',
'comments_atom_url',
'comments_rss2_url',
'url'
];
if ( $this->is_dustpress_ajax() ) {
$remove_infos = array( 'wpurl', 'admin_email', 'version', 'user' );
$remove_infos = apply_filters( 'dustpress/ajax/remove_wp', $remove_infos );
$infos = array_diff( $infos, $remove_infos );
}
foreach ( $infos as $info ) {
$wp_data[ $info ] = get_bloginfo( $info );
}
// Insert user info to collection
$currentuser = wp_get_current_user();
if ( 0 === $currentuser->ID ) {
$wp_data['loggedin'] = false;
}
else {
$wp_data['loggedin'] = true;
$wp_data['user'] = $currentuser->data;
$wp_data['user']->roles = $currentuser->roles;
unset( $wp_data['user']->user_pass );
}
// Insert WP title to collection
ob_start();
wp_title();
$wp_data['title'] = ob_get_clean();
// Insert admin ajax url
$wp_data['admin_ajax_url'] = admin_url( 'admin-ajax.php' );
// Insert current page permalink
$wp_data['permalink'] = get_permalink();
// Insert body classes
$wp_data['body_class'] = get_body_class();
// Return collection after filters
$apply_filters = apply_filters( 'dustpress/data/wp', $wp_data );
$this->save_dustpress_performance( $performance_measure_id );
return $apply_filters;
}
/**
* This function will render the given data in selected format
*
* @type function
* @date 17/3/2015
* @since 0.0.1
*
* @param array $args
* @return bool
*/
public function render( $args = array() ) {
$performance_measure_id = $this->start_dustpress_performance( __FUNCTION__ );
global $dustpress;
global $hash;
$defaults = [
'data' => false,
'type' => 'default',
'echo' => true,
'main' => false,
];
$options = array_merge( $defaults, (array) $args );
// FIXME -> WP function
extract( $options );
if ( 'default' == $type && ! get_option( 'dustpress_default_format' ) ) {
$type = 'html';
}
else if ( 'default' == $type && get_option( 'dustpress_default_format' ) ) {
$type = get_option( 'dustpress_default_format' );
}
if ( $this->get_setting( 'json_url' ) && isset( $_GET['json'] ) ) {
$type = 'json';
}
if ( $this->get_setting( 'json_headers' ) && isset( $_SERVER['HTTP_ACCEPT'] ) && $_SERVER['HTTP_ACCEPT'] == 'application/json' ) {
$type = 'json';
}
$types = array(
'html' => function( $data, $partial, $dust ) {
try {
if ( apply_filters( 'dustpress/cache/partials', false ) && apply_filters( 'dustpress/cache/partials/' . $partial, true ) ) {
if ( ! ( $compiled = wp_cache_get( $partial, 'dustpress/partials' ) ) ) {
$compiled = $dust->compileFile( $partial );
wp_cache_set( $partial, $compiled, 'dustpress/partials' );
}
}
else {
$compiled = $dust->compileFile( $partial );
}
}
catch ( Exception $e ) {
http_response_code(500);
die( 'DustPress error: '. $e->getMessage() );
}
if ( apply_filters( 'dustpress/cache/rendered', false ) && apply_filters( 'dustpress/cache/rendered/' . $partial, true ) ) {
$data_hash = sha1( serialize( $compiled ) . serialize( $data ) );
$cache_time = apply_filters( 'dustpress/settings/partial/' . $partial, $this->get_setting( 'rendered_expire_time' ) );
if ( ! ( $rendered = wp_cache_get( $data_hash, 'dustpress/rendered' ) ) ) {
$rendered = $dust->renderTemplate( $compiled, $data );
wp_cache_set( $data_hash, $rendered, 'dustpress/rendered', $cache_time );
}
}
else {
$rendered = $dust->renderTemplate( $compiled, $data );
}
return $rendered;
},
'json' => function( $data, $partial, $dust ) {
try {
$output = json_encode( $data );
header( 'Content-Type: application/json' );
}
catch ( Exception $e ) {
http_response_code(500);
die( 'JSON encode error: ' . $e->getMessage() );
}
return $output;
}
);
add_filter( 'dustpress/formats/use_dust/html', '__return_true' );
$types = apply_filters( 'dustpress/formats', $types );
if ( ! $data && ! empty( $this->model ) ) {
$this->model->data = (array) $this->model->data;
$this->model->data['WP'] = $this->populate_data_collection();
}
if ( apply_filters( 'dustpress/formats/use_dust/' . $type, false ) ) {
// Ensure we have a DustPHP instance.
if ( isset( $this->dust ) ) {
$dust = $this->dust;
}
else {
http_response_code(500);
die( 'DustPress error: Something very unexpected happened: there is no DustPHP.' );
}
if ( ! isset( $args['partial'] ) ) {
http_response_code(500);
die( '<p><b>DustPress error:</b> No partial is given to the render function.</p>' );
}
$dust->helpers = apply_filters( 'dustpress/helpers', $dust->helpers );
$dust->filters = apply_filters( 'dustpress/filters', $dust->filters );
// Fetch Dust partial by given name. Throw error if there is something wrong.
try {
$template = $this->get_template( $partial );
$helpers = $this->prerender( $partial );
$this->prerun_helpers( $helpers );
}
catch ( Exception $e ) {
http_response_code(500);
die( 'DustPress error: '. $e->getMessage() );
}
}
else {
$dust = null;
$template = $partial;
}
if ( ! empty( $data ) ) {
$render_data = apply_filters( 'dustpress/data', $data );
}
elseif ( ! empty( $this->model->data ) ) {
$render_data = apply_filters( 'dustpress/data', $this->model->data );
$render_data = apply_filters( 'dustpress/data/main', $render_data );
}
else {
$render_data = null;
}
// Create output with wanted format.
$output = call_user_func_array( $types[$type], array( $render_data, $template, $dust ) );
// Filter output
$output = apply_filters( 'dustpress/output', $output, $options );
$this->save_dustpress_performance( $performance_measure_id );
if ( $main ) {
# A bit hacky but create_instance performance has to be ended here, because
# debugger fetches the data on dustpress/data/after_render filter.
$this->save_dustpress_performance( 'create_instance--0' );
}
// Do something with the data after rendering
apply_filters( 'dustpress/data/after_render', $render_data, $main );
if ( $echo ) {
if ( empty ( strlen( $output ) ) ) {
$error = true;
echo 'DustPress warning: empty output.';
}
else {
echo $output;
}
}
else {
return $output;
}
if ( isset( $error ) ) {
return false;
}
else {
return true;
}
}
/**
* This function checks whether the given partial exists and returns the contents of the file as a string
*
* @type function
* @date 17/3/2015
* @since 0.0.1
*
* @param string $partial
* @return $template (string)
*/
private function get_template( $partial ) {
$performance_measure_id = $this->start_dustpress_performance( __FUNCTION__ );
// Check if we have received an absolute path.
if ( file_exists( $partial ) ) {
$this->save_dustpress_performance( $performance_measure_id );
return $partial;
}
else {
$templatefile = $partial . '.dust';
$templates = $this->get_templates();
if ( isset( $templates[ $templatefile ] ) ) {
$this->save_dustpress_performance( $performance_measure_id );
return $templates[ $templatefile ];
}
// Force searching of templates bypassing the cache.
$templates = $this->get_templates( true );
if ( isset( $templates[ $templatefile ] ) ) {
$this->save_dustpress_performance( $performance_measure_id );
return $templates[ $templatefile ];
}
// If we could not find such template.
throw new Exception( 'Error loading template file: ' . $partial, 1 );
}
}
/**
* This function initializes DustPress settings with default values
*
* @type function
* @date 01/04/2016
* @since 0.4.0
*/
public function init_settings() {
$performance_measure_id = $this->start_dustpress_performance( __FUNCTION__ );
$this->settings = [
'cache' => false,
'debug_data_block_name' => 'Debug',
'rendered_expire_time' => 7*60*60*24,
'json_url' => false,
'json_headers' => false,
];
// loop through the settings and execute possible filters from functions
foreach ( $this->settings as $key => $value ) {