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

Assets   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 78
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
developmentAssets() 0 1 ?
productionAssets() 0 1 ?
adminAssets() 0 1 ?
A __construct() 0 8 3
A addScript() 0 16 2
A addStylesheet() 0 11 1
A registerAjaxUrls() 0 11 3
A path() 0 4 1
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
declare(strict_types=1);
13
14
namespace LIN3S\WPFoundation;
15
16
/**
17
 * @author Beñat Espiña <[email protected]>
18
 * @author Gorka Laucirica <[email protected]>
19
 */
20
abstract class Assets
21
{
22
    abstract public function developmentAssets() : void;
23
24
    abstract public function productionAssets() : void;
25
26
    abstract public function adminAssets() : void;
27
28
    public function __construct()
29
    {
30
        defined('WP_DEBUG') && true === WP_DEBUG
31
            ? add_action('wp_enqueue_scripts', [$this, 'developmentAssets'])
32
            : add_action('wp_enqueue_scripts', [$this, 'productionAssets']);
33
34
        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...
35
    }
36
37
    protected function addScript(
38
        string $name,
39
        string $from,
40
        array $dependencies = ['jquery'],
41
        string $version = '1.0.0',
42
        bool $inFooter = true,
43
        ?string $ajaxUrl = null
44
    ) {
45
        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...
46
47
        if (null !== $ajaxUrl) {
48
            $this->registerAjaxUrls($name, $ajaxUrl);
49
        }
50
51
        return $this;
52
    }
53
54
    protected function addStylesheet(
55
        string $name,
56
        string $from,
57
        array $dependencies = [],
58
        string $version = '1.0.0',
59
        string $media = 'all'
60
    ) {
61
        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...
62
63
        return $this;
64
    }
65
66
    /*
67
     *  Usage example with name="subscribe" and ajaxUrl="subscribeAjax":
68
     *
69
     *      // subscribe.js
70
     *
71
     *      $.ajax({
72
     *          url: subscribeAjax.ajaxUrl,
73
     *          method: 'GET',
74
     *          data: {
75
     *              action: 'ajax-action-registered-in-your-php-file',
76
     *          }
77
     *      }).done(function (response) {
78
     *         (...)
79
     *      });
80
     */
81
    protected function registerAjaxUrls(string $name, string $ajaxUrl) : void
82
    {
83
        if (false === is_array($ajaxUrl)) {
84
            $ajaxUrl = [$ajaxUrl];
85
        }
86
        foreach ($ajaxUrl as $url) {
87
            wp_localize_script($name, $url, [
88
                'ajaxUrl' => admin_url('admin-ajax.php'),
89
            ]);
90
        }
91
    }
92
93
    private function path(string $from, string $name, string $fileType = 'js') : string
94
    {
95
        return get_template_directory_uri() . '/Resources/' . $from . '/' . $name . '.' . $fileType;
96
    }
97
}
98