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
339 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.', 'sc' ) . '" value="' . $post->post_name . '" name="post_name" id="post_name" pattern="[a-zA-z0-9\-_]+" required placeholder="' . __( 'Enter shortcode name', 'sc' ) . '" />'; |
| 33 | echo '</div>'; |
| 34 | |
| 35 | echo '<div id="edit-slug-box">'; |
| 36 | echo '<strong>' . __( 'Your shortcode', 'sc' ) . ': </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</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', 'sc' ), array( __class__, 'settings_form' ), SC_POST_TYPE, 'normal', 'default' ); |
| 52 | |
| 53 | add_meta_box( 'sc_mb_coffee', __( 'Buy me a coffee !', 'sc' ), array( __class__, 'coffee_box' ), SC_POST_TYPE, 'side', 'default' ); |
| 54 | |
| 55 | add_meta_box( 'sc_mb_links', __( 'Feedback', 'sc' ), 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', 'sc' ), 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', 'sc' ) |
| 78 | ))), |
| 79 | |
| 80 | array( __( 'Temporarily disable shortcode', 'sc' ), 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.', 'sc' ) |
| 88 | ))), |
| 89 | |
| 90 | array( __( 'Disable shortcode for administrators', 'sc' ), 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.', 'sc' ) |
| 98 | ))), |
| 99 | |
| 100 | array( __( 'Execute shortcode in devices', 'sc' ), 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.', 'sc' ) |
| 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 | return $post; |
| 154 | } |
| 155 | |
| 156 | public static function editor_props( $settings ){ |
| 157 | |
| 158 | $g = SC_Admin::clean_get(); |
| 159 | |
| 160 | $list = array( |
| 161 | 'text' => __( 'Text editor', 'sc' ), |
| 162 | 'visual' => __( 'Visual editor', 'sc' ), |
| 163 | 'code' => __( 'Code editor', 'sc' ) |
| 164 | ); |
| 165 | |
| 166 | $editor = ( isset( $g[ 'editor' ] ) && array_key_exists( $g[ 'editor' ], $list ) ) ? $g[ 'editor' ] : $settings[ '_sc_editor' ]; |
| 167 | |
| 168 | $switch = '<span class="sc_editor_list sc_editor_icon_' . $editor . '">'; |
| 169 | $switch .= '<select name="_sc_editor" class="sc_editor" title="' . __( 'Switch editor', 'sc' ) . '">'; |
| 170 | foreach( $list as $id => $name ){ |
| 171 | $switch .= '<option value="' . $id . '" ' . selected( $editor, $id, false ) . '>' . $name . '</option>'; |
| 172 | } |
| 173 | $switch .= '</select>'; |
| 174 | $switch .= '</span>'; |
| 175 | |
| 176 | return array( |
| 177 | 'active' => $editor, |
| 178 | 'switch_html' => $switch |
| 179 | ); |
| 180 | |
| 181 | } |
| 182 | |
| 183 | public static function editor( $post, $settings ){ |
| 184 | |
| 185 | $editor = self::editor_props( $settings ); |
| 186 | |
| 187 | echo '<div class="hidden">'; |
| 188 | echo '<div class="sc_editor_toolbar">'; |
| 189 | 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>'; |
| 190 | echo $editor[ 'switch_html' ]; |
| 191 | echo '</div>'; |
| 192 | echo '</div>'; |
| 193 | |
| 194 | if( $editor[ 'active' ] == 'code' ){ |
| 195 | echo '<div class="sc_cm_menu"></div>'; |
| 196 | $content = user_can_richedit() ? esc_textarea( $post->post_content ) : $post->post_content; |
| 197 | echo '<textarea name="content" id="content" class="sc_cm_content">' . $content . '</textarea>'; |
| 198 | }else{ |
| 199 | wp_editor( $post->post_content, 'content', array( |
| 200 | 'wpautop'=> false, |
| 201 | 'textarea_rows'=> 20, |
| 202 | 'tinymce' => ( $editor[ 'active' ] == 'visual' ) |
| 203 | )); |
| 204 | } |
| 205 | |
| 206 | } |
| 207 | |
| 208 | public static function enqueue_scripts( $hook ){ |
| 209 | |
| 210 | global $post; |
| 211 | |
| 212 | if( !SC_Admin::is_sc_admin_page() || $hook == 'edit.php' || $hook == 'edit-tags.php' || $hook == 'term.php' ){ |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | $settings = Shortcoder::get_sc_settings( $post->ID ); |
| 217 | $editor = self::editor_props( $settings ); |
| 218 | |
| 219 | wp_localize_script( 'sc-admin-js', 'SC_EDITOR', $editor[ 'active' ] ); |
| 220 | |
| 221 | if( $editor[ 'active' ] != 'code' ){ |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | $cm_cdn_url = 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/'; |
| 226 | $cm_files = array( |
| 227 | 'css' => array( |
| 228 | 'codemirror.min.css' |
| 229 | ), |
| 230 | 'js' => array( |
| 231 | 'codemirror.min.js', |
| 232 | 'mode/htmlmixed/htmlmixed.min.js', |
| 233 | 'mode/css/css.min.js', |
| 234 | 'mode/xml/xml.min.js', |
| 235 | 'mode/javascript/javascript.min.js', |
| 236 | 'addon/selection/active-line.min.js', |
| 237 | 'addon/mode/overlay.min.js' |
| 238 | ) |
| 239 | ); |
| 240 | |
| 241 | foreach( $cm_files as $type => $files ){ |
| 242 | foreach( $files as $index => $file ){ |
| 243 | $url = $cm_cdn_url . $file; |
| 244 | $id = 'sc-cm-' . $index; |
| 245 | if( $type == 'css' ){ |
| 246 | wp_enqueue_style( $id, $url, array(), SC_VERSION ); |
| 247 | }else{ |
| 248 | wp_enqueue_script( $id, $url, array( 'sc-admin-js' ), SC_VERSION ); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | } |
| 254 | |
| 255 | public static function custom_params_list(){ |
| 256 | |
| 257 | $sc_wp_params = Shortcoder::wp_params_list(); |
| 258 | |
| 259 | echo '<ul class="sc_params_list">'; |
| 260 | |
| 261 | foreach( $sc_wp_params as $group => $group_info ){ |
| 262 | echo '<li><span class="dashicons dashicons-' . $group_info['icon'] . '"></span>'; |
| 263 | echo $group_info[ 'name' ]; |
| 264 | echo '<ul class="sc_wp_params">'; |
| 265 | foreach( $group_info[ 'params' ] as $param_id => $param_name ){ |
| 266 | echo '<li data-id="' . $param_id . '">' . $param_name . '</li>'; |
| 267 | } |
| 268 | echo '</ul></li>'; |
| 269 | } |
| 270 | |
| 271 | echo '<li><span class="dashicons dashicons-list-view"></span>' . __( 'Custom parameter', 'shortcoder' ) . '<ul>'; |
| 272 | echo '<li class="sc_params_form"><h4>' . __( 'Enter custom parameter name', 'shortcoder' ) . '</h4>'; |
| 273 | echo '<input type="text" class="sc_cp_box widefat" pattern="[a-zA-Z0-9]+"/>'; |
| 274 | echo '<button class="button sc_cp_btn">' . __( 'Insert parameter', 'shortcoder' ) . '</button>'; |
| 275 | echo '<p class="sc_cp_info"><small>' . __( 'Only alphabets and numbers allowed. Custom parameters are case insensitive', 'shortcoder' ) . '</small></p></li>'; |
| 276 | echo '</ul></li>'; |
| 277 | |
| 278 | echo '<li><span class="dashicons dashicons-screenoptions"></span>' . __( 'Custom Fields', 'shortcoder' ) . '<ul>'; |
| 279 | echo '<li class="sc_params_form"><h4>' . __( 'Enter custom field name', 'shortcoder' ) . '</h4>'; |
| 280 | echo '<input type="text" class="sc_cf_box widefat" pattern="[a-zA-Z0-9_-]+"/>'; |
| 281 | echo '<button class="button sc_cf_btn">' . __( 'Insert custom field', 'shortcoder' ) . '</button>'; |
| 282 | echo '<p class="sc_cf_info"><small>' . __( 'Only alphabets, numbers, underscore and hyphens are allowed. Cannot be empty.', 'shortcoder' ) . '</small></p></li>'; |
| 283 | echo '</ul></li>'; |
| 284 | |
| 285 | echo '</ul>'; |
| 286 | |
| 287 | } |
| 288 | |
| 289 | public static function hidden_section( $post, $settings ){ |
| 290 | |
| 291 | self::custom_params_list(); |
| 292 | |
| 293 | } |
| 294 | |
| 295 | public static function coffee_box( $post ){ |
| 296 | echo '<div class="cfe_mb">'; |
| 297 | echo '<div class="cfe_text">'; |
| 298 | echo '<p>If you like this plugin, buy me a coffee !</p>'; |
| 299 | echo '<img src="' . SC_ADMIN_URL . '/images/coffee.svg" />'; |
| 300 | echo '</div>'; |
| 301 | echo '<div class="cfe_form">'; |
| 302 | echo '<select class="cfe_amt">'; |
| 303 | for($i = 5; $i <= 15; $i++){ |
| 304 | echo '<option value="' . $i . '" ' . ($i == 6 ? 'selected="selected"' : '') . '>$' . $i . '</option>'; |
| 305 | } |
| 306 | echo '<option value="">Custom</option>'; |
| 307 | echo '</select>'; |
| 308 | 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 coffee !</a>'; |
| 309 | echo '</div>'; |
| 310 | echo '</div>'; |
| 311 | } |
| 312 | |
| 313 | public static function feedback( $post ){ |
| 314 | echo '<div class="feedback">'; |
| 315 | echo '<ul> |
| 316 | <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> |
| 317 | <li><a href="https://www.aakashweb.com/forum/discuss/wordpress-plugins/shortcoder/#new-post" target="_blank">Request a feature</a></li> |
| 318 | <li><a href="https://www.aakashweb.com/forum/discuss/wordpress-plugins/shortcoder/#new-post" target="_blank">Report a bug</a></li> |
| 319 | <li><a href="https://www.aakashweb.com/docs/shortcoder-doc/" target="_blank">FAQ</a></li> |
| 320 | <li><a href="https://www.aakashweb.com/wordpress-plugins/" target="_blank">More plugins</a></li> |
| 321 | </ul>'; |
| 322 | echo '</div>'; |
| 323 | } |
| 324 | |
| 325 | public static function footer_text( $text ){ |
| 326 | |
| 327 | if( SC_Admin::is_sc_admin_page() ){ |
| 328 | 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 • <a href="https://www.paypal.me/vaakash/" target="_blank">Buy me a coffee !</a></span>'; |
| 329 | } |
| 330 | |
| 331 | return $text; |
| 332 | |
| 333 | } |
| 334 | |
| 335 | } |
| 336 | |
| 337 | SC_Admin_Edit::init(); |
| 338 | |
| 339 | ?> |