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']); |
|
|
|
|
24
|
|
|
} else { |
25
|
|
|
add_action('wp_enqueue_scripts', [$this, 'productionAssets']); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
add_action('admin_enqueue_scripts', [$this, 'adminAssets']); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|