interfaces
2 years ago
traits
2 years ago
action-factory.php
2 years ago
base-api-action.php
2 years ago
create-tags-action.php
2 years ago
get-categories-action.php
2 years ago
get-categories-interests-action.php
2 years ago
get-fields-action.php
2 years ago
get-lists-action.php
2 years ago
put-member-in-list-action.php
1 year ago
put-member-in-list-action.php
120 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Modules\Actions_V2\Mailchimp\Api; |
| 4 | |
| 5 | use Jet_Form_Builder\Classes\Tools; |
| 6 | use JFB_Modules\Actions_V2\Mailchimp\Api\interfaces\Subscriber_Interface; |
| 7 | use JFB_Modules\Actions_V2\Mailchimp\Api\traits\Subscriber_Trait; |
| 8 | |
| 9 | class Put_Member_In_List_Action extends Base_Api_Action implements Subscriber_Interface { |
| 10 | |
| 11 | use Subscriber_Trait; |
| 12 | |
| 13 | protected $method = 'PUT'; |
| 14 | |
| 15 | private $status_if_new = ''; |
| 16 | private $merge_fields = array(); |
| 17 | private $interests = array(); |
| 18 | |
| 19 | public function action_endpoint() { |
| 20 | return 'lists/{list_id}/members/{email_md5}'; |
| 21 | } |
| 22 | |
| 23 | |
| 24 | public function action_headers() { |
| 25 | return array( |
| 26 | 'Content-Type' => 'application/json; charset=utf-8', |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | public function action_body() { |
| 31 | $data = array( |
| 32 | 'status' => 'subscribed', |
| 33 | 'email_address' => $this->get_email_address(), |
| 34 | 'status_if_new' => $this->get_status_if_new(), |
| 35 | 'merge_fields' => $this->get_merge_fields(), |
| 36 | ); |
| 37 | |
| 38 | $interests = $this->get_interests(); |
| 39 | if ( ! empty( $interests ) ) { |
| 40 | $data['interests'] = $interests; |
| 41 | } |
| 42 | |
| 43 | return $data; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * @param int|string $birthday |
| 49 | * |
| 50 | * @return void |
| 51 | */ |
| 52 | public function set_birthday( $birthday ) { |
| 53 | if ( ! $birthday ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | if ( ! Tools::is_valid_timestamp( $birthday ) ) { |
| 58 | $birthday = strtotime( $birthday ); |
| 59 | } |
| 60 | |
| 61 | // phpcs:ignore WordPress.DateTime.RestrictedFunctions |
| 62 | $this->set_merge_fields( 'BIRTHDAY', date( 'm/d', $birthday ) ); |
| 63 | } |
| 64 | |
| 65 | public function set_merge_fields( string $name, $value ) { |
| 66 | $this->merge_fields[ $name ] = $value; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return array |
| 71 | */ |
| 72 | public function get_merge_fields(): array { |
| 73 | return $this->merge_fields; |
| 74 | } |
| 75 | |
| 76 | public function set_opt_in( $opt_in ) { |
| 77 | $is_opt_in = filter_var( |
| 78 | $opt_in, |
| 79 | defined( 'FILTER_VALIDATE_BOOL' ) ? FILTER_VALIDATE_BOOL : FILTER_VALIDATE_BOOLEAN |
| 80 | ); |
| 81 | |
| 82 | $this->set_status_if_new( $is_opt_in ? 'pending' : 'subscribed' ); |
| 83 | } |
| 84 | |
| 85 | public function set_status_if_new( string $status_if_new ) { |
| 86 | $this->status_if_new = $status_if_new; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return string |
| 91 | */ |
| 92 | public function get_status_if_new(): string { |
| 93 | return $this->status_if_new; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return array |
| 98 | */ |
| 99 | public function get_interests(): array { |
| 100 | return $this->interests; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param array $interests |
| 105 | */ |
| 106 | public function set_interests( array $interests ) { |
| 107 | if ( ! wp_is_numeric_array( $interests ) ) { |
| 108 | $this->interests = $interests; |
| 109 | |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | foreach ( $interests as $interest ) { |
| 114 | $this->interests[ $interest ] = true; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | } |
| 120 |