forked from florincatalin/AddScriptPluginTypesetter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOutput.php
154 lines (123 loc) · 4.46 KB
/
Output.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
*
* AddScript plugin for Typesetter CMS
* Authors: Florin-Cătălin Tofan, Jürgen Krausz
* Copyright (c) 2019 Florin-Cătălin Tofan
* License: MIT
*
*/
namespace Addon\AddScript;
defined('is_running') or die('Not an entry point...');
class Output extends \Addon\AddScript\Common
{
static function GetHead() {
global $page;
// msg('$page = ' . pre(get_object_vars($page)));
\gp\tool\Plugins::css('css/user.css');
self::GetGlobalScripts();
foreach( self::$global_scripts as $script_id => $script ){
if( !isset($script['load_in']) || !isset($script['code']) ){
continue;
}
$data_attr = ' data-addscript-global="' . htmlspecialchars($script_id) . '"';
switch( $script['type'] ){
case 'url':
$script_url = $script['code'];
switch( $script['load_in'] ){
case 'head':
$page->head .= "<script\n" . ' src="' . $script_url . '"' // "<script\n" will prevent moving the script into body
. ( isset($script['attrs']) && in_array('async', $script['attrs']) ? ' async="async"' : '' )
. ( isset($script['attrs']) && in_array('defer', $script['attrs']) ? ' defer="defer"' : '' )
. $data_attr . '></script>';
break;
case 'body':
$page->head .= '<script src="' . $script_url . '"'
. ( isset($script['attrs']) && in_array('async', $script['attrs']) ? ' async="async"' : '' )
. ( isset($script['attrs']) && in_array('defer', $script['attrs']) ? ' defer="defer"' : '' )
. $data_attr . '></script>';
break;
case 'jQuery':
$page->jQueryCode .= "\n/* AddScript global [" . htmlspecialchars($script_id) . "] START */\n"
. '$.getScript("' . $script_url . '");'
. "\n/* AddScript END */\n";
break;
case 'winLoad':
$page->head_script .= "\n/* AddScript global [" . htmlspecialchars($script_id) . "] START */\n"
. '$(window).on("load", function(){' . "\n"
. ' $.getScript("' . $script_url . '");' . "\n"
. '});'
. "\n/* AddScript global END */\n";
break;
}
break;
case 'js':
switch( $script['load_in'] ){
case 'head':
$page->head .= "<script\n" . $data_attr . '>' // "<script\n" will prevent moving the script into body
. $script['code']
. '</script>';
break;
case 'body':
$page->head_script .= "\n/* AddScript global [" . htmlspecialchars($script_id) . "] START */\n"
. $script['code']
. "\n/* AddScript global END */\n";
break;
case 'jQuery':
$page->jQueryCode .= "\n/* AddScript global [" . htmlspecialchars($script_id) . "] START */\n"
. $script['code']
. "\n/* AddScript global END */\n";
break;
case 'winLoad':
$page->head_script .= "\n/* AddScript global [" . htmlspecialchars($script_id) . "] START */\n"
. '$(window).on("load", function(){' . "\n"
. $script['code'] . "\n"
. '});'
. "\n/* AddScript global END */\n";
break;
}
break;
}
}
if( \gp\tool::LoggedIn() ){
\gp\tool\Plugins::css('css/admin.css', false);
}
}
static function SectionTypes($section_types) {
$section_types[self::$sectionType] = array();
$section_types[self::$sectionType]['label'] = 'AddScript';
return $section_types;
}
static function SectionToContent($section_data) {
global $page;
if( empty($section_data['script']) ){
return $section_data;
}
switch( $section_data['script_type'] ){
case 'jQuery':
$page->jQueryCode .= "\n/* AddScript section START */\n"
. $section_data['script']
. "\n/* AddScript section END */\n";
break;
case 'js':
$page->head_script .= "\n/* AddScript section START */\n"
. $section_data['script']
. "\n/* AddScript section END */\n";
break;
case 'url':
$page->head .= "\n<!-- AddScript section START -->\n"
. "<script\n" . ' src="' . $section_data['script'] . '"' // "<script\n" will prevent moving the script into body
. ( isset($section_data['script_attrs']) && in_array('async', $section_data['script_attrs']) ? ' async="async"' : '' )
. ( isset($section_data['script_attrs']) && in_array('defer', $section_data['script_attrs']) ? ' defer="defer"' : '' )
. '></script>'
. "\n<!-- AddScript section END -->\n";
break;
case 'raw':
$section_data['content'] = "<!-- AddScript Section Start -->\n"
. $section_data['script']
. "\n<!-- AddScript section END -->";
break;
}
return $section_data;
}
}