-
Notifications
You must be signed in to change notification settings - Fork 98
/
shortcodegenerator.php
372 lines (333 loc) · 9.45 KB
/
shortcodegenerator.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
class VP_ShortcodeGenerator
{
public static $pool = array();
public $name;
public $template;
public $modal_title = '';
public $button_title = '';
public $main_image;
public $sprite_image;
public $types;
public $include_pages;
public function __construct($arr)
{
$this->main_image = VP_PUBLIC_URL . '/img/vp_shortcode_icon.png';
$this->sprite_image = VP_PUBLIC_URL . '/img/vp_shortcode_icon_sprite.png';
$this->types = array( 'post', 'page' );
$this->included_pages = array();
if (is_array($arr))
{
foreach ($arr as $n => $v)
{
$this->$n = $v;
}
if (empty($this->name)) die('Unique name required');
if (empty($this->template)) die('Template array / path required');
}
if( is_string($this->template) and is_file($this->template) )
$this->template = include $this->template;
if(!empty($this->template))
{
$this->normalize();
add_action( 'current_screen', array($this, 'init_mce_plugin') );
}
self::$pool[$this->name] = $this;
}
function init_mce_plugin()
{
if( $this->can_output() )
{
// print modal dialog dom
add_action( 'admin_footer', array($this, 'print_modal') );
// populate scripts and styles dependencies
$loader = VP_WP_Loader::instance();
$loader->add_types( $this->get_field_types(), 'shortcodegenerator' );
}
}
function normalize()
{
if(is_array($this->template)) foreach ($this->template as &$shortcode)
{
foreach ($shortcode['elements'] as &$elements)
{
if(isset($elements['attributes'])) foreach ($elements['attributes'] as &$f)
{
if( $f['type'] === 'codeeditor' )
{
$f['type'] = 'textarea';
}
}
}
}
}
function get_field_types()
{
$field_types = array();
if(is_array($this->template)) foreach ($this->template as $shortcode)
{
foreach ($shortcode['elements'] as $elements)
{
if(isset($elements['attributes'])) foreach ($elements['attributes'] as $f)
{
if( ! in_array($f['type'], $field_types) )
{
$field_types[] = $f['type'];
}
}
}
}
return $field_types;
}
public static function get_pool()
{
return self::$pool;
}
public static function pool_supports_editor()
{
foreach (self::$pool as $sg)
{
if( $sg->supports_editor() )
{
return true;
}
}
return false;
}
public function supports_editor()
{
$post_type = VP_Metabox::_get_current_post_type();
$has_editor = post_type_supports( $post_type, 'editor' );
return $has_editor;
}
public static function pool_can_output()
{
foreach (self::$pool as $sg)
{
if( $sg->can_output() )
{
return true;
}
}
return false;
}
public function can_output()
{
$screen = '';
$can = true;
if( function_exists('get_current_screen') )
{
$screen = get_current_screen();
if( !is_null($screen) )
$screen = $screen->id;
}
// if in post / page
if( VP_Metabox::_is_post_or_page() )
{
// then consider the types
if( !in_array("*", $this->types) ) // if wildcard exists, then always shows
$can &= in_array(VP_Metabox::_get_current_post_type(), $this->types);
else
$can &= true;
}
else
{
// if not, only consider the screen id
if( !empty($screen) )
{
$can &= in_array($screen, $this->included_pages);
}
else
{
if( !is_admin() )
{
$can &= false;
}
}
}
return $can;
}
public function print_modal()
{
$modal_id = $this->name . '_modal';
?>
<div id="<?php echo $modal_id; ?>" class="vp-sc-dialog reveal-modal xlarge">
<h1><?php echo $this->modal_title; ?></h1>
<div class="vp-sc-scroll-container">
<div class="vp-sc-wrapper">
<ul class="vp-sc-menu">
<?php foreach ($this->template as $title => $menu): ?>
<?php if(reset($this->template) == $menu): ?>
<li class="current"><a href="#<?php echo str_replace(' ', '_', $title); ?>"><?php echo $title ?></li></a>
<?php else: ?>
<li><a href="#<?php echo str_replace(' ', '_', $title); ?>"><?php echo $title ?></li></a>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<div class="vp-sc-main">
<?php foreach ($this->template as $title => $menu): ?>
<?php if (reset($this->template) == $menu) : ?>
<ul class="current vp-sc-sub-menu-list vp-sc-sub-menu-<?php echo str_replace(' ', '_', $title); ?>">
<?php else : ?>
<ul class="vp-hide vp-sc-sub-menu-list vp-sc-sub-menu-<?php echo str_replace(' ', '_', $title); ?>">
<?php endif; ?>
<?php foreach ($menu['elements'] as $name => $element): ?>
<li class="vp-sc-element postbox<?php if(isset($element['attributes'])) echo ' has-options'; ?><?php if(isset($element['active']) && $element['active'] == true) echo ' active'; ?>">
<h3 class="hndle vp-sc-element-heading">
<a href="#">
<?php echo $element['title']; ?>
<?php if(isset($element['attributes'])) echo '<i class="fa fa-arrow-down"></i>'; ?>
</a>
</h3>
<div class="hidden vp-sc-code"><?php echo htmlentities($element['code']); ?></div>
<?php if(isset($element['attributes']) and !empty($element['attributes'])): ?>
<form class="vp-sc-element-form <?php if(!isset($element['active']) || isset($element['active']) && $element['active'] == false):?>vp-hide<?php endif; ?> inside">
<?php echo $this->print_form($element['attributes']); ?>
</form>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</div>
</div>
<a class="close-reveal-modal">×</a>
</div>
</div>
<?php
}
function print_form($attributes)
{
?>
<div class="vp-sc-fields">
<?php
foreach ($attributes as $attr)
{
// create the object
$make = VP_Util_Reflection::field_class_from_type($attr['type']);
// prefix name
$attr['name'] = '_' . $attr['name'];
$field = call_user_func("$make::withArray", $attr);
$default = $field->get_default();
if(!is_null($default))
$field->set_value($default);
?>
<?php if($attr['type'] !== 'notebox'): ?>
<div class="vp-sc-field vp-<?php echo $attr['type']; ?>" data-vp-type="vp-<?php echo $attr['type']; ?>">
<div class="label"><label><?php echo $attr['label']; ?></label></div>
<div class="field"><div class="input"><?php echo $field->render(true); ?></div></div>
</div>
<?php else: ?>
<?php $status = isset($attr['status']) ? $attr['status'] : 'normal'; ?>
<div class="vp-sc-field vp-<?php echo $attr['type']; ?> note-<?php echo $status; ?>" data-vp-type="vp-<?php echo $attr['type']; ?>">
<?php echo $field->render(true); ?>
</div>
<?php endif; ?>
<?php
}
?>
</div>
<div class="vp-sc-action">
<button class="vp-sc-insert button"><?php _e('Insert', 'vp_textdomain'); ?></button>
<button class="vp-sc-cancel button"><?php _e('Cancel', 'vp_textdomain') ?></button>
</div>
<?php
}
public static function build_localize()
{
$localize = array();
foreach (self::$pool as $sg)
{
$localize[] = array(
'name' => $sg->name,
'modal_title' => $sg->modal_title,
'button_title' => $sg->button_title,
'main_image' => $sg->main_image,
'sprite_image' => $sg->sprite_image,
);
}
return $localize;
}
public static function init_buttons()
{
if( VP_Metabox::_is_post_or_page() && !current_user_can( 'edit_posts' ) &&
!current_user_can( 'edit_pages' ) && get_user_option( 'rich_editing' ) == 'true')
return;
add_filter( 'mce_external_plugins' , array(__CLASS__, 'add_buttons') );
add_filter( 'mce_buttons' , array(__CLASS__, 'register_buttons') );
add_filter( 'wp_fullscreen_buttons', array(__CLASS__, 'fullscreen_buttons') );
add_filter( 'admin_print_styles' , array(__CLASS__, 'print_styles') );
}
public static function print_styles($buttons)
{
?>
<style type="text/css">
<?php foreach (self::$pool as $sg): ?>
#qt_content_<?php echo $sg->name; ?>{
background: url('<?php echo $sg->sprite_image; ?>') 2px -21px no-repeat !important;
text-indent: -999px;
}
span.mce_<?php echo $sg->name; ?>{
background: url('<?php echo $sg->sprite_image; ?>') 0 0 no-repeat !important;
}
<?php endforeach; ?>
</style>
<?php
}
public static function register_buttons($buttons)
{
foreach (self::$pool as $sg)
{
if( $sg->can_output() )
$vp_buttons[] = $sg->name;
}
$buttons = array_merge($buttons, $vp_buttons);
return $buttons;
}
public static function add_buttons($plugin_array)
{
$plugin_array['vp_sc_button'] = VP_PUBLIC_URL .'/js/shortcodes.js';
foreach (self::$pool as $sg)
{
if( $sg->can_output() )
$plugin_array[$sg->name] = VP_PUBLIC_URL .'/js/dummy.js';
}
return $plugin_array;
}
public static function fullscreen_buttons($buttons)
{
foreach (self::$pool as $sg)
{
if( $sg->can_output() )
{
// add a separator
$buttons[] = 'separator';
// format: title, onclick, show in both editors
$buttons[$sg->name] = array(
// Title of the button
'title' => $sg->button_title,
// Command to execute
'onclick' => "tinyMCE.execCommand('{$sg->name}_cmd');",
// Show on visual AND html mode
'both' => true
);
}
}
return $buttons;
}
public function get_shortcode_tags() {
$shortcodes = $this->template;
$tags = array();
foreach ($shortcodes as $menu) {
foreach ($menu['elements'] as $sc) {
$code = $sc['code'];
preg_match('/\[(\w+).*\]/', $code, $matches);
$tags[] = $matches[1];
}
}
return $tags;
}
}
/**
* EOF
*/