PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 4.0.2
Shortcoder — Create Shortcodes for Anything v4.0.2
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 9 years ago images 9 years ago js 9 years ago sc-admin.php 9 years ago sc-insert.php 9 years ago
sc-admin.php
510 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 $shortcodes = Shortcoder::list_all();
87
88 echo '<h3 class="page_title">' . __( 'List of shortcodes created', 'shortcoder' );
89 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>';
90 echo '</h3>';
91
92 echo '<ul class="sc_list" data-empty="' . __( 'No shortcodes are created. Go ahead create one !', 'shortcoder' ) . '">';
93 foreach( $shortcodes as $name => $data ){
94
95 $data = wp_parse_args( $data, Shortcoder::defaults() );
96
97 $link = self::get_link(array(
98 'action' => 'edit',
99 'name' => $name
100 ));
101
102 $delete_link = self::get_link(array(
103 'action' => 'sc_admin_ajax',
104 'do' => 'delete',
105 'name' => $name,
106 '_wpnonce' => wp_create_nonce( 'sc_delete_nonce' )
107 ), 'admin-ajax.php' );
108
109 $disabled_text = ( $data[ 'disabled' ] == '1' ) ? '<small class="disabled_text">' . __( 'Temporarily disabled', 'shortcoder' ) . '</small>' : '';
110
111 echo '<li>';
112 echo '<a href="' . $link . '" class="sc_link" title="' . __( 'Edit shortcode', 'shortcoder' ) . '">' . $name . $disabled_text . '</a>';
113 echo '<span class="sc_controls">';
114 echo '<a href="#" class="sc_copy" title="' . __( 'Copy shortcode', 'shortcoder' ) . '"><span class="dashicons dashicons-editor-code"></span></a>';
115 echo '<a href="' . $delete_link . '" class="sc_delete" title="' . __( 'Delete', 'shortcoder' ) . '"><span class="dashicons dashicons-trash"></span></a>';
116 echo '</span>';
117
118 echo '<input type="text" value="' . self::get_shortcode( $name ) . '" class="sc_copy_box" readonly="readonly" title="' . __( 'Copy shortcode', 'shortcoder' ) . '" />';
119
120 echo '</li>';
121
122 }
123 echo '</ul>';
124
125
126
127 }
128
129 public static function new_shortcode(){
130 self::edit_shortcode( 'new' );
131 }
132
133 public static function edit_shortcode( $action = 'edit' ){
134
135 self::save_shortcode();
136
137 $shortcodes = Shortcoder::list_all();
138 $g = self::clean_get();
139
140 $page_title = __( 'New shortcode', 'shortcoder' );
141 $action_btn = __( 'Create shortcode', 'shortcoder' );
142 $sc_name = '';
143 $values = array();
144
145 if( $action == 'edit' ){
146
147 $page_title = __( 'Edit shortcode', 'shortcoder' );
148 $action_btn = __( 'Save settings', 'shortcoder' );
149
150 if( !( isset( $g[ 'name' ] ) && array_key_exists( $g[ 'name' ], $shortcodes ) ) ){
151 echo '<p align="center">' . __( 'Invalid shortcode or Shortcode does not exist !' ) . '</p>';
152 return false;
153 }
154
155 $sc_name = $g[ 'name' ];
156 $values = $shortcodes[ $sc_name ];
157
158 }
159
160 $values = wp_parse_args( $values, Shortcoder::defaults() );
161
162 echo '<h3 class="page_title">' . $page_title;
163 echo '<a href="' . self::get_link() . '" class="button sc_back_btn"><span class="dashicons dashicons-arrow-left-alt2"></span> ' . __( 'Back', 'shortcoder' ) . '</a>';
164 echo '</h3>';
165
166 echo '<form method="post">';
167
168 echo '<div class="sc_section">';
169 echo '<label for="sc_name">' . __( 'Name', 'shortcoder' ) . '</label>';
170 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 \-]+" title="' . __( 'Allowed characters A to Z, a to z, 0 to 9, hyphens, underscores and space', 'shortcoder' ) . '" />';
171 echo ( $action == 'edit' ) ? '<div class="copy_shortcode">Your shortcode is - <strong contenteditable>' . self::get_shortcode( $sc_name ) . '</strong></div>' : '';
172 echo '</div></div>';
173
174 echo '<div class="sc_section">';
175 echo '<label for="sc_content">' . __( 'Shortcode content', 'shortcoder' ) . '</label>';
176 wp_editor( $values[ 'content' ], 'sc_content', array( 'wpautop'=> false, 'textarea_rows'=> 12 ) );
177 echo '</div>';
178
179 echo '<h4>' . __( 'Settings', 'shortcoder' ) . '</h4>';
180 echo '<div class="sc_section">';
181 echo '<label><input type="checkbox" name="sc_disable" value="1" ' . checked( $values[ 'disabled' ], '1', false ) . '/> ' . __( 'Temporarily disable this shortcode', 'shortcoder' ) . '</label>';
182 echo '<label><input type="checkbox" name="sc_hide_admin" value="1" ' . checked( $values[ 'hide_admin' ], '1', false ) . '/> ' . __( 'Disable this Shortcode for administrators' ) . '</label>';
183 echo '</div>';
184
185 $device_options = array(
186 'all' => __( 'On both desktop and mobile devices', 'shortcoder' ),
187 'mobile_only' => __( 'On mobile devices alone', 'shortcoder' ),
188 'desktop_only' => __( 'On desktops alone', 'shortcoder' )
189 );
190
191 echo '<h4>' . __( 'Visibility', 'shortcoder' ) . '</h4>';
192 echo '<div class="sc_section">';
193 echo '<label>' . __( 'Show this shortcode', 'shortcoder' );
194 echo '<select name="sc_devices">';
195 foreach( $device_options as $id => $name ){
196 echo '<option value="' . $id . '" ' . selected( $values[ 'devices' ], $id ) . '>' . $name . '</option>';
197 }
198 echo '</select></label>';
199 echo '</div>';
200
201 wp_nonce_field( 'sc_edit_nonce' );
202
203 echo '<footer class="page_footer">';
204 echo '<button class="button button-primary">' . $action_btn . '</button>';
205
206 if( $action == 'edit' ){
207 $delete_link = self::get_link(array(
208 'action' => 'sc_admin_ajax',
209 'do' => 'delete',
210 'name' => $sc_name,
211 '_wpnonce' => wp_create_nonce( 'sc_delete_nonce' )
212 ), 'admin-ajax.php' );
213 echo '<a href="' . $delete_link . '" class="button sc_delete_ep" title="' . __( 'Delete', 'shortcoder' ) . '"><span class="dashicons dashicons-trash"></span></a>';
214 }
215
216 echo '</footer>';
217
218 echo '</form>';
219
220 $sc_wp_params = Shortcoder::wp_params_list();
221
222 echo '<ul class="params_wrap">';
223 echo '<li>' . __( 'WordPress information', 'shortcoder' ) . '<ul class="wp_params">';
224 foreach( $sc_wp_params as $id => $name ){
225 echo '<li data-id="' . $id . '">' . $name . '</li>';
226 }
227 echo '</ul></li>';
228 echo '<li>' . __( 'Custom parameter', 'shortcoder' ) . '<ul>';
229 echo '<li class="cp_form"><h4>' . __( 'Enter custom parameter name', 'shortcoder' ) . '</h4>';
230 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>';
231 echo '</ul></li>';
232 echo '</ul>';
233 }
234
235 public static function save_shortcode(){
236
237 if( $_POST && check_admin_referer( 'sc_edit_nonce' ) ){
238
239 $p = wp_parse_args( self::clean_post(), array(
240 'sc_name' => '',
241 'sc_content' => '',
242 'sc_disable' => 0,
243 'sc_hide_admin' => 0,
244 'sc_devices' => 'all',
245 ));
246
247 if( !trim( $p[ 'sc_name' ] ) ){
248 self::print_notice( 0 );
249 return false;
250 }
251
252 $shortcodes = Shortcoder::list_all();
253 $name = self::clean_name( $p[ 'sc_name' ] );
254 $values = array(
255 'content' => $p[ 'sc_content' ],
256 'disabled' => $p[ 'sc_disable' ],
257 'hide_admin' => $p[ 'sc_hide_admin' ],
258 'devices' => $p[ 'sc_devices' ]
259 );
260
261 if( array_key_exists( $name, $shortcodes ) ){
262 self::print_notice( 2 );
263 }else{
264 self::print_notice( 1 );
265 }
266
267 $shortcodes[ $name ] = $values;
268
269 update_option( 'shortcoder_data', $shortcodes );
270
271 /*
272 wp_safe_redirect( self::get_link( array(
273 'action' => 'edit',
274 'name' => urlencode( $name ),
275 'msg' => ( $todo == 'new' ) ? 1 : 2
276 )));*/
277 }
278
279 }
280
281 public static function delete_shortcode( $name ){
282
283 $shortcodes = Shortcoder::list_all();
284
285 if( array_key_exists( $name, $shortcodes ) ){
286 unset( $shortcodes[ $name ] );
287 update_option( 'shortcoder_data', $shortcodes );
288 return true;
289 }else{
290 return false;
291 }
292
293 }
294
295 public static function get_link( $params = array(), $page = 'options-general.php' ){
296
297 $params[ 'page' ] = 'shortcoder';
298 return add_query_arg( $params, admin_url( $page ) );
299
300 }
301
302 public static function get_shortcode( $name = '' ){
303 return esc_attr( '[sc name="' . $name . '"]' );
304 }
305
306 public static function admin_ajax(){
307
308 $g = self::clean_get();
309
310 if( $g[ 'do' ] == 'delete' && isset( $g[ 'name' ] ) && check_admin_referer( 'sc_delete_nonce' ) ){
311 if( self::delete_shortcode( $g[ 'name' ] ) ){
312 echo 'DELETED';
313 }else{
314 echo 'FAILED';
315 }
316 }
317
318 if( $g[ 'do' ] == 'insert_shortcode' ){
319 include_once( 'sc-insert.php' );
320 }
321
322 die(0);
323 }
324
325 public static function add_qt_button(){
326
327 $screen = get_current_screen();
328 if( self::$pagehook == $screen->id )
329 return;
330
331 echo '
332 <script>
333 window.onload = function(){
334 if( typeof QTags === "function" ){
335 QTags.addButton( "QT_sc_insert", "Shortcoder", sc_show_insert );
336 }
337 }
338 function sc_show_insert(){
339 tb_show( "Insert a Shortcode", "' . admin_url( 'admin-ajax.php?action=sc_admin_ajax&do=insert_shortcode&TB_iframe=true' ) . '" );
340 }
341 </script>';
342 }
343
344 public static function register_mce(){
345 add_filter( 'mce_buttons', array( __class__, 'register_mce_button' ) );
346 add_filter( 'mce_external_plugins', array( __class__, 'register_mce_js' ) );
347 }
348
349 public static function register_mce_button( $buttons ){
350
351 if( self::is_sc_admin() )
352 return $buttons;
353
354 array_push( $buttons, 'separator', 'shortcoder' );
355 return $buttons;
356 }
357
358 public static function register_mce_js( $plugins ){
359
360 if( self::is_sc_admin() )
361 return $plugins;
362
363 $plugins[ 'shortcoder' ] = SC_ADMIN_URL . '/js/tinymce/editor_plugin.js';
364 return $plugins;
365 }
366
367 public static function page_bottom(){
368
369 echo '<div class="coffee_box">
370 <div class="coffee_amt_wrap">
371 <p><select class="coffee_amt">
372 <option value="2">$2</option>
373 <option value="3">$3</option>
374 <option value="4">$4</option>
375 <option value="5" selected="selected">$5</option>
376 <option value="6">$6</option>
377 <option value="7">$7</option>
378 <option value="8">$8</option>
379 <option value="9">$9</option>
380 <option value="10">$10</option>
381 <option value="11">$11</option>
382 <option value="12">$12</option>
383 <option value="">Custom</option>
384 </select></p>
385 <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>
386 </div>
387 <h2>Buy me a coffee !</h2>
388 <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>';
389 echo '</div>';
390
391 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>.
392
393 <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>
394
395 </p>';
396
397 }
398
399 public static function top_sharebar(){
400 echo '
401 <div class="top_sharebar">
402
403 <a href="https://goo.gl/r8Qr7Y" class="help_link" target="_blank" title="Help"><span class="dashicons dashicons-editor-help"></span></a>
404 <a href="https://goo.gl/URfxp2" class="help_link" target="_blank" title="Report issue"><span class="dashicons dashicons-flag"></span></a>
405
406 <div class="a2a_kit a2a_kit_size_32 a2a_default_style">
407 <a class="a2a_button_twitter"></a>
408 <a class="a2a_button_facebook"></a>
409 <a class="a2a_button_google_plus"></a>
410 <a class="a2a_dd" href="https://www.addtoany.com/share"></a>
411 </div>
412
413 <div class="admin_sb share_text">Share &amp; talk about<br/> Shortcoder</div>
414
415 </div>';
416
417 echo '
418 <script>
419 var a2a_config = a2a_config || {};
420 a2a_config.linkname = "Shortcoder - A WordPress plugin to create custom Shortcodes for HTML, Javascript snippets !";
421 a2a_config.linkurl = "https://www.aakashweb.com/wordpress-plugins/shortcoder/";
422 a2a_config.templates = {
423 twitter: "Check out ${title} ${link} - @vaakash"
424 };
425 </script>
426 <script async src="https://static.addtoany.com/menu/page.js"></script>
427 ';
428 }
429
430 public static function action_links( $links ){
431 array_unshift( $links, '<a href="https://goo.gl/qMF3iE" target="_blank">Donate</a>' );
432 array_unshift( $links, '<a href="'. esc_url( admin_url( 'options-general.php?page=shortcoder' ) ) .'">⚙️ Settings</a>' );
433 return $links;
434 }
435
436 public static function print_notice( $id = '' ){
437
438 $g = self::clean_get();
439 $type = 'success';
440 $msg = '';
441
442 if( $id == '' ){
443 if( !isset( $g[ 'msg' ] ) ){
444 return false;
445 }
446 $id = $g[ 'msg' ];
447 }
448
449 if( $id == 0 ){
450 $msg = __( 'Shortcode name is empty. Cannot save settings !', 'shortcoder' );
451 $type = 'error';
452 }
453
454 if( $id == 1 ){
455 $msg = __( 'Shortcode created successfully', 'shortcoder' );
456 }
457
458 if( $id == 2 ){
459 $msg = __( 'Shortcode updated successfully', 'shortcoder' );
460 }
461
462 if( $id == 3 ){
463 $msg = __( 'Shortcode deleted successfully', 'shortcoder' );
464 }
465
466 if( $msg != '' ){
467 echo '<div class="notice notice-' . $type . ' is-dismissible"><p>' . $msg . '</p></div>';
468 }
469 }
470
471 public static function clean_name( $name = '' ){
472
473 return trim( preg_replace('/[^0-9a-zA-Z\- _]/', '', $name ) );
474
475 }
476
477 public static function clean_get(){
478
479 foreach( $_GET as $k=>$v ){
480 $_GET[$k] = sanitize_text_field( $v );
481 }
482
483 return $_GET;
484 }
485
486 public static function clean_post(){
487
488 return stripslashes_deep( $_POST );
489
490 }
491
492 public static function is_sc_admin(){
493
494 if( !function_exists( 'get_current_screen' ) )
495 return false;
496
497 $screen = get_current_screen();
498 if( self::$pagehook == $screen->id ){
499 return true;
500 }else{
501 return false;
502 }
503
504 }
505
506 }
507
508 Shortcoder_Admin::init();
509
510 ?>