Skip to content

Commit 703f839

Browse files
committed
Add initial version of importable form HTML
Allows using a filter hook to import a static HTML template for a specific form.
1 parent c4cfa05 commit 703f839

3 files changed

Lines changed: 142 additions & 0 deletions

File tree

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Use standard HTML5 markup to create fully functional forms for WordPress
1313
- Email notifications of received form submissions
1414
- Full file upload support to Media Library with input type=file
1515
- Multilingual support with Polylang
16+
- Predefined static HTML forms via filter hooks
1617

1718
## Why?
1819

@@ -207,3 +208,49 @@ The attribute will render as is on the `<form>` element
207208
```html
208209
<form class="libre-form libre-form-1" data-custom-attr="contactme">
209210
```
211+
212+
## Importing forms from a static HTML template
213+
214+
Sometimes a project might require static forms which are not supposed to
215+
be editable in the admin panel.
216+
217+
This plugin allows you to define HTML forms in your project source code
218+
and import them into the form admin.
219+
220+
### Creating a static HTML template
221+
222+
Importable forms are defined using standard HTML5 templates, meaning
223+
files ending in `.html`.
224+
225+
You can create your templates in a theme, a plugin, or anywhere where
226+
the plugin can read files.
227+
228+
### Importing a template into wp-libre-form
229+
230+
Once you're done creating a form template, you need to inform
231+
wp-libre-form about it. You can use the `wplf_register_html_template`
232+
filter hook for this:
233+
234+
```php
235+
<?php
236+
237+
add_filter( 'wplf_import_html_template', function ($template, $form_id) {
238+
$some_form_id = 123;
239+
240+
if ($form_id = $some_form_id) {
241+
return '/path/to/template/file.html';
242+
}
243+
244+
return $template;
245+
} );
246+
```
247+
248+
Now the plugin knows there is a template file which can be used for a
249+
form.
250+
251+
After a template is imported for a certain form the form's editview will
252+
be set to read only mode, meaning you must make changes to the static
253+
HTML file instead of editing the form inside the admin panel.
254+
255+
Otherwise the form should function normally, meaning you can use WPLF
256+
features as always.

classes/class-cpt-wplf-form.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function __construct() {
2727
add_action( 'save_post', array( $this, 'save_cpt' ) );
2828
add_filter( 'content_save_pre', array( $this, 'strip_form_tags' ), 10, 1 );
2929
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes_cpt' ) );
30+
add_action( 'add_meta_boxes', array( $this, 'maybe_load_imported_template' ), 10, 2 );
3031
add_action( 'admin_enqueue_scripts', array( $this, 'admin_post_scripts_cpt' ), 10, 1 );
3132

3233
// edit.php view
@@ -460,6 +461,99 @@ class="code"
460461
<?php
461462
}
462463

464+
/**
465+
* Check and maybe load a static HTML template for a specific form.
466+
*
467+
* Hooked to `add_meta_boxes`.
468+
*
469+
* @param string $post_type Post type for which editor is being rendered for.
470+
* @param \WP_Post $post Current post object.
471+
*
472+
* @return void
473+
*/
474+
function maybe_load_imported_template( $post_type, $post ) {
475+
if ( $post_type !== 'wplf-form' ) {
476+
return;
477+
}
478+
479+
$form_id = (int) $post->ID;
480+
481+
/**
482+
* Allows importing a static HTML template for a specific form ID.
483+
*
484+
* If the template returned is `null` then no template is loaded.
485+
*
486+
* @param string|null $template_path Absolute path to a HTML file to import.
487+
* @param int $form_id Form ID (WP_Post ID) to import template for.
488+
*/
489+
$template_path = apply_filters( 'wplf_import_html_template', null, $form_id );
490+
491+
if ($template_path === null) {
492+
return;
493+
}
494+
495+
$this->override_form_template( $template_path, $form_id );
496+
}
497+
498+
/**
499+
* Override a forms template with an imported template file.
500+
*
501+
* @param string $template_path Absolute path to template HTML file.
502+
* @param int $form_id ID of form we're overriding the template for.
503+
*
504+
* @return void
505+
*/
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+
$contents = apply_filters( 'wplf_imported_html_template_contents', $contents, $form_id );
517+
518+
// Make the editor textarea uneditable. Also remove TinyMCE.
519+
add_filter( 'the_editor', function ( $editor ) {
520+
if (!preg_match('%id="wp-content-editor-container"%', $editor)) {
521+
return $editor;
522+
}
523+
524+
$editor = preg_replace( '%\<textarea %', '<textarea readonly="readonly" ', $editor );
525+
526+
$editor = preg_replace( '%<div [^<>]*? class="quicktags-toolbar">.*?</div><textarea%imux', '<textarea', $editor );
527+
528+
return $editor;
529+
} );
530+
531+
// Replace all editor content with template content.
532+
add_filter( 'the_editor_content', function ( $content ) use ( $contents ) {
533+
return $contents;
534+
} );
535+
536+
// Add a notice about the override.
537+
add_action( 'edit_form_after_title', function () use ( $template_path ) {
538+
$printable_path = explode(DIRECTORY_SEPARATOR, $template_path);
539+
540+
if (count($printable_path) < 4) {
541+
$printable_path = implode(DIRECTORY_SEPARATOR, $template_path);
542+
} else {
543+
$printable_path = array_reverse(array_slice(array_reverse($printable_path), 0, 4));
544+
545+
$printable_path = '..' . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $printable_path);
546+
}
547+
548+
$notice = sprintf(
549+
__( 'This form template is being overridden by code, the template being used is<br> <code>%s</code>', 'wplf' ),
550+
$printable_path
551+
);
552+
553+
printf('<p>%s</p>', $notice);
554+
} );
555+
}
556+
463557
/**
464558
* Handles saving our post meta
465559
*/

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Use standard HTML5 markup to create fully functional forms for WordPress
2222
- Email notifications of received form submissions
2323
- Full file upload support to Media Library with input type=file
2424
- Multilingual support with Polylang
25+
- Predefined static HTML markup for forms via filter hooks
2526

2627
**Why?**
2728

0 commit comments

Comments
 (0)