Skip to content

Commit

Permalink
Disabled for WP JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
dmhendricks committed Jul 1, 2018
1 parent 11a9cb7 commit dc19a57
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Usual DOM parser: https://github.com/wasinger/htmlpagedom
Simple removing text example: https://stackoverflow.com/a/12510430/3799374
PHP DOM Documentation (search for ->removeChild()): http://php.net/manual/en/domdocument.loadhtml.php
- removeChild, insertAfter, insertbefore: http://php.net/manual/en/domnode.removechild.php
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ A simple [mu-plugin](https://codex.wordpress.org/Must_Use_Plugins) that buffers

[Original code](http://stackoverflow.com/a/22818089/3799374) by [kfriend](https://stackoverflow.com/users/419673/kfriend) on Stack Overflow. Licensed as GPL because it is a WordPress derivative.

[![Analytics](https://ga-beacon.appspot.com/UA-67333102-2/dmhendricks/wordpress-output-buffering?flat)](https://ga-beacon.appspot.com/?utm_source=github.com&utm_medium=campaign&utm_content=button&utm_campaign=wordpress-output-buffering)

## Installation

To install, simply download and copy `output-buffering.php` to your `/wp-content/mu-plugins` directory.
Expand Down Expand Up @@ -42,7 +44,7 @@ define( 'OB_REQUEST_TYPES', array( 'site', 'admin', 'ajax' ) ); // Case-sensitiv
In the example above, output buffering is enable on the frontend ("site"), in WP Admin ("admin") and during AJAX requests ("ajax"). Add or remove from the array as desired. For example, to **only** load output buffering in WP Admin and **not** on the frontend or during AJAX calls:

```
define( 'OB_ENABLE_SCREENS', array( 'admin' ) ); // Case-sensitive
define( 'OB_ENABLE_SCREENS', array( 'admin' ) );
```

##### Caution
Expand All @@ -53,7 +55,10 @@ Always test first before using in a production setting!

## Changelog

**1.0.3 (master)**
**1.0.4**
* Disabled when doing WP JSON (for Gutenberg compatibility)

**1.0.3**
* Added wp-config.php constants to control where output buffering is enabled.

**1.0.0**
Expand Down
27 changes: 14 additions & 13 deletions output-buffering.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
* 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.3
* Version: 1.0.4
* 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 TwoLab\MustUse;
namespace CloudVerve\MustUse;

class OutputBuffering {

public function __construct() {

if($this->can_load()) $this->output_buffering();
if( $this->can_load() ) $this->output_buffering();

}

Expand All @@ -35,18 +35,17 @@ public function output_buffering() {

ob_start();

add_action('shutdown', function() {
add_action( 'shutdown', function() {
$final = '';

// Iterate over each OB level
$levels = ob_get_level();
for ($i = 0; $i < $levels; $i++)
{
for ( $i = 0; $i < $levels; $i++ ) {
$final .= ob_get_clean();
}

// Apply any filters to the final output
echo apply_filters('final_output', $final);
echo apply_filters( 'final_output', $final );
}, 0);

}
Expand All @@ -59,14 +58,14 @@ public function output_buffering() {
*/
private function get_load_screens() {

if( defined('OB_ENABLE_SCREENS')) {
return is_array(OB_ENABLE_SCREENS) ? OB_ENABLE_SCREENS : array( OB_ENABLE_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';
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;
Expand All @@ -81,6 +80,8 @@ private function get_load_screens() {
*/
private function can_load() {

if( strpos( $_SERVER['REQUEST_URI'], '/wp-json' ) === 0 ) 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 );
Expand All @@ -96,7 +97,7 @@ private function can_load() {
* @return bool
*/
private function is_ajax() {
return defined('DOING_AJAX') && DOING_AJAX;
return defined( 'DOING_AJAX' ) && DOING_AJAX;
}

}
Expand Down

0 comments on commit dc19a57

Please sign in to comment.