diff --git a/phpcs.xml b/phpcs.xml
index 8748885b..cf253a9d 100755
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -7,11 +7,14 @@
includes/
integrations/
config/
+ sample-code-snippets/
*\.(html|css|js|md|json|xml|sh)
+
+
diff --git a/sample-code-snippets/add-ons/download-monitor/redirect-to-detailed-form-if-new-subscriber.php b/sample-code-snippets/add-ons/download-monitor/redirect-to-detailed-form-if-new-subscriber.php
index b823a9a1..e4778b30 100755
--- a/sample-code-snippets/add-ons/download-monitor/redirect-to-detailed-form-if-new-subscriber.php
+++ b/sample-code-snippets/add-ons/download-monitor/redirect-to-detailed-form-if-new-subscriber.php
@@ -1,30 +1,30 @@
ID != 500 ) {
- return;
- }
- dlm_mailchimp_set_cookie( $form->data['EMAIL'] );
+add_action('mc4wp_form_updated_subscriber', function ($form) {
+ if ($form->ID != 500) {
+ return;
+ }
+
+ dlm_mailchimp_set_cookie($form->data['EMAIL']);
// form was used to update a subscriber; redirect to download
- wp_redirect( 'http://my-site.com/download-url' );
- exit;
+ wp_redirect('http://my-site.com/download-url');
+ exit;
});
-add_action( 'mc4wp_form_subscribed', function( $form ) {
- if( $form->ID != 500 ) {
- return;
- }
+add_action('mc4wp_form_subscribed', function ($form) {
+ if ($form->ID != 500) {
+ return;
+ }
// form was used to add a new subscriber; redirect to detailed form
- wp_redirect( 'http://my-site.com/page-with-detailed-form' );
- exit;
+ wp_redirect('http://my-site.com/page-with-detailed-form');
+ exit;
});
-
diff --git a/sample-code-snippets/add-ons/top-bar/add-consent-checkbox.php b/sample-code-snippets/add-ons/top-bar/add-consent-checkbox.php
index 0b9de711..a2e5f713 100755
--- a/sample-code-snippets/add-ons/top-bar/add-consent-checkbox.php
+++ b/sample-code-snippets/add-ons/top-bar/add-consent-checkbox.php
@@ -1,5 +1,5 @@
- I have read and agree to the terms & conditions';
+add_action('mctb_before_submit_button', function () {
+ echo ' I have read and agree to the terms & conditions';
});
diff --git a/sample-code-snippets/add-ons/top-bar/add-name-field.php b/sample-code-snippets/add-ons/top-bar/add-name-field.php
index 78c5a6ce..b6a27850 100755
--- a/sample-code-snippets/add-ons/top-bar/add-name-field.php
+++ b/sample-code-snippets/add-ons/top-bar/add-name-field.php
@@ -3,8 +3,9 @@
/**
* Echo a NAME field just before the submit button.
*/
-add_action( 'mctb_before_submit_button', function() {
- echo '';
+
+add_action('mctb_before_submit_button', function () {
+ echo '';
});
@@ -13,7 +14,7 @@
*
* @param array $vars
*/
-add_filter( 'mctb_data', function( $vars ) {
- $vars['NAME'] = ( isset( $_POST['NAME'] ) ) ? sanitize_text_field( $_POST['NAME'] ) : '';
- return $vars;
+add_filter('mctb_data', function ($vars) {
+ $vars['NAME'] = ( isset($_POST['NAME']) ) ? sanitize_text_field($_POST['NAME']) : '';
+ return $vars;
});
diff --git a/sample-code-snippets/add-ons/top-bar/add-subscriber-tag.php b/sample-code-snippets/add-ons/top-bar/add-subscriber-tag.php
index e77e3ce0..ac105382 100755
--- a/sample-code-snippets/add-ons/top-bar/add-subscriber-tag.php
+++ b/sample-code-snippets/add-ons/top-bar/add-subscriber-tag.php
@@ -3,8 +3,8 @@
/**
* Echo a susbcriber tag
*/
-
- add_filter( 'mctb_subscriber_data', function( $subscriber ) {
+
+ add_filter('mctb_subscriber_data', function ($subscriber) {
$subscriber->tags[] = 'My tag';
return $subscriber;
-});
+ });
diff --git a/sample-code-snippets/add-ons/top-bar/add-to-interest-groups.php b/sample-code-snippets/add-ons/top-bar/add-to-interest-groups.php
index d2fa27a8..0338f913 100755
--- a/sample-code-snippets/add-ons/top-bar/add-to-interest-groups.php
+++ b/sample-code-snippets/add-ons/top-bar/add-to-interest-groups.php
@@ -1,14 +1,14 @@
-
-
+
+ ';
+function mc4wp_insert_after_paragraph($content, $insertion, $paragraph_number = 2)
+{
+ static $closing_p = '
';
- $paragraphs = explode( $closing_p, $content );
- $new_content = '';
- $target_index = $paragraph_number - 1;
+ $paragraphs = explode($closing_p, $content);
+ $new_content = '';
+ $target_index = $paragraph_number - 1;
- foreach( $paragraphs as $index => $paragraph ) {
- $new_content .= $paragraph;
+ foreach ($paragraphs as $index => $paragraph) {
+ $new_content .= $paragraph;
- if( $index == $target_index ) {
- $new_content .= $insertion;
- }
- }
+ if ($index == $target_index) {
+ $new_content .= $insertion;
+ }
+ }
- return $new_content;
+ return $new_content;
}
diff --git a/sample-code-snippets/forms/add-form-after-post.php b/sample-code-snippets/forms/add-form-after-post.php
index 9891e26f..6166176a 100755
--- a/sample-code-snippets/forms/add-form-after-post.php
+++ b/sample-code-snippets/forms/add-form-after-post.php
@@ -3,13 +3,12 @@
/**
* Insert a sign-up form after the post content.
*/
-add_filter( 'the_content', function( $content ) {
- if( is_single() ) {
- $content .= mc4wp_get_form();
- }
-
- return $content;
-});
+add_filter('the_content', function ($content) {
+ if (is_single()) {
+ $content .= mc4wp_get_form();
+ }
+ return $content;
+});
diff --git a/sample-code-snippets/forms/blacklist-email-addresses.php b/sample-code-snippets/forms/blacklist-email-addresses.php
index aa6dd07e..e50c2907 100755
--- a/sample-code-snippets/forms/blacklist-email-addresses.php
+++ b/sample-code-snippets/forms/blacklist-email-addresses.php
@@ -3,19 +3,20 @@
/**
* Block certain email addresses from signing up through forms.
*/
-add_filter( 'mc4wp_form_errors', function( $errors, MC4WP_Form $form ) {
+
+add_filter('mc4wp_form_errors', function ($errors, MC4WP_Form $form) {
$data = $form->get_data();
- $email = strtolower( $data['EMAIL'] );
+ $email = strtolower($data['EMAIL']);
// add your blocked email addresses here.
- $blocked_emails = array(
+ $blocked_emails = [
'someemail@email.com',
'someotheremail@email.com',
- );
+ ];
- if( in_array( $email, $blocked_emails ) ) {
+ if (in_array($email, $blocked_emails)) {
$errors[] = 'spam';
}
return $errors;
-}, 10, 2 );
\ No newline at end of file
+}, 10, 2);
diff --git a/sample-code-snippets/forms/blacklist-ip-addresses.php b/sample-code-snippets/forms/blacklist-ip-addresses.php
index b7ed7614..cf27a2bd 100755
--- a/sample-code-snippets/forms/blacklist-ip-addresses.php
+++ b/sample-code-snippets/forms/blacklist-ip-addresses.php
@@ -3,16 +3,17 @@
/**
* Block certain IP addresses from signing up through forms.
*/
-add_filter( 'mc4wp_form_errors', function( $errors ) {
+
+add_filter('mc4wp_form_errors', function ($errors) {
// add your blocked IP Addresses here.
- $blocked_ips = array(
+ $blocked_ips = [
'123.456.789.1'
- );
+ ];
- if( in_array( $_SERVER['REMOTE_ADDR'], $blocked_ips ) ) {
+ if (in_array($_SERVER['REMOTE_ADDR'], $blocked_ips)) {
$errors[] = 'spam';
}
return $errors;
-});
\ No newline at end of file
+});
diff --git a/sample-code-snippets/forms/combine-two-fields-into-one.php b/sample-code-snippets/forms/combine-two-fields-into-one.php
index faea19c1..78f53d62 100755
--- a/sample-code-snippets/forms/combine-two-fields-into-one.php
+++ b/sample-code-snippets/forms/combine-two-fields-into-one.php
@@ -8,17 +8,18 @@
*
* @return array
*/
-function myprefix_combine_fields( $data, MC4WP_Form $form ) {
+function myprefix_combine_fields($data, MC4WP_Form $form)
+{
- // get values for both fields
- $field1 = ( isset( $data['FIELD_ONE'] ) ) ? $data['FIELD_ONE'] : '';
- $field2 = ( isset( $data['FIELD_TWO'] ) ) ? $data['FIELD_TWO'] : '';
+ // get values for both fields
+ $field1 = ( isset($data['FIELD_ONE']) ) ? $data['FIELD_ONE'] : '';
+ $field2 = ( isset($data['FIELD_TWO']) ) ? $data['FIELD_TWO'] : '';
- // merge the two fields into one
+ // merge the two fields into one
$data['COMBINED_FIELD'] = $field1 . ' ' . $field2;
- // return customized data
- return $data;
+ // return customized data
+ return $data;
}
-add_filter( 'mc4wp_form_data', 'myprefix_combine_fields', 10, 2 );
\ No newline at end of file
+add_filter('mc4wp_form_data', 'myprefix_combine_fields', 10, 2);
diff --git a/sample-code-snippets/forms/create-user-on-success.php b/sample-code-snippets/forms/create-user-on-success.php
index 3ef2b7f6..061933c6 100755
--- a/sample-code-snippets/forms/create-user-on-success.php
+++ b/sample-code-snippets/forms/create-user-on-success.php
@@ -5,27 +5,28 @@
*
* @param MC4WP_Form $form
*/
-add_action( 'mc4wp_form_subscribed', function( MC4WP_Form $form ) {
- // do nothing if current user is logged in
- if( is_user_logged_in() ) {
- return;
- }
+add_action('mc4wp_form_subscribed', function (MC4WP_Form $form) {
- // get form data
- $data = $form->get_data();
+ // do nothing if current user is logged in
+ if (is_user_logged_in()) {
+ return;
+ }
- // use email as username
- $username = $data['EMAIL'];
+ // get form data
+ $data = $form->get_data();
- // generate a random password
- $password = wp_generate_password();
+ // use email as username
+ $username = $data['EMAIL'];
- // try to create the user (or error when user already exists)
- $user_id = wp_create_user( $username, $password );
+ // generate a random password
+ $password = wp_generate_password();
- // send email notification with password reset
- if( $user_id ) {
- wp_new_user_notification( $user_id );
- }
-});
\ No newline at end of file
+ // try to create the user (or error when user already exists)
+ $user_id = wp_create_user($username, $password);
+
+ // send email notification with password reset
+ if ($user_id) {
+ wp_new_user_notification($user_id);
+ }
+});
diff --git a/sample-code-snippets/forms/custom-validation.php b/sample-code-snippets/forms/custom-validation.php
index 8a3c4323..a699fd2f 100755
--- a/sample-code-snippets/forms/custom-validation.php
+++ b/sample-code-snippets/forms/custom-validation.php
@@ -9,19 +9,19 @@
* @param MC4WP_Form $form
* @return array
*/
-add_filter( 'mc4wp_form_errors', function( array $errors, MC4WP_Form $form ) {
- // perform logic here
- $custom_validation_passed = false;
+add_filter('mc4wp_form_errors', function (array $errors, MC4WP_Form $form) {
- if( ! $custom_validation_passed ) {
- $errors[] = 'your_error_code';
- }
+ // perform logic here
+ $custom_validation_passed = false;
- // $errors is empty if there were no errors.
- return $errors;
+ if (! $custom_validation_passed) {
+ $errors[] = 'your_error_code';
+ }
-}, 10, 2 );
+ // $errors is empty if there were no errors.
+ return $errors;
+}, 10, 2);
/**
@@ -29,13 +29,13 @@
*
* Require SOME_FIELD to have a value of "Some value"
*/
-add_filter( 'mc4wp_form_errors', function( array $errors, MC4WP_Form $form ) {
+add_filter('mc4wp_form_errors', function (array $errors, MC4WP_Form $form) {
$data = $form->get_data();
- if( $data['SOME_FIELD'] !== 'Some value' ) {
- $errors[] = 'incorrect_value';
- }
+ if ($data['SOME_FIELD'] !== 'Some value') {
+ $errors[] = 'incorrect_value';
+ }
- return $errors;
-}, 10, 2 );
\ No newline at end of file
+ return $errors;
+}, 10, 2);
diff --git a/sample-code-snippets/forms/enable-all-gdpr-marketing-permissions.php b/sample-code-snippets/forms/enable-all-gdpr-marketing-permissions.php
index 049221a3..1d610d0b 100755
--- a/sample-code-snippets/forms/enable-all-gdpr-marketing-permissions.php
+++ b/sample-code-snippets/forms/enable-all-gdpr-marketing-permissions.php
@@ -3,15 +3,16 @@
/**
* This snippet enables every marketing permission in the list a new subscriber is about to be added to (or updated in)
*/
-add_filter( 'mc4wp_subscriber_data', function ( $subscriber, $list_id ) {
- $mailchimp = new MC4WP_MailChimp();
- $marketing_permissions = $mailchimp->get_list_marketing_permissions( $list_id );
- foreach ( $marketing_permissions as $mp ) {
- $subscriber->marketing_permissions[] = (object) array(
- 'marketing_permission_id' => $mp->marketing_permission_id,
- 'enabled' => true,
- );
- }
- return $subscriber;
-}, 10, 2 );
+add_filter('mc4wp_subscriber_data', function ($subscriber, $list_id) {
+ $mailchimp = new MC4WP_MailChimp();
+ $marketing_permissions = $mailchimp->get_list_marketing_permissions($list_id);
+ foreach ($marketing_permissions as $mp) {
+ $subscriber->marketing_permissions[] = (object) [
+ 'marketing_permission_id' => $mp->marketing_permission_id,
+ 'enabled' => true,
+ ];
+ }
+
+ return $subscriber;
+}, 10, 2);
diff --git a/sample-code-snippets/forms/hide-form-for-people-that-subscribed.php b/sample-code-snippets/forms/hide-form-for-people-that-subscribed.php
index ef248291..9e27efac 100755
--- a/sample-code-snippets/forms/hide-form-for-people-that-subscribed.php
+++ b/sample-code-snippets/forms/hide-form-for-people-that-subscribed.php
@@ -3,15 +3,16 @@
/**
* Set a cookie whenever someone subscribes
*/
-add_action( 'mc4wp_form_subscribed', function() {
- setcookie( 'mc4wp_subscribed', 1, time() + 3600 * 24 * 90, '/' );
+
+add_action('mc4wp_form_subscribed', function () {
+ setcookie('mc4wp_subscribed', 1, time() + 3600 * 24 * 90, '/');
});
/**
* Prints CSS that hides the MailChimp for WordPress form if the "subscribed" cookie is set.
*/
-add_action( 'wp_head', function() {
- if( isset( $_COOKIE['mc4wp_subscribed'] ) && empty( $_POST['_mc4wp_form_id'] ) ) {
+add_action('wp_head', function () {
+ if (isset($_COOKIE['mc4wp_subscribed']) && empty($_POST['_mc4wp_form_id'])) {
?>
ID, $expires, '/' );
+ setcookie('mc4wp_subscribed', $form->ID, $expires, '/');
});
/**
* Prints CSS that hides a specific MailChimp for WordPress form if the "subscribed" cookie is set.
*/
-add_action( 'wp_head', function() {
- if( isset( $_COOKIE['mc4wp_subscribed'] ) && empty( $_POST['_mc4wp_form_id'] ) ) {
+add_action('wp_head', function () {
+ if (isset($_COOKIE['mc4wp_subscribed']) && empty($_POST['_mc4wp_form_id'])) {
$form_id = (int) $_COOKIE['mc4wp_subscribed'];
?>
name;
- return $data;
+ return $data;
}
-add_filter( 'mc4wp_form_data', 'myprefix_send_additional_field', 10, 2 );
+add_filter('mc4wp_form_data', 'myprefix_send_additional_field', 10, 2);
diff --git a/sample-code-snippets/forms/replace-string-in-form-content.php b/sample-code-snippets/forms/replace-string-in-form-content.php
index a34ec9bf..3b22dd98 100755
--- a/sample-code-snippets/forms/replace-string-in-form-content.php
+++ b/sample-code-snippets/forms/replace-string-in-form-content.php
@@ -1,16 +1,16 @@
- elements
*/
-add_filter( 'mc4wp_form_content', function($content) {
- $posts = get_posts(array( 'post_status' => 'publish' ) );
- $options = array();
- foreach( $posts as $post) {
- $options[] = sprintf( '', $post->ID, $post->post_title );
- }
- $options = join('', $options );
- $content = str_replace('{MY_POST_OPTIONS}', $options, $content);
- return $content;
-});
+add_filter('mc4wp_form_content', function ($content) {
+ $posts = get_posts([ 'post_status' => 'publish' ]);
+ $options = [];
+ foreach ($posts as $post) {
+ $options[] = sprintf('', $post->ID, $post->post_title);
+ }
+ $options = join('', $options);
+ $content = str_replace('{MY_POST_OPTIONS}', $options, $content);
+ return $content;
+});
diff --git a/sample-code-snippets/forms/send-email-for-mailchimp-errors.php b/sample-code-snippets/forms/send-email-for-mailchimp-errors.php
index 20323ea9..e94537eb 100755
--- a/sample-code-snippets/forms/send-email-for-mailchimp-errors.php
+++ b/sample-code-snippets/forms/send-email-for-mailchimp-errors.php
@@ -3,12 +3,13 @@
/**
* This will send an email for every API error that occurs on forms.
*/
-add_action( 'mc4wp_form_api_error', function( $form, $error_message ) {
+
+add_action('mc4wp_form_api_error', function ($form, $error_message) {
// email variables
$email_to = 'email@email.com';
$email_subject = 'Form API failure';
- $email_message = sprintf( 'Form %d encountered a MailChimp API error: %s', $form->ID, $error_message );
+ $email_message = sprintf('Form %d encountered a MailChimp API error: %s', $form->ID, $error_message);
// send the email
- wp_mail( $email_to, $email_subject, $email_message );
-}, 10, 2 );
+ wp_mail($email_to, $email_subject, $email_message);
+}, 10, 2);
diff --git a/sample-code-snippets/forms/set-language.php b/sample-code-snippets/forms/set-language.php
index e25561c7..bc6a6e18 100755
--- a/sample-code-snippets/forms/set-language.php
+++ b/sample-code-snippets/forms/set-language.php
@@ -1,6 +1,7 @@
-German
-If you use WPML you may want to use https://wordpress.org/plugins/mc4wp-wpml/ to send in the WPML lanugage.
+If you use WPML you may want to use https://wordpress.org/plugins/mc4wp-wpml/ to send in the WPML lanugage.
*/
-add_filter( 'mc4wp_subscriber_data', function( MC4WP_MailChimp_Subscriber $subscriber ) {
- $language_code = sanitize_text_field( $_POST[ 'language' ] );
- $subscriber->language = strtolower( substr( $language_code, 0, 2 ) );
- return $subscriber;
+add_filter('mc4wp_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
+ $language_code = sanitize_text_field($_POST[ 'language' ]);
+ $subscriber->language = strtolower(substr($language_code, 0, 2));
+ return $subscriber;
});
diff --git a/sample-code-snippets/forms/subscriber-tags.php b/sample-code-snippets/forms/subscriber-tags.php
index 26fe16f1..8c5946cb 100755
--- a/sample-code-snippets/forms/subscriber-tags.php
+++ b/sample-code-snippets/forms/subscriber-tags.php
@@ -6,7 +6,8 @@
* The "mc4wp_subscriber_form_data" filter only runs for form requests.
* Use "mc4wp_subscriber_data" to hook into both form & integration requests.
*/
-add_filter( 'mc4wp_subscriber_data', function(MC4WP_MailChimp_Subscriber $subscriber) {
- $subscriber->tags[] = 'My tag';
- return $subscriber;
+
+add_filter('mc4wp_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
+ $subscriber->tags[] = 'My tag';
+ return $subscriber;
});
diff --git a/sample-code-snippets/integrations/buddypress/custom-user-fields.php b/sample-code-snippets/integrations/buddypress/custom-user-fields.php
index 1e382b38..a5832f51 100755
--- a/sample-code-snippets/integrations/buddypress/custom-user-fields.php
+++ b/sample-code-snippets/integrations/buddypress/custom-user-fields.php
@@ -6,7 +6,8 @@
* - Replace "CUST1" with your MailChimp field name.
* - Replace "13" with your BuddyPress field ID.
*/
-add_filter( 'mc4wp_integration_buddypress_data', function( $data, $user_id ) {
- $data['CUST1'] = xprofile_get_field_data( 13 , $user_id );
+
+add_filter('mc4wp_integration_buddypress_data', function ($data, $user_id) {
+ $data['CUST1'] = xprofile_get_field_data(13, $user_id);
return $data;
-}, 10, 2);
\ No newline at end of file
+}, 10, 2);
diff --git a/sample-code-snippets/integrations/contact-form-7/different-tag.php b/sample-code-snippets/integrations/contact-form-7/different-tag.php
index 0090e39c..331c6226 100755
--- a/sample-code-snippets/integrations/contact-form-7/different-tag.php
+++ b/sample-code-snippets/integrations/contact-form-7/different-tag.php
@@ -1,10 +1,10 @@
tags[] = 'ES';
- } else if ($cf7_form_id == 510) {
- $subscriber->tags[] = 'NL';
- }
- return $subscriber;
+add_filter('mc4wp_integration_contact-form-7_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber, $cf7_form_id) {
+ if ($cf7_form_id == 500) {
+ $subscriber->tags[] = 'ES';
+ } elseif ($cf7_form_id == 510) {
+ $subscriber->tags[] = 'NL';
+ }
+ return $subscriber;
}, 10, 2);
diff --git a/sample-code-snippets/integrations/contact-form-7/require-checkbox.php b/sample-code-snippets/integrations/contact-form-7/require-checkbox.php
index 117125cc..c9115c76 100755
--- a/sample-code-snippets/integrations/contact-form-7/require-checkbox.php
+++ b/sample-code-snippets/integrations/contact-form-7/require-checkbox.php
@@ -5,7 +5,10 @@
*
* This code uses CF7 logic to ensure that the subscribe checkbox is checked.
*/
-add_filter( 'wpcf7_acceptance', function( $yes ) {
- if( ! $yes ) { return false; }
- return ! empty( $_POST['_mc4wp_subscribe_contact-form-7'] );
-});
\ No newline at end of file
+
+add_filter('wpcf7_acceptance', function ($yes) {
+ if (! $yes) {
+ return false;
+ }
+ return ! empty($_POST['_mc4wp_subscribe_contact-form-7']);
+});
diff --git a/sample-code-snippets/integrations/disable-css-stylesheet.php b/sample-code-snippets/integrations/disable-css-stylesheet.php
index 6e7be0e0..0fce737a 100755
--- a/sample-code-snippets/integrations/disable-css-stylesheet.php
+++ b/sample-code-snippets/integrations/disable-css-stylesheet.php
@@ -8,7 +8,8 @@
* - 1: Yes, load CSS file.
* - 0: No, load no CSS file.
*/
-add_filter( 'default_option_mc4wp_checkbox', function( $options ) {
- $options['css'] = 0; //Set value of "Load some default CSS?"" to false
- return $options;
-});
\ No newline at end of file
+
+add_filter('default_option_mc4wp_checkbox', function ($options) {
+ $options['css'] = 0; //Set value of "Load some default CSS?"" to false
+ return $options;
+});
diff --git a/sample-code-snippets/integrations/filter-integration-options.php b/sample-code-snippets/integrations/filter-integration-options.php
index 478c46fe..693f77c2 100755
--- a/sample-code-snippets/integrations/filter-integration-options.php
+++ b/sample-code-snippets/integrations/filter-integration-options.php
@@ -3,7 +3,8 @@
/**
* This filter enables the "update existing subscribers" option for the MailChimp for WordPress Gravity Forms integration
*/
-add_filter( 'mc4wp_integration_gravity-forms_options', function( $opts ) {
- $opts['update_existing'] = true;
- return $opts;
+
+add_filter('mc4wp_integration_gravity-forms_options', function ($opts) {
+ $opts['update_existing'] = true;
+ return $opts;
});
diff --git a/sample-code-snippets/integrations/gravity-forms-field-mapping.php b/sample-code-snippets/integrations/gravity-forms-field-mapping.php
index 3229f1df..ca140979 100755
--- a/sample-code-snippets/integrations/gravity-forms-field-mapping.php
+++ b/sample-code-snippets/integrations/gravity-forms-field-mapping.php
@@ -3,10 +3,10 @@
// The problem with Gravity forms in this regard is that it generates field names sort of randomly, such as name=”input_4″ so it’s hard for MC4WP to know what field is what.
// The only way to do it is if you manually map those field names (you can get them via “right click > inspect” when you view the form) to the correct MailChimp fields with some custom code.
-add_filter( 'mc4wp_integration_gravity-forms_subscriber_data', function( MC4WP_MailChimp_Subscriber $subscriber ) {
- $subscriber->merge_fields[ "FNAME" ] = sanitize_text_field( $_POST['input_2'] );
- $subscriber->merge_fields[ "COUNTRY" ] = sanitize_text_field( $_POST['input_4'] );
- return $subscriber;
+add_filter('mc4wp_integration_gravity-forms_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
+ $subscriber->merge_fields[ "FNAME" ] = sanitize_text_field($_POST['input_2']);
+ $subscriber->merge_fields[ "COUNTRY" ] = sanitize_text_field($_POST['input_4']);
+ return $subscriber;
});
diff --git a/sample-code-snippets/integrations/hide-checkboxes-for-logged-in-users.php b/sample-code-snippets/integrations/hide-checkboxes-for-logged-in-users.php
index 6c5b48b1..9e21dd7b 100755
--- a/sample-code-snippets/integrations/hide-checkboxes-for-logged-in-users.php
+++ b/sample-code-snippets/integrations/hide-checkboxes-for-logged-in-users.php
@@ -5,18 +5,19 @@
*
* The CSS will hide the sign-up checkboxes.
*/
-function myprefix_hide_checkbox_for_logged_in_users() {
- // get current user
- if( is_user_logged_in() ) {
- ?>
-
-
+
+ list_subscribe($mailchimp_list_id, $data['email'], array(
+ $mailchimp->list_subscribe($mailchimp_list_id, $data['email'], [
'status' => 'pending',
- 'merge_fields' => array(
+ 'merge_fields' => [
'FNAME' => $data['first_name'],
'LNAME' => $data['last_name'],
// 'MEMLEVEL' => $data['membership_level_name'],
- ),
+ ],
'tags' => $tags,
- ));
-});
\ No newline at end of file
+ ]);
+});
diff --git a/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php b/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php
index 618c52df..54716d65 100755
--- a/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php
+++ b/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php
@@ -1,10 +1,10 @@
tags[] = 'ES';
- } else if ($form_id == 510) {
+ } elseif ($form_id == 510) {
$subscriber->tags[] = 'NL';
}
return $subscriber;
-}, 10, 2);
\ No newline at end of file
+}, 10, 2);
diff --git a/sample-code-snippets/integrations/require-checkbox-to-be-checked.php b/sample-code-snippets/integrations/require-checkbox-to-be-checked.php
index cd9c289a..7d2e6b42 100755
--- a/sample-code-snippets/integrations/require-checkbox-to-be-checked.php
+++ b/sample-code-snippets/integrations/require-checkbox-to-be-checked.php
@@ -9,12 +9,13 @@
* @param MC4WP_Integration $integration
* @return array
*/
-add_filter( 'mc4wp_integration_checkbox_attributes', function( array $attributes, $integration ) {
- $attributes['required'] = 'required';
- return $attributes;
+
+add_filter('mc4wp_integration_checkbox_attributes', function (array $attributes, $integration) {
+ $attributes['required'] = 'required';
+ return $attributes;
}, 10, 2);
/**
* If you're running Contact Form 7, please also include the following line to enable HTML5 validation.
*/
-add_filter( 'wpcf7_form_novalidate', '__return_false' );
\ No newline at end of file
+add_filter('wpcf7_form_novalidate', '__return_false');
diff --git a/sample-code-snippets/integrations/restrict-content-pro.php b/sample-code-snippets/integrations/restrict-content-pro.php
index 0d269bdc..3f232af5 100755
--- a/sample-code-snippets/integrations/restrict-content-pro.php
+++ b/sample-code-snippets/integrations/restrict-content-pro.php
@@ -7,72 +7,76 @@
Author URI: http://dvk.co/
*/
-add_action( 'rcp_after_password_registration_field', 'rcp_mailchimp_add_profile_fields' );
-add_action( 'rcp_profile_editor_after', 'rcp_mailchimp_add_profile_fields' );
-add_action( 'rcp_form_processing', 'rcp_mailchimp_save_profile_fields', 10, 2 );
-add_action( 'rcp_user_profile_updated', 'rcp_mailchimp_save_profile_fields', 10 );
+add_action('rcp_after_password_registration_field', 'rcp_mailchimp_add_profile_fields');
+add_action('rcp_profile_editor_after', 'rcp_mailchimp_add_profile_fields');
+add_action('rcp_form_processing', 'rcp_mailchimp_save_profile_fields', 10, 2);
+add_action('rcp_user_profile_updated', 'rcp_mailchimp_save_profile_fields', 10);
-add_filter( 'mailchimp_sync_should_sync_user', 'rcp_mailchimp_should_subscribe', 10, 2 );
-add_filter( 'mailchimp_sync_subscriber_data', 'rcp_mailchimp_user_data', 10, 2 );
+add_filter('mailchimp_sync_should_sync_user', 'rcp_mailchimp_should_subscribe', 10, 2);
+add_filter('mailchimp_sync_subscriber_data', 'rcp_mailchimp_user_data', 10, 2);
-function rcp_mailchimp_add_profile_fields( $user_id = 0 ) {
- $selected = $user_id && get_user_meta( $user_id, 'rcp_mailchimp', true );
+function rcp_mailchimp_add_profile_fields($user_id = 0)
+{
+ $selected = $user_id && get_user_meta($user_id, 'rcp_mailchimp', true);
?>
-
+
ID, 'rcp_mailchimp', true );
+ $should = (bool) get_user_meta($user->ID, 'rcp_mailchimp', true);
return $should;
}
-function rcp_mailchimp_subscriber_data( MC4WP_MailChimp_Subscriber $subscriber, WP_User $user ) {
+function rcp_mailchimp_subscriber_data(MC4WP_MailChimp_Subscriber $subscriber, WP_User $user)
+{
// map of RCP subscription ID => MailChimp interest ID
- $map = array(
+ $map = [
'1' => "interest-id-1", // Trade Membership
'2' => "interest-id-2", // Artist Membership
'3' => "interest-id-3", // Associate Membership
'4' => "d8v1k2ilx", // Corporate Membership
'5' => "a0192m3az", // Supporter Membership
- );
+ ];
// get RCP subscription ID
- $subscription_id = rcp_get_subscription_id( $user->ID );
+ $subscription_id = rcp_get_subscription_id($user->ID);
// see if this maps to a known interest
- if( isset( $map[ $subscription_id ] ) ) {
+ if (isset($map[ $subscription_id ])) {
$interest_id = $map[ $subscription_id ];
$subscriber->interests[ $interest_id ] = true;
}
// return modified subscriber object
- return $subscriber;
+ return $subscriber;
}
diff --git a/sample-code-snippets/integrations/send-email.php b/sample-code-snippets/integrations/send-email.php
index 0b916055..94eb7964 100755
--- a/sample-code-snippets/integrations/send-email.php
+++ b/sample-code-snippets/integrations/send-email.php
@@ -3,13 +3,13 @@
/**
* This will send an email every time a MailChimp for WordPress integration is successfully used to subscribe.
*/
-add_action( 'mc4wp_integration_subscribed', function( $integration, $email_address, $merge_vars ) {
+
+add_action('mc4wp_integration_subscribed', function ($integration, $email_address, $merge_vars) {
// email variables
$email_to = 'email@email.com';
$email_subject = 'Someone subscribed through an integration';
- $email_message = sprintf( 'Integration %s used by %s', $integration->name, $email_address );
+ $email_message = sprintf('Integration %s used by %s', $integration->name, $email_address);
// send the email
- wp_mail( $email_to, $email_subject, $email_message );
-}, 10, 3 );
-
+ wp_mail($email_to, $email_subject, $email_message);
+}, 10, 3);
diff --git a/sample-code-snippets/integrations/send-name-field-from-wpforms.php b/sample-code-snippets/integrations/send-name-field-from-wpforms.php
index 2b4d4460..30369033 100755
--- a/sample-code-snippets/integrations/send-name-field-from-wpforms.php
+++ b/sample-code-snippets/integrations/send-name-field-from-wpforms.php
@@ -1,6 +1,6 @@
MailChimp.
+You can either return an Array with multiple list IDs or you can return a string with 1 list ID.
+You can get the list IDs from MailChimp for WP > MailChimp.
-This code will overwrite whatever lists you've set in the integration settings.
+This code will overwrite whatever lists you've set in the integration settings.
*/
-add_filter( 'mc4wp_integration_gravity-forms_lists', function() {
- return array("f2415574a4","bd0c7cefa9","a53c0bf8e5");
+add_filter('mc4wp_integration_gravity-forms_lists', function () {
+ return ["f2415574a4","bd0c7cefa9","a53c0bf8e5"];
});
diff --git a/sample-code-snippets/integrations/subscriber-tags.php b/sample-code-snippets/integrations/subscriber-tags.php
index 739845ee..6b8ede06 100755
--- a/sample-code-snippets/integrations/subscriber-tags.php
+++ b/sample-code-snippets/integrations/subscriber-tags.php
@@ -6,7 +6,8 @@
* The "mc4wp_integration_subscriber_data" filter only runs for integration requests.
* Use "mc4wp_subscriber_data" to hook into both form & integration requests.
*/
-add_filter( 'mc4wp_integration_subscriber_data', function(MC4WP_MailChimp_Subscriber $subscriber) {
- $subscriber->tags[] = 'My tag';
- return $subscriber;
+
+add_filter('mc4wp_integration_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
+ $subscriber->tags[] = 'My tag';
+ return $subscriber;
});
diff --git a/sample-code-snippets/integrations/woocommerce/add-interest-group-to-checkout.php b/sample-code-snippets/integrations/woocommerce/add-interest-group-to-checkout.php
index 37132dfc..05c5292b 100755
--- a/sample-code-snippets/integrations/woocommerce/add-interest-group-to-checkout.php
+++ b/sample-code-snippets/integrations/woocommerce/add-interest-group-to-checkout.php
@@ -1,34 +1,38 @@
+add_action('woocommerce_after_order_notes', 'mc4wp_show_interest_group_in_checkout');
+function mc4wp_show_interest_group_in_checkout()
+{
+ ?>
-
-
-
-
+
+
+
+
-
-
- Choose the topic of your interest
-
-
+
+
+ Choose the topic of your interest
+
+
-
-
-
-
-
+add_action('woocommerce_register_form_start', 'mc4wp_show_interest_group_in_registration');
+function mc4wp_show_interest_group_in_registration()
+{
+ ?>
-
-
- Choose the topic of your interest
-
-
+
+
+
+
-
+
+ Choose the topic of your interest
+
+
+
+ interests[ "interest-id" ] = true;
@@ -9,4 +9,4 @@
// $subscriber->interests[ "91lxm10xzl" ] = false;
return $subscriber;
-});
\ No newline at end of file
+});
diff --git a/sample-code-snippets/integrations/woocommerce/custom-checkbox-html.php b/sample-code-snippets/integrations/woocommerce/custom-checkbox-html.php
index b596a3b8..b451d8d4 100755
--- a/sample-code-snippets/integrations/woocommerce/custom-checkbox-html.php
+++ b/sample-code-snippets/integrations/woocommerce/custom-checkbox-html.php
@@ -1,14 +1,16 @@
';
-});
\ No newline at end of file
+});
diff --git a/sample-code-snippets/integrations/woocommerce/list-choice.php b/sample-code-snippets/integrations/woocommerce/list-choice.php
index cf968329..06d10ced 100755
--- a/sample-code-snippets/integrations/woocommerce/list-choice.php
+++ b/sample-code-snippets/integrations/woocommerce/list-choice.php
@@ -3,18 +3,19 @@
/**
* This snippet adds the HTML for a MailChimp list-choice to your WooCommerce checkout.
*/
-add_action( 'woocommerce_after_order_notes', function() {
- ?>
-
-
-
-
+add_action('woocommerce_after_order_notes', function () {
+ ?>
-
-
-
+
+
+
+
+
+
+ get_billing_country();
+add_filter('mc4wp_integration_woocommerce_data', function ($data, $order_id) {
+ $order = wc_get_order($order_id);
+
+ // this sends the billing_country field from WooCommerce to a Mailchimp field called "BILLING_COUNTRY"
+ $data[ 'BILLING_COUNTRY' ] = $order->get_billing_country();
+
+ // if it's a custom checkout field, usually you can get its value like this:
+ $data[ 'NAME_OF_FIELD_IN_MAILCHIMP' ] = $order->get_meta('name_of_field_in_woocommerce', true);
- // if it's a custom checkout field, usually you can get its value like this:
- $data[ 'NAME_OF_FIELD_IN_MAILCHIMP' ] = $order->get_meta( 'name_of_field_in_woocommerce', true );
-
- // this sends the billing_phone field from WooCommerce to a Mailchimp field called "PHONE".
- // I recommend using a text field, so that any format of phone number will pass validation in Mailchimp.
- $data[ 'PHONE' ] = $order->get_billing_phone();
+ // this sends the billing_phone field from WooCommerce to a Mailchimp field called "PHONE".
+ // I recommend using a text field, so that any format of phone number will pass validation in Mailchimp.
+ $data[ 'PHONE' ] = $order->get_billing_phone();
- return $data;
+ return $data;
}, 10, 2);
diff --git a/sample-code-snippets/integrations/woocommerce/send-birthday-field-from-checkout.php b/sample-code-snippets/integrations/woocommerce/send-birthday-field-from-checkout.php
index 0ec07dd9..61c470b3 100755
--- a/sample-code-snippets/integrations/woocommerce/send-birthday-field-from-checkout.php
+++ b/sample-code-snippets/integrations/woocommerce/send-birthday-field-from-checkout.php
@@ -2,11 +2,12 @@
/**
* The following snippet assumes a checkout field with name "billing_birthdate" and sends the value of that field to a MailChimp list field named MMERGE3
- *
+ *
* This assumes that the format of the billing_birthdate field is correct.
*/
-add_filter( 'mc4wp_integration_woocommerce_data', function( $data, $order_id ) {
- $order = wc_get_order( $order_id );
- $data['MMERGE3'] = $order->get_meta( 'billing_birthdate', true );
+
+add_filter('mc4wp_integration_woocommerce_data', function ($data, $order_id) {
+ $order = wc_get_order($order_id);
+ $data['MMERGE3'] = $order->get_meta('billing_birthdate', true);
return $data;
-}, 10, 2);
\ No newline at end of file
+}, 10, 2);
diff --git a/sample-code-snippets/integrations/woocommerce/send-coupon-code.php b/sample-code-snippets/integrations/woocommerce/send-coupon-code.php
index da395eee..64c99cb5 100755
--- a/sample-code-snippets/integrations/woocommerce/send-coupon-code.php
+++ b/sample-code-snippets/integrations/woocommerce/send-coupon-code.php
@@ -5,13 +5,14 @@
*
* @return array
*/
-add_filter( 'mc4wp_integration_woocommerce_data', function( $data, $order_id ) {
- $order = wc_get_order( $order_id );
- if( $order->get_used_coupons() ) {
- foreach( $order->get_used_coupons() as $coupon) {
- $custom_coupon_code_field = $coupon;
+
+add_filter('mc4wp_integration_woocommerce_data', function ($data, $order_id) {
+ $order = wc_get_order($order_id);
+ if ($order->get_used_coupons()) {
+ foreach ($order->get_used_coupons() as $coupon) {
+ $custom_coupon_code_field = $coupon;
}
$data[ 'MMERGE5' ] = $custom_coupon_code_field;
}
- return $data;
+ return $data;
}, 10, 2);
diff --git a/sample-code-snippets/integrations/woocommerce/send-utm-values-from-url.php b/sample-code-snippets/integrations/woocommerce/send-utm-values-from-url.php
index 717022c3..7decb0e8 100755
--- a/sample-code-snippets/integrations/woocommerce/send-utm-values-from-url.php
+++ b/sample-code-snippets/integrations/woocommerce/send-utm-values-from-url.php
@@ -1,32 +1,32 @@
mailchimp field name
- $map = array(
+ $map = [
'adid' => 'MMERGE9',
'marketer' => 'MMERGE7',
'publisher' => 'MMERGE8',
- );
+ ];
- foreach( $map as $cookie_name => $mailchimp_field_name ) {
- if( isset( $_COOKIE[ $cookie_name ] ) ) {
+ foreach ($map as $cookie_name => $mailchimp_field_name) {
+ if (isset($_COOKIE[ $cookie_name ])) {
$data[ $mailchimp_field_name ] = $_COOKIE[ $cookie_name ];
}
}
return $data;
-} );
\ No newline at end of file
+});
diff --git a/sample-code-snippets/integrations/woocommerce/set-interest-based-on-language.php b/sample-code-snippets/integrations/woocommerce/set-interest-based-on-language.php
index 5323759b..d1f3536a 100755
--- a/sample-code-snippets/integrations/woocommerce/set-interest-based-on-language.php
+++ b/sample-code-snippets/integrations/woocommerce/set-interest-based-on-language.php
@@ -1,18 +1,19 @@
interests[ "interest-id" ] = true;
- break;
- // english
- case 'en':
- // replace "interest-id" with the actual ID of your interest.
- $subscriber->interests[ "interest-id" ] = true;
- break;
- }
- }
- return $subscriber;
-});
+
+ add_filter('mc4wp_integration_woocommerce_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
+ if (defined('ICL_LANGUAGE_CODE')) {
+ switch (ICL_LANGUAGE_CODE) {
+ // spanish
+ case 'es':
+ // replace "interest-id" with the actual ID of your interest.
+ $subscriber->interests[ "interest-id" ] = true;
+ break;
+ // english
+ case 'en':
+ // replace "interest-id" with the actual ID of your interest.
+ $subscriber->interests[ "interest-id" ] = true;
+ break;
+ }
+ }
+ return $subscriber;
+ });
diff --git a/sample-code-snippets/integrations/woocommerce/set-interest-based-on-role.php b/sample-code-snippets/integrations/woocommerce/set-interest-based-on-role.php
index c47b024a..1df4102c 100755
--- a/sample-code-snippets/integrations/woocommerce/set-interest-based-on-role.php
+++ b/sample-code-snippets/integrations/woocommerce/set-interest-based-on-role.php
@@ -1,11 +1,12 @@
roles );
+ $user_role = array_shift($user->roles);
// toggle interest based on user role
- switch( $user_role ) {
+ switch ($user_role) {
case "customer":
$subscriber->interests[ "interest-id" ] = true;
break;
@@ -14,6 +15,6 @@
$subscriber->interests[ "other-interest-id" ] = true;
break;
}
-
+
return $subscriber;
-});
\ No newline at end of file
+});
diff --git a/sample-code-snippets/integrations/woocommerce/set-list-based-on-user-role.php b/sample-code-snippets/integrations/woocommerce/set-list-based-on-user-role.php
index 3c618a34..f87ab4c9 100755
--- a/sample-code-snippets/integrations/woocommerce/set-list-based-on-user-role.php
+++ b/sample-code-snippets/integrations/woocommerce/set-list-based-on-user-role.php
@@ -1,26 +1,26 @@
mailchimp list ID's
- $map = array(
+ $map = [
'subscriber' => 'list-id-1',
'customer' => 'list-id-2',
'editor' => 'list-id-3',
- );
+ ];
// get user role
- $user_role = array_shift( $user->roles );
+ $user_role = array_shift($user->roles);
// use custom list if set for this role
- if( isset( $map[ $user_role ] ) ) {
+ if (isset($map[ $user_role ])) {
return $map[ $user_role ];
}
return $lists;
-});
\ No newline at end of file
+});
diff --git a/sample-code-snippets/integrations/woocommerce/woocommerce-subscribe-my-account.php b/sample-code-snippets/integrations/woocommerce/woocommerce-subscribe-my-account.php
index b340dbb1..aea581ad 100755
--- a/sample-code-snippets/integrations/woocommerce/woocommerce-subscribe-my-account.php
+++ b/sample-code-snippets/integrations/woocommerce/woocommerce-subscribe-my-account.php
@@ -1,37 +1,38 @@
-newsletter ) ) {
- $mailchimp = new MC4WP_MailChimp();
- $subscribed = $mailchimp->list_has_subscriber( $mailchimp_list_id, $user->billing_email );
- update_user_meta( $user->ID, 'newsletter', $subscribed ? 1 : 0 );
- }
-
-
- echo '';
+add_action('woocommerce_edit_account_form', function () use ($mailchimp_list_id) {
+ $user = wp_get_current_user();
+
+ if (empty($user->newsletter)) {
+ $mailchimp = new MC4WP_MailChimp();
+ $subscribed = $mailchimp->list_has_subscriber($mailchimp_list_id, $user->billing_email);
+ update_user_meta($user->ID, 'newsletter', $subscribed ? 1 : 0);
+ }
+
+
+ echo '';
});
-add_action( 'woocommerce_save_account_details', function($user_id) use ( $mailchimp_list_id ) {
- $mailchimp = new MC4WP_MailChimp();
- $user = get_userdata( $user_id );
- $update_existing = true;
- $subscribe_to_newsletter = ! empty( $_POST['subscribe_to_newsletter'] ) ? 1 : 0;
- update_user_meta( $user_id, 'newsletter', $subscribe_to_newsletter );
-
- if( $subscribe_to_newsletter ) {
- $mailchimp->list_subscribe( $mailchimp_list_id, $user->billing_email, array(
- 'merge_fields' => array(
- // merge fields go here
- )
- ), $update_existing );
- } else {
- $mailchimp->list_unsubscribe( $mailchimp_list_id, $user->billing_email );
- }
+add_action('woocommerce_save_account_details', function ($user_id) use ($mailchimp_list_id) {
+ $mailchimp = new MC4WP_MailChimp();
+ $user = get_userdata($user_id);
+ $update_existing = true;
+ $subscribe_to_newsletter = ! empty($_POST['subscribe_to_newsletter']) ? 1 : 0;
+ update_user_meta($user_id, 'newsletter', $subscribe_to_newsletter);
+
+ if ($subscribe_to_newsletter) {
+ $mailchimp->list_subscribe($mailchimp_list_id, $user->billing_email, [
+ 'merge_fields' => [
+ // merge fields go here
+ ]
+ ], $update_existing);
+ } else {
+ $mailchimp->list_unsubscribe($mailchimp_list_id, $user->billing_email);
+ }
});
diff --git a/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php b/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php
index 9bea5cd3..c927aa31 100755
--- a/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php
+++ b/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php
@@ -4,7 +4,8 @@
* This adds the "My tag" tag to all new subscribers added using WooCommerce Checkout integration.
*
*/
-add_filter( 'mc4wp_integration_woocommerce_subscriber_data', function(MC4WP_MailChimp_Subscriber $subscriber) {
- $subscriber->tags[] = 'My tag';
- return $subscriber;
+
+add_filter('mc4wp_integration_woocommerce_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
+ $subscriber->tags[] = 'My tag';
+ return $subscriber;
});
diff --git a/sample-code-snippets/integrations/wp-comment-form/output-after-submit-button.php b/sample-code-snippets/integrations/wp-comment-form/output-after-submit-button.php
index e4dc71cf..db5f0c11 100755
--- a/sample-code-snippets/integrations/wp-comment-form/output-after-submit-button.php
+++ b/sample-code-snippets/integrations/wp-comment-form/output-after-submit-button.php
@@ -7,18 +7,20 @@
*/
// only run once templates are loaded
-add_action( 'template_redirect', function() {
+add_action('template_redirect', function () {
- // make sure mc4wp is activated
- if( ! function_exists( 'mc4wp' ) ) { return; }
+ // make sure mc4wp is activated
+ if (! function_exists('mc4wp')) {
+ return;
+ }
- // get integration manager
- $integrations = mc4wp('integrations');
- if( ! $integrations instanceof MC4WP_Integration_Manager ) {
- return;
- }
+ // get integration manager
+ $integrations = mc4wp('integrations');
+ if (! $integrations instanceof MC4WP_Integration_Manager) {
+ return;
+ }
- // get comment form integration
- $comment_form_integration = $integrations->get('wp-comment-form');
- remove_filter( 'comment_form_submit_field', array( $comment_form_integration, 'add_checkbox_before_submit_button' ), 90 );
-} );
\ No newline at end of file
+ // get comment form integration
+ $comment_form_integration = $integrations->get('wp-comment-form');
+ remove_filter('comment_form_submit_field', [ $comment_form_integration, 'add_checkbox_before_submit_button' ], 90);
+});
diff --git a/sample-code-snippets/integrations/wp-forms-field-mapping.php b/sample-code-snippets/integrations/wp-forms-field-mapping.php
index dc7d1da4..8976ba25 100755
--- a/sample-code-snippets/integrations/wp-forms-field-mapping.php
+++ b/sample-code-snippets/integrations/wp-forms-field-mapping.php
@@ -1,9 +1,10 @@
merge_fields[ "FNAME" ] = sanitize_text_field( $_POST['wpforms[fields][7][first]'] );
- $subscriber->merge_fields[ "COUNTRY" ] = sanitize_text_field( $_POST['wpforms[fields][7][country]'] );
- return $subscriber;
+add_filter('mc4wp_integration_wpforms_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
+ $subscriber->merge_fields[ "FNAME" ] = sanitize_text_field($_POST['wpforms[fields][7][first]']);
+ $subscriber->merge_fields[ "COUNTRY" ] = sanitize_text_field($_POST['wpforms[fields][7][country]']);
+ return $subscriber;
});
diff --git a/sample-code-snippets/integrations/wp-registration-form/add-to-grouping.php b/sample-code-snippets/integrations/wp-registration-form/add-to-grouping.php
index 54575948..8aa44fde 100755
--- a/sample-code-snippets/integrations/wp-registration-form/add-to-grouping.php
+++ b/sample-code-snippets/integrations/wp-registration-form/add-to-grouping.php
@@ -1,10 +1,10 @@
interests[ "interest-id" ] = true;
- // repeat for all interests you want to enable or disable
- // $subscriber->interests[ "91lxm10xzl" ] = true;
- // $subscriber->interests[ "91lxm10xzl" ] = false;
- return $subscriber;
+add_filter('mc4wp_integration_wp-registration-form_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
+ // replace "interest-id" with the actual ID of your interest.
+ $subscriber->interests[ "interest-id" ] = true;
+ // repeat for all interests you want to enable or disable
+ // $subscriber->interests[ "91lxm10xzl" ] = true;
+ // $subscriber->interests[ "91lxm10xzl" ] = false;
+ return $subscriber;
});
diff --git a/sample-code-snippets/integrations/wp-registration-form/custom-registration-fields.php b/sample-code-snippets/integrations/wp-registration-form/custom-registration-fields.php
index e3fce11d..a19e8f97 100755
--- a/sample-code-snippets/integrations/wp-registration-form/custom-registration-fields.php
+++ b/sample-code-snippets/integrations/wp-registration-form/custom-registration-fields.php
@@ -3,39 +3,40 @@
/**
* Add fields to the WordPress registration form.
*/
-add_action( 'register_form', function() {
- ?>
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+ tags[] = 'My tag';
- return $subscriber;
+add_filter('mc4wp_integration_wp-registration-form_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
+ $subscriber->tags[] = 'My tag';
+ return $subscriber;
});
diff --git a/sample-code-snippets/integrations/wp-registration-form/unset-fname.php b/sample-code-snippets/integrations/wp-registration-form/unset-fname.php
index bf877246..cca7ee47 100755
--- a/sample-code-snippets/integrations/wp-registration-form/unset-fname.php
+++ b/sample-code-snippets/integrations/wp-registration-form/unset-fname.php
@@ -4,7 +4,8 @@
* Unset the "FNAME" field that is sent via WP Registration integration.
*
*/
-add_filter( 'mc4wp_integration_wp-registration-form_data', function( $data ) {
- unset($data['FNAME']);
- return $data;
+
+add_filter('mc4wp_integration_wp-registration-form_data', function ($data) {
+ unset($data['FNAME']);
+ return $data;
});
diff --git a/sample-code-snippets/integrations/wp-registration-form/user-sync-opt-in-status-from-registration-form.php b/sample-code-snippets/integrations/wp-registration-form/user-sync-opt-in-status-from-registration-form.php
index c695fd01..5380d7eb 100755
--- a/sample-code-snippets/integrations/wp-registration-form/user-sync-opt-in-status-from-registration-form.php
+++ b/sample-code-snippets/integrations/wp-registration-form/user-sync-opt-in-status-from-registration-form.php
@@ -3,13 +3,14 @@
/**
* The snippet below will set the correct usermeta value to ensure the Registration Form integration sets the opt-in status for MailChimp User Sync too.
*/
-add_action( 'user_register', function( $user_id ) {
+
+add_action('user_register', function ($user_id) {
// do nothing if checkbox was not checked
- if( empty( $_POST['_mc4wp_subscribe_wp-registration-form'] ) ) {
- return;
- }
+ if (empty($_POST['_mc4wp_subscribe_wp-registration-form'])) {
+ return;
+ }
- $list_id = '6d93b47d25'; // change this to your MailChimp list ID
- $meta_key = sprintf( 'mailchimp_sync_%s_opted_in', $list_id );
- update_user_meta( $user_id, $meta_key, '1' );
+ $list_id = '6d93b47d25'; // change this to your MailChimp list ID
+ $meta_key = sprintf('mailchimp_sync_%s_opted_in', $list_id);
+ update_user_meta($user_id, $meta_key, '1');
});
diff --git a/sample-code-snippets/manually-add-email-address-to-mailchimp.php b/sample-code-snippets/manually-add-email-address-to-mailchimp.php
index c58bd26f..54e5d0fb 100755
--- a/sample-code-snippets/manually-add-email-address-to-mailchimp.php
+++ b/sample-code-snippets/manually-add-email-address-to-mailchimp.php
@@ -8,18 +8,18 @@
$mailchimp_list_id = 'your-list-id-here'; // the mailchimp list to subscribe to
$use_double_optin = true; // whether to use double opt-in or not
$email_address = 'johndoe@email.com';
-$merge_fields = array(
- 'FNAME' => 'John',
-);
+$merge_fields = [
+ 'FNAME' => 'John',
+];
try {
- $subscriber = $api->add_list_member( $mailchimp_list_id, array(
- 'email_address' => $email_address,
- 'status' => $use_double_optin ? 'pending' : 'subscribed',
- 'merge_fields' => $merge_fields,
- ));
-} catch( \MC4WP_API_Exception $e ) {
- // an error occured
- // you can handle it here by inspecting the expection object and removing the line below
- throw $e;
+ $subscriber = $api->add_list_member($mailchimp_list_id, [
+ 'email_address' => $email_address,
+ 'status' => $use_double_optin ? 'pending' : 'subscribed',
+ 'merge_fields' => $merge_fields,
+ ]);
+} catch (\MC4WP_API_Exception $e) {
+ // an error occured
+ // you can handle it here by inspecting the expection object and removing the line below
+ throw $e;
}
diff --git a/sample-code-snippets/misc/ajax-endpoint.php b/sample-code-snippets/misc/ajax-endpoint.php
index 1dd0c843..797a7b75 100755
--- a/sample-code-snippets/misc/ajax-endpoint.php
+++ b/sample-code-snippets/misc/ajax-endpoint.php
@@ -6,7 +6,7 @@
* Point your AJAX request at: /wp-admin/admin-ajax.php?action=my_mailchimp_subscribe
*/
-add_action( 'wp_ajax_my_mailchimp_subscribe', function() {
+add_action('wp_ajax_my_mailchimp_subscribe', function () {
// get API class instance
$api = mc4wp('api');
@@ -18,18 +18,18 @@
$first_name = $_POST['first_name'];
try {
- $subscriber = $api->add_list_member( $list_id, array(
+ $subscriber = $api->add_list_member($list_id, [
'email_address' => $email_address,
'status' => $double_optin ? 'pending' : 'subscribed',
- 'merge_fields' => array(
+ 'merge_fields' => [
'FNAME' => $first_name,
- )
- ));
- } catch( MC4WP_API_Exception $e ) {
+ ]
+ ]);
+ } catch (MC4WP_API_Exception $e) {
// an error occured
- wp_send_json_error( $e->getMessage(), $e->getCode() );
+ wp_send_json_error($e->getMessage(), $e->getCode());
}
// successfully subscribed
wp_send_json_success();
-});
\ No newline at end of file
+});
diff --git a/sample-code-snippets/misc/custom-subscriber-count.php b/sample-code-snippets/misc/custom-subscriber-count.php
index 3477a80e..2e7eed95 100755
--- a/sample-code-snippets/misc/custom-subscriber-count.php
+++ b/sample-code-snippets/misc/custom-subscriber-count.php
@@ -1,16 +1,16 @@
get_list($list_id, array('fields' => 'stats'));
- $count += $data->stats->member_count + $data->stats->unsubscribe_count;
- }
- set_transient('guillaume_subscriber_count', $count, 60);
- }
- return (int) $count;
+add_filter('mc4wp_subscriber_count', function ($count, $list_ids) {
+ $count = get_transient('guillaume_subscriber_count');
+
+ if ($count === false) {
+ $api = mc4wp_get_api_v3();
+ $count = 0;
+ foreach ($list_ids as $list_id) {
+ $data = $api->get_list($list_id, ['fields' => 'stats']);
+ $count += $data->stats->member_count + $data->stats->unsubscribe_count;
+ }
+ set_transient('guillaume_subscriber_count', $count, 60);
+ }
+ return (int) $count;
}, 10, 2);
diff --git a/sample-code-snippets/misc/defaults-options.php b/sample-code-snippets/misc/defaults-options.php
index a3dccc3d..e9c83130 100755
--- a/sample-code-snippets/misc/defaults-options.php
+++ b/sample-code-snippets/misc/defaults-options.php
@@ -9,7 +9,8 @@
* - css: Should the CSS stylesheet be loaded (boolean)
* - label: The text for the checkbox label (string)
*/
-add_filter( 'default_option_mc4wp_checkbox', function( $options ) {
- $options['css'] = 0; //Set value of "Load some default CSS?"" to false
- return $options;
-});
\ No newline at end of file
+
+add_filter('default_option_mc4wp_checkbox', function ($options) {
+ $options['css'] = 0; //Set value of "Load some default CSS?"" to false
+ return $options;
+});
diff --git a/sample-code-snippets/misc/disable-auto-scroll.php b/sample-code-snippets/misc/disable-auto-scroll.php
index 994e07dd..7d943f3b 100755
--- a/sample-code-snippets/misc/disable-auto-scroll.php
+++ b/sample-code-snippets/misc/disable-auto-scroll.php
@@ -3,4 +3,5 @@
/**
* This snippet will disable auto scrolling of page after form submission.
*/
-add_filter( 'mc4wp_form_auto_scroll', '__return_false' );
\ No newline at end of file
+
+add_filter('mc4wp_form_auto_scroll', '__return_false');
diff --git a/sample-code-snippets/misc/disable-log-debugging.php b/sample-code-snippets/misc/disable-log-debugging.php
index e538b3e2..202cf95f 100755
--- a/sample-code-snippets/misc/disable-log-debugging.php
+++ b/sample-code-snippets/misc/disable-log-debugging.php
@@ -1,4 +1,6 @@
service( 'download_repository' )->retrieve_single( absint( $_POST['mc4wp_dlm_download_id'] ) );
- $url = $download->get_the_download_link();
- } catch ( Exception $exception ) {
- // no download with given ID found
- }
-
- }
-
- return $url;
+function mc4wp_dlm_dynamic_url($url, $form)
+{
+
+ if (isset($_POST['mc4wp_dlm_download_id']) && ! empty($_POST['mc4wp_dlm_download_id'])) {
+ try {
+ /** @var DLM_Download $download */
+ $download = download_monitor()->service('download_repository')->retrieve_single(absint($_POST['mc4wp_dlm_download_id']));
+ $url = $download->get_the_download_link();
+ } catch (Exception $exception) {
+ // no download with given ID found
+ }
+ }
+
+ return $url;
}
-add_filter( 'mc4wp_form_redirect_url', 'mc4wp_dlm_dynamic_url', 20, 2 );
+add_filter('mc4wp_form_redirect_url', 'mc4wp_dlm_dynamic_url', 20, 2);
// add hidden field to the access page on the "No Access" page.
-function mc4wp_dlm_add_download_id( $content ) {
- global $wp;
+function mc4wp_dlm_add_download_id($content)
+{
+ global $wp;
- if ( isset( $wp->query_vars ) && isset( $wp->query_vars['download-id'] ) ) {
- $content .= "" . PHP_EOL;
- }
+ if (isset($wp->query_vars) && isset($wp->query_vars['download-id'])) {
+ $content .= "" . PHP_EOL;
+ }
- return $content;
+ return $content;
}
-add_filter( 'mc4wp_form_content', 'mc4wp_dlm_add_download_id' );
\ No newline at end of file
+add_filter('mc4wp_form_content', 'mc4wp_dlm_add_download_id');
diff --git a/sample-code-snippets/misc/enable-log-debugging.php b/sample-code-snippets/misc/enable-log-debugging.php
index be104bde..3fc0d295 100755
--- a/sample-code-snippets/misc/enable-log-debugging.php
+++ b/sample-code-snippets/misc/enable-log-debugging.php
@@ -1,4 +1,5 @@
get_client();
$workflow_id = 'abcdef';
$workflow_email_id = 'ghijklm';
-$url = sprintf( '/automations/%s/emails/%s/queue', $workflow_id, $workflow_email_id );
-$data = array(
+$url = sprintf('/automations/%s/emails/%s/queue', $workflow_id, $workflow_email_id);
+$data = [
'email_address' => 'johndoe@email.com',
-);
-$response = $client->post( $url, $data );
+];
+$response = $client->post($url, $data);
diff --git a/sample-code-snippets/misc/set-list-based-on-wpml-language.php b/sample-code-snippets/misc/set-list-based-on-wpml-language.php
index 8e5129aa..502f4af8 100755
--- a/sample-code-snippets/misc/set-list-based-on-wpml-language.php
+++ b/sample-code-snippets/misc/set-list-based-on-wpml-language.php
@@ -8,24 +8,24 @@
* @param array $lists
* @return array $lists
*/
-function myprefix_filter_mc4wp_lists( $lists ) {
- $list_id_spanish_list = '123abcdef456';
- $list_id_english_list = '456defabc123';
+function myprefix_filter_mc4wp_lists($lists)
+{
+ $list_id_spanish_list = '123abcdef456';
+ $list_id_english_list = '456defabc123';
- if( defined( 'ICL_LANGUAGE_CODE') ) {
- switch( ICL_LANGUAGE_CODE ) {
-
- // spanish
- case 'es':
- $lists = array( $list_id_spanish_list );
- break;
- // english
- case 'en':
- $lists = array( $list_id_english_list );
- break;
- }
- }
- return $lists;
+ if (defined('ICL_LANGUAGE_CODE')) {
+ switch (ICL_LANGUAGE_CODE) {
+ // spanish
+ case 'es':
+ $lists = [ $list_id_spanish_list ];
+ break;
+ // english
+ case 'en':
+ $lists = [ $list_id_english_list ];
+ break;
+ }
+ }
+ return $lists;
}
-add_filter( 'mc4wp_lists', 'myprefix_filter_mc4wp_lists' );
\ No newline at end of file
+add_filter('mc4wp_lists', 'myprefix_filter_mc4wp_lists');
diff --git a/sample-code-snippets/misc/set-subscriber-language-using-browser-language.php b/sample-code-snippets/misc/set-subscriber-language-using-browser-language.php
index 2c3c4d18..35b62dee 100755
--- a/sample-code-snippets/misc/set-subscriber-language-using-browser-language.php
+++ b/sample-code-snippets/misc/set-subscriber-language-using-browser-language.php
@@ -1,4 +1,5 @@
language = substr( $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2 );
+ $subscriber->language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
return $subscriber;
-});
\ No newline at end of file
+});
diff --git a/sample-code-snippets/misc/set-subscriber-language-using-wpml-site-language.php b/sample-code-snippets/misc/set-subscriber-language-using-wpml-site-language.php
index 98e48fc6..2f9b6f93 100755
--- a/sample-code-snippets/misc/set-subscriber-language-using-wpml-site-language.php
+++ b/sample-code-snippets/misc/set-subscriber-language-using-wpml-site-language.php
@@ -1,4 +1,5 @@
language = substr( ICL_LANGUAGE_CODE, 0, 2 );
+ $subscriber->language = substr(ICL_LANGUAGE_CODE, 0, 2);
return $subscriber;
-});
\ No newline at end of file
+});
diff --git a/sample-code-snippets/misc/store-subscriber-status-in-user-meta-for-each-user.php b/sample-code-snippets/misc/store-subscriber-status-in-user-meta-for-each-user.php
index d6fabcf2..027b7894 100755
--- a/sample-code-snippets/misc/store-subscriber-status-in-user-meta-for-each-user.php
+++ b/sample-code-snippets/misc/store-subscriber-status-in-user-meta-for-each-user.php
@@ -1,4 +1,5 @@
- $offset, 'number' => $limit ) );
- if( ! is_array( $users ) || empty( $users ) ) {
- return;
- }
-
- foreach( $users as $user ) {
-
- // don't process same user more than once
- $meta_value = get_user_meta( $user->ID, 'mailchimp_opted_in', true );
- if( strlen( $meta_value ) > 0 ) {
- continue;
- }
-
- // sleep for 0.2 seconds
- usleep(2000);
-
- try{
- // make remote API call
- $subscriber = $api->get_list_member( $mailchimp_list_id, $user->user_email );
- $opted_in = ( $subscriber && in_array( $subscriber->status, array( 'pending', 'subscribed' ) ) );
- } catch( MC4WP_API_Resource_Not_Found_Exception $e ) {
- $opted_in = false;
- } catch( Exception $e ) {
- die($e);
- }
-
- // set meta value
- update_user_meta( $user->ID, 'mailchimp_opted_in', $opted_in ? "1" : "0" );
- }
-
- // redirect to same page but with higher offset to process next batch
- wp_redirect( add_query_arg( array( 'offset' => $offset + $limit ) ) );
- exit;
- });
+if (is_admin() && isset($_GET['mc4wp-fetch-user-subscriber-status'])) {
+ add_action('admin_init', function () {
+ $offset = isset($_GET['offset']) ? (int) $_GET['offset'] : 0;
+ $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 20; // TODO: Change this
+ $mailchimp_list_id = isset($_GET['mailchimp_list_id']) ? $_GET['mailchimp_list_id'] : "bc502db480";
+
+ $api = mc4wp_get_api_v3();
+
+ $users = get_users([ 'offset' => $offset, 'number' => $limit ]);
+ if (! is_array($users) || empty($users)) {
+ return;
+ }
+
+ foreach ($users as $user) {
+ // don't process same user more than once
+ $meta_value = get_user_meta($user->ID, 'mailchimp_opted_in', true);
+ if (strlen($meta_value) > 0) {
+ continue;
+ }
+
+ // sleep for 0.2 seconds
+ usleep(2000);
+
+ try {
+ // make remote API call
+ $subscriber = $api->get_list_member($mailchimp_list_id, $user->user_email);
+ $opted_in = ( $subscriber && in_array($subscriber->status, [ 'pending', 'subscribed' ]) );
+ } catch (MC4WP_API_Resource_Not_Found_Exception $e) {
+ $opted_in = false;
+ } catch (Exception $e) {
+ die($e);
+ }
+
+ // set meta value
+ update_user_meta($user->ID, 'mailchimp_opted_in', $opted_in ? "1" : "0");
+ }
+
+ // redirect to same page but with higher offset to process next batch
+ wp_redirect(add_query_arg([ 'offset' => $offset + $limit ]));
+ exit;
+ });
}
-
diff --git a/sample-code-snippets/misc/use-wp-proxy-for-api-requests.php b/sample-code-snippets/misc/use-wp-proxy-for-api-requests.php
index c7f9aa8e..830aca4d 100755
--- a/sample-code-snippets/misc/use-wp-proxy-for-api-requests.php
+++ b/sample-code-snippets/misc/use-wp-proxy-for-api-requests.php
@@ -3,11 +3,11 @@
define('WP_PROXY_HOST', 'http://proxy-url-here.com/');
define('WP_PROXY_PORT', '80');
-add_filter('pre_http_send_through_proxy', function($use, $url) {
- if (strpos($url,'api.mailchimp.com/') !== false) {
+add_filter('pre_http_send_through_proxy', function ($use, $url) {
+ if (strpos($url, 'api.mailchimp.com/') !== false) {
return true;
}
// alternatively, return false here to only use proxy for conditions above
return $use;
-}, 10, 2);
\ No newline at end of file
+}, 10, 2);
diff --git a/sample-code-snippets/modify-data-before-sending-to-mailchimp.php b/sample-code-snippets/modify-data-before-sending-to-mailchimp.php
index 1cce0d44..32afde45 100755
--- a/sample-code-snippets/modify-data-before-sending-to-mailchimp.php
+++ b/sample-code-snippets/modify-data-before-sending-to-mailchimp.php
@@ -6,7 +6,8 @@
* @param MC4WP_MailChimp_Subscriber $subscriber
* @return MC4WP_MailChimp_Subscriber
*/
-function myprefix_subscriber_data( MC4WP_MailChimp_Subscriber $subscriber ) {
+function myprefix_subscriber_data(MC4WP_MailChimp_Subscriber $subscriber)
+{
// add merge field
$subscriber->merge_fields['FIELD'] = 'Value';
@@ -21,13 +22,13 @@ function myprefix_subscriber_data( MC4WP_MailChimp_Subscriber $subscriber ) {
}
// all sign-up methods
-add_filter( 'mc4wp_subscriber_data', 'myprefix_subscriber_data' );
+add_filter('mc4wp_subscriber_data', 'myprefix_subscriber_data');
// forms only
-add_filter( 'mc4wp_form_subscriber_data', 'myprefix_subscriber_data' );
+add_filter('mc4wp_form_subscriber_data', 'myprefix_subscriber_data');
// integrations only
-add_filter( 'mc4wp_integration_subscriber_data', 'myprefix_subscriber_data' );
+add_filter('mc4wp_integration_subscriber_data', 'myprefix_subscriber_data');
// specific integration: woocommerce
-add_filter( 'mc4wp_integration_woocommerce_subscriber_data', 'myprefix_subscriber_data' );
\ No newline at end of file
+add_filter('mc4wp_integration_woocommerce_subscriber_data', 'myprefix_subscriber_data');
diff --git a/sample-code-snippets/premium/disable-functionality.php b/sample-code-snippets/premium/disable-functionality.php
index 79d0304e..659f3ccd 100755
--- a/sample-code-snippets/premium/disable-functionality.php
+++ b/sample-code-snippets/premium/disable-functionality.php
@@ -1,4 +1,5 @@
Reports)
*/
-define( 'MC4WP_LOGGING', false );
+
+define('MC4WP_LOGGING', false);
diff --git a/sample-code-snippets/premium/ecommerce/custom-repopulate-cart.php b/sample-code-snippets/premium/ecommerce/custom-repopulate-cart.php
index 449ba8d0..b4e37e5b 100755
--- a/sample-code-snippets/premium/ecommerce/custom-repopulate-cart.php
+++ b/sample-code-snippets/premium/ecommerce/custom-repopulate-cart.php
@@ -1,18 +1,18 @@
cart;
// empty cart
$wc_cart->empty_cart();
// add items from MailChimp cart object
- foreach( $cart_data->lines as $line ) {
+ foreach ($cart_data->lines as $line) {
$variation_id = $line->product_variant_id != $line->product_id ? $line->product_variant_id : 0;
- $wc_cart->add_to_cart( $line->product_id, $line->quantity, $variation_id );
+ $wc_cart->add_to_cart($line->product_id, $line->quantity, $variation_id);
}
// redirect to cart page
wp_redirect(wc_get_cart_url());
exit;
-});
\ No newline at end of file
+});
diff --git a/sample-code-snippets/premium/ecommerce/disable-object-tracking.php b/sample-code-snippets/premium/ecommerce/disable-object-tracking.php
index 1797e682..ec87cc21 100755
--- a/sample-code-snippets/premium/ecommerce/disable-object-tracking.php
+++ b/sample-code-snippets/premium/ecommerce/disable-object-tracking.php
@@ -1,10 +1,11 @@
E-Commerce settings page.
*/
-add_filter( 'mc4wp_ecommerce_options', function($opts) {
- $opts['enable_object_tracking'] = false;
- return $opts;
+
+add_filter('mc4wp_ecommerce_options', function ($opts) {
+ $opts['enable_object_tracking'] = false;
+ return $opts;
});
diff --git a/sample-code-snippets/premium/ecommerce/filter-order-data.php b/sample-code-snippets/premium/ecommerce/filter-order-data.php
index 8f9ee5e3..9cf05f12 100755
--- a/sample-code-snippets/premium/ecommerce/filter-order-data.php
+++ b/sample-code-snippets/premium/ecommerce/filter-order-data.php
@@ -1,20 +1,19 @@
$line ) {
-
+ foreach ($data['lines'] as $index => $line) {
// strip order lines with product 501
- if( $line['product_id'] == 501 ) {
- unset( $data['lines'][$index] );
+ if ($line['product_id'] == 501) {
+ unset($data['lines'][$index]);
}
}
// reset array because MailChimp API expects an array type.
- $data['lines'] = array_values( $data['lines'] );
+ $data['lines'] = array_values($data['lines']);
- return $data;
-}, 10, 2);
\ No newline at end of file
+ return $data;
+}, 10, 2);
diff --git a/sample-code-snippets/premium/ecommerce/hide-products-from-recommendations.php b/sample-code-snippets/premium/ecommerce/hide-products-from-recommendations.php
index 46e49519..7f1545ec 100755
--- a/sample-code-snippets/premium/ecommerce/hide-products-from-recommendations.php
+++ b/sample-code-snippets/premium/ecommerce/hide-products-from-recommendations.php
@@ -1,21 +1,21 @@
$variant_data ) {
- $product_data['variants'][$key]['inventory_quantity'] = 0;
- }
+ foreach ($product_data['variants'] as $key => $variant_data) {
+ $product_data['variants'][$key]['inventory_quantity'] = 0;
+ }
- return $product_data;
+ return $product_data;
});
diff --git a/sample-code-snippets/premium/ecommerce/one-category-per-product.php b/sample-code-snippets/premium/ecommerce/one-category-per-product.php
index e8f4d33c..ff6f16a7 100755
--- a/sample-code-snippets/premium/ecommerce/one-category-per-product.php
+++ b/sample-code-snippets/premium/ecommerce/one-category-per-product.php
@@ -1,37 +1,38 @@
- 0) {
- $data['vendor'] = reset($single_category);
- } else {
- $data['vendor'] = $categories[0];
- }
-
- return $data;
-}, 10, 2);
+add_filter('mc4wp_ecommerce_product_data', function ($data, $product) {
+ $priority_categories = [
+ 'Test1',
+ 'Test2',
+ 'Test3'
+ ];
+
+ if (empty($data['vendor'])) {
+ return $data;
+ }
+
+ $single_category = [];
+ $categories = explode('|', $data['vendor']);
+ $single_category = array_intersect($categories, $priority_categories);
+
+
+ if (count($single_category) > 0) {
+ $data['vendor'] = reset($single_category);
+ } else {
+ $data['vendor'] = $categories[0];
+ }
+
+ return $data;
+}, 10, 2);
diff --git a/sample-code-snippets/premium/ecommerce/only-completed-orders.php b/sample-code-snippets/premium/ecommerce/only-completed-orders.php
index da546f29..4e8711f2 100755
--- a/sample-code-snippets/premium/ecommerce/only-completed-orders.php
+++ b/sample-code-snippets/premium/ecommerce/only-completed-orders.php
@@ -5,6 +5,7 @@
*
* Here, we are returning a new array with only the "wc-completed" status in it so that only orders with that status are sent to MailChimp.
*/
-add_filter( 'mc4wp_ecommerce_order_statuses', function( $statuses ) {
- return array( 'wc-completed' );
+
+add_filter('mc4wp_ecommerce_order_statuses', function ($statuses) {
+ return [ 'wc-completed' ];
});
diff --git a/sample-code-snippets/premium/ecommerce/only-if-email-already-on-list.php b/sample-code-snippets/premium/ecommerce/only-if-email-already-on-list.php
index 6ec5cbd1..e411c7c5 100755
--- a/sample-code-snippets/premium/ecommerce/only-if-email-already-on-list.php
+++ b/sample-code-snippets/premium/ecommerce/only-if-email-already-on-list.php
@@ -1,4 +1,5 @@
get_list_member($mailchimp_list_id, $email);
- } catch (Exception $e) {
- //In case of an exception return false
- return false;
- }
+ try {
+ //Send the request to get the status
+ $list_member = mc4wp_get_api_v3()->get_list_member($mailchimp_list_id, $email);
+ } catch (Exception $e) {
+ //In case of an exception return false
+ return false;
+ }
//Compare the status
- return in_array($list_member->status, array('subscribed', 'pending'), true);
+ return in_array($list_member->status, ['subscribed', 'pending'], true);
}
/**
@@ -52,7 +54,8 @@ function mc4wp_mailchimp_list_member_has_status_pending_or_subscribed($email) {
*
* @return string the email address of the customer
*/
-function mc4wp_get_customer_email($customer) {
+function mc4wp_get_customer_email($customer)
+{
switch (get_class($customer)) {
case stdClass::class:
if (! isset($customer->billing_email)) {
@@ -83,7 +86,7 @@ function mc4wp_get_customer_email($customer) {
add_filter('mc4wp_ecommerce_send_cart_to_mailchimp', function ($value, $customer) {
$customer_email = mc4wp_get_customer_email($customer);
- return mc4wp_mailchimp_list_member_has_status_pending_or_subscribed($customer_email);
+ return mc4wp_mailchimp_list_member_has_status_pending_or_subscribed($customer_email);
}, 10, 2);
/**
@@ -96,7 +99,7 @@ function mc4wp_get_customer_email($customer) {
add_filter('mc4wp_ecommerce_send_customer_to_mailchimp', function ($value, $customer) {
$customer_email = mc4wp_get_customer_email($customer);
- return mc4wp_mailchimp_list_member_has_status_pending_or_subscribed($customer_email);
+ return mc4wp_mailchimp_list_member_has_status_pending_or_subscribed($customer_email);
}, 10, 2);
/**
@@ -111,5 +114,5 @@ function mc4wp_get_customer_email($customer) {
return false;
}
- return mc4wp_mailchimp_list_member_has_status_pending_or_subscribed($order->get_billing_email());
+ return mc4wp_mailchimp_list_member_has_status_pending_or_subscribed($order->get_billing_email());
}, 10, 2);
diff --git a/sample-code-snippets/premium/ecommerce/order-financial-status.php b/sample-code-snippets/premium/ecommerce/order-financial-status.php
index 6db81f56..847285db 100755
--- a/sample-code-snippets/premium/ecommerce/order-financial-status.php
+++ b/sample-code-snippets/premium/ecommerce/order-financial-status.php
@@ -8,27 +8,27 @@
// - refunded: sends refund confirmation
//
// fullfilment_status:
-// - shipped: sends shipping confirmation
+// - shipped: sends shipping confirmation
-add_filter( 'mc4wp_ecommerce_order_data', function( $data, $order ) {
- switch( $order->get_status() ) {
- case "pending":
- $data['financial_status'] = 'pending';
- break;
+add_filter('mc4wp_ecommerce_order_data', function ($data, $order) {
+ switch ($order->get_status()) {
+ case "pending":
+ $data['financial_status'] = 'pending';
+ break;
- case "on-hold":
- $data['financial_status'] = 'pending';
- break;
+ case "on-hold":
+ $data['financial_status'] = 'pending';
+ break;
- case "processing":
- $data['financial_status'] = 'pending';
- break;
+ case "processing":
+ $data['financial_status'] = 'pending';
+ break;
- case "completed":
- $data['financial_status'] = 'paid';
- $data['fulfillment_status'] = 'shipped';
- break;
- }
+ case "completed":
+ $data['financial_status'] = 'paid';
+ $data['fulfillment_status'] = 'shipped';
+ break;
+ }
- return $data;
+ return $data;
}, 10, 2);
diff --git a/sample-code-snippets/premium/ecommerce/skip-old-orders.php b/sample-code-snippets/premium/ecommerce/skip-old-orders.php
index 12de1098..cec73fc2 100755
--- a/sample-code-snippets/premium/ecommerce/skip-old-orders.php
+++ b/sample-code-snippets/premium/ecommerce/skip-old-orders.php
@@ -1,8 +1,8 @@
get_date_completed() );
+add_filter('mc4wp_ecommerce_send_order_to_mailchimp', function ($send, WC_Order $order) {
+ $time_one_month_ago = strtotime('-1 month');
+ $time_order_completed = strtotime($order->get_date_completed());
$send = ( $time_order_completed > $time_one_month_ago );
return $send;
}, 10, 2);
diff --git a/sample-code-snippets/premium/ecommerce/subscribe-guest-customers.php b/sample-code-snippets/premium/ecommerce/subscribe-guest-customers.php
index 3b92e8fc..5a87e08d 100755
--- a/sample-code-snippets/premium/ecommerce/subscribe-guest-customers.php
+++ b/sample-code-snippets/premium/ecommerce/subscribe-guest-customers.php
@@ -1,105 +1,106 @@
- * : The MailChimp list ID to subscribe guest customers to
- *
- * ## EXAMPLES
- *
- * wp mc4wp-ecommerce-subscribe-guest-customers run
- *
- * @subcommand run
- */
- public function add_order( $args, $assoc_args = array() ) {
- if ( empty( $assoc_args['list_id'] ) ) {
- WP_CLI::error( "Please provide a list_id argument." );
- return;
- }
-
- $query = new WC_Order_Query( array(
- 'type' => array( 'shop_order' ),
- 'customer' => 0,
- 'limit' => isset( $assoc_args['limit'] ) ? $assoc_args['limit'] : '-1',
- 'status' => 'completed',
- 'orderby' => isset( $assoc_args['orderby'] ) ? $assoc_args['orderby'] : 'date',
- 'order' => isset( $assoc_args['order'] ) ? $assoc_args['order'] : 'ASC',
- 'page' => isset( $assoc_args['page'] ) ? $assoc_args['page'] : '1',
- 'offset' => isset( $assoc_args['offset'] ) ? $assoc_args['offset'] : null,
- 'return' => 'ids',
- ) );
-
- $guest_orders = $query->get_orders();
-
- $total_guest_orders = count( $guest_orders );
- WP_CLI::log( sprintf( '%d guest orders found.', $total_guest_orders ) );
+if (defined('WP_CLI') && WP_CLI) {
+ if (class_exists('WP_CLI') && class_exists('WP_CLI_Command')) {
+ class MC4WP_Ecommerce_Subscribe_Guest_Customers_Command extends WP_CLI_Command
+ {
+ /**
+ * Subscribes email address found in guest orders to a MailChimp list
+ *
+ * @param $args
+ * @param $assoc_args
+ *
+ * ## OPTIONS
+ *
+ *
+ * : The MailChimp list ID to subscribe guest customers to
+ *
+ * ## EXAMPLES
+ *
+ * wp mc4wp-ecommerce-subscribe-guest-customers run
+ *
+ * @subcommand run
+ */
+ public function add_order($args, $assoc_args = [])
+ {
+ if (empty($assoc_args['list_id'])) {
+ WP_CLI::error("Please provide a list_id argument.");
+ return;
+ }
+
+ $query = new WC_Order_Query([
+ 'type' => [ 'shop_order' ],
+ 'customer' => 0,
+ 'limit' => isset($assoc_args['limit']) ? $assoc_args['limit'] : '-1',
+ 'status' => 'completed',
+ 'orderby' => isset($assoc_args['orderby']) ? $assoc_args['orderby'] : 'date',
+ 'order' => isset($assoc_args['order']) ? $assoc_args['order'] : 'ASC',
+ 'page' => isset($assoc_args['page']) ? $assoc_args['page'] : '1',
+ 'offset' => isset($assoc_args['offset']) ? $assoc_args['offset'] : null,
+ 'return' => 'ids',
+ ]);
+
+ $guest_orders = $query->get_orders();
+
+ $total_guest_orders = count($guest_orders);
+ WP_CLI::log(sprintf('%d guest orders found.', $total_guest_orders));
+
+ $mailchimp = new MC4WP_MailChimp();
+ $mailchimp_list_id = $assoc_args['list_id'];
+ $args = [
+ 'status' => 'pending', // default: "pending", set to "subscribed" to skip double opt-in (not recommended)
+ ];
+
+ $progress = \WP_CLI\Utils\make_progress_bar('Progress Bar', $total_guest_orders);
+
+ foreach ($guest_orders as $order_id) {
+ try {
+ $order = wc_get_order($order_id);
+ $email_address = $order->get_billing_email();
+
+ if (empty($email_address)) {
+ $this->get_log()->info(sprintf('WP CLI Guest Orders: Skipping guest order #%d because it has no billing_email property.', $order->get_order_number()));
+ continue;
+ }
+
+ // query MailChimp to see if this guest is subscribed
+ if ($mailchimp->list_has_subscriber($mailchimp_list_id, $email_address)) {
+ $this->get_log()->info(sprintf('WP CLI Guest Orders: %s is already subscribed.', $email_address));
+ $progress->tick();
+
+ usleep(300); // 300ms delay to prevent being rate-limited by MailChimp
+ continue;
+ }
+
+ // if not already on the list, subscribe this guest customer as a new subscriber
+ $args['merge_fields'] = [
+ 'FNAME' => $order->get_billing_first_name(),
+ 'LNAME' => $order->get_billing_last_name(),
+ ];
+ $mailchimp->list_subscribe($mailchimp_list_id, $email_address, $args);
+ $this->get_log()->info(sprintf('WP CLI Guest Orders: Subscribed %s', $email_address));
+
+ // increment progress bar
+ $progress->tick();
+ } catch (Exception $e) {
+ $this->get_log()->info(sprintf('WP CLI Guest Orders: Exception: %s Order ID: %s', $e->getMessage()));
+ }
+ }
+
+ $progress->finish();
+
+ WP_CLI::success('Done.');
+ }
- $mailchimp = new MC4WP_MailChimp();
- $mailchimp_list_id = $assoc_args['list_id'];
- $args = array(
- 'status' => 'pending', // default: "pending", set to "subscribed" to skip double opt-in (not recommended)
- );
-
- $progress = \WP_CLI\Utils\make_progress_bar( 'Progress Bar', $total_guest_orders );
-
- foreach ( $guest_orders as $order_id ) {
-
- try {
- $order = wc_get_order( $order_id );
- $email_address = $order->get_billing_email();
-
- if ( empty( $email_address ) ) {
- $this->get_log()->info( sprintf( 'WP CLI Guest Orders: Skipping guest order #%d because it has no billing_email property.', $order->get_order_number() ) );
- continue;
- }
-
- // query MailChimp to see if this guest is subscribed
- if ( $mailchimp->list_has_subscriber( $mailchimp_list_id, $email_address ) ) {
- $this->get_log()->info( sprintf( 'WP CLI Guest Orders: %s is already subscribed.', $email_address ) );
- $progress->tick();
-
- usleep(300); // 300ms delay to prevent being rate-limited by MailChimp
- continue;
- }
-
- // if not already on the list, subscribe this guest customer as a new subscriber
- $args['merge_fields'] = array(
- 'FNAME' => $order->get_billing_first_name(),
- 'LNAME' => $order->get_billing_last_name(),
- );
- $mailchimp->list_subscribe( $mailchimp_list_id, $email_address, $args );
- $this->get_log()->info( sprintf( 'WP CLI Guest Orders: Subscribed %s', $email_address ) );
-
- // increment progress bar
- $progress->tick();
- } catch ( Exception $e ) {
- $this->get_log()->info( sprintf( 'WP CLI Guest Orders: Exception: %s Order ID: %s', $e->getMessage() ) );
- }
-
- }
-
- $progress->finish();
-
- WP_CLI::success( 'Done.' );
- }
-
- /**
- * @return MC4WP_Debug_Log
- */
- private function get_log() {
- return mc4wp( 'log' );
- }
-
- }
- }
- WP_CLI::add_command( 'mc4wp-ecommerce-subscribe-guest-customers', 'MC4WP_Ecommerce_Subscribe_Guest_Customers_Command' );
+ /**
+ * @return MC4WP_Debug_Log
+ */
+ private function get_log()
+ {
+ return mc4wp('log');
+ }
+ }
+ }
+ WP_CLI::add_command('mc4wp-ecommerce-subscribe-guest-customers', 'MC4WP_Ecommerce_Subscribe_Guest_Customers_Command');
}
diff --git a/sample-code-snippets/premium/ecommerce/sync-only-parent-orders.php b/sample-code-snippets/premium/ecommerce/sync-only-parent-orders.php
index 5eef4380..591081cc 100755
--- a/sample-code-snippets/premium/ecommerce/sync-only-parent-orders.php
+++ b/sample-code-snippets/premium/ecommerce/sync-only-parent-orders.php
@@ -1,10 +1,11 @@
-get_parent_id() > 0) {
- return false;
- }
- return $send;
+// Some setups that work with Parent and Sub orders such as WCMp will result in both the parent and sub order being synced ot Mailchimp, resulting in every order being synced twice.
+// This code snippet will make sure that only the parent order issynced and all sub orders are ignored during sync.
+
+add_filter('mc4wp_ecommerce_send_order_to_mailchimp', function ($send, WC_Order $order) {
+ if ($order->get_parent_id() > 0) {
+ return false;
+ }
+ return $send;
}, 10, 2);
diff --git a/sample-code-snippets/premium/email-notification-from-email.php b/sample-code-snippets/premium/email-notification-from-email.php
index fe97deaf..953c11b4 100755
--- a/sample-code-snippets/premium/email-notification-from-email.php
+++ b/sample-code-snippets/premium/email-notification-from-email.php
@@ -5,9 +5,10 @@
*
* This example just sets a static name & email address
*/
-add_filter( 'mc4wp_form_email_notification_headers', function( $headers ) {
- $headers[] = 'From: John Doe ';
- return $headers;
+
+add_filter('mc4wp_form_email_notification_headers', function ($headers) {
+ $headers[] = 'From: John Doe ';
+ return $headers;
});
@@ -19,8 +20,8 @@
*
* This example uses the values from the "FNAME" and "EMAIL" field
*/
-add_filter( 'mc4wp_form_email_notification_headers', function( $headers, MC4WP_Form $form ) {
+add_filter('mc4wp_form_email_notification_headers', function ($headers, MC4WP_Form $form) {
$data = $form->get_data();
- $headers[] = sprintf( '%s: %s <%s>', 'From', $data['FNAME'], $data['EMAIL'] );
- return $headers;
-}, 10, 2 );
\ No newline at end of file
+ $headers[] = sprintf('%s: %s <%s>', 'From', $data['FNAME'], $data['EMAIL']);
+ return $headers;
+}, 10, 2);
diff --git a/sample-code-snippets/premium/email-notification-reply-to.php b/sample-code-snippets/premium/email-notification-reply-to.php
index 28fecde0..6a7c0d04 100755
--- a/sample-code-snippets/premium/email-notification-reply-to.php
+++ b/sample-code-snippets/premium/email-notification-reply-to.php
@@ -3,8 +3,9 @@
/**
* This will set the header of the email notification to the person filling in the form.
*/
-add_filter( 'mc4wp_form_email_notification_headers', function( array $headers, MC4WP_Form $form ) {
+
+add_filter('mc4wp_form_email_notification_headers', function (array $headers, MC4WP_Form $form) {
$data = $form->get_data();
- $headers[] = sprintf( '%s: %s', 'Reply-To', $data['EMAIL'] );
+ $headers[] = sprintf('%s: %s', 'Reply-To', $data['EMAIL']);
return $headers;
-}, 10, 2 );
+}, 10, 2);
diff --git a/sample-code-snippets/premium/overwrite_domain_suggestions.php b/sample-code-snippets/premium/overwrite_domain_suggestions.php
index 8c36f2f2..3eb3984a 100755
--- a/sample-code-snippets/premium/overwrite_domain_suggestions.php
+++ b/sample-code-snippets/premium/overwrite_domain_suggestions.php
@@ -1,33 +1,34 @@
'buddypress_field_name',
'user_id' => $user->ID
- )
+ ]
);
// add to merge fields, change "mailchimp_field_name" to the name of your merge field in MailChimp.
$subscriber->merge_fields[ 'mailchimp_field_name' ] = $field_value;
return $subscriber;
-}, 10, 2 );
\ No newline at end of file
+}, 10, 2);
diff --git a/sample-code-snippets/premium/user-sync/change-user-role-on-subscribe-or-unsubscribe.php b/sample-code-snippets/premium/user-sync/change-user-role-on-subscribe-or-unsubscribe.php
index 3057849f..4d7b159c 100755
--- a/sample-code-snippets/premium/user-sync/change-user-role-on-subscribe-or-unsubscribe.php
+++ b/sample-code-snippets/premium/user-sync/change-user-role-on-subscribe-or-unsubscribe.php
@@ -1,17 +1,18 @@
Usersync
-// It will change the user role when someone subscribes on unsubscribes from the Audience.
-// Replace 'unsubscribed' and 'subscriber' with the slugs of the roles you are using for this.
+// It will change the user role when someone subscribes on unsubscribes from the Audience.
+// Replace 'unsubscribed' and 'subscriber' with the slugs of the roles you are using for this.
// If you want this to apply when someone subscribes through a single opt-in MC4WP form, you need to turn on API trigger in the webhook
-// under Audience > Settings > Webhooks, edit the webhook and check the box for "via the API".
+// under Audience > Settings > Webhooks, edit the webhook and check the box for "via the API".
-add_action( 'mc4wp_user_sync_webhook_unsubscribe', function($data, $user) {
-$user->remove_role( 'subscriber' );
-$user->add_role( 'unsubscribed' );
+add_action('mc4wp_user_sync_webhook_unsubscribe', function ($data, $user) {
+ $user->remove_role('subscriber');
+ $user->add_role('unsubscribed');
}, 10, 2);
-add_action( 'mc4wp_user_sync_webhook_subscribe', function($data, $user) {
-$user->remove_role( 'unsubscribed' );
-$user->add_role( 'subscriber' );
+add_action('mc4wp_user_sync_webhook_subscribe', function ($data, $user) {
+ $user->remove_role('unsubscribed');
+ $user->add_role('subscriber');
}, 10, 2);
diff --git a/sample-code-snippets/premium/user-sync/custom-field-map-setting.php b/sample-code-snippets/premium/user-sync/custom-field-map-setting.php
index e659d3df..f1928133 100755
--- a/sample-code-snippets/premium/user-sync/custom-field-map-setting.php
+++ b/sample-code-snippets/premium/user-sync/custom-field-map-setting.php
@@ -1,11 +1,12 @@
'INTERESTS',
- 'user_field' => '_meta_key_for_storing_interest_field',
- );
- return $settings;
+add_filter('mc4wp_user_sync_settings', function ($settings) {
+ $settings['field_map'][] = [
+ 'mailchimp_field' => 'INTERESTS',
+ 'user_field' => '_meta_key_for_storing_interest_field',
+ ];
+ return $settings;
});
diff --git a/sample-code-snippets/premium/user-sync/dont-overwrite-blank-values.php b/sample-code-snippets/premium/user-sync/dont-overwrite-blank-values.php
index c3956aef..9a1e96dc 100755
--- a/sample-code-snippets/premium/user-sync/dont-overwrite-blank-values.php
+++ b/sample-code-snippets/premium/user-sync/dont-overwrite-blank-values.php
@@ -1,12 +1,13 @@
merge_fields = array_filter($subscriber->merge_fields, function($value) {
- return $value !== '';
- });
+add_filter('mc4wp_user_sync_subscriber_data', function ($subscriber) {
+ $subscriber->merge_fields = array_filter($subscriber->merge_fields, function ($value) {
+ return $value !== '';
+ });
});
diff --git a/sample-code-snippets/premium/user-sync/groupings-based-on-role.php b/sample-code-snippets/premium/user-sync/groupings-based-on-role.php
index 775a9246..d47d5a0f 100755
--- a/sample-code-snippets/premium/user-sync/groupings-based-on-role.php
+++ b/sample-code-snippets/premium/user-sync/groupings-based-on-role.php
@@ -3,36 +3,37 @@
/**
* Add or remove users with the role "role_1" to the interest group with the ID interest-group-id and "role_2" to interest id "other-interest-group-id".
*/
-add_filter( 'mc4wp_user_sync_subscriber_data', function( \MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user ) {
- $subscriber->interests[ 'interest-group-id' ] = in_array( 'role_1', $user->roles );
- $subscriber->interests[ 'other-interest-group-id' ] = in_array( 'role_2', $user->roles );
- //you can repeat this line for more groups / roles.
+
+add_filter('mc4wp_user_sync_subscriber_data', function (\MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user) {
+ $subscriber->interests[ 'interest-group-id' ] = in_array('role_1', $user->roles);
+ $subscriber->interests[ 'other-interest-group-id' ] = in_array('role_2', $user->roles);
+ //you can repeat this line for more groups / roles.
return $subscriber;
-}, 14, 2 );
+}, 14, 2);
/**
* This is another way to do the same thing, for the role "subscriber".
*/
-add_filter( 'mc4wp_user_sync_subscriber_data', function( \MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user ) {
+add_filter('mc4wp_user_sync_subscriber_data', function (\MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user) {
// toggle interest ID based on user role
- if( in_array( 'subscriber', $user->roles ) ) {
+ if (in_array('subscriber', $user->roles)) {
$subscriber->interests[ "interest-id-members" ] = true;
} else {
$subscriber->interests[ "interest-id-members" ] = false;
}
- return $subscriber;
-}, 10, 2 );
+ return $subscriber;
+}, 10, 2);
/**
- * A simple example how to include your own function in a Wordpress theme for setting Interest group.
+ * A simple example how to include your own function in a Wordpress theme for setting Interest group.
* The code below is not needed for the code above to work.
*/
-add_filter( 'mc4wp_user_sync_subscriber_data', function( \MC4WP_MailChimp_Subscriber $subscriber ) {
+add_filter('mc4wp_user_sync_subscriber_data', function (\MC4WP_MailChimp_Subscriber $subscriber) {
// do nothing if user is logged in
- if( is_user_logged_in() ) {
+ if (is_user_logged_in()) {
return $subscriber;
}
@@ -40,4 +41,4 @@
// you can find this ID by going to MailChimp for WP > MailChimp > List Overview
$subscriber->interests[ "interest-id" ] = true;
return $subscriber;
-} );
+});
diff --git a/sample-code-snippets/premium/user-sync/include-multiple-user-roles.php b/sample-code-snippets/premium/user-sync/include-multiple-user-roles.php
index fde18430..eb7f4ee6 100755
--- a/sample-code-snippets/premium/user-sync/include-multiple-user-roles.php
+++ b/sample-code-snippets/premium/user-sync/include-multiple-user-roles.php
@@ -3,19 +3,20 @@
/**
* This snippet allows you to define multiple roles which should be synchronized with Mailchimp.
*/
-add_filter( 'mc4wp_user_sync_should_sync_user', function($sync, WP_User $user) {
- $roles_to_sync = array(
- 'editor',
- 'customer'
- );
- // if user has any of the above roles, return true
- $intersect = array_intersect($user->roles, $roles_to_sync);
- if (count($intersect) > 0) {
- return true;
- }
+add_filter('mc4wp_user_sync_should_sync_user', function ($sync, WP_User $user) {
+ $roles_to_sync = [
+ 'editor',
+ 'customer'
+ ];
- // otherwise, return given value from plugin's own logic (eg the settings page)
- // return false here to explicitly disallow other roles from being sent to Mailchimp
- return $sync;
+ // if user has any of the above roles, return true
+ $intersect = array_intersect($user->roles, $roles_to_sync);
+ if (count($intersect) > 0) {
+ return true;
+ }
+
+ // otherwise, return given value from plugin's own logic (eg the settings page)
+ // return false here to explicitly disallow other roles from being sent to Mailchimp
+ return $sync;
}, 10, 2);
diff --git a/sample-code-snippets/premium/user-sync/interests.php b/sample-code-snippets/premium/user-sync/interests.php
index e92cf32a..c61dbcf4 100755
--- a/sample-code-snippets/premium/user-sync/interests.php
+++ b/sample-code-snippets/premium/user-sync/interests.php
@@ -8,8 +8,9 @@
*
* @return array Our modified data array
*/
-add_filter( 'mc4wp_user_sync_subscriber_data', function( \MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user ) {
+
+add_filter('mc4wp_user_sync_subscriber_data', function (\MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user) {
$subscriber->interests[ "interest-id-1" ] = true;
$subscriber->interests[ "interest-id-2" ] = true;
return $subscriber;
-}, 10, 2 );
\ No newline at end of file
+}, 10, 2);
diff --git a/sample-code-snippets/premium/user-sync/optimizemember-custom-fields.php b/sample-code-snippets/premium/user-sync/optimizemember-custom-fields.php
index 62b67f43..dd8039fe 100755
--- a/sample-code-snippets/premium/user-sync/optimizemember-custom-fields.php
+++ b/sample-code-snippets/premium/user-sync/optimizemember-custom-fields.php
@@ -3,15 +3,16 @@
/**
* This code adds support for custom fields added by OptimizeMember.
*/
-add_filter( 'mc4wp_user_sync_get_user_field', function( $value, $field, $user ) {
- $custom_fields = get_user_option( 'optimizemember_custom_fields', $user->ID );
+add_filter('mc4wp_user_sync_get_user_field', function ($value, $field, $user) {
- if( $custom_fields ) {
- if( isset( $custom_fields[ $field ] ) ) {
- return $custom_fields[ $field ];
- }
- }
+ $custom_fields = get_user_option('optimizemember_custom_fields', $user->ID);
- return $value;
-}, 10, 3 );
\ No newline at end of file
+ if ($custom_fields) {
+ if (isset($custom_fields[ $field ])) {
+ return $custom_fields[ $field ];
+ }
+ }
+
+ return $value;
+}, 10, 3);
diff --git a/sample-code-snippets/premium/user-sync/send-acf-fields.php b/sample-code-snippets/premium/user-sync/send-acf-fields.php
index 9f9fac85..2a48f58e 100755
--- a/sample-code-snippets/premium/user-sync/send-acf-fields.php
+++ b/sample-code-snippets/premium/user-sync/send-acf-fields.php
@@ -5,11 +5,12 @@
*
* It will then take an ACF field "bar" and send it to the MailChimp "FOO" field.
*/
-add_filter( 'mc4wp_user_sync_subscriber_data', function( \MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user ) {
+
+add_filter('mc4wp_user_sync_subscriber_data', function (\MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user) {
// MailChimp field name: FOO
// ACF field name: bar
- $subscriber->merge_fields['FOO'] = get_field( 'bar', 'user_' . $user->ID );
+ $subscriber->merge_fields['FOO'] = get_field('bar', 'user_' . $user->ID);
return $subscriber;
-}, 10, 2 );
+}, 10, 2);
diff --git a/sample-code-snippets/premium/user-sync/send-user-roles-as-comma-separated-string.php b/sample-code-snippets/premium/user-sync/send-user-roles-as-comma-separated-string.php
index 6a16aa4d..9901c674 100755
--- a/sample-code-snippets/premium/user-sync/send-user-roles-as-comma-separated-string.php
+++ b/sample-code-snippets/premium/user-sync/send-user-roles-as-comma-separated-string.php
@@ -1,7 +1,7 @@
merge_fields[ 'ROLES' ] = join( ',', $user->roles );
+ $subscriber->merge_fields[ 'ROLES' ] = join(',', $user->roles);
return $subscriber;
-}, 10, 2 );
+}, 10, 2);
diff --git a/sample-code-snippets/premium/user-sync/webhook-update-user-meta-field.php b/sample-code-snippets/premium/user-sync/webhook-update-user-meta-field.php
index f6580ffb..a9e55d0d 100755
--- a/sample-code-snippets/premium/user-sync/webhook-update-user-meta-field.php
+++ b/sample-code-snippets/premium/user-sync/webhook-update-user-meta-field.php
@@ -1,12 +1,13 @@
ID, 'user_meta_key', $data['merges']['INTERESTS']);
+add_action('mc4wp_user_sync_webhook', function ($data, $user) {
+ /* $data['merges'] contains an associative array with mailchimp field values */
+ /* in this example we take the INTERESTS fields, which is a comma-separated string of interest groups */
+ update_user_meta($user->ID, 'user_meta_key', $data['merges']['INTERESTS']);
}, 10, 2);
diff --git a/sample-code-snippets/premium/user-sync/woocommerce-subscriptions-field.php b/sample-code-snippets/premium/user-sync/woocommerce-subscriptions-field.php
index 2b086ee3..14359382 100755
--- a/sample-code-snippets/premium/user-sync/woocommerce-subscriptions-field.php
+++ b/sample-code-snippets/premium/user-sync/woocommerce-subscriptions-field.php
@@ -1,14 +1,14 @@
ID ) ) {
- $subscriber->merge_fields[ "ACTIVE" ] = 'Yes';
- } else {
- $subscriber->merge_fields[ "ACTIVE" ] = 'No';
- }
+ /** @var MC4WP_MailChimp_Subscriber $subscriber */
+ if (WC_Subscriptions_Manager::user_has_subscription($user->ID)) {
+ $subscriber->merge_fields[ "ACTIVE" ] = 'Yes';
+ } else {
+ $subscriber->merge_fields[ "ACTIVE" ] = 'No';
+ }
- return $subscriber;
-}, 10, 2 );
+ return $subscriber;
+}, 10, 2);
diff --git a/sample-code-snippets/premium/user-sync/woocommerce-subscriptions-interest-group.php b/sample-code-snippets/premium/user-sync/woocommerce-subscriptions-interest-group.php
index ada958cc..8823e4ab 100755
--- a/sample-code-snippets/premium/user-sync/woocommerce-subscriptions-interest-group.php
+++ b/sample-code-snippets/premium/user-sync/woocommerce-subscriptions-interest-group.php
@@ -1,12 +1,13 @@
ID, '100', 'active' ) ) {
+add_filter('mc4wp_user_sync_subscriber_data', function (\MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user) {
+ if (\WC_Subscriptions_Manager::user_has_subscription($user->ID, '100', 'active')) {
$subscriber->interests[ "interest-id-1" ] = true;
} else {
$subscriber->interests[ "interest-id-1" ] = false;
}
return $subscriber;
-}, 10, 2 );
+}, 10, 2);
diff --git a/sample-code-snippets/premium/user-sync/woocommerce-sync-address-field.php b/sample-code-snippets/premium/user-sync/woocommerce-sync-address-field.php
index 567bc5fe..d5d9b7d9 100755
--- a/sample-code-snippets/premium/user-sync/woocommerce-sync-address-field.php
+++ b/sample-code-snippets/premium/user-sync/woocommerce-sync-address-field.php
@@ -3,14 +3,14 @@
// The snippet below instructs User Sync to include the various WooCommerce billing address fields
// And send them to a field named "ADDRESS" in Mailchimp
-add_filter( 'mc4wp_user_sync_subscriber_data', function( \MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user ) {
+add_filter('mc4wp_user_sync_subscriber_data', function (\MC4WP_MailChimp_Subscriber $subscriber, \WP_User $user) {
// change ADDRESS to the name of your Mailchimp field with type "address"
- $subscriber->merge_fields['ADDRESS'] = array(
+ $subscriber->merge_fields['ADDRESS'] = [
'addr1' => $user->billing_address_1,
'city' => $user->billing_city,
'state' => $user->billing_state,
'zip' => $user->billing_postcode,
'country' => $user->billing_country,
- );
+ ];
return $subscriber;
-}, 10, 2 );
+}, 10, 2);
diff --git a/uninstall.php b/uninstall.php
index 9e0538b4..df4970ed 100644
--- a/uninstall.php
+++ b/uninstall.php
@@ -1,7 +1,9 @@