| 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 foreach ($this->template as $title => $menu): ?> |
| 195 |
<?php if (reset($this->template) == $menu) : ?> |
| 196 |
<ul class="current vp-sc-sub-menu-list vp-sc-sub-menu-<?php echo str_replace(' ', '_', $title); ?>"> |
| 197 |
<?php else : ?> |
| 198 |
<ul class="vp-hide vp-sc-sub-menu-list vp-sc-sub-menu-<?php echo str_replace(' ', '_', $title); ?>"> |
| 199 |
<?php endif; ?> |
| 200 |
<?php foreach ($menu['elements'] as $name => $element): ?> |
| 201 |
<li class="vp-sc-element postbox<?php if(isset($element['attributes'])) echo ' has-options'; ?><?php if(isset($element['active']) && $element['active'] == true) echo ' active'; ?>"> |
| 202 |
<h3 class="hndle vp-sc-element-heading"> |
| 203 |
<a href="#"> |
| 204 |
<?php echo $element['title']; ?> |
| 205 |
<?php if(isset($element['attributes'])) echo '<i class="fa fa-arrow-down"></i>'; ?> |
| 206 |
</a> |
| 207 |
</h3> |
| 208 |
<div class="hidden vp-sc-code"><?php echo htmlentities($element['code']); ?></div> |
| 209 |
<?php if(isset($element['attributes']) and !empty($element['attributes'])): ?> |
| 210 |
<form class="vp-sc-element-form <?php if(!isset($element['active']) || isset($element['active']) && $element['active'] == false):?>vp-hide<?php endif; ?> inside"> |
| 211 |
<?php echo $this->print_form($element['attributes']); ?> |
| 212 |
</form> |
| 213 |
<?php endif; ?> |
| 214 |
</li> |
| 215 |
<?php endforeach; ?> |
| 216 |
</ul> |
| 217 |
<?php endforeach; ?> |
| 218 |
</div> |
| 219 |
</div> |
| 220 |
<a class="close-reveal-modal">×</a> |
| 221 |
</div> |
| 222 |
</div> |
| 223 |
<?php |
| 224 |
} |
| 225 |
|
| 226 |
function print_form($attributes) |
| 227 |
{ |
| 228 |
?> |
| 229 |
<div class="vp-sc-fields"> |
| 230 |
<?php |
| 231 |
foreach ($attributes as $attr) |
| 232 |
{ |
| 233 |
// create the object |
| 234 |
$make = VP_Util_Reflection::field_class_from_type($attr['type']); |
| 235 |
// prefix name |
| 236 |
$attr['name'] = '_' . $attr['name']; |
| 237 |
$field = call_user_func("$make::withArray", $attr); |
| 238 |
$default = $field->get_default(); |
| 239 |
if(!is_null($default)) |
| 240 |
$field->set_value($default); |
| 241 |
?> |
| 242 |
|
| 243 |
<?php if($attr['type'] !== 'notebox'): ?> |
| 244 |
<div class="vp-sc-field vp-<?php echo $attr['type']; ?>" data-vp-type="vp-<?php echo $attr['type']; ?>"> |
| 245 |
<div class="label"><label><?php echo $attr['label']; ?></label></div> |
| 246 |
<div class="field"><div class="input"><?php echo $field->render(true); ?></div></div> |
| 247 |
</div> |
| 248 |
<?php else: ?> |
| 249 |
<?php $status = isset($attr['status']) ? $attr['status'] : 'normal'; ?> |
| 250 |
<div class="vp-sc-field vp-<?php echo $attr['type']; ?> note-<?php echo $status; ?>" data-vp-type="vp-<?php echo $attr['type']; ?>"> |
| 251 |
<?php echo $field->render(true); ?> |
| 252 |
</div> |
| 253 |
<?php endif; ?> |
| 254 |
|
| 255 |
<?php |
| 256 |
} |
| 257 |
?> |
| 258 |
</div> |
| 259 |
<div class="vp-sc-action"> |
| 260 |
<button class="vp-sc-insert button"><?php _e('Insert', 'wp-ultimate-recipe'); ?></button> |
| 261 |
<button class="vp-sc-cancel button"><?php _e('Cancel', 'wp-ultimate-recipe') ?></button> |
| 262 |
</div> |
| 263 |
<?php |
| 264 |
} |
| 265 |
|
| 266 |
public static function build_localize() |
| 267 |
{ |
| 268 |
$localize = array(); |
| 269 |
foreach (self::$pool as $sg) |
| 270 |
{ |
| 271 |
$localize[] = array( |
| 272 |
'name' => $sg->name, |
| 273 |
'modal_title' => $sg->modal_title, |
| 274 |
'button_title' => $sg->button_title, |
| 275 |
'main_image' => $sg->main_image, |
| 276 |
'sprite_image' => $sg->sprite_image, |
| 277 |
); |
| 278 |
} |
| 279 |
return $localize; |
| 280 |
} |
| 281 |
|
| 282 |
public static function init_buttons() |
| 283 |
{ |
| 284 |
if( VP_Metabox::_is_post_or_page() && !current_user_can( 'edit_posts' ) && |
| 285 |
!current_user_can( 'edit_pages' ) && get_user_option( 'rich_editing' ) == 'true') |
| 286 |
return; |
| 287 |
|
| 288 |
add_filter( 'mce_external_plugins' , array(__CLASS__, 'add_buttons') ); |
| 289 |
add_filter( 'mce_buttons' , array(__CLASS__, 'register_buttons') ); |
| 290 |
add_filter( 'wp_fullscreen_buttons', array(__CLASS__, 'fullscreen_buttons') ); |
| 291 |
add_filter( 'admin_print_styles' , array(__CLASS__, 'print_styles') ); |
| 292 |
} |
| 293 |
|
| 294 |
public static function print_styles($buttons) |
| 295 |
{ |
| 296 |
?> |
| 297 |
<style type="text/css"> |
| 298 |
<?php foreach (self::$pool as $sg): ?> |
| 299 |
#qt_content_<?php echo $sg->name; ?>{ |
| 300 |
background: url('<?php echo $sg->sprite_image; ?>') 2px -21px no-repeat !important; |
| 301 |
text-indent: -999px; |
| 302 |
} |
| 303 |
span.mce_<?php echo $sg->name; ?>{ |
| 304 |
background: url('<?php echo $sg->sprite_image; ?>') 0 0 no-repeat !important; |
| 305 |
} |
| 306 |
<?php endforeach; ?> |
| 307 |
</style> |
| 308 |
<?php |
| 309 |
} |
| 310 |
|
| 311 |
public static function register_buttons($buttons) |
| 312 |
{ |
| 313 |
foreach (self::$pool as $sg) |
| 314 |
{ |
| 315 |
if( $sg->can_output() ) |
| 316 |
$vp_buttons[] = $sg->name; |
| 317 |
} |
| 318 |
$buttons = array_merge($buttons, $vp_buttons); |
| 319 |
return $buttons; |
| 320 |
} |
| 321 |
|
| 322 |
public static function add_buttons($plugin_array) |
| 323 |
{ |
| 324 |
$plugin_array['vp_sc_button'] = VP_PUBLIC_URL .'/js/shortcodes.js'; |
| 325 |
foreach (self::$pool as $sg) |
| 326 |
{ |
| 327 |
if( $sg->can_output() ) |
| 328 |
$plugin_array[$sg->name] = VP_PUBLIC_URL .'/js/dummy.js'; |
| 329 |
} |
| 330 |
return $plugin_array; |
| 331 |
} |
| 332 |
|
| 333 |
public static function fullscreen_buttons($buttons) |
| 334 |
{ |
| 335 |
foreach (self::$pool as $sg) |
| 336 |
{ |
| 337 |
if( $sg->can_output() ) |
| 338 |
{ |
| 339 |
// add a separator |
| 340 |
$buttons[] = 'separator'; |
| 341 |
// format: title, onclick, show in both editors |
| 342 |
$buttons[$sg->name] = array( |
| 343 |
// Title of the button |
| 344 |
'title' => $sg->button_title, |
| 345 |
// Command to execute |
| 346 |
'onclick' => "tinyMCE.execCommand('{$sg->name}_cmd');", |
| 347 |
// Show on visual AND html mode |
| 348 |
'both' => true |
| 349 |
); |
| 350 |
} |
| 351 |
} |
| 352 |
return $buttons; |
| 353 |
} |
| 354 |
|
| 355 |
public function get_shortcode_tags() { |
| 356 |
$shortcodes = $this->template; |
| 357 |
$tags = array(); |
| 358 |
foreach ($shortcodes as $menu) { |
| 359 |
foreach ($menu['elements'] as $sc) { |
| 360 |
$code = $sc['code']; |
| 361 |
preg_match('/\[(\w+).*\]/', $code, $matches); |
| 362 |
$tags[] = $matches[1]; |
| 363 |
} |
| 364 |
} |
| 365 |
return $tags; |
| 366 |
} |
| 367 |
|
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* EOF |
| 372 |
*/ |
| 373 |
|