Completed
Push — componentlibrary ( 56c322...de047a )
by Dominik
02:25 queued 12s
created

functions.php ➔ enqueueComponentScripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
namespace Flynt\Features\AdminComponentPreview;
3
4
use Flynt\Utils\Asset;
5
6
define(__NAMESPACE__ . '\NS', __NAMESPACE__ . '\\');
7
8
add_action('wp_enqueue_scripts', function () {
9
    if (is_user_logged_in()) {
10
        Asset::register([
11
            'type' => 'script',
12
            'name' => 'draggabilly',
13
            'path' => 'vendor/draggabilly.js'
14
        ]);
15
        Asset::addDependencies('Flynt/assets/auth', ['draggabilly']);
16
    }
17
    $data = [
18
        'templateDirectoryUri' => get_template_directory_uri() . '/dist',
19
    ];
20
    wp_localize_script('Flynt/assets/auth', 'wpData', $data);
21
});
22
23
add_action('wp_admin_enqueue_scripts', function () {
24
    $data = [
25
        'templateDirectoryUri' => get_template_directory_uri() . '/dist',
26
    ];
27
    wp_localize_script('Flynt/assets/admin', 'wpData', $data);
28
});
29
30
if (class_exists('acf')) {
31
    if (is_user_logged_in() || is_admin()) {
32
        if (is_admin()) {
33
            // add image to the flexible content component name
34
            add_filter('acf/fields/flexible_content/layout_title', function ($title, $field, $layout, $i) {
0 ignored issues
show
Unused Code introduced by
The parameter $i is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
                $componentName = ucfirst($layout['name']);
36
                $componentPath = "Components/{$componentName}";
37
                $componentPreviewDesktopPath = Asset::requirePath("{$componentPath}/preview-desktop.jpg");
38
                $componentPreviewDesktopUrl = Asset::requireUrl("{$componentPath}/preview-desktop.jpg");
39
                if (is_file($componentPreviewDesktopPath)) {
40
                    $newTitle = '<span class="flyntComponentPreview">';
41
                    $newTitle .= '<img class="flyntComponentPreview-imageElement" src="' . $componentPreviewDesktopUrl . '" height="36px">';
42
                    $newTitle .= '<span class="flyntComponentPreview-label">' . $title . '</span>';
43
                    $newTitle .= '</span>';
44
                    $title = $newTitle;
45
                }
46
                return $title;
47
            }, 11, 4);
48
        } else {
49
            // adds Component Previews button to admin bar on front-end when logged in
50
            add_action('admin_bar_menu', function ($wpAdminBar) {
51
                $title = __('Component Previews', 'flynt-starter-theme');
52
                $wpAdminBar->add_node([
53
                    'id' => 'toggleComponentPreviews',
54
                    'title' => $title,
55
                    'href' => '#'
56
                ]);
57
            }, 31);
58
        }
59
    }
60
}
61