Mailgun for WordPress - Version 1.4

Version Description

(2015-11-15): = * Added shortcode and widget support for list subscription

Download this release

Release Info

Developer Mailgun
Plugin Icon 128x128 Mailgun for WordPress
Version 1.4
Comparing to
See all releases

Code changes from version 1.3.1 to 1.4

includes/admin.php CHANGED
@@ -67,6 +67,7 @@ class MailgunAdmin extends Mailgun {
67
  function admin_menu() {
68
  if ( current_user_can( 'manage_options' ) ) {
69
  $this->hook_suffix = add_options_page( __( 'Mailgun', 'mailgun' ), __( 'Mailgun', 'mailgun' ), 'manage_options', 'mailgun', array( &$this , 'options_page' ) );
 
70
  add_action( "admin_print_scripts-{$this->hook_suffix}" , array( &$this , 'admin_js' ) );
71
  add_filter( "plugin_action_links_{$this->plugin_basename}" , array( &$this , 'filter_plugin_actions' ) );
72
  add_action( "admin_footer-{$this->hook_suffix}" , array( &$this , 'admin_footer_js' ) );
@@ -155,6 +156,20 @@ class MailgunAdmin extends Mailgun {
155
  printf( __( '<div id="message" class="updated fade"><p>The options page for the <strong>Mailgun</strong> plugin cannot be displayed. The file <strong>%s</strong> is missing. Please reinstall the plugin.</p></div>', 'mailgun'), dirname( __FILE__ ) . '/options-page.php' );
156
  }
157
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
  /**
160
  * Wrapper function hooked into admin_init to register settings
67
  function admin_menu() {
68
  if ( current_user_can( 'manage_options' ) ) {
69
  $this->hook_suffix = add_options_page( __( 'Mailgun', 'mailgun' ), __( 'Mailgun', 'mailgun' ), 'manage_options', 'mailgun', array( &$this , 'options_page' ) );
70
+ add_options_page( __( 'Mailgun Lists', 'mailgun' ), null, 'manage_options', 'mailgun-lists', array( &$this , 'lists_page' ) );
71
  add_action( "admin_print_scripts-{$this->hook_suffix}" , array( &$this , 'admin_js' ) );
72
  add_filter( "plugin_action_links_{$this->plugin_basename}" , array( &$this , 'filter_plugin_actions' ) );
73
  add_action( "admin_footer-{$this->hook_suffix}" , array( &$this , 'admin_footer_js' ) );
156
  printf( __( '<div id="message" class="updated fade"><p>The options page for the <strong>Mailgun</strong> plugin cannot be displayed. The file <strong>%s</strong> is missing. Please reinstall the plugin.</p></div>', 'mailgun'), dirname( __FILE__ ) . '/options-page.php' );
157
  }
158
  }
159
+
160
+ /**
161
+ * Output the lists page
162
+ *
163
+ * @return none
164
+ * @since 0.1
165
+ */
166
+ function lists_page() {
167
+ if ( ! @include( 'lists-page.php' ) ) {
168
+ printf( __( '<div id="message" class="updated fade"><p>The lists page for the <strong>Mailgun</strong> plugin cannot be displayed. The file <strong>%s</strong> is missing. Please reinstall the plugin.</p></div>', 'mailgun'), dirname( __FILE__ ) . '/lists-page.php' );
169
+ }
170
+ }
171
+
172
+ // /options-general.php?page=mailgun-lists
173
 
174
  /**
175
  * Wrapper function hooked into admin_init to register settings
includes/lists-page.php ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ global $mailgun;
4
+
5
+ // check mailgun domain & api key
6
+ $missing_error = '';
7
+ $api_key = $this->get_option( 'apiKey' );
8
+ $mailgun_domain = $this->get_option( 'domain' );
9
+ if($api_key != ''){
10
+ if($mailgun_domain == ''){
11
+ $missing_error = '<strong style="color:red;">Missing or invalid Mailgun Domain</strong>. ';
12
+ }
13
+ } else {
14
+ $missing_error = '<strong style="color:red;">Missing or invalid API Key</strong>. ';
15
+ }
16
+
17
+ // import available lists
18
+ $lists_arr = $mailgun->get_lists();
19
+
20
+ ?>
21
+
22
+ <div class="wrap">
23
+
24
+ <div id="icon-options-general" class="icon32"><br /></div>
25
+
26
+ <span class="alignright">
27
+ <a target="_blank" href="http://www.mailgun.com/">
28
+ <img src="https://2e6874288eee3bf7ca22-d122329f808928cff1e9967578106854.ssl.cf1.rackcdn.com/mailgun-logo.png" alt="Mailgun" />
29
+ </a>
30
+ </span>
31
+
32
+ <h2><?php _e( 'Mailgun Lists' , 'mailgun' ); ?></h2>
33
+
34
+ <?php settings_fields( 'mailgun' ); ?>
35
+
36
+ <h3><?php _e( 'Available Mailing Lists' , 'mailgun' ); ?> | <a href="/wp-admin/options-general.php?page=mailgun">Back to settings</a></h3>
37
+
38
+ <p><?php _e( "{$missing_error}You must use a valid Mailgun domain name and API key to access lists", 'mailgun' ); ?></p>
39
+
40
+ <div id="mailgun-lists" style="margin-top:20px;">
41
+
42
+ <?php if( !empty($lists_arr) ) : ?>
43
+
44
+ <table class="wp-list-table widefat fixed striped pages">
45
+
46
+ <tr>
47
+ <th>List Address</th>
48
+ <th>Description</th>
49
+ <th>Shortcode</th>
50
+ </tr>
51
+
52
+ <?php foreach($lists_arr as $list) : ?>
53
+
54
+ <tr>
55
+ <td><?php echo $list['address']; ?></td>
56
+ <td><?php echo $list['description']; ?></td>
57
+ <td>
58
+ [mailgun id="<?php echo $list['address']; ?>"]
59
+ </td>
60
+ </tr>
61
+
62
+ <?php endforeach; ?>
63
+
64
+ </table>
65
+
66
+ <h3>Multi-list subscription</h3>
67
+ <p>
68
+ <?php _e( 'To allow users to subscribe to multiple lists on a single form, comma-separate the Mailgun list ids.', 'mailgun' ); ?></p>
69
+ <p class="description">
70
+ <?php _e( '<strong>Example:</strong> [mailgun id="list1@mydomain.com,list2@mydomain.com"]'); ?>
71
+ </p>
72
+
73
+ <?php endif; ?>
74
+
75
+ </div>
76
+ </div>
includes/options-page.php CHANGED
@@ -95,23 +95,39 @@
95
  </tr>
96
  <tr valign="top">
97
  <th scope="row">
98
- <?php _e( 'Campaign ID' , 'mailgun' ); ?>
99
  </th>
100
  <td>
101
- <input type="text" class="regular-text" name="mailgun[campaign-id]" value="<?php esc_attr_e( $this->get_option( 'campaign-id' ) ); ?>" placeholder="campaign-id" />
102
- <p class="description"><?php _e( 'If added, this campaign will exist on every outbound message. Statistics will be populated in the Mailgun Control Panel. Use a comma to define multiple campaigns.', 'mailgun' ); ?> <a href="http://documentation.mailgun.com/user_manual.html#campaign-analytics" target="_blank">Campaign Documentation</a></p>
103
  </td>
104
  </tr>
 
 
 
105
  <tr valign="top">
106
  <th scope="row">
107
- <?php _e( 'Tag' , 'mailgun' ); ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  </th>
109
  <td>
110
- <input type="text" class="regular-text" name="mailgun[tag]" value="<?php esc_attr_e( $this->get_option( 'tag' ) ); ?>" placeholder="tag" />
111
- <p class="description"><?php _e( 'If added, this tag will exist on every outbound message. Statistics will be populated in the Mailgun Control Panel. Use a comma to define multiple tags.', 'mailgun' ); ?> <a href="http://documentation.mailgun.com/user_manual.html#tagging" target="_blank">Tagging Documentation</a></p>
112
  </td>
113
  </tr>
114
  </table>
 
115
  <p><?php _e( 'Before attempting to test the configuration, please click "Save Changes".', 'mailgun' ); ?></p>
116
  <p class="submit">
117
  <input type="submit" class="button-primary" value="<?php _e( 'Save Changes' , 'mailgun' ); ?>" />
95
  </tr>
96
  <tr valign="top">
97
  <th scope="row">
98
+ <?php _e( 'Tag' , 'mailgun' ); ?>
99
  </th>
100
  <td>
101
+ <input type="text" class="regular-text" name="mailgun[campaign-id]" value="<?php esc_attr_e( $this->get_option( 'campaign-id' ) ); ?>" placeholder="tag" />
102
+ <p class="description"><?php _e( 'If added, this tag will exist on every outbound message. Statistics will be populated in the Mailgun Control Panel. Use a comma to define multiple tags.', 'mailgun' ); ?> <?php _e('Learn more about','mailgun'); ?> <a href="https://documentation.mailgun.com/user_manual.html#tracking-messages" target="_blank">Tracking</a> <?php _e('and','mailgun'); ?> <a href="https://documentation.mailgun.com/user_manual.html#tagging" target="_blank">Tagging</a>.</p>
103
  </td>
104
  </tr>
105
+ </table>
106
+ <h3><?php _e( 'Lists' , 'mailgun' ); ?></h3>
107
+ <table class="form-table">
108
  <tr valign="top">
109
  <th scope="row">
110
+ <?php _e( 'Shortcode' , 'mailgun' ); ?>
111
+ </th>
112
+ <td>
113
+ <div>
114
+ <strong>[mailgun id="<em>{mailgun list id}</em>" collect_name="true"]</strong>
115
+ </div>
116
+ <div>
117
+ <p class="description"><?php _e( 'Use the shortcode above to associate a widget instance with a mailgun list', 'mailgun' ); ?></p>
118
+ </div>
119
+ </td>
120
+ </tr>
121
+ <tr valign="top">
122
+ <th scope="row">
123
+ <?php _e( 'Lists' , 'mailgun' ); ?>
124
  </th>
125
  <td>
126
+ <a href="?page=mailgun-lists">View available lists</a>
 
127
  </td>
128
  </tr>
129
  </table>
130
+
131
  <p><?php _e( 'Before attempting to test the configuration, please click "Save Changes".', 'mailgun' ); ?></p>
132
  <p class="submit">
133
  <input type="submit" class="button-primary" value="<?php _e( 'Save Changes' , 'mailgun' ); ?>" />
includes/widget.php ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class list_widget extends WP_Widget {
4
+
5
+ function __construct() {
6
+ parent::__construct(
7
+ // Base ID of your widget
8
+ 'list_widget',
9
+
10
+ // Widget name will appear in UI
11
+ __('Mailgun List Widget', 'wpb_widget_domain'),
12
+
13
+ // Widget description
14
+ array( 'description' => __( 'Mailgun list widget', 'wpb_widget_domain' ), )
15
+ );
16
+ }
17
+
18
+ // Creating widget front-end
19
+ // This is where the action happens
20
+ public function widget($args, $instance) {
21
+ global $mailgun;
22
+
23
+ // vars
24
+ $list_address = apply_filters('list_address', $instance['list_address']);
25
+
26
+ if( isset($instance['collect_name']) ) {
27
+ $args['collect_name'] = true;
28
+ }
29
+
30
+ if( isset($instance['list_title']) ) {
31
+ $args['list_title'] = $instance['list_title'];
32
+ }
33
+
34
+ if( isset($instance['list_description']) ) {
35
+ $args['list_description'] = $instance['list_description'];
36
+ }
37
+
38
+ $mailgun->list_form($list_address, $args, $instance);
39
+ }
40
+
41
+ // Widget Backend
42
+ public function form( $instance ) {
43
+ global $mailgun;
44
+
45
+ if ( isset( $instance[ 'list_address' ] ) ) {
46
+ $list_address = $instance[ 'list_address' ];
47
+ } else {
48
+ $list_address = __( 'New list_address', 'wpb_widget_domain' );
49
+ }
50
+
51
+ if ( isset( $instance[ 'collect_name' ] ) && $instance[ 'collect_name' ] === 'on' ) {
52
+ $collect_name = 'checked';
53
+ } else {
54
+ $collect_name = '';
55
+ }
56
+
57
+ $list_title = isset( $instance[ 'list_title' ] ) ? $instance['list_title'] : null;
58
+ $list_description = isset( $instance[ 'list_description' ] ) ? $instance['list_description'] : null;
59
+
60
+ // Widget admin form
61
+ ?>
62
+ <div class="mailgun-list-widget-back">
63
+ <p>
64
+ <label for="<?php echo $this->get_field_id( 'list_title' ); ?>"><?php _e( 'Title (optional):' ); ?></label>
65
+ <input class="widefat" id="<?php echo $this->get_field_id( 'list_title' ); ?>" name="<?php echo $this->get_field_name( 'list_title' ); ?>" type="text" value="<?php echo esc_attr( $list_title ); ?>" />
66
+ </p>
67
+ <p>
68
+ <label for="<?php echo $this->get_field_id( 'list_description' ); ?>"><?php _e( 'Description (optional):' ); ?></label>
69
+ <input class="widefat" id="<?php echo $this->get_field_id( 'list_description' ); ?>" name="<?php echo $this->get_field_name( 'list_description' ); ?>" type="text" value="<?php echo esc_attr( $list_description ); ?>" />
70
+ </p>
71
+ <p>
72
+ <label for="<?php echo $this->get_field_id( 'list_address' ); ?>"><?php _e( 'List addresses (required):' ); ?></label>
73
+ <input class="widefat" id="<?php echo $this->get_field_id( 'list_address' ); ?>" name="<?php echo $this->get_field_name( 'list_address' ); ?>" type="text" value="<?php echo esc_attr( $list_address ); ?>" />
74
+ </p>
75
+ <p>
76
+ <label for="<?php echo $this->get_field_id( 'collect_name' ); ?>"><?php _e( 'Collect name:' ); ?></label>
77
+ <input class="widefat" id="<?php echo $this->get_field_id( 'collect_name' ); ?>" name="<?php echo $this->get_field_name( 'collect_name' ); ?>" type="checkbox" <?php echo esc_attr( $collect_name ); ?> />
78
+ </p>
79
+ </div>
80
+ <?php
81
+ }
82
+
83
+ // Updating widget replacing old instances with new
84
+ public function update( $new_instance, $old_instance ) {
85
+ $instance = array();
86
+ $instance = $new_instance;
87
+
88
+ return $instance;
89
+ }
90
+ }
91
+
92
+
93
+
includes/wp-mail.php CHANGED
@@ -142,14 +142,20 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
142
  $body['o:tracking-clicks'] = isset( $mailgun['track-clicks'] ) ? $mailgun['track-clicks'] : "no";
143
  $body['o:tracking-opens'] = isset( $mailgun['track-opens'] ) ? "yes" : "no";
144
 
 
145
  if ( isset( $mailgun['tag'] ) ){
146
  $tags = explode(",", str_replace(" ","", $mailgun['tag']));
147
  $body['o:tag'] = $tags;
148
  }
149
 
 
150
  if ( isset( $mailgun['campaign-id'] ) ){
151
- $campaigns = explode(",", str_replace(" ","", $mailgun['campaign-id']));
152
- $body['o:campaign'] = $campaigns;
 
 
 
 
153
  }
154
 
155
  if ( ! empty( $cc ) && is_array( $cc ) )
142
  $body['o:tracking-clicks'] = isset( $mailgun['track-clicks'] ) ? $mailgun['track-clicks'] : "no";
143
  $body['o:tracking-opens'] = isset( $mailgun['track-opens'] ) ? "yes" : "no";
144
 
145
+ // this is the wordpress site tag
146
  if ( isset( $mailgun['tag'] ) ){
147
  $tags = explode(",", str_replace(" ","", $mailgun['tag']));
148
  $body['o:tag'] = $tags;
149
  }
150
 
151
+ // campaign-id now refers to a list of tags which will be appended to the site tag
152
  if ( isset( $mailgun['campaign-id'] ) ){
153
+ $tags = explode(",", str_replace(" ","", $mailgun['campaign-id']));
154
+ if ($body['o:tag']=='') {
155
+ $body['o:tag']= $tags;
156
+ } else {
157
+ $body['o:tag'].=','.$tags;
158
+ }
159
  }
160
 
161
  if ( ! empty( $cc ) && is_array( $cc ) )
mailgun.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Mailgun
4
  * Plugin URI: http://wordpress.org/extend/plugins/mailgun/
5
  * Description: Mailgun integration for WordPress
6
- * Version: 1.3.1
7
  * Author: Mailgun
8
  * Author URI: http://www.mailgun.com/
9
  * License: GPLv2 or later
@@ -19,10 +19,11 @@ class Mailgun {
19
  * @return none
20
  * @since 0.1
21
  */
22
- function __construct() {
23
  $this->options = get_option( 'mailgun' );
24
  $this->plugin_file = __FILE__;
25
  $this->plugin_basename = plugin_basename( $this->plugin_file );
 
26
 
27
  // Either override the wp_mail function or configure PHPMailer to use the
28
  // Mailgun SMTP servers
@@ -43,7 +44,7 @@ class Mailgun {
43
  * @return mixed
44
  * @since 0.1
45
  */
46
- function get_option( $option, $options = null ) {
47
  if ( is_null( $options ) )
48
  $options = &$this->options;
49
  if ( isset( $options[$option] ) )
@@ -60,7 +61,7 @@ class Mailgun {
60
  * @return none
61
  * @since 0.1
62
  */
63
- function phpmailer_init( &$phpmailer ) {
64
  $username = ( defined( 'MAILGUN_USERNAME' ) && MAILGUN_USERNAME ) ? MAILGUN_USERNAME : $this->get_option( 'username' );
65
  $domain = ( defined( 'MAILGUN_DOMAIN' ) && MAILGUN_DOMAIN ) ? MAILGUN_DOMAIN : $this->get_option( 'domain' );
66
  $username = preg_replace( '/@.+$/', '', $username ) . "@{$domain}";
@@ -84,7 +85,7 @@ class Mailgun {
84
  * @since 0.1
85
  * @return none
86
  */
87
- function deactivate_and_die( $file ) {
88
  load_plugin_textdomain( 'mailgun', false, 'mailgun/languages' );
89
  $message = sprintf( __( "Mailgun has been automatically deactivated because the file <strong>%s</strong> is missing. Please reinstall the plugin and reactivate." ), $file );
90
  if ( ! function_exists( 'deactivate_plugins' ) )
@@ -92,7 +93,302 @@ class Mailgun {
92
  deactivate_plugins( __FILE__ );
93
  wp_die( $message );
94
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  }
97
 
98
  if ( is_admin() ) {
@@ -101,6 +397,8 @@ if ( is_admin() ) {
101
  } else {
102
  Mailgun::deactivate_and_die( dirname( __FILE__ ) . '/includes/admin.php' );
103
  }
104
- } else {
105
- $mailgun = new Mailgun();
106
  }
 
 
 
 
3
  * Plugin Name: Mailgun
4
  * Plugin URI: http://wordpress.org/extend/plugins/mailgun/
5
  * Description: Mailgun integration for WordPress
6
+ * Version: 1.4
7
  * Author: Mailgun
8
  * Author URI: http://www.mailgun.com/
9
  * License: GPLv2 or later
19
  * @return none
20
  * @since 0.1
21
  */
22
+ public function __construct() {
23
  $this->options = get_option( 'mailgun' );
24
  $this->plugin_file = __FILE__;
25
  $this->plugin_basename = plugin_basename( $this->plugin_file );
26
+ $this->api_endpoint = 'https://api.mailgun.net/v3/';
27
 
28
  // Either override the wp_mail function or configure PHPMailer to use the
29
  // Mailgun SMTP servers
44
  * @return mixed
45
  * @since 0.1
46
  */
47
+ public function get_option( $option, $options = null ) {
48
  if ( is_null( $options ) )
49
  $options = &$this->options;
50
  if ( isset( $options[$option] ) )
61
  * @return none
62
  * @since 0.1
63
  */
64
+ public function phpmailer_init( &$phpmailer ) {
65
  $username = ( defined( 'MAILGUN_USERNAME' ) && MAILGUN_USERNAME ) ? MAILGUN_USERNAME : $this->get_option( 'username' );
66
  $domain = ( defined( 'MAILGUN_DOMAIN' ) && MAILGUN_DOMAIN ) ? MAILGUN_DOMAIN : $this->get_option( 'domain' );
67
  $username = preg_replace( '/@.+$/', '', $username ) . "@{$domain}";
85
  * @since 0.1
86
  * @return none
87
  */
88
+ public function deactivate_and_die( $file ) {
89
  load_plugin_textdomain( 'mailgun', false, 'mailgun/languages' );
90
  $message = sprintf( __( "Mailgun has been automatically deactivated because the file <strong>%s</strong> is missing. Please reinstall the plugin and reactivate." ), $file );
91
  if ( ! function_exists( 'deactivate_plugins' ) )
93
  deactivate_plugins( __FILE__ );
94
  wp_die( $message );
95
  }
96
+
97
+ /**
98
+ * Make a Mailgun api call
99
+ *
100
+ * @param string $endpoint The Mailgun endpoint uri
101
+ * @return array
102
+ * @since 0.1
103
+ */
104
+ public function api_call($uri, $params = array(), $method = 'POST') {
105
+
106
+ $options = get_option( 'mailgun' );
107
+ $apiKey = ( defined( 'MAILGUN_APIKEY' ) && MAILGUN_APIKEY ) ? MAILGUN_APIKEY : $options['apiKey'];
108
+ $domain = ( defined( 'MAILGUN_DOMAIN' ) && MAILGUN_DOMAIN ) ? MAILGUN_DOMAIN : $options['domain'];
109
+
110
+ $time = time();
111
+ $url = $this->api_endpoint . $uri;
112
+ $headers = array('Authorization' => 'Basic ' . base64_encode( "api:{$apiKey}"));
113
+
114
+ switch ($method) {
115
+ case 'GET':
116
+ $params['sess'] = '';
117
+ $querystring = http_build_query($params);
118
+ $url = $url.'?'.$querystring;
119
+ $params = '';
120
+ break;
121
+ case 'POST':
122
+ case 'PUT':
123
+ case 'DELETE':
124
+ $params['sess'] = '';
125
+ $params['time'] = $time;
126
+ $params['hash'] = sha1(date('U'));
127
+ break;
128
+ }
129
+
130
+ // make the request
131
+ $args = array(
132
+ 'method' => $method,
133
+ 'body' => $params,
134
+ 'headers' => $headers,
135
+ 'sslverify' => true
136
+ );
137
+
138
+ // make the remote request
139
+ $result = wp_remote_request($url, $args);
140
+ if ( !is_wp_error($result) ) {
141
+ return $result['body'];
142
+ } else {
143
+ return $result->get_error_message();
144
+ }
145
+
146
+ }
147
+
148
+ /**
149
+ * Get account associated lists
150
+ *
151
+ * @return array
152
+ * @since 0.1
153
+ */
154
+ public function get_lists() {
155
+
156
+ $results = array();
157
+
158
+ $lists_json = $this->api_call("lists", array(), 'GET');
159
+ $lists_arr = json_decode($lists_json, true);
160
+ if( isset($lists_arr['items']) && !empty($lists_arr['items']) ){
161
+ $results = $lists_arr['items'];
162
+ }
163
+
164
+ return $results;
165
+ }
166
+
167
+ /**
168
+ * Handle add list ajax post
169
+ *
170
+ * @return string json
171
+ * @since 0.1
172
+ */
173
+ public function add_list() {
174
+
175
+ $response = array();
176
+
177
+ $name = isset($_POST['name']) ? $_POST['name'] : null;
178
+ $email = isset($_POST['email']) ? $_POST['email'] : null;
179
+
180
+ $list_addresses = $_POST['addresses'];
181
+
182
+ if( ! empty($list_addresses)) {
183
+
184
+ foreach($list_addresses as $address => $val) {
185
+
186
+ $response[] = $this->api_call(
187
+ "lists/{$address}/members",
188
+ array(
189
+ 'address' => $email,
190
+ 'name' => $name
191
+ )
192
+ );
193
+
194
+ }
195
+
196
+ echo json_encode(array('status' => 200, 'message' => 'Thank you!'));
197
+
198
+ } else {
199
+
200
+ echo json_encode(array('status' => 500, 'message' => 'Uh oh. We weren\'t able to add you to the list' . count($list_addresses) ? 's.' : '. Please try again.'));
201
+ }
202
+
203
+ wp_die();
204
+ }
205
+
206
+ /**
207
+ * Frontend List Form
208
+ *
209
+ * @param string $list_address Mailgun address list id
210
+ * @param array $args widget arguments
211
+ * @param string $instance widget instance params
212
+ * @since 0.1
213
+ */
214
+ public function list_form($list_address, $args = array(), $instance = array()) {
215
+
216
+ $widget_class_id = "mailgun-list-widget-{$args['widget_id']}";
217
+ $form_class_id = "list-form-{$args['widget_id']}";
218
+
219
+ // List addresses from the plugin config
220
+ $list_addresses = array_map('trim', explode(',', $list_address));
221
 
222
+ // All list info from the API; used for list info when more than one list is available to subscribe to
223
+ $all_list_addresses = $this->get_lists();
224
+
225
+ ?>
226
+ <div class="mailgun-list-widget-front <?php echo $widget_class_id; ?> widget">
227
+ <form class="list-form <?php echo $form_class_id; ?>">
228
+ <div class="mailgun-list-widget-inputs">
229
+ <?php if(isset($args['list_title']) ) : ?>
230
+ <div class="mailgun-list-title">
231
+ <h4 class="widget-title">
232
+ <span><?php echo $args['list_title']; ?></span>
233
+ </h4>
234
+ </div>
235
+ <?php endif; ?>
236
+ <?php if(isset($args['list_description']) ) : ?>
237
+ <div class="mailgun-list-description">
238
+ <p class="widget-description">
239
+ <span><?php echo $args['list_description']; ?></span>
240
+ </p>
241
+ </div>
242
+ <?php endif; ?>
243
+ <?php if(isset($args['collect_name']) && intval($args['collect_name']) === 1) : ?>
244
+ <p class="mailgun-list-widget-name">
245
+ <strong>Name:</strong>
246
+ <input type="text" name="name" />
247
+ </p>
248
+ <?php endif; ?>
249
+ <p class="mailgun-list-widget-email">
250
+ <strong>Email:</strong>
251
+ <input type="text" name="email" />
252
+ </p>
253
+ </div>
254
+
255
+ <?php if(count($list_addresses) > 1) : ?>
256
+ <ul class="mailgun-lists" style="list-style: none;">
257
+ <?php foreach($all_list_addresses as $la) : ?>
258
+ <?php if( ! in_array($la['address'], $list_addresses ) ) continue; ?>
259
+ <li>
260
+ <input type="checkbox" class="mailgun-list-name" name="addresses[<?=$la['address'];?>]" /> <?php echo $la['name']; ?>
261
+ </li>
262
+ <?php endforeach; ?>
263
+ </ul>
264
+ <?php else : ?>
265
+ <input type="hidden" name="addresses[<?=$list_addresses[0];?>]" value="on" />
266
+ <?php endif; ?>
267
+
268
+ <input class="mailgun-list-submit-button" data-form-id="<?php echo $form_class_id; ?>" type="button" value="Subscribe" />
269
+ <input type="hidden" name="mailgun-submission" value="1" />
270
+
271
+ </form>
272
+ <div class="widget-list-panel result-panel" style="display:none;">
273
+ <span>Thank you for subscribing!</span>
274
+ </div>
275
+ </div>
276
+
277
+ <script>
278
+
279
+ jQuery(document).ready(function(){
280
+
281
+ jQuery('.mailgun-list-submit-button').on('click', function() {
282
+
283
+ var form_id = jQuery(this).data('form-id');
284
+
285
+ if(jQuery('.mailgun-list-name').length > 0 && jQuery('.'+form_id+' .mailgun-list-name:checked').length < 1) {
286
+ alert('Please select a list to subscribe to.');
287
+ return;
288
+ }
289
+
290
+ if(jQuery('.'+form_id+' .mailgun-list-widget-name input') && jQuery('.'+form_id+' .mailgun-list-widget-name input').val() === '') {
291
+ alert('Please enter your subscription name.');
292
+ return;
293
+ }
294
+
295
+ if(jQuery('.'+form_id+' .mailgun-list-widget-email input').val() === '') {
296
+ alert('Please enter your subscription email.');
297
+ return;
298
+ }
299
+
300
+ jQuery.ajax({
301
+ url: '<?php echo admin_url('admin-ajax.php?action=add_list'); ?>',
302
+ action:'add_list',
303
+ type: 'post',
304
+ dataType: 'json',
305
+ data: jQuery('.'+form_id+'').serialize(),
306
+ success: function(data) {
307
+
308
+ data_msg = data.message;
309
+ already_exists = false;
310
+ if(data_msg !== undefined){
311
+ already_exists = data_msg.indexOf('Address already exists') > -1;
312
+ }
313
+
314
+ // success
315
+ if((data.status === 200)) {
316
+ jQuery('.<?php echo $widget_class_id; ?> .widget-list-panel').css('display', 'none');
317
+ jQuery('.<?php echo $widget_class_id; ?> .list-form').css('display', 'none');
318
+ jQuery('.<?php echo $widget_class_id; ?> .result-panel').css('display', 'block');
319
+ // error
320
+ } else {
321
+ alert(data_msg);
322
+ }
323
+ }
324
+ });
325
+ });
326
+ });
327
+
328
+ </script>
329
+
330
+ <?php
331
+ }
332
+
333
+ /**
334
+ * Initialize List Form
335
+ *
336
+ * @param array $atts Form attributes
337
+ * @since 0.1
338
+ */
339
+ public function build_list_form($atts) {
340
+
341
+ if(isset($atts['id']) && $atts['id'] != '' ) {
342
+
343
+ $args['widget_id'] = md5(rand(10000, 99999) + $atts['id']);
344
+
345
+ if( isset($atts['collect_name']) ) {
346
+ $args['collect_name'] = true;
347
+ }
348
+
349
+ if( isset($atts['title']) ) {
350
+ $args['list_title'] = $atts['title'];
351
+ }
352
+
353
+ if( isset($atts['description']) ) {
354
+ $args['list_description'] = $atts['description'];
355
+ }
356
+
357
+ ob_start();
358
+ $this->list_form($atts['id'], $args);
359
+ $output_string = ob_get_contents();;
360
+ ob_end_clean();
361
+
362
+ return $output_string;
363
+
364
+ } else {
365
+
366
+ ?>
367
+ <span>Mailgun list ID needed to render form!</span>
368
+ <br />
369
+ <strong>Example :</strong> [mailgun id="[your list id]"]
370
+ <?php
371
+ }
372
+ }
373
+
374
+ /**
375
+ * Initialize List Widget
376
+ *
377
+ * @since 0.1
378
+ */
379
+ public function load_list_widget() {
380
+ register_widget('list_widget');
381
+ add_shortcode('mailgun', array(&$this, 'build_list_form'));
382
+ }
383
+
384
+ }
385
+
386
+ $mailgun = new Mailgun();
387
+
388
+ if ( @include( dirname( __FILE__ ) . '/includes/widget.php' ) ) {
389
+ add_action('widgets_init', array(&$mailgun, 'load_list_widget'));
390
+ add_action('wp_ajax_nopriv_add_list', array(&$mailgun, 'add_list'));
391
+ add_action('wp_ajax_add_list', array(&$mailgun, 'add_list'));
392
  }
393
 
394
  if ( is_admin() ) {
397
  } else {
398
  Mailgun::deactivate_and_die( dirname( __FILE__ ) . '/includes/admin.php' );
399
  }
 
 
400
  }
401
+
402
+
403
+
404
+
readme.txt CHANGED
@@ -1,12 +1,12 @@
1
  === Mailgun for WordPress ===
2
- Contributors: Mailgun, sivel
3
  Tags: mailgun, smtp, http, api, mail, email
4
  Requires at least: 3.3
5
  Tested up to: 3.9
6
- Stable tag: 1.3.1
7
  License: GPLv2 or later
8
 
9
- Easily send email from your WordPress site through Mailgun using the HTTP API or SMTP.
10
 
11
  == Description ==
12
 
@@ -14,13 +14,17 @@ Easily send email from your WordPress site through Mailgun using the HTTP API or
14
 
15
  One particularly useful feature of this plugin is that it provides you with a way to send email when the server you are on does not support SMTP or where outbound SMTP is restricted since the plug-in uses the Mailgun HTTP API for sending email by default. All you need to use the plugin is a [Mailgun account](http://www.mailgun.com/). Mailgun has a free account that lets you send up to 200 emails per day, which is great for testing. Paid subscriptions are available for increased limits.
16
 
17
- The current version of this plugin only handles sending emails. Future releases will include additional functionality around processing incoming emails, tracking and analytics. Until then, you can configure how to handle incoming emails and set up analytics in the Mailgun control panel or using the Mailgun API.
 
 
18
 
19
  == Installation ==
20
 
21
  1. Upload the `mailgun` folder to the `/wp-content/plugins/` directory or install directly through the plugin installer
22
- 1. Activate the plugin through the 'Plugins' menu in WordPress or by using the link provided by the plugin installer
23
- 1. Visit the settings page in the Admin at `Settings -> Mailgun` and configure the plugin with your account details
 
 
24
 
25
  == Frequently Asked Questions ==
26
 
@@ -49,6 +53,10 @@ MAILGUN_SECURE Type: boolean
49
 
50
  1. Configuration options for using the Mailgun HTTP API
51
  2. Configuration options for using the Mailgun SMTP servers
 
 
 
 
52
 
53
  == Upgrade Notice ==
54
 
@@ -62,6 +70,9 @@ Initial Release
62
 
63
  == ChangeLog ==
64
 
 
 
 
65
  = 1.3.1 (2014-11-19): =
66
  * Switched to Semantic Versioning
67
  * Fixed issue with campaigns and tags
@@ -81,4 +92,4 @@ Initial Release
81
  * Re-release to update versioning to start at 1.0 instead of 0.1
82
 
83
  = 0.1 (2012-11-21): =
84
- * Initial Release
1
  === Mailgun for WordPress ===
2
+ Contributors: Mailgun, sivel, lookahead.io, m35dev
3
  Tags: mailgun, smtp, http, api, mail, email
4
  Requires at least: 3.3
5
  Tested up to: 3.9
6
+ Stable tag: 1.4
7
  License: GPLv2 or later
8
 
9
+ Easily send email from your WordPress site through Mailgun using the HTTP API or SMTP. And now, the plugin supports Mailgun List subscription allowing your visitors to subscribe to one or more e-mail lists via widgets or shortcodes.
10
 
11
  == Description ==
12
 
14
 
15
  One particularly useful feature of this plugin is that it provides you with a way to send email when the server you are on does not support SMTP or where outbound SMTP is restricted since the plug-in uses the Mailgun HTTP API for sending email by default. All you need to use the plugin is a [Mailgun account](http://www.mailgun.com/). Mailgun has a free account that lets you send up to 200 emails per day, which is great for testing. Paid subscriptions are available for increased limits.
16
 
17
+ The latest version of this plugin adds support for Mailgun list subscription. Using the shortcode, you can place a form on an article or page to allow the visitor to subscribe to one or more lists. Using the widget, you can provide subscription functionality in sidebars or anywhere widgets are supported e.g. footers.
18
+
19
+ The current version of this plugin only handles sending emails, tracking and tagging and list subscription.
20
 
21
  == Installation ==
22
 
23
  1. Upload the `mailgun` folder to the `/wp-content/plugins/` directory or install directly through the plugin installer
24
+ 2. Activate the plugin through the 'Plugins' menu in WordPress or by using the link provided by the plugin installer
25
+ 3. Visit the settings page in the Admin at `Settings -> Mailgun` and configure the plugin with your account details
26
+ 4. Click the Test Configuration button to verify that your settings are correct.
27
+ 5. Click View Available Lists to review shortcode settings for lists in your Mailgun account that you may wish to help users subscribe to
28
 
29
  == Frequently Asked Questions ==
30
 
53
 
54
  1. Configuration options for using the Mailgun HTTP API
55
  2. Configuration options for using the Mailgun SMTP servers
56
+ 3. Administration option to View Available Lists for subscription
57
+ 4. Setting up a Subscription Widget
58
+ 5. Using a Subscription Code
59
+ 6. Subscription Form Seen By Site Visitors
60
 
61
  == Upgrade Notice ==
62
 
70
 
71
  == ChangeLog ==
72
 
73
+ = 1.4 (2015-11-15): =
74
+ * Added shortcode and widget support for list subscription
75
+
76
  = 1.3.1 (2014-11-19): =
77
  * Switched to Semantic Versioning
78
  * Fixed issue with campaigns and tags
92
  * Re-release to update versioning to start at 1.0 instead of 0.1
93
 
94
  = 0.1 (2012-11-21): =
95
+ * Initial Release