PluginProbe
GetResponse Forms by Optin Cat / 1.5.2
GetResponse Forms by Optin Cat v1.5.2
1.3.4 1.3.5 1.3.6 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 2.0.0 All 36 releases
getresponse / includes / skelet / core / core-shortcodes.php

core-shortcodes.php in GetResponse Forms by Optin Cat 1.5.2, at includes/skelet/core/core-shortcodes.php

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