-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoutput-buffering.php
106 lines (83 loc) · 3.03 KB
/
output-buffering.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
<?php
/**
* @wordpress-plugin
* Plugin Name: Output Buffering
* Plugin URI: https://github.com/dmhendricks/wordpress-output-buffering
* Description: Buffers the entire WP process, capturing the final output for manipulation.
* Version: 1.0.5
* Author: Daniel M. Hendricks
* Original Author: kfriend (https://stackoverflow.com/users/419673/kfriend)
* Author URI: https://www.danhendricks.com
* License: GPL-2.0
* License URI: https://opensource.org/licenses/GPL-2.0
* GitHub Plugin URI: dmhendricks/wordpress-output-buffering
*/
namespace CloudVerve\MustUse;
class OutputBuffering {
public function __construct() {
if( $this->can_load() ) $this->output_buffering();
}
/**
* Returns string of addition CSS classes based on post type
*
* Returns CSS classes such as page-{slug}, parent-{slug}, post-type-{type} and
* category-{slug} for easier selector targeting
*
* @param array $classes An array of *current* body_class classes
* @return array Modified array of body classes including new ones
*/
public function output_buffering() {
ob_start();
add_action( 'shutdown', function() {
$final = '';
// Iterate over each OB level
$levels = ob_get_level();
for ( $i = 0; $i < $levels; $i++ ) {
$final .= ob_get_clean();
}
// Apply any filters to the final output
echo apply_filters( 'final_output', $final );
}, 0);
}
/**
* Get a list of screens/requests types to enable output buffering for.
* 'site' for frontend (default), 'admin' for WP Admin, 'ajax' for AJAX requests.
*
* @return array
*/
private function get_load_screens() {
if( defined( 'OB_ENABLE_SCREENS' ) ) {
return is_array( OB_ENABLE_SCREENS ) ? OB_ENABLE_SCREENS : array( OB_ENABLE_SCREENS );
}
$screens = array( 'site' );
if( defined( 'OB_ENABLE_ADMIN' ) || defined( 'OB_ENABLE_AJAX' ) ) {
if( defined( 'OB_ENABLE_ADMIN' ) && OB_ENABLE_ADMIN ) $screens[] = 'admin';
if( defined( 'OB_ENABLE_AJAX' ) && OB_ENABLE_AJAX ) $screens[] = 'ajax';
}
return $screens;
}
/**
* Determine if output buffering should be enabled on frontend (default), WP Admin,
* and/or while doing AJAX requests.
*
* @return bool Whether or not output buffering should be performed.
*/
private function can_load() {
if( strpos( $_SERVER['REQUEST_URI'], '/wp-json' ) === 0 || ( defined( 'WP_CLI' ) && WP_CLI ) ) return false;
$load_screens = $this->get_load_screens();
$load_admin = is_admin() && in_array( 'admin', $load_screens );
$load_ajax = $this->is_ajax() && in_array( 'ajax', $load_screens );
$load_site = (!is_admin() && !$this->is_ajax()) && in_array( 'site', $load_screens );
return $load_admin || $load_ajax || $load_site;
}
/**
* Determine if current request is AJAX
*
* @return bool
*/
private function is_ajax() {
return defined( 'DOING_AJAX' ) && DOING_AJAX;
}
}
new OutputBuffering();
?>