From dc19a57eee08fcbae1abc693e4bb0f2fefe37878 Mon Sep 17 00:00:00 2001 From: Daniel Hendricks Date: Sun, 1 Jul 2018 08:54:47 -0500 Subject: [PATCH] Disabled for WP JSON --- .notes.txt | 4 ++++ README.md | 9 +++++++-- output-buffering.php | 27 ++++++++++++++------------- 3 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 .notes.txt diff --git a/.notes.txt b/.notes.txt new file mode 100644 index 0000000..cfb98b6 --- /dev/null +++ b/.notes.txt @@ -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 diff --git a/README.md b/README.md index 97b283c..917a15e 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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** diff --git a/output-buffering.php b/output-buffering.php index fd22619..e25d76d 100644 --- a/output-buffering.php +++ b/output-buffering.php @@ -4,7 +4,7 @@ * 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 @@ -12,13 +12,13 @@ * 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(); } @@ -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); } @@ -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; @@ -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 ); @@ -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; } }