| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package skelet |
| 5 |
*/ |
| 6 |
|
| 7 |
// Load the TinyMCE plugin |
| 8 |
add_filter( 'mce_external_plugins', 'skelet_add_tinyMCE_plugin' ); |
| 9 |
function skelet_add_tinyMCE_plugin( $buttons ) { |
| 10 |
|
| 11 |
global $paf_shortcodes; |
| 12 |
|
| 13 |
if( $paf_shortcodes ) { |
| 14 |
$buttons[ 'skelet' ] = site_url( '?skelet=tinyMCE_js' ); |
| 15 |
} |
| 16 |
return $buttons; |
| 17 |
} |
| 18 |
|
| 19 |
// Add endpoints rewrite rules |
| 20 |
// add_action( 'init', 'skelet_add_endpoint' ); |
| 21 |
function skelet_add_endpoint() { |
| 22 |
|
| 23 |
$rules = array( |
| 24 |
'tinyMCE\.js' => 'tinyMCE_js', |
| 25 |
'tinyMCE\.php/([a-zA-Z_][a-zA-Z0-9_-]*)' => 'tinyMCE_php&tag=$matches[1]', |
| 26 |
); |
| 27 |
|
| 28 |
foreach ( $rules as $regex => $redirect ) { |
| 29 |
add_rewrite_rule( |
| 30 |
sprintf( '^skelet/%s$', $regex ) |
| 31 |
, sprintf( 'index.php?skelet=%s', $redirect ) |
| 32 |
, 'top' |
| 33 |
); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// Add the query_var "skelet" and "tag" |
| 38 |
add_filter( 'query_vars', 'skelet_add_query_vars' ); |
| 39 |
function skelet_add_query_vars( $vars ) { |
| 40 |
$vars[] = 'skelet'; |
| 41 |
$vars[] = 'tag'; |
| 42 |
return $vars; |
| 43 |
} |
| 44 |
|
| 45 |
// Capture requests and serve files |
| 46 |
add_action( 'parse_request', 'skelet_sniff_requests' ); |
| 47 |
function skelet_sniff_requests() { |
| 48 |
|
| 49 |
global $wp; |
| 50 |
$serve = K::get_var( 'skelet', $wp->query_vars ); |
| 51 |
$tag = K::get_var( 'tag', $wp->query_vars ); |
| 52 |
|
| 53 |
switch ( $serve ) { |
| 54 |
case 'tinyMCE_php': |
| 55 |
$shortcode = K::get_var( $tag, $GLOBALS[ 'paf_shortcodes' ] ); |
| 56 |
// Exist if shortcode doesn't exist or doesn't have parameters |
| 57 |
if( ! $shortcode || ! K::get_var( 'parameters', $shortcode ) ) { |
| 58 |
exit; |
| 59 |
} |
| 60 |
// Show HTML for shortcode popup window |
| 61 |
header( 'Content-Type: text/html; charset=utf-8'); |
| 62 |
die( call_user_func( 'skelet_' . $serve, $tag ) ); |
| 63 |
case 'tinyMCE_js': |
| 64 |
header( 'Content-Type: application/javascript; charset=utf-8' ); |
| 65 |
die( call_user_func( 'skelet_' . $serve ) ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Add buttons to WordPress editor |
| 70 |
add_filter( 'mce_buttons', 'skelet_tinyMCE_buttons' ); |
| 71 |
function skelet_tinyMCE_buttons( $buttons ) { |
| 72 |
|
| 73 |
$shortcodes = K::get_var( 'paf_shortcodes', $GLOBALS, array() ); |
| 74 |
return array_merge( $buttons, array_keys( $shortcodes ) ); |
| 75 |
} |
| 76 |
|
| 77 |
// output for "skelet/tinyMCE.php/$tag" |
| 78 |
function skelet_tinyMCE_php( $tag ) { |
| 79 |
global $paf_shortcodes; |
| 80 |
|
| 81 |
$select2_enqueued = false; |
| 82 |
$protocol = is_ssl() ? 'https' : 'http'; |
| 83 |
|
| 84 |
// Head |
| 85 |
printf( '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title>' ); |
| 86 |
|
| 87 |
// CSS |
| 88 |
printf( '<link rel="stylesheet" href="%s" />', admin_url( 'css/common' . ( is_rtl() ? '-rtl' : '' ) . '.css' ) ); |
| 89 |
printf( '<link rel="stylesheet" href="%s" />', admin_url( 'css/forms' . ( is_rtl() ? '-rtl' : '' ) . '.css' ) ); |
| 90 |
printf( '<link rel="stylesheet" href="%s" />', site_url( 'wp-includes/css/dashicons' . ( is_rtl() ? '-rtl' : '' ) . '.min.css' ) ); |
| 91 |
printf( '<link rel="stylesheet" href="%s" />', site_url( 'wp-includes/css/buttons' . ( is_rtl() ? '-rtl' : '' ) . '.min.css' ) ); |
| 92 |
print( '<style>body { height: auto; margin: 0; min-width: 0; padding: 1em; }</style>' ); |
| 93 |
|
| 94 |
paf_asset_css( 'paf' ); |
| 95 |
|
| 96 |
// JS |
| 97 |
printf( '<script src="%s"></script>', "$protocol://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js" ); |
| 98 |
printf( '<script src="%s"></script>', "$protocol://cdnjs.cloudflare.com/ajax/libs/jquery.serializeJSON/1.2.0/jquery.serializeJSON.min.js" ); |
| 99 |
|
| 100 |
paf_asset_js( 'paf' ); |
| 101 |
|
| 102 |
ob_start(); |
| 103 |
?> |
| 104 |
<script> |
| 105 |
var paf; |
| 106 |
var shortcode = ''; |
| 107 |
var wrap = <?php echo K::get_var( 'wrap', $paf_shortcodes[ $tag ] ) ? 'true' : 'false'; ?>; |
| 108 |
|
| 109 |
jQuery( document ).ready( function ( $ ) { |
| 110 |
|
| 111 |
/* Fill content field with selected text if any */ |
| 112 |
var content = parent.tinymce.activeEditor.selection.getContent( { format : 'text' } ); |
| 113 |
$( '#content' ).val( content ); |
| 114 |
|
| 115 |
// Update shortcode |
| 116 |
$( 'input,select,textarea', 'form' ).on( 'change keyup', function() { $( 'form' ).change(); } ); |
| 117 |
|
| 118 |
// Autoselect shortcode |
| 119 |
$( "#shortcode" ).mouseover( function() { $( this ).select(); } ); |
| 120 |
|
| 121 |
/** |
| 122 |
* Bind to form events |
| 123 |
* |
| 124 |
* - On submit: Fill the WP editor |
| 125 |
* - On change: update the shortcode value |
| 126 |
*/ |
| 127 |
$( 'form' ).on( 'submit change', function( e ) { |
| 128 |
|
| 129 |
e.preventDefault(); |
| 130 |
|
| 131 |
shortcode = ''; |
| 132 |
paf = $( this ).serializeJSON().paf; |
| 133 |
content = $( '#content' ).val(); |
| 134 |
|
| 135 |
if( 'undefined' === typeof paf ) { |
| 136 |
paf = {}; |
| 137 |
} |
| 138 |
|
| 139 |
// Build the shortcode |
| 140 |
Object.keys( paf ).map( function(v) { |
| 141 |
if( 'undefined' !== paf[ v ] && paf[ v ] ) { |
| 142 |
shortcode += ' ' |
| 143 |
+ v |
| 144 |
+ '="' |
| 145 |
+ paf[ v ].toString().split().join().replace( '"', '\\"', 'g' ) |
| 146 |
+ '"' |
| 147 |
; |
| 148 |
} |
| 149 |
} ); |
| 150 |
|
| 151 |
if( wrap ) { |
| 152 |
shortcode = "[<?php echo $tag; ?>" + shortcode + "]" |
| 153 |
+ content |
| 154 |
+ "[/<?php echo $tag; ?>]" |
| 155 |
; |
| 156 |
} else { |
| 157 |
shortcode = "[<?php echo $tag; ?>" + shortcode + "]"; |
| 158 |
} |
| 159 |
|
| 160 |
// Update the demo |
| 161 |
$( "#shortcode" ).val( shortcode ); |
| 162 |
|
| 163 |
// Fill the editor and close |
| 164 |
if ( 'submit' === e.type ) { |
| 165 |
parent.tinymce.activeEditor.execCommand( "mceInsertContent", false, shortcode ); |
| 166 |
parent.tinymce.activeEditor.windowManager.close( window ); |
| 167 |
} |
| 168 |
} ).change(); |
| 169 |
} ); |
| 170 |
</script> |
| 171 |
<?php |
| 172 |
print( JSMin::minify( ob_get_clean() ) ); |
| 173 |
|
| 174 |
// Close head |
| 175 |
print( '</head><body class="wp-core-ui">' ); |
| 176 |
|
| 177 |
// Fields |
| 178 |
$parameters = $paf_shortcodes[ $tag ]['parameters']; |
| 179 |
echo '<form id="paf-form" action = "">'; |
| 180 |
foreach ( $parameters as $k => $v ) { |
| 181 |
// Validate title |
| 182 |
$v[ 'title' ] = k::get_var( 'title', $v, $k ); |
| 183 |
// Print option |
| 184 |
paf_print_option( $k, $v ); |
| 185 |
if( 'select' === K::get_var( 'type', $v ) ) { |
| 186 |
printf( '<link rel="stylesheet" href="%s" />', "$protocol://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.css" ); |
| 187 |
printf( '<script src="%s"></script>', "$protocol://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.js" ); |
| 188 |
print( "<script>jQuery( document ).ready( function( $ ) { $( 'select' ).select2(); } );</script>" ); |
| 189 |
$select2_enqueued = true; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// Content field |
| 194 |
if ( K::get_var( 'wrap', $paf_shortcodes[ $tag ] ) ) { |
| 195 |
K::textarea( 'content' |
| 196 |
, array( 'class' => 'large-text', 'id' => 'content' ) |
| 197 |
, array( 'format' => '<p><label><strong>Content:</strong>:textarea</label></p>' ) |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
// Buttons |
| 202 |
echo '<hr />'; |
| 203 |
echo '<p><label><strong>Shortcode:</strong><input type="text" class="large-text" id="shortcode" value=""/></label></p>'; |
| 204 |
echo '<hr />'; |
| 205 |
echo '<p>'; |
| 206 |
K::input( 'submit' |
| 207 |
, array( |
| 208 |
'class' => 'button button-large button-primary aligncenter ', |
| 209 |
'type' => 'submit', |
| 210 |
'value' => 'Add shortcode', |
| 211 |
) |
| 212 |
); |
| 213 |
echo ' '; |
| 214 |
K::wrap( 'Reset' |
| 215 |
,array( |
| 216 |
'class' => 'button button-large paf-reset', |
| 217 |
'href' => '#', |
| 218 |
'id' => 'paf-reset', |
| 219 |
) |
| 220 |
, array( 'in' => 'a' ) |
| 221 |
); |
| 222 |
echo '</p>'; |
| 223 |
|
| 224 |
// Close document |
| 225 |
echo '</form></body></html>'; |
| 226 |
|
| 227 |
// C ya! |
| 228 |
return ob_get_clean(); |
| 229 |
} |
| 230 |
|
| 231 |
// Output for "skelet/tinyMCE.js" |
| 232 |
function skelet_tinyMCE_js() { |
| 233 |
global $paf_shortcodes; |
| 234 |
$minify = true; |
| 235 |
|
| 236 |
// Prepare a shortcodes object for JavaScript |
| 237 |
$shortcodes = $paf_shortcodes; |
| 238 |
foreach ( $paf_shortcodes as $tag => $settings ) { |
| 239 |
|
| 240 |
// Is it a menu button |
| 241 |
$shortcodes[ $tag ][ 'isMenu' ] = ( bool ) K::get_var( 'menu', $shortcodes[ $tag ] ); |
| 242 |
$shortcodes[ $tag ][ 'isMenu' ] && $shortcodes[ $tag ][ '_type' ] = 'menu'; |
| 243 |
|
| 244 |
// Or a modal window |
| 245 |
$shortcodes[ $tag ][ 'isModal' ] = ( bool ) K::get_var( 'parameters', $shortcodes[ $tag ] ); |
| 246 |
$shortcodes[ $tag ][ 'isModal' ] && $shortcodes[ $tag ][ '_type' ] = 'modal'; |
| 247 |
$shortcodes[ $tag ][ 'parameters' ] = array(); |
| 248 |
unset( $shortcodes[ $tag ][ 'parameters' ] ); |
| 249 |
|
| 250 |
// or a normal button |
| 251 |
0 |
| 252 |
|| $shortcodes[ $tag ][ 'isMenu' ] |
| 253 |
|| $shortcodes[ $tag ][ 'isModal' ] |
| 254 |
|| ( |
| 255 |
1 |
| 256 |
&& $shortcodes[ $tag ][ 'isBasic' ] = true |
| 257 |
&& $shortcodes[ $tag ][ '_type' ] = 'basic' |
| 258 |
); |
| 259 |
|
| 260 |
// Get parent if any |
| 261 |
$parent = K::get_var( 'parent', $shortcodes[ $tag ] ); |
| 262 |
if ( $parent && array_key_exists( $parent, $shortcodes ) |
| 263 |
) { |
| 264 |
$shortcodes[ $tag ][ 'isChildOf' ] = $parent; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
ob_start(); |
| 269 |
?> |
| 270 |
/*<script>*/ |
| 271 |
(function () { |
| 272 |
|
| 273 |
var i |
| 274 |
, $ = jQuery |
| 275 |
, s |
| 276 |
, shortcodes = <?php echo json_encode( $shortcodes, JSON_FORCE_OBJECT ); ?> |
| 277 |
, shortcodes_handlers = [] |
| 278 |
; |
| 279 |
|
| 280 |
function createShortcodeHandler( tag ) { |
| 281 |
var s = shortcodes[ tag ]; |
| 282 |
|
| 283 |
switch ( s._type ) { |
| 284 |
case 'basic' : |
| 285 |
return function() { |
| 286 |
|
| 287 |
var ed = tinymce.activeEditor |
| 288 |
, tag_end |
| 289 |
, tag_start |
| 290 |
; |
| 291 |
|
| 292 |
tag_end = '[/tag]'.replace( 'tag', tag ); |
| 293 |
tag_start = '[tag]'.replace( 'tag', tag ); |
| 294 |
|
| 295 |
/* Wrap or replace */ |
| 296 |
ed.selection.setContent( s.wrap |
| 297 |
? tag_start + ed.selection.getContent() + tag_end |
| 298 |
: tag_start |
| 299 |
); |
| 300 |
}; |
| 301 |
case 'modal' : |
| 302 |
return function() { |
| 303 |
|
| 304 |
var ed = tinymce.activeEditor |
| 305 |
, $w = $( window ) |
| 306 |
, h = ( 'undefined' !== s.height && s.height && s.height <= 1 ) |
| 307 |
? $w.height() * s.height |
| 308 |
: $w.height() * 0.5 |
| 309 |
, w = ( 'undefined' !== s.width && s.width && s.width <= 1 ) |
| 310 |
? $w.width() * s.width |
| 311 |
: $w.width() * 0.5 |
| 312 |
; |
| 313 |
|
| 314 |
ed.windowManager.open( { |
| 315 |
title: s.title, |
| 316 |
text: s.text, |
| 317 |
url: '<?php echo site_url( "?skelet=tinyMCE_php&tag=" ); ?>' + tag, |
| 318 |
width: w, |
| 319 |
height: h |
| 320 |
} ); |
| 321 |
}; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Define shortcode handlers here |
| 327 |
* |
| 328 |
* This is a solution to the closure loops infamous issue |
| 329 |
*/ |
| 330 |
for ( tag in shortcodes ) { |
| 331 |
|
| 332 |
s = shortcodes[ tag ]; |
| 333 |
|
| 334 |
/* Skip menus since they don't have functions */ |
| 335 |
if( [ 'basic', 'modal' ].indexOf( s._type ) == -1 ) { |
| 336 |
continue; |
| 337 |
} |
| 338 |
|
| 339 |
shortcodes_handlers[ tag ] = createShortcodeHandler( tag ); |
| 340 |
} |
| 341 |
|
| 342 |
/* Register the buttons */ |
| 343 |
tinymce.create('tinymce.plugins.skelet', { |
| 344 |
init : function(ed, url) { |
| 345 |
|
| 346 |
/* Gets sub_items recursively in a format compatible with tinyMCE */ |
| 347 |
var getMenuItems = function( _parent ) { |
| 348 |
|
| 349 |
var items = []; |
| 350 |
var s; |
| 351 |
|
| 352 |
/* Loop all, non-children are skipped inside */ |
| 353 |
for ( var tag in shortcodes ) { |
| 354 |
|
| 355 |
s = shortcodes[ tag ]; |
| 356 |
|
| 357 |
/* Skip non-children */ |
| 358 |
if( 'undefined' === typeof s.isChildOf || s.isChildOf !== _parent ) { |
| 359 |
continue; |
| 360 |
} |
| 361 |
|
| 362 |
/* Get item */ |
| 363 |
switch ( s._type ) { |
| 364 |
case 'menu' : |
| 365 |
s.menu = getMenuItems( tag ); |
| 366 |
break; |
| 367 |
case 'basic' : |
| 368 |
case 'modal' : |
| 369 |
s.onclick = shortcodes_handlers [ tag ]; |
| 370 |
} |
| 371 |
|
| 372 |
/* Add item to array */ |
| 373 |
items.push( s ); |
| 374 |
} |
| 375 |
|
| 376 |
return items; |
| 377 |
}; |
| 378 |
|
| 379 |
/* Adds button to tinyMCE toolbar */ |
| 380 |
var addControl = function( tag ) { |
| 381 |
|
| 382 |
var s = shortcodes[ tag ]; |
| 383 |
|
| 384 |
if ( s.isChildOf ) { |
| 385 |
|
| 386 |
/* Skip non-top-level elements, those are added by there parents */ |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
/* Get the control parameters */ |
| 391 |
switch ( s._type ) { |
| 392 |
case 'menu' : |
| 393 |
s.menu = getMenuItems( tag ); |
| 394 |
s.type = 'menubutton'; |
| 395 |
break; |
| 396 |
case 'basic' : |
| 397 |
case 'modal' : |
| 398 |
s.onclick = shortcodes_handlers [ tag ]; |
| 399 |
} |
| 400 |
|
| 401 |
/* Add the control */ |
| 402 |
return ed.addButton( tag, s ); |
| 403 |
}; |
| 404 |
|
| 405 |
/* Loops through the object defining the shortcodes and add them */ |
| 406 |
for( var tag in shortcodes ) { |
| 407 |
|
| 408 |
addControl( tag ); |
| 409 |
} |
| 410 |
} |
| 411 |
}); |
| 412 |
/* Add buttons */ |
| 413 |
tinymce.PluginManager.add( 'skelet', tinymce.plugins.skelet ); |
| 414 |
})();<?php |
| 415 |
|
| 416 |
return empty( $minify ) |
| 417 |
? ob_get_clean() |
| 418 |
: JSMin::minify( ob_get_clean() ) |
| 419 |
; |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* Bind shortcodes to fuctions |
| 424 |
* |
| 425 |
* For each shortcode, the function will try functions in this order: |
| 426 |
* - the func parameter |
| 427 |
* - the tag with _func added to it |
| 428 |
* - the tag |
| 429 |
*/ |
| 430 |
add_action( 'init', 'skelet_process_shortcodes' ); |
| 431 |
function skelet_process_shortcodes() { |
| 432 |
foreach ( K::get_var( 'paf_shortcodes', $GLOBALS, array() ) as $tag => $specs ) { |
| 433 |
// Get func |
| 434 |
$func = K::get_var( 'func', $specs ); |
| 435 |
if ( ! function_exists( $func ) ) { |
| 436 |
$func = $tag . '_func'; |
| 437 |
if ( ! function_exists( $func ) ) { |
| 438 |
$func = $tag; |
| 439 |
if ( ! function_exists( $func ) ) { |
| 440 |
$func = 'skelet_func'; |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
// bind |
| 445 |
add_shortcode( $tag, $func ); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
// Callback used for a shortcode when non is defined for it |
| 450 |
function skelet_func() { |
| 451 |
$args[ 'atts' ] = func_get_arg( 0 ); |
| 452 |
$args[ 'content' ] = func_get_arg( 1 ); |
| 453 |
$tag = func_get_arg( 2 ); |
| 454 |
|
| 455 |
if( $args[ 'atts' ] ) { |
| 456 |
$atts = substr( json_encode( $args[ 'atts' ], JSON_PRETTY_PRINT ), 2, -2); |
| 457 |
} else { |
| 458 |
$atts = ' ' . htmlspecialchars( '<' . __( 'none' ) . '>' ); |
| 459 |
} |
| 460 |
|
| 461 |
if( $args[ 'content' ] ) { |
| 462 |
$content = ' "' . $args[ 'content' ] . '"'; |
| 463 |
} else { |
| 464 |
$content = ' ' . htmlspecialchars( '<' . __( 'none' ) . '>' ); |
| 465 |
} |
| 466 |
|
| 467 |
$ret = '<pre>' |
| 468 |
. sprintf( __( 'Shortcode <strong>%s</strong> used with parameters:' ) , $tag ) |
| 469 |
. "\n" |
| 470 |
. $atts |
| 471 |
. "\n" |
| 472 |
. __( 'With this enclosed content:' ) |
| 473 |
. "\n" |
| 474 |
. $content |
| 475 |
. '</pre>' |
| 476 |
; |
| 477 |
|
| 478 |
return $ret; |
| 479 |
} |
| 480 |
|