3 namespace wcf\system\template\plugin
;
5 use wcf\system\exception\SystemException
;
6 use wcf\system\request\RequestHandler
;
7 use wcf\system\template\TemplateEngine
;
9 use wcf\util\StringUtil
;
12 * Template function plugin which generates script tags. File extension is automatically
13 * added to the script source and MUST NOT be provided.
15 * If ENABLE_DEBUG_MODE=0 then the extension is '.min.js', don't fail to provide it.
17 * The option VISITOR_USE_TINY_BUILD enables a specialized build, that is designed to
18 * provide smaller builds for visitors in order to decrease the overall payload and
19 * reduce page load time. Supporting them is optional and can be supplied by setting
20 * `hasTiny=true`, the extension is assumed to be `.tiny.min.js`.
23 * {js application='wbb' file='WBB'}
24 * http://example.com/js/WBB.js
26 * {js application='wcf' file='WCF.User' bundle='WCF.Combined'}
27 * http://example.com/wcf/js/WCF.User.js (ENABLE_DEBUG_MODE=1)
28 * http://example.com/wcf/js/WCF.Combined.min.js (ENABLE_DEBUG_MODE=0)
30 * {js application='wcf' lib='jquery'}
31 * http://example.com/wcf/js/3rdParty/jquery.js
33 * {js application='wcf' lib='jquery-ui' file='awesomeWidget'}
34 * http://example.com/wcf/js/3rdParty/jquery-ui/awesomeWidget.js
36 * {js application='wcf' file='WCF.User' bundle='WCF.Combined' hasTiny=true}
37 * http://example.com/wcf/js/WCF.User.js (ENABLE_DEBUG_MODE=1)
38 * http://example.com/wcf/js/WCF.Combined.min.js (ENABLE_DEBUG_MODE=0)
39 * http://example.com/wcf/js/WCF.Combined.tiny.min.js (ENABLE_DEBUG_MODE=0 && VISITOR_USE_TINY_BUILD=1)
41 * @author Alexander Ebert
42 * @copyright 2001-2019 WoltLab GmbH
43 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
46 class JsFunctionTemplatePlugin
implements IFunctionTemplatePlugin
49 * list of already included JavaScript files
52 protected $includedFiles = [];
57 public function execute($tagArgs, TemplateEngine
$tplObj)
59 // needed arguments: application and lib/file
60 if (empty($tagArgs['application'])) {
61 throw new SystemException("missing 'application' argument in js tag");
63 if (empty($tagArgs['file']) && empty($tagArgs['lib'])) {
64 throw new SystemException("missing 'file' or 'lib' argument in js tag");
69 isset($tagArgs['lib'])
70 && ($tagArgs['lib'] === 'jquery' ||
$tagArgs['lib'] === 'jquery-ui')
71 && empty($tagArgs['file'])
73 $tagArgs['bundle'] = '';
77 $src = WCF
::getPath($tagArgs['application']) . (isset($tagArgs['acp']) && $tagArgs['acp'] === 'true' ?
'acp/' : '') . 'js/';
78 if (!empty($tagArgs['bundle']) && !ENABLE_DEBUG_MODE
) {
79 $src .= $tagArgs['bundle'];
80 } elseif (!empty($tagArgs['lib'])) {
82 $src .= ENABLE_DEBUG_MODE ?
'3rdParty/' . $tagArgs['lib'] : 'WCF.Combined';
84 $src .= '3rdParty/' . $tagArgs['lib'];
85 if (!empty($tagArgs['file'])) {
86 $src .= '/' . $tagArgs['file'];
90 $src .= $tagArgs['file'];
93 if (isset($this->includedFiles
[$src])) {
97 $this->includedFiles
[$src] = true;
98 if (!ENABLE_DEBUG_MODE
) {
100 \
defined('VISITOR_USE_TINY_BUILD')
101 && VISITOR_USE_TINY_BUILD
102 && !WCF
::getUser()->userID
103 && !empty($tagArgs['hasTiny'])
110 $src .= '.js?v=' . LAST_UPDATE_TIME
;
112 $relocate = !RequestHandler
::getInstance()->isACPRequest() && (!isset($tagArgs['core']) ||
$tagArgs['core'] !== 'true');
113 $html = '<script' . ($relocate ?
' data-relocate="true"' : '') . ' data-cfasync="false" src="' . $src . '"></script>' . "\n";
115 if (isset($tagArgs['encodeJs']) && $tagArgs['encodeJs'] === 'true') {
116 $html = StringUtil
::encodeJS($html);