Skip to content

Commit c68ffa5

Browse files
authored
Merge pull request #565 from WebDevStudios/feature/CC-194-additional-multiselect
Feature/cc 194 additional multiselect
2 parents 235ef0a + fb4a517 commit c68ffa5

File tree

1 file changed

+68
-31
lines changed

1 file changed

+68
-31
lines changed

includes/class-settings.php

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ protected function register_fields_general() {
404404
$cmb->add_field( [
405405
'name' => esc_html__( 'Add subscribers to', 'constant-contact-forms' ),
406406
'id' => '_ctct_optin_list',
407-
'type' => 'select',
407+
'type' => 'multicheck',
408408
'show_option_none' => false,
409409
'default' => esc_html__( 'Select a list', 'constant-contact-forms' ),
410410
'options' => $lists,
@@ -692,14 +692,19 @@ public function optin_form_field() {
692692
}
693693

694694
$saved_label = constant_contact_get_option( '_ctct_optin_label', '' );
695-
$list = constant_contact_get_option( '_ctct_optin_list', '' );
695+
696+
$lists = $this->get_optin_list_options();
696697
$label = $saved_label ?: esc_html__( 'Sign up to our newsletter.', 'constant-contact-forms' );
698+
697699
?>
698700
<p class="ctct-optin-wrapper" style="padding: 0 0 1em 0;">
699-
<label for="ctct_optin">
700-
<input type="checkbox" value="<?php echo esc_attr( $list ); ?>" class="checkbox" id="ctct_optin" name="ctct_optin_list" />
701-
<?php echo esc_attr( $label ); ?>
702-
</label>
701+
<p><?php echo esc_attr( $label ); ?></p>
702+
<?php foreach ( $lists as $key => $list ) { ?>
703+
<label for="ctct_optin_<?php echo esc_attr( $key ); ?>">
704+
<input type="checkbox" value="<?php echo esc_attr( $key ); ?>" class="checkbox" id="ctct_optin_<?php echo esc_attr( $key ); ?>" name="ctct_optin_list[]" /> <?php echo esc_attr( $list ); ?>
705+
</label>
706+
<br/>
707+
<?php } ?>
703708
<?php echo wp_kses_post( constant_contact()->display->get_disclose_text() ); ?>
704709
</p>
705710
<?php
@@ -715,9 +720,9 @@ public function optin_form_field() {
715720
* @return array Comment form data.
716721
*/
717722
public function process_optin_comment_form( $comment_data ) {
718-
$ctct_optin_list = filter_input( INPUT_POST, 'ctct_optin_list', FILTER_SANITIZE_STRING );
723+
$ctct_optin_lists = filter_input( INPUT_POST, 'ctct_optin_list', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
719724

720-
if ( empty( $ctct_optin_list ) ) {
725+
if ( empty( $ctct_optin_lists ) ) {
721726
return $comment_data;
722727
}
723728

@@ -738,21 +743,23 @@ public function process_comment_data_for_optin( $comment_data ) {
738743

739744
$name = isset( $comment_data['comment_author'] ) ? $comment_data['comment_author'] : '';
740745
$website = isset( $comment_data['comment_author_url'] ) ? $comment_data['comment_author_url'] : '';
741-
$list = filter_input( INPUT_POST, 'ctct_optin_list', FILTER_SANITIZE_STRING );
746+
$lists = filter_input( INPUT_POST, 'ctct_optin_list', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
742747

743-
if ( empty( $list ) ) {
748+
if ( empty( $lists ) ) {
744749
return $comment_data;
745750
}
746751

747-
$args = [
748-
'list' => sanitize_text_field( wp_unslash( $list ) ),
749-
'email' => sanitize_email( $comment_data['comment_author_email'] ),
750-
'first_name' => sanitize_text_field( $name ),
751-
'last_name' => '',
752-
'website' => sanitize_text_field( $website ),
753-
];
752+
foreach ( $lists as $list ) {
753+
$args = [
754+
'list' => sanitize_text_field( wp_unslash( $list ) ),
755+
'email' => sanitize_email( $comment_data['comment_author_email'] ),
756+
'first_name' => sanitize_text_field( $name ),
757+
'last_name' => '',
758+
'website' => sanitize_text_field( $website ),
759+
];
760+
constantcontact_api()->add_contact( $args );
761+
}
754762

755-
constantcontact_api()->add_contact( $args );
756763
}
757764

758765
return $comment_data;
@@ -769,9 +776,9 @@ public function process_comment_data_for_optin( $comment_data ) {
769776
* @return object|array CTCT return API for contact or original $user array.
770777
*/
771778
public function process_optin_login_form( $user, $username, $password ) {
772-
$ctct_optin_list = filter_input( INPUT_POST, 'ctct_optin_list', FILTER_SANITIZE_STRING );
779+
$ctct_optin_lists = filter_input( INPUT_POST, 'ctct_optin_list', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
773780

774-
if ( empty( $ctct_optin_list ) ) {
781+
if ( empty( $ctct_optin_lists ) ) {
775782
return $user;
776783
}
777784

@@ -793,7 +800,9 @@ public function process_optin_login_form( $user, $username, $password ) {
793800
*/
794801
public function process_optin_register_form( $user_id ) {
795802

796-
if ( ! isset( $_POST['ctct_optin_list'] ) ) {
803+
$ctct_optin_lists = filter_input( INPUT_POST, 'ctct_optin_list', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
804+
805+
if ( empty( $ctct_optin_lists ) ) {
797806
return $user_id;
798807
}
799808

@@ -842,21 +851,22 @@ private function add_user_to_list( $user ) {
842851
return;
843852
}
844853

845-
$list = filter_input( INPUT_POST, 'ctct_optin_list', FILTER_SANITIZE_STRING );
854+
$lists = filter_input( INPUT_POST, 'ctct_optin_list', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
846855

847-
if ( empty( $list ) ) {
856+
if ( empty( $lists ) ) {
848857
return;
849858
}
850859

851860
if ( $email ) {
852-
$args = [
853-
'email' => $email,
854-
'list' => sanitize_text_field( wp_unslash( $list ) ),
855-
'first_name' => $name,
856-
'last_name' => '',
857-
];
858-
859-
constantcontact_api()->add_contact( $args );
861+
foreach ( $lists as $list ) {
862+
$args = [
863+
'email' => $email,
864+
'list' => sanitize_text_field( wp_unslash( $list ) ),
865+
'first_name' => $name,
866+
'last_name' => '',
867+
];
868+
constantcontact_api()->add_contact( $args );
869+
}
860870
}
861871

862872
}
@@ -1061,6 +1071,33 @@ public function sanitize_recaptcha_api_key_string( $value, $field_args, $field )
10611071
private function get_default_spam_error() {
10621072
return __( 'We do not think you are human', 'constant-contact-forms' );
10631073
}
1074+
1075+
/**
1076+
* Returns formated list of available lists during opt-in.
1077+
*
1078+
* @author Scott Anderson <scott.anderson@webdevstudios.com>
1079+
* @since NEXT
1080+
*
1081+
* @return array
1082+
*/
1083+
private function get_optin_list_options() {
1084+
$lists = constant_contact_get_option( '_ctct_optin_list', '' );
1085+
1086+
$formatted_lists = [];
1087+
foreach ( $lists as $list_id ) {
1088+
1089+
$list_args = array(
1090+
'numberposts' => 1,
1091+
'post_type' => 'ctct_lists',
1092+
'meta_key' => '_ctct_list_id',
1093+
'meta_value' => $list_id
1094+
);
1095+
$list = get_posts( $list_args );
1096+
1097+
$formatted_lists[ $list_id ] = $list[0]->post_title;
1098+
}
1099+
return $formatted_lists;
1100+
}
10641101
}
10651102

10661103
/**

0 commit comments

Comments
 (0)