Completed
Push — develop ( 246a8f...cccc3c )
by Doğa
07:45
created

functions.php ➔ addDebugInfo()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 2
nop 3
dl 0
loc 13
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Features\ComponentLogServer;
4
5
use Flynt;
6
7
define(__NAMESPACE__ . '\NS', __NAMESPACE__ . '\\');
8
9
add_action('Flynt/afterRegisterFeatures', function () {
10
    $componentManager = Flynt\ComponentManager::getInstance();
11
    $componentWhitelist = [];
12
    if (isset($_GET['component']) && !empty($_GET['component'])) {
13
        $componentWhitelist = explode(',', $_GET['component']);
14
    }
15
    if (count($componentWhitelist) === 0) {
16
        foreach ($componentManager->getAll() as $name => $path) {
17
            add_filter("Flynt/addComponentData?name={$name}", NS . 'addDebugInfo', 12, 3);
18
        }
19
    } else {
20
        foreach ($componentManager->getAll() as $name => $path) {
21
            if (in_array($name, $componentWhitelist)) {
22
                add_filter("Flynt/addComponentData?name={$name}", NS . 'addDebugInfo', 12, 3);
23
            }
24
        }
25
    }
26
}, 11);
27
28
function addDebugInfo($data, $parentData, $config)
29
{
30
    if ((WP_ENV === 'development' || current_user_can('editor') || current_user_can('administrator')) && isset($_GET['log'])) {
31
        consoleDebug([
32
            'component' => $config['name'],
33
            'config' => $config,
34
            'data' => $data,
35
            'parentData' => $parentData,
36
        ]);
37
    }
38
39
    return $data;
40
}
41
42
function consoleDebug($data, $postpone = true)
43
{
44
    $output = json_encode($data);
45
    $result =  "<script>console.log({$output});</script>\n";
46
    echoDebug($result, $postpone);
47
}
48
49
function echoDebug($data, $postpone)
50
{
51
    if ($postpone) {
52
        add_action('wp_footer', function () use ($data) {
53
            echo $data;
54
        }, 30);
55
    } else {
56
        echo $data;
57
    }
58
}
59