forked from nathanielks/Shortcodes-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cur-shortcodes-generator.php
executable file
·549 lines (430 loc) · 14.1 KB
/
cur-shortcodes-generator.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<?php
/*
Plugin Name: Shortcodes Generator
Plugin URI: http://fightthecurrent.org/plugins/shortcodes-generator
Description: A plugin to generate shortcodes and a corresponding button in the WordPress visual editor. Wicked!
Version: 1.2.1
Author: Nathaniel Schweinberg
Author URI: http://fightthecurrent.org
Author Email: nathaniel@fightthecurrent.org
License:
Copyright 2012 Nathaniel Schweinberg (nathaniel@fightthecurrent.org)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if( ! defined( 'CSG_URI' ) )
define( 'CSG_URI', plugin_dir_url( __FILE__ ) );
add_action( 'plugins_loaded', array( 'Cur_Shortcodes_Generator', 'get_instance' ) );
class Cur_Shortcodes_Generator{
var $shortcodes;
var $generate_output = false;
var $plugin_path;
var $in;
var $out;
var $editor_plugin_path;
protected static $instance = null;
function __construct() {
$shortcodes =& $this->get_shortcodes_array();
if ( $shortcodes ) {
if( !isset( $shortcodes['title'] ) ){
$shortcodes['title'] = 'CurShortcodes';
}
if( !isset( $shortcodes['slug'] ) ){
$shortcodes['slug'] = 'cur_shortcodes_button';
}
$this->shortcodes = $shortcodes;
$this->plugin_path = plugin_dir_path( __FILE__ );
/**
* Shortcode buttons
*
* @see add_shortcode_button()
* @see refresh_mce()
*/
add_action( 'init', array( &$this, 'add_shortcode_button') );
add_filter( 'tiny_mce_version', array( &$this, 'refresh_mce' ) );
add_action( 'admin_print_styles', array( &$this, 'shortcodes_button_css' ) );
// Require related shortcodes functions, if they exist
add_action( 'init', array( &$this, 'get_shortcodes_functions' ) );
add_action( 'init', array( &$this, 'generate_shortcodes' ) );
}
else {
// Display a notice if options aren't present in the theme
add_action('admin_notices', array( &$this, 'admin_notice') );
add_action('admin_init', array(&$this, 'nag_ignore') );
}
}
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Searches for shortcodes/array.php in the active theme's directory. If it
* can't find it, it defaults to the cur_shortcodes_generator_shortcodes()
* function, in case the theme uses the function instead of the files. If
* it can't find that, it returns false.
*
* @access public
* @return $shortcodes
* @return false
*/
function get_shortcodes_array(){
// Load shortcodes from shortcodes/array.php file (if it exists)
$location = apply_filters( 'cur_shortcodes_array_location', '/shortcodes/array.php' );
$location_path = get_template_directory() . $location;
$this->in = $location_path;
if ( file_exists( $location_path ) ) {
$maybe_shortcodes = require_once $location_path;
if ( !is_array( $maybe_shortcodes ) && function_exists('cur_shortcodes_generator_shortcodes') ) {
$shortcodes = cur_shortcodes_generator_shortcodes();
}
return $shortcodes;
}
return false;
}
/**
* Searches for shortcodes/functions.php in the active theme's directory
* and requires it if it exists.
*
* @access public
* @return void
*/
function get_shortcodes_functions(){
// Load shortcodes from shortcodes/functions.php file (if it exists)
$location = apply_filters( 'cur_shortcodes_functions_location', '/shortcodes/functions.php' );
$location_path = get_template_directory() . $location;
if ( file_exists( $location_path ) ) {
require_once $location_path;
}
}
/**
* Displays a notice if the active theme doesn't have an array.php file or
* cur_shortcodes_generator_shortcodes function.
*
* @access public
* @return void
*/
function admin_notice() {
global $pagenow;
if ( $pagenow == 'plugins.php' || $pagenow == 'themes.php' ) {
global $current_user;
$user_id = $current_user->ID;
if ( ! get_user_meta($user_id, 'cur_shortcodes_generator_ignore_notice') ) {
echo '<div class="updated cur_shortcodes_generator_setup_nag"><p>';
printf( __('Your current theme does not have support for the Shortcodes Generator plugin. <a href="%1$s" target="_blank">Learn More</a> | <a href="%2$s">Hide Notice</a>', 'cur-shortcodes-generator'), 'http://fightthecurrent.org/plugins/shortcodes-generator', '?cur_shortcodes_generator_nag_ignore=0');
echo "</p></div>";
}
}
}
/**
* Adds user meta if the current user toggled "Hide Notice" in
* admin_notice()
*
* @access public
* @return void
*/
function nag_ignore() {
global $current_user;
$user_id = $current_user->ID;
if ( isset($_GET['cur_shortcodes_generator_nag_ignore']) && '0' == $_GET['cur_shortcodes_generator_nag_ignore'] ) {
add_user_meta($user_id, 'cur_shortcodes_generator_ignore_notice', 'true', true);
}
}
/**
* Queue admin menu icons CSS.
*
* @access public
* @return void
*/
function shortcodes_button_css() {
$css_path = apply_filters('cur_shortcodes_css_location', '/shortcodes/shortcodes.css');
if ( file_exists( get_template_directory() . $css_path ) ){
wp_enqueue_style( 'cur_shortcodes_button_css', get_template_directory_uri() . $css_path );
} else {
wp_enqueue_style( 'cur_shortcodes_button_css', CSG_URI . 'public/css/shortcodes.css' );
}
}
/**
* Add a button for shortcodes to the WP editor.
*
* @access public
* @return void
*/
function add_shortcode_button() {
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) return;
if ( get_user_option('rich_editing') == 'true') :
add_filter('mce_external_plugins', array( &$this, 'add_shortcode_tinymce_plugin' ) );
add_filter('mce_buttons', array( &$this, 'register_shortcode_button') );
endif;
}
/**
* Register the shortcode button.
*
* @access public
* @param mixed $buttons
* @return array
*/
function register_shortcode_button($buttons) {
array_push($buttons, "|", $this->shortcodes['slug'] );
return $buttons;
}
/**
* Add the shortcode button to TinyMCE
*
* @access public
* @param mixed $plugin_array
* @return array
*/
function add_shortcode_tinymce_plugin($plugin_array) {
$location = apply_filters( 'cur_shortcodes_editor_plugin_location', '/shortcodes/editor_plugin.js' );
$location_path = get_template_directory_uri() . $location;
$plugin_array[ $this->shortcodes['title'] ] = $location_path;
return $plugin_array;
}
/**
* Force TinyMCE to refresh.
*
* @access public
* @param mixed $ver
* @return int
*/
function refresh_mce( $ver ) {
$ver += 3;
return $ver;
}
/**
* Takes two variables: $out and $shortcodes. $out is the path of
* editor_plugin.js. $shortcodes is the array of shortcodes to parse.
*
* @param string $out
* @param array $shortcodes
* @access public
* @return void
*/
function generate_shortcodes(){
$location = apply_filters( 'cur_shortcodes_editor_plugin_location', '/shortcodes/editor_plugin.js' );
$editor_plugin = get_template_directory() . $location;
if (!is_file($editor_plugin) || filemtime($this->in) > filemtime($editor_plugin)) {
$this->generate_output = true;
}
$data = $this->parse_shortcodes( $this->shortcodes );
if ( $data ){
$this->compile( $data, $editor_plugin );
}
}
/**
* Iterates over the shortcodes array adding the shortcodes via anonymous
* functions or a function that's been defined by the user. If
* $this->generate_output is true, it also generates the output to write to
* editor_plugin.js
*
* @param array $shortcodes
* @param int $isChildren
* @param int $isSelectable
* @param string $tag
* @access public
* @return void
*/
function parse_shortcodes( $shortcodes, $isChildren = 0, $isSelectable = 0, $tag = '' ){
$output = '';
foreach ( $shortcodes as $sc ){
if ( !is_array( $sc ) && !$isChildren ){
continue;
} elseif ( $isChildren && !is_array( $sc ) ){
unset( $shortcode );
unset( $function );
$title = $sc;
} else{
unset($tag);
extract( $sc );
}
$function = ( isset( $function ) ) ? $function : '';
$selectable = ( isset( $isSelectable ) ) ? 1 : ( isset( $selectable ) ) ? 1 : 0;
$shortcode = ( !empty( $shortcode ) ) ? $shortcode : $sc;
$title = ucwords( preg_replace( '/[^\w-]/', ' ', $shortcode ) );
$tag = ( !empty( $tag ) ) ? $tag : '';
if ( isset( $children ) && is_array( $children ) ){
if( !$this->generate_output ){
$this->parse_shortcodes( $children, 1, $selectable, $tag );
} else {
$output .= 'c=b.addMenu({title:"' . $title . '"});' . "\n";
$output .= $this->parse_shortcodes( $children, 1, $selectable, $tag );
}
unset( $children );
} else {
$this->add_shortcode( $shortcode, $function, $tag );
if( $this->generate_output ){
$scope = 'b';
$prefix = '';
if( $isChildren == 1 ){
$scope = 'c';
$prefix = "\t";
}
$atts = ( isset( $atts ) && is_array( $atts ) ) ? $this->parse_attributes( $atts ) : '';
if ( $selectable ){
$output .= $prefix . 'a.addSelectable(' . $scope . ', \'' .$title. '\' , \'[' .$shortcode . $atts. ']\', \'[/' .$shortcode. ']\');' . "\n";
} else {
$output .= $prefix . 'a.addImmediate(' . $scope . ', "' .$title. '" , "[' .$shortcode . $atts. ']");' . "\n";
}
unset( $shortcode );
//unset( $tag );
unset( $function );
}
}
}
return $output;
}
/**
* Parses the shortcode attributes. If $atts is an associative array, it
* will add the default values defined to the shortcode output. If it's an
* indexed array, it just adds the param key with an empty value.
*
* @param array $atts
* @access public
* @return void
*/
function parse_attributes( $atts ){
$output = '';
if ( $this->is_assoc( $atts ) ){
foreach( $atts as $k => $v ){
$output .= ' ' . $k . '="' .$v. '"';
}
} else {
foreach( $atts as $p ){
$output .= ' ' . $p . '=""';
}
}
return $output;
}
/**
* Checks to see if an array is associative or indexed.
*
* @param array $array
* @access public
* @return bool
*/
function is_assoc($array) {
return (bool)count(array_filter(array_keys($array), 'is_string'));
}
/**
* Adds a shortcode to WordPress. If no function is provided, it creates
* a simple shortcode based via add_simple_shortcode.
*
* @see add_simple_shortcode
* @param string $shortcode
* @param string $function
* @param string $tag
* @access public
* @return void
*/
function add_shortcode( $shortcode = '', $function = '', $tag = null ){
$tag = ( $tag != null ) ? $tag : 'div';
if( empty( $function ) ){
$this->add_simple_shortcode( $shortcode, $tag );
return;
}
add_shortcode( $shortcode, $function );
}
/**
* Creates a simple shortcode based on this template: <tag
* class="shortcode"> Content </tag>, [shortcode][/shortcode]
*
* @param string $shortcode
* @param string $tag
* @access public
* @return void
*/
function add_simple_shortcode( $shortcode, $tag ){
$sc = function( $atts, $content = null ) use ( $shortcode, $tag ){
return '<' . $tag . ' class="' . $shortcode . '">' . do_shortcode($content) . '</' . $tag . '>';
};
add_shortcode($shortcode, $sc);
}
/**
* Compiles all the shortcode data to write to editor_plugin.js
*
* @param string $data
* @param string $output
* @access public
* @return void
*/
function compile( $data, $output ){
$file_start = '
(
function(){
tinymce.create(
"tinymce.plugins.'. $this->shortcodes['title'] . '",
{
init: function(d,e) {},
createControl:function(d,e)
{
if(d=="' . $this->shortcodes['slug'] . '"){
d=e.createMenuButton( "' . $this->shortcodes['slug'] . '",{
title:"Insert Shortcode",
icons:false
});
var a=this;
d.onRenderMenu.add(function(c,b){
';
$file_end = '
});
return d
} // End IF Statement
return null
},
addImmediate: function (d,e,a){
d.add({
title:e,
onclick:function(){
tinyMCE.activeEditor.execCommand( "mceInsertContent",false,a)
}
})
},
addSelectable: function (d,e,open,close,a){
d.add({
title: e,
onclick:function(){
//.execCommand( "mceInsertContent",false,a)
tinyMCE.activeEditor.selection.setContent(open + tinyMCE.activeEditor.selection.getContent() + close);
}
})
}
}
);
tinymce.PluginManager.add( "' . $this->shortcodes['title'] . '", tinymce.plugins.' . $this->shortcodes['title'] . ');
}
)();
';
$file_content = $file_start . $data . $file_end;
return file_put_contents( $output, $file_content );
}
}
function cur_shortcode_atts( $shortcode_slug, $atts ){
$csg = Cur_Shortcodes_Generator::get_instance();
if( is_array( $shortcode_slug ) ){
$default_pairs = $csg->shortcodes[ $shortcode_slug['parent'] ]['children'][ $shortcode_slug['child'] ]['atts'];
} else {
$default_pairs = $csg->shortcodes[ $shortcode_slug ]['atts'];
}
if( ! empty( $default_pairs ) && is_array( $default_pairs ) )
return shortcode_atts( $default_pairs, $atts );
return false;
}
/*
*Thanks JapanPro & forsvarir
*http://stackoverflow.com/questions/3809108/how-to-remove-empty-paragraph-tags-from-string
*/
function cur_remove_empty_tags( $content ){
$pattern = "/<[^\/>]*>([\s]?)*<\/[^>]*>/";
return preg_replace($pattern, '', $content);
}