Completed
Push — master ( 5ba7f7...a2a0a6 )
by Beñat
8s
created

Assets::assets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
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-2016 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
 * Abstract class of assets class that implements the interface.
16
 * This class avoids the use of callbacks in the constructor.
17
 *
18
 * @author Beñat Espiña <[email protected]>
19
 * @author Gorka Laucirica <[email protected]>
20
 */
21
abstract class Assets implements AssetsInterface
22
{
23
    /**
24
     * Constructor.
25
     */
26
    public function __construct()
27
    {
28
        if (true === WP_DEBUG) {
29
            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...
30
        } else {
31
            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...
32
        }
33
        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...
34
35
        // @deprecated since version 1.5, will be removed in 2.0. Implement productionAssets and developmentAssets instead.
36
        add_action('wp_enqueue_scripts', [$this, 'assets']);
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...
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function developmentAssets()
43
    {
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function productionAssets()
50
    {
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function adminAssets()
57
    {
58
    }
59
60
    /**
61
     * Method that wraps the WordPress internal "wp_enqueue_script"
62
     * simplifying the process adding some common default values.
63
     *
64
     * @param string            $name         The name of asset
65
     * @param string            $from         The from location, by default is the JS files default location
66
     * @param array             $dependencies Array which contains the dependencies of the given asset
67
     * @param string            $version      The version, by default is "1.0.0"
68
     * @param bool              $inFooter     Checks if the asset is going to be in the footer or not
69
     * @param array|string|null $ajaxUrl      The ajax url to expose in JS files
70
     *
71
     * @return $this Self class instance
72
     */
73
    protected function addScript(
74
        $name,
75
        $from = AssetsInterface::ASSETS_JS,
76
        array $dependencies = ['jquery'],
77
        $version = '1.0.0',
78
        $inFooter = true,
79
        $ajaxUrl = null
80
    ) {
81
        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...
82
83
        if (null !== $ajaxUrl) {
84
            $this->registerAjaxUrls($name, $ajaxUrl);
0 ignored issues
show
Bug introduced by
It seems like $ajaxUrl defined by parameter $ajaxUrl on line 79 can also be of type array; however, LIN3S\WPFoundation\Confi...ets::registerAjaxUrls() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * Method that wraps the WordPress internal "wp_enqueue_style"
92
     * simplifying the process adding some common default values.
93
     *
94
     * @param string $name         The name of asset
95
     * @param string $from         The from location, by default is the CSS files default location
96
     * @param array  $dependencies Array which contains the dependencies of the given asset, by default is empty
97
     * @param string $version      The version, by default is "1.0.0"
98
     * @param string $media        The media, by default is "all"
99
     *
100
     * @return $this Self class instance
101
     */
102
    protected function addStylesheet(
103
        $name,
104
        $from = AssetsInterface::CSS,
105
        array $dependencies = [],
106
        $version = '1.0.0',
107
        $media = 'all'
108
    ) {
109
        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...
110
111
        return $this;
112
    }
113
114
    /**
115
     * Registers the ajax urls inside given JS filename.
116
     *
117
     * @param string $name    The script file name
118
     * @param string $ajaxUrl The name that is going to expose in JS file as ajaxUrl
119
     *
120
     *      Usage example with name="subscribe" and ajaxUrl="subscribeAjax":
121
     *
122
     *          // subscribe.js
123
     *
124
     *          $.ajax({
125
     *              url: subscribeAjax.ajaxUrl,
126
     *              method: 'GET',
127
     *              data: {
128
     *                  action: 'ajax-action-registered-in-your-php-file',
129
     *              }
130
     *          }).done(function (response) {
131
     *             (...)
132
     *          });
133
     */
134
    protected function registerAjaxUrls($name, $ajaxUrl)
135
    {
136
        if (false === is_array($ajaxUrl)) {
137
            $ajaxUrl = [$ajaxUrl];
138
        }
139
        foreach ($ajaxUrl as $url) {
140
            wp_localize_script($name, $url, [
141
                'ajaxUrl' => admin_url('admin-ajax.php'),
142
            ]);
143
        }
144
    }
145
146
    /**
147
     * Build dynamically the asset directory path with the given parameters.
148
     *
149
     * @param string $from     The from location
150
     * @param string $name     The filename without extension
151
     * @param string $fileType The file type, by default is "js"
152
     *
153
     * @return string
154
     */
155
    private function path($from, $name, $fileType = 'js')
156
    {
157
        return get_template_directory_uri() . '/Resources/' . $from . '/' . $name . '.' . $fileType;
158
    }
159
160
    /**
161
     * Registers all the scripts and stylesheet files.
162
     * It's a callback of WordPress internal "wp_enqueue_scripts" method.
163
     *
164
     * @deprecated since version 1.5, will be removed in 2.0. Implement productionAssets and developmentAssets instead.
165
     */
166
    public function assets()
167
    {
168
    }
169
}
170