Geen omschrijving

grunion-editor-view.php 13KB

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