Newsletter Sign-Up - Version 2.0.9

Version Description

  • Dec 5, 2019 =

  • Fix code styling to adhere to WordPress' coding standards

  • Add the plugin to GitHub: ibericode/newsletter-sign-up

  • Update tested WordPress version

Download this release

Release Info

Developer DvanKooten
Plugin Icon wp plugin Newsletter Sign-Up
Version 2.0.9
Comparing to
See all releases

Code changes from version 2.0.8 to 2.0.9

Files changed (85) hide show
  1. includes/NSU.php +0 -294
  2. includes/NSU_Admin.php +0 -325
  3. includes/NewsletterSignUpWidget.php +0 -91
  4. includes/class-nsu-admin.php +326 -0
  5. includes/{NSU_Checkbox.php → class-nsu-checkbox.php} +10 -13
  6. includes/{NSU_Form.php → class-nsu-form.php} +15 -17
  7. includes/class-nsu-widget.php +91 -0
  8. includes/class-nsu.php +371 -0
  9. includes/functions.php +10 -14
  10. includes/index.php +6 -6
  11. includes/views/checkbox-settings.php +62 -0
  12. includes/views/checkbox_settings.php +0 -62
  13. includes/views/config-helper.php +73 -0
  14. includes/views/config_helper.php +0 -73
  15. includes/views/dashboard.php +149 -113
  16. includes/views/form-settings.php +105 -0
  17. includes/views/form_settings.php +0 -90
  18. includes/views/index.php +6 -6
  19. includes/views/parts/index.php +6 -6
  20. includes/views/parts/navigation.php +5 -6
  21. includes/views/parts/rows-aweber.php +7 -3
  22. includes/views/parts/rows-mailchimp.php +59 -23
  23. includes/views/parts/rows-phplist.php +5 -3
  24. includes/views/parts/rows-ymlp.php +31 -11
  25. includes/views/parts/sidebar.php +21 -15
  26. newsletter-sign-up.php +36 -36
  27. phpcs.xml +49 -0
  28. readme.txt +20 -13
  29. trunk/LICENSE +339 -0
  30. trunk/assets/css/admin.css +21 -0
  31. trunk/assets/css/checkbox.css +6 -0
  32. trunk/assets/css/css.php +16 -0
  33. trunk/assets/css/form.css +14 -0
  34. trunk/assets/css/index.php +6 -0
  35. trunk/assets/img/close.png +0 -0
  36. trunk/assets/img/email-icon.png +0 -0
  37. trunk/assets/img/icon.png +0 -0
  38. trunk/assets/img/index.php +6 -0
  39. trunk/assets/img/mailchimp-for-wp-icon.png +0 -0
  40. trunk/assets/img/overlay.png +0 -0
  41. trunk/assets/img/recent-facebook-posts-icon.png +0 -0
  42. trunk/assets/img/rss-icon.png +0 -0
  43. trunk/assets/img/twitter-icon.png +0 -0
  44. trunk/assets/index.php +6 -0
  45. trunk/assets/js/admin.js +34 -0
  46. trunk/assets/js/index.php +6 -0
  47. trunk/includes/class-nsu-admin.php +330 -0
  48. trunk/includes/class-nsu-checkbox.php +242 -0
  49. trunk/includes/class-nsu-form.php +186 -0
  50. trunk/includes/class-nsu-widget.php +92 -0
  51. trunk/includes/class-nsu.php +371 -0
  52. trunk/includes/functions.php +23 -0
  53. trunk/includes/index.php +6 -0
  54. trunk/includes/views/checkbox-settings.php +62 -0
  55. trunk/includes/views/config-helper.php +73 -0
  56. trunk/includes/views/dashboard.php +149 -0
  57. trunk/includes/views/form-settings.php +105 -0
  58. trunk/includes/views/index.php +6 -0
  59. trunk/includes/views/parts/index.php +6 -0
  60. trunk/includes/views/parts/navigation.php +7 -0
  61. trunk/includes/views/parts/rows-aweber.php +12 -0
  62. trunk/includes/views/parts/rows-mailchimp.php +113 -0
  63. trunk/includes/views/parts/rows-phplist.php +12 -0
  64. trunk/includes/views/parts/rows-ymlp.php +57 -0
  65. trunk/includes/views/parts/sidebar.php +35 -0
  66. trunk/index.php +6 -0
  67. trunk/newsletter-sign-up.php +36 -0
  68. trunk/phpcs.xml +49 -0
  69. trunk/readme.txt +235 -0
  70. trunk/vendor/autoload.php +7 -0
  71. trunk/vendor/composer/ClassLoader.php +445 -0
  72. trunk/vendor/composer/LICENSE +21 -0
  73. trunk/vendor/composer/autoload_classmap.php +9 -0
  74. trunk/vendor/composer/autoload_namespaces.php +9 -0
  75. trunk/vendor/composer/autoload_psr4.php +9 -0
  76. trunk/vendor/composer/autoload_real.php +52 -0
  77. trunk/vendor/composer/autoload_static.php +15 -0
  78. vendor/autoload.php +7 -0
  79. vendor/composer/ClassLoader.php +445 -0
  80. vendor/composer/LICENSE +21 -0
  81. vendor/composer/autoload_classmap.php +9 -0
  82. vendor/composer/autoload_namespaces.php +9 -0
  83. vendor/composer/autoload_psr4.php +9 -0
  84. vendor/composer/autoload_real.php +52 -0
  85. vendor/composer/autoload_static.php +15 -0
