Completed
Push — sync/dbtlr/r159298-wpcom-15003... ( bc0eb2...c27aa0 )
by
unknown
12:20
created

Grunion_Editor_View::admin_head()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
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_style_add_data( 'grunion-editor-ui', 'rtl', 'replace' );
51
		wp_enqueue_script( 'grunion-editor-view', plugins_url( 'js/editor-view.js', __FILE__ ), array( 'wp-util', 'jquery', 'quicktags' ), false, true );
52
		wp_localize_script( 'grunion-editor-view', 'grunionEditorView', array(
53
			'inline_editing_style' => plugins_url( 'css/editor-inline-editing-style.css', __FILE__ ),
54
			'inline_editing_style_rtl' => plugins_url( 'css/editor-inline-editing-style-rtl.css', __FILE__ ),
55
			'dashicons_css_url'    => includes_url( 'css/dashicons.css' ),
56
			'default_form'  => '[contact-field label="' . __( 'Name', 'jetpack' ) . '" type="name"  required="true" /]' .
57
								'[contact-field label="' . __( 'Email', 'jetpack' )   . '" type="email" required="true" /]' .
58
								'[contact-field label="' . __( 'Website', 'jetpack' ) . '" type="url" /]' .
59
								'[contact-field label="' . __( 'Message', 'jetpack' ) . '" type="textarea" /]',
60
			'labels'      => array(
61
				'submit_button_text'  => __( 'Submit', 'jetpack' ),
62
				/** This filter is documented in modules/contact-form/grunion-contact-form.php */
63
				'required_field_text' => apply_filters( 'jetpack_required_field_text', __( '(required)', 'jetpack' ) ),
64
				'edit_close_ays'      => __( 'Are you sure you\'d like to stop editing this form without saving your changes?', 'jetpack' ),
65
				'quicktags_label'     => __( 'contact form', 'jetpack' ),
66
				'tinymce_label'       => __( 'Add contact form', 'jetpack' ),
67
			)
68
		) );
69
70
		add_editor_style( plugins_url( 'css/editor-style.css', __FILE__ ) );
71
	}
72
73
	/**
74
	 * JS Templates.
75
	 */
