Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added more updates/fixes and upgrade detection

  • Loading branch information...
commit 97f1da0b76b4063a99951bc6eea34bb95f2f9803 1 parent baa5535
Eric Martin authored

Showing 2 changed files with 152 additions and 115 deletions. Show diff stats Hide diff stats

  1. +41 39 js/default.js
  2. +111 76 simplemodal-login.php
80 js/default.js
@@ -8,9 +8,9 @@ jQuery(function ($) {
8 8 var SimpleModalLogin = {
9 9 init: function () {
10 10 var s = this;
11   - s.error = null;
  11 + s.error = [];
12 12
13   - $('.simplemodal-login, .simplemodal-register, .simplemodal-forgotpw').live('click', function (e) {
  13 + $('.simplemodal-login, .simplemodal-register, .simplemodal-forgotpw').live('click.simplemodal', function (e) {
14 14 var login = $('#loginform'),
15 15 lostpw = $('#lostpasswordform'),
16 16 register = $('#registerform');
@@ -54,7 +54,7 @@ jQuery(function ($) {
54 54 // focus on first element
55 55 $(':input:visible:first', form[0]).focus();
56 56
57   - form.submit(function (e) {
  57 + form.unbind('submit.simplemodal').bind('submit.simplemodal', function (e) {
58 58 e.preventDefault();
59 59
60 60 // remove any existing errors
@@ -62,6 +62,12 @@ jQuery(function ($) {
62 62
63 63 if (SimpleModalLogin.isValid(form)) {
64 64 fields.hide(); activity.show();
  65 +
  66 + if (SimpleModalLogin.url && SimpleModalLogin.url.indexOf("redirect_to") !== -1) {
  67 + var p = SimpleModalLogin.url.split("=");
  68 + $('#redirect_to', form[0]).val(unescape(p[1]));
  69 + }
  70 +
65 71 $.ajax({
66 72 url: form[0].action,
67 73 data: form.serialize(),
@@ -69,37 +75,29 @@ jQuery(function ($) {
69 75 cache: false,
70 76 success: function (resp) {
71 77 var data = $(document.createElement('div')).html(resp),
72   - error = $('#login_error', data[0]),
73   - loginform = $(SimpleModalLogin.form, data[0]),
74 78 redirect = $('#simplemodal-login-redirect', data[0]);
75 79
76   - if (error.length) {
77   - error.find('a').addClass('simplemodal-forgotpw');
78   - $('p:first', form[0]).before(error);
79   - activity.hide(); fields.show();
80   - }
81   - else if (loginform.length) {
82   - SimpleModalLogin.showError(form, 'empty_both');
83   - activity.hide(); fields.show();
84   - }
85   - else {
86   - var rt = $('#redirect_to', form[0]).val(),
87   - href = location.href;
88   -
  80 + if (redirect.length) {
  81 + var href = location.href;
89 82 if (redirect.length) {
90 83 href = redirect.html();
91 84 }
92   - else if (rt.length) {
93   - if (SimpleModalLogin.url && SimpleModalLogin.url.indexOf("redirect_to") !== -1) {
94   - var p = SimpleModalLogin.url.split("=");
95   - href = unescape(p[1]);
96   - }
97   - else {
98   - href = rt;
99   - }
100   - }
101 85 window.location = href;
102 86 }
  87 + else {
  88 + var error = $('#login_error', data[0]),
  89 + loginform = $(SimpleModalLogin.form, data[0]);
  90 +
  91 + if (error.length) {
  92 + error.find('a').addClass('simplemodal-forgotpw');
  93 + $('p:first', form[0]).before(error);
  94 + activity.hide(); fields.show();
  95 + }
  96 + else if (loginform.length) {
  97 + SimpleModalLogin.showError(form, ['empty_both']);
  98 + activity.hide(); fields.show();
  99 + }
  100 + }
103 101 }
104 102 });
105 103 }
@@ -114,29 +112,30 @@ jQuery(function ($) {
114 112 email = $('.user_email', form[0]),
115 113 fields = $(':text, :password', form[0]),
116 114 valid = true;
117   -
118 115
119   - if (log && !$.trim(log.val())) {
120   - SimpleModalLogin.error = 'empty_username';
  116 + SimpleModalLogin.error = [];
  117 +
  118 + if (log.length && !$.trim(log.val())) {
  119 + SimpleModalLogin.error.push('empty_username');
121 120 valid = false;
122 121 }
123   - else if (!pass && !$.trim(pass.val())) {
124   - SimpleModalLogin.error = 'empty_password';
  122 + else if (pass.length && !$.trim(pass.val())) {
  123 + SimpleModalLogin.error.push('empty_password');
125 124 valid = false;
126 125 }
127   - else if (!email && !$.trim(email.val())) {
128   - SimpleModalLogin.error = 'empty_email';
  126 + else if (email.length && !$.trim(email.val())) {
  127 + SimpleModalLogin.error.push('empty_email');
129 128 valid = false;
130 129 }
131   -
  130 +
132 131 var empty_count = 0;
133 132 fields.each(function () {
134 133 if (!$.trim(this.value)) {
135 134 empty_count++;
136 135 }
137 136 });
138   - if (empty_count === fields.length) {
139   - SimpleModalLogin.error = 'empty_all';
  137 + if (fields.length > 1 && empty_count === fields.length) {
  138 + SimpleModalLogin.error = ['empty_all'];
140 139 valid = false;
141 140 }
142 141
@@ -147,10 +146,13 @@ jQuery(function ($) {
147 146 SimpleModalLoginL10n[key].replace(/&gt;/g, '>').replace(/&lt;/g, '<') :
148 147 key;
149 148 },
150   - showError: function (form, key) {
  149 + showError: function (form, keys) {
  150 + keys = $.map(keys, function (key) {
  151 + return SimpleModalLogin.message(key);
  152 + });
151 153 $('p:first', form[0])
152 154 .before($('<div id="login_error"></div>').html(
153   - SimpleModalLogin.message(key)
  155 + keys.join('<br/>')
154 156 ));
155 157 }
156 158 };
187 simplemodal-login.php
@@ -43,7 +43,7 @@ class SimpleModalLogin {
43 43 /**
44 44 * @var string The plugin version
45 45 */
46   - var $version = '0.3';
  46 + var $version = '1.0';
47 47
48 48 /**
49 49 * @var string The options string name for this plugin
@@ -56,6 +56,11 @@ class SimpleModalLogin {
56 56 var $localizationDomain = 'simplemodal_login';
57 57
58 58 /**
  59 + * @var string $nonce String used for nonce security
  60 + */
  61 + var $nonce = 'simplemodal-login-update-options';
  62 +
  63 + /**
59 64 * @var string $pluginurl The url to this plugin
60 65 */
61 66 var $pluginurl = '';
@@ -94,16 +99,50 @@ function __construct() {
94 99 add_action('admin_menu', array(&$this, 'admin_menu_link'));
95 100
96 101 if (!is_admin()) {
97   - add_filter('login_redirect', array(&$this, 'simplemodal_login_redirect'), 5, 3);
98   - add_filter('register', array(&$this, 'simplemodal_register'));
99   - add_filter('loginout', array(&$this, 'simplemodal_login_loginout'));
100   - add_action('wp_footer', array($this, 'simplemodal_login_footer'));
101   - add_action('wp_print_styles', array(&$this, 'simplemodal_login_css'));
102   - add_action('wp_print_scripts', array(&$this, 'simplemodal_login_js'));
  102 + add_filter('login_redirect', array(&$this, 'login_redirect'), 5, 3);
  103 + add_filter('register', array(&$this, 'register'));
  104 + add_filter('loginout', array(&$this, 'login_loginout'));
  105 + add_action('wp_footer', array($this, 'login_footer'));
  106 + add_action('wp_print_styles', array(&$this, 'login_css'));
  107 + add_action('wp_print_scripts', array(&$this, 'login_js'));
  108 + }
  109 + }
  110 +
  111 + function check_options() {
  112 + $options = null;
  113 + if (!$options = get_option($this->optionsName)) {
  114 + // default options for a clean install
  115 + $options = array(
  116 + 'theme' => 'default',
  117 + 'version' => $this->version
  118 + );
  119 + update_option($this->optionsName, $options);
103 120 }
  121 + else {
  122 + // check for upgrades
  123 + if (isset($options['version'])) {
  124 + if ($options['version'] < $this->version) {
  125 + // upgrade logic goes here
  126 + }
  127 + }
  128 + else {
  129 + // pre v1.0 updates
  130 + if (isset($options['admin'])) {
  131 + unset($options['admin']);
  132 + $options['version'] = $this->version;
  133 + update_option($this->optionsName, $options);
  134 + }
  135 + }
  136 + }
  137 +
  138 + return $options;
104 139 }
105 140
106   - function simplemodal_login_css() {
  141 + function is_plugin_active($plugin) {
  142 + return in_array($plugin, (array) get_option('active_plugins', array()));
  143 + }
  144 +
  145 + function login_css() {
107 146 $style = sprintf("%s.css", $this->options['theme']);
108 147 wp_enqueue_style('simplemodal-login', $this->pluginurl . "css/$style", false, $this->version, 'screen');
109 148 if (false !== @file_exists(TEMPLATEPATH . "simplemodal-login-$style")) {
@@ -111,7 +150,8 @@ function simplemodal_login_css() {
111 150 }
112 151 }
113 152
114   - function simplemodal_login_footer() {
  153 + function login_footer() {
  154 + $can_register = get_option('users_can_register');
115 155 printf('<div id="simplemodal-login-form">
116 156 <form name="loginform" id="loginform" action="%s" method="post" style="display:none;">
117 157 <div class="title">%s</div>
@@ -137,17 +177,15 @@ function simplemodal_login_footer() {
137 177 <p class="submit">
138 178 <input type="submit" name="wp-submit" value="%s" tabindex="100" />
139 179 <input type="button" class="simplemodal-close" value="%s" tabindex="101" />
140   - <input type="hidden" id="redirect_to" name="redirect_to" value="%s" />
141 180 <input type="hidden" name="testcookie" value="1" />
142 181 </p>
143 182 <p class="nav">',
144 183 __('Remember Me', $this->localizationDomain),
145 184 __('Log In', $this->localizationDomain),
146   - __('Cancel', $this->localizationDomain),
147   - $this->options['admin'] === true ? admin_url() : ''
  185 + __('Cancel', $this->localizationDomain)
148 186 );
149 187
150   - if (get_option('users_can_register')) {
  188 + if ($can_register) {
151 189 printf('<a class="simplemodal-register" href="%s">%s</a> | ', site_url('wp-login.php?action=register', 'login'), __('Register', $this->localizationDomain));
152 190 }
153 191
@@ -156,7 +194,14 @@ function simplemodal_login_footer() {
156 194 </p>
157 195 </div>
158 196 <div class="simplemodal-login-activity" style="display:none;"></div>
159   - </form>
  197 + </form>',
  198 + site_url('wp-login.php?action=lostpassword', 'login'),
  199 + __('Password Lost and Found', $this->localizationDomain),
  200 + __('Lost your password?', $this->localizationDomain)
  201 + );
  202 +
  203 + if ($can_register) {
  204 + printf('
160 205 <form name="registerform" id="registerform" action="%s" method="post" style="display:none;">
161 206 <div class="title">%s</div>
162 207 <div class="simplemodal-login-fields">
@@ -167,19 +212,16 @@ function simplemodal_login_footer() {
167 212 <p>
168 213 <label>%s<br />
169 214 <input type="text" name="user_email" class="user_email input" value="" size="25" tabindex="20" /></label>
170   - </p>',
171   - site_url('wp-login.php?action=lostpassword', 'login'),
172   - __('Password Lost and Found', $this->localizationDomain),
173   - __('Lost your password?', $this->localizationDomain),
174   - site_url('wp-login.php?action=register', 'login_post'),
175   - __('Register', $this->localizationDomain),
176   - __('Username', $this->localizationDomain),
177   - __('E-mail', $this->localizationDomain)
178   - );
  215 + </p>',
  216 + site_url('wp-login.php?action=register', 'login_post'),
  217 + __('Register', $this->localizationDomain),
  218 + __('Username', $this->localizationDomain),
  219 + __('E-mail', $this->localizationDomain)
  220 + );
179 221
180   - do_action('register_form');
181   -
182   - printf('
  222 + do_action('register_form');
  223 +
  224 + printf('
183 225 <p class="reg_passmail">%s</p>
184 226 <p class="submit">
185 227 <input type="submit" name="wp-submit" value="%s" tabindex="100" />
@@ -190,7 +232,19 @@ function simplemodal_login_footer() {
190 232 </p>
191 233 </div>
192 234 <div class="simplemodal-login-activity" style="display:none;"></div>
193   - </form>
  235 + </form>',
  236 + __('A password will be e-mailed to you.', $this->localizationDomain),
  237 + __('Register', $this->localizationDomain),
  238 + __('Cancel', $this->localizationDomain),
  239 + site_url('wp-login.php', 'login'),
  240 + __('Log in', $this->localizationDomain),
  241 + site_url('wp-login.php?action=lostpassword', 'login'),
  242 + __('Password Lost and Found', $this->localizationDomain),
  243 + __('Lost your password?', $this->localizationDomain)
  244 + );
  245 + }
  246 +
  247 + printf('
194 248 <form name="lostpasswordform" id="lostpasswordform" action="%s" method="post" style="display:none;">
195 249 <div class="title">%s</div>
196 250 <div class="simplemodal-login-fields">
@@ -198,14 +252,6 @@ function simplemodal_login_footer() {
198 252 <label>%s<br />
199 253 <input type="text" name="user_login" class="user_login input" value="" size="20" tabindex="10" /></label>
200 254 </p>',
201   - __('A password will be e-mailed to you.', $this->localizationDomain),
202   - __('Register', $this->localizationDomain),
203   - __('Cancel', $this->localizationDomain),
204   - site_url('wp-login.php', 'login'),
205   - __('Log in', $this->localizationDomain),
206   - site_url('wp-login.php?action=lostpassword', 'login'),
207   - __('Password Lost and Found', $this->localizationDomain),
208   - __('Lost your password?', $this->localizationDomain),
209 255 site_url('wp-login.php?action=lostpassword', 'login_post'),
210 256 __('Reset Password', $this->localizationDomain),
211 257 __('Username or E-mail:', $this->localizationDomain)
@@ -219,24 +265,29 @@ function simplemodal_login_footer() {
219 265 <input type="button" class="simplemodal-close" value="%s" tabindex="101" />
220 266 </p>
221 267 <p class="nav">
222   - <a class="simplemodal-login" href="%s">%s</a> | <a class="simplemodal-register" href="%s">%s</a>
  268 + <a class="simplemodal-login" href="%s">%s</a>',
  269 + __('Get New Password', $this->localizationDomain),
  270 + __('Cancel', $this->localizationDomain),
  271 + site_url('wp-login.php', 'login'),
  272 + __('Log in', $this->localizationDomain)
  273 + );
  274 +
  275 + if ($can_register) {
  276 + printf('| <a class="simplemodal-register" href="%s">%s</a>', site_url('wp-login.php?action=register', 'login'), __('Register', $this->localizationDomain));
  277 + }
  278 +
  279 + printf('
223 280 </p>
224 281 </div>
225 282 <div class="simplemodal-login-activity" style="display:none;"></div>
226 283 </form>
227 284 <div class="simplemodal-login-credit"><a href="http://www.ericmmartin.com/projects/simplemodal-login/">%s</a></div>
228   -</div>',
229   - __('Get New Password', $this->localizationDomain),
230   - __('Cancel', $this->localizationDomain),
231   - site_url('wp-login.php', 'login'),
232   - __('Log in', $this->localizationDomain),
233   - site_url('wp-login.php?action=register', 'login_post'),
234   - __('Register', $this->localizationDomain),
  285 +</div>',
235 286 __('Powered by', $this->localizationDomain) . " SimpleModal Login"
236 287 );
237 288 }
238 289
239   - function simplemodal_login_js() {
  290 + function login_js() {
240 291 wp_enqueue_script("jquery-simplemodal", $this->pluginurl . "js/jquery.simplemodal.js", array("jquery"), "1.3.3", true);
241 292
242 293 $script = sprintf("js/%s.js", $this->options['theme']);
@@ -250,28 +301,26 @@ function simplemodal_login_js() {
250 301
251 302 }
252 303
253   - function simplemodal_login_loginout($link) {
  304 + function login_loginout($link) {
254 305 if (!is_user_logged_in()) {
255 306 $link = str_replace('href=', 'class="simplemodal-login" href=', $link);
256 307 }
257 308 return $link;
258 309 }
259 310
260   - function simplemodal_login_redirect($redirect_to, $req_redirect_to, $user) {
  311 + function login_redirect($redirect_to, $req_redirect_to, $user) {
261 312 if (!isset($user->user_login)) {
262 313 return $redirect_to;
263 314 }
264   - if (is_plugin_active('peters-login-redirect/wplogin_redirect.php')
  315 + if ($this->is_plugin_active('peters-login-redirect/wplogin_redirect.php')
265 316 && function_exists('redirect_to_front_page')) {
266   - //file_put_contents('log.txt', "function exists: redirect_to_front_page()\n", FILE_APPEND);
267 317 $redirect_to = redirect_to_front_page($redirect_to, $req_redirect_to, $user);
268 318 }
269   - //file_put_contents('log.txt', "$redirect_to, $req_redirect_to\n", FILE_APPEND);
270 319 echo "<div id='simplemodal-login-redirect'>$redirect_to</div>";
271 320 exit();
272 321 }
273 322
274   - function simplemodal_register($link) {
  323 + function register($link) {
275 324 if (!is_user_logged_in()) {
276 325 $link = str_replace('href=', 'class="simplemodal-register" href=', $link);
277 326 }
@@ -283,15 +332,10 @@ function simplemodal_register($link) {
283 332 * @return array
284 333 */
285 334 function get_options() {
286   - if (!$options = get_option($this->optionsName)) {
287   - $options = array(
288   - 'admin' => true,
289   - 'theme' => 'default'
290   - );
291   - update_option($this->optionsName, $options);
292   - }
  335 + $options = $this->check_options();
293 336 $this->options = $options;
294 337 }
  338 +
295 339 /**
296 340 * Saves the admin options to the database.
297 341 */
@@ -322,32 +366,23 @@ function filter_plugin_actions($links, $file) {
322 366 */
323 367 function admin_options_page() {
324 368 if (isset($_POST['simplemodal_login_save'])) {
325   - if (wp_verify_nonce($_POST['_wpnonce'], 'simplemodal-login-update-options')) {
326   - $this->options['admin'] = (isset($_POST['admin']) && $_POST['admin'] === 'on') ? true : false;
327   - $this->options['theme'] = $_POST['theme'];
328   -
329   - $this->save_admin_options();
330   -
331   - echo '<div class="updated"><p>' . __('Success! Your changes were successfully saved!', $this->localizationDomain) . '</p></div>';
332   - }
333   - else {
334   - echo '<div class="error"><p>' . __('Whoops! There was a problem with the data you posted. Please try again.', $this->localizationDomain) . '</p></div>';
335   - }
  369 + check_admin_referer($this->nonce);
  370 +
  371 + $this->options['theme'] = $_POST['theme'];
  372 +
  373 + $this->save_admin_options();
  374 +
  375 + echo '<div class="updated"><p>' . __('Success! Your changes were successfully saved!', $this->localizationDomain) . '</p></div>';
336 376 }
337 377 ?>
338 378
339 379 <div class="wrap">
340 380 <div class="icon32" id="icon-options-general"><br/></div>
341   -<h2>SimpleModal Login</h2>
  381 +<h2>SimpleModal Login <span style='font-size:60%;'>v<?php echo $this->version; ?></span></h2>
342 382 <form method="post" id="simplemodal_login_options">
343   -<?php wp_nonce_field('simplemodal-login-update-options'); ?>
  383 +<?php wp_nonce_field($this->nonce); ?>
344 384 <table class="form-table">
345 385 <tr valign="top">
346   - <th scope="row"><?php _e('Redirect after login?:', $this->localizationDomain); ?></th>
347   - <td><label for="admin">
348   - <input type="checkbox" id="admin" name="admin" <?php echo ($this->options['admin'] === true) ? "checked='checked'" : ""; ?>/> <?php _e('Select this option to be redirected to the WP Admin screen or the URL you provided in the <code>wp_loginout</code> function after logging in. If not selected, you will be returned to the current page.', $this->localizationDomain); ?></label></td>
349   - </tr>
350   - <tr valign="top">
351 386 <th scope="row"><?php _e('Theme:', $this->localizationDomain); ?></th>
352 387 <td>
353 388 <select name="theme" id="theme">
@@ -355,7 +390,7 @@ function admin_options_page() {
355 390 $cssfile = basename($cssfile);
356 391 $theme = str_replace('.css', '', $cssfile);
357 392
358   - if (!file_exists($this->pluginpath . "js/{$theme}.js")) {
  393 + if (false === @file_exists($this->pluginpath . "js/{$theme}.js")) {
359 394 continue;
360 395 }
361 396 ?>

0 comments on commit 97f1da0

Please sign in to comment.
Something went wrong with that request. Please try again.