| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plausible Analytics | Integrations | Form Submissions. |
| 4 |
* @since 2.2.0 |
| 5 |
* @package WordPress |
| 6 |
* @subpackage Plausible Analytics |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Plausible\Analytics\WP\Integrations; |
| 10 |
|
| 11 |
use Plausible\Analytics\WP\Helpers; |
| 12 |
use Plausible\Analytics\WP\Proxy; |
| 13 |
|
| 14 |
class FormSubmit { |
| 15 |
/** |
| 16 |
* Build class. |
| 17 |
* |
| 18 |
* @codeCoverageIgnore |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
$this->init(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Init |
| 26 |
* |
| 27 |
* @return void |
| 28 |
* |
| 29 |
* @codeCoverageIgnore |
| 30 |
*/ |
| 31 |
private function init() { |
| 32 |
/** |
| 33 |
* Adds required JS and classes. |
| 34 |
*/ |
| 35 |
add_action( 'wp_enqueue_scripts', [ $this, 'add_js' ], 1 ); |
| 36 |
/** |
| 37 |
* Contact Form 7 doesn't respect JS' checkValidity() function, so this is a custom compatibility fix. |
| 38 |
*/ |
| 39 |
add_filter( 'wpcf7_validate', [ $this, 'maybe_track_submission' ], 10, 2 ); |
| 40 |
/** |
| 41 |
* Gravity Forms contains its own form submission handler, so this is a custom compatibility fix. |
| 42 |
*/ |
| 43 |
add_action( 'gform_after_submission', [ $this, 'track_gravity_forms_submission' ], 10 ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Enqueues the required JavaScript for form submissions integration. |
| 48 |
* @return void |
| 49 |
* |
| 50 |
* @codeCoverageIgnore because there's nothing to test here. |
| 51 |
*/ |
| 52 |
public function add_js() { |
| 53 |
if ( ! Helpers::main_script_is_registered() ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
wp_register_script( |
| 58 |
'plausible-form-submit-integration', |
| 59 |
PLAUSIBLE_ANALYTICS_PLUGIN_URL . 'assets/dist/js/plausible-form-submit-integration.js', |
| 60 |
[ 'plausible-analytics' ], |
| 61 |
filemtime( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-form-submit-integration.js' ), |
| 62 |
[ 'in_footer' => true ], |
| 63 |
); |
| 64 |
|
| 65 |
wp_localize_script( |
| 66 |
'plausible-form-submit-integration', |
| 67 |
'plausible_analytics_i18n', |
| 68 |
[ 'form_completions' => __( 'WP Form Completions', 'plausible-analytics' ), ] |
| 69 |
); |
| 70 |
|
| 71 |
wp_enqueue_script( 'plausible-form-submit-integration' ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Tracks the form submission if CF7 says it's valid. |
| 76 |
* |
| 77 |
* @filter wpcf7_validate |
| 78 |
* |
| 79 |
* @param \WPCF7_Validation $result Form submission result object containing validation results. |
| 80 |
* @param array $tags Array of tags associated with the form fields. |
| 81 |
* |
| 82 |
* @return \WPCF7_Validation |
| 83 |
* |
| 84 |
* @codeCoverageIgnore because we can't test XHR requests here. |
| 85 |
*/ |
| 86 |
public function maybe_track_submission( $result, $tags ) { |
| 87 |
$invalid_fields = $result->get_invalid_fields(); |
| 88 |
|
| 89 |
if ( empty( $invalid_fields ) ) { |
| 90 |
$post = get_post( $_POST['_wpcf7_container_post'] ); |
| 91 |
$uri = '/' . $post->post_name . '/'; |
| 92 |
|
| 93 |
$this->track_submission( $uri ); |
| 94 |
} |
| 95 |
|
| 96 |
return $result; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Track submission using the Proxy. |
| 101 |
* |
| 102 |
* @param $uri |
| 103 |
* |
| 104 |
* @return void |
| 105 |
* |
| 106 |
* @codeCoverageIgnore because we can't test XHR requests here. |
| 107 |
*/ |
| 108 |
private function track_submission( $uri ) { |
| 109 |
$proxy = new Proxy( false ); |
| 110 |
|
| 111 |
$proxy->do_request( |
| 112 |
__( 'WP Form Completions', 'plausible-analytics' ), |
| 113 |
null, |
| 114 |
null, |
| 115 |
[ 'path' => $uri ] |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Compatibility fix for Gravity Forms. |
| 121 |
* |
| 122 |
* @action gform_after_submission |
| 123 |
* |
| 124 |
* @param $form |
| 125 |
* @param $entry |
| 126 |
* |
| 127 |
* @return void |
| 128 |
* |
| 129 |
* @codeCoverageIgnore because we can't test XHR requests here. |
| 130 |
*/ |
| 131 |
public function track_gravity_forms_submission( $form ) { |
| 132 |
$uri = str_replace( home_url(), '', $form['source_url'] ) ?? ''; |
| 133 |
|
| 134 |
if ( empty( $uri ) ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
$this->track_submission( $uri ); |
| 139 |
} |
| 140 |
} |
| 141 |
|