flyntwp /
flynt
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Usage: |
||
| 5 | * Add get param `log` to url e.g. `http://localhost:3000/?log` and all the data will be output to via console.log in the dev tools in the browser. |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace Flynt\ComponentLogServer; |
||
| 9 | |||
| 10 | use Flynt; |
||
| 11 | |||
| 12 | define(__NAMESPACE__ . '\NS', __NAMESPACE__ . '\\'); |
||
| 13 | |||
| 14 | add_action('Flynt/afterRegisterComponents', function () { |
||
| 15 | if (((defined('WP_ENV') && WP_ENV === 'development') || current_user_can('editor') || current_user_can('administrator')) && isset($_GET['log'])) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 16 | if (isset($_GET['component']) && !empty($_GET['component'])) { |
||
| 17 | define(__NAMESPACE__ . '\COMPONENT_WHITELIST', explode(',', $_GET['component'])); |
||
| 18 | } |
||
| 19 | add_filter("Flynt/addComponentData", NS . 'addDebugInfo', 99999, 2); |
||
| 20 | } |
||
| 21 | }, 11); |
||
| 22 | |||
| 23 | function addDebugInfo($data, $componentName) |
||
| 24 | { |
||
| 25 | if ( |
||
| 26 | !defined(__NAMESPACE__ . '\COMPONENT_WHITELIST') || |
||
| 27 | in_array($componentName, Flynt\ComponentLogServer\COMPONENT_WHITELIST) |
||
|
0 ignored issues
–
show
|
|||
| 28 | ) { |
||
| 29 | consoleDebug([ |
||
| 30 | 'component' => $componentName, |
||
| 31 | 'data' => $data, |
||
| 32 | ]); |
||
| 33 | } |
||
| 34 | return $data; |
||
| 35 | } |
||
| 36 | |||
| 37 | function consoleDebug($data, $postpone = true) |
||
| 38 | { |
||
| 39 | $type = gettype($data); |
||
|
0 ignored issues
–
show
|
|||
| 40 | $output = json_encode($data); |
||
| 41 | $result = "<script>console.log({$output});</script>\n"; |
||
| 42 | echoDebug($result, $postpone); |
||
| 43 | } |
||
| 44 | |||
| 45 | function consoleTable($data, $postpone = true) |
||
| 46 | { |
||
| 47 | $output = json_encode($data); |
||
| 48 | $result = "<script>console.table({$output});</script>\n"; |
||
| 49 | echoDebug($result, $postpone); |
||
| 50 | } |
||
| 51 | |||
| 52 | function echoDebug($data, $postpone) |
||
| 53 | { |
||
| 54 | if ($postpone) { |
||
| 55 | add_action('wp_footer', function () use ($data) { |
||
| 56 | echo $data; |
||
| 57 | }, 30); |
||
| 58 | } else { |
||
| 59 | echo $data; |
||
| 60 | } |
||
| 61 | } |
||
| 62 |