AddFormModal.php
5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Add a button to tinyMCE editors when eidting posts/pages.
*
* @since 2.9.22
*/
class NF_Admin_AddFormModal {
function __construct() {
// Add a tinyMCE button to our post and page editor
if( ! apply_filters( 'ninja_forms_hide_add_form_button', false ) ) {
add_action( 'media_buttons', array( $this, 'insert_form_tinymce_buttons' ) );
}
}
/**
* Output our tinyMCE field buttons
*
* @access public
* @since 2.8
* @return void
*/
public function insert_form_tinymce_buttons( $context ) {
global $pagenow;
if( ! in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ){
return;
}
$html = '<style>
span.nf-insert-form {
color:#888;
font: 400 18px/1 dashicons;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
display: inline-block;
width: 18px;
height: 18px;
vertical-align: text-top;
margin: 0 2px 0 0;
}
.ui-autocomplete li {
white-space: normal;
}
</style>';
$html .= '<a href="#" class="button nf-insert-form"><span class="nf-insert-form dashicons dashicons-feedback"></span> ' . esc_html__( 'Add Form', 'ninja-forms' ) . '</a>';
wp_enqueue_script( 'nf-combobox', Ninja_Forms::$url . 'assets/js/lib/combobox.min.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-button', 'jquery-ui-autocomplete' ) );
wp_enqueue_script( 'jBox', Ninja_Forms::$url . 'assets/js/lib/jBox.min.js', array( 'jquery' ) );
wp_enqueue_style( 'jBox', Ninja_Forms::$url . 'assets/css/jBox.css' );
// wp_enqueue_style( 'jquery-smoothness', NINJA_FORMS_URL .'css/smoothness/jquery-smoothness.css' );
?>
<div id="nf-insert-form-modal" style="display:none;">
<p>
<?php
global $wpdb;
$all_forms = $wpdb->get_results( 'SELECT id, title FROM `' . $wpdb->prefix
. 'nf3_forms` ORDER BY title' );
// $all_forms = Ninja_Forms()->form()->get_forms();
// $first_option = __( 'Select a form or type to search', 'ninja-forms' );
echo '<select class="nf-forms-combobox" id="nf-form-select" data-first-option="">';
echo '<option value=""></option>';
foreach( $all_forms as $form ) {
$label = $form->title;
$form_id = $form->id;
if ( strlen( $label ) > 30 )
$label = substr( $label, 0, 30 ) . '...';
echo '<option value="' . intval( $form_id ) . '">' . $label . ' - ID: ' . $form_id . '</option>';
}
echo '</select>';
?>
</p>
<p>
<input type="button" id="nf-insert-form" class="button-primary" value="<?php esc_attr_e( 'Insert', 'ninja-forms' )?>" />
</p>
</div>
<?php
add_action( 'admin_footer', array( $this, 'output_tinymce_button_js' ) );
echo $html;
}
/**
* Output our tinyMCE field buttons
*
* @access public
* @since 2.8
* @return void
*/
public function output_tinymce_button_js( $context ) {
?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
var jBox = jQuery( '.nf-insert-form' ).jBox( 'Modal', {
title: '<?php esc_html_e( 'Insert Form', 'ninja-forms' )?>',
position: {
x: 'center',
y: 'center'
},
closeButton: 'title',
closeOnClick: 'overlay',
closeOnEsc: true,
// theme: 'TooltipBorder',
content: jQuery( '#nf-insert-form-modal' ),
onOpen: function() {
jQuery( '.nf-forms-combobox' ).combobox();
jQuery( this )[0].content.find( '.ui-autocomplete-input' ).attr( 'placeholder', '<?php esc_attr_e( 'Select a form or type to search', 'ninja-forms' )?>' )
.css( 'margin-right', 0 );
jQuery( this )[0].content.find( '.ui-combobox-button' ).css( 'position', 'relative' ).css( 'top', '-3px' );
jQuery( this )[0].content.find( 'ul.ui-autocomplete' ).css( 'max-height', '175px' ).css( 'overflow', 'scroll' );
jQuery( this )[0].content.css( 'overflow', 'visible' );
jQuery( this )[0].content.find( '.ui-icon-triangle-1-s' ).addClass( 'dashicons dashicons-arrow-down' ).css( 'margin-left', '-3px' );
},
onClose: function() {
jQuery( '.nf-forms-combobox' ).combobox( 'destroy' );
}
});
jQuery( document ).on( 'click', '#nf-insert-form', function( e ) {
e.preventDefault();
var form_id = jQuery( '#nf-form-select' ).val();
var shortcode = '[ninja_form id=' + form_id + ']';
window.parent.send_to_editor( shortcode );
jBox.close();
jQuery( '#nf-form-select' ).val( '' );
} );
} );
</script>
<?php
}
}