updated plugin Simple Local Avatars
version 2.8.3
This commit is contained in:
@ -65,9 +65,9 @@ class Simple_Local_Avatars {
|
||||
public function __construct() {
|
||||
$this->add_hooks();
|
||||
|
||||
$this->options = (array) get_option( 'simple_local_avatars' );
|
||||
$this->user_key = 'simple_local_avatar';
|
||||
$this->rating_key = 'simple_local_avatar_rating';
|
||||
$this->options = (array) get_option( 'simple_local_avatars' );
|
||||
$this->user_key = 'simple_local_avatar';
|
||||
$this->rating_key = 'simple_local_avatar_rating';
|
||||
|
||||
if (
|
||||
! $this->is_avatar_shared() // Are we sharing avatars?
|
||||
@ -326,6 +326,20 @@ class Simple_Local_Avatars {
|
||||
return $user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the local avatar user meta.
|
||||
*
|
||||
* @param int $user_id User ID.
|
||||
* @return array Array with avatar data.
|
||||
*/
|
||||
public function get_user_local_avatar( $user_id ) {
|
||||
$local_avatars = get_user_meta( $user_id, $this->user_key, true );
|
||||
if ( ! is_array( $local_avatars ) || empty( $local_avatars ) ) {
|
||||
return [];
|
||||
}
|
||||
return $local_avatars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get local avatar url.
|
||||
*
|
||||
@ -343,8 +357,8 @@ class Simple_Local_Avatars {
|
||||
}
|
||||
|
||||
// Fetch local avatar from meta and make sure it's properly set.
|
||||
$local_avatars = get_user_meta( $user_id, $this->user_key, true );
|
||||
if ( empty( $local_avatars['full'] ) ) {
|
||||
$local_avatars = $this->get_user_local_avatar( $user_id );
|
||||
if ( ! isset( $local_avatars['full'] ) || empty( $local_avatars['full'] ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
@ -432,7 +446,7 @@ class Simple_Local_Avatars {
|
||||
$dest_file_url = '';
|
||||
if ( false !== strpos( $dest_file, $upload_path['basedir'] ) ) {
|
||||
$dest_file_url = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $dest_file );
|
||||
} else if ( is_multisite() && false !== strpos( $dest_file, ABSPATH . 'wp-content/uploads' ) ) {
|
||||
} elseif ( is_multisite() && false !== strpos( $dest_file, ABSPATH . 'wp-content/uploads' ) ) {
|
||||
$dest_file_url = str_replace( ABSPATH . 'wp-content/uploads', network_site_url( '/wp-content/uploads' ), $dest_file );
|
||||
}
|
||||
|
||||
@ -479,8 +493,8 @@ class Simple_Local_Avatars {
|
||||
}
|
||||
|
||||
// Fetch local avatar from meta and make sure we have a media ID.
|
||||
$local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
|
||||
if ( empty( $local_avatars['media_id'] ) ) {
|
||||
$local_avatars = $this->get_user_local_avatar( $user_id );
|
||||
if ( ! isset( $local_avatars['media_id'] ) || empty( $local_avatars['media_id'] ) ) {
|
||||
$alt = '';
|
||||
// If no avatar is set, check if we are using a default avatar with alt text.
|
||||
if ( 'simple_local_avatar' === get_option( 'avatar_default' ) ) {
|
||||
@ -557,13 +571,13 @@ class Simple_Local_Avatars {
|
||||
*/
|
||||
$this->avatar_ratings = array(
|
||||
/* translators: Content suitability rating: https://en.wikipedia.org/wiki/Motion_Picture_Association_of_America_film_rating_system */
|
||||
'G' => __( 'G — Suitable for all audiences' ),
|
||||
'G' => __( 'G — Suitable for all audiences', 'simple-local-avatars' ),
|
||||
/* translators: Content suitability rating: https://en.wikipedia.org/wiki/Motion_Picture_Association_of_America_film_rating_system */
|
||||
'PG' => __( 'PG — Possibly offensive, usually for audiences 13 and above' ),
|
||||
'PG' => __( 'PG — Possibly offensive, usually for audiences 13 and above', 'simple-local-avatars' ),
|
||||
/* translators: Content suitability rating: https://en.wikipedia.org/wiki/Motion_Picture_Association_of_America_film_rating_system */
|
||||
'R' => __( 'R — Intended for adult audiences above 17' ),
|
||||
'R' => __( 'R — Intended for adult audiences above 17', 'simple-local-avatars' ),
|
||||
/* translators: Content suitability rating: https://en.wikipedia.org/wiki/Motion_Picture_Association_of_America_film_rating_system */
|
||||
'X' => __( 'X — Even more mature than above' ),
|
||||
'X' => __( 'X — Even more mature than above', 'simple-local-avatars' ),
|
||||
);
|
||||
}
|
||||
|
||||
@ -702,7 +716,7 @@ class Simple_Local_Avatars {
|
||||
$this->avatar_settings_field(
|
||||
array(
|
||||
'key' => 'only',
|
||||
'desc' => __( 'Only allow local avatars (still uses Gravatar for default avatars) ', 'simple-local-avatars' ),
|
||||
'desc' => __( 'Only allow local avatars (still uses Gravatar for default avatars)', 'simple-local-avatars' ),
|
||||
)
|
||||
);
|
||||
?>
|
||||
@ -950,7 +964,7 @@ class Simple_Local_Avatars {
|
||||
if ( ! is_admin() || ! current_user_can( 'upload_files' ) ) {
|
||||
?>
|
||||
<p style="display: inline-block; width: 26em;">
|
||||
<span class="description"><?php esc_html_e( 'Choose an image from your computer:' ); ?></span><br />
|
||||
<span class="description"><?php esc_html_e( 'Choose an image from your computer:', 'simple-local-avatars' ); ?></span><br />
|
||||
<input type="file" name="simple-local-avatar" id="simple-local-avatar" class="standard-text" />
|
||||
</p>
|
||||
<?php } ?>
|
||||
@ -976,10 +990,10 @@ class Simple_Local_Avatars {
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="ratings-row">
|
||||
<th scope="row"><?php esc_html_e( 'Rating' ); ?></th>
|
||||
<th scope="row"><?php esc_html_e( 'Rating', 'simple-local-avatars' ); ?></th>
|
||||
<td colspan="2">
|
||||
<fieldset id="simple-local-avatar-ratings" <?php disabled( empty( $profileuser->simple_local_avatar ) ); ?>>
|
||||
<legend class="screen-reader-text"><span><?php esc_html_e( 'Rating' ); ?></span></legend>
|
||||
<legend class="screen-reader-text"><span><?php esc_html_e( 'Rating', 'simple-local-avatars' ); ?></span></legend>
|
||||
<?php
|
||||
if ( empty( $profileuser->simple_local_avatar_rating ) || ! array_key_exists( $profileuser->simple_local_avatar_rating, $this->avatar_ratings ) ) {
|
||||
$profileuser->simple_local_avatar_rating = 'G';
|
||||
@ -1035,7 +1049,7 @@ class Simple_Local_Avatars {
|
||||
*
|
||||
* @param int $user_id Id of the user who's avatar was updated
|
||||
*/
|
||||
do_action( 'simple_local_avatar_updated' , $user_id );
|
||||
do_action( 'simple_local_avatar_updated', $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1064,6 +1078,7 @@ class Simple_Local_Avatars {
|
||||
|
||||
$max_upload_size = $this->upload_size_limit( wp_max_upload_size() );
|
||||
if ( $_FILES['simple-local-avatar']['size'] > $max_upload_size ) {
|
||||
// translators: %s: Formatted size.
|
||||
$this->avatar_upload_error = sprintf( __( 'Max allowed avatar size is %s', 'simple-local-avatars' ), size_format( $max_upload_size ) );
|
||||
add_action( 'user_profile_update_errors', array( $this, 'user_profile_update_errors' ) );
|
||||
return;
|
||||
@ -1110,7 +1125,7 @@ class Simple_Local_Avatars {
|
||||
endif;
|
||||
|
||||
// Handle ratings
|
||||
if ( isset( $avatar_id ) || get_user_meta( $user_id, $this->user_key, true ) ) {
|
||||
if ( isset( $avatar_id ) || ! empty( $this->get_user_local_avatar( $user_id ) ) ) {
|
||||
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 );
|
||||
}
|
||||
@ -1180,7 +1195,7 @@ class Simple_Local_Avatars {
|
||||
* @param int $user_id User ID.
|
||||
*/
|
||||
public function avatar_delete( $user_id ) {
|
||||
$old_avatars = (array) get_user_meta( $user_id, $this->user_key, true );
|
||||
$old_avatars = $this->get_user_local_avatar( $user_id );
|
||||
|
||||
if ( empty( $old_avatars ) ) {
|
||||
return;
|
||||
@ -1191,10 +1206,20 @@ class Simple_Local_Avatars {
|
||||
unset( $old_avatars['media_id'], $old_avatars['full'] );
|
||||
}
|
||||
|
||||
// Remove the blog_id key as we don't need to try deleting a file based on that.
|
||||
if ( array_key_exists( 'blog_id', $old_avatars ) ) {
|
||||
unset( $old_avatars['blog_id'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $old_avatars ) ) {
|
||||
$upload_path = wp_upload_dir();
|
||||
|
||||
foreach ( $old_avatars as $old_avatar ) {
|
||||
// Ensure the avatar is in the uploads directory before we delete it.
|
||||
if ( strpos( $old_avatar, $upload_path['baseurl'] ) !== 0 ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 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 ) ) {
|
||||
@ -1271,7 +1296,7 @@ class Simple_Local_Avatars {
|
||||
* @param object $user User object
|
||||
*/
|
||||
public function get_avatar_rest( $user ) {
|
||||
$local_avatar = get_user_meta( $user['id'], $this->user_key, true );
|
||||
$local_avatar = $this->get_user_local_avatar( $user['id'] );
|
||||
if ( empty( $local_avatar ) ) {
|
||||
return;
|
||||
}
|
||||
@ -1288,9 +1313,34 @@ class Simple_Local_Avatars {
|
||||
*
|
||||
* @param array $input Input submitted via REST request.
|
||||
* @param object $user The user making the request.
|
||||
* @return null|\WP_Error
|
||||
*/
|
||||
public function set_avatar_rest( $input, $user ) {
|
||||
$this->assign_new_user_avatar( $input['media_id'], $user->ID );
|
||||
// Ensure media_id is set and is a number.
|
||||
if (
|
||||
empty( $input['media_id'] ) ||
|
||||
! is_numeric( $input['media_id'] )
|
||||
) {
|
||||
return new \WP_Error( 'invalid_media_id', esc_html__( 'Request did not contain a valid media_id field.', 'simple-local-avatars' ) );
|
||||
}
|
||||
|
||||
$attachment = get_post( (int) $input['media_id'] );
|
||||
|
||||
// Ensure this media_id is a valid attachment.
|
||||
if (
|
||||
! $attachment ||
|
||||
'attachment' !== $attachment->post_type ||
|
||||
! wp_attachment_is_image( $attachment )
|
||||
) {
|
||||
return new \WP_Error( 'invalid_media_id', esc_html__( 'Media ID did not match a valid attachment.', 'simple-local-avatars' ) );
|
||||
}
|
||||
|
||||
// Ensure this attachment is associated with this user.
|
||||
if ( (int) $attachment->post_author !== (int) $user->ID ) {
|
||||
return new \WP_Error( 'invalid_media_id', esc_html__( 'This attachment was not uploaded by this user.', 'simple-local-avatars' ) );
|
||||
}
|
||||
|
||||
$this->assign_new_user_avatar( (int) $input['media_id'], $user->ID );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1373,6 +1423,13 @@ class Simple_Local_Avatars {
|
||||
*/
|
||||
public function sla_clear_user_cache() {
|
||||
check_ajax_referer( 'sla_clear_cache_nonce', 'nonce' );
|
||||
|
||||
// Ensure this was run by a user with proper privileges.
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
// Match what `check_ajax_referer` does.
|
||||
wp_die( -1, 403 );
|
||||
}
|
||||
|
||||
$step = isset( $_REQUEST['step'] ) ? intval( $_REQUEST['step'] ) : 1;
|
||||
|
||||
// Setup defaults.
|
||||
@ -1396,7 +1453,7 @@ class Simple_Local_Avatars {
|
||||
if ( ! empty( $users ) ) {
|
||||
foreach ( $users as $user ) {
|
||||
$user_id = $user->ID;
|
||||
$local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
|
||||
$local_avatars = $this->get_user_local_avatar( $user_id );
|
||||
$media_id = isset( $local_avatars['media_id'] ) ? $local_avatars['media_id'] : '';
|
||||
$this->clear_user_avatar_cache( $local_avatars, $user_id, $media_id );
|
||||
}
|
||||
@ -1442,9 +1499,9 @@ class Simple_Local_Avatars {
|
||||
$file_name_data = pathinfo( get_attached_file( $media_id ) );
|
||||
}
|
||||
|
||||
$file_dir_name = $file_name_data['dirname'];
|
||||
$file_name = $file_name_data['filename'];
|
||||
$file_ext = $file_name_data['extension'];
|
||||
$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 );
|
||||
@ -1477,8 +1534,8 @@ class Simple_Local_Avatars {
|
||||
<input type="hidden" name="simple-local-avatar-file-id" id="simple-local-avatar-file-id" value="<?php echo ! empty( $default_avatar_file_id ) ? esc_attr( $default_avatar_file_id ) : ''; ?>"/>
|
||||
<input type="hidden" name="simple-local-avatar-file-url" id="simple-local-avatar-file-url" value="<?php echo ! empty( $default_avatar_file_url ) ? esc_url( $default_avatar_file_url ) : ''; ?>"/>
|
||||
<?php wp_nonce_field( 'simple_local_avatar_default', 'simple-local-avatar-file-wpnonce' ); ?>
|
||||
<input type="button" name="simple-local-avatar" id="simple-local-avatar-default" class="button-secondary" value="<?php esc_attr_e( 'Choose Default Avatar', 'simple-local-avatar' ); ?>"/>
|
||||
<p class="description" style="margin-left: 23px;"><?php esc_html_e( 'Note that this avatar needs to be publicly available or a broken image will be shown.', 'simple-local-avatar' ); ?></p>
|
||||
<input type="button" name="simple-local-avatar" id="simple-local-avatar-default" class="button-secondary" value="<?php esc_attr_e( 'Choose Default Avatar', 'simple-local-avatars' ); ?>"/>
|
||||
<p class="description" style="margin-left: 23px;"><?php esc_html_e( 'Note that this avatar needs to be publicly available or a broken image will be shown.', 'simple-local-avatars' ); ?></p>
|
||||
<?php
|
||||
$defaults['simple_local_avatar'] = ob_get_clean();
|
||||
|
||||
|
Reference in New Issue
Block a user