-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-agsgShortcode.php
255 lines (238 loc) · 10.5 KB
/
class-agsgShortcode.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* Class agsgShortcode
* Abstract class to interface and is implemented by all shortcode concrete classes*
* You can extend this class to override the functions, please do not directly edit it, if you do make SURE you save a copy.
* Product Class - Defines a base class for the type of product were going to be making
* @package WordPress
* @subpackage AGSG
*/
abstract class agsgShortcode
{
public $shortcode_code;
public $type;
public $name;
public $kind;
public $tag;
public $htmlstg;
public $htmletg;
public $example;
public $description;
public $exists;
public $filename;
public $html_id_OR;
public $htmlTagOR;
public $shortcodes_atts_str; // contains all attributes for this shortcode
public $shortcode_atts; // contains all attributes for this shortcode
public $error;
public $error_msg;
public $preview;
public $regenerate;
public $conditions;
public $scripts;
public $styles;
public $att_names;
/**
* Build all conditions *
*/
public function buildConditions()
{
if (count($this->conditions)) {
foreach ($this->conditions as $condition) {
$type = $condition["type"];
$operator = $condition["operator"];
$attribute = 'self::$a[\'' . $condition["attribute"] . '\']';
$value = $condition["value"];
$tinyMCE = $condition["tinyMCE"];
// parse tiny mce content and find references to attributes
foreach ($this->att_names as $att_name) {
// if attributes exist
if (strpos($tinyMCE, '<<' . $att_name . '>>') || strpos($tinyMCE, '<<' . $att_name . '>>')) {
// get an array of just attributes to work with
preg_match('/(\<\;\<\;[a-z_0-9]+\>\;\>\;)/', $tinyMCE, $matches);
// cycle through each, create var = value string, and add to array
if (is_array($matches)) {
foreach ($matches as $iv) {
$iv = preg_replace('/(\<\;\<\;[a-z_0-9]+\>\;\>\;)/', 'self::$a[\'' . $att_name . '\']', $iv);
$ref_atts[] = "$$att_name = $iv";
}
} else {
// get an array of just attributes to work with
preg_match('/(<<[a-z_0-9]+>>)/', $tinyMCE, $matches);
// cycle through each, create var = value string, and add to array
if (is_array($matches)) {
foreach ($matches as $iv) {
$iv = preg_replace('/(<<[a-z_0-9]+>>)/', 'self::$a[\'' . $att_name . '\']', $iv);
$ref_atts[] = "$$att_name = $iv";
}
}
}
}
// replace original with the att var generated
$tinyMCE = str_replace('<<' . $att_name . '>>', "$$att_name", $tinyMCE);
}
if (isset($ref_atts) && is_array($ref_atts)) {
$ref_atts = array_unique($ref_atts);
} else {
$ref_atts = array();
}
if ($value === true || $value === false || $value === 0 || $value === 1) {
} else {
$value = "'$value'";
}
$this->shortcode_code .= <<<STRING
$type ( $attribute $operator $value ){
STRING;
foreach ($ref_atts as $ref_att) {
$this->shortcode_code .= <<<STRING
$ref_att;
STRING;
}
$this->shortcode_code .= <<<'VARSTR'
$var .=
VARSTR;
$tinyMCE = preg_replace("#<script>(.*?)</script>#is", "", $tinyMCE);
$tinyMCE = str_replace('"', "'", $tinyMCE);
$tinyMCE = preg_replace("(\\\\')", "'", $tinyMCE);
$this->shortcode_code .= <<<STRING
"$tinyMCE";
STRING;
$this->_buildInternalScriptFunction($condition['tinyMCE']); // adds closure functions for scripts and ending bracket for condition
}
}
}
/**
* Build all styles if there any
*/
public function buildStyles()
{
// check for and add external css
if (is_array($this->styles) && count($this->styles)) {
foreach ($this->styles as $style) {
$css_handle = '"' . $style['handle'] . '"';
$css_src = '"' . $style["src"] . '"';
if (isset($style["deps"]) && $style["deps"] !== '') {
$css_deps = explode(',', $style["deps"]);
$css_deps_str = 'array( ';
// build array string
foreach ($css_deps as $css_dep) {
$css_deps_str .= "'$css_dep'";
}
$css_deps_str .= ' )';
}else{
$css_deps_str = 'array()';
}
$css_ver = '"' . $style["ver"] . '"';
$css_media = '"' . $style["media"] . '"';
$this->shortcode_code .= <<<STRING
wp_enqueue_style( $css_handle, $css_src, $css_deps_str, $css_ver, $css_media );
STRING;
}
}
}
/**
* Build all scritps if there are any
*/
public function buildScripts()
{
// check for and add external js
if (is_array($this->scripts) && count($this->scripts)) {
foreach ($this->scripts as $script) {
$js_handle = '"' . $script['handle'] . '"';
$js_src = '"' . $script["src"] . '"';
if (isset($script["deps"]) && $script["deps"] !== '') {
$js_deps = explode(',', $script["deps"]);
$js_deps_str = 'array( ';
// build array string
foreach ($js_deps as $js_dep) {
$js_deps_str .= "'$js_dep'";
}
$js_deps_str .= ' )';
} else {
$js_deps_str = 'array()';
}
$js_ver = '"' . $script["ver"] . '"';
$this->shortcode_code .= <<<STRING
wp_enqueue_script( $js_handle, $js_src, $js_deps_str, $js_ver, true );
STRING;
}
}
}
/**
* Build internal scripts if there are any
* Should be processed at the end after addShortcode
* @param $tinyMCE - Raw data from a tinyMCE to parse
* @param $var_string - PHP variables in the use() part of closure
*/
private function _buildInternalScriptFunction($tinyMCE)
{
// find all script tags
$count = preg_match('#<script>(.*?)</script>#is', $tinyMCE, $intscripts);
// get everything between any script tags storing them each in index in intscripts array and how many in count
if ($count) {
for ($i = 0; $i < $count; $i++) {
$intscript = str_replace('\"', "'", $intscripts[$i]);
// parse $intscript and find references to attributes
foreach ($this->att_names as $att_name) {
// if attributes exist
if (strpos($intscript, '<<' . $att_name . '>>') || strpos($intscript, '<<' . $att_name . '>>')) {
// get an array of just attributes to work with
preg_match('/(<<[a-z_0-9]+>>)/', $intscript, $matches);
// cycle through each, create var = value string, and add to array
if (is_array($matches)) {
foreach ($matches as $iv) {
$iv = preg_replace('/(<<[a-z_0-9]+>>)/', '$' . $att_name, $iv);
$script_ref_use_atts[] = "$iv";
}
} else {
// get an array of just attributes to work with
preg_match('/(\<\;\<\;[a-z_0-9]+\>\;\>\;)/', $tinyMCE, $matches);
// cycle through each, create var = value string, and add to array
if (is_array($matches)) {
foreach ($matches as $iv) {
$iv = preg_replace('/(\<\;\<\;[a-z_0-9]+\>\;\>\;)/', '$' . $att_name, $iv);
$script_ref_use_atts[] = "$iv";
}
}
}
}
// replace original with the att var generated
$intscript = str_replace('<<' . $att_name . '>>', "<?php echo $$att_name; ?>", $intscript);
$intscript = str_replace('<<' . $att_name . '>>', "<?php echo $$att_name; ?>", $intscript);
}
$var_string = '';
if (isset($script_ref_use_atts) && is_array($script_ref_use_atts)) {
$script_ref_use_atts = array_unique($script_ref_use_atts);
for ($j = 0; $j < count($script_ref_use_atts) * 2; $j = $j + 2) {
if ($j === 0) {
$var_string = $script_ref_use_atts[$j];
} else if ($j % 2 == 0) {
$var_string .= ', ' . $script_ref_use_atts[$j];
}
}
}
$this->shortcode_code .= <<<'VARSTR'
$cb =
VARSTR;
$this->shortcode_code .= <<<STRING
function () use ( $var_string ) {
STRING;
$this->shortcode_code .= <<<STRING
?>
$intscript
<?php
}; // end closure function
STRING;
$this->shortcode_code .= <<<STRING
add_action( 'wp_footer',
STRING;
$this->shortcode_code .= <<<'VARSTR'
$cb );
} // end condition
VARSTR;
}
}
}
abstract public function generateExample();
abstract public function generatePreview();
}