PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 4.1.3
Shortcoder — Create Shortcodes for Anything v4.1.3
trunk 3.0 3.0.1 3.1 3.2 3.3 3.4 3.4.1 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2 4.3 4.4 4.5 4.6 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.1 5.2 5.2.1 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.4 5.5 5.6 5.7 5.8 6.0 6.1 6.2 6.3 6.3.1 6.3.2 6.4 6.5 6.5.1 6.5.2 6.5.3
shortcoder / admin / sc-admin.php
shortcoder / admin Last commit date
css 8 years ago images 8 years ago js 8 years ago sc-admin.php 8 years ago sc-insert.php 8 years ago
sc-admin.php
534 lines
1 <?php
2
3 class Shortcoder_Admin{
4
5 private static $pagehook = 'settings_page_shortcoder';
6
7 public static function init(){
8
9 // Add menu
10 add_action( 'admin_menu', array( __class__, 'add_menu' ) );
11
12 // Enqueue the scripts and styles
13 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
14
15 // Register the action for admin ajax features
16 add_action( 'wp_ajax_sc_admin_ajax', array( __CLASS__, 'admin_ajax' ) );
17
18 // Register action links
19 add_filter( 'plugin_action_links_' . SC_BASE_NAME, array( __CLASS__, 'action_links' ) );
20
21 // Add Quick Tag button to the editor
22 add_action( 'admin_footer', array( __class__, 'add_qt_button' ) );
23
24 // Add TinyMCE button
25 add_action( 'admin_init', array( __class__, 'register_mce' ) );
26
27 }
28
29 public static function add_menu(){
30
31 add_options_page( 'Shortcoder', 'Shortcoder', 'manage_options', 'shortcoder', array( __class__, 'admin_page' ) );
32
33 }
34
35 public static function enqueue_scripts( $hook ){
36
37 if( $hook == self::$pagehook ){
38
39 wp_enqueue_style( 'sc-admin-css', SC_ADMIN_URL . '/css/style.css', array(), SC_VERSION );
40
41 wp_enqueue_script( 'jquery' );
42 wp_enqueue_script( 'sc-admin-js', SC_ADMIN_URL . '/js/script.js', array( 'jquery' ), SC_VERSION );
43
44 }
45 }
46
47 public static function admin_page(){
48
49 echo '<div class="wrap">';
50 echo '<div class="head_wrap">';
51 echo '<h1 class="sc_title">Shortcoder <span class="title-count">' . SC_VERSION . '</span></h1>';
52 self::top_sharebar();
53 self::print_notice();
54 echo '</div>';
55
56 echo '<div id="content">';
57
58 $g = self::clean_get();
59
60 if( !isset( $g[ 'action' ] ) ){
61 $g[ 'action' ] = 'list';
62 }
63
64 if( $g[ 'action' ] == 'list' ){
65 self::list_shortcodes();
66 }
67
68 if( $g[ 'action' ] == 'edit' ){
69 self::edit_shortcode();
70 }
71
72 if( $g[ 'action' ] == 'new' ){
73 self::new_shortcode();
74 }
75
76 echo '</div>';
77
78 self::page_bottom();
79
80 echo '</div>';
81
82 }
83
84 public static function list_shortcodes(){
85
86 Shortcoder_Import::check_import();
87
88 $shortcodes = Shortcoder::list_all();
89 $g = self::clean_get();
90
91 echo '<h3 class="page_title">' . __( 'List of shortcodes created', 'shortcoder' ) . ' (' . count( $shortcodes ) . ')';
92 echo '<span class="sc_menu">';
93
94 echo '<span class="button search_btn" title="' . __( 'Search shortcodes', 'shortcoder' ) . '"><span class="dashicons dashicons-search"></span><input type="search" class="search_box" placeholder="Search ..."/></span>';
95
96 echo '<label for="import" class="button"><span class="dashicons dashicons-download"></span><em>' . __( 'Import shortcodes', 'shortcoder' ) . '</em></label>';
97
98 echo '<a href="' . self::get_link(array(
99 'action' => 'sc_export',
100 '_wpnonce' => wp_create_nonce( 'sc_export_data' )
101 ), 'admin-ajax.php' ) . '" class="button"><span class="dashicons dashicons-upload"></span><em>' . __( 'Export shortcodes', 'shortcoder' ) . '</em></a>';
102
103
104 echo '<button class="button sort_btn" title="' . __( 'Sort list', 'shortcoder' ) . '"><span class="dashicons dashicons-menu"></span> <span class="dashicons dashicons-arrow-down-alt sort_icon"></span></button>';
105
106 echo '<a href="' . self::get_link(array( 'action' => 'new' )) . '" class="button button-primary sc_new_btn"><span class="dashicons dashicons-plus"></span> ' . __( 'Create a new shortcode', 'shortcoder' ) . '</a>';
107
108 echo '</span>';
109 echo '</h3>';
110
111 if( isset( $g[ 'sort' ] ) ){
112 $sort = $g[ 'sort' ];
113 if( $sort == 'asc' ){
114 uksort($shortcodes, 'strcasecmp' );
115 }else if( $sort == 'desc' ){
116 uksort($shortcodes, 'strcasecmp' );
117 $shortcodes = array_reverse( $shortcodes, True );
118 }
119 }
120
121 echo '<ul class="sc_list" data-empty="' . __( 'No shortcodes are created. Go ahead create one !', 'shortcoder' ) . '">';
122 foreach( $shortcodes as $name => $data ){
123
124 $data = wp_parse_args( $data, Shortcoder::defaults() );
125
126 $link = self::get_link(array(
127 'action' => 'edit',
128 'id' => base64_encode( $name )
129 ));
130
131 $delete_link = self::get_link(array(
132 'action' => 'sc_admin_ajax',
133 'do' => 'delete',
134 'id' => base64_encode( $name ),
135 '_wpnonce' => wp_create_nonce( 'sc_delete_nonce' )
136 ), 'admin-ajax.php' );
137
138 $disabled_text = ( $data[ 'disabled' ] == '1' ) ? '<small class="disabled_text">' . __( 'Temporarily disabled', 'shortcoder' ) . '</small>' : '';
139
140 echo '<li data-name="' . esc_attr( $name ) . '">';
141 echo '<a href="' . $link . '" class="sc_link" title="' . __( 'Edit shortcode', 'shortcoder' ) . '">' . esc_attr( $name ) . $disabled_text . '</a>';
142 echo '<span class="sc_controls">';
143 echo '<a href="#" class="sc_copy" title="' . __( 'Copy shortcode', 'shortcoder' ) . '"><span class="dashicons dashicons-editor-code"></span></a>';
144 echo '<a href="' . $delete_link . '" class="sc_delete" title="' . __( 'Delete', 'shortcoder' ) . '"><span class="dashicons dashicons-trash"></span></a>';
145 echo '</span>';
146
147 echo '<input type="text" value="' . self::get_shortcode( $name ) . '" class="sc_copy_box" readonly="readonly" title="' . __( 'Copy shortcode', 'shortcoder' ) . '" />';
148
149 echo '</li>';
150
151 }
152 echo '</ul>';
153
154 Shortcoder_Import::import_form();
155
156 }
157
158 public static function new_shortcode(){
159 self::edit_shortcode( 'new' );
160 }
161
162 public static function edit_shortcode( $action = 'edit' ){
163
164 self::save_shortcode();
165
166 $shortcodes = Shortcoder::list_all();
167 $g = self::clean_get();
168
169 $page_title = __( 'New shortcode', 'shortcoder' );
170 $action_btn = __( 'Create shortcode', 'shortcoder' );
171 $sc_name = '';
172 $values = array();
173
174 if( $action == 'edit' ){
175
176 $page_title = __( 'Edit shortcode', 'shortcoder' );
177 $action_btn = __( 'Save settings', 'shortcoder' );
178
179 if( !isset( $g[ 'id' ] ) ){
180 echo '<p align="center">' . __( 'No shortcode ID provided !' ) . '</p>';
181 return false;
182 }
183
184 $sc_name = base64_decode( $g[ 'id' ] );
185
186 if( !array_key_exists( $sc_name, $shortcodes ) ){
187 echo '<p align="center">' . __( 'Invalid shortcode ID or no such shortcode with name [' . esc_attr( $sc_name ) . '] exists !' ) . '</p>';
188 return false;
189 }
190
191 $values = $shortcodes[ $sc_name ];
192
193 }
194
195 $values = wp_parse_args( $values, Shortcoder::defaults() );
196
197 echo '<h3 class="page_title">' . $page_title;
198 echo '<div class="sc_menu">';
199 echo '<a href="' . self::get_link() . '" class="button sc_back_btn"><span class="dashicons dashicons-arrow-left-alt2"></span> ' . __( 'Back', 'shortcoder' ) . '</a>';
200 echo '</div>';
201 echo '</h3>';
202
203 echo '<form method="post">';
204
205 echo '<div class="sc_section">';
206 echo '<label for="sc_name">' . __( 'Name', 'shortcoder' ) . '</label>';
207 echo '<div class="sc_name_wrap"><input type="text" id="sc_name" name="sc_name" value="' . esc_attr( $sc_name ) . '" class="widefat" required="required" ' . ( ( $action == 'edit' ) ? 'readonly="readonly"' : 'placeholder="' . __( 'Enter a name for the shortcode, case sensitive', 'shortcoder' ) . '"' ) . ' pattern="[a-zA-z0-9 \-]+" />';
208 echo ( $action == 'edit' ) ? '<div class="copy_shortcode">Your shortcode is - <strong contenteditable>' . self::get_shortcode( $sc_name ) . '</strong></div>' : '';
209 echo ( $action != 'edit' ) ? '<div class="copy_shortcode">' . __( 'Allowed characters A to Z, a to z, 0 to 9, hyphens, underscores and space', 'shortcoder' ) . '</div>' : '';
210 echo '</div></div>';
211
212 echo '<div class="sc_section">';
213 echo '<label for="sc_content">' . __( 'Shortcode content', 'shortcoder' ) . '</label>';
214 wp_editor( $values[ 'content' ], 'sc_content', array( 'wpautop'=> false, 'textarea_rows'=> 15, 'tinymce' => isset( $g[ 'visual_edit' ] ) ) );
215 echo '</div>';
216
217 echo '<p class="sc_note">' . __( 'Note: You can use any HTML, JavaScript, CSS as shortcode content. Shortcoder does not manipulate the shortcode content. What you provide above is what you get as output. Please verify the shortcode content for any syntax or JavaScript errors.', 'shortcoder' ) . '</p>';
218
219 echo '<h4>' . __( 'Settings', 'shortcoder' ) . '</h4>';
220 echo '<div class="sc_section">';
221 echo '<label><input type="checkbox" name="sc_disable" value="1" ' . checked( $values[ 'disabled' ], '1', false ) . '/> ' . __( 'Temporarily disable this shortcode', 'shortcoder' ) . '</label>';
222 echo '<label><input type="checkbox" name="sc_hide_admin" value="1" ' . checked( $values[ 'hide_admin' ], '1', false ) . '/> ' . __( 'Disable this Shortcode for administrators' ) . '</label>';
223 echo '</div>';
224
225 $device_options = array(
226 'all' => __( 'On both desktop and mobile devices', 'shortcoder' ),
227 'mobile_only' => __( 'On mobile devices alone', 'shortcoder' ),
228 'desktop_only' => __( 'On desktops alone', 'shortcoder' )
229 );
230
231 echo '<h4>' . __( 'Visibility', 'shortcoder' ) . '</h4>';
232 echo '<div class="sc_section">';
233 echo '<label>' . __( 'Show this shortcode', 'shortcoder' );
234 echo '<select name="sc_devices">';
235 foreach( $device_options as $id => $name ){
236 echo '<option value="' . $id . '" ' . selected( $values[ 'devices' ], $id ) . '>' . $name . '</option>';
237 }
238 echo '</select></label>';
239 echo '</div>';
240
241 wp_nonce_field( 'sc_edit_nonce' );
242
243 echo '<footer class="page_footer">';
244 echo '<button class="button button-primary">' . $action_btn . '</button>';
245
246 if( $action == 'edit' ){
247 $delete_link = self::get_link(array(
248 'action' => 'sc_admin_ajax',
249 'do' => 'delete',
250 'id' => base64_encode( $sc_name ),
251 '_wpnonce' => wp_create_nonce( 'sc_delete_nonce' )
252 ), 'admin-ajax.php' );
253 echo '<a href="' . $delete_link . '" class="button sc_delete_ep" title="' . __( 'Delete', 'shortcoder' ) . '"><span class="dashicons dashicons-trash"></span></a>';
254 }
255
256 echo '</footer>';
257
258 echo '</form>';
259
260 $sc_wp_params = Shortcoder::wp_params_list();
261
262 echo '<ul class="params_wrap">';
263 echo '<li>' . __( 'WordPress information', 'shortcoder' ) . '<ul class="wp_params">';
264 foreach( $sc_wp_params as $id => $name ){
265 echo '<li data-id="' . $id . '">' . $name . '</li>';
266 }
267 echo '</ul></li>';
268 echo '<li>' . __( 'Custom parameter', 'shortcoder' ) . '<ul>';
269 echo '<li class="cp_form"><h4>' . __( 'Enter custom parameter name', 'shortcoder' ) . '</h4>';
270 echo '<input type="text" class="cp_box" pattern="[a-zA-Z0-9]+"/> <button class="button cp_btn">' . __( 'Insert parameter', 'shortcoder' ) . '</button><p class="cp_info"><small>' . __( 'Only alphabets and numbers allowed. Custom parameters are case insensitive', 'shortcoder' ) . '</small></p></li>';
271 echo '</ul></li>';
272 echo '</ul>';
273 }
274
275 public static function save_shortcode(){
276
277 if( $_POST && check_admin_referer( 'sc_edit_nonce' ) ){
278
279 $p = wp_parse_args( self::clean_post(), array(
280 'sc_name' => '',
281 'sc_content' => '',
282 'sc_disable' => 0,
283 'sc_hide_admin' => 0,
284 'sc_devices' => 'all',
285 ));
286
287 if( !trim( $p[ 'sc_name' ] ) ){
288 self::print_notice( 0 );
289 return false;
290 }
291
292 $shortcodes = Shortcoder::list_all();
293 $name = self::clean_name( $p[ 'sc_name' ] );
294 $values = array(
295 'content' => $p[ 'sc_content' ],
296 'disabled' => $p[ 'sc_disable' ],
297 'hide_admin' => $p[ 'sc_hide_admin' ],
298 'devices' => $p[ 'sc_devices' ]
299 );
300
301 if( array_key_exists( $name, $shortcodes ) ){
302 self::print_notice( 2 );
303 }else{
304 self::print_notice( 1 );
305 }
306
307 $shortcodes[ $name ] = $values;
308
309 update_option( 'shortcoder_data', $shortcodes );
310
311 /*
312 wp_safe_redirect( self::get_link( array(
313 'action' => 'edit',
314 'name' => urlencode( $name ),
315 'msg' => ( $todo == 'new' ) ? 1 : 2
316 )));*/
317 }
318
319 }
320
321 public static function delete_shortcode( $name ){
322
323 $shortcodes = Shortcoder::list_all();
324
325 if( array_key_exists( $name, $shortcodes ) ){
326 unset( $shortcodes[ $name ] );
327 update_option( 'shortcoder_data', $shortcodes );
328 return true;
329 }else{
330 return false;
331 }
332
333 }
334
335 public static function get_link( $params = array(), $page = 'options-general.php' ){
336
337 $params[ 'page' ] = 'shortcoder';
338 return add_query_arg( $params, admin_url( $page ) );
339
340 }
341
342 public static function get_shortcode( $name = '' ){
343 return esc_attr( '[sc name="' . $name . '"]' );
344 }
345
346 public static function admin_ajax(){
347
348 $g = self::clean_get();
349
350 if( $g[ 'do' ] == 'delete' && isset( $g[ 'id' ] ) && check_admin_referer( 'sc_delete_nonce' ) ){
351 $sc_name = base64_decode( $g[ 'id' ] );
352 if( self::delete_shortcode( $sc_name ) ){
353 echo 'DELETED';
354 }else{
355 echo 'FAILED';
356 }
357 }
358
359 if( $g[ 'do' ] == 'insert_shortcode' ){
360 include_once( 'sc-insert.php' );
361 }
362
363 die(0);
364 }
365
366 public static function add_qt_button(){
367
368 $screen = get_current_screen();
369 if( self::$pagehook == $screen->id )
370 return;
371
372 echo '
373 <script>
374 window.onload = function(){
375 if( typeof QTags === "function" ){
376 QTags.addButton( "QT_sc_insert", "Shortcoder", sc_show_insert );
377 }
378 }
379 function sc_show_insert(){
380 tb_show( "Insert a Shortcode", "' . admin_url( 'admin-ajax.php?action=sc_admin_ajax&do=insert_shortcode&TB_iframe=true' ) . '" );
381 }
382 </script>';
383 }
384
385 public static function register_mce(){
386 add_filter( 'mce_buttons', array( __class__, 'register_mce_button' ) );
387 add_filter( 'mce_external_plugins', array( __class__, 'register_mce_js' ) );
388 }
389
390 public static function register_mce_button( $buttons ){
391
392 if( self::is_sc_admin() )
393 return $buttons;
394
395 array_push( $buttons, 'separator', 'shortcoder' );
396 return $buttons;
397 }
398
399 public static function register_mce_js( $plugins ){
400
401 if( self::is_sc_admin() )
402 return $plugins;
403
404 $plugins[ 'shortcoder' ] = SC_ADMIN_URL . '/js/tinymce/editor_plugin.js';
405 return $plugins;
406 }
407
408 public static function page_bottom(){
409
410 echo '<div class="coffee_box">
411 <div class="coffee_amt_wrap">
412 <p><select class="coffee_amt">
413 <option value="2">$2</option>
414 <option value="3">$3</option>
415 <option value="4">$4</option>
416 <option value="5" selected="selected">$5</option>
417 <option value="6">$6</option>
418 <option value="7">$7</option>
419 <option value="8">$8</option>
420 <option value="9">$9</option>
421 <option value="10">$10</option>
422 <option value="11">$11</option>
423 <option value="12">$12</option>
424 <option value="">Custom</option>
425 </select></p>
426 <a class="button button-primary buy_coffee_btn" href="https://www.paypal.me/vaakash/5" data-link="https://www.paypal.me/vaakash/" target="_blank">Buy me a coffee !</a>
427 </div>
428 <h2>Buy me a coffee !</h2>
429 <p>Thank you for using Shortcoder. If you found the plugin useful buy me a coffee ! Your donation will motivate and make me happy for all the efforts. You can donate via PayPal.</p>';
430 echo '</div>';
431
432 echo '<p class="credits_box"><img src="' . SC_ADMIN_URL . '/images/aw.png" /> Created by <a href="https://goo.gl/aHKnsM" target="_blank">Aakash Chakravarthy</a> - Follow me on <a href="https://twitter.com/vaakash" target="_blank">Twitter</a>, <a href="https://fb.com/aakashweb" target="_blank">Facebook</a>, <a href="https://www.linkedin.com/in/vaakash/" target="_blank">LinkedIn</a>. Check out <a href="https://goo.gl/OAxx4l" target="_blank">my other works</a>.
433
434 <a href="https://goo.gl/ltvnIE" class="rate_link" target="_blank">Rate <span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span> if you like Shortcoder</a>
435
436 </p>';
437
438 }
439
440 public static function top_sharebar(){
441 echo '
442 <div class="top_sharebar">
443
444 <a href="https://goo.gl/r8Qr7Y" class="help_link" target="_blank" title="Help"><span class="dashicons dashicons-editor-help"></span></a>
445 <a href="https://goo.gl/URfxp2" class="help_link" target="_blank" title="Report issue"><span class="dashicons dashicons-flag"></span></a>
446 <a href="https://goo.gl/ltvnIE" class="help_link" target="_blank" title="Rate 5 stars"><span class="dashicons dashicons-star-filled"></span></a>
447
448 <a class="share_btn googleplus" href="https://plus.google.com/share?url=https%3A%2F%2Fwww.aakashweb.com%2Fwordpress-plugins%2Fshortcoder%2F" target="_blank"><span class="dashicons dashicons-googleplus"></span> Share</a>
449 <a class="share_btn twitter" href="https://twitter.com/intent/tweet?ref_src=twsrc%5Etfw&related=vaakash&text=Check%20out%20Shortcoder,%20a%20%23wordpress%20plugin%20to%20create%20shortcodes%20for%20HTML,%20JavaScript%20snippets%20easily&tw_p=tweetbutton&url=https%3A%2F%2Fwww.aakashweb.com%2Fwordpress-plugins%2Fshortcoder%2F&via=vaakash" target="_blank"><span class="dashicons dashicons-twitter"></span> Tweet about Shortcoder</a>
450
451 </div>';
452 }
453
454 public static function action_links( $links ){
455 array_unshift( $links, '<a href="https://goo.gl/qMF3iE" target="_blank">Donate</a>' );
456 array_unshift( $links, '<a href="'. esc_url( admin_url( 'options-general.php?page=shortcoder' ) ) .'">⚙️ Settings</a>' );
457 return $links;
458 }
459
460 public static function print_notice( $id = '' ){
461
462 $g = self::clean_get();
463 $type = 'success';
464 $msg = '';
465
466 if( $id == '' ){
467 if( !isset( $g[ 'msg' ] ) ){
468 return false;
469 }
470 $id = $g[ 'msg' ];
471 }
472
473 if( $id == 0 ){
474 $msg = __( 'Shortcode name is empty. Cannot save settings !', 'shortcoder' );
475 $type = 'error';
476 }
477
478 if( $id == 1 ){
479 $msg = __( 'Shortcode created successfully', 'shortcoder' );
480 }
481
482 if( $id == 2 ){
483 $msg = __( 'Shortcode updated successfully', 'shortcoder' );
484 }
485
486 if( $id == 3 ){
487 $msg = __( 'Shortcode deleted successfully', 'shortcoder' );
488 }
489
490 if( $msg != '' ){
491 echo '<div class="notice notice-' . $type . ' is-dismissible"><p>' . $msg . '</p></div>';
492 }
493 }
494
495 public static function clean_name( $name = '' ){
496
497 return trim( preg_replace('/[^0-9a-zA-Z\- _]/', '', $name ) );
498
499 }
500
501 public static function clean_get(){
502
503 foreach( $_GET as $k=>$v ){
504 $_GET[$k] = sanitize_text_field( $v );
505 }
506
507 return $_GET;
508 }
509
510 public static function clean_post(){
511
512 return stripslashes_deep( $_POST );
513
514 }
515
516 public static function is_sc_admin(){
517
518 if( !function_exists( 'get_current_screen' ) )
519 return false;
520
521 $screen = get_current_screen();
522 if( self::$pagehook == $screen->id ){
523 return true;
524 }else{
525 return false;
526 }
527
528 }
529
530 }
531
532 Shortcoder_Admin::init();
533
534 ?>