avatar_settings_field(
array(
'key' => 'caps',
'desc' => __( 'Only allow users with file upload capabilities to upload local avatars (Authors and above)', 'simple-local-avatars' ),
)
);
?>
avatar_settings_field(
array(
'key' => 'shared',
'desc' => __( 'Uploaded avatars will be shared across the entire network, instead of being unique per site', 'simple-local-avatars' ),
'default' => 1,
)
);
?>
remove_nonce = wp_create_nonce( 'remove_simple_local_avatar_nonce' );
wp_enqueue_script( 'simple-local-avatars', plugins_url( '', dirname( __FILE__ ) ) . '/dist/simple-local-avatars.js', array( 'jquery' ), SLA_VERSION, true );
wp_localize_script(
'simple-local-avatars',
'i10n_SimpleLocalAvatars',
array(
'user_id' => $user_id,
'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ),
'selectCrop' => __( 'Select avatar and Crop', 'simple-local-avatars' ),
'deleteNonce' => $this->remove_nonce,
'cacheNonce' => wp_create_nonce( 'sla_clear_cache_nonce' ),
'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ),
'migrateFromWpUserAvatarNonce' => wp_create_nonce( 'migrate_from_wp_user_avatar_nonce' ),
'clearCacheError' => esc_html__( 'Something went wrong while clearing cache, please try again.', 'simple-local-avatars' ),
'insertMediaTitle' => esc_html__( 'Choose default avatar', 'simple-local-avatars' ),
'migrateFromWpUserAvatarSuccess' => __( 'Number of avatars successfully migrated from WP User Avatar', 'simple-local-avatars' ),
'migrateFromWpUserAvatarFailure' => __( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ),
'migrateFromWpUserAvatarProgress' => __( 'Migration in progress.', 'simple-local-avatars' ),
)
);
}
/**
* Sanitize new settings field before saving
*
* @param array|string $input Passed input values to sanitize
* @return array|string Sanitized input fields
*/
public function sanitize_options( $input ) {
$new_input['caps'] = empty( $input['caps'] ) ? 0 : 1;
$new_input['only'] = empty( $input['only'] ) ? 0 : 1;
if ( is_multisite() ) {
$new_input['shared'] = empty( $input['shared'] ) ? 0 : 1;
}
return $new_input;
}
/**
* Settings field for avatar upload capabilities
*
* @param array $args Field arguments
*/
public function avatar_settings_field( $args ) {
$args = wp_parse_args(
$args,
array(
'key' => '',
'desc' => '',
'default' => 0,
)
);
if ( ! isset( $this->options[ $args['key'] ] ) ) {
$this->options[ $args['key'] ] = $args['default'];
}
if ( 'clear_cache' !== $args['key'] ) {
echo '
';
} else {
echo ' ';
echo '';
}
// Output warning if needed.
if (
SLA_IS_NETWORK // If network activated.
&& $this->is_enforced() // And in enforce mode.
&& 'shared' === $args['key'] // And we are displaying the last setting.
) {
echo '
' . esc_html__( 'Simple Local Avatar settings are currently enforced across all sites on the network.', 'simple-local-avatars' ) . '
';
}
}
/**
* Settings field for migrating avatars away from WP User Avatar
*/
public function migrate_from_wp_user_avatar_settings_field() {
printf(
'',
esc_attr( 'simple-local-avatars-migrate-from-wp-user-avatar' ),
esc_html__( 'Migrate avatars from WP User Avatar to Simple Local Avatars', 'simple-local-avatars' )
);
}
/**
* Output new Avatar fields to user editing / profile screen
*
* @param object $profileuser User object
*/
public function edit_user_profile( $profileuser ) {
?>
ID ) );
remove_filter( 'pre_option_avatar_rating', '__return_null' );
?>
simple_local_avatar ) ) {
echo '' . esc_html__( 'No local avatar is set. Set up your avatar at Gravatar.com.', 'simple-local-avatars' ) . '';
} else {
echo '' . esc_html__( 'You do not have media management permissions. To change your local avatar, contact the blog administrator.', 'simple-local-avatars' ) . '';
}
}
?>
avatar_delete( $user_id ); // delete old images if successful.
$meta_value = array();
// set the new avatar
if ( is_int( $url_or_media_id + 0 ) ) {
$meta_value['media_id'] = $url_or_media_id;
$url_or_media_id = wp_get_attachment_url( $url_or_media_id );
}
$meta_value['full'] = $url_or_media_id;
$meta_value['blog_id'] = get_current_blog_id();
update_user_meta( $user_id, $this->user_key, $meta_value ); // save user information (overwriting old).
}
/**
* Save any changes to the user profile
*
* @param int $user_id ID of user being updated
*/
public function edit_user_profile_update( $user_id ) {
// check nonces
if ( empty( $_POST['_simple_local_avatar_nonce'] ) || ! wp_verify_nonce( $_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce' ) ) {
return;
}
// check for uploaded files
if ( ! empty( $_FILES['simple-local-avatar']['name'] ) ) :
// need to be more secure since low privelege users can upload
if ( false !== strpos( $_FILES['simple-local-avatar']['name'], '.php' ) ) {
$this->avatar_upload_error = __( 'For security reasons, the extension ".php" cannot be in your file name.', 'simple-local-avatars' );
add_action( 'user_profile_update_errors', array( $this, 'user_profile_update_errors' ) );
return;
}
// front end (theme my profile etc) support
if ( ! function_exists( 'media_handle_upload' ) ) {
include_once ABSPATH . 'wp-admin/includes/media.php';
}
// allow developers to override file size upload limit for avatars
add_filter( 'upload_size_limit', array( $this, 'upload_size_limit' ) );
$this->user_id_being_edited = $user_id; // make user_id known to unique_filename_callback function
$avatar_id = media_handle_upload(
'simple-local-avatar',
0,
array(),
array(
'mimes' => array(
'jpg|jpeg|jpe' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
),
'test_form' => false,
'unique_filename_callback' => array( $this, 'unique_filename_callback' ),
)
);
remove_filter( 'upload_size_limit', array( $this, 'upload_size_limit' ) );
if ( is_wp_error( $avatar_id ) ) { // handle failures.
$this->avatar_upload_error = '' . __( 'There was an error uploading the avatar:', 'simple-local-avatars' ) . ' ' . esc_html( $avatar_id->get_error_message() );
add_action( 'user_profile_update_errors', array( $this, 'user_profile_update_errors' ) );
return;
}
$this->assign_new_user_avatar( $avatar_id, $user_id );
endif;
// Handle ratings
if ( isset( $avatar_id ) || get_user_meta( $user_id, $this->user_key, true ) ) {
if ( empty( $_POST['simple_local_avatar_rating'] ) || ! array_key_exists( $_POST['simple_local_avatar_rating'], $this->avatar_ratings ) ) {
$_POST['simple_local_avatar_rating'] = key( $this->avatar_ratings );
}
update_user_meta( $user_id, $this->rating_key, $_POST['simple_local_avatar_rating'] );
}
}
/**
* Allow developers to override the maximum allowable file size for avatar uploads
*
* @param int $bytes WordPress default byte size check
* @return int Maximum byte size
*/
public function upload_size_limit( $bytes ) {
return apply_filters( 'simple_local_avatars_upload_limit', $bytes );
}
/**
* Runs when a user clicks the Remove button for the avatar
*/
public function action_remove_simple_local_avatar() {
if ( ! empty( $_GET['user_id'] ) && ! empty( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'remove_simple_local_avatar_nonce' ) ) {
$user_id = (int) $_GET['user_id'];
if ( ! current_user_can( 'edit_user', $user_id ) ) {
wp_die( esc_html__( 'You do not have permission to edit this user.', 'simple-local-avatars' ) );
}
$this->avatar_delete( $user_id ); // delete old images if successful
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
echo wp_kses_post( get_simple_local_avatar( $user_id ) );
}
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
die;
}
}
/**
* AJAX callback for assigning media ID fetched from media library to user
*/
public function ajax_assign_simple_local_avatar_media() {
// check required information and permissions
if ( empty( $_POST['user_id'] ) || empty( $_POST['media_id'] ) || ! current_user_can( 'upload_files' ) || ! current_user_can( 'edit_user', $_POST['user_id'] ) || empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'assign_simple_local_avatar_nonce' ) ) {
die;
}
$media_id = (int) $_POST['media_id'];
$user_id = (int) $_POST['user_id'];
// ensure the media is real is an image
if ( wp_attachment_is_image( $media_id ) ) {
$this->assign_new_user_avatar( $media_id, $user_id );
}
echo wp_kses_post( get_simple_local_avatar( $user_id ) );
die;
}
/**
* Delete avatars based on a user_id
*
* @param int $user_id User ID.
*/
public function avatar_delete( $user_id ) {
$old_avatars = (array) get_user_meta( $user_id, $this->user_key, true );
if ( empty( $old_avatars ) ) {
return;
}
// if it was uploaded media, don't erase the full size or try to erase an the ID
if ( array_key_exists( 'media_id', $old_avatars ) ) {
unset( $old_avatars['media_id'], $old_avatars['full'] );
}
if ( ! empty( $old_avatars ) ) {
$upload_path = wp_upload_dir();
foreach ( $old_avatars as $old_avatar ) {
// derive the path for the file based on the upload directory
$old_avatar_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $old_avatar );
if ( file_exists( $old_avatar_path ) ) {
unlink( $old_avatar_path );
}
}
}
delete_user_meta( $user_id, $this->user_key );
delete_user_meta( $user_id, $this->rating_key );
}
/**
* Creates a unique, meaningful file name for uploaded avatars.
*
* @param string $dir Path for file
* @param string $name Filename
* @param string $ext File extension (e.g. ".jpg")
* @return string Final filename
*/
public function unique_filename_callback( $dir, $name, $ext ) {
$user = get_user_by( 'id', (int) $this->user_id_being_edited );
$name = $base_name = sanitize_file_name( $user->display_name . '_avatar_' . time() ); //phpcs:ignore
// ensure no conflicts with existing file names
$number = 1;
while ( file_exists( $dir . "/$name$ext" ) ) {
$name = $base_name . '_' . $number;
$number ++;
}
return $name . $ext;
}
/**
* Adds errors based on avatar upload problems.
*
* @param WP_Error $errors Error messages for user profile screen.
*/
public function user_profile_update_errors( WP_Error $errors ) {
$errors->add( 'avatar_error', $this->avatar_upload_error );
}
/**
* Registers the simple_local_avatar field in the REST API.
*/
public function register_rest_fields() {
register_rest_field(
'user',
'simple_local_avatar',
array(
'get_callback' => array( $this, 'get_avatar_rest' ),
'update_callback' => array( $this, 'set_avatar_rest' ),
'schema' => array(
'description' => 'The users simple local avatar',
'type' => 'object',
),
)
);
}
/**
* Returns the simple_local_avatar meta key for the given user.
*
* @param object $user User object
*/
public function get_avatar_rest( $user ) {
$local_avatar = get_user_meta( $user['id'], $this->user_key, true );
if ( empty( $local_avatar ) ) {
return;
}
return $local_avatar;
}
/**
* Updates the simple local avatar from a REST request.
*
* Since we are just adding a field to the existing user endpoint
* we don't need to worry about ensuring the calling user has proper permissions.
* Only the user or an administrator would be able to change the avatar.
*
* @param array $input Input submitted via REST request.
* @param object $user The user making the request.
*/
public function set_avatar_rest( $input, $user ) {
$this->assign_new_user_avatar( $input['media_id'], $user->ID );
}
/**
* Short-circuit filter the `simple_local_avatars` option to match network if necessary
*
* @param bool $value Value of `simple_local_avatars` option, typically false.
*
* @return array
*/
public function pre_option_simple_local_avatars( $value ) {
if ( SLA_IS_NETWORK && 'enforce' === $this->get_network_mode() ) {
$value = get_site_option( 'simple_local_avatars', array() );
}
return $value;
}
/**
* Set plugin defaults for a new site
*
* @param int $blog_id Blog ID.
*/
public function set_defaults( $blog_id ) {
if ( 'enforce' === $this->get_network_mode() ) {
return;
}
switch_to_blog( $blog_id );
update_option( 'simple_local_avatars', $this->sanitize_options( $this->options ) );
restore_current_blog();
}
/**
* Add some basic styling on the Discussion page
*/
public function admin_print_styles() {
?>
is_enforced() ) {
$classes .= ' sla-enforced';
}
return $classes;
}
/**
* Overwriting existing avatar_ratings so this can be called just before the rating strings would be used so that
* translations will work correctly.
* Default text-domain because the strings have already been translated
*/
private function update_avatar_ratings() {
$this->avatar_ratings = array(
'G' => __( 'G — Suitable for all audiences' ),
'PG' => __( 'PG — Possibly offensive, usually for audiences 13 and above' ),
'R' => __( 'R — Intended for adult audiences above 17' ),
'X' => __( 'X — Even more mature than above' ),
);
}
/**
* Clear user cache.
*/
public function sla_clear_user_cache() {
check_ajax_referer( 'sla_clear_cache_nonce', 'nonce' );
$step = isset( $_REQUEST['step'] ) ? intval( $_REQUEST['step'] ) : 1;
// Setup defaults.
$users_per_page = 50;
$offset = ( $step - 1 ) * $users_per_page;
$users_query = new \WP_User_Query(
array(
'fields' => array( 'ID' ),
'number' => $users_per_page,
'offset' => $offset,
)
);
// Total users in the site.
$total_users = $users_query->get_total();
// Get the users.
$users = $users_query->get_results();
if ( ! empty( $users ) ) {
foreach ( $users as $user ) {
$user_id = $user->ID;
$local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
$media_id = isset( $local_avatars['media_id'] ) ? $local_avatars['media_id'] : '';
$this->clear_user_avatar_cache( $local_avatars, $user_id, $media_id );
}
wp_send_json_success(
array(
'step' => $step + 1,
'message' => sprintf(
/* translators: 1: Offset, 2: Total users */
esc_html__( 'Processing %1$s/%2$s users...', 'simple-local-avatars' ),
$offset,
$total_users
),
)
);
}
wp_send_json_success(
array(
'step' => 'done',
'message' => sprintf(
/* translators: %s Total users */
esc_html__( 'Completed clearing cache for all %s user(s) avatars.', 'simple-local-avatars' ),
$total_users
),
)
);
}
/**
* Clear avatar cache for given user.
*
* @param array $local_avatars Local avatars.
* @param int $user_id User ID.
* @param mixed $media_id Media ID.
*/
private function clear_user_avatar_cache( $local_avatars, $user_id, $media_id ) {
if ( ! empty( $media_id ) ) {
$file_name_data = pathinfo( wp_get_original_image_path( $media_id ) );
$file_dir_name = $file_name_data['dirname'];
$file_name = $file_name_data['filename'];
$file_ext = $file_name_data['extension'];
foreach ( $local_avatars as $local_avatars_key => $local_avatar_value ) {
if ( ! in_array( $local_avatars_key, [ 'media_id', 'full' ], true ) ) {
$file_size_path = sprintf( '%1$s/%2$s-%3$sx%3$s.%4$s', $file_dir_name, $file_name, $local_avatars_key, $file_ext );
if ( ! file_exists( $file_size_path ) ) {
unset( $local_avatars[ $local_avatars_key ] );
}
}
}
// Update meta, remove sizes that don't exist.
update_user_meta( $user_id, 'simple_local_avatar', $local_avatars );
}
}
/**
* Add default avatar upload file field.
*
* @param array $defaults Default options for avatar.
*
* @return array Default options of avatar.
*/
public function add_avatar_default_field( $defaults ) {
if ( ! did_action( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
$default_avatar_file_url = '';
$default_avatar_file_id = get_option( 'simple_local_avatar_default', '' );
if ( ! empty( $default_avatar_file_id ) ) {
$default_avatar_file_url = wp_get_attachment_image_url( $default_avatar_file_id );
}
ob_start();
?>
blog_id = 1;
$sites = array( $site );
}
// Bail early if we don't find sites.
if ( empty( $sites ) ) {
return $count;
}
foreach ( $sites as $site ) {
// Get the blog ID to use in the meta key and user query.
$blog_id = isset( $site->blog_id ) ? $site->blog_id : 1;
// Get the name of the meta key for WP User Avatar.
$meta_key = $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar';
// Get processed users from database.
$migrations = get_option( 'simple_local_avatars_migrations', array() );
$processed_users = isset( $migrations['wp_user_avatar'] ) ? $migrations['wp_user_avatar'] : array();
// Get all users that have a local avatar.
$users = get_users(
array(
'blog_id' => $blog_id,
'exclude' => $processed_users,
'meta_key' => $meta_key,
'meta_compare' => 'EXISTS',
)
);
// Bail early if we don't find users.
if ( empty( $users ) ) {
continue;
}
foreach ( $users as $user ) {
// Get the existing avatar media ID.
$avatar_id = get_user_meta( $user->ID, $meta_key, true );
// Attach the user and media to Simple Local Avatars.
$sla = new Simple_Local_Avatars();
$sla->assign_new_user_avatar( (int) $avatar_id, $user->ID );
// Check that it worked.
$is_migrated = get_user_meta( $user->ID, 'simple_local_avatar', true );
if ( ! empty( $is_migrated ) ) {
// Build array of user IDs.
$migrations['wp_user_avatar'][] = $user->ID;
// Record the user IDs so we don't process a second time.
$is_saved = update_option( 'simple_local_avatars_migrations', $migrations );
// Record how many avatars we migrate to be used in our messaging.
if ( $is_saved ) {
$count ++;
}
}
}
}
return $count;
}
/**
* Migrate the user's avatar data away from WP User Avatar/ProfilePress via the dashboard.
*
* Sends the number of avatars processed back to the AJAX response before stopping execution.
*
* @return void
*/
public function ajax_migrate_from_wp_user_avatar() {
// Bail early if nonce is not available.
if ( empty( sanitize_text_field( $_POST['migrateFromWpUserAvatarNonce'] ) ) ) {
die;
}
// Bail early if nonce is invalid.
if ( ! wp_verify_nonce( sanitize_text_field( $_POST['migrateFromWpUserAvatarNonce'] ), 'migrate_from_wp_user_avatar_nonce' ) ) {
die();
}
// Run the migration script and store the number of avatars processed.
$count = $this->migrate_from_wp_user_avatar();
// Create the array we send back to javascript here.
$array_we_send_back = array( 'count' => $count );
// Make sure to json encode the output because that's what it is expecting.
echo wp_json_encode( $array_we_send_back );
// Make sure you die when finished doing ajax output.
wp_die();
}
/**
* Migrate the user's avatar data from WP User Avatar/ProfilePress via the command line.
*
* ## OPTIONS
*
* [--yes]
* : Skips the confirmations (for automated systems).
*
* ## EXAMPLES
*
* $ wp simple-local-avatars migrate wp-user-avatar
* Success: Number of avatars successfully migrated from WP User Avatar: 5
*
* @param array $args The arguments.
* @param array $assoc_args The associative arguments.
*
* @return void
*/
public function wp_cli_migrate_from_wp_user_avatar( $args, $assoc_args ) {
// Argument --yes to prevent confirmation (for automated systems).
if ( ! isset( $assoc_args['yes'] ) ) {
WP_CLI::confirm( esc_html__( 'Do you want to migrate avatars from WP User Avatar?', 'simple-local-avatars' ) );
}
// Run the migration script and store the number of avatars processed.
$count = $this->migrate_from_wp_user_avatar();
// Error out if we don't process any avatars.
if ( 0 === absint( $count ) ) {
WP_CLI::error( esc_html__( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ) );
}
WP_CLI::success(
sprintf(
'%s: %s',
esc_html__( 'Number of avatars successfully migrated from WP User Avatar', 'simple-local-avatars' ),
esc_html( $count )
)
);
}
}