functions.php ➔ enqueueComponentScripts()   A
last analyzed

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
function enqueueComponentScripts()
9
{
10
    Asset::register([
11
        'type' => 'script',
12
        'name' => 'draggabilly',
13
        'path' => 'vendor/draggabilly.js'
14
    ]);
15
16
    Asset::enqueue([
17
        'type' => 'script',
18
        'name' => 'Flynt/Features/AdminComponentPreview',
19
        'path' => 'Features/AdminComponentPreview/script.js',
20
        'dependencies' => ['jquery', 'draggabilly']
21
    ]);
22
23
    Asset::enqueue([
24
        'type' => 'style',
25
        'name' => 'Flynt/Features/AdminComponentPreview',
26
        'path' => 'Features/AdminComponentPreview/style.css'
27
    ]);
28
29
    // add data to the javascript
30
    $data = [
31
        'templateDirectoryUri' => get_template_directory_uri() . '/dist'
32
    ];
33
    wp_localize_script('Flynt/Features/AdminComponentPreview', 'wpData', $data);
34
}
35
36
if (class_exists('acf')) {
37
    if (is_user_logged_in() || is_admin()) {
38
        if (is_admin()) {
39
            add_action('admin_enqueue_scripts', NS . 'enqueueComponentScripts');
40
            // add image to the flexible content component name
41
            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...
42
                $componentName = ucfirst($layout['name']);
43
                $componentPath = "Components/{$componentName}";
44
                $componentPreviewDesktopPath = Asset::requirePath("{$componentPath}/preview-desktop.jpg") ;
45
                $componentPreviewDesktopUrl = Asset::requireUrl("{$componentPath}/preview-desktop.jpg");
46
                if (is_file($componentPreviewDesktopPath)) {
47
                    $newTitle = '<span class="flyntComponentPreview">';
48
                    $newTitle .= '<img class="flyntComponentPreview-imageElement" src="' . $componentPreviewDesktopUrl . '" height="36px">';
49
                    $newTitle .= '<span class="flyntComponentPreview-label">' . $title . '</span>';
50
                    $newTitle .= '</span>';
51
                    $title = $newTitle;
52
                }
53
                return $title;
54
            }, 11, 4);
55
        } else {
56
            add_action('wp_enqueue_scripts', NS . 'enqueueComponentScripts');
57
            // adds Component Previews button to admin bar on front-end when logged in
58
            add_action('admin_bar_menu', function ($wpAdminBar) {
59
                $title = __('Component Previews', 'flynt-starter-theme');
60
                $wpAdminBar->add_menu([
61
                    'id' => 'toggleComponentPreviews',
62
                    'title' => $title
63
                ]);
64
            });
65
        }
66
    }
67
}
68