Skip to content

Commit 9aa31d2

Browse files
committed
Change HTML template import to support more than a file
This change allows the template to be imported from a raw HTML string, meaning a developer can render form templates with Twig or similar instead of relying on simple .html files.
1 parent b381899 commit 9aa31d2

3 files changed

Lines changed: 52 additions & 58 deletions

File tree

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ The attribute will render as is on the `<form>` element
209209
<form class="libre-form libre-form-1" data-custom-attr="contactme">
210210
```
211211

212-
## Importing forms from a static HTML template
212+
## Importing forms from a predefined HTML template
213213

214214
Sometimes a project might require static forms which are not supposed to
215215
be editable in the admin panel.
@@ -219,12 +219,8 @@ and import them into the form admin for specific forms.
219219

220220
### Creating a static HTML template
221221

222-
Importable forms are defined using standard HTML5 templates, meaning
223-
files ending in `.html`. The template can contain anything the regular
224-
WPLF form edit view can.
225-
226-
You can create your templates in a theme, a plugin, or anywhere where
227-
the plugin can read files.
222+
The simplest way is to create a HTML5 file and read its contents. Other
223+
options include using Twig to render HTML templates.
228224

229225
### Importing a template into WPLF
230226

@@ -239,19 +235,21 @@ add_filter( 'wplf_import_html_template', function ($template, $form_id) {
239235
$some_form_id = 123;
240236

241237
if ($form_id === $some_form_id) {
242-
return '/path/to/template/file.html';
238+
// You can also render Twig templates and similar here
239+
return file_get_contents('/path/to/template/file.html');
243240
}
244241

245242
return $template;
246243
} );
247244
```
248245

249-
Now the plugin knows there is a template file which can be used for a
250-
form.
246+
The `$template` variable should be a raw HTML string. If it is set to
247+
`null` no template will be imported.
251248

252249
After a template is imported for a certain form the form's editview will
253250
be set to read only mode, meaning you must make changes to the static
254-
HTML file instead of editing the form inside the admin panel.
251+
HTML template in code instead of editing the form inside the admin
252+
panel.
255253

256254
Otherwise the form should function normally, meaning you can use WPLF
257255
features as always.

assets/styles/wplf-admin-form.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* wp-libre-form admin styles
3+
*/
4+
5+
.wplf-template-override-notice {
6+
line-height: 20px;
7+
padding: 1ex 0;
8+
margin-top: 1em;
9+
color: #666;
10+
font-style: italic;
11+
}

classes/class-cpt-wplf-form.php

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function disable_tinymce( $default ) {
132132
}
133133

134134
/**
135-
* Include custom JS on the edit screen
135+
* Include custom JS and CSS on the edit screen
136136
*/
137137
function admin_post_scripts_cpt( $hook ) {
138138
global $post;
@@ -147,8 +147,13 @@ function admin_post_scripts_cpt( $hook ) {
147147
return;
148148
}
149149

150+
$assets_url = plugins_url( 'assets', dirname( __FILE__ ) );
151+
150152
// enqueue the custom JS for this view
151-
wp_enqueue_script( 'wplf-form-edit-js', plugins_url( 'assets/scripts/wplf-admin-form.js', dirname( __FILE__ ) ) );
153+
wp_enqueue_script( 'wplf-form-edit-js', $assets_url . '/scripts/wplf-admin-form.js' );
154+
155+
// enqueue the custom CSS for this view
156+
wp_enqueue_style( 'wplf-form-edit-css', $assets_url . '/styles/wplf-admin-form.css' );
152157
}
153158

154159

