Completed
Pull Request — master (#14)
by Beñat
07:49
created

Assets::assets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the WPFoundation library.
5
 *
6
 * Copyright (c) 2015-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\WPFoundation\Configuration\Assets;
13
14
/**
15
 * @author Beñat Espiña <[email protected]>
16
 * @author Gorka Laucirica <[email protected]>
17
 */
18
abstract class Assets implements AssetsInterface
19
{
20
    public function __construct()
21
    {
22
        if (true === WP_DEBUG) {
23
            add_action('wp_enqueue_scripts', [$this, 'developmentAssets']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
24
        } else {
25
            add_action('wp_enqueue_scripts', [$this, 'productionAssets']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
26
        }
27
        add_action('admin_enqueue_scripts', [$this, 'adminAssets']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
28
    }
29
30
    public function developmentAssets() : void
31
    {
32
    }
33
34
    public function productionAssets() : void
35
    {
36
    }
37
38
    public function adminAssets() : void
39
    {
40
    }
41
42
    protected function addScript(
43
        string $name,
44
        string $from = AssetsInterface::ASSETS_JS,
45
        array $dependencies = ['jquery'],
46
        string $version = '1.0.0',
47
        bool $inFooter = true,
48
        ?string $ajaxUrl = null
49
    ) {
50
        wp_enqueue_script($name, $this->path($from, $name), $dependencies, $version, $inFooter);
0 ignored issues
show
Unused Code introduced by
The call to the function wp_enqueue_script() seems unnecessary as the function has no side-effects.
Loading history...
51
52
        if (null !== $ajaxUrl) {
53
            $this->registerAjaxUrls($name, $ajaxUrl);
54
        }
55
56
        return $this;
57
    }
58
59
    protected function addStylesheet(
60
        string $name,
61
        string $from = AssetsInterface::CSS,
62
        array $dependencies = [],
63
        string $version = '1.0.0',
64
        string $media = 'all'
65
    ) {
66
        wp_enqueue_style($name, $this->path($from, $name, 'css'), $dependencies, $version, $media);
0 ignored issues
show
Unused Code introduced by
The call to the function wp_enqueue_style() seems unnecessary as the function has no side-effects.
Loading history...
67
68
        return $this;
69
    }
70
71
    /*
72
     *  Usage example with name="subscribe" and ajaxUrl="subscribeAjax":
73
     *
74
     *      // subscribe.js
75
     *
76
     *      $.ajax({
77
     *          url: subscribeAjax.ajaxUrl,
78
     *          method: 'GET',
79
     *          data: {
80
     *              action: 'ajax-action-registered-in-your-php-file',
81
     *          }
82
     *      }).done(function (response) {
83
     *         (...)
84
     *      });
85
     */
86
    protected function registerAjaxUrls(string $name, string $ajaxUrl) : void
87
    {
88
        if (false === is_array($ajaxUrl)) {
89
            $ajaxUrl = [$ajaxUrl];
90
        }
91
        foreach ($ajaxUrl as $url) {
92
            wp_localize_script($name, $url, [
93
                'ajaxUrl' => admin_url('admin-ajax.php'),
94
            ]);
95
        }
96
    }
97
98
    private function path(string $from, string $name, string $fileType = 'js') : string
99
    {
100
        return get_template_directory_uri() . '/Resources/' . $from . '/' . $name . '.' . $fileType;
101
    }
102
}
103