| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
function accua_forms_register_tinymce_buttons($buttons) { |
| 5 |
array_push($buttons, "|", "accua_fancy_button"); |
| 6 |
return $buttons; |
| 7 |
} |
| 8 |
|
| 9 |
function accua_shortcode_buttons() { |
| 10 |
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) |
| 11 |
return; |
| 12 |
|
| 13 |
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'); |
| 17 |
} |
| 18 |
|
| 19 |
add_action('admin_footer', 'accua_add_nonces_to_js_on_form_editor'); |
| 20 |
} |
| 21 |
|
| 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'); |
| 60 |
} |
| 61 |
|
| 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; |
| 88 |
} |
| 89 |
|