| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die("Jog on!"); |
| 4 |
|
| 5 |
/** |
| 6 |
* Include Tinymce button |
| 7 |
*/ |
| 8 |
function sh_cd_tinymce_button_add() { |
| 9 |
|
| 10 |
// Ensure user can edit pages before bringing in libraries |
| 11 |
if ( false === current_user_can('edit_posts') || false === current_user_can('edit_pages') ) { |
| 12 |
return; |
| 13 |
} |
| 14 |
|
| 15 |
// Ensure WYSIWYG is enabled |
| 16 |
if ( 'true' === get_user_option( 'rich_editing' ) ) { |
| 17 |
|
| 18 |
add_filter( 'mce_external_plugins', 'sh_cd_tinymce_button_plugin_js' ); |
| 19 |
add_filter( 'mce_buttons', 'sh_cd_tinymce_button_register' ); |
| 20 |
|
| 21 |
sh_cd_tinymce_js_variables(); |
| 22 |
} |
| 23 |
} |
| 24 |
add_action( 'admin_head', 'sh_cd_tinymce_button_add' ); |
| 25 |
|
| 26 |
/** |
| 27 |
* Output Js config |
| 28 |
*/ |
| 29 |
function sh_cd_tinymce_js_variables() { |
| 30 |
|
| 31 |
$config = [ |
| 32 |
'button-text' => 'Snippet Shortcodes', |
| 33 |
'select-text' => 'Premade Variables', |
| 34 |
'premium' => SH_CD_IS_PREMIUM, |
| 35 |
'upgrade-url' => sh_cd_license_upgrade_link(), |
| 36 |
'upgrade-text' => 'This is a premium feature. Would you like to upgrade Snippet Shortcodes?', |
| 37 |
'dialog-title' => 'Select a shortcode', |
| 38 |
'dialog-label' => 'Shortcode', |
| 39 |
'values-your' => sh_cd_tinymce_js_varables_shortcodes( 'your' ), |
| 40 |
'values-premade' => sh_cd_tinymce_js_varables_shortcodes( 'premade' ) |
| 41 |
]; |
| 42 |
|
| 43 |
printf( '<script type="text/javascript">var sh_cd_tinymce = %s;</script>', json_encode( $config ) ); |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Fetch options for shortcode selects |
| 49 |
* |
| 50 |
* @param $mode |
| 51 |
* |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
function sh_cd_tinymce_js_varables_shortcodes( $mode ) { |
| 55 |
|
| 56 |
$data = []; |
| 57 |
|
| 58 |
if ( false === SH_CD_IS_PREMIUM ) { |
| 59 |
return []; |
| 60 |
} |
| 61 |
|
| 62 |
if ( 'premade' === $mode ) { |
| 63 |
|
| 64 |
$options = sh_cd_presets_both_lists(); |
| 65 |
$options = array_keys( $options ); |
| 66 |
|
| 67 |
} else { |
| 68 |
|
| 69 |
$options = sh_cd_db_shortcodes_all_enabled(); |
| 70 |
$options = wp_list_pluck( $options, 'slug' ); |
| 71 |
} |
| 72 |
|
| 73 |
$formatted = []; |
| 74 |
|
| 75 |
foreach ( $options as $option ) { |
| 76 |
|
| 77 |
$formatted[] = [ |
| 78 |
'value' => sprintf( '[sv slug="%s"]', $option ), |
| 79 |
'text' => $option |
| 80 |
]; |
| 81 |
|
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
return $formatted ; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Add JS to TinyMCE plugins |
| 90 |
* |
| 91 |
* @param $plugins |
| 92 |
* |
| 93 |
* @return mixed |
| 94 |
*/ |
| 95 |
function sh_cd_tinymce_button_plugin_js( $plugins ) { |
| 96 |
|
| 97 |
$plugins[ 'sh_cd_tinymce_button' ] = plugins_url( '../assets/js/tinymce.js', __FILE__ ); |
| 98 |
return $plugins; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Register TinyMCE button |
| 103 |
* |
| 104 |
* @param $buttons |
| 105 |
* |
| 106 |
* @return mixed |
| 107 |
*/ |
| 108 |
function sh_cd_tinymce_button_register( $buttons ) { |
| 109 |
|
| 110 |
array_push( $buttons, 'sh_cd_tinymce_button' ); |
| 111 |
|
| 112 |
return $buttons; |
| 113 |
} |
| 114 |
|