76
	public static function editor_view_js_templates() {
77
		?>
78
<script type="text/html" id="tmpl-grunion-contact-form">
79
	<form class="card" action='#' method='post' class='contact-form commentsblock' onsubmit="return false;">
80
		{{{ data.body }}}
81
		<p class='contact-submit'>
82
			<input type='submit' value='{{ data.submit_button_text }}' class='pushbutton-wide'/>
83
		</p>
84
	</form>
85
</script>
86
87
<script type="text/html" id="tmpl-grunion-field-email">
88
	<div>
89
		<label for='{{ data.id }}' class='grunion-field-label email'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
90
		<input type='email' name='{{ data.id }}' id='{{ data.id }}' value='{{ data.value }}' class='{{ data.class }}' placeholder='{{ data.placeholder }}' />
91
	</div>
92
</script>
93
94
<script type="text/html" id="tmpl-grunion-field-telephone">
95
	<div>
96
		<label for='{{ data.id }}' class='grunion-field-label telephone'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
97
		<input type='tel' name='{{ data.id }}' id='{{ data.id }}' value='{{ data.value }}' class='{{ data.class }}' placeholder='{{ data.placeholder }}' />
98
	</div>
99
</script>
100
101
<script type="text/html" id="tmpl-grunion-field-textarea">
102
	<div>
103
		<label for='contact-form-comment-{{ data.id }}' class='grunion-field-label textarea'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
104
		<textarea name='{{ data.id }}' id='contact-form-comment-{{ data.id }}' rows='20' class='{{ data.class }}' placeholder='{{ data.placeholder }}'>{{ data.value }}</textarea>
105
	</div>
106
</script>
107
108
<script type="text/html" id="tmpl-grunion-field-radio">
109
	<div>
110
		<label class='grunion-field-label'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
111
		<# _.each( data.options, function( option ) { #>
112
			<label class='grunion-radio-label radio'>
113
				<input type='radio' name='{{ data.id }}' value='{{ option }}' class="{{ data.class }}" <# if ( option === data.value ) print( "checked='checked'" ) #> />
114
				<span>{{ option }}</span>
115
			</label>
116
		<# }); #>
117
		<div class='clear-form'></div>
118
	</div>
119
</script>
120
121
<script type="text/html" id="tmpl-grunion-field-checkbox">
122
	<div>
123
		<label class='grunion-field-label checkbox'>
124
			<input type='checkbox' name='{{ data.id }}' value='<?php esc_attr__( 'Yes', 'jetpack' ); ?>' class="{{ data.class }}" <# if ( data.value ) print( 'checked="checked"' ) #> />
125
				<span>{{ data.label }}</span><# if ( data.required ) print( " <span>" + data.required + "</span>" ) #>
126
		</label>
127
		<div class='clear-form'></div>
128
	</div>
129
</script>
130
131
<script type="text/html" id="tmpl-grunion-field-checkbox-multiple">
132
	<div>
133
		<label class='grunion-field-label'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
134
		<# _.each( data.options, function( option ) { #>
135
			<label class='grunion-checkbox-multiple-label checkbox-multiple'>
136
				<input type='checkbox' name='{{ data.id }}[]' value='{{ option }}' class="{{ data.class }}" <# if ( option === data.value || _.contains( data.value, option ) ) print( "checked='checked'" ) #> />
137
				<span>{{ option }}</span>
138
			</label>
139
		<# }); #>
140
		<div class='clear-form'></div>
141
	</div>
142
</script>
143
144
<script type="text/html" id="tmpl-grunion-field-select">
145
	<div>
146
		<label for='{{ data.id }}' class='grunion-field-label select'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
147
		<select name='{{ data.id }}' id='{{ data.id }}' class="{{ data.class }}">
148
			<# _.each( data.options, function( option ) { #>
149
				<option <# if ( option === data.value ) print( "selected='selected'" ) #>>{{ option }}</option>
150
			<# }); #>
151
		</select>
152
	</div>
153
</script>
154
155
<script type="text/html" id="tmpl-grunion-field-date">
156
	<div>
157
		<label for='{{ data.id }}' class='grunion-field-label {{ data.type }}'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
158
		<input type='date' name='{{ data.id }}' id='{{ data.id }}' value='{{ data.value }}' class="{{ data.class }}" />
159
	</div>
160
</script>
161
162
<script type="text/html" id="tmpl-grunion-field-text">
163
	<div>
164
		<label for='{{ data.id }}' class='grunion-field-label {{ data.type }}'>{{ data.label }}<# if ( data.required ) print( " <span>" + data.required + "</span>" ) #></label>
165
		<input type='text' name='{{ data.id }}' id='{{ data.id }}' value='{{ data.value }}' class='{{ data.class }}' placeholder='{{ data.placeholder }}' />
166
	</div>
167
</script>
168
169
170
<script type="text/html" id="tmpl-grunion-field-edit">
171
	<div class="card is-compact grunion-field-edit grunion-field-{{ data.type }}" aria-label="<?php esc_attr_e( 'Form Field', 'jetpack' ); ?>">
172
		<label class="grunion-name">
173
			<span><?php esc_html_e( 'Field Label', 'jetpack' ); ?></span>
174
			<input type="text" name="label" placeholder="<?php esc_attr_e( 'Label', 'jetpack' ); ?>" value="{{ data.label }}"/>
175
		</label>
176
177
		<?php
178
		$grunion_field_types = array(
179
			'text'              => __( 'Text', 'jetpack' ),
180
			'name'              => __( 'Name', 'jetpack' ),
181
			'email'             => __( 'Email', 'jetpack' ),
182
			'url'               => __( 'Website', 'jetpack' ),
183
			'textarea'          => __( 'Textarea', 'jetpack' ),
184
			'checkbox'          => __( 'Checkbox', 'jetpack' ),
185
			'checkbox-multiple' => __( 'Checkbox with Multiple Items', 'jetpack' ),
186
			'select'            => __( 'Drop down', 'jetpack' ),
187
			'radio'             => __( 'Radio', 'jetpack' ),
188
		);
189
		?>
190
		<div class="grunion-type-options">
191
			<label class="grunion-type">
192
				<?php esc_html_e( 'Field Type', 'jetpack' ); ?>
193
				<select name="type">
194
					<?php foreach ( $grunion_field_types as $type => $label ) : ?>
195
					<option <# if ( '<?php echo esc_js( $type ); ?>' === data.type ) print( "selected='selected'" ) #> value="<?php echo esc_attr( $type ); ?>">
196
						<?php echo esc_html( $label ); ?>
197
					</option>
198
					<?php endforeach; ?>
199
				</select>
200
			</label>
201
202
			<label class="grunion-required">
203
				<input type="checkbox" name="required" value="1" <# if ( data.required ) print( 'checked="checked"' ) #> />
204
				<span><?php esc_html_e( 'Required?', 'jetpack' ); ?></span>
205
			</label>
206
		</div>
207
208
		<label class="grunion-options">
209
			<?php esc_html_e( 'Options', 'jetpack' ); ?>
210
			<ol>
211
				<# if ( data.options ) { #>
212
					<# _.each( data.options, function( option ) { #>
213
						<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>
214
					<# }); #>
215
				<# } else { #>
216
					<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>
217
					<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>
218
					<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>
219
				<# } #>
220
				<li><a class="add-option" href="javascript:;"><?php esc_html_e( 'Add new option...', 'jetpack' ); ?></a></li>
221
			</ol>
222
		</label>
223
224
		<a href="javascript:;" class="delete-field"><span class="screen-reader-text"><?php esc_html_e( 'Delete Field', 'jetpack' ); ?></span></a>
225
	</div>
226
</script>
227
228
<script type="text/html" id="tmpl-grunion-field-edit-option">
229
	<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>
230
</script>
231
232
<script type="text/html" id="tmpl-grunion-editor-inline">
233
			<h1 id="form-settings-header" class="grunion-section-header"><?php esc_html_e( 'Form Settings', 'jetpack' ); ?></h1>
234
			<section class="card grunion-form-settings" aria-labelledby="form-settings-header">
235
				<label><?php esc_html_e( 'What would you like the subject of the email to be?', 'jetpack' ); ?>
236
					<input type="text" name="subject" value="{{ data.subject }}" />
237
				</label>
238
				<label><?php esc_html_e( 'Which email address should we send the submissions to?', 'jetpack' ); ?>
239
					<input type="text" name="to" value="{{ data.to }}" />
240
				</label>
241
			</section>
242
			<h1 id="form-fields-header" class="grunion-section-header"><?php esc_html_e( 'Form Fields', 'jetpack' ); ?></h1>
243
			<section class="grunion-fields" aria-labelledby="form-fields-header">
244
				{{{ data.fields }}}
245
			</section>
246
			<section class="buttons">
247
				<?php submit_button( esc_html__( 'Add Field', 'jetpack' ), 'secondary', 'add-field', false ); ?>
248
				<?php submit_button( esc_html__( 'Cancel', 'jetpack' ), 'delete', 'cancel', false ); ?>
249
				<?php submit_button( esc_html__( 'Update Form', 'jetpack' ), 'primary', 'submit', false ); ?>
250
			</section>
251
</script>
252
253
</div>
254
	<?php
255
	}
256
}
257
258
259
Grunion_Editor_View::add_hooks();
260