@@ -483,38 +488,27 @@ function maybe_load_imported_template( $post_type, $post ) {
483488
*
484489
* If the template returned is `null` then no template is loaded.
485490
*
486-
* @param string|null $template_path Absolute path to a HTML file to import.
491+
* @param string|null $template_content Raw HTML to import for a form.
487492
* @param int $form_id Form ID (WP_Post ID) to import template for.
488493
*/
489-
$template_path = apply_filters( 'wplf_import_html_template', null, $form_id );
494+
$template_content = apply_filters( 'wplf_import_html_template', null, $form_id );
490495

491-
if ( $template_path === null ) {
496+
if ( $template_content === null ) {
492497
return;
493498
}
494499

495-
$this->override_form_template( $template_path, $form_id );
500+
$this->override_form_template( $template_content, $form_id );
496501
}
497502

498503
/**
499504
* Override a form's template with an imported template file.
500505
*
501-
* @param string $template_path Absolute path to template HTML file.
506+
* @param string $template_content Raw HTML content to use for the form content.
502507
* @param int $form_id ID of form we're overriding the template for.
503508
*
504509
* @return void
505510
*/
506-
protected function override_form_template( $template_path, $form_id ) {
507-
$contents = file_get_contents( $template_path );
508-
509-
/**
510-
* Allows parsing the imported HTML form contents before we use it as the actual
511-
* form content.
512-
*
513-
* @param string $contents The form HTML.
514-
* @param int $form_id ID of form we are importing a template for.
515-
*/
516-
$template_contents = apply_filters( 'wplf_imported_html_template_contents', $contents, $form_id );
517-
511+
protected function override_form_template( $template_content ) {
518512
// Make the editor textarea uneditable. Also remove TinyMCE.
519513
add_filter( 'the_editor', function ( $editor ) {
520514
if ( ! preg_match( '%id="wp-content-editor-container"%', $editor ) ) {
@@ -523,42 +517,33 @@ protected function override_form_template( $template_path, $form_id ) {
523517

524518
$editor = preg_replace( '%\<textarea %', '<textarea readonly="readonly" ', $editor );
525519

526-
$editor = preg_replace(
527-
'%<div [^<>]*? class="quicktags-toolbar">.*?</div><textarea%imux',
528-
'<textarea',
529-
$editor
520+
$notice = _x(
521+
'This form template is being overridden by code, you must edit it in your project code',
522+
'Template override notice in form edit admin view',
523+
'wp-libre-form'
530524
);
531525

532-
return $editor;
533-
} );
526+
$notice = sprintf( '<div class="wplf-template-override-notice">%s</div>', $notice );
534527

535-
// Replace all editor content with template content.
536-
add_filter( 'the_editor_content', function ( $content ) use ( $template_contents ) {
537-
return $template_contents;
528+
return $notice . $editor;
538529
} );
539530

540-
// Add a notice about the override.
541-
add_action( 'edit_form_after_title', function () use ( $template_path ) {
542-
$printable_path = explode( DIRECTORY_SEPARATOR, $template_path );
531+
// Custom settings for the form editor.
532+
add_filter( 'wp_editor_settings', function ( $settings, $editor_id ) {
533+
if ( $editor_id !== 'content' ) {
534+
return $settings;
535+
}
543536

544-
// We may not want the full system path to be shown, 4 parent directories oughta suffice.
545-
if ( count( $printable_path ) < 4 ) {
546-
$printable_path = implode( DIRECTORY_SEPARATOR, $printable_path );
547-
} else {
548-
$printable_path = array_reverse( array_slice( array_reverse( $printable_path ), 0, 4 ) );
549-
$printable_path = '..' . DIRECTORY_SEPARATOR . implode( DIRECTORY_SEPARATOR, $printable_path );
550-
}
537+
$settings['tinymce'] = false;
538+
$settings['quicktags'] = false;
539+
$settings['media_buttons'] = false;
551540

552-
$notice = sprintf(
553-
_x(
554-
'This form template is being overridden by code, the template being used is<br> <code>%s</code>',
555-
'Template override notice in form edit admin view',
556-
'wp-libre-form'
557-
),
558-
$printable_path
559-
);
541+
return $settings;
542+
}, 10, 2 );
560543

561-
printf( '<p>%s</p>', $notice );
544+
// Replace all editor content with template content.
545+
add_filter( 'the_editor_content', function ( $content ) use ( $template_content ) {
546+
return $template_content;
562547
} );
563548
}
564549

0 commit comments

Comments
 (0)