Skip to content
Merged
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
61 changes: 54 additions & 7 deletions src/Integrations/FormSubmit.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class FormSubmit {
/**
* Build class.
*
* @codeCoverageIgnore
*/
public function __construct() {
Expand All @@ -21,7 +22,9 @@ public function __construct() {

/**
* Init
*
* @return void
*
* @codeCoverageIgnore
*/
private function init() {
Expand All @@ -33,11 +36,16 @@ private function init() {
* Contact Form 7 doesn't respect JS checkValidity() function, so this is a custom compatibility fix.
*/
add_filter( 'wpcf7_validate', [ $this, 'maybe_track_submission' ], 10, 2 );
/**
* Gravity Forms contains its own form submission handler, so this is a custom compatibility fix.
*/
add_action( 'gform_after_submission', [ $this, 'track_gravity_forms_submission' ], 10 );
}

/**
* Enqueues the required JavaScript for form submissions integration.
* @return void
*
* @codeCoverageIgnore because there's nothing to test here.
*/
public function add_js() {
Expand All @@ -60,10 +68,13 @@ public function add_js() {
/**
* Tracks the form submission if CF7 says it's valid.
*
* @filter wpcf7_validate
*
* @param \WPCF7_Validation $result Form submission result object containing validation results.
* @param array $tags Array of tags associated with the form fields.
*
* @return \WPCF7_Validation
*
* @codeCoverageIgnore because we can't test XHR requests here.
*/
public function maybe_track_submission( $result, $tags ) {
Expand All @@ -73,15 +84,51 @@ public function maybe_track_submission( $result, $tags ) {
$post = get_post( $_POST[ '_wpcf7_container_post' ] );
$uri = '/' . $post->post_name . '/';

$proxy = new Proxy( false );
$proxy->do_request(
__( 'WP Form Completions', 'plausible-analytics' ),
null,
null,
[ 'path' => $uri ]
);
$this->track_submission( $uri );
}

return $result;
}

/**
* Track submission using the Proxy.
*
* @param $uri
*
* @return void
*
* @codeCoverageIgnore because we can't test XHR requests here.
*/
private function track_submission( $uri ) {
$proxy = new Proxy( false );

$proxy->do_request(
__( 'WP Form Completions', 'plausible-analytics' ),
null,
null,
[ 'path' => $uri ]
);
}

/**
* Compatibility fix for Gravity Forms.
*
* @action gform_after_submission
*
* @param $form
* @param $entry
*
* @return void
*
* @codeCoverageIgnore because we can't test XHR requests here.
*/
public function track_gravity_forms_submission( $form ) {
$uri = str_replace( home_url(), '', $form[ 'source_url' ] ) ?? '';

if ( empty( $uri ) ) {
return;
}

$this->track_submission( $uri );
}
}
Loading