includes/NSU.php DELETED
@@ -1,294 +0,0 @@
1
- <?php
2
-
3
- class NSU {
4
-
5
- private $options = array();
6
- private static $instance = null;
7
- private static $checkbox = null;
8
- private static $form = null;
9
-
10
- public function __construct() {
11
- self::$instance = $this;
12
-
13
- $opts = $this->get_options();
14
-
15
- self::checkbox();
16
- self::form();
17
-
18
- // widget hooks
19
- add_action( 'widgets_init', array( $this, 'register_widget' ) );
20
-
21
- // check if this is an AJAX request
22
- if ( !defined( "DOING_AJAX" ) || !DOING_AJAX ) {
23
-
24
- if ( is_admin() ) {
25
-
26
- // backend only
27
- require_once NSU_PLUGIN_DIR . '/includes/NSU_Admin.php';
28
- new NSU_Admin();
29
-
30
- } else {
31
-
32
- // frontend only
33
- require_once NSU_PLUGIN_DIR . '/includes/functions.php';
34
-
35
- add_action( 'wp_enqueue_scripts', array( $this, 'load_stylesheets' ) );
36
- add_action( 'login_enqueue_scripts', array( $this, 'load_stylesheets' ) );
37
-
38
- }
39
- }
40
- }
41
-
42
- public static function checkbox() {
43
- if ( !self::$checkbox ) {
44
- require_once NSU_PLUGIN_DIR .'/includes/NSU_Checkbox.php';
45
- self::$checkbox = new NSU_Checkbox;
46
- }
47
-
48
- return self::$checkbox;
49
- }
50
-
51
- public static function form() {
52
- if ( !self::$form ) {
53
- require_once NSU_PLUGIN_DIR . '/includes/NSU_Form.php';
54
- self::$form = new NSU_Form;
55
- }
56
-
57
- return self::$form;
58
- }
59
-
60
-
61
- /**
62
- * Initalize options
63
- *
64
- * @return array $options
65
- */
66
- public function get_options() {
67
- if ( empty( $this->options ) ) {
68
- $keys = array( 'form', 'mailinglist', 'checkbox' );
69
-
70
- $defaults = array(
71
- 'form' => array( 'load_form_css' => 0, 'submit_button' => 'Sign up',
72
- 'name_label' => 'Name:', 'email_label' => "Email:", 'email_default_value' => 'Your emailaddress..', 'name_required' => 0, 'name_default_value' => 'Your name..', 'wpautop' => 0,
73
- 'text_after_signup' => 'Thanks for signing up to our newsletter. Please check your inbox to confirm your email address.', 'redirect_to' => '',
74
- 'text_empty_name' => 'Please fill in the name field.', 'text_empty_email' => 'Please fill in the email field.', 'text_invalid_email' => 'Please enter a valid email address.'
75
- ),
76
- 'mailinglist' => array( 'provider' => '', 'use_api' => 0, 'subscribe_with_name' => 0, 'email_id' => '', 'name_id' => '', 'form_action' => '' ),
77
- 'checkbox' => array( 'text' => 'Sign me up for the newsletter', 'redirect_to' => '', 'precheck' => 0, 'cookie_hide' => 0, 'css_reset' => 0,
78
- 'add_to_registration_form' => 0, 'add_to_comment_form' => 1, 'add_to_buddypress_form' => 0,
79
- 'add_to_multisite_form' => 0, 'add_to_bbpress_forms' => 0
80
- )
81
- );
82
-
83
- foreach ( $keys as $key ) {
84
- if ( ( $option = get_option( 'nsu_'. $key ) ) == false ) {
85
- add_option( 'nsu_'. $key, $defaults[$key] );
86
- }
87
-
88
- $this->options[$key] = array_merge( $defaults[$key], (array) $option );
89
- }
90
-
91
- }
92
-
93
- return $this->options;
94
- }
95
-
96
- /**
97
- * Registers the Newsletter Sign-Up Widget
98
- */
99
- public function register_widget() {
100
- require_once NSU_PLUGIN_DIR . '/includes/NewsletterSignUpWidget.php';
101
- register_widget( 'NewsletterSignUpWidget' );
102
- }
103
-
104
- /**
105
- * Factory method for NewsletterSignUp class. Only instantiate once.
106
- *
107
- * @return NSU Instance of Newsletter Sign-Up class
108
- */
109
- public static function instance() {
110
- if ( !self::$instance ) self::$instance = new NSU();
111
-
112
- return self::$instance;
113
- }
114
-
115
- public function load_stylesheets() {
116
- $opts = $this->get_options();
117
-
118
- $stylesheets = array();
119
- if ( $opts['checkbox']['css_reset'] == 1 ) { $stylesheets['checkbox'] = 1; }
120
- if ( $opts['form']['load_form_css'] == 1 ) { $stylesheets['form'] = 1; }
121
-
122
- if ( !empty( $stylesheets ) ) {
123
- $stylesheet_url = add_query_arg( $stylesheets, plugins_url( "/assets/css/css.php", dirname( __FILE__ ) ) );
124
- wp_enqueue_style( 'newsletter-sign-up', $stylesheet_url );
125
- }
126
-
127
- }
128
-
129
-
130
-
131
-
132
- /**
133
- * Send the post data to the newsletter service, mimic form request
134
- */
135
- function send_post_data( $email, $name = '', $type = 'checkbox' ) {
136
- $opts = $this->options['mailinglist'];
137
-
138
- // when not using api and no form action has been given, abandon.
139
- if ( empty( $opts['use_api'] ) && empty( $opts['form_action'] ) ) return;
140
-
141
- /* Are we using API? */
142
- if ( $opts['use_api'] == 1 ) {
143
-
144
- switch ( $opts['provider'] ) {
145
-
146
- /* Send data using the YMLP API */
147
- case 'ymlp':
148
- $data = array(
149
- 'key' => $opts['ymlp_api_key'],
150
- 'username' => $opts['ymlp_username'],
151
- 'Email' => $email,
152
- 'GroupId' => $opts['ymlp_groupid'],
153
- 'output' => 'JSON'
154
- );
155
-
156
- $data = array_merge( $data, $this->add_additional_data( array( 'api' => 'ymlp' ) ) );
157
- $data = http_build_query( $data );
158
- $url = 'https://www.ymlp.com/api/Contacts.Add?'.$data;
159
-
160
- $result = wp_remote_post( $url );
161
-
162
- break;
163
-
164
- /* Send data using the MailChimp API */
165
- case 'mailchimp':
166
- $request = array(
167
- 'apikey' => $opts['mc_api_key'],
168
- 'id' => $opts['mc_list_id'],
169
- 'email_address' => $email,
170
- 'double_optin' => ( isset( $opts['mc_no_double_optin'] ) && $opts['mc_no_double_optin'] == 1 ) ? FALSE : TRUE,
171
- 'merge_vars' => array(
172
- 'OPTIN_TIME' => date( 'Y-M-D H:i:s' )
173
- )
174
- );
175
-
176
- if ( isset( $opts['mc_use_groupings'] ) && $opts['mc_use_groupings'] == 1 && !empty( $opts['mc_groupings_name'] ) ) {
177
- $request['merge_vars']['GROUPINGS'] = array(
178
- array( 'name' => $opts['mc_groupings_name'], 'groups' => $opts['mc_groupings_groups'] )
179
- );
180
- }
181
-
182
- /* Subscribe with name? If so, add name to merge_vars array */
183
- if ( isset( $opts['subscribe_with_name'] ) && $opts['subscribe_with_name'] == 1 ) {
184
- // Try to provide values for First and Lastname fields
185
- // These can be overridden, of just ignored by mailchimp.
186
- $strpos = strpos( $name, ' ' );
187
-
188
- $request['merge_vars']['FNAME'] = $name;
189
-
190
- if ( $strpos ) {
191
- $request['merge_vars']['FNAME'] = substr( $name, 0, $strpos );
192
- $request['merge_vars']['LNAME'] = substr( $name, $strpos );
193
- } else {
194
- $request['merge_vars']['FNAME'] = $name;
195
- }
196
-
197
- $request['merge_vars'][$opts['name_id']] = $name;
198
- }
199
-
200
- // Add any set additional data to merge_vars array
201
- $request['merge_vars'] = array_merge( $request['merge_vars'], $this->add_additional_data( array( 'email' => $email, 'name' => $name ) ) );
202
-
203
- $result = wp_remote_post(
204
- 'https://'.substr( $opts['mc_api_key'], -3 ).'.api.mailchimp.com/1.3/?output=php&method=listSubscribe',
205
- array( 'body' => json_encode( $request ) )
206
- );
207
-
208
- break;
209
-
210
- }
211
-
212
- } else {
213
- /* We are not using API, mimic a normal form request */
214
-
215
- $post_data = array(
216
- $opts['email_id'] => $email,
217
- );
218
-
219
- // Subscribe with name? Add to $post_data array.
220
- if ( $opts['subscribe_with_name'] == 1 ) $post_data[$opts['name_id']] = $name;
221
-
222
- // Add list specific data
223
- switch ( $opts['provider'] ) {
224
-
225
- case 'aweber':
226
- $post_data['listname'] = $opts['aweber_list_name'];
227
- $post_data['redirect'] = get_bloginfo( 'wpurl' );
228
- $post_data['meta_message'] = '1';
229
- $post_data['meta_required'] = 'email';
230
- break;
231
-
232
- case 'phplist':
233
- $post_data['list['.$opts['phplist_list_id'].']'] = 'signup';
234
- $post_data['subscribe'] = "Subscribe";
235
- $post_data["htmlemail"] = "1";
236
- $post_data['emailconfirm'] = $email;
237
- $post_data['makeconfirmed']='0';
238
- break;
239
-
240
- }
241
-
242
- $post_data = array_merge( $post_data, $this->add_additional_data( array_merge( array( 'email' => $email, 'name' => $name ), $post_data ) ) );
243
-
244
- $result = wp_remote_post( $opts['form_action'],
245
- array( 'body' => $post_data )
246
- );
247
-
248
- }
249
-
250
- // store a cookie, if preferred by site owner
251
- if ( $type == 'checkbox' && $this->options['checkbox']['cookie_hide'] == 1 ) @setcookie( 'ns_subscriber', TRUE, time() + 9999999 );
252
-
253
- // Check if we should redirect to a given page
254
- if ( $type == 'form' && strlen( $this->options['form']['redirect_to'] ) > 6 ) {
255
- wp_redirect( $this->options['form']['redirect_to'] );
256
- exit;
257
- } elseif ( $type == 'checkbox' && strlen( $this->options['checkbox']['redirect_to'] ) > 6 ) {
258
- wp_redirect( $this->options['checkbox']['redirect_to'] );
259
- exit;
260
- }
261
-
262
- return true;
263
- }
264
-
265
-
266
- /**
267
- * Returns array with additional data names as key, values as value.
268
- *
269
- * @param array $args, the normal form data (name, email, list variables)
270
- * @return array
271
- */
272
- function add_additional_data( $args = array() ) {
273
- $opts = $this->options['mailinglist'];
274
- $defaults = array(
275
- 'format' => 'array',
276
- 'api' => NULL
277
- );
278
-
279
- $args = wp_parse_args( $args, $defaults );
280
-
281
- $add_data = array();
282
- if ( isset( $opts['extra_data'] ) && is_array( $opts['extra_data'] ) ) {
283
- foreach ( $opts['extra_data'] as $key => $value ) {
284
- if ( $args['api'] == 'ymlp' ) $value['name'] = str_replace( 'YMP', 'Field', $value['name'] );
285
- $value['value'] = str_replace( "%%NAME%%", $args['name'], $value['value'] );
286
- $value['value'] = str_replace( "%%IP%%", $_SERVER['REMOTE_ADDR'], $value['value'] );
287
- $add_data[$value['name']] = $value['value'];
288
- }
289
- }
290
-
291
- return $add_data;
292
- }
293
-
294
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/NSU_Admin.php DELETED
@@ -1,325 +0,0 @@
1
- <?php
2
- if (!class_exists('NSU_Admin')) {
3
-
4
- class NSU_Admin {
5
-
6
- private $hook = 'newsletter-sign-up';
7
- private $longname = 'Newsletter Sign-Up';
8
- private $shortname = 'Newsletter Sign-Up';
9
- private $plugin_url = 'https://dannyvankooten.com/wordpress-plugins/newsletter-sign-up/';
10
- private $filename = 'newsletter-sign-up/newsletter-sign-up.php';
11
- private $accesslvl = 'manage_options';
12
- private $bp_active = FALSE;
13
- private $options = array();
14
-
15
- public function __construct() {
16
- $this->options = NSU::instance()->get_options();
17
-
18
- add_filter("plugin_action_links_{$this->filename}", array($this, 'add_settings_link'));
19
- add_action('admin_menu', array($this, 'add_option_page'));
20
- add_action('admin_init', array($this, 'settings_init'));
21
- add_action( 'admin_enqueue_scripts', array($this, 'load_css_and_js') );
22
- add_action('bp_include', array($this, 'set_bp_active'));
23
-
24
- if(isset($_GET['nsu-hide-mc4wp-notice'])) {
25
- add_option("nsu_hide_mc4wp_notice", true, false);
26
- } elseif($this->options['mailinglist']['provider'] == 'mailchimp' && get_option('nsu_hide_mc4wp_notice') == false) {
27
- add_action( 'admin_notices', array($this, 'notice_mailchimp_for_wp'));
28
- }
29
-
30
-
31
- }
32
-
33
- public function get_checkbox_compatible_plugins()
34
- {
35
-
36
- $checkbox_plugins = array(
37
- 'comment_form' => "Comment form",
38
- "registration_form" => "Registration form"
39
- );
40
- if(is_multisite()) { $checkbox_plugins['ms_form'] = "MultiSite forms"; }
41
- if(class_exists("BuddyPress")) $checkbox_plugins['bp_form'] = "BuddyPress registration";
42
- if(class_exists('bbPress')) $checkbox_plugins['bbpress_forms'] = "bbPress";
43
- return $checkbox_plugins;
44
- }
45
-
46
- public function notice_mailchimp_for_wp()
47
- {
48
- ?>
49
- <div class="updated">
50
- <p><strong>Newsletter Sign-Up Notice:</strong> You are using MailChimp, great! Please consider switching to our improved <a href="https://mc4wp.com/">MailChimp for WordPress</a> plugin.
51
- It can be downloaded from the WordPress repository <a href="https://wordpress.org/plugins/mailchimp-for-wp/">here</a>. | <a href="?nsu-hide-mc4wp-notice=1">Hide Notice</a></p>
52
- </div>
53
- <?php
54
- }
55
-
56
- public function load_css_and_js($hook)
57
- {
58
- if(!stripos($hook, $this->hook)) { return false; }
59
-
60
- wp_enqueue_style($this->hook, plugins_url('newsletter-sign-up/assets/css/admin.css'));
61
- wp_enqueue_script($this->hook, plugins_url('newsletter-sign-up/assets/js/admin.js'), array('jquery'));
62
- }
63
-
64
- /**
65
- * If buddypress is loaded, set buddypress_active to TRUE
66
- */
67
- public function set_bp_active() {
68
- $this->bp_active = TRUE;
69
- }
70
-
71
- /**
72
- * The default settings page
73
- */
74
- public function options_page_default() {
75
- $tab = 'mailinglist-settings';
76
- $opts = $this->options['mailinglist'];
77
-
78
- $viewed_mp = NULL;
79
- if (!empty($_GET['mp']))
80
- $viewed_mp = $_GET['mp'];
81
- elseif (empty($_GET['mp']) && isset($opts['provider']))
82
- $viewed_mp = $opts['provider'];
83
- if (!in_array($viewed_mp, array('mailchimp', 'icontact', 'aweber', 'phplist', 'ymlp', 'other')))
84
- $viewed_mp = NULL;
85
-
86
- // Fill in some predefined values if options not set or set for other newsletter service
87
- if ($opts['provider'] != $viewed_mp) {
88
- switch ($viewed_mp) {
89
-
90
- case 'mailchimp':
91
- if (empty($opts['email_id']))
92
- $opts['email_id'] = 'EMAIL';
93
- if (empty($opts['name_id']))
94
- $opts['name_id'] = 'NAME';
95
- break;
96
-
97
- case 'ymlp':
98
- if (empty($opts['email_id']))
99
- $opts['email_id'] = 'YMP0';
100
- break;
101
-
102
- case 'aweber':
103
- if (empty($opts['form_action']))
104
- $opts['form_action'] = 'http://www.aweber.com/scripts/addlead.pl';
105
- if (empty($opts['email_id']))
106
- $opts['email_id'] = 'email';
107
- if (empty($opts['name_id']))
108
- $opts['name_id'] = 'name';
109
- break;
110
-
111
- case 'icontact':
112
- if (empty($opts['email_id']))
113
- $opts['email_id'] = 'fields_email';
114
- break;
115
- }
116
- }
117
-
118
- require_once NSU_PLUGIN_DIR . '/includes/views/dashboard.php';
119
- }
120
-
121
- /**
122
- * The admin page for managing checkbox settings
123
- */
124
- public function options_page_checkbox_settings() {
125
- $tab = 'checkbox-settings';
126
- $opts = $this->options['checkbox'];
127
- require_once NSU_PLUGIN_DIR . '/includes/views/checkbox_settings.php';
128
- }
129
-
130
- /**
131
- * The admin page for managing form settings
132
- */
133
- public function options_page_form_settings() {
134
- $tab = 'form-settings';
135
- $opts = $this->options['form'];
136
- $opts['mailinglist'] = $this->options['mailinglist'];
137
- require_once NSU_PLUGIN_DIR . '/includes/views/form_settings.php';
138
- }
139
-
140
- /**
141
- * The page for the configuration extractor
142
- */
143
- public function options_page_config_helper() {
144
- $tab = 'config-helper';
145
-
146
- if ( isset( $_POST['form'] ) && ! empty( $_POST['form'] ) ) {
147
-
148
- $result = $this->extract_form_config( $_POST['form'] );
149
-
150
- }
151
-
152
- require_once NSU_PLUGIN_DIR . '/includes/views/config_helper.php';
153
- }
154
-
155
- private function extract_form_config( $form_html ) {
156
-
157
- // strip unneccessary tags
158
- $form = stripslashes( strip_tags($form_html, '<form><label><input><select><textarea><button>') );
159
-
160
- // set defaults
161
- $form_action = '';
162
- $email_name = 'Sorry, can\'t help you with this one.';
163
- $name_name = 'Sorry, can\'t help you with this one.';
164
- $additional_data = array();
165
-
166
- preg_match_all("'<(.*?)>'si", $form, $matches);
167
-
168
- if( is_array( $matches ) && isset($matches[0])) {
169
- $matches = $matches[0];
170
- $html = join('', $matches);
171
- } else {
172
- $html = $form;
173
- }
174
-
175
- // fake wrap in html and body tags
176
- $html = '<html><body>' . $html . '</body></html>';
177
-
178
- $doc = new DOMDocument();
179
- $doc->strictErrorChecking = false;
180
- $doc->loadHTML( $html);
181
-
182
- $xml = simplexml_import_dom($doc);
183
-
184
- if ( is_object( $xml ) ) {
185
- $form = $xml->body->form;
186
-
187
- if ($form) {
188
-
189
- $form_action = (isset($form['action'])) ? $form['action'] : 'Can\'t help you on this one..';
190
-
191
-
192
- if ( $form->input ) {
193
-
194
- /* Loop trough input fields */
195
- foreach ($form->input as $input) {
196
-
197
-
198
- // Check if this is a hidden field
199
- if ($input['type'] == 'hidden') {
200
- $additional_data[] = array($input['name'], $input['value']);
201
- // Check if this is the input field that is supposed to hold the EMAIL data
202
- } elseif (stripos($input['id'], 'email') !== FALSE || stripos($input['name'], 'email') !== FALSE) {
203
- $email_name = $input['name'];
204
-
205
- // Check if this is the input field that is supposed to hold the NAME data
206
- } elseif (stripos($input['id'], 'name') !== FALSE || stripos($input['name'], 'name') !== FALSE) {
207
- $name_name = $input['name'];
208
- }
209
-
210
- // remove input attributes
211
- unset($input['id']);
212
- unset($input['class']);
213
- unset($input['style']);
214
- unset($input['onfocus']);
215
- unset($input['onblur']);
216
- unset($input['size']);
217
- }
218
- }
219
-
220
- // remove form attributes
221
- unset($form['target']);
222
- unset($form['id']);
223
- unset($form['name']);
224
- unset($form['class']);
225
- unset($form['onsubmit']);
226
- unset($form['enctype']);
227
- }
228
-
229
- }
230
-
231
- $doc->removeChild($doc->firstChild);
232
- $doc->replaceChild($doc->firstChild->firstChild->firstChild, $doc->firstChild);
233
-
234
- $simpler_form = $doc->saveHTML();
235
-
236
- // add tabs for improved readability
237
- $simpler_form = str_replace(array('><', '<input'), array(">\n<", "\t<input"), $simpler_form);
238
-
239
-
240
- return array(
241
- 'simpler_form' => $simpler_form,
242
- 'form_action' => $form_action,
243
- 'email_name' => $email_name,
244
- 'name_name' => $name_name,
245
- 'additional_data' => $additional_data
246
- );
247
- }
248
-
249
-
250
- /**
251
- * Adds the different menu pages
252
- */
253
- public function add_option_page() {
254
- add_menu_page($this->longname, "Newsl. Sign-up", $this->accesslvl, $this->hook, array($this, 'options_page_default'), plugins_url('newsletter-sign-up/assets/img/icon.png'));
255
- add_submenu_page($this->hook, "Newsletter Sign-Up :: Mailinglist Settings", "List Settings", $this->accesslvl, $this->hook, array($this, 'options_page_default'));
256
- add_submenu_page($this->hook, "Newsletter Sign-Up :: Checkbox Settings", "Checkbox Settings", $this->accesslvl, $this->hook . '-checkbox-settings', array($this, 'options_page_checkbox_settings'));
257
- add_submenu_page($this->hook, "Newsletter Sign-Up :: Form Settings", "Form Settings", $this->accesslvl, $this->hook . '-form-settings', array($this, 'options_page_form_settings'));
258
- add_submenu_page($this->hook, "Newsletter Sign-Up :: Configuration Extractor", "Config Extractor", $this->accesslvl, $this->hook . '-config-helper', array($this, 'options_page_config_helper'));
259
- }
260
-
261
- /**
262
- * Adds the settings link on the plugin's overview page
263
- * @param array $links Array containing all the settings links for the various plugins.
264
- * @return array The new array containing all the settings links
265
- */
266
- public function add_settings_link($links) {
267
- $url = admin_url( 'admin.php?page='.$this->hook);
268
- $settings_link = '<a href="'. $url . '">Settings</a>';
269
- array_unshift($links, $settings_link);
270
- return $links;
271
- }
272
-
273
- /**
274
- * Registers the settings using WP Settings API.
275
- */
276
- public function settings_init() {
277
- register_setting('nsu_form_group', 'nsu_form', array($this, 'validate_form_options'));
278
- register_setting('nsu_mailinglist_group', 'nsu_mailinglist', array($this, 'validate_mailinglist_options'));
279
- register_setting('nsu_checkbox_group', 'nsu_checkbox', array($this, 'validate_checkbox_options'));
280
- }
281
-
282
- /**
283
- * Validate the submitted options
284
- * @param array $options The submitted options
285
- * @return array
286
- */
287
- public function validate_options($options) {
288
- return $options;
289
- }
290
-
291
- public function validate_form_options($options) {
292
-
293
- $options['text_after_signup'] = strip_tags($options['text_after_signup'], '<a><b><strong><i><img><em><br><p><ul><li><ol>');
294
-
295
- // redirect to url should start with http
296
- if(isset($options['redirect_to']) && substr($options['redirect_to'],0,4) != 'http') {
297
- $options['redirect_to'] = '';
298
- }
299
-
300
- $options['name_required'] = (isset($options['name_required'])) ? 1 : 0;
301
- $options['wpautop'] = (isset($options['wpautop'])) ? 1 : 0;
302
- $options['load_form_css'] = (isset($options['load_form_css'])) ? 1 : 0;
303
-
304
- return $options;
305
- }
306
-
307
- public function validate_mailinglist_options($options) {
308
- if (is_array($options['extra_data'])) {
309
- foreach ($options['extra_data'] as $key => $value) {
310
- if (empty($value['name']))
311
- unset($options['extra_data'][$key]);
312
- }
313
- }
314
-
315
- return $options;
316
- }
317
-
318
- public function validate_checkbox_options($options) {
319
- $options['add_to_comment_form'] = (isset($options['add_to_comment_form'])) ? 1 : 0;
320
- return $options;
321
- }
322
-
323
- }
324
-
325
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/NewsletterSignUpWidget.php DELETED
@@ -1,91 +0,0 @@
1
- <?php
2
-
3
- if(!class_exists('NewsletterSignUpWidget')) {
4
-
5
- class NewsletterSignUpWidget extends WP_Widget {
6
-
7
- function __construct() {
8
- $widget_ops = array('classname' => 'nsu_widget', 'description' => __('Displays a newsletter sign-up form.'));
9
- $control_ops = array('width' => 400, 'height' => 350);
10
- parent::__construct(false, 'Newsletter Sign-Up', $widget_ops, $control_ops);
11
- }
12
-
13
- function widget($args, $instance) {
14
-
15
- /* Get Newsletter Sign-up options */
16
- $options = get_option('nsu_form');
17
-
18
- /* Provide some defaults */
19
- $defaults = array( 'title' => 'Sign up for our newsletter!', 'text_before_form' => '', 'text_after_form' => '');
20
- $instance = wp_parse_args( (array) $instance, $defaults );
21
-
22
- extract( $args );
23
- extract($instance);
24
- $title = apply_filters('widget_title', $title);
25
-
26
- echo $before_widget;
27
- echo $before_title . $title . $after_title;
28
-
29
- if(!empty($text_before_form)) {
30
- ?><div class="nsu-text-before-form"><?php
31
- $instance['filter'] ? _e(wpautop($text_before_form),'nsu-widget') : _e($text_before_form,'nsu-widget');
32
- ?></div><?php
33
- }
34
-
35
- NSU::form()->output_form(true);
36
-
37
- if(!empty($text_after_form)) {
38
- ?><div class="nsu-text-after-form"><?php
39
- $instance['filter'] ? _e(wpautop($text_after_form),'nsu-widget') : _e($text_after_form,'nsu-widget');
40
- ?></div><?php
41
- }
42
-
43
- echo $after_widget;
44
- }
45
-
46
- function update($new_instance, $old_instance) {
47
- $instance = $old_instance;
48
- $instance['title'] = strip_tags($new_instance['title']);
49
-
50
- if ( current_user_can('unfiltered_html') ) {
51
- $instance['text_before_form'] = $new_instance['text_before_form'];
52
- $instance['text_after_form'] = $new_instance['text_after_form'];
53
- } else {
54
- $instance['text_before_form'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text_before_form']) ) );
55
- $instance['text_after_form'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text_after_form']) ) );
56
- }
57
- $instance['filter'] = isset($new_instance['filter']);
58
-
59
- return $instance;
60
- }
61
-
62
- function form($instance) {
63
- $defaults = array( 'title' => 'Sign up for our newsletter!', 'text_before_form' => '', 'text_after_form' => '');
64
- $instance = wp_parse_args( (array) $instance, $defaults );
65
-
66
- extract($instance);
67
- $title = strip_tags($title);
68
-
69
- ?>
70
-
71
- <p>
72
- <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
73
- <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
74
- </p>
75
-
76
- <label title="You can use the following HTML-codes: &lt;a&gt;, &lt;strong&gt;, &lt;br /&gt;,&lt;em&gt; &lt;img ..&gt;" for="<?php echo $this->get_field_id('text_before_form'); ?>"><?php _e('Text to show before the form:'); ?></label>
77
- <textarea rows="8" cols="10" class="widefat wysiwyg-overlay-toggle" id="<?php echo $this->get_field_id('text_before_form'); ?>" name="<?php echo $this->get_field_name('text_before_form'); ?>"><?php echo $text_before_form; ?></textarea>
78
- <br />
79
- <label for="<?php echo $this->get_field_id('text_after_form'); ?>"><?php _e('Text to show after the form:'); ?></label>
80
- <textarea rows="8" cols="10" class="widefat wysiwyg-overlay-toggle" id="<?php echo $this->get_field_id('text_after_form'); ?>" name="<?php echo $this->get_field_name('text_after_form'); ?>"><?php echo $text_after_form; ?></textarea>
81
-
82
- <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
83
-
84
- <p>
85
- Configure the sign-up form at the <a href="<?php admin_url( 'admin.php?page=newsletter-sign-up-form-settings'); ?>">Newsletter Sign-Up configuration page</a>.
86
- </p>
87
- <?php
88
- }
89
-
90
- }
91
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/class-nsu-admin.php ADDED
@@ -0,0 +1,326 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class NSU_Admin {
4
+
5
+ private $hook = 'newsletter-sign-up';
6
+ private $longname = 'Newsletter Sign-Up';
7
+ private $filename = 'newsletter-sign-up/newsletter-sign-up.php';
8
+ private $accesslvl = 'manage_options';
9
+ private $bp_active = false;
10
+ private $options = array();
11
+
12
+ public function __construct() {
13
+ $this->options = NSU::instance()->get_options();
14
+
15
+ add_filter( "plugin_action_links_{$this->filename}", array( $this, 'add_settings_link' ) );
16
+ add_action( 'admin_menu', array( $this, 'add_option_page' ) );
17
+ add_action( 'admin_init', array( $this, 'settings_init' ) );
18
+ add_action( 'admin_enqueue_scripts', array( $this, 'load_css_and_js' ) );
19
+ add_action( 'bp_include', array( $this, 'set_bp_active' ) );
20
+
21
+ if ( isset( $_GET['nsu-hide-mc4wp-notice'] ) ) {
22
+ add_option( 'nsu_hide_mc4wp_notice', true, false );
23
+ } elseif ( $this->options['mailinglist']['provider'] == 'mailchimp' && get_option( 'nsu_hide_mc4wp_notice' ) == false ) {
24
+ add_action( 'admin_notices', array( $this, 'notice_mailchimp_for_wp' ) );
25
+ }
26
+
27
+ }
28
+
29
+ public function get_checkbox_compatible_plugins() {
30
+ $checkbox_plugins = array(
31
+ 'comment_form' => 'Comment form',
32
+ 'registration_form' => 'Registration form',
33
+ );
34
+ if ( is_multisite() ) {
35
+ $checkbox_plugins['ms_form'] = 'MultiSite forms'; }
36
+ if ( class_exists( 'BuddyPress' ) ) {
37
+ $checkbox_plugins['bp_form'] = 'BuddyPress registration';
38
+ }
39
+ if ( class_exists( 'bbPress' ) ) {
40
+ $checkbox_plugins['bbpress_forms'] = 'bbPress';
41
+ }
42
+ return $checkbox_plugins;
43
+ }
44
+
45
+ public function notice_mailchimp_for_wp() { ?>
46
+ <div class="updated">
47
+ <p><strong>Newsletter Sign-Up Notice:</strong> You are using Mailchimp, great! Please consider switching to our improved <a href="https://mc4wp.com/">Mailchimp for WordPress</a> plugin.
48
+ It can be downloaded from the WordPress repository <a href="https://wordpress.org/plugins/mailchimp-for-wp/">here</a>. | <a href="?nsu-hide-mc4wp-notice=1">Hide Notice</a></p>
49
+ </div>
50
+ <?php
51
+ }
52
+
53
+ public function load_css_and_js( $hook ) {
54
+ if ( ! stripos( $hook, $this->hook ) ) {
55
+ return false; }
56
+
57
+ wp_enqueue_style( $this->hook, plugins_url( 'newsletter-sign-up/assets/css/admin.css' ) );
58
+ wp_enqueue_script( $this->hook, plugins_url( 'newsletter-sign-up/assets/js/admin.js' ), array( 'jquery' ) );
59
+ }
60
+
61
+ /**
62
+ * If buddypress is loaded, set buddypress_active to TRUE
63
+ */
64
+ public function set_bp_active() {
65
+ $this->bp_active = true;
66
+ }
67
+
68
+ /**
69
+ * The default settings page
70
+ */
71
+ public function options_page_default() {
72
+ $tab = 'mailinglist-settings';
73
+ $opts = $this->options['mailinglist'];
74
+
75
+ $viewed_mp = null;
76
+ if ( ! empty( $_GET['mp'] ) ) {
77
+ $viewed_mp = $_GET['mp'];
78
+ } elseif ( empty( $_GET['mp'] ) && isset( $opts['provider'] ) ) {
79
+ $viewed_mp = $opts['provider'];
80
+ }
81
+ if ( ! in_array( $viewed_mp, array( 'mailchimp', 'icontact', 'aweber', 'phplist', 'ymlp', 'other' ) ) ) {
82
+ $viewed_mp = null;
83
+ }
84
+
85
+ // Fill in some predefined values if options not set or set for other newsletter service
86
+ if ( $opts['provider'] != $viewed_mp ) {
87
+ switch ( $viewed_mp ) {
88
+
89
+ case 'mailchimp':
90
+ if ( empty( $opts['email_id'] ) ) {
91
+ $opts['email_id'] = 'EMAIL';
92
+ }
93
+ if ( empty( $opts['name_id'] ) ) {
94
+ $opts['name_id'] = 'NAME';
95
+ }
96
+ break;
97
+
98
+ case 'ymlp':
99
+ if ( empty( $opts['email_id'] ) ) {
100
+ $opts['email_id'] = 'YMP0';
101
+ }
102
+ break;
103
+
104
+ case 'aweber':
105
+ if ( empty( $opts['form_action'] ) ) {
106
+ $opts['form_action'] = 'http://www.aweber.com/scripts/addlead.pl';
107
+ }
108
+ if ( empty( $opts['email_id'] ) ) {
109
+ $opts['email_id'] = 'email';
110
+ }
111
+ if ( empty( $opts['name_id'] ) ) {
112
+ $opts['name_id'] = 'name';
113
+ }
114
+ break;
115
+
116
+ case 'icontact':
117
+ if ( empty( $opts['email_id'] ) ) {
118
+ $opts['email_id'] = 'fields_email';
119
+ }
120
+ break;
121
+ }
122
+ }
123
+
124
+ require_once NSU_PLUGIN_DIR . '/includes/views/dashboard.php';
125
+ }
126
+
127
+ /**
128
+ * The admin page for managing checkbox settings
129
+ */
130
+ public function options_page_checkbox_settings() {
131
+ $tab = 'checkbox-settings';
132
+ $opts = $this->options['checkbox'];
133
+ require_once NSU_PLUGIN_DIR . '/includes/views/checkbox-settings.php';
134
+ }
135
+
136
+ /**
137
+ * The admin page for managing form settings
138
+ */
139
+ public function options_page_form_settings() {
140
+ $tab = 'form-settings';
141
+ $opts = $this->options['form'];
142
+ $opts['mailinglist'] = $this->options['mailinglist'];
143
+ require_once NSU_PLUGIN_DIR . '/includes/views/form-settings.php';
144
+ }
145
+
146
+ /**
147
+ * The page for the configuration extractor
148
+ */
149
+ public function options_page_config_helper() {
150
+ $tab = 'config-helper';
151
+
152
+ if ( isset( $_POST['form'] ) && ! empty( $_POST['form'] ) ) {
153
+
154
+ $result = $this->extract_form_config( $_POST['form'] );
155
+
156
+ }
157
+
158
+ require_once NSU_PLUGIN_DIR . '/includes/views/config-helper.php';
159
+ }
160
+
161
+ private function extract_form_config( $form_html ) {
162
+
163
+ // strip unneccessary tags
164
+ $form = stripslashes( strip_tags( $form_html, '<form><label><input><select><textarea><button>' ) );
165
+
166
+ // set defaults
167
+ $form_action = '';
168
+ $email_name = 'Sorry, can\'t help you with this one.';
169
+ $name_name = 'Sorry, can\'t help you with this one.';
170
+ $additional_data = array();
171
+
172
+ preg_match_all( "'<(.*?)>'si", $form, $matches );
173
+
174
+ if ( is_array( $matches ) && isset( $matches[0] ) ) {
175
+ $matches = $matches[0];
176
+ $html = join( '', $matches );
177
+ } else {
178
+ $html = $form;
179
+ }
180
+
181
+ // fake wrap in html and body tags
182
+ $html = '<html><body>' . $html . '</body></html>';
183
+
184
+ $doc = new DOMDocument();
185
+ $doc->strictErrorChecking = false;
186
+ $doc->loadHTML( $html );
187
+
188
+ $xml = simplexml_import_dom( $doc );
189
+
190
+ if ( is_object( $xml ) ) {
191
+ $form = $xml->body->form;
192
+
193
+ if ( $form ) {
194
+
195
+ $form_action = ( isset( $form['action'] ) ) ? $form['action'] : 'Can\'t help you on this one..';
196
+
197
+ if ( $form->input ) {
198
+
199
+ /* Loop trough input fields */
200
+ foreach ( $form->input as $input ) {
201
+
202
+ // Check if this is a hidden field
203
+ if ( $input['type'] == 'hidden' ) {
204
+ $additional_data[] = array( $input['name'], $input['value'] );
205
+ // Check if this is the input field that is supposed to hold the EMAIL data
206
+ } elseif ( stripos( $input['id'], 'email' ) !== false || stripos( $input['name'], 'email' ) !== false ) {
207
+ $email_name = $input['name'];
208
+
209
+ // Check if this is the input field that is supposed to hold the NAME data
210
+ } elseif ( stripos( $input['id'], 'name' ) !== false || stripos( $input['name'], 'name' ) !== false ) {
211
+ $name_name = $input['name'];
212
+ }
213
+
214
+ // remove input attributes
215
+ unset( $input['id'] );
216
+ unset( $input['class'] );
217
+ unset( $input['style'] );
218
+ unset( $input['onfocus'] );
219
+ unset( $input['onblur'] );
220
+ unset( $input['size'] );
221
+ }
222
+ }
223
+
224
+ // remove form attributes
225
+ unset( $form['target'] );
226
+ unset( $form['id'] );
227
+ unset( $form['name'] );
228
+ unset( $form['class'] );
229
+ unset( $form['onsubmit'] );
230
+ unset( $form['enctype'] );
231
+ }
232
+ }
233
+
234
+ $doc->removeChild( $doc->firstChild );
235
+ $doc->replaceChild( $doc->firstChild->firstChild->firstChild, $doc->firstChild );
236
+
237
+ $simpler_form = $doc->saveHTML();
238
+
239
+ // add tabs for improved readability
240
+ $simpler_form = str_replace( array( '><', '<input' ), array( ">\n<", "\t<input" ), $simpler_form );
241
+
242
+ return array(
243
+ 'simpler_form' => $simpler_form,
244
+ 'form_action' => $form_action,
245
+ 'email_name' => $email_name,
246
+ 'name_name' => $name_name,
247
+ 'additional_data' => $additional_data,
248
+ );
249
+ }
250
+
251
+
252
+ /**
253
+ * Adds the different menu pages
254
+ */
255
+ public function add_option_page() {
256
+ add_menu_page( $this->longname, 'Newsl. Sign-up', $this->accesslvl, $this->hook, array( $this, 'options_page_default' ), plugins_url( 'newsletter-sign-up/assets/img/icon.png' ) );
257
+ add_submenu_page( $this->hook, 'Newsletter Sign-Up :: Mailinglist Settings', 'List Settings', $this->accesslvl, $this->hook, array( $this, 'options_page_default' ) );
258
+ add_submenu_page( $this->hook, 'Newsletter Sign-Up :: Checkbox Settings', 'Checkbox Settings', $this->accesslvl, $this->hook . '-checkbox-settings', array( $this, 'options_page_checkbox_settings' ) );
259
+ add_submenu_page( $this->hook, 'Newsletter Sign-Up :: Form Settings', 'Form Settings', $this->accesslvl, $this->hook . '-form-settings', array( $this, 'options_page_form_settings' ) );
260
+ add_submenu_page( $this->hook, 'Newsletter Sign-Up :: Configuration Extractor', 'Config Extractor', $this->accesslvl, $this->hook . '-config-helper', array( $this, 'options_page_config_helper' ) );
261
+ }
262
+
263
+ /**
264
+ * Adds the settings link on the plugin's overview page
265
+ * @param array $links Array containing all the settings links for the various plugins.
266
+ * @return array The new array containing all the settings links
267
+ */
268
+ public function add_settings_link( $links ) {
269
+ $url = admin_url( 'admin.php?page=' . $this->hook );
270
+ $settings_link = '<a href="' . $url . '">Settings</a>';
271
+ array_unshift( $links, $settings_link );
272
+ return $links;
273
+ }
274
+
275
+ /**
276
+ * Registers the settings using WP Settings API.
277
+ */
278
+ public function settings_init() {
279
+ register_setting( 'nsu_form_group', 'nsu_form', array( $this, 'validate_form_options' ) );
280
+ register_setting( 'nsu_mailinglist_group', 'nsu_mailinglist', array( $this, 'validate_mailinglist_options' ) );
281
+ register_setting( 'nsu_checkbox_group', 'nsu_checkbox', array( $this, 'validate_checkbox_options' ) );
282
+ }
283
+
284
+ /**
285
+ * Validate the submitted options
286
+ * @param array $options The submitted options
287
+ * @return array
288
+ */
289
+ public function validate_options( $options ) {
290
+ return $options;
291
+ }
292
+
293
+ public function validate_form_options( $options ) {
294
+
295
+ $options['text_after_signup'] = strip_tags( $options['text_after_signup'], '<a><b><strong><i><img><em><br><p><ul><li><ol>' );
296
+
297
+ // redirect to url should start with http
298
+ if ( isset( $options['redirect_to'] ) && substr( $options['redirect_to'], 0, 4 ) != 'http' ) {
299
+ $options['redirect_to'] = '';
300
+ }
301
+
302
+ $options['name_required'] = ( isset( $options['name_required'] ) ) ? 1 : 0;
303
+ $options['wpautop'] = ( isset( $options['wpautop'] ) ) ? 1 : 0;
304
+ $options['load_form_css'] = ( isset( $options['load_form_css'] ) ) ? 1 : 0;
305
+
306
+ return $options;
307
+ }
308
+
309
+ public function validate_mailinglist_options( $options ) {
310
+ if ( is_array( $options['extra_data'] ) ) {
311
+ foreach ( $options['extra_data'] as $key => $value ) {
312
+ if ( empty( $value['name'] ) ) {
313
+ unset( $options['extra_data'][ $key ] );
314
+ }
315
+ }
316
+ }
317
+
318
+ return $options;
319
+ }
320
+
321
+ public function validate_checkbox_options( $options ) {
322
+ $options['add_to_comment_form'] = ( isset( $options['add_to_comment_form'] ) ) ? 1 : 0;
323
+ return $options;
324
+ }
325
+
326
+ }
includes/{NSU_Checkbox.php → class-nsu-checkbox.php} RENAMED
@@ -1,7 +1,6 @@
1
  <?php
2
 
3
  class NSU_Checkbox {
4
-
5
  /**
6
  * @var bool
7
  */
@@ -11,29 +10,27 @@ class NSU_Checkbox {
11
  private $options;
12
 
13
  public function __construct() {
14
-
15
  $options = NSU::instance()->get_options();
16
- $opts = $this->options = $options['checkbox'];
17
 
18
  // add hooks
19
-
20
- if ( $opts['add_to_comment_form'] == 1 ) {
21
  add_action( 'thesis_hook_after_comment_box', array( $this, 'output_checkbox' ), 20 );
22
  add_action( 'comment_form', array( $this, 'output_checkbox' ), 20 );
23
  add_action( 'comment_post', array( $this, 'grab_email_from_comment' ), 20, 2 );
24
  }
25
 
26
- if ( $opts['add_to_registration_form'] == 1 ) {
27
  add_action( 'register_form', array( $this, 'output_checkbox' ), 20 );
28
  add_action( 'register_post', array( $this, 'grab_email_from_wp_signup' ), 50 );
29
  }
30
 
31
- if ( $opts['add_to_buddypress_form'] == 1 ) {
32
  add_action( 'bp_before_registration_submit_buttons', array( $this, 'output_checkbox' ), 20 );
33
  add_action( 'bp_complete_signup', array( $this, 'grab_email_from_wp_signup' ), 20 );
34
  }
35
 
36
- if ( $opts['add_to_multisite_form'] == 1 ) {
37
  add_action( 'signup_extra_fields', array( $this, 'output_checkbox' ), 20 );
38
  add_action( 'signup_blogform', array( $this, 'add_hidden_checkbox' ), 20 );
39
  add_filter( 'add_signup_meta', array( $this, 'add_checkbox_to_usermeta' ) );
@@ -42,7 +39,7 @@ class NSU_Checkbox {
42
  }
43
 
44
  /* bbPress actions */
45
- if ( $opts['add_to_bbpress_forms'] ) {
46
  add_action( 'bbp_theme_after_topic_form_subscriptions', array( $this, 'output_checkbox' ), 10 );
47
  add_action( 'bbp_theme_after_reply_form_subscription', array( $this, 'output_checkbox' ), 10 );
48
  add_action( 'bbp_theme_anonymous_form_extras_bottom', array( $this, 'output_checkbox' ), 10 );
@@ -73,11 +70,11 @@ class NSU_Checkbox {
73
  }
74
 
75
  ?>
76
- <!-- Checkbox by Newsletter Sign-Up Checkbox v<?php echo NSU_VERSION_NUMBER; ?> - https://wordpress.org/plugins/newsletter-sign-up/ -->
77
  <p id="nsu-checkbox">
78
  <label for="nsu-checkbox-input" id="nsu-checkbox-label">
79
  <input value="1" id="nsu-checkbox-input" type="checkbox" name="newsletter-sign-up-do" <?php checked( $opts['precheck'], 1 ); ?> />
80
- <?php _e( $opts['text'], 'newsletter-sign-up' ); ?>
81
  </label>
82
  </p>
83
  <!-- / Newsletter Sign-Up -->
@@ -95,7 +92,7 @@ class NSU_Checkbox {
95
  function add_hidden_checkbox() {
96
  ?>
97
  <input type="hidden" name="newsletter-sign-up-do" value="<?php echo ( isset( $_POST['newsletter-sign-up-do'] ) ) ? 1 : 0; ?>" />
98
- <?php
99
  }
100
 
101
  /**
@@ -242,4 +239,4 @@ class NSU_Checkbox {
242
  return $this->subscribe_from_bbpress( $anonymous_data, $reply_author );
243
  }
244
 
245
- }
1
  <?php
2
 
3
  class NSU_Checkbox {
 
4
  /**
5
  * @var bool
6
  */
10
  private $options;
11
 
12
  public function __construct() {
 
13
  $options = NSU::instance()->get_options();
14
+ $this->options = $options['checkbox'];
15
 
16
  // add hooks
17
+ if ( $this->options['add_to_comment_form'] == 1 ) {
 
18
  add_action( 'thesis_hook_after_comment_box', array( $this, 'output_checkbox' ), 20 );
19
  add_action( 'comment_form', array( $this, 'output_checkbox' ), 20 );
20
  add_action( 'comment_post', array( $this, 'grab_email_from_comment' ), 20, 2 );
21
  }
22
 
23
+ if ( $this->options['add_to_registration_form'] == 1 ) {
24
  add_action( 'register_form', array( $this, 'output_checkbox' ), 20 );
25
  add_action( 'register_post', array( $this, 'grab_email_from_wp_signup' ), 50 );
26
  }
27
 
28
+ if ( $this->options['add_to_buddypress_form'] == 1 ) {
29
  add_action( 'bp_before_registration_submit_buttons', array( $this, 'output_checkbox' ), 20 );
30
  add_action( 'bp_complete_signup', array( $this, 'grab_email_from_wp_signup' ), 20 );
31
  }
32
 
33
+ if ( $this->options['add_to_multisite_form'] == 1 ) {
34
  add_action( 'signup_extra_fields', array( $this, 'output_checkbox' ), 20 );
35
  add_action( 'signup_blogform', array( $this, 'add_hidden_checkbox' ), 20 );
36
  add_filter( 'add_signup_meta', array( $this, 'add_checkbox_to_usermeta' ) );
39
  }
40
 
41
  /* bbPress actions */
42
+ if ( $this->options['add_to_bbpress_forms'] ) {
43
  add_action( 'bbp_theme_after_topic_form_subscriptions', array( $this, 'output_checkbox' ), 10 );
44
  add_action( 'bbp_theme_after_reply_form_subscription', array( $this, 'output_checkbox' ), 10 );
45
  add_action( 'bbp_theme_anonymous_form_extras_bottom', array( $this, 'output_checkbox' ), 10 );
70
  }
71
 
72
  ?>
73
+ <!-- Checkbox by Newsletter Sign-Up Checkbox v<?php echo NSU_VERSION; ?> - https://wordpress.org/plugins/newsletter-sign-up/ -->
74
  <p id="nsu-checkbox">
75
  <label for="nsu-checkbox-input" id="nsu-checkbox-label">
76
  <input value="1" id="nsu-checkbox-input" type="checkbox" name="newsletter-sign-up-do" <?php checked( $opts['precheck'], 1 ); ?> />
77
+ <?php echo $opts['text']; ?>
78
  </label>
79
  </p>
80
  <!-- / Newsletter Sign-Up -->
92
  function add_hidden_checkbox() {
93
  ?>
94
  <input type="hidden" name="newsletter-sign-up-do" value="<?php echo ( isset( $_POST['newsletter-sign-up-do'] ) ) ? 1 : 0; ?>" />
95
+ <?php
96
  }
97
 
98
  /**
239
  return $this->subscribe_from_bbpress( $anonymous_data, $reply_author );
240
  }
241
 
242
+ }
includes/{NSU_Form.php → class-nsu-form.php} RENAMED
@@ -58,7 +58,6 @@ class NSU_Form {
58
  return true;
59
  }
60
 
61
-
62
  return false;
63
  }
64
 
@@ -87,7 +86,7 @@ class NSU_Form {
87
  $opts = NSU::instance()->get_options();
88
 
89
  $additional_fields = '';
90
- $output = "\n<!-- Form by Newsletter Sign-Up v" . NSU_VERSION_NUMBER . " - https://wordpress.org/plugins/newsletter-sign-up/ -->\n";
91
 
92
  $formno = $this->number_of_forms ++;
93
 
@@ -108,7 +107,6 @@ class NSU_Form {
108
  if ( ! empty( $opts['mailinglist']['name_id'] ) ) {
109
  $name_id = $opts['mailinglist']['name_id'];
110
  }
111
-
112
  }
113
 
114
  /* Set up additional fields */
@@ -120,13 +118,13 @@ class NSU_Form {
120
  continue;
121
  }
122
 
123
- $ed['value'] = str_replace( "%%IP%%", $_SERVER['REMOTE_ADDR'], $ed['value'] );
124
  $additional_fields .= sprintf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $ed['name'] ), esc_attr( $ed['value'] ) );
125
  }
126
  }
127
 
128
- $email_label = __( $opts['form']['email_label'], 'nsu' );
129
- $name_label = __( $opts['form']['name_label'], 'nsu' );
130
 
131
  $email_type = 'email';
132
  $email_atts = 'placeholder="' . esc_attr( $opts['form']['email_default_value'] ) . '" required';
@@ -140,38 +138,38 @@ class NSU_Form {
140
 
141
  $text_after_signup = $opts['form']['text_after_signup'];
142
  $text_after_signup = ( $opts['form']['wpautop'] == 1 ) ? wpautop( wptexturize( $text_after_signup ) ) : $text_after_signup;
143
-
144
  // check if form was not submitted or contains error
145
  if ( ! isset( $_POST['nsu_submit'] ) || count( $errors ) > 0 ) {
146
 
147
- $output .= '<form class="nsu-form" id="nsu-form-' . esc_attr( $formno ) .'" action="' . esc_attr( $form_action ) . '" method="post">';
148
 
149
  if ( $opts['mailinglist']['subscribe_with_name'] == 1 ) {
150
- $output .= '<p><label for="nsu-name-'. esc_attr( $formno ) . '">'. esc_html( $name_label ) . '</label>';
151
- $output .= '<input class="nsu-field" id="nsu-name-"' . $formno .'" type="text" name="'. esc_attr( $name_id ) .'" ' . $name_atts;
152
 
153
- $output .= "/>";
154
 
155
  if ( isset( $errors['name-field'] ) ) {
156
  $output .= '<span class="nsu-error error notice">' . $errors['name-field'] . '</span>';
157
  }
158
- $output .= "</p>";
159
  }
160
 
161
  $output .= "<p><label for=\"nsu-email-$formno\">$email_label</label><input class=\"nsu-field\" id=\"nsu-email-$formno\" type=\"$email_type\" name=\"$email_id\" $email_atts ";
162
- $output .= "/>";
163
  if ( isset( $errors['email-field'] ) ) {
164
  $output .= '<span class="nsu-error error notice">' . $errors['email-field'] . '</span>';
165
  }
166
- $output .= "</p>";
167
  $output .= $additional_fields;
168
  $output .= '<textarea name="nsu_robocop" style="display: none;"></textarea>';
169
  $output .= sprintf( '<p><input type="submit" id="nsu-submit-%s" class="nsu-submit" name="nsu_submit" value="%s" /></p>', $formno, esc_attr( $submit_button ) );
170
- $output .= "</form>";
171
 
172
  } else { // form has been submitted
173
 
174
- $output .= "<p id=\"nsu-signed-up-$formno\" class=\"nsu-signed-up\">" . ( $text_after_signup ) . "</p>";
175
 
176
  }
177
 
@@ -185,4 +183,4 @@ class NSU_Form {
185
 
186
  }
187
 
188
- }
58
  return true;
59
  }
60
 
 
61
  return false;
62
  }
63
 
86
  $opts = NSU::instance()->get_options();
87
 
88
  $additional_fields = '';
89
+ $output = "\n<!-- Form by Newsletter Sign-Up v" . NSU_VERSION . " - https://wordpress.org/plugins/newsletter-sign-up/ -->\n";
90
 
91
  $formno = $this->number_of_forms ++;
92
 
107
  if ( ! empty( $opts['mailinglist']['name_id'] ) ) {
108
  $name_id = $opts['mailinglist']['name_id'];
109
  }
 
110
  }
111
 
112
  /* Set up additional fields */
118
  continue;
119
  }
120
 
121
+ $ed['value'] = str_replace( '%%IP%%', $_SERVER['REMOTE_ADDR'], $ed['value'] );
122
  $additional_fields .= sprintf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $ed['name'] ), esc_attr( $ed['value'] ) );
123
  }
124
  }
