| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die('Jog on!'); |
| 4 |
|
| 5 |
function sh_cd_pages_your_shortcodes_edit( $action = 'add', $save_result = NULL ) { |
| 6 |
|
| 7 |
if ( false === in_array( $action, [ 'add', 'edit', 'save' ] ) ) { |
| 8 |
return; |
| 9 |
} |
| 10 |
|
| 11 |
sh_cd_permission_check(); |
| 12 |
|
| 13 |
// Saving / Inserting a shortcode? |
| 14 |
if ( false === $save_result ) { |
| 15 |
sh_cd_message_display( __( 'There was an error saving your shortcode!' , SH_CD_SLUG ), ! $save_result ); |
| 16 |
} |
| 17 |
global $wpdb; |
| 18 |
|
| 19 |
// Load |
| 20 |
$shortcode = ( false === empty( $_GET['id'] ) ) ? |
| 21 |
sh_cd_db_shortcodes_by_id( (int) $_GET['id'] ) : |
| 22 |
sh_cd_get_values_from_post( [ 'id', 'slug', 'previous_slug', 'data', 'disabled', 'multisite', 'editor' ] ); |
| 23 |
|
| 24 |
$shortcode = apply_filters( 'sh-cd-shortcode-new', $shortcode, $action ); |
| 25 |
|
| 26 |
$shortcode['data'] = stripslashes( $shortcode['data'] ); |
| 27 |
|
| 28 |
$current_editor = ( false === empty( $_GET[ 'editor' ] ) ) ? $_GET[ 'editor' ] : $shortcode[ 'editor' ]; |
| 29 |
|
| 30 |
if ( true === empty( $current_editor ) ) { |
| 31 |
$current_editor = sh_cd_default_editor_get(); |
| 32 |
} |
| 33 |
?> |
| 34 |
<form method="post" action="<?php echo sh_cd_link_your_shortcodes() . '&action=save'; ?>" class="sh-cd-form"> |
| 35 |
|
| 36 |
<div class="wrap"> |
| 37 |
<div id="icon-options-general" class="icon32"></div> |
| 38 |
<div id="poststuff"> |
| 39 |
<div id="post-body" class="metabox-holder columns-3"> |
| 40 |
<div id="post-body-content"> |
| 41 |
<div class="meta-box-sortables ui-sortable"> |
| 42 |
<div class="postbox sh-cd-postbox-edit-slug"> |
| 43 |
<h3 class="postbox-header"> |
| 44 |
<span> |
| 45 |
<?php echo __( 'Slug', SH_CD_SLUG ); ?> |
| 46 |
</span> |
| 47 |
</h3> |
| 48 |
<div style="padding: 0px 15px 0px 15px"> |
| 49 |
<?php |
| 50 |
|
| 51 |
if ( true === $save_result ) : |
| 52 |
|
| 53 |
printf('<p>%1$s.<a href="%2$s">%3$s</a>.</p>', |
| 54 |
__( 'Your shortcode has been saved successfully', SH_CD_SLUG ), |
| 55 |
sh_cd_link_your_shortcodes(), |
| 56 |
__( 'Return to all shortcodes', SH_CD_SLUG ) |
| 57 |
); |
| 58 |
|
| 59 |
else: |
| 60 |
|
| 61 |
?> |
| 62 |
|
| 63 |
<input type="hidden" id="id" name="id" value="<?php echo esc_attr( $shortcode['id'] ); ?>" /> |
| 64 |
<?php wp_nonce_field( 'save-shortcode' ); ?> |
| 65 |
|
| 66 |
<div class="sh-cd-row"> |
| 67 |
<?php if ( true === sh_cd_is_premium() || 'add' === $action ) : ?> |
| 68 |
<input type="text" required class="regular-text sh-cd-slug-validation" size="100" id="slug" name="slug" |
| 69 |
placeholder="Enter a name for your shortcode" title="Only letters, numbers, dashes and underscores are allowed." |
| 70 |
value="<?php echo esc_attr( $shortcode['slug'] )?>" /> |
| 71 |
<?php endif; ?> |
| 72 |
|
| 73 |
<?php $previous_slug = ( false === empty( $shortcode['previous_slug'] ) ) ? $shortcode['previous_slug'] : $shortcode['slug']; ?> |
| 74 |
<input type="hidden" id="previous_slug" name="previous_slug" value="<?php echo esc_attr( $previous_slug )?>" /> |
| 75 |
|
| 76 |
<p class="sh-cd-shortcode-slug"> |
| 77 |
<?php |
| 78 |
$default_slug = ( 'add' === $action ) ? 'enter-a-name-for-your-shortcode' : $previous_slug; |
| 79 |
?> |
| 80 |
<?php echo __( 'Shortcode:', SH_CD_SLUG ); ?><span id="sh-cd-test-shortcode-slug">[<?php echo SH_CD_SHORTCODE; ?> slug="<span id="sh-cd-shortcode-slug-preview"><?php echo esc_html( $default_slug ); ?></span>"]</span> |
| 81 |
<i class="far fa-copy sh-cd-copy-trigger <?php if ( 'add' === $action ): ?>sh-cd-hide<?php endif; ?>" data-clipboard-text="[<?php echo SH_CD_SHORTCODE; ?> slug="<?php echo esc_html( $default_slug ); ?>"]"></i> |
| 82 |
</p> |
| 83 |
</div> |
| 84 |
</div> |
| 85 |
</div> |
| 86 |
<div class="postbox sh-cd-postbox-edit-content"> |
| 87 |
<h3 class="postbox-header"> |
| 88 |
<span> |
| 89 |
<?php echo __( 'Content', SH_CD_SLUG ); ?> |
| 90 |
</span> |
| 91 |
<?php sh_cd_editor_change_button( $current_editor ); ?> |
| 92 |
</h3> |
| 93 |
<div class="sh-cd-postbox-content"> |
| 94 |
<?php |
| 95 |
if ( 'code' !== $current_editor ) { |
| 96 |
wp_editor( $shortcode['data'], 'data', [ 'textarea_name' => 'data' ] ); |
| 97 |
} else { |
| 98 |
printf( ' <textarea id="sh-cd-code-editor" name="data">%s</textarea> |
| 99 |
<script> |
| 100 |
jQuery( document ).ready(function ($) { |
| 101 |
wp.codeEditor.initialize($("#sh-cd-code-editor"), cm_settings); |
| 102 |
}); |
| 103 |
</script>', $shortcode['data'] ); |
| 104 |
} |
| 105 |
|
| 106 |
$premium_icon = sh_cd_display_premium_star(); |
| 107 |
|
| 108 |
?> |
| 109 |
<input type="hidden" id="editor" name="editor" value="<?php echo esc_attr( $current_editor ); ?>" /> |
| 110 |
</div> |
| 111 |
</div> |
| 112 |
<div class="postbox sh-cd-postbox-edit-content"> |
| 113 |
<h3 class="postbox-header"> |
| 114 |
<span> |
| 115 |
<?php echo __( 'Options', SH_CD_SLUG ); ?> |
| 116 |
</span> |
| 117 |
</h3> |
| 118 |
<div class="sh-cd-postbox-content"> |
| 119 |
<?php |
| 120 |
if ( false === apply_filters( 'sh-cd-premium-plugin-likely-to-add-edit-options', false ) ) { |
| 121 |
sh_cd_marketing_page_edit_additional_options(); |
| 122 |
} else { |
| 123 |
do_action( 'sh-cd-page-edit', $shortcode ); // Allow Premium plugin to add additional fields |
| 124 |
} |
| 125 |
?> |
| 126 |
</div> |
| 127 |
</div> |
| 128 |
<div class="sh-cd-button-row"> |
| 129 |
<a class="comment-submit button" href="<?php echo sh_cd_link_your_shortcodes(); ?>"><?php echo __( 'Cancel', SH_CD_SLUG ); ?></a> |
| 130 |
<input name="submit_button" type="submit" value="Save Shortcode" class="comment-submit button button-primary sh-cd-button"> |
| 131 |
</div> |
| 132 |
<?php endif; ?> |
| 133 |
</div> |
| 134 |
</div> |
| 135 |
</div> |
| 136 |
</div> |
| 137 |
</div> |
| 138 |
</form> |
| 139 |
<?php |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Button for toggling editor |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
function sh_cd_editor_change_button( $current_editor = null ) { |
| 148 |
|
| 149 |
if ( true === empty( $current_editor ) ) { |
| 150 |
$current_editor = sh_cd_default_editor_get(); |
| 151 |
} |
| 152 |
|
| 153 |
$editor = ( 'tinymce' === $current_editor ) ? 'code' : 'tinymce'; |
| 154 |
$text = ( 'tinymce' === $current_editor ) ? __( 'Change to HTML Editor', SH_CD_SLUG ) : __( 'Change to WordPress Editor', SH_CD_SLUG ); |
| 155 |
$icon = ( 'tinymce' === $current_editor ) ? 'fa-solid fa-code' : 'fa-brands fa-wordpress'; |
| 156 |
|
| 157 |
$url = add_query_arg( [ 'editor' => $editor ], sh_cd_get_current_url() ); |
| 158 |
|
| 159 |
echo sprintf( '<a type="button" class="button sh-cd-button sh-cd-button-editor-select sh-cd-button-%2$s" href="%1$s" data-new-editor="%2$s"><i class="%3$s"></i> %4$s</a>', |
| 160 |
esc_url( $url ), |
| 161 |
$editor, |
| 162 |
esc_html( $icon ), |
| 163 |
esc_html( $text )); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* If using code editor, load the code editor |
| 168 |
*/ |
| 169 |
add_action( 'admin_enqueue_scripts', function() { |
| 170 |
|
| 171 |
if ( false === sh_cd_is_snippet_shortcodes_admin_page() ) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
// Types found here: https://docs.classicpress.net/reference/functions/wp_enqueue_code_editor/ |
| 176 |
$cm_settings['codeEditor'] = wp_enqueue_code_editor( [ 'type' => 'text/html' ] ); |
| 177 |
wp_localize_script( 'jquery', 'cm_settings', $cm_settings) ; |
| 178 |
|
| 179 |
wp_enqueue_script( 'wp-theme-plugin-editor' ); |
| 180 |
wp_enqueue_style( 'wp-codemirror' ); |
| 181 |
}); |