Skip to content

Commit 8719323

Browse files
author
Antti Kuosmanen
committed
Fix preview autop, allow overriding form html content
- Also, better checking for file inputs in forms
1 parent 59d21f1 commit 8719323

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

classes/class-cpt-wplf-form.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,21 @@ private function __construct() {
4040
// front end
4141
add_shortcode( 'libre-form', array( $this, 'shortcode' ) );
4242
add_action( 'wp', array( $this, 'maybe_set_404_for_single_form' ) );
43-
add_filter( 'the_content', array( $this, 'use_shortcode_for_preview' ) );
43+
add_filter( 'the_content', array( $this, 'use_shortcode_for_preview' ), 0 );
4444
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_frontend_script' ) );
4545

4646
// default filters for the_content, but we don't want to use actual the_content
4747
add_filter( 'wplf_form', 'convert_smilies' );
4848
add_filter( 'wplf_form', 'convert_chars' );
4949
add_filter( 'wplf_form', 'shortcode_unautop' );
5050

51+
// we want to keep form content strictly html, so let's remove auto <p> tags
5152
remove_filter( 'wplf_form', 'wpautop' );
5253
remove_filter( 'wplf_form', 'wptexturize' );
5354

5455
// Removing wpautop isn't enough if form is used inside a ACF field or so.
5556
// Fitting the output to one line prevents <br> tags from appearing.
56-
add_filter( 'wplf_form', array( $this, 'minify_output' ) );
57+
add_filter( 'wplf_form', array( $this, 'minify_html' ) );
5758
}
5859

5960
public static function register_cpt() {
@@ -511,9 +512,9 @@ function wplf_form( $id, $content = '', $xclass = '', $attributes = [] ) {
511512
class="libre-form libre-form-<?php echo esc_attr( $id . ' ' . $xclass ); ?>"
512513
<?php
513514
// check if form contains file inputs
514-
if ( strpos( $content, "type='file'" ) >= 0
515-
|| strpos( $content, 'type="file"' ) >= 0
516-
|| strpos( $content, 'type=file' ) >= 0
515+
if ( false !== strpos( $content, "type='file'" )
516+
|| false !== strpos( $content, 'type="file"' )
517+
|| false !== strpos( $content, 'type=file' )
517518
) : ?>
518519
enctype="multipart/form-data"
519520
<?php endif; ?>
@@ -523,22 +524,21 @@ class="libre-form libre-form-<?php echo esc_attr( $id . ' ' . $xclass ); ?>"
523524
echo esc_attr( $attr_name ) . '="' . esc_attr( $attr_value ) . "\"\n";
524525
}
525526
?>
526-
527527
>
528-
<?php if ( is_singular( 'wplf-form' ) && current_user_can( 'edit_post', $id ) ) {
529-
$publicly_visible = $this->get_publicly_visible_state( $id );
530-
if ( ! $publicly_visible ) {
531-
?>
528+
<?php if ( is_singular( 'wplf-form' ) && current_user_can( 'edit_post', $id ) ) : ?>
529+
<?php
530+
$publicly_visible = $this->get_publicly_visible_state( $id );
531+
if ( ! $publicly_visible ) :
532+
?>
532533
<p style="background:#f5f5f5;border-left:4px solid #dc3232;padding:6px 12px;">
533534
<strong style="color:#dc3232;">
534535
<?php esc_html_e( 'This form preview URL is not public and cannot be shared.', 'wp-libre-form' ) ?>
535536
</strong>
536537
<br />
537538
<?php esc_html_e( 'Non-logged in visitors will see a 404 error page instead.', 'wp-libre-form' ) ?>
538539
</p>
539-
<?php
540-
}
541-
} ?>
540+
<?php endif; ?>
541+
<?php endif; ?>
542542
<?php
543543
// This is where we output the user-input form html. We allow all HTML here. Yes, even scripts.
544544
// @codingStandardsIgnoreStart
@@ -589,7 +589,7 @@ function maybe_enqueue_frontend_script() {
589589
/**
590590
* Shortcode for displaying a Form
591591
*/
592-
function shortcode( $shortcode_atts ) {
592+
function shortcode( $shortcode_atts, $content = null ) {
593593
$attributes = shortcode_atts( array(
594594
'id' => null,
595595
'xclass' => '',
@@ -604,7 +604,7 @@ function shortcode( $shortcode_atts ) {
604604
) );
605605

606606
// display form
607-
return $this->wplf_form( $id, null, $xclass, $attributes );
607+
return $this->wplf_form( $id, $content, $xclass, $attributes );
608608
}
609609

610610

@@ -613,10 +613,10 @@ function shortcode( $shortcode_atts ) {
613613
*/
614614
function use_shortcode_for_preview( $content ) {
615615
global $post;
616-
if ( isset( $post->post_type ) && $post->post_type === 'wplf-form' ) {
617-
return $this->wplf_form( $post->ID, $content );
616+
if ( ! isset( $post->post_type ) || $post->post_type !== 'wplf-form' ) {
617+
return $content;
618618
}
619-
return $content;
619+
return '[libre-form id="' . (int) $post->ID . '"]' . $this->minify_html( $content ) . '[/libre-form]';
620620
}
621621

622622
/**
@@ -648,8 +648,11 @@ function get_publicly_visible_state( $id ) {
648648
return apply_filters( 'wplf-form-publicly-visible', false, $id );
649649
}
650650

651-
function minify_output( $html ) {
652-
return str_replace( array( "\n", "\r" ), '', $html );
651+
/**
652+
* A very simple uglify. Just removes line breaks from html
653+
*/
654+
function minify_html( $html ) {
655+
return str_replace( array( "\n", "\r" ), ' ', $html );
653656
}
654657
}
655658

0 commit comments

Comments
 (0)