mailchimp_api_key = $user_data['mailchimp']['api_key']; } } /** * Build block content */ public function build_content() { $name = $this->render_name_form(); $phone = $this->render_phone_form(); $email = $this->render_email_form(); $button = $this->render_button(); $message = esc_attr( $this->attribute['sg_form_success_message'] ); $style = esc_attr( $this->attribute['sg_form_style'] ); $list = esc_attr( $this->attribute['sg_form_list'] ); $error = esc_html__( 'Something went wrong', 'jeg-elementor-kit' ); $extra = 'email-form'; if ( $name || $phone ) { $extra = 'extra-fields'; } $content = '
' . $name . $phone . $email . $button . '
'; return $this->render_wrapper( 'mailchimp', $content, array( 'style-' . $style ) ); } /** * Build Email Form */ protected function render_email_form() { $icon_label = null; $label = null; $icon_enable = 'yes' === $this->attribute['sg_form_email_icon_enable']; $label_text = esc_attr( $this->attribute['sg_form_email_label'] ); $placeholder = esc_attr( $this->attribute['sg_form_email_placeholder'] ); $input = ''; if ( $icon_enable ) { $icon_position = esc_attr( $this->attribute['sg_form_email_icon_position'] ); $icon_label = '
' . $this->render_icon_element( $this->attribute['sg_form_email_icon'] ) . '
'; if ( 'before' === $icon_position ) { $input = $icon_label . $input; } else { $input = $input . $icon_label; } } if ( ! empty( $label_text ) ) { $label = ''; } $form = '
' . $label . '
' . $input . '
'; return $form; } /** * Build Phone Form */ protected function render_phone_form() { $form = null; $enable = 'yes' === $this->attribute['sg_form_phone_enable']; if ( $enable ) { $icon_label = null; $label = null; $icon_enable = 'yes' === $this->attribute['sg_form_phone_icon_enable']; $label_text = esc_attr( $this->attribute['sg_form_phone_label'] ); $placeholder = esc_attr( $this->attribute['sg_form_phone_placeholder'] ); $input = ''; if ( $icon_enable ) { $icon_position = esc_attr( $this->attribute['sg_form_phone_icon_position'] ); $icon_label = '
' . $this->render_icon_element( $this->attribute['sg_form_phone_icon'] ) . '
'; if ( 'before' === $icon_position ) { $input = $icon_label . $input; } else { $input = $input . $icon_label; } } if ( ! empty( $label_text ) ) { $label = ''; } $form = '
' . $label . '
' . $input . '
'; } return $form; } /** * Build Name Form */ protected function render_name_form() { $form = ''; $enable = 'yes' === $this->attribute['sg_form_name_enable']; if ( $enable ) { /** * First Name Form */ $icon_label = null; $label = null; $icon_enable = 'yes' === $this->attribute['sg_form_name_first_icon_enable']; $label_text = esc_attr( $this->attribute['sg_form_name_first_label'] ); $placeholder = esc_attr( $this->attribute['sg_form_name_first_placeholder'] ); $input = ''; if ( $icon_enable ) { $icon_position = esc_attr( $this->attribute['sg_form_name_first_icon_position'] ); $icon_label = '
' . $this->render_icon_element( $this->attribute['sg_form_name_first_icon'] ) . '
'; if ( 'before' === $icon_position ) { $input = $icon_label . $input; } else { $input = $input . $icon_label; } } if ( ! empty( $label_text ) ) { $label = ''; } $form = $form . '
' . $label . '
' . $input . '
'; /** * Last Name Form */ $icon_label = null; $label = null; $icon_enable = 'yes' === $this->attribute['sg_form_name_last_icon_enable']; $label_text = esc_attr( $this->attribute['sg_form_name_last_label'] ); $placeholder = esc_attr( $this->attribute['sg_form_name_last_placeholder'] ); $input = ''; if ( $icon_enable ) { $icon_position = esc_attr( $this->attribute['sg_form_name_last_icon_position'] ); $icon_label = '
' . $this->render_icon_element( $this->attribute['sg_form_name_last_icon'] ) . '
'; if ( 'before' === $icon_position ) { $input = $icon_label . $input; } else { $input = $input . $icon_label; } } if ( ! empty( $label_text ) ) { $label = ''; } $form = $form . '
' . $label . '
' . $input . '
'; } return $form; } /** * Build Button * * @return mixed */ protected function render_button() { $text = esc_attr( $this->attribute['sg_form_button_text'] ); $icon_position = esc_attr( $this->attribute['sg_form_button_icon_position'] ); $icon = 'yes' === $this->attribute['sg_form_button_icon_enable'] ? $this->render_icon_element( $this->attribute['sg_form_button_icon'] ) : ''; if ( 'before' === $icon_position ) { $text = $icon . $text; } else { $text = $text . $icon; } $button = '
'; return $button; } /** * Ajax request handler */ public function ajax_request() { $this->set_mailchimp_api_key(); // @codingStandardsIgnoreStart sanitize value using jeg_sanitize_array $data = jeg_sanitize_array( $_REQUEST['data'] ); // @codingStandardsIgnoreEnd $check_admin = in_array( 'administrator', wp_get_current_user()->roles, true ); if ( empty( $this->mailchimp_api_key ) ) { $error_message = esc_html__( 'Something went wrong', 'jeg-elementor-kit' ); if ( $check_admin ) { $error_message = esc_html__( 'Please set API Key into dashboard user data.', 'jeg-elementor-kit' ); } wp_send_json( array( 'message' => $error_message, 'status_code' => 400, ) ); return; } $server = explode( '-', $this->mailchimp_api_key ); if ( ! isset( $server[1] ) || empty( $server[1] ) || preg_match( '/^.*\..+$/', $server[1] ) ) { $error_message = esc_html__( 'Something went wrong', 'jeg-elementor-kit' ); if ( $check_admin ) { $error_message = esc_html__( 'Please make sure your API Key is correct in dashboard user data.', 'jeg-elementor-kit' ); } wp_send_json( array( 'message' => $error_message, 'status_code' => 400, ) ); return; } $url = 'https://' . $server[1] . '.api.mailchimp.com/3.0/lists/' . $data['list'] . '/members'; $payload = array( 'email_address' => $data['email'], 'status' => 'subscribed', 'status_if_new' => 'subscribed', 'merge_fields' => array( 'FNAME' => isset( $data['first_name'] ) ? $data['first_name'] : '', 'LNAME' => isset( $data['last_name'] ) ? $data['last_name'] : '', 'PHONE' => isset( $data['phone'] ) ? $data['phone'] : '', ), ); $response = wp_remote_post( $url, array( 'method' => 'POST', 'data_format' => 'body', 'timeout' => 45, 'headers' => array( 'Authorization' => 'apikey ' . $this->mailchimp_api_key, 'Content-Type' => 'application/json; charset=utf-8', 'Content-Language' => get_locale(), ), 'body' => wp_json_encode( $payload ), ) ); if ( is_wp_error( $response ) ) { $error_message = $response->get_error_message(); wp_send_json( array( 'message' => esc_html__( 'Something went wrong: ', 'jeg-elementor-kit' ) . $error_message, 'status_code' => 400, ) ); return; } $response_body = wp_remote_retrieve_body( $response ); $response_body = ! is_array( $response_body ) ? json_decode( $response_body ) : $response_body; if ( is_object( $response_body ) && $response_body->status >= 400 ) { $response_data = apply_filters( 'jkit_mailchimp_response', array( 'message' => $response_body->title, 'status_code' => $response_body->status, ), $response_body ); wp_send_json( $response_data ); return; } wp_send_json( array( 'message' => null, 'status_code' => 200, ) ); } }