| @@ -1,88 +1,36 @@ | ||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) exit; | |
| 3 | - | |
| 4 | -function accua_forms_register_tinymce_buttons($buttons) { | |
| 2 | +// registers the buttons for use | |
| 3 | +function register_accua_fancy_buttons($buttons) { | |
| 4 | + // inserts a separator between existing buttons and our new one | |
| 5 | + // "accua_fancy_button" is the ID of our button | |
| 5 | 6 | array_push($buttons, "|", "accua_fancy_button"); |
| 6 | 7 | return $buttons; |
| 7 | 8 | } |
| 8 | 9 | |
| 10 | +// filters the tinyMCE buttons and adds our custom buttons | |
| 9 | 11 | function accua_shortcode_buttons() { |
| 12 | + // Don't bother doing this stuff if the current user lacks permissions | |
| 10 | 13 | if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) |
| 11 | 14 | return; |
| 12 | - | |
| 15 | + | |
| 16 | + // Add only in Rich Editor mode | |
| 13 | 17 | if ( get_user_option('rich_editing') == 'true') { |
| 14 | - add_filter("mce_external_plugins", "accua_forms_add_tinymce_plugin"); | |
| 15 | - add_filter('mce_buttons', 'accua_forms_register_tinymce_buttons'); | |
| 16 | - add_action('admin_enqueue_scripts', 'accua_forms_enqueue_shortcode_modal_assets'); | |
| 18 | + // filter the tinyMCE buttons and add our own | |
| 19 | + add_filter("mce_external_plugins", "add_accua_tinymce_plugin"); | |
| 20 | + add_filter('mce_buttons', 'register_accua_fancy_buttons'); | |
| 17 | 21 | } |
| 18 | - | |
| 19 | - add_action('admin_footer', 'accua_add_nonces_to_js_on_form_editor'); | |
| 20 | 22 | } |
| 23 | +// init process for button control | |
| 24 | +add_action('init', 'accua_shortcode_buttons'); | |
| 21 | 25 | |
| 22 | -// Enqueue modal CSS and localize form data — only on post editor screens | |
| 23 | -function accua_forms_enqueue_shortcode_modal_assets($hook) { | |
| 24 | - if ( ! in_array($hook, array('post.php', 'post-new.php'), true) ) { | |
| 25 | - return; | |
| 26 | - } | |
| 27 | - | |
| 28 | - wp_enqueue_style('accua-forms-shortcode-modal', plugins_url('assets/css/shortcode-modal.css', ACCUA_FORMS_FILE), array(), ACCUA_FORMS_CSS_VERSION); | |
| 29 | - | |
| 30 | - do_action('accua_forms_nonces_output'); | |
| 31 | - | |
| 32 | - $forms = get_option('accua_forms_saved_forms', array()); | |
| 33 | - $form_list = array(); | |
| 34 | - foreach ($forms as $fid => $form_data) { | |
| 35 | - $title = !empty($form_data['title']) ? $form_data['title'] : __('(untitled)', 'contact-forms'); | |
| 36 | - $form_list[] = array( | |
| 37 | - 'text' => $title, | |
| 38 | - 'value' => (string) $fid, | |
| 39 | - ); | |
| 40 | - } | |
| 41 | - usort($form_list, function($a, $b) { | |
| 42 | - return (int) $b['value'] - (int) $a['value']; | |
| 43 | - }); | |
| 44 | - | |
| 45 | - // Use wp_add_inline_script on a script guaranteed to be on the page | |
| 46 | - wp_add_inline_script('jquery-core', 'var accua_forms_shortcode = ' . wp_json_encode(array( | |
| 47 | - 'forms' => $form_list, | |
| 48 | - 'i18n' => array( | |
| 49 | - 'title' => __('Insert Contact Form', 'contact-forms'), | |
| 50 | - 'close' => __('Close', 'contact-forms'), | |
| 51 | - 'insert' => __('Insert', 'contact-forms'), | |
| 52 | - 'cancel' => __('Cancel', 'contact-forms'), | |
| 53 | - 'search' => __('Search forms', 'contact-forms'), | |
| 54 | - 'no_results' => __('No forms found.', 'contact-forms'), | |
| 55 | - ), | |
| 56 | - )) . ';' | |
| 57 | - . 'var accua_forms_nonces = { preview_nonce: ' . wp_json_encode(wp_create_nonce('accua_forms_preview')) . ' };' | |
| 58 | - . 'var accua_forms_i18n = { saving: ' . wp_json_encode(__('Saving...', 'contact-forms')) . ', unsaved_changes: ' . wp_json_encode(__('The changes you made will be lost if you navigate away from this page.', 'contact-forms')) . ' };' | |
| 59 | - , 'before'); | |
| 26 | +// add the button to the tinyMCE bar | |
| 27 | +function add_accua_tinymce_plugin($plugin_array) { | |
| 28 | + $plugin_array['accua_fancy_button'] = plugins_url('accua-shortcode-button.js?v='.ACCUA_FORMS_JS_VERSION, ACCUA_FORMS_FILE); | |
| 29 | + return $plugin_array; | |
| 60 | 30 | } |
| 61 | 31 | |
| 62 | -// Add nonces only on Contact Forms admin pages (form editor needs preview_nonce) | |
| 63 | -function accua_add_nonces_to_js_on_form_editor() { | |
| 64 | - $screen = get_current_screen(); | |
| 65 | - if (!$screen || strpos($screen->id, 'accua_forms') === false) { | |
| 66 | - return; | |
| 67 | - } | |
| 68 | - if (did_action('accua_forms_nonces_output')) { | |
| 69 | - return; | |
| 70 | - } | |
| 71 | - do_action('accua_forms_nonces_output'); | |
| 72 | - ?> | |
| 73 | - <script type="text/javascript"> | |
| 74 | - var accua_forms_nonces = accua_forms_nonces || {}; | |
| 75 | - accua_forms_nonces.preview_nonce = '<?php echo esc_js( wp_create_nonce('accua_forms_preview') ); ?>'; | |
| 76 | - var accua_forms_i18n = accua_forms_i18n || {}; | |
| 77 | - accua_forms_i18n.saving = '<?php echo esc_js(__('Saving...', 'contact-forms')); ?>'; | |
| 78 | - accua_forms_i18n.unsaved_changes = '<?php echo esc_js(__('The changes you made will be lost if you navigate away from this page.', 'contact-forms')); ?>'; | |
| 79 | - </script> | |
| 80 | - <?php | |
| 81 | -} | |
| 82 | - | |
| 83 | -add_action('init', 'accua_shortcode_buttons'); | |
| 84 | - | |
| 85 | -function accua_forms_add_tinymce_plugin($plugin_array) { | |
| 86 | - $plugin_array['accua_fancy_button'] = plugins_url('assets/js/admin/shortcode-button.js?v='.ACCUA_FORMS_JS_VERSION, ACCUA_FORMS_FILE); | |
| 87 | - return $plugin_array; | |
| 32 | +add_action('wp_ajax_accua_shortcode_button_popup', 'accua_shortcode_buttons_popup'); | |
| 33 | +function accua_shortcode_buttons_popup() { | |
| 34 | + require_once('accua-shortcode-button-popup.php'); | |
| 35 | + die(''); | |
| 88 | 36 | } |