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