grunion-editor-view.php 13 KB

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