'widget-oceanwp-mailchimp mailchimp-widget', 'description' => esc_html__( 'Displays mailchimp subscription form.', 'ocean-extra' ), 'customize_selective_refresh' => true, ) ); add_action( 'wp_enqueue_scripts', array( $this, 'ocean_extra_mailchimp_js' ) ); add_filter( 'ocean_localize_array', array( $this, 'localize_array' ) ); add_action( 'wp_ajax_oceanwp_mailchimp_request', array( $this, 'oceanwp_mailchimp_request_callback' ) ); add_action( 'wp_ajax_nopriv_oceanwp_mailchimp_request', array( $this, 'oceanwp_mailchimp_request_callback' ) ); } public function oceanwp_mailchimp_request_callback() { $apikey = get_option( 'owp_mailchimp_api_key' ); $list_id = get_option( 'owp_mailchimp_list_id' ); $email = ( isset( $_POST['email'] ) ) ? $_POST['email'] : ''; $status = false; if ( $email && $apikey && $list_id ) { $root = 'https://api.mailchimp.com/3.0'; if ( strstr( $apikey, '-' ) ) { list( $key, $dc ) = explode( '-', $apikey, 2 ); } $root = str_replace( 'https://api', 'https://' . $dc . '.api', $root ); $root = rtrim( $root, '/' ) . '/'; $params = array( 'apikey' => $apikey, 'id' => $list_id, 'email_address' => $email, 'status' => 'subscribed', 'double_optin' => false, 'send_welcome' => false, 'replace_interests' => false, 'update_existing' => true, ); $ch = curl_init(); $params = json_encode( $params ); curl_setopt( $ch, CURLOPT_URL, $root . '/lists/' . $list_id . '/members/' . $email ); curl_setopt( $ch, CURLOPT_USERPWD, 'user:' . $apikey ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $params ); $response_body = curl_exec( $ch ); $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); curl_close( $ch ); if ( $httpCode == 200 ) { $status = true; } } wp_send_json( array( 'status' => $status ) ); } /** * Front-end display of widget. * * @see WP_Widget::widget() * @since 1.0.0 * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { wp_enqueue_script( 'oe-mailchimp-script' ); $title = isset( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : ''; $subscribe_text = isset( $instance['subscribe_text'] ) ? $instance['subscribe_text'] : ''; $mailchimp_gdpr_label = isset( $instance['mailchimp_gdpr_label'] ) ? $instance['mailchimp_gdpr_label'] : ''; $width = isset( $instance['width'] ) ? $instance['width'] : ''; $height = isset( $instance['height'] ) ? $instance['height'] : ''; $placeholder = isset( $instance['placeholder'] ) ? $instance['placeholder'] : ''; $submit_text = isset( $instance['submit_text'] ) ? $instance['submit_text'] : ''; // Sanitize vars $width = $width ? $width : ''; $height = $height ? $height : ''; // Inline style $form_style = ''; $input_style = ''; if ( $width ) { $form_style .= 'width:' . esc_attr( $width ) . ';'; } if ( $height ) { $input_style .= 'height:' . esc_attr( $height ) . ';'; } if ( $form_style ) { $form_style = ' style="' . esc_attr( $form_style ) . '"'; } if ( $input_style ) { $input_style = ' style="' . esc_attr( $input_style ) . '"'; } // Before widget WP hook echo $args['before_widget']; // Show widget title if ( $title ) { echo $args['before_title'] . esc_html( $title ) . $args['after_title']; } ?>
esc_attr__( 'Newsletter', 'ocean-extra' ), 'subscribe_text' => esc_html__( 'Get all latest content delivered to your email a few times a month. Updates and news about all categories will send to you.', 'ocean-extra' ), 'mailchimp_gdpr_label' => esc_attr__( 'Accept GDPR Terms', 'ocean-extra' ), 'width' => '', 'height' => '', 'placeholder' => esc_html__( 'Your Email', 'ocean-extra' ), 'submit_text' => esc_html__( 'Go', 'ocean-extra' ), ) ); // If no API KEy and List ID if ( ! get_option( 'owp_mailchimp_api_key' ) || ! get_option( 'owp_mailchimp_list_id' ) ) { ?>'oceanwp#integration' ), esc_url( admin_url( 'admin.php' ) ) ) . '" target="_blank">', '' ); ?>