Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable storing form data in TablePress #48

Merged
merged 24 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"wp-phpunit/wp-phpunit": "^6.7",
"yoast/phpunit-polyfills": "^3.0",
"wpackagist-plugin/query-monitor": "^3.17",
"wpackagist-plugin/wp-mail-debugger": "^1.1"
"wpackagist-plugin/wp-mail-debugger": "^1.1",
"wpackagist-plugin/tablepress": "^3.0"
},
"scripts": {
"lint": [
Expand Down
20 changes: 19 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
* Text Domain: contact-form-7-extras
*/

require_once __DIR__ . '/src/class-cf7-extras-form-settings.php';
require_once __DIR__ . '/src/class-cf7-extras-integration.php'; // Before all integrations.
require_once __DIR__ . '/src/class-cf7-extras-integration-tablepress.php';
require_once __DIR__ . '/src/class-cf7-extras.php';

$plugin = Cf7_Extras::instance();
$plugin->set_plugin_dir( __DIR__ );
$plugin->init();

add_action( 'plugins_loaded', array( $plugin, 'init' ) );
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Load later to allow disabling, if needed.

13 changes: 10 additions & 3 deletions readme.txt.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Tested up to: 6.7
Stable tag: STABLETAG
License: GPLv2 or later

Simple controls, analytics, tracking and redirects for Contact Form 7.
Analytics, tracking, redirects and storage for Contact Form 7.


## Description
Expand All @@ -23,6 +23,7 @@ This is an addon for the [Contact Form 7](https://wordpress.org/plugins/contact-
- 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 All @@ -34,9 +35,15 @@ Please note that some settings work on the per-page level and will apply to all

The plugin adds a new "Controls" tab for each Contact Form 7 form in the WordPress administration area.

### Form Submission Storage

*Setup the official companion plugin [Storage for Contact Form 7 plugin](https://preseto.com/go/cf7-storage?utm_source=wporg) for capturing the form submissions safely in the WordPres database.*

Alternatively, there is also a basic integration with the [TablePress plugin](https://wordpress.org/plugins/tablepress/). Select the TablePress table where to store the form submissions. The plugin will add any missing columns for each form field, and append the form entries as rows to the table. Additionally, fields `cf7_time` (submission time as [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601)) and `cf7_url` (URL where the form was submitted) are stored along with the form data.

### Analytics Tracking

The plugin _automatically_ triggers analytics events for the following services:
The plugin *automatically* triggers analytics events for the following services:

- [Google Analytics (GA4)](https://support.google.com/analytics/topic/14088998) using [Google Tag Manager](https://support.google.com/tagmanager/answer/9442095) and [Global Tag (gtag.js)](https://developers.google.com/tag-platform/gtagjs) with `ga()`, `_gaq.push()` and `dataLayer.push()` implementations,
- [Matomo](https://matomo.org/) (formerly Piwik),
Expand Down Expand Up @@ -80,7 +87,7 @@ Add it as [a Composer dependency](https://packagist.org/packages/kasparsd/contac

### How to save Contact Form 7 submissions in the WordPress database?

The "[Storage for Contact Form 7](https://codecanyon.net/item/storage-for-contact-form-7-/7806229)" plugin stores all contact form submissions (including attachments) securely in the WordPress database. It also provides a CSV export of the form entries.
The "[Storage for Contact Form 7](https://preseto.com/go/cf7-storage?utm_source=wporg)" plugin stores all contact form submissions (including attachments) securely in the WordPress database. It also provides a CSV export of the form entries.


## Screenshots
Expand Down
100 changes: 100 additions & 0 deletions src/class-cf7-extras-form-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/**
* Form settings.
*/
class Cf7_Extras_Form_Settings {

/**
* Default settings.
*
* @var array
*/
private $defaults = array(
'disable-css' => false,
'disable-ajax' => false,
'html5-disable' => false,
'html5-fallback' => false,
'disable-autop' => false,
'redirect-success' => false,
'track-ga-success' => false,
'track-ga-submit' => false,
'track-ga' => false,
'google-recaptcha-lang' => null,
);

/**
* Form instance.
*
* @var WPCF7_ContactForm
*/
protected $form;

/**
* Setup settings for a form.
*
* @param WPCF7_ContactForm $form Form instance.
*/
public function __construct( $form ) {
$this->form = $form;
}

/**
* Get settings for a form by ID.
*
* @param int $form_id Form ID.
*
* @return self
*/
public static function from_form_id( $form_id ) {
return new self( wpcf7_contact_form( $form_id ) );
}

/**
* Get the form ID.
*
* @return int
*/
public function form_id() {
return $this->form->id();
}

/**
* Get a setting value.
*
* @param string $key Setting key.
*
* @return mixed|null Return null if the setting is not found.
*/
public function get( $key ) {
$settings = $this->all();

if ( isset( $settings[ $key ] ) ) {
return $settings[ $key ];
}

return null;
}

/**
* Get all settings.
*
* @return array
*/
public function all() {
$settings = get_post_meta( $this->form->id(), 'extras', true );

if ( ! is_array( $settings ) ) {
$settings = array();
}

$settings = array_merge( $this->defaults, $settings );

// Convert legacy settings into one.
if ( ! empty( $settings['track-ga-success'] ) || ! empty( $settings['track-ga-submit'] ) ) {
$settings['track-ga'] = true;
}

return $settings;
}
}
Loading
Loading