2017-10-26 13:51:35 -07:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Biscotti
|
|
|
|
*
|
2022-12-20 12:22:44 -08:00
|
|
|
* Biscotti is a plugin that modifies the expiration of the logged in user
|
2023-01-05 17:35:16 -08:00
|
|
|
* cookie in WordPress to three months, six months, or one year. Because
|
2022-12-20 12:22:44 -08:00
|
|
|
* some people hate to have to keep entering their passwords.
|
2017-10-26 13:51:35 -07:00
|
|
|
*
|
2022-12-20 12:22:44 -08:00
|
|
|
* @package Biscotti
|
|
|
|
* @author Jason Cosper <boogah@gmail.com>
|
2023-01-03 10:07:29 -08:00
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
|
2022-12-20 12:22:44 -08:00
|
|
|
* @link https://github.com/boogah/biscotti
|
2017-10-26 13:51:35 -07:00
|
|
|
*
|
|
|
|
* @wordpress-plugin
|
|
|
|
* Plugin Name: Biscotti
|
|
|
|
* Plugin URI: https://github.com/boogah/biscotti
|
2023-01-03 10:07:29 -08:00
|
|
|
* Description: Biscotti makes your user's login cookie a little bit longer.
|
2023-01-05 17:37:10 -08:00
|
|
|
* Version: 2.0.2
|
2023-01-03 11:16:41 -08:00
|
|
|
* Requires at least: 6.0
|
|
|
|
* Requires PHP: 7.4
|
2017-10-26 13:51:35 -07:00
|
|
|
* Author: Jason Cosper
|
|
|
|
* Author URI: https://jasoncosper.com/
|
2022-12-20 12:22:44 -08:00
|
|
|
* License: GPL-2.0+
|
2023-01-03 10:07:29 -08:00
|
|
|
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
|
2017-10-26 13:51:35 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
// If this file is called directly, abort.
|
2022-12-20 12:22:44 -08:00
|
|
|
if (! defined('WPINC') ) {
|
|
|
|
die;
|
2017-10-26 13:51:35 -07:00
|
|
|
}
|
|
|
|
|
2023-01-03 10:07:29 -08:00
|
|
|
// Add a dropdown menu to the user profile page that allows you to choose the login cookie's expiration date.
|
2022-12-20 12:22:44 -08:00
|
|
|
function biscotti_login_cookie_expiration_form_fields( $user )
|
|
|
|
{
|
|
|
|
$expiration_options = array(
|
|
|
|
'3 months' => '3 months',
|
|
|
|
'6 months' => '6 months',
|
|
|
|
'1 year' => '1 year',
|
|
|
|
);
|
|
|
|
$selected_expiration = get_the_author_meta('biscotti_login_cookie_expiration', $user->ID);
|
|
|
|
?>
|
|
|
|
<h3>Login Cookie Expiration</h3>
|
|
|
|
<table class="form-table">
|
|
|
|
<tr>
|
|
|
|
<th><label for="biscotti_login_cookie_expiration">Expiration</label></th>
|
|
|
|
<td>
|
|
|
|
<select name="biscotti_login_cookie_expiration" id="biscotti_login_cookie_expiration">
|
|
|
|
<?php foreach ( $expiration_options as $value => $label ) : ?>
|
|
|
|
<option value="<?php echo esc_attr($value); ?>" <?php selected($selected_expiration, $value); ?>><?php echo esc_html($label); ?></option>
|
|
|
|
<?php endforeach; ?>
|
|
|
|
</select>
|
|
|
|
<br>
|
|
|
|
<span class="description">Choose your login cookie's expiration date.</span>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<?php
|
|
|
|
}
|
2023-01-03 10:07:29 -08:00
|
|
|
|
|
|
|
// Add the form fields to the user profile page.
|
2022-12-20 12:22:44 -08:00
|
|
|
add_action('show_user_profile', 'biscotti_login_cookie_expiration_form_fields');
|
|
|
|
add_action('edit_user_profile', 'biscotti_login_cookie_expiration_form_fields');
|
|
|
|
|
2023-01-03 10:07:29 -08:00
|
|
|
// Update the user meta with the chosen login cookie expiration date.
|
2022-12-20 12:22:44 -08:00
|
|
|
function biscotti_login_cookie_expiration_form_fields_update( $user_id )
|
|
|
|
{
|
|
|
|
if (! current_user_can('edit_user', $user_id) ) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-05 17:35:16 -08:00
|
|
|
update_user_meta($user_id, 'biscotti_login_cookie_expiration', sanitize_text_field($_POST['biscotti_login_cookie_expiration']));
|
2022-12-20 12:22:44 -08:00
|
|
|
}
|
2023-01-03 10:07:29 -08:00
|
|
|
|
|
|
|
// Save the chosen login cookie expiration date when the user profile is updated.
|
2022-12-20 12:22:44 -08:00
|
|
|
add_action('personal_options_update', 'biscotti_login_cookie_expiration_form_fields_update');
|
|
|
|
add_action('edit_user_profile_update', 'biscotti_login_cookie_expiration_form_fields_update');
|
|
|
|
|
2023-01-13 16:24:47 +11:00
|
|
|
/**
|
|
|
|
* Modify the expiration of the logged in user cookie.
|
|
|
|
* @param int $expiration
|
|
|
|
* @param int $user_id
|
|
|
|
* @param bool $remember
|
|
|
|
* @return int
|
|
|
|
*/
|
2023-01-13 16:31:27 +11:00
|
|
|
function biscotti_login_cookie_expiration_set_auth_cookie( $expiration, $user_id, $remember )
|
2022-12-20 12:22:44 -08:00
|
|
|
{
|
|
|
|
$expiration_time = get_user_meta($user_id, 'biscotti_login_cookie_expiration', true);
|
|
|
|
|
|
|
|
if (! empty($expiration_time) ) {
|
|
|
|
if ($expiration_time == '3 months' ) {
|
2023-01-03 10:07:29 -08:00
|
|
|
$expiration = 90 * DAY_IN_SECONDS; // Set expiration to 3 months.
|
2022-12-20 12:22:44 -08:00
|
|
|
} elseif ($expiration_time == '6 months' ) {
|
2023-01-03 10:07:29 -08:00
|
|
|
$expiration = 180 * DAY_IN_SECONDS; // Set expiration to 6 months
|
2022-12-20 12:22:44 -08:00
|
|
|
} elseif ($expiration_time == '1 year' ) {
|
2023-01-03 10:07:29 -08:00
|
|
|
$expiration = 365 * DAY_IN_SECONDS; // Set expiration to 1 year.
|
2022-12-20 12:22:44 -08:00
|
|
|
} else {
|
2023-01-03 10:07:29 -08:00
|
|
|
$expiration = ''; // Use default expiration of 14 days.
|
2022-12-20 12:22:44 -08:00
|
|
|
}
|
|
|
|
}
|
2023-01-13 16:24:47 +11:00
|
|
|
return $expiration;
|
2017-10-26 13:51:35 -07:00
|
|
|
}
|
2023-01-03 10:07:29 -08:00
|
|
|
|
|
|
|
// Modify the expiration of the logged in user cookie when a user logs into the site.
|
2023-01-05 13:43:46 -08:00
|
|
|
add_filter('auth_cookie_expiration', 'biscotti_login_cookie_expiration_set_auth_cookie', 10, 3);
|