125
 
126
+ $email_label = $opts['form']['email_label'];
127
+ $name_label = $opts['form']['name_label'];
128
 
129
  $email_type = 'email';
130
  $email_atts = 'placeholder="' . esc_attr( $opts['form']['email_default_value'] ) . '" required';
138
 
139
  $text_after_signup = $opts['form']['text_after_signup'];
140
  $text_after_signup = ( $opts['form']['wpautop'] == 1 ) ? wpautop( wptexturize( $text_after_signup ) ) : $text_after_signup;
141
+
142
  // check if form was not submitted or contains error
143
  if ( ! isset( $_POST['nsu_submit'] ) || count( $errors ) > 0 ) {
144
 
145
+ $output .= '<form class="nsu-form" id="nsu-form-' . esc_attr( $formno ) . '" action="' . esc_attr( $form_action ) . '" method="post">';
146
 
147
  if ( $opts['mailinglist']['subscribe_with_name'] == 1 ) {
148
+ $output .= '<p><label for="nsu-name-' . esc_attr( $formno ) . '">' . esc_html( $name_label ) . '</label>';
149
+ $output .= '<input class="nsu-field" id="nsu-name-"' . $formno . '" type="text" name="' . esc_attr( $name_id ) . '" ' . $name_atts;
150
 
151
+ $output .= '/>';
152
 
153
  if ( isset( $errors['name-field'] ) ) {
154
  $output .= '<span class="nsu-error error notice">' . $errors['name-field'] . '</span>';
155
  }
156
+ $output .= '</p>';
157
  }
158
 
159
  $output .= "<p><label for=\"nsu-email-$formno\">$email_label</label><input class=\"nsu-field\" id=\"nsu-email-$formno\" type=\"$email_type\" name=\"$email_id\" $email_atts ";
160
+ $output .= '/>';
161
  if ( isset( $errors['email-field'] ) ) {
162
  $output .= '<span class="nsu-error error notice">' . $errors['email-field'] . '</span>';
163
  }
164
+ $output .= '</p>';
165
  $output .= $additional_fields;
166
  $output .= '<textarea name="nsu_robocop" style="display: none;"></textarea>';
167
  $output .= sprintf( '<p><input type="submit" id="nsu-submit-%s" class="nsu-submit" name="nsu_submit" value="%s" /></p>', $formno, esc_attr( $submit_button ) );
168
+ $output .= '</form>';
169
 
170
  } else { // form has been submitted
171
 
172
+ $output .= "<p id=\"nsu-signed-up-$formno\" class=\"nsu-signed-up\">" . ( $text_after_signup ) . '</p>';
173
 
174
  }
