This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Features/ComponentLogServer): add a simple logging functionality (…
- Loading branch information
Doğa Gürdal
authored
Nov 5, 2018
1 parent
246a8f9
commit cccc3c5
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# HowTo | ||
|
||
## Register Feature `/lib/init.php` | ||
```php | ||
<?php | ||
|
||
function initTheme() | ||
{ | ||
add_theme_support('flynt-component-log-server'); | ||
} | ||
``` | ||
|
||
## Usage | ||
This functionality is only enabled on the development environment or for logged in administrators / editors on all environments. | ||
|
||
Add the GET parameter `log` to any url (e.g. `http://localhost:3000/?log`) and all the data for that page will be output via console.log in the dev tools of your browser. By specifying an additional `component` parameter with component names separated by a comma you can limit the output to specific components: `http://localhost:3000/?log&component=BlockWysiwyg,DocumentDefault`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Flynt\Features\ComponentLogServer; | ||
|
||
use Flynt; | ||
|
||
define(__NAMESPACE__ . '\NS', __NAMESPACE__ . '\\'); | ||
|
||
add_action('Flynt/afterRegisterFeatures', function () { | ||
$componentManager = Flynt\ComponentManager::getInstance(); | ||
$componentWhitelist = []; | ||
if (isset($_GET['component']) && !empty($_GET['component'])) { | ||
$componentWhitelist = explode(',', $_GET['component']); | ||
} | ||
if (count($componentWhitelist) === 0) { | ||
foreach ($componentManager->getAll() as $name => $path) { | ||
add_filter("Flynt/addComponentData?name={$name}", NS . 'addDebugInfo', 12, 3); | ||
} | ||
} else { | ||
foreach ($componentManager->getAll() as $name => $path) { | ||
if (in_array($name, $componentWhitelist)) { | ||
add_filter("Flynt/addComponentData?name={$name}", NS . 'addDebugInfo', 12, 3); | ||
} | ||
} | ||
} | ||
}, 11); | ||
|
||
function addDebugInfo($data, $parentData, $config) | ||
{ | ||
if ((WP_ENV === 'development' || current_user_can('editor') || current_user_can('administrator')) && isset($_GET['log'])) { | ||
consoleDebug([ | ||
'component' => $config['name'], | ||
'config' => $config, | ||
'data' => $data, | ||
'parentData' => $parentData, | ||
]); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
function consoleDebug($data, $postpone = true) | ||
{ | ||
$output = json_encode($data); | ||
$result = "<script>console.log({$output});</script>\n"; | ||
echoDebug($result, $postpone); | ||
} | ||
|
||
function echoDebug($data, $postpone) | ||
{ | ||
if ($postpone) { | ||
add_action('wp_footer', function () use ($data) { | ||
echo $data; | ||
}, 30); | ||
} else { | ||
echo $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters