Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Kint to 5, stop overriding d() function in favor of new function ddb() #1

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,27 @@ Kint Debugger works well with the [Debug Bar](https://wordpress.org/plugins/debu
## Basic Usage

```php
d( $var );
ddb( $var );
```

## Examples:

```php
global $post;
d( $post );
ddb( $post );
```

```php
global $post;
$term_list = wp_get_post_terms( $post->ID, 'my_taxonomy', array( 'fields' => 'all' ) );
d( $term_list );
ddb( $term_list );
```

Kint Debugger also provides some helper functions for dumping variables that are frequently needed.

* `dump_wp_query()`
* `dump_wp()`
* `dump_post()`
* `dump_this( $var, $inline = false )` - explained below

Examples:

Expand All @@ -46,7 +45,7 @@ dump_post();
add_action( 'wp_head', 'dump_post' );
```

Obviously, if this plugin is not active, calls to the helper functions will cause errors.
Obviously, if this plugin is not active, calls to these functions will cause errors.

## Your Own Functions

Expand All @@ -56,7 +55,7 @@ If you are dumping the same information in different places, consider writing yo
function my_dump_terms() {
global $post;
$term_list = wp_get_post_terms( $post->ID, 'my_taxonomy', array( 'fields' => 'all' ) );
d( $term_list );
ddb( $term_list );
}
```

Expand All @@ -68,33 +67,24 @@ my_dump_terms();

## With Debug Bar

By default, when [Debug Bar](https://wordpress.org/plugins/debug-bar/) is installed and active, Kint Debugger will send d() output to its Debug Bar panel.
When [Debug Bar](https://wordpress.org/plugins/debug-bar/) is installed and active, Kint Debugger will send ddb() output to its Debug Bar panel.

To print debug output inline instead, as if Debug Bar was not active, declare the constant KINT_TO_DEBUG_BAR in your config.php (or really anywhere before your d() call):
To print a specific dump inline, use Kint's native d() call:

```php
define( 'KINT_TO_DEBUG_BAR', false );
d( $var );
```

Or to print a specific dump inline, use a helper function with the parameter `$inline`. The generic `dump_this()` takes `$inline` as the second parameter.

Examples:
Helper functions take a boolean parameter `$inline`. Examples:

```php
dump_post( true );
```

```php
global $post;
$term_list = wp_get_post_terms( $post->ID, 'my_taxonomy', array( 'fields' => 'all' ) );
dump_this( $term_list , true );
dump_wp( true );
```

Kint Debugger overrides Kint's d() function in order to buffer its output for Debug Bar. If you already have a modified d() function, you need to prevent the override in one of two ways.

1. Move your modified d() function to an mu-plugin. Kint Debugger checks if the function exists before declaring it so putting yours in an mu-plugin is the only way to ensure it exists first.
1. Declare KINT_TO_DEBUG_BAR as described above.

## Restricting Output

To restrict visibility, use the `kint_debug_display` filter. For example, to prevent non-admins from seeing the debug output:
Expand All @@ -109,15 +99,13 @@ add_filter( 'kint_debug_display', function( $allow ) { return is_super_admin();

### I have called a debug function, but I can't find the output.

If Debug Bar is installed and active, your debug results will be displayed on the "Kint Debugger" panel.
If Debug Bar is installed and active, your debug results when calling `ddb` will be displayed on the "Kint Debugger" panel.

Otherwise, your debug results will be inserted into the current page's HTML.

### Can I change the style of the output?

Currently, the Kint library includes some themes and a config file. Feel free to configure as you see fit. In order to leave the Kint library intact, the plugin does not provide additional configuration.

Fortunately, the developers of Kint are working on version 2 which will make it easier to configure and extend it.
See Kint library documentation. This plugin does not theme the output, nor should it prevent Kint's theming options from being used.

---

Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "jameelmoses/wordpress-kint-debugger",
"version": "1.0.1",
"description": "WordPress Kint Debugger is a simple wrapper for Kint, a debugging tool to output information about variables and traces in a styled, collapsible format that makes understanding deep arrays and objects easier.",
"keywords": [
"WP",
Expand All @@ -18,6 +17,10 @@
"minimum-stability": "stable",
"license": "GPL-3.0+",
"require": {
"php": ">=5.6.0"
"php": ">=7.1",
"kint-php/kint": "^5"
},
"config": {
"optimize-autoloader": true
}
}
86 changes: 86 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 35 additions & 54 deletions kint-debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* Plugin Name: WordPress Kint Debugger
* Plugin URI: https://github.com/jameelmoses/wordpress-kint-debugger
* Description: Dump variables and traces in an organized and interactive display. Works with Debug Bar.
* Version: 1.0.1
* Version: 2.0.0
* Author: Jameel Moses
* Author URI: https://github.com/jameelmoses
* Requires: 2.5 or higher
* Requires PHP: 7.1
* License: Dual license GPL-3.0 & MIT (Kint is licensed MIT)
*
* This program is free software; you can redistribute it and/or modify
Expand All @@ -24,107 +25,87 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

use Kint\Kint;

/**
* Load Kint after this plugin to ensure our modified d() function override.
*
* @since 1.0
* Load Kint via plugin-specific autoloader, only if the class is not already present.
*/
function kint_debug_load_kint() {
require 'vendor/kint/Kint.class.php';
if ( !class_exists( Kint::class ) ) {
require_once( __DIR__ . '/vendor/autoload.php' );
}
add_action( 'plugins_loaded', 'kint_debug_load_kint' );

/**
* Generic data dump.
*
* @since 1.0
* Helper functions.
*/

if ( ! function_exists( 'dump_this' ) ) {
/**
* Generic data dump.
*
* @since 1.0
*/
function dump_this( $var, $inline = false ) {
/**
* Some hooks send WP objects which then get passed as $inline
* so check type too.
*/
if ( true === $inline ) {
$_ = array( $var );
echo call_user_func_array( array( 'Kint', 'dump' ), $_ );
Kint::dump( $var );
}
else {
d( $var );
ddb( $var );
}
}
}

/**
* Helper functions.
*/
Kint::$aliases[] = 'dump_this';

if ( ! function_exists( 'dump_wp_query' ) ) {
function dump_wp_query( $inline = false ) {
function dump_wp_query( bool $inline = false ) {
global $wp_query;
dump_this( $wp_query, $inline );
}
}
Kint::$aliases[] = 'dump_wp_query';

if ( ! function_exists( 'dump_wp' ) ) {
function dump_wp( $inline = false ) {
function dump_wp( bool $inline = false ) {
global $wp;
dump_this( $wp, $inline );
}
}
Kint::$aliases[] = 'dump_wp';

if ( ! function_exists( 'dump_post' ) ) {
function dump_post( $inline = false ) {
function dump_post( bool $inline = false ) {
global $post;
dump_this( $post, $inline );
}
}
Kint::$aliases[] = 'dump_post';

/**
* Override can be prevented using config constant.
* Alias of Kint::dump() similar to d().
*
* @since 1.0
* Unlike d(), this sends Kint output to Debug Bar if active.
*/
if ( ! defined( 'KINT_TO_DEBUG_BAR' ) || KINT_TO_DEBUG_BAR ) {
// An mu-plugin can still override the function.
if ( ! function_exists( 'd' ) ) {
/**
* Alias of Kint::dump()
*
* This sends Kint output to Debug Bar if active.
*
* Can be prevented by declaring the function first in an mu-plugin
* (but not a theme due to WordPress load sequence).
*
* @return string
*/
function d() {
/** @noinspection PhpUndefinedClassInspection */
if ( ! Kint::enabled() ) {
return '';
}
$_ = func_get_args();
if ( class_exists( 'Debug_Bar' ) ) {
ob_start( 'kint_debug_ob' );
echo call_user_func_array( array( 'Kint', 'dump' ), $_ );
ob_end_flush();
} else {
return call_user_func_array( array( 'Kint', 'dump' ), $_ );
}

return '';
}
function ddb( ...$args ): int|string {
if ( class_exists( 'Debug_Bar' ) ) {
ob_start( 'kint_debug_ob' );
Kint::dump( ...$args );
ob_end_flush();
return '';
}

return Kint::dump( ...$args );
}
Kint::$aliases[] = 'ddb';

/**
* Output buffer callback.
*
* @param $buffer
*
* @return string
*/
function kint_debug_ob( $buffer ) {
function kint_debug_ob( $buffer ): string {
global $kint_debug;
$kint_debug[] = $buffer;
if ( class_exists( 'Debug_Bar' ) ) {
Expand Down
25 changes: 25 additions & 0 deletions vendor/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

// autoload.php @generated by Composer

if (PHP_VERSION_ID < 50600) {
if (!headers_sent()) {
header('HTTP/1.1 500 Internal Server Error');
}
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
if (!ini_get('display_errors')) {
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
fwrite(STDERR, $err);
} elseif (!headers_sent()) {
echo $err;
}
}
trigger_error(
$err,
E_USER_ERROR
);
}

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit03bab38e7d5f05d62c02910a3c8448a4::getLoader();
Loading