175
 
183
 
184
  }
185
 
186
+ }
includes/class-nsu-widget.php ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class NSU_Widget extends WP_Widget {
4
+
5
+ function __construct() {
6
+ $widget_ops = array(
7
+ 'classname' => 'nsu_widget',
8
+ 'description' => __( 'Displays a newsletter sign-up form.', 'newsletter-sign-up' ),
9
+ );
10
+ $control_ops = array(
11
+ 'width' => 400,
12
+ 'height' => 350,
13
+ );
14
+ parent::__construct( false, 'Newsletter Sign-Up', $widget_ops, $control_ops );
15
+ }
16
+
17
+ function widget( $args, $instance ) {
18
+ /* Provide some defaults */
19
+ $defaults = array(
20
+ 'title' => 'Sign up for our newsletter!',
21
+ 'text_before_form' => '',
22
+ 'text_after_form' => '',
23
+ );
24
+ $instance = array_merge( $defaults, (array) $instance );
25
+
26
+ $title = apply_filters( 'widget_title', $instance['title'] );
27
+
28
+ echo $args['before_widget'];
29
+ echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
30
+
31
+ if ( ! empty( $instance['text_before_form'] ) ) {
32
+ echo '<div class="nsu-text-before-form">';
33
+ echo $instance['filter'] ? wpautop( $instance['text_before_form'] ) : $instance['text_before_form'];
34
+ echo '</div>';
35
+ }
36
+
37
+ NSU::form()->output_form( true );
38
+
39
+ if ( ! empty( $instance['text_after_form'] ) ) {
40
+ echo '<div class="nsu-text-after-form">';
41
+ echo $instance['filter'] ? wpautop( $instance['text_after_form'] ) : $instance['text_after_form'];
42
+ echo '</div>';
43
+ }
44
+
45
+ echo $args['after_widget'];
46
+ }
47
+
48
+ function update( $new_instance, $old_instance ) {
49
+ $instance = $old_instance;
50
+ $instance['title'] = strip_tags( $new_instance['title'] );
51
+
52
+ if ( current_user_can( 'unfiltered_html' ) ) {
53
+ $instance['text_before_form'] = $new_instance['text_before_form'];
54
+ $instance['text_after_form'] = $new_instance['text_after_form'];
55
+ } else {
56
+ $instance['text_before_form'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text_before_form'] ) ) );
57
+ $instance['text_after_form'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text_after_form'] ) ) );
58
+ }
59
+ $instance['filter'] = isset( $new_instance['filter'] );
60
+
61
+ return $instance;
62
+ }
63
+
64
+ function form( $instance = array() ) {
65
+ $defaults = array(
66
+ 'title' => 'Sign up for our newsletter!',
67
+ 'text_before_form' => '',
68
+ 'text_after_form' => '',
69
+ );
70
+ $instance = array_merge( $defaults, (array) $instance );
71
+ ?>
72
+ <p>
73
+ <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'newsletter-sign-up' ); ?></label>
74
+ <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
75
+ </p>
76
+
77
+ <label title="You can use the following HTML-codes: &lt;a&gt;, &lt;strong&gt;, &lt;br /&gt;,&lt;em&gt; &lt;img ..&gt;" for="<?php echo $this->get_field_id( 'text_before_form' ); ?>"><?php _e( 'Text to show before the form', 'newsletter-sign-up' ); ?></label>
78
+ <textarea rows="8" cols="10" class="widefat wysiwyg-overlay-toggle" id="<?php echo $this->get_field_id( 'text_before_form' ); ?>" name="<?php echo $this->get_field_name( 'text_before_form' ); ?>"><?php echo esc_attr( $instance['text_before_form'] ); ?></textarea>
79
+ <br />
80
+ <label for="<?php echo $this->get_field_id( 'text_after_form' ); ?>"><?php _e( 'Text to show after the form', 'newsletter-sign-up' ); ?></label>
81
+ <textarea rows="8" cols="10" class="widefat wysiwyg-overlay-toggle" id="<?php echo $this->get_field_id( 'text_after_form' ); ?>" name="<?php echo $this->get_field_name( 'text_after_form' ); ?>"><?php echo esc_attr( $instance['text_after_form'] ); ?></textarea>
82
+
83
+ <p><input id="<?php echo $this->get_field_id( 'filter' ); ?>" name="<?php echo $this->get_field_name( 'filter' ); ?>" type="checkbox" <?php checked( isset( $instance['filter'] ) ? $instance['filter'] : 0 ); ?> />&nbsp;<label for="<?php echo $this->get_field_id( 'filter' ); ?>"><?php _e( 'Automatically add paragraphs', 'newsletter-sign-up' ); ?></label></p>
84
+
85
+ <p>
86
+ Configure the sign-up form at the <a href="<?php admin_url( 'admin.php?page=newsletter-sign-up-form-settings' ); ?>">Newsletter Sign-Up configuration page</a>.
87
+ </p>
88
+ <?php
89
+ }
90
+
91
+ }
includes/class-nsu.php ADDED
@@ -0,0 +1,371 @@