Completed
Push — add/contact-form/re-add-old-bu... ( a92bd3 )
by George
103:18 queued 93:12
created

Grunion_Editor_View   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 262
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 0

7 Methods

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