Skip to content

Commit 7f0fc47

Browse files
committed
Always hook mailpoet logic to form submission
1 parent 181651a commit 7f0fc47

File tree

2 files changed

+40
-48
lines changed

2 files changed

+40
-48
lines changed

projects/packages/forms/src/contact-form/class-contact-form-plugin.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ protected function __construct() {
302302
}
303303

304304
self::register_contact_form_blocks();
305+
306+
// Register MailPoet integration hook after the class is loaded.
307+
add_action(
308+
'grunion_after_feedback_post_inserted',
309+
array( MailPoet_Integration::class, 'handle_mailpoet_integration' ),
310+
15,
311+
3
312+
);
305313
}
306314

307315
/**
@@ -1385,10 +1393,6 @@ public function process_form_submission() {
13851393
Post_To_Url::init();
13861394
}
13871395

1388-
// MailPoet integration: initialize if enabled.
1389-
if ( ! empty( $form->attributes['connectMailPoet'] ) ) {
1390-
MailPoet_Integration::init();
1391-
}
13921396
// Process the form
13931397
return $form->process_submission();
13941398
}

projects/packages/forms/src/service/class-mailpoet-integration.php

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,24 @@
1313
* Handles integration with MailPoet for Jetpack Contact Forms.
1414
*/
1515
class MailPoet_Integration {
16-
/**
17-
* Singleton instance
18-
*
19-
* @var MailPoet_Integration
20-
*/
21-
private static $instance = null;
22-
2316
/**
2417
* MailPoet API instance
2518
*
2619
* @var mixed
2720
*/
28-
protected $mailpoet_api = null;
29-
30-
/**
31-
* Initialize and return singleton instance.
32-
*
33-
* @return MailPoet_Integration
34-
*/
35-
public static function init() {
36-
if ( null === self::$instance ) {
37-
self::$instance = new self();
38-
}
39-
return self::$instance;
40-
}
41-
42-
/**
43-
* MailPoet_Integration class constructor.
44-
* Hooks on `grunion_after_feedback_post_inserted` action to handle MailPoet integration.
45-
*/
46-
private function __construct() {
47-
add_action( 'grunion_after_feedback_post_inserted', array( $this, 'handle_mailpoet_integration' ), 15, 3 );
48-
}
21+
protected static $mailpoet_api = null;
4922

5023
/**
5124
* Get the MailPoet API instance (v1), instantiating if necessary.
5225
*
5326
* @return mixed
5427
*/
55-
protected function get_api() {
56-
if ( null === $this->mailpoet_api && class_exists( '\MailPoet\API\API' ) ) {
28+
protected static function get_api() {
29+
if ( null === self::$mailpoet_api && class_exists( '\MailPoet\API\API' ) ) {
5730
// @phan-suppress-next-line PhanUndeclaredClassMethod
58-
$this->mailpoet_api = \MailPoet\API\API::MP( 'v1' );
31+
self::$mailpoet_api = \MailPoet\API\API::MP( 'v1' );
5932
}
60-
return $this->mailpoet_api;
33+
return self::$mailpoet_api;
6134
}
6235

6336
/**
@@ -67,7 +40,7 @@ protected function get_api() {
6740
* @param string|null $list_name Optional. The name of the list to get or create. Defaults to 'Jetpack Form Subscribers'.
6841
* @return string|null List ID or null on failure.
6942
*/
70-
protected function get_or_create_list_id( $mailpoet_api, $list_name = null ) {
43+
protected static function get_or_create_list_id( $mailpoet_api, $list_name = null ) {
7144
$default_list_name = 'Jetpack Form Subscribers';
7245
$default_list_description = 'Subscribers from Jetpack Forms';
7346
$list_name = $list_name ? $list_name : $default_list_name;
@@ -101,7 +74,7 @@ protected function get_or_create_list_id( $mailpoet_api, $list_name = null ) {
10174
* @param array $subscriber_data Associative array with at least 'email', optionally 'first_name', 'last_name'.
10275
* @return array|null Subscriber data on success, or null on failure.
10376
*/
104-
protected function add_subscriber_to_list( $mailpoet_api, $list_id, $subscriber_data ) {
77+
protected static function add_subscriber_to_list( $mailpoet_api, $list_id, $subscriber_data ) {
10578
try {
10679
$subscriber = $mailpoet_api->addSubscriber(
10780
$subscriber_data,
@@ -119,7 +92,7 @@ protected function add_subscriber_to_list( $mailpoet_api, $list_id, $subscriber_
11992
* @param array $fields Collection of Contact_Form_Field instances.
12093
* @return array Associative array with at least 'email', optionally 'first_name', 'last_name'. Empty array if no email found.
12194
*/
122-
protected function get_subscriber_data_from_fields( $fields ) {
95+
protected static function get_subscriber_data_from_fields( $fields ) {
12396
// Try and get the form from any of the fields
12497
$form = null;
12598
foreach ( $fields as $field ) {
@@ -132,7 +105,6 @@ protected function get_subscriber_data_from_fields( $fields ) {
132105
return array();
133106
}
134107

135-
// Extract email, first_name, last_name from form fields.
136108
$subscriber_data = array();
137109
foreach ( $form->fields as $field ) {
138110
$id = strtolower( str_replace( array( ' ', '_' ), '', $field->get_attribute( 'id' ) ) );
@@ -148,7 +120,6 @@ protected function get_subscriber_data_from_fields( $fields ) {
148120
}
149121
}
150122

151-
// Only return the subscriber data if we have an email.
152123
if ( empty( $subscriber_data['email'] ) ) {
153124
return array();
154125
}
@@ -163,28 +134,45 @@ protected function get_subscriber_data_from_fields( $fields ) {
163134
* @param array $fields Collection of Contact_Form_Field instances.
164135
* @param bool $is_spam Whether the submission is spam.
165136
*/
166-
public function handle_mailpoet_integration( $post_id, $fields, $is_spam ) {
137+
public static function handle_mailpoet_integration( $post_id, $fields, $is_spam ) {
167138
if ( $is_spam ) {
168139
return;
169140
}
170-
$mailpoet_api = $this->get_api();
141+
142+
// Try and get the form from any of the fields
143+
$form = null;
144+
foreach ( $fields as $field ) {
145+
if ( ! empty( $field->form ) ) {
146+
$form = $field->form;
147+
break;
148+
}
149+
}
150+
if ( ! $form || ! is_a( $form, 'Automattic\Jetpack\Forms\ContactForm\Contact_Form' ) ) {
151+
return;
152+
}
153+
154+
if ( empty( $form->attributes['connectMailPoet'] ) ) {
155+
return;
156+
}
157+
158+
$mailpoet_api = self::get_api();
171159
if ( ! $mailpoet_api ) {
172160
// MailPoet is not active or not loaded.
173161
return;
174162
}
175163

176-
$list_id = $this->get_or_create_list_id( $mailpoet_api );
164+
$list_id = self::get_or_create_list_id( $mailpoet_api );
177165
if ( ! $list_id ) {
178-
// Could not get or create the list.
166+
// Could not get or create the list; bail out.
179167
return;
180168
}
181169

182-
$subscriber_data = $this->get_subscriber_data_from_fields( $fields );
170+
$subscriber_data = self::get_subscriber_data_from_fields( $fields );
183171
if ( empty( $subscriber_data ) ) {
184-
// Could not get minimum required subscriber data (email).
172+
// Email is required for MailPoet subscribers.
185173
return;
186174
}
187175

188-
$this->add_subscriber_to_list( $mailpoet_api, $list_id, $subscriber_data );
176+
self::add_subscriber_to_list( $mailpoet_api, $list_id, $subscriber_data );
189177
}
190178
}

0 commit comments

Comments
 (0)