2022-01-14 13:13:03 +01:00
< ? php
2022-04-02 10:26:41 +02: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 ;
private $notices_enabled_pages = [ 'users.php' , 'profile.php' , 'user-new.php' , 'user-edit.php' ];
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
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
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
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
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
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
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
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
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
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
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 = [
'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' ,
'input_name' => SUA_USER_META_KEY
];
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
*/
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
if ( empty ( $user_email ) || ! filter_var ( $user_email , FILTER_VALIDATE_EMAIL ) ) {
return null ;
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Sanitize email and get md5
$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
$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
2023-12-07 09:44: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
*/
public function render_custom_user_profile_fields ( $user ) {
// Get user meta
$attachment_id = get_user_meta ( $user -> ID , SUA_USER_META_KEY , true );
?>
< table class = " form-table " >
< tbody >
< tr >
< th scope = " row " >
< label for = " btn-media-add " >< ? php _e ( 'Profile picture' , 'simple-user-avatar' ); ?> </label>
</ th >
< td >
< ? 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>
< div class = " sua-btn-container " >
< button type = " button " class = " button " id = " btn-media-add " >< ? php _e ( 'Select' , 'simple-user-avatar' ); ?> </button>
< button type = " button " class = " button <?php if ( empty( $attachment_id ) ) echo 'hidden'; ?> " id = " btn-media-remove " >< ? php _e ( 'Remove' , 'simple-user-avatar' ); ?> </button>
</ 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
*/
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
if ( ! current_user_can ( 'edit_user' , $user_id ) ) {
return false ;
}
2022-01-14 13:13:03 +01:00
2023-12-07 09:44:11 +01:00
// Delete old user meta
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
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 ] );
}
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
*/
public function custom_delete_attachment ( $post_id ) {
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
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
switch ( $_GET [ 'error' ] ) {
// Transient not saved
case 'sua_transient_not_set' :
printf (
$notice_error_container ,
sprintf (
__ ( '<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 )
)
);
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
$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
if ( $notice_is_expired !== false && is_numeric ( $notice_is_expired ) && $notice_is_expired == 1 ) {
return ;
}
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
// Set the nonce field
$wp_nonce_field = wp_nonce_field ( SUA_TRANSIENT_NAME , '_wpnonce' , true , false );
$wp_nonce_field = preg_replace ( '/id=("|\').*?("|\')/' , '' , $wp_nonce_field );
?>
2022-04-02 10:26:41 +02:00
2023-12-07 09:44:11 +01:00
< div class = " notice notice-info " >
< form method = " post " action = " <?php echo admin_url( 'admin-post.php' ); ?> " >
< 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 ☕ will be greatly appreciated to <strong>support</strong> the development of the plugin in the future. Best regards, Matteo.' , 'simple-user-avatar' ),
sanitize_text_field ( $current_user -> display_name ),
esc_url ( $this -> plugin_public_permalink )
);
?>
</ p >
< p >
< 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>
</ 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
$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
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
if ( set_transient ( $transient , $value , $expiration ) === false ) {
$redirect_url = add_query_arg ( 'error' , 'sua_transient_not_set' , $redirect_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
// Safe redirect
if ( wp_safe_redirect ( esc_url ( $redirect_url ) ) ) {
exit ;
}
2022-01-14 13:13:03 +01:00
}
2023-12-07 09:44:11 +01:00
}
add_action ( 'plugins_loaded' , [ 'SimpleUserAvatar_Admin' , 'init' ] );
2022-01-14 13:13:03 +01:00
2022-04-02 10:26:41 +02:00
}