@@ -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