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