PluginProbe
WP Ultimate Post Grid / 2.8.2
WP Ultimate Post Grid v2.8.2
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / vendor / vafpress / classes / shortcodegenerator.php

shortcodegenerator.php in WP Ultimate Post Grid 2.8.2, at vendor/vafpress/classes/shortcodegenerator.php

377 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class VP_ShortcodeGenerator
4 {
5
6 public static $pool = array();
7
8 public $name;
9
10 public $template;
11
12 public $modal_title = '';
13
14 public $button_title = '';
15
16 public $main_image;
17
18 public $sprite_image;
19
20 public $types;
21
22 public $include_pages;
23
24 public function __construct($arr)
25 {
26 $this->main_image = VP_PUBLIC_URL . '/img/vp_shortcode_icon.png';
27 $this->sprite_image = VP_PUBLIC_URL . '/img/vp_shortcode_icon_sprite.png';
28 $this->types = array( 'post', 'page' );
29 $this->included_pages = array();
30
31 if (is_array($arr))
32 {
33 foreach ($arr as $n => $v)
34 {
35 $this->$n = $v;
36 }
37 if (empty($this->name)) die('Unique name required');
38 if (empty($this->template)) die('Template array / path required');
39 }
40
41 if( is_string($this->template) and is_file($this->template) )
42 $this->template = include $this->template;
43
44 if(!empty($this->template))
45 {
46 $this->normalize();
47 add_action( 'current_screen', array($this, 'init_mce_plugin') );
48 }
49
50 self::$pool[$this->name] = $this;
51 }
52
53 function init_mce_plugin()
54 {
55 if( $this->can_output() )
56 {
57 // print modal dialog dom
58 add_action( 'admin_footer', array($this, 'print_modal') );
59 // populate scripts and styles dependencies
60 $loader = VP_WP_Loader::instance();
61 $loader->add_types( $this->get_field_types(), 'shortcodegenerator' );
62 }
63 }
64
65 function normalize()
66 {
67 if(is_array($this->template)) foreach ($this->template as &$shortcode)
68 {
69 foreach ($shortcode['elements'] as &$elements)
70 {
71 if(isset($elements['attributes'])) foreach ($elements['attributes'] as &$f)
72 {
73 if( $f['type'] === 'codeeditor' )
74 {
75 $f['type'] = 'textarea';
76 }
77 }
78 }
79 }
80 }
81
82 function get_field_types()
83 {
84 $field_types = array();
85 if(is_array($this->template)) foreach ($this->template as $shortcode)
86 {
87 foreach ($shortcode['elements'] as $elements)
88 {
89 if(isset($elements['attributes'])) foreach ($elements['attributes'] as $f)
90 {
91 if( ! in_array($f['type'], $field_types) )
92 {
93 $field_types[] = $f['type'];
94 }
95 }
96 }
97 }
98 return $field_types;
99 }
100
101 public static function get_pool()
102 {
103 return self::$pool;
104 }
105
106 public static function pool_supports_editor()
107 {
108 foreach (self::$pool as $sg)
109 {
110 if( $sg->supports_editor() )
111 {
112 return true;
113 }
114 }
115 return false;
116 }
117
118 public function supports_editor()
119 {
120 $post_type = VP_Metabox::_get_current_post_type();
121 $has_editor = post_type_supports( $post_type, 'editor' );
122 return $has_editor;
123 }
124
125 public static function pool_can_output()
126 {
127 foreach (self::$pool as $sg)
128 {
129 if( $sg->can_output() )
130 {
131 return true;
132 }
133 }
134 return false;
135 }
136
137 public function can_output()
138 {
139 $screen = '';
140 $can = true;
141 if( function_exists('get_current_screen') )
142 {
143 $screen = get_current_screen();
144 if( !is_null($screen) )
145 $screen = $screen->id;
146 }
147
148 // if in post / page
149 if( VP_Metabox::_is_post_or_page() )
150 {
151 // then consider the types
152 if( !in_array("*", $this->types) ) // if wildcard exists, then always shows
153 $can &= in_array(VP_Metabox::_get_current_post_type(), $this->types);
154 else
155 $can &= true;
156 }
157 else
158 {
159 // if not, only consider the screen id
160 if( !empty($screen) )
161 {
162 $can &= in_array($screen, $this->included_pages);
163 }
164 else
165 {
166 if( !is_admin() )
167 {
168 $can &= false;
169 }
170 }
171 }
172
173 return $can;
174 }
175
176 public function print_modal()
177 {
178 $modal_id = $this->name . '_modal';
179 ?>
180 <div id="<?php echo $modal_id; ?>" class="vp-sc-dialog reveal-modal xlarge">
181 <h1><?php echo $this->modal_title; ?></h1>
182 <div class="vp-sc-scroll-container">
183 <div class="vp-sc-wrapper">
184 <ul class="vp-sc-menu">
185 <?php foreach ($this->template as $title => $menu): ?>
186 <?php if(reset($this->template) == $menu): ?>
187 <li class="current"><a href="#<?php echo str_replace(' ', '_', $title); ?>"><?php echo $title ?></li></a>
188 <?php else: ?>
189 <li><a href="#<?php echo str_replace(' ', '_', $title); ?>"><?php echo $title ?></li></a>
190 <?php endif; ?>
191 <?php endforeach; ?>
192 </ul>
193 <div class="vp-sc-main">
194 <?php
195 $id_counter = 1;
196 foreach ($this->template as $title => $menu):
197 ?>
198 <?php if (reset($this->template) == $menu) : ?>
199 <ul class="current vp-sc-sub-menu-list vp-sc-sub-menu-<?php echo str_replace(' ', '_', $title); ?>">
200 <?php else : ?>
201 <ul class="vp-hide vp-sc-sub-menu-list vp-sc-sub-menu-<?php echo str_replace(' ', '_', $title); ?>">
202 <?php endif; ?>
203 <?php foreach ($menu['elements'] as $name => $element): ?>
204 <li id="vafpress-element-<?php echo $id_counter; $id_counter++; ?>" class="vp-sc-element postbox<?php if(isset($element['attributes'])) echo ' has-options'; ?><?php if(isset($element['active']) && $element['active'] == true) echo ' active'; ?>">
205 <h3 class="hndle vp-sc-element-heading">
206 <a href="#">
207 <?php echo $element['title']; ?>
208 <?php if(isset($element['attributes'])) echo '<i class="fa fa-arrow-down"></i>'; ?>
209 </a>
210 </h3>
211 <div class="hidden vp-sc-code"><?php echo htmlentities($element['code']); ?></div>
212 <?php if(isset($element['attributes']) and !empty($element['attributes'])): ?>
213 <form class="vp-sc-element-form <?php if(!isset($element['active']) || isset($element['active']) && $element['active'] == false):?>vp-hide<?php endif; ?> inside">
214 <?php echo $this->print_form($element['attributes']); ?>
215 </form>
216 <?php endif; ?>
217 </li>
218 <?php endforeach; ?>
219 </ul>
220 <?php endforeach; ?>
221 </div>
222 </div>
223 <a class="close-reveal-modal">&#215;</a>
224 </div>
225 </div>
226 <?php
227 }
228
229 function print_form($attributes)
230 {
231 ?>
232 <div class="vp-sc-fields">
233 <?php
234 foreach ($attributes as $attr)
235 {
236 // create the object
237 $make = VP_Util_Reflection::field_class_from_type($attr['type']);
238 // prefix name
239 $attr['name'] = '_' . $attr['name'];
240 $field = call_user_func("$make::withArray", $attr);
241 $default = $field->get_default();
242 if(!is_null($default))
243 $field->set_value($default);
244 ?>
245
246 <?php if($attr['type'] !== 'notebox'): ?>
247 <div class="vp-sc-field vp-<?php echo $attr['type']; ?>" data-vp-type="vp-<?php echo $attr['type']; ?>">
248 <div class="label"><label><?php echo $attr['label']; ?></label></div>
249 <div class="field"><div class="input"><?php echo $field->render(true); ?></div></div>
250 </div>
251 <?php else: ?>
252 <?php $status = isset($attr['status']) ? $attr['status'] : 'normal'; ?>
253 <div class="vp-sc-field vp-<?php echo $attr['type']; ?> note-<?php echo $status; ?>" data-vp-type="vp-<?php echo $attr['type']; ?>">
254 <?php echo $field->render(true); ?>
255 </div>
256 <?php endif; ?>
257
258 <?php
259 }
260 ?>
261 </div>
262 <div class="vp-sc-action">
263 <button class="vp-sc-insert button"><?php _e('Insert', 'wp-ultimate-recipe'); ?></button>
264 <button class="vp-sc-cancel button"><?php _e('Cancel', 'wp-ultimate-recipe') ?></button>
265 </div>
266 <?php
267 }
268
269 public static function build_localize()
270 {
271 $localize = array();
272 foreach (self::$pool as $sg)
273 {
274 $localize[] = array(
275 'name' => $sg->name,
276 'modal_title' => $sg->modal_title,
277 'button_title' => $sg->button_title,
278 'main_image' => $sg->main_image,
279 'sprite_image' => $sg->sprite_image,
280 );
281 }
282 return $localize;
283 }
284
285 public static function init_buttons()
286 {
287 if( VP_Metabox::_is_post_or_page() && !current_user_can( 'edit_posts' ) &&
288 !current_user_can( 'edit_pages' ) && get_user_option( 'rich_editing' ) == 'true')
289 return;
290
291 add_filter( 'mce_external_plugins' , array(__CLASS__, 'add_buttons') );
292 add_filter( 'mce_buttons' , array(__CLASS__, 'register_buttons') );
293 add_filter( 'wp_fullscreen_buttons', array(__CLASS__, 'fullscreen_buttons') );
294 add_filter( 'admin_print_styles' , array(__CLASS__, 'print_styles') );
295 }
296
297 public static function print_styles($buttons)
298 {
299 ?>
300 <style type="text/css">
301 <?php foreach (self::$pool as $sg): ?>
302 #qt_content_<?php echo $sg->name; ?>{
303 background: url('<?php echo $sg->sprite_image; ?>') 2px -21px no-repeat !important;
304 text-indent: -999px;
305 }
306 span.mce_<?php echo $sg->name; ?>{
307 background: url('<?php echo $sg->sprite_image; ?>') 0 0 no-repeat !important;
308 }
309 <?php endforeach; ?>
310 </style>
311 <?php
312 }
313
314 public static function register_buttons($buttons)
315 {
316 $vp_buttons = array();
317 foreach (self::$pool as $sg)
318 {
319 if( $sg->can_output() )
320 $vp_buttons[] = $sg->name;
321 }
322 $buttons = array_merge($buttons, $vp_buttons);
323 return $buttons;
324 }
325
326 public static function add_buttons($plugin_array)
327 {
328 $plugin_array['vp_sc_button'] = VP_PUBLIC_URL .'/js/shortcodes.js';
329 foreach (self::$pool as $sg)
330 {
331 if( $sg->can_output() )
332 $plugin_array[$sg->name] = VP_PUBLIC_URL .'/js/dummy.js';
333 }
334 return $plugin_array;
335 }
336
337 public static function fullscreen_buttons($buttons)
338 {
339 foreach (self::$pool as $sg)
340 {
341 if( $sg->can_output() )
342 {
343 // add a separator
344 $buttons[] = 'separator';
345 // format: title, onclick, show in both editors
346 $buttons[$sg->name] = array(
347 // Title of the button
348 'title' => $sg->button_title,
349 // Command to execute
350 'onclick' => "tinyMCE.execCommand('{$sg->name}_cmd');",
351 // Show on visual AND html mode
352 'both' => true
353 );
354 }
355 }
356 return $buttons;
357 }
358
359 public function get_shortcode_tags() {
360 $shortcodes = $this->template;
361 $tags = array();
362 foreach ($shortcodes as $menu) {
363 foreach ($menu['elements'] as $sc) {
364 $code = $sc['code'];
365 preg_match('/\[(\w+).*\]/', $code, $matches);
366 $tags[] = $matches[1];
367 }
368 }
369 return $tags;
370 }
371
372 }
373
374 /**
375 * EOF
376 */
377