Skip to content

Commit

Permalink
Add native WordPress shortcode support and improve the reliability of…
Browse files Browse the repository at this point in the history
… autop disable (#51)
  • Loading branch information
kasparsd authored Jan 20, 2025
1 parent 9977c90 commit 78c350f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 21 deletions.
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Plugin URI: https://formcontrols.com
* Author: Kaspars Dambis
* Author URI: https://formcontrols.com
* Version: 0.9.0
* Version: 0.10.0
* License: GPL2
* Text Domain: contact-form-7-extras
*/
Expand Down
21 changes: 7 additions & 14 deletions readme.txt.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ Analytics, tracking, redirects and storage for Contact Form 7.
This is an addon for the [Contact Form 7](https://wordpress.org/plugins/contact-form-7/) plugin with the following features:

- [Track form submissions, errors and completions](https://formcontrols.com/docs) with Google Analytics (GA4), Google Tag (gtag.js), Google Tag Manager (GTM), Matomo (formerly Piwik) and Facebook Pixel.
- Redirect to URL on form submission
- Disable AJAX form submissions
- Disable default form CSS
- Disable automatic paragraph formatting
- Disable HTML5 input field types or enable the HTML5 input type fallback
- Specify the Google reCAPTCHA language
- Redirect to URL on form submission.
- Enable native WordPress shortcodes in form content.
- Disable AJAX form submissions.
- Disable default form CSS.
- Disable automatic paragraph formatting.
- Disable HTML5 input field types or enable the HTML5 input type fallback.
- Specify the Google reCAPTCHA language.
- Store form submissions in [Storage for Contact Form 7](https://preseto.com/go/cf7-storage?utm_source=wporg) or [TablePress](https://wordpress.org/plugins/tablepress/).

Please note that some settings work on the per-page level and will apply to all forms on the same page. For example, disabling AJAX form submissions for one form will disable AJAX submissions on all forms on the same page.
Expand Down Expand Up @@ -103,11 +104,3 @@ The "[Storage for Contact Form 7](https://preseto.com/go/cf7-storage?utm_source=
### 0.9.0

New feature: enable storing form submissions in TablePress plugin tables.

### 0.8.0

Use the suggested Google Global Site Tag (gtag.js) event structure. This will make the "Contact Form" events appear in both Google Analytics and Google Tag Manager.

### 0.7.3

Compatibility with the Javascript event changes in the latest version 5.2 of the Contact Form 7 plugin.
1 change: 1 addition & 0 deletions src/class-cf7-extras-form-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Cf7_Extras_Form_Settings {
'html5-disable' => false,
'html5-fallback' => false,
'disable-autop' => false,
'enable-shortcodes' => false,
'redirect-success' => false,
'track-ga-success' => false,
'track-ga-submit' => false,
Expand Down
76 changes: 70 additions & 6 deletions src/class-cf7-extras.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,16 @@ public function init() {
add_action( 'wpcf7_submit', array( $this, 'wpcf7_submit' ), 987, 2 );

// TODO: Enable Google analytics tracking when AJAX is disabled.
add_filter( 'wpcf7_form_elements', array( $this, 'maybe_reset_autop' ) );

/**
* Use a filter instead of the WPCF7_AUTOP to disable formatting on specific forms only.
*
* Run very early since we're replacing the rendered form content with a fresh
* version that doesn't have the autop applied.
*/
add_filter( 'wpcf7_form_elements', array( $this, 'maybe_reset_autop' ), 1 );

add_filter( 'wpcf7_form_elements', array( $this, 'maybe_enable_shortcodes' ) );

$integrations = array(
new Cf7_Extras_Integration_TablePress(),
Expand Down Expand Up @@ -172,6 +181,29 @@ public function wpcf7_add_meta_boxes( $post_id ) {
);
}

/**
* Sanitize the field label with a few allowed HTML tags.
*
* @param string $label Field label markup.
*
* @return string
*/
private function esc_field_label( $label ) {
return wp_kses(
$label,
array(
'a' => array(
'href' => array(),
'target' => array(),
),
'strong' => array(),
'em' => array(),
'span' => array(),
'code' => array(),
)
);
}

/**
* Display our custom form settings.
*
Expand All @@ -195,7 +227,7 @@ public function wpcf7_metabox( $cf7 ) {
<p class="desc">%s</p>',
checked( $settings['disable-ajax'], true, false ),
esc_html__( 'Disable AJAX for this form', 'contact-form-7-extras' ),
__( 'Same as <code>define( \'WPCF7_LOAD_JS\', false );</code>. Disabling AJAX will also disable Google Analytics event tracking and HTML5 input type fallback for this form.', 'contact-form-7-extras' )
$this->esc_field_label( __( 'Same as <code>define( \'WPCF7_LOAD_JS\', false );</code>. Disabling AJAX will also disable Google Analytics event tracking and HTML5 input type fallback for this form.', 'contact-form-7-extras' ) )
),
),
'extra-disable-css' => array(
Expand All @@ -209,7 +241,7 @@ public function wpcf7_metabox( $cf7 ) {
<p class="desc">%s</p>',
checked( $settings['disable-css'], true, false ),
esc_html__( 'Disable default CSS for this form', 'contact-form-7-extras' ),
__( 'Disables CSS that comes bundled with Contact Form 7. Same as <code>define( \'WPCF7_LOAD_CSS\', false );</code>.', 'contact-form-7-extras' )
$this->esc_field_label( __( 'Disables CSS that comes bundled with Contact Form 7. Same as <code>define( \'WPCF7_LOAD_CSS\', false );</code>.', 'contact-form-7-extras' ) )
),
),
'extra-disable-autop' => array(
Expand All @@ -223,7 +255,21 @@ public function wpcf7_metabox( $cf7 ) {
<p class="desc">%s</p>',
checked( $settings['disable-autop'], true, false ),
esc_html__( 'Disable automatic paragraph formatting in form output', 'contact-form-7-extras' ),
__( 'Same as <code>define( \'WPCF7_AUTOP\', false );</code>.', 'contact-form-7-extras' )
$this->esc_field_label( __( 'Same as <code>define( \'WPCF7_AUTOP\', false );</code>.', 'contact-form-7-extras' ) )
),
),
'extra-enable-shortcodes' => array(
'label' => __( 'Enable Shortcodes', 'contact-form-7-extras' ),
'docs_url' => 'https://formcontrols.com/docs/enable-wordpress-shortcodes',
'field' => sprintf(
'<label>
<input id="extra-enable-shortcodes" name="extra[enable-shortcodes]" value="1" %s type="checkbox" />
<span>%s</span>
</label>
<p class="desc">%s</p>',
checked( $settings['enable-shortcodes'], true, false ),
esc_html__( 'Enable WordPress shortcodes', 'contact-form-7-extras' ),
esc_html__( 'Adds support for standard WordPress shortcodes in the form content.', 'contact-form-7-extras' )
),
),
'extra-html5' => array(
Expand Down Expand Up @@ -668,9 +714,9 @@ public function wpcf7_submit( $form, $result ) {
* Maybe disable WP core autop() on form email contents
* by re-parsing the form content without the autop.
*
* @param WPCF7_ContactForm $form Current CF7 form.
* @param string $form Current CF7 form.
*
* @return WPCF7_ContactForm
* @return string
*/
public function maybe_reset_autop( $form ) {
$form_instance = WPCF7_ContactForm::get_current();
Expand All @@ -697,6 +743,24 @@ public function maybe_reset_autop( $form ) {
return $form;
}

/**
* Maybe enable WordPress shortcodes in the form content.
*
* @param string $form Current CF7 form content.
*
* @return string
*/
public function maybe_enable_shortcodes( $form ) {
$form_instance = WPCF7_ContactForm::get_current();
$enable_shortcodes = $this->get_form_settings( $form_instance, 'enable-shortcodes' );

if ( $enable_shortcodes ) {
$form = do_shortcode( $form );
}

return $form;
}

/**
* Register an error for the current request.
*
Expand Down

0 comments on commit 78c350f

Please sign in to comment.