'widget_cm_ajax', 'description' => __( 'Ajax signup form for Campaign Monitor lists', 'cm_ajax' ) ) ; parent::__construct( 'widget_cm_ajax', __( 'Newsletter Signup', 'cm_ajax' ), $widget_ops ); add_action( 'switch_theme', array(&$this, 'flush_widget_cache') ); add_action( 'init', array( $this, 'ajax_receiver' ) ); add_action( 'init', array( $this, 'init_actions' ) ); } /** * Init actions * */ function init_actions() { if ( ! is_admin() ) { wp_enqueue_script('jquery'); } } /** * Handle Ajax requests * */ function ajax_receiver() { if ( ! isset ( $_POST['cm_ajax_action'] ) ) return; switch ( $_POST['cm_ajax_action'] ) { case 'subscribe': $this->subscribe(); break; default: break; } if( isset ( $_POST['cm_ajax_response'] ) && $_POST['cm_ajax_response'] == 'ajax' ) { die(); } } /** * Subscribe someone to a list * */ function subscribe() { $settings = get_option ( $this->option_name ); if ( isset ( $settings[$_POST['cm_ajax_widget_id']] ) ) { $settings = $settings[$_POST['cm_ajax_widget_id']]; } else { return 'FAILED\nNo Settings'; } $cm = new CS_REST_Subscribers($settings['list_api_key'], $settings['account_api_key']); $record = Array ( 'EmailAddress' => $_POST['cm-ajax-email'], 'Resubscribe' => true ); if ($settings['show_name_field']) { $record['Name'] = $_POST['cm-ajax-name']; } $result = $cm->add ( $record ); if( isset ( $_POST['cm_ajax_response'] ) && $_POST['cm_ajax_response'] == 'ajax' ) { if ($result->was_successful()) { echo 'SUCCESS'; } else { echo 'FAILED\n'; echo ($result->response->Code).': '; echo ($result->response->Message); } } else { $this->result = $result->was_successful(); } return; } /** * Output the widget content * * @param unknown $args * @param unknown $instance */ function widget($args, $instance) { $cache = wp_cache_get('widget_cm_ajax', 'widget'); if ( !is_array($cache) ) $cache = array(); if ( isset($cache[$args['widget_id']]) ) { echo $cache[$args['widget_id']]; return; } ob_start(); extract($args); echo $before_widget; $title = apply_filters('widget_title', empty ( $instance['title'] ) ? __('Newsletter Signup', 'cm_ajax') : $instance['title'], $instance, $this->id_base); echo $before_title . $title . $after_title; if ( isset ( $instance['pretext'] ) ) { echo $instance['pretext']; } if (isset($this->result)) { if ($this->result) { $success_style = ''; $failed_style = 'style="display: none;"'; $submit_style = 'style="display: none;"'; } else { $success_style = 'style="display: none;"'; $failed_style = ''; $submit_style = ''; } } else { $success_style = 'style="display: none;"'; $failed_style = 'style="display: none;"'; $submit_style = ''; } // Main signup form ?>

class="cm_ajax_success"> class="cm_ajax_failed">

type="submit" name="cm-ajax-submit" value="">

flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset($alloptions['widget_cm_ajax']) ) delete_option('widget_cm_ajax'); return $instance; } /** * Empty the widget cache */ function flush_widget_cache() { wp_cache_delete('widget_cm_ajax', 'widget'); } /** * Widget configuration form * * @param unknown $instance */ function form( $instance ) { $title = isset($instance['title']) ? esc_attr($instance['title']) : ''; $pretext = isset($instance['pretext']) ? esc_attr($instance['pretext']) : ''; $account_api_key = isset($instance['account_api_key']) ? esc_attr($instance['account_api_key']) : ''; $list_api_key = isset($instance['list_api_key']) ? esc_attr($instance['list_api_key']) : ''; $show_name_field = isset($instance['show_name_field']) ? esc_attr($instance['show_name_field']) : TRUE; ?>

/>