css
6 years ago
font
6 years ago
images
6 years ago
js
6 years ago
admin.php
6 years ago
edit.php
6 years ago
form.php
6 years ago
insert.php
6 years ago
manage.php
6 years ago
tools.php
6 years ago
edit.php
345 lines
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; |
| 4 | |
| 5 | class SC_Admin_Edit{ |
| 6 | |
| 7 | public static function init(){ |
| 8 | |
| 9 | add_action( 'edit_form_after_title', array( __class__, 'after_title' ) ); |
| 10 | |
| 11 | add_action( 'add_meta_boxes', array( __class__, 'add_meta_boxes' ) ); |
| 12 | |
| 13 | add_action( 'save_post_' . SC_POST_TYPE, array( __class__, 'save_post' ) ); |
| 14 | |
| 15 | add_filter( 'wp_insert_post_data' , array( __class__, 'before_insert_post' ) , 99, 1 ); |
| 16 | |
| 17 | add_action( 'admin_enqueue_scripts', array( __class__, 'enqueue_scripts' ) ); |
| 18 | |
| 19 | add_filter( 'admin_footer_text', array( __class__, 'footer_text' ) ); |
| 20 | |
| 21 | } |
| 22 | |
| 23 | public static function after_title( $post ){ |
| 24 | |
| 25 | if( $post->post_type != SC_POST_TYPE ){ |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | $settings = Shortcoder::get_sc_settings( $post->ID ); |
| 30 | |
| 31 | echo '<div id="sc_name">'; |
| 32 | echo '<input type="text" class="widefat" title="' . __( 'Name of the shortcode. Allowed characters are alphabets, numbers, hyphens and underscore.', 'shortcoder' ) . '" value="' . $post->post_name . '" name="post_name" id="post_name" pattern="[a-zA-z0-9\-_]+" required placeholder="' . __( 'Enter shortcode name', 'shortcoder' ) . '" />'; |
| 33 | echo '</div>'; |
| 34 | |
| 35 | echo '<div id="edit-slug-box">'; |
| 36 | echo '<strong>' . __( 'Your shortcode', 'shortcoder' ) . ': </strong>'; |
| 37 | echo '<code class="sc_preview_text">' . Shortcoder::get_sc_tag( $post->ID ) . '</code>'; |
| 38 | echo '<span id="edit-slug-buttons"><button type="button" class="sc_copy button button-small"><span class="dashicons dashicons-yes"></span> ' . __( 'Copy', 'shortcoder' ) . '</button></span>'; |
| 39 | echo '</div>'; |
| 40 | |
| 41 | // Editor |
| 42 | self::editor( $post, $settings ); |
| 43 | |
| 44 | // Hidden section |
| 45 | self::hidden_section( $post, $settings ); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | public static function add_meta_boxes(){ |
| 50 | |
| 51 | add_meta_box( 'sc_mb_settings', __( 'Shortcode settings', 'shortcoder' ), array( __class__, 'settings_form' ), SC_POST_TYPE, 'normal', 'default' ); |
| 52 | |
| 53 | add_meta_box( 'sc_mb_coffee', __( 'Buy me a coffee !', 'shortcoder' ), array( __class__, 'coffee_box' ), SC_POST_TYPE, 'side', 'default' ); |
| 54 | |
| 55 | add_meta_box( 'sc_mb_links', __( 'Feedback', 'shortcoder' ), array( __class__, 'feedback' ), SC_POST_TYPE, 'side', 'default' ); |
| 56 | |
| 57 | remove_meta_box( 'slugdiv', SC_POST_TYPE, 'normal' ); |
| 58 | |
| 59 | remove_meta_box( 'commentstatusdiv', SC_POST_TYPE, 'normal' ); |
| 60 | |
| 61 | remove_meta_box( 'commentsdiv', SC_POST_TYPE, 'normal' ); |
| 62 | |
| 63 | } |
| 64 | |
| 65 | public static function settings_form( $post ){ |
| 66 | |
| 67 | wp_nonce_field( 'sc_post_nonce', 'sc_nonce' ); |
| 68 | |
| 69 | $settings = Shortcoder::get_sc_settings( $post->ID ); |
| 70 | |
| 71 | $fields = array( |
| 72 | |
| 73 | array( __( 'Display name', 'shortcoder' ), SC_Admin_Form::field( 'text', array( |
| 74 | 'value' => $post->post_title, |
| 75 | 'name' => 'post_title', |
| 76 | 'class' => 'widefat', |
| 77 | 'helper' => __( 'Name of the shortcode to display when it is listed', 'shortcoder' ) |
| 78 | ))), |
| 79 | |
| 80 | array( __( 'Temporarily disable shortcode', 'shortcoder' ), SC_Admin_Form::field( 'select', array( |
| 81 | 'value' => $settings[ '_sc_disable_sc' ], |
| 82 | 'name' => '_sc_disable_sc', |
| 83 | 'list' => array( |
| 84 | 'yes' => 'Yes', |
| 85 | 'no' => 'No' |
| 86 | ), |
| 87 | 'helper' => __( 'Select to disable the shortcode from executing in all the places where it is used.', 'shortcoder' ) |
| 88 | ))), |
| 89 | |
| 90 | array( __( 'Disable shortcode for administrators', 'shortcoder' ), SC_Admin_Form::field( 'select', array( |
| 91 | 'value' => $settings[ '_sc_disable_admin' ], |
| 92 | 'name' => '_sc_disable_admin', |
| 93 | 'list' => array( |
| 94 | 'yes' => 'Yes', |
| 95 | 'no' => 'No' |
| 96 | ), |
| 97 | 'helper' => __( 'Select to disable the shortcode from executing for administrators.', 'shortcoder' ) |
| 98 | ))), |
| 99 | |
| 100 | array( __( 'Execute shortcode in devices', 'shortcoder' ), SC_Admin_Form::field( 'select', array( |
| 101 | 'value' => $settings[ '_sc_allowed_devices' ], |
| 102 | 'name' => '_sc_allowed_devices', |
| 103 | 'list' => array( |
| 104 | 'all' => 'All devices', |
| 105 | 'desktop_only' => 'Desktop only', |
| 106 | 'mobile_only' => 'Mobile only' |
| 107 | ), |
| 108 | 'helper' => __( 'Select the devices where the shortcode should be executed. Note: If any caching plugin is used, a separate caching for desktop and mobile might be required.', 'shortcoder' ) |
| 109 | ))), |
| 110 | |
| 111 | ); |
| 112 | |
| 113 | echo SC_Admin_Form::table($fields); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | public static function save_post( $post_id ){ |
| 118 | |
| 119 | // Checks save status |
| 120 | $is_autosave = wp_is_post_autosave( $post_id ); |
| 121 | $is_revision = wp_is_post_revision( $post_id ); |
| 122 | $is_valid_nonce = ( isset( $_POST[ 'sc_nonce' ] ) && wp_verify_nonce( $_POST[ 'sc_nonce' ], 'sc_post_nonce' ) ); |
| 123 | |
| 124 | // Exits script depending on save status |
| 125 | if ( $is_autosave || $is_revision || !$is_valid_nonce ){ |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | $default_settings = Shortcoder::default_sc_settings(); |
| 130 | |
| 131 | foreach( $default_settings as $key => $val ){ |
| 132 | |
| 133 | if( array_key_exists( $key, $_POST ) ){ |
| 134 | $val = sanitize_text_field( $_POST[ $key ] ); |
| 135 | update_post_meta( $post_id, $key, $val ); |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | |
| 142 | public static function before_insert_post( $post ){ |
| 143 | |
| 144 | if( $post[ 'post_type' ] != SC_POST_TYPE ){ |
| 145 | return $post; |
| 146 | } |
| 147 | |
| 148 | $post_title = trim( $post[ 'post_title' ] ); |
| 149 | if( empty( $post_title ) ){ |
| 150 | $post[ 'post_title' ] = $post[ 'post_name' ]; |
| 151 | } |
| 152 | |
| 153 | if( $_POST && isset( $_POST[ 'sc_content' ] ) ){ |
| 154 | $post[ 'post_content' ] = $_POST[ 'sc_content' ]; |
| 155 | } |
| 156 | |
| 157 | return $post; |
| 158 | } |
| 159 | |
| 160 | public static function editor_props( $settings ){ |
| 161 | |
| 162 | $g = SC_Admin::clean_get(); |
| 163 | |
| 164 | $list = array( |
| 165 | 'text' => __( 'Text editor', 'shortcoder' ), |
| 166 | 'visual' => __( 'Visual editor', 'shortcoder' ), |
| 167 | 'code' => __( 'Code editor', 'shortcoder' ) |
| 168 | ); |
| 169 | |
| 170 | $editor = ( isset( $g[ 'editor' ] ) && array_key_exists( $g[ 'editor' ], $list ) ) ? $g[ 'editor' ] : $settings[ '_sc_editor' ]; |
| 171 | |
| 172 | $switch = '<span class="sc_editor_list sc_editor_icon_' . $editor . '">'; |
| 173 | $switch .= '<select name="_sc_editor" class="sc_editor" title="' . __( 'Switch editor', 'shortcoder' ) . '">'; |
| 174 | foreach( $list as $id => $name ){ |
| 175 | $switch .= '<option value="' . $id . '" ' . selected( $editor, $id, false ) . '>' . $name . '</option>'; |
| 176 | } |
| 177 | $switch .= '</select>'; |
| 178 | $switch .= '</span>'; |
| 179 | |
| 180 | return array( |
| 181 | 'active' => $editor, |
| 182 | 'switch_html' => $switch |
| 183 | ); |
| 184 | |
| 185 | } |
| 186 | |
| 187 | public static function editor( $post, $settings ){ |
| 188 | |
| 189 | $editor = self::editor_props( $settings ); |
| 190 | |
| 191 | echo '<div class="hidden">'; |
| 192 | echo '<div class="sc_editor_toolbar">'; |
| 193 | echo '<button class="button button-primary sc_insert_param"><span class="dashicons dashicons-plus"></span>' . __( 'Insert shortcode parameters', 'shortcoder' ) . '<span class="dashicons dashicons-arrow-down"></span></button>'; |
| 194 | echo $editor[ 'switch_html' ]; |
| 195 | echo '</div>'; |
| 196 | echo '</div>'; |
| 197 | |
| 198 | if( $editor[ 'active' ] == 'code' ){ |
| 199 | echo '<div class="sc_cm_menu"></div>'; |
| 200 | $content = user_can_richedit() ? esc_textarea( $post->post_content ) : $post->post_content; |
| 201 | echo '<textarea name="sc_content" id="sc_content" class="sc_cm_content">' . $content . '</textarea>'; |
| 202 | }else{ |
| 203 | wp_editor( $post->post_content, 'sc_content', array( |
| 204 | 'wpautop'=> false, |
| 205 | 'textarea_rows'=> 20, |
| 206 | 'tinymce' => ( $editor[ 'active' ] == 'visual' ) |
| 207 | )); |
| 208 | } |
| 209 | |
| 210 | } |
| 211 | |
| 212 | public static function enqueue_scripts( $hook ){ |
| 213 | |
| 214 | global $post; |
| 215 | |
| 216 | if( !SC_Admin::is_sc_admin_page() || $hook == 'edit.php' || $hook == 'edit-tags.php' || $hook == 'term.php' ){ |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | $settings = Shortcoder::get_sc_settings( $post->ID ); |
| 221 | $editor = self::editor_props( $settings ); |
| 222 | |
| 223 | wp_localize_script( 'sc-admin-js', 'SC_EDITOR', $editor[ 'active' ] ); |
| 224 | |
| 225 | if( $editor[ 'active' ] != 'code' ){ |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | $cm_cdn_url = 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/'; |
| 230 | $cm_files = array( |
| 231 | 'css' => array( |
| 232 | 'codemirror.min.css' |
| 233 | ), |
| 234 | 'js' => array( |
| 235 | 'codemirror.min.js', |
| 236 | 'mode/htmlmixed/htmlmixed.min.js', |
| 237 | 'mode/css/css.min.js', |
| 238 | 'mode/xml/xml.min.js', |
| 239 | 'mode/javascript/javascript.min.js', |
| 240 | 'addon/selection/active-line.min.js', |
| 241 | 'addon/mode/overlay.min.js' |
| 242 | ) |
| 243 | ); |
| 244 | |
| 245 | foreach( $cm_files as $type => $files ){ |
| 246 | foreach( $files as $index => $file ){ |
| 247 | $url = $cm_cdn_url . $file; |
| 248 | $id = 'sc-cm-' . $index; |
| 249 | if( $type == 'css' ){ |
| 250 | wp_enqueue_style( $id, $url, array(), SC_VERSION ); |
| 251 | }else{ |
| 252 | wp_enqueue_script( $id, $url, array( 'sc-admin-js' ), SC_VERSION ); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | } |
| 258 | |
| 259 | public static function custom_params_list(){ |
| 260 | |
| 261 | $sc_wp_params = Shortcoder::wp_params_list(); |
| 262 | |
| 263 | echo '<ul class="sc_params_list">'; |
| 264 | |
| 265 | foreach( $sc_wp_params as $group => $group_info ){ |
| 266 | echo '<li><span class="dashicons dashicons-' . $group_info['icon'] . '"></span>'; |
| 267 | echo $group_info[ 'name' ]; |
| 268 | echo '<ul class="sc_wp_params">'; |
| 269 | foreach( $group_info[ 'params' ] as $param_id => $param_name ){ |
| 270 | echo '<li data-id="' . $param_id . '">' . $param_name . '</li>'; |
| 271 | } |
| 272 | echo '</ul></li>'; |
| 273 | } |
| 274 | |
| 275 | echo '<li><span class="dashicons dashicons-list-view"></span>' . __( 'Custom parameter', 'shortcoder' ) . '<ul>'; |
| 276 | echo '<li class="sc_params_form"><h4>' . __( 'Enter custom parameter name', 'shortcoder' ) . '</h4>'; |
| 277 | echo '<input type="text" class="sc_cp_box widefat" pattern="[a-zA-Z0-9_]+"/>'; |
| 278 | echo '<h4>' . __( 'Default value', 'shortcoder' ) . '</h4>'; |
| 279 | echo '<input type="text" class="sc_cp_default widefat"/>'; |
| 280 | echo '<button class="button sc_cp_btn">' . __( 'Insert parameter', 'shortcoder' ) . '</button>'; |
| 281 | echo '<p class="sc_cp_info"><small>' . __( 'Only alphabets, numbers and underscores are allowed. Custom parameters are case insensitive', 'shortcoder' ) . '</small></p></li>'; |
| 282 | echo '</ul></li>'; |
| 283 | |
| 284 | echo '<li><span class="dashicons dashicons-screenoptions"></span>' . __( 'Custom Fields', 'shortcoder' ) . '<ul>'; |
| 285 | echo '<li class="sc_params_form"><h4>' . __( 'Enter custom field name', 'shortcoder' ) . '</h4>'; |
| 286 | echo '<input type="text" class="sc_cf_box widefat" pattern="[a-zA-Z0-9_-]+"/>'; |
| 287 | echo '<button class="button sc_cf_btn">' . __( 'Insert custom field', 'shortcoder' ) . '</button>'; |
| 288 | echo '<p class="sc_cf_info"><small>' . __( 'Only alphabets, numbers, underscore and hyphens are allowed. Cannot be empty.', 'shortcoder' ) . '</small></p></li>'; |
| 289 | echo '</ul></li>'; |
| 290 | |
| 291 | echo '</ul>'; |
| 292 | |
| 293 | } |
| 294 | |
| 295 | public static function hidden_section( $post, $settings ){ |
| 296 | |
| 297 | self::custom_params_list(); |
| 298 | |
| 299 | } |
| 300 | |
| 301 | public static function coffee_box( $post ){ |
| 302 | echo '<div class="cfe_mb">'; |
| 303 | echo '<div class="cfe_text">'; |
| 304 | echo '<p>If you like this plugin, buy me a coffee !</p>'; |
| 305 | echo '<img src="' . SC_ADMIN_URL . '/images/coffee.svg" />'; |
| 306 | echo '</div>'; |
| 307 | echo '<div class="cfe_form">'; |
| 308 | echo '<select class="cfe_amt">'; |
| 309 | for($i = 5; $i <= 15; $i++){ |
| 310 | echo '<option value="' . $i . '" ' . ($i == 6 ? 'selected="selected"' : '') . '>$' . $i . '</option>'; |
| 311 | } |
| 312 | echo '<option value="">Custom</option>'; |
| 313 | echo '</select>'; |
| 314 | echo '<a class="button button-primary cfe_btn" href="https://www.paypal.me/vaakash/6" data-link="https://www.paypal.me/vaakash/" target="_blank">' . __( 'Buy me a coffee !', 'shortcoder' ) . '</a>'; |
| 315 | echo '</div>'; |
| 316 | echo '</div>'; |
| 317 | } |
| 318 | |
| 319 | public static function feedback( $post ){ |
| 320 | echo '<div class="feedback">'; |
| 321 | echo '<ul> |
| 322 | <li><a href="https://wordpress.org/support/plugin/shortcoder/reviews/?rate=5#new-post" target="_blank">Rate and review</a><div class="stars"><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></div></li> |
| 323 | <li><a href="https://www.aakashweb.com/forum/discuss/wordpress-plugins/shortcoder/#new-post" target="_blank">Support forum</a> / <a href="https://www.aakashweb.com/docs/shortcoder-doc/" target="_blank">FAQ</a></li> |
| 324 | <li class="adjust_icons"><a href="https://twitter.com/aakashweb" target="_blank">Follow on Twitter <span class="dashicons dashicons-twitter"></span></a> <a href="https://facebook.com/aakashweb" target="_blank">Facebook <span class="dashicons dashicons-facebook"></span></a></li> |
| 325 | <li><a href="https://www.aakashweb.com/wordpress-plugins/" target="_blank">More plugins</a></li> |
| 326 | <li class="ufw"><a href="https://www.aakashweb.com/wordpress-plugins/ultimate-floating-widgets/" target="_blank"><i>Check out</i> - <strong>Ultimate floating widgets</strong></a><span>A WordPress plugin to create floating widgets from the same developer</span></li> |
| 327 | </ul>'; |
| 328 | echo '</div>'; |
| 329 | } |
| 330 | |
| 331 | public static function footer_text( $text ){ |
| 332 | |
| 333 | if( SC_Admin::is_sc_admin_page() ){ |
| 334 | return '<span class="footer_thanks">Thanks for using <a href="https://www.aakashweb.com/wordpress-plugins/shortcoder/" target="_blank">Shortcoder</a> • Please <a href="https://wordpress.org/support/plugin/shortcoder/reviews/?rate=5#new-post" target="_blank">rate 5 stars</a> and spread the word.</span>'; |
| 335 | } |
| 336 | |
| 337 | return $text; |
| 338 | |
| 339 | } |
| 340 | |
| 341 | } |
| 342 | |
| 343 | SC_Admin_Edit::init(); |
| 344 | |
| 345 | ?> |