Opt-In Forms Settings.', 'powerkit' ), esc_url( powerkit_get_page_url( 'opt_in_forms' ) ) ) );
}
} else {
/* translators: MailChimp Settings. */
powerkit_alert_warning( sprintf( __( 'Please add your MailChimp Token in .', 'powerkit' ), esc_url( powerkit_get_page_url( 'opt_in_forms' ) ) ) );
}
}
/**
* Subscription.
*
* @since 1.0.0
*/
public function subscription() {
powerkit_uuid_hash();
// Check wpnonce.
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
return;
}
if ( isset( $_REQUEST['list_id'] ) ) { // Input var ok.
$list_id = sanitize_text_field( wp_unslash( $_REQUEST['list_id'] ) ); // Input var ok.
}
if ( isset( $_REQUEST['USER'] ) ) { // Input var ok.
$user = sanitize_text_field( wp_unslash( $_REQUEST['USER'] ) ); // Input var ok.
}
if ( isset( $_REQUEST['EMAIL'] ) ) { // Input var ok.
$email = sanitize_email( wp_unslash( $_REQUEST['EMAIL'] ) ); // Input var ok.
}
if ( ! isset( $list_id ) || ! $list_id ) {
wp_send_json_error( esc_html__( 'Something is wrong with your list ID.', 'powerkit' ) );
}
if ( ! isset( $email ) || ! $email || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
wp_send_json_error( esc_html__( 'Email is invalid.', 'powerkit' ) );
}
$token = get_option( 'powerkit_mailchimp_token' );
if ( $token ) {
if ( get_option( 'powerkit_mailchimp_double_optin', false ) ) {
$status = 'pending';
} else {
$status = 'subscribed';
}
// Request parameters.
$args = array(
'email_address' => $email,
'status' => $status,
);
// If display first name field.
if ( isset( $user ) ) {
$args['merge_fields'] = array(
'FNAME' => $user,
);
}
$result = powerkit_mailchimp_request( 'POST', "lists/$list_id/members", $args );
if ( isset( $result['status'] ) && 'subscribed' === $result['status'] ) {
wp_send_json_success( esc_html__( 'You have successfully subscribed.', 'powerkit' ) );
} elseif ( isset( $result['status'] ) && 'pending' === $result['status'] ) {
wp_send_json_success( esc_html__( 'You have successfully subscribed. Confirm the subscription in your mailbox.', 'powerkit' ) );
} elseif ( isset( $result['title'] ) && 'Member Exists' === $result['title'] ) {
wp_send_json_error( esc_html__( 'You are already subscribed.', 'powerkit' ) );
} else {
if ( isset( $result['status'] ) && isset( $result['detail'] ) && 400 <= $result['status'] ) {
$result = $result['detail'];
}
wp_send_json_error( $result );
}
}
}
/**
* Register the stylesheets for the public-facing side of the site.
*/
public function wp_enqueue_scripts() {
// Styles.
wp_enqueue_style( 'powerkit-opt-in-forms', powerkit_style( plugin_dir_url( __FILE__ ) . 'css/public-powerkit-opt-in-forms.css' ), array(), powerkit_get_setting( 'version' ), 'all' );
// Add RTL support.
wp_style_add_data( 'powerkit-opt-in-forms', 'rtl', 'replace' );
// Scripts.
wp_enqueue_script( 'powerkit-opt-in-forms', plugin_dir_url( __FILE__ ) . 'js/public-powerkit-opt-in-forms.js', array( 'jquery' ), powerkit_get_setting( 'version' ), true );
wp_localize_script( 'powerkit-opt-in-forms', 'opt_in', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'warning_privacy' => esc_html__( 'Please confirm that you agree with our policies.', 'powerkit' ),
) );
}
}