Completed
Push — contact-form/visual-shortcode-... ( 2094e3 )
by George
14:26 queued 04:21
created

Grunion_Editor_View::mce_buttons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * A prototype to allow inline editing / editor views for contact forms.\
5
 *
6
 * Originally developed in: http://github.com/automattic/gm2016-grunion-editor
7
 * Authors: Michael Arestad, Andrew Ozz, and George Stephanis
8
 */
9
10
class Grunion_Editor_View {
11
	public static function add_hooks() {
12
		add_action( 'admin_notices', array( __CLASS__, 'handle_editor_view_js' ) );
13
		add_filter( 'mce_external_plugins', array( __CLASS__, 'mce_external_plugins' ) );
14
		add_filter( 'mce_buttons', array( __CLASS__, 'mce_buttons' ) );
15
		add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
16
	}
17
18
	public static function admin_head() {
19
		remove_action( 'media_buttons', 'grunion_media_button', 999 );
20
	}
21
22
	public static function mce_external_plugins( $plugin_array ) {
23
		$plugin_array['grunion_form'] =  plugins_url( 'js/tinymce-plugin-form-button.js', __FILE__ );
24
		return $plugin_array;
25
	}
26
27
	public static function mce_buttons( $buttons ) {
28
		$size = sizeof( $buttons );
29
		$buttons1 = array_slice( $buttons, 0, $size - 1 );
30
		$buttons2 = array_slice( $buttons, $size - 1 );
31
		return array_merge(
32
			$buttons1,
33
			array( 'grunion' ),
34
			$buttons2
35
		);
36
	}
37
38
	/**
39
	 * WordPress Shortcode Editor View JS Code
40
	 */
41
	public static function handle_editor_view_js() {
42
		$current_screen = get_current_screen();
43
		if ( ! isset( $current_screen->id ) || $current_screen->base !== 'post' ) {
44
			return;
45
		}
46
47
		add_action( 'admin_print_footer_scripts', array( __CLASS__, 'editor_view_js_templates' ), 1 );
48
49
		wp_enqueue_style( 'grunion-editor-ui', plugins_url( 'css/editor-ui.css', __FILE__ ) );
50
		wp_enqueue_script( 'grunion-editor-view', plugins_url( 'js/editor-view.js', __FILE__ ), array( 'wp-util', 'jquery', 'quicktags' ), false, true );
51
		wp_localize_script( 'grunion-editor-view', 'grunionEditorView', array(
52
			'inline_editing_style' => plugins_url( 'css/editor-inline-editing-style.css', __FILE__ ),
53
			'dashicons_css_url'    => includes_url( 'css/dashicons.css' ),
54
			'default_form'  => '[contact-field label="' . __( 'Name', 'jetpack' ) . '" type="name"  required="true" /]' .
55
								'[contact-field label="' . __( 'Email', 'jetpack' )   . '" type="email" required="true" /]' .
56
								'[contact-field label="' . __( 'Website', 'jetpack' ) . '" type="url" /]' .
57
								'[contact-field label="' . __( 'Message', 'jetpack' ) . '" type="textarea" /]',
58
			'labels'      => array(
59
				'submit_button_text'  => __( 'Submit', 'jetpack' ),
60
				/** This filter is documented in modules/contact-form/grunion-contact-form.php */
61
				'required_field_text' => apply_filters( 'jetpack_required_field_text', __( '(required)', 'jetpack' ) ),
62
				'edit_close_ays'      => __( 'Are you sure you\'d like to stop editing this form without saving your changes?', 'jetpack' ),
63
				'quicktags_label'     => __( 'contact form', 'jetpack' ),
64
				'tinymce_label'       => __( 'Add contact form', 'jetpack' ),
65
			)
66
		) );
67
68
		add_editor_style( plugins_url( 'css/editor-style.css', __FILE__ ) );
69
	}
70
71
	/**
72
	 * JS Templates.
73
	 */
74
	public static function editor_view_js_templates() {
75
		?>
76
<script type="text/html" id="tmpl-grunion-contact-form">
77
	<form class="card" action='#' method='post' class='contact-form commentsblock' onsubmit="return false;">
78
		{{{ data.body }}}
79
		<p class='contact-submit'>
80
			<input type='submit' value='{{ data.submit_button_text }}' class='pushbutton-wide'/>
81
		</p>
82
	</form>
83
</script>
84
85
<script type="text/html" id="tmpl-grunion-field-email">
86
	<div>
87
		<label for='{{ data.id }}' class='grunion-field-label email'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
88
		<input type='email' name='{{ data.id }}' id='{{ data.id }}' value='{{ data.value }}' class='{{ data.class }}' placeholder='{{ data.placeholder }}' />
89
	</div>
90
</script>
91
92
<script type="text/html" id="tmpl-grunion-field-telephone">
93
	<div>
94
		<label for='{{ data.id }}' class='grunion-field-label telephone'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
95
		<input type='tel' name='{{ data.id }}' id='{{ data.id }}' value='{{ data.value }}' class='{{ data.class }}' placeholder='{{ data.placeholder }}' />
96
	</div>
97
</script>
98
99
<script type="text/html" id="tmpl-grunion-field-textarea">
100
	<div>
101
		<label for='contact-form-comment-{{ data.id }}' class='grunion-field-label textarea'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
102
		<textarea name='{{ data.id }}' id='contact-form-comment-{{ data.id }}' rows='20' class='{{ data.class }}' placeholder='{{ data.placeholder }}'>{{ data.value }}</textarea>
103
	</div>
104
</script>
105
106
<script type="text/html" id="tmpl-grunion-field-radio">
107
	<div>
108
		<label class='grunion-field-label'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
109
		<# _.each( data.options, function( option ) { #>
110
			<label class='grunion-radio-label radio'>
111
				<input type='radio' name='{{ data.id }}' value='{{ option }}' class="{{ data.class }}" <# if ( option === data.value ) print( "checked='checked'" ) #> />
112
				<span>{{ option }}</span>
113
			</label>
114
		<# }); #>
115
		<div class='clear-form'></div>
116
	</div>
117
</script>
118
119
<script type="text/html" id="tmpl-grunion-field-checkbox">
120
	<div>
121
		<label class='grunion-field-label checkbox'>
122
			<input type='checkbox' name='{{ data.id }}' value='<?php esc_attr__( 'Yes', 'jetpack' ); ?>' class="{{ data.class }}" <# if ( data.value ) print( 'checked="checked"' ) #> />
123
				<span>{{ data.label }}</span><# if ( data.required ) print( " <span>" + data.required + "</span>" ) #>
124
		</label>
125
		<div class='clear-form'></div>
126
	</div>
127
</script>
128
129
<script type="text/html" id="tmpl-grunion-field-checkbox-multiple">
130
	<div>
131
		<label class='grunion-field-label'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
132
		<# _.each( data.options, function( option ) { #>
133
			<label class='grunion-checkbox-multiple-label checkbox-multiple'>
134
				<input type='checkbox' name='{{ data.id }}[]' value='{{ option }}' class="{{ data.class }}" <# if ( option === data.value || _.contains( data.value, option ) ) print( "checked='checked'" ) #> />
135
				<span>{{ option }}</span>
136
			</label>
137
		<# }); #>
138
		<div class='clear-form'></div>
139
	</div>
140
</script>
141
142
<script type="text/html" id="tmpl-grunion-field-select">
143
	<div>
144
		<label for='{{ data.id }}' class='grunion-field-label select'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
145
		<select name='{{ data.id }}' id='{{ data.id }}' class="{{ data.class }}">
146
			<# _.each( data.options, function( option ) { #>
147
				<option <# if ( option === data.value ) print( "selected='selected'" ) #>>{{ option }}</option>
148
			<# }); #>
149
		</select>
150
	</div>
151
</script>
152
153
<script type="text/html" id="tmpl-grunion-field-date">
154
	<div>
155
		<label for='{{ data.id }}' class='grunion-field-label {{ data.type }}'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
156
		<input type='date' name='{{ data.id }}' id='{{ data.id }}' value='{{ data.value }}' class="{{ data.class }}" />
157
	</div>
158
</script>
159
160
<script type="text/html" id="tmpl-grunion-field-text">
161
	<div>
162
		<label for='{{ data.id }}' class='grunion-field-label {{ data.type }}'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
163
		<input type='text' name='{{ data.id }}' id='{{ data.id }}' value='{{ data.value }}' class='{{ data.class }}' placeholder='{{ data.placeholder }}' />
164
	</div>
165
</script>
166
167
168
<script type="text/html" id="tmpl-grunion-field-edit">
169
	<div class="card is-compact grunion-field-edit grunion-field-{{ data.type }}" aria-label="<?php esc_attr_e( 'Form Field', 'jetpack' ); ?>">
170
		<label class="grunion-name">
171
			<span><?php esc_html_e( 'Field Label', 'jetpack' ); ?></span>
172
			<input type="text" name="label" placeholder="<?php esc_attr_e( 'Label', 'jetpack' ); ?>" value="{{ data.label }}"/>
173
		</label>
174
175
		<?php
176
		$grunion_field_types = array(
177
			'text'              => __( 'Text', 'jetpack' ),
178
			'name'              => __( 'Name', 'jetpack' ),
179
			'email'             => __( 'Email', 'jetpack' ),
180
			'url'               => __( 'Website', 'jetpack' ),
181
			'textarea'          => __( 'Textarea', 'jetpack' ),
182
			'checkbox'          => __( 'Checkbox', 'jetpack' ),
183
			'checkbox-multiple' => __( 'Checkbox with Multiple Items', 'jetpack' ),
184
			'select'            => __( 'Drop down', 'jetpack' ),
185
			'radio'             => __( 'Radio', 'jetpack' ),
186
		);
187
		?>
188
		<label class="grunion-type">
189
			<?php esc_html_e( 'Field Type', 'jetpack' ); ?>
190
			<select name="type">
191
				<?php foreach ( $grunion_field_types as $type => $label ) : ?>
192
				<option <# if ( '<?php echo esc_js( $type ); ?>' === data.type ) print( "selected='selected'" ) #> value="<?php echo esc_attr( $type ); ?>">
193
					<?php echo esc_html( $label ); ?>
194
				</option>
195
				<?php endforeach; ?>
196
			</select>
197
		</label>
198
199
		<label class="grunion-required">
200
			<input type="checkbox" name="required" value="1" <# if ( data.required ) print( 'checked="checked"' ) #> />
201
			<span><?php esc_html_e( 'Required?', 'jetpack' ); ?></span>
202
		</label>
203
204
		<label class="grunion-options">
205
			<?php esc_html_e( 'Options', 'jetpack' ); ?>
206
			<ol>
207
				<# if ( data.options ) { #>
208
					<# _.each( data.options, function( option ) { #>
209
						<li><input type="text" name="option" value="{{ option }}" /> <a class="delete-option" href="javascript:;"><span class="screen-reader-text"><?php esc_html_e( 'Delete Option', 'jetpack' ); ?></span></a></li>
210
					<# }); #>
211
				<# } else { #>
212
					<li><input type="text" name="option" /> <a class="delete-option" href="javascript:;"><span class="screen-reader-text"><?php esc_html_e( 'Delete Option', 'jetpack' ); ?></span></a></li>
213
					<li><input type="text" name="option" /> <a class="delete-option" href="javascript:;"><span class="screen-reader-text"><?php esc_html_e( 'Delete Option', 'jetpack' ); ?></span></a></li>
214
					<li><input type="text" name="option" /> <a class="delete-option" href="javascript:;"><span class="screen-reader-text"><?php esc_html_e( 'Delete Option', 'jetpack' ); ?></span></a></li>
215
				<# } #>
216
				<li><a class="add-option" href="javascript:;"><?php esc_html_e( 'Add new option...', 'jetpack' ); ?></a></li>
217
			</ol>
218
		</label>
219
220
		<a href="javascript:;" class="delete-field"><span class="screen-reader-text"><?php esc_html_e( 'Delete Field', 'jetpack' ); ?></span></a>
221
	</div>
222
</script>
223
224
<script type="text/html" id="tmpl-grunion-field-edit-option">
225
	<li><input type="text" name="option" /> <a class="delete-option" href="javascript:;"><span class="screen-reader-text"><?php esc_html_e( 'Delete Option', 'jetpack' ); ?></span></a></li>
226
</script>
227
228
<script type="text/html" id="tmpl-grunion-editor-inline">
229
			<h1 id="form-settings-header" class="grunion-section-header"><?php esc_html_e( 'Form Settings', 'jetpack' ); ?></h1>
230
			<section class="card grunion-form-settings" aria-labelledby="form-settings-header">
231
				<label><?php esc_html_e( 'What would you like the subject of the email to be?', 'jetpack' ); ?>
232
					<input type="text" name="subject" value="{{ data.subject }}" />
233
				</label>
234
				<label><?php esc_html_e( 'Which email address should we send the submissions to?', 'jetpack' ); ?>
235
					<input type="text" name="to" value="{{ data.to }}" />
236
				</label>
237
			</section>
238
			<h1 id="form-fields-header" class="grunion-section-header"><?php esc_html_e( 'Form Fields', 'jetpack' ); ?></h1>
239
			<section class="grunion-fields" aria-labelledby="form-fields-header">
240
				{{{ data.fields }}}
241
			</section>
242
			<section class="buttons">
243
				<?php submit_button( esc_html__( 'Add Field', 'jetpack' ), 'secondary', 'add-field', false ); ?>
244
				<?php submit_button( esc_html__( 'Update Form', 'jetpack' ), 'primary', 'submit', false ); ?>
245
				<?php submit_button( esc_html__( 'Cancel', 'jetpack' ), 'delete', 'cancel', false ); ?>
246
			</section>
247
</script>
248
	<?php
249
	}
250
}
251
252
253
Grunion_Editor_View::add_hooks();
254