@@ -511,41 +511,52 @@ function maybe_load_imported_template( $post_type, $post ) {
511511 protected function override_form_template ( $ template_content , $ form_id ) {
512512 $ this ->maybe_persist_override_template ( $ template_content , $ form_id );
513513
514+ static $ times_content_replaced = 0 ;
515+
514516 // Make the editor textarea uneditable.
515517 add_filter ( 'the_editor ' , function ( $ editor ) {
516- if ( ! preg_match ( '%id="wp-content-editor-container"% ' , $ editor ) ) {
517- return $ editor ;
518- }
518+ if ( ! preg_match ( '%id="wp-content-editor-container"% ' , $ editor ) ) {
519+ return $ editor ;
520+ }
519521
520- $ editor = preg_replace ( '%\<textarea % ' , '<textarea readonly="readonly" ' , $ editor );
522+ $ editor = preg_replace ( '%\<textarea % ' , '<textarea readonly="readonly" ' , $ editor );
521523
522- $ notice = _x (
523- 'This form template is being overridden by code, you must edit it in your project code ' ,
524- 'Template override notice in form edit admin view ' ,
525- 'wp-libre-form '
526- );
524+ $ notice = _x (
525+ 'This form template is being overridden by code, you must edit it in your project code ' ,
526+ 'Template override notice in form edit admin view ' ,
527+ 'wp-libre-form '
528+ );
527529
528- $ notice = sprintf ( '<div class="wplf-template-override-notice">%s</div> ' , $ notice );
530+ $ notice = sprintf ( '<div class="wplf-template-override-notice">%s</div> ' , $ notice );
529531
530- return $ notice . $ editor ;
532+ return $ notice . $ editor ;
531533 } );
532534
533535 // Custom settings for the form editor.
534- add_filter ( 'wp_editor_settings ' , function ( $ settings , $ editor_id ) {
535- if ( $ editor_id !== 'content ' ) {
536- return $ settings ;
537- }
536+ add_filter ( 'wp_editor_settings ' , function ( $ settings , $ editor_id ) {
537+ if ( $ editor_id !== 'content ' ) {
538+ return $ settings ;
539+ }
538540
539- $ settings ['tinymce ' ] = false ;
540- $ settings ['quicktags ' ] = false ;
541- $ settings ['media_buttons ' ] = false ;
541+ $ settings ['tinymce ' ] = false ;
542+ $ settings ['quicktags ' ] = false ;
543+ $ settings ['media_buttons ' ] = false ;
542544
543- return $ settings ;
544- }, 10 , 2 );
545+ return $ settings ;
546+ }, 10 , 2 );
545547
546548 // Replace all editor content with template content.
547- add_filter ( 'the_editor_content ' , function ( $ content ) use ( $ template_content ) {
548- return $ template_content ;
549+ add_filter ( 'the_editor_content ' , function ( $ content ) use ( $ template_content , &$ times_content_replaced ) {
550+ // This is hacky, yes. We only want to override the content for the first
551+ // editor field we come by, meaning 99% of the time we hit the wanted form
552+ // template editor field at the top of the edit view page.
553+ if ( $ times_content_replaced > 0 ) {
554+ return $ content ;
555+ }
556+
557+ $ times_content_replaced ++;
558+
559+ return $ template_content ;
549560 } );
550561 }
551562
0 commit comments