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']); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|