@@ -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 */
0 commit comments