Skip to content

Commit b381899

Browse files
committed
Code style and documentation for HTML import
1 parent 703f839 commit b381899

2 files changed

Lines changed: 34 additions & 25 deletions

File tree

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,21 @@ Sometimes a project might require static forms which are not supposed to
215215
be editable in the admin panel.
216216

217217
This plugin allows you to define HTML forms in your project source code
218-
and import them into the form admin.
218+
and import them into the form admin for specific forms.
219219

220220
### Creating a static HTML template
221221

222222
Importable forms are defined using standard HTML5 templates, meaning
223-
files ending in `.html`.
223+
files ending in `.html`. The template can contain anything the regular
224+
WPLF form edit view can.
224225

225226
You can create your templates in a theme, a plugin, or anywhere where
226227
the plugin can read files.
227228

228-
### Importing a template into wp-libre-form
229+
### Importing a template into WPLF
229230

230231
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+
WPLF about it. You can use the `wplf_import_html_template`
232233
filter hook for this:
233234

234235
```php
@@ -237,7 +238,7 @@ filter hook for this:
237238
add_filter( 'wplf_import_html_template', function ($template, $form_id) {
238239
$some_form_id = 123;
239240

240-
if ($form_id = $some_form_id) {
241+
if ($form_id === $some_form_id) {
241242
return '/path/to/template/file.html';
242243
}
243244

classes/class-cpt-wplf-form.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -488,15 +488,15 @@ function maybe_load_imported_template( $post_type, $post ) {
488488
*/
489489
$template_path = apply_filters( 'wplf_import_html_template', null, $form_id );
490490

491-
if ($template_path === null) {
491+
if ( $template_path === null ) {
492492
return;
493493
}
494494

495495
$this->override_form_template( $template_path, $form_id );
496496
}
497497

498498
/**
499-
* Override a forms template with an imported template file.
499+
* Override a form's template with an imported template file.
500500
*
501501
* @param string $template_path Absolute path to template HTML file.
502502
* @param int $form_id ID of form we're overriding the template for.
@@ -513,44 +513,52 @@ protected function override_form_template( $template_path, $form_id ) {
513513
* @param string $contents The form HTML.
514514
* @param int $form_id ID of form we are importing a template for.
515515
*/
516-
$contents = apply_filters( 'wplf_imported_html_template_contents', $contents, $form_id );
516+
$template_contents = apply_filters( 'wplf_imported_html_template_contents', $contents, $form_id );
517517

518518
// Make the editor textarea uneditable. Also remove TinyMCE.
519519
add_filter( 'the_editor', function ( $editor ) {
520-
if (!preg_match('%id="wp-content-editor-container"%', $editor)) {
520+
if ( ! preg_match( '%id="wp-content-editor-container"%', $editor ) ) {
521521
return $editor;
522522
}
523523

524524
$editor = preg_replace( '%\<textarea %', '<textarea readonly="readonly" ', $editor );
525525

526-
$editor = preg_replace( '%<div [^<>]*? class="quicktags-toolbar">.*?</div><textarea%imux', '<textarea', $editor );
526+
$editor = preg_replace(
527+
'%<div [^<>]*? class="quicktags-toolbar">.*?</div><textarea%imux',
528+
'<textarea',
529+
$editor
530+
);
527531

528532
return $editor;
529533
} );
530534

531535
// Replace all editor content with template content.
532-
add_filter( 'the_editor_content', function ( $content ) use ( $contents ) {
533-
return $contents;
536+
add_filter( 'the_editor_content', function ( $content ) use ( $template_contents ) {
537+
return $template_contents;
534538
} );
535539

536540
// Add a notice about the override.
537541
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));
542+
$printable_path = explode( DIRECTORY_SEPARATOR, $template_path );
544543

545-
$printable_path = '..' . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $printable_path);
546-
}
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+
}
547551

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+
$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+
);
552560

553-
printf('<p>%s</p>', $notice);
561+
printf( '<p>%s</p>', $notice );
554562
} );
555563
}
556564

0 commit comments

Comments
 (0)