kollapsminoriteten/wp-content/plugins/simple-user-avatar/admin/class-sua-admin.php

338 lines
10 KiB
PHP
Raw Permalink Normal View History

2022-01-14 13:13:03 +01:00
<?php
2025-02-28 08:42:11 +01:00
if (!class_exists('SimpleUserAvatar_Admin')) {
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
/**
* PHP class SimpleUserAvatar_Admin
*
* @since 2.8
*/
class SimpleUserAvatar_Admin {
2022-01-14 13:13:03 +01:00
/**
2023-12-07 09:44:11 +01:00
* Properties
2022-01-14 13:13:03 +01:00
*
2023-12-07 09:44:11 +01:00
* @since 3.6
2022-01-14 13:13:03 +01:00
*/
2023-12-07 09:44:11 +01:00
private $avatar_size = 96;
private $notice_months_expiration = 12;
2025-02-28 08:42:11 +01:00
private $notices_enabled_pages = ['users.php', 'profile.php', 'user-new.php', 'user-edit.php'];
2023-12-07 09:44:11 +01:00
private $donation_public_permalink = 'https://www.paypal.com/donate/?cmd=_donations&business=matteomanna87%40gmail%2ecom';
private $reference_public_permalink = 'https://developer.wordpress.org/reference/functions/set_transient/';
private $plugin_public_permalink = 'https://wordpress.org/plugins/simple-user-avatar/';
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
public function __construct() {
2022-04-02 10:26:41 +02:00
2025-02-28 08:42:11 +01:00
// Global $pagenow
2023-12-07 09:44:11 +01:00
global $pagenow;
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// HTML render profile fields
2025-02-28 08:42:11 +01:00
add_action('show_user_profile', [$this, 'render_custom_user_profile_fields']);
add_action('edit_user_profile', [$this, 'render_custom_user_profile_fields']);
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Update profile fields
2025-02-28 08:42:11 +01:00
add_action('personal_options_update', [$this, 'update_custom_user_profile_fields']);
add_action('edit_user_profile_update', [$this, 'update_custom_user_profile_fields']);
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Delete user meta when attachment is deleted
2025-02-28 08:42:11 +01:00
add_action('delete_attachment', [$this, 'custom_delete_attachment']);
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Post call to hide notice
2025-02-28 08:42:11 +01:00
add_action('admin_post_hide_notice', [$this, 'post_hide_notice']);
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Load functions only if pages are enabled
2025-02-28 08:42:11 +01:00
if (in_array($pagenow, $this->notices_enabled_pages)) {
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Admin scripts
2025-02-28 08:42:11 +01:00
add_action('admin_enqueue_scripts', [$this, 'custom_admin_enqueue_scripts']);
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
// HTML render of notices
2025-02-28 08:42:11 +01:00
add_action('admin_notices', [$this, 'render_admin_error_notices']);
add_action('admin_notices', [$this, 'render_admin_donation_notice']);
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
}
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
}
2022-04-02 10:26:41 +02:00
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
public static function init() {
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
new self;
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
/**
* Load CSS and JavaScript for wp-admin
*
* @since 1.0
* @return void
*/
public function custom_admin_enqueue_scripts() {
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Get current user
global $current_user;
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Enqueue WordPress Media Library
wp_enqueue_media();
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// CSS for wp-admin
2025-02-28 08:42:11 +01:00
wp_enqueue_style('sua', plugins_url('/css/style.min.css', __FILE__), [], SUA_PLUGIN_VERSION, 'all');
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// JavaScript for wp-admin
2025-02-28 08:42:11 +01:00
wp_enqueue_script('sua', plugins_url('/js/scripts.js', __FILE__), ['jquery'], SUA_PLUGIN_VERSION, true);
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Get default avatar URL by user_email
$l10n = [
2025-02-28 08:42:11 +01:00
'default_avatar_src' => $this->get_default_avatar_url_by_email($current_user->user_email, $this->avatar_size),
'default_avatar_srcset' => $this->get_default_avatar_url_by_email($current_user->user_email, ($this->avatar_size * 2)) . ' 2x',
2023-12-07 09:44:11 +01:00
'input_name' => SUA_USER_META_KEY
];
2025-02-28 08:42:11 +01:00
wp_localize_script('sua', 'sua_obj', $l10n);
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
/**
* Default WordPress avatar URL by user email
*
* @since 2.8
* @return string
*/
2025-02-28 08:42:11 +01:00
private function get_default_avatar_url_by_email($user_email = '', $size = 96) {
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Check the email provided
2025-02-28 08:42:11 +01:00
if (empty($user_email) || !filter_var($user_email, FILTER_VALIDATE_EMAIL)) {
2023-12-07 09:44:11 +01:00
return null;
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Sanitize email and get md5
2025-02-28 08:42:11 +01:00
$user_email = sanitize_email($user_email);
$md5_user_email = md5($user_email);
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// SSL Gravatar URL
$url = 'https://secure.gravatar.com/avatar/' . $md5_user_email;
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Add query args
2025-02-28 08:42:11 +01:00
$url = add_query_arg('s', $size, $url);
$url = add_query_arg('d', 'mm', $url);
$url = add_query_arg('r', 'g', $url);
2022-01-14 13:13:03 +01:00
2025-02-28 08:42:11 +01:00
return esc_url($url);
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
/**
* Add table in user profile
*
* @since 1.0
* @return void
*/
2025-02-28 08:42:11 +01:00
public function render_custom_user_profile_fields($user) {
2023-12-07 09:44:11 +01:00
// Get user meta
2025-02-28 08:42:11 +01:00
$attachment_id = get_user_meta($user->ID, SUA_USER_META_KEY, true);
2023-12-07 09:44:11 +01:00
?>
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<label for="btn-media-add"><?php _e('Profile picture', 'simple-user-avatar'); ?></label>
</th>
<td>
2025-02-28 08:42:11 +01:00
<?php echo get_avatar($user->ID, $this->avatar_size, '', $user->display_name, ['class' => 'sua-attachment-avatar']); ?>
<p class="description <?php if (!empty($attachment_id)) echo 'hidden'; ?>" id="sua-attachment-description"><?php _e("You're seeing the default profile picture.", 'simple-user-avatar'); ?></p>
2023-12-07 09:44:11 +01:00
<div class="sua-btn-container">
<button type="button" class="button" id="btn-media-add"><?php _e('Select', 'simple-user-avatar'); ?></button>
2025-02-28 08:42:11 +01:00
<button type="button" class="button <?php if (empty($attachment_id)) echo 'hidden'; ?>" id="btn-media-remove"><?php _e('Remove', 'simple-user-avatar'); ?></button>
2023-12-07 09:44:11 +01:00
</div>
</td>
</tr>
</tbody>
</table>
<!-- Hidden attachment ID -->
<input type="hidden" name="<?php echo SUA_USER_META_KEY; ?>" value="<?php echo $attachment_id; ?>" />
<?php
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
}
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
/**
* Update the user meta when user save
*
* @since 1.0
* @return bool
*/
2025-02-28 08:42:11 +01:00
public function update_custom_user_profile_fields($user_id) {
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// If user don't have permissions
2025-02-28 08:42:11 +01:00
if (!current_user_can('edit_user', $user_id)) {
2023-12-07 09:44:11 +01:00
return false;
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Delete old user meta
2025-02-28 08:42:11 +01:00
delete_user_meta($user_id, SUA_USER_META_KEY);
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Validate POST data and, if is ok, add it
2025-02-28 08:42:11 +01:00
if (isset($_POST[SUA_USER_META_KEY]) && is_numeric($_POST[SUA_USER_META_KEY])) {
add_user_meta($user_id, SUA_USER_META_KEY, (int)$_POST[SUA_USER_META_KEY]);
2023-12-07 09:44:11 +01:00
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
return true;
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
/**
* Delete user meta when attachment is deleted
*
* @since 3.9
* @return void
*/
2025-02-28 08:42:11 +01:00
public function custom_delete_attachment($post_id) {
2023-12-07 09:44:11 +01:00
global $wpdb;
// Delete all user meta where deleted attachment post ID exists
$wpdb->delete(
$wpdb->usermeta,
[
'meta_key' => SUA_USER_META_KEY,
'meta_value' => (int)$post_id
],
[
'%s',
'%d'
]
);
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
/**
* Admin notices for errors
*
* @since 3.9
* @return void
*/
public function render_admin_error_notices() {
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Check if there is a GET error
2025-02-28 08:42:11 +01:00
if (isset( $_GET['error']) ) {
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
// Notice error container
$notice_error_container = '<div class="notice notice-error is-dismissible">%s</div>';
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
// Switch errors
2025-02-28 08:42:11 +01:00
switch($_GET['error']) {
2023-12-07 09:44:11 +01:00
// Transient not saved
case 'sua_transient_not_set':
printf(
$notice_error_container,
sprintf(
2025-02-28 08:42:11 +01:00
__('<p>An error occurred while <strong>saving the transient</strong>. Please make sure this website can <a href="%s" title="WordPress code reference" target="_blank" rel="noopener">save transients</a>.</p>', 'simple-user-avatar'),
esc_url($this->reference_public_permalink)
2023-12-07 09:44:11 +01:00
)
);
break;
2022-04-02 10:26:41 +02:00
}
2023-12-07 09:44:11 +01:00
}
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
}
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
/**
* Admin notice for donations, if transient not exists or is expired
*
* @since 2.6
* @return void
*/
public function render_admin_donation_notice() {
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Get Current user
global $current_user;
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Get the transient
2025-02-28 08:42:11 +01:00
$notice_is_expired = get_transient(SUA_TRANSIENT_NAME);
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
// Check the return of transient, if it's okay empty return
2025-02-28 08:42:11 +01:00
if ($notice_is_expired !== false && is_numeric($notice_is_expired) && $notice_is_expired == 1) {
2023-12-07 09:44:11 +01:00
return;
}
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
// Set the nonce field
2025-02-28 08:42:11 +01:00
$wp_nonce_field = wp_nonce_field(SUA_TRANSIENT_NAME, '_wpnonce', true, false);
$wp_nonce_field = preg_replace('/id=("|\').*?("|\')/', '', $wp_nonce_field);
2023-12-07 09:44:11 +01:00
?>
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
<div class="notice notice-info">
2025-02-28 08:42:11 +01:00
<form method="post" action="<?php echo admin_url('admin-post.php'); ?>">
2023-12-07 09:44:11 +01:00
<p>
2022-04-02 10:26:41 +02:00
<?php
2023-12-07 09:44:11 +01:00
printf(
__( 'Dear <strong>%s</strong>, thank you for using my plugin <a href="%s" title="Simple User Avatar" target="_blank" rel="noopener">Simple User Avatar</a>! Even a small amount, such as <strong>1$</strong> for one coffee &#x2615 will be greatly appreciated to <strong>support</strong> the development of the plugin in the future. Best regards, Matteo.', 'simple-user-avatar' ),
2025-02-28 08:42:11 +01:00
sanitize_text_field($current_user->display_name),
esc_url($this->plugin_public_permalink)
2023-12-07 09:44:11 +01:00
);
?>
</p>
<p>
2025-02-28 08:42:11 +01:00
<a href="<?php echo esc_url($this->donation_public_permalink); ?>" class="button button-primary" target="_blank" rel="noopener"><?php _e('Donate now', 'simple-user-avatar'); ?></a>
<button type="submit" class="button"><?php printf(__('Hide for %d months', 'simple-user-avatar' ), $this->notice_months_expiration); ?></button>
2023-12-07 09:44:11 +01:00
</p>
<input type="hidden" name="action" value="hide_notice" />
<?php echo $wp_nonce_field; ?>
</form>
</div>
<?php
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
/**
* Set the transient
* Add query arg if there is an error
*
* @since 2.6
* @return void
*/
public function post_hide_notice() {
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Set default redirect URL
2025-02-28 08:42:11 +01:00
$redirect_url = !empty($_POST['_wp_http_referer']) ? $_POST['_wp_http_referer'] : admin_url('users.php') ;
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Verify nonce
2025-02-28 08:42:11 +01:00
if (isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], SUA_TRANSIENT_NAME)) {
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Transient settings
$transient = SUA_TRANSIENT_NAME;
$value = 1;
$expiration = (86400 * 30) * $this->notice_months_expiration;
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Set the transient but, if an error has occurred, add query arg at redirect URL
2025-02-28 08:42:11 +01:00
if (set_transient($transient, $value, $expiration) === false) {
$redirect_url = add_query_arg('error', 'sua_transient_not_set', $redirect_url);
2023-12-07 09:44:11 +01:00
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Safe redirect
2025-02-28 08:42:11 +01:00
if (wp_safe_redirect(esc_url($redirect_url))) {
2023-12-07 09:44:11 +01:00
exit;
}
2022-01-14 13:13:03 +01:00
}
2023-12-07 09:44:11 +01:00
}
2025-02-28 08:42:11 +01:00
add_action('plugins_loaded', ['SimpleUserAvatar_Admin', 'init']);
2022-01-14 13:13:03 +01:00
2025-02-28 08:42:11 +01:00
}