add_hooks(); $this->options = (array) get_option( 'simple_local_avatars' ); $this->user_key = 'simple_local_avatar'; $this->rating_key = 'simple_local_avatar_rating'; $this->avatar_ratings = array( 'G' => __( 'G — Suitable for all audiences', 'simple-local-avatars' ), 'PG' => __( 'PG — Possibly offensive, usually for audiences 13 and above', 'simple-local-avatars' ), 'R' => __( 'R — Intended for adult audiences above 17', 'simple-local-avatars' ), 'X' => __( 'X — Even more mature than above', 'simple-local-avatars' ), ); if ( ! $this->is_avatar_shared() // Are we sharing avatars? && ( ( // And either an ajax request not in the network admin. defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_SERVER['HTTP_REFERER'] ) && ! preg_match( '#^' . network_admin_url() . '#i', $_SERVER['HTTP_REFERER'] ) ) || ( // Or normal request not in the network admin. ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && ! is_network_admin() ) ) && is_multisite() ) { $this->user_key = sprintf( $this->user_key . '_%d', get_current_blog_id() ); $this->rating_key = sprintf( $this->rating_key . '_%d', get_current_blog_id() ); } } /** * Register actions and filters. */ public function add_hooks() { global $pagenow; add_filter( 'plugin_action_links_' . SLA_PLUGIN_BASENAME, array( $this, 'plugin_filter_action_links' ) ); add_filter( 'pre_get_avatar_data', array( $this, 'get_avatar_data' ), 10, 2 ); add_filter( 'pre_option_simple_local_avatars', array( $this, 'pre_option_simple_local_avatars' ), 10, 1 ); add_action( 'admin_init', array( $this, 'admin_init' ) ); // Load the JS on BE & FE both, in order to support third party plugins like bbPress. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) ); add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) ); add_action( 'personal_options_update', array( $this, 'edit_user_profile_update' ) ); add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) ); add_action( 'admin_action_remove-simple-local-avatar', array( $this, 'action_remove_simple_local_avatar' ) ); add_action( 'wp_ajax_assign_simple_local_avatar_media', array( $this, 'ajax_assign_simple_local_avatar_media' ) ); add_action( 'wp_ajax_remove_simple_local_avatar', array( $this, 'action_remove_simple_local_avatar' ) ); add_action( 'user_edit_form_tag', array( $this, 'user_edit_form_tag' ) ); add_action( 'rest_api_init', array( $this, 'register_rest_fields' ) ); add_action( 'wp_ajax_migrate_from_wp_user_avatar', array( $this, 'ajax_migrate_from_wp_user_avatar' ) ); if ( defined( 'WP_CLI' ) && WP_CLI ) { WP_CLI::add_command( 'simple-local-avatars migrate wp-user-avatar', array( $this, 'wp_cli_migrate_from_wp_user_avatar' ) ); } add_action( 'wp_ajax_sla_clear_user_cache', array( $this, 'sla_clear_user_cache' ) ); add_filter( 'avatar_defaults', array( $this, 'add_avatar_default_field' ) ); add_action( 'wpmu_new_blog', array( $this, 'set_defaults' ) ); if ( 'profile.php' === $pagenow ) { add_filter( 'media_view_strings', function ( $strings ) { $strings['skipCropping'] = esc_html__( 'Default Crop', 'simple-local-avatars' ); return $strings; }, 10, 1 ); } } /** * Determine if plugin is network activated. * * @param string $plugin The plugin slug to check. * * @return boolean */ public static function is_network( $plugin ) { $plugins = get_site_option( 'active_sitewide_plugins', array() ); if ( is_multisite() && isset( $plugins[ $plugin ] ) ) { return true; } return false; } /** * Get current plugin network mode */ public function get_network_mode() { if ( SLA_IS_NETWORK ) { return get_site_option( 'simple_local_avatars_mode', 'default' ); } return 'default'; } /** * Determines if settings handling is enforced on a network level * * Important: this is only meant for admin UI purposes. * * @return boolean */ public function is_enforced() { if ( ( ! is_network_admin() && ( SLA_IS_NETWORK && 'enforce' === $this->get_network_mode() ) ) ) { return true; } return false; } /** * Determine if avatars should be shared * * @return boolean */ public function is_avatar_shared() { if ( is_multisite() // Are we on multisite. && ! isset( $this->options['shared'] ) // And our shared option doesn't exist. || ( isset( $this->options['shared'] ) // Or our shared option is set. && 1 === $this->options['shared'] ) ) { return true; } return false; } /** * Add the settings action link to the plugin page. * * @param array $links The Action links for the plugin. * * @return array */ public function plugin_filter_action_links( $links ) { if ( ! is_array( $links ) ) { return $links; } $links['settings'] = sprintf( ' %s ', esc_url( admin_url( 'options-discussion.php' ) ), __( 'Settings', 'simple-local-avatars' ) ); return $links; } /** * Retrieve the local avatar for a user who provided a user ID, email address or post/comment object. * * @param string $avatar Avatar return by original function * @param int|string|object $id_or_email A user ID, email address, or post/comment object * @param int $size Size of the avatar image * @param string $default URL to a default image to use if no avatar is available * @param string $alt Alternative text to use in image tag. Defaults to blank * @param array $args Optional. Extra arguments to retrieve the avatar. * * @return string tag for the user's avatar */ public function get_avatar( $avatar = '', $id_or_email = '', $size = 96, $default = '', $alt = '', $args = array() ) { return apply_filters( 'simple_local_avatar', get_avatar( $id_or_email, $size, $default, $alt, $args ) ); } /** * Filter avatar data early to add avatar url if needed. This filter hooks * before Gravatar setup to prevent wasted requests. * * @since 2.2.0 * * @param array $args Arguments passed to get_avatar_data(), after processing. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash, * user email, WP_User object, WP_Post object, or WP_Comment object. */ public function get_avatar_data( $args, $id_or_email ) { if ( ! empty( $args['force_default'] ) ) { return $args; } $simple_local_avatar_url = $this->get_simple_local_avatar_url( $id_or_email, $args['size'] ); if ( $simple_local_avatar_url ) { $args['url'] = $simple_local_avatar_url; } // Local only mode if ( ! $simple_local_avatar_url && ! empty( $this->options['only'] ) ) { $args['url'] = $this->get_default_avatar_url( $args['size'] ); } if ( ! empty( $args['url'] ) ) { $args['found_avatar'] = true; // If custom alt text isn't passed, pull alt text from the local image. if ( empty( $args['alt'] ) ) { $args['alt'] = $this->get_simple_local_avatar_alt( $id_or_email ); } } return $args; } /** * Get a user ID from certain possible values. * * @since 2.5.0 * * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash, * user email, WP_User object, WP_Post object, or WP_Comment object. * @return int|false */ public function get_user_id( $id_or_email ) { $user_id = false; if ( is_numeric( $id_or_email ) ) { $user_id = (int) $id_or_email; } elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) ) { $user_id = (int) $id_or_email->user_id; } elseif ( $id_or_email instanceof WP_Post && ! empty( $id_or_email->post_author ) ) { $user_id = (int) $id_or_email->post_author; } elseif ( is_string( $id_or_email ) ) { $user = get_user_by( 'email', $id_or_email ); $user_id = $user ? $user->ID : ''; } return $user_id; } /** * Get local avatar url. * * @since 2.2.0 * * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash, * user email, WP_User object, WP_Post object, or WP_Comment object. * @param int $size Requested avatar size. */ public function get_simple_local_avatar_url( $id_or_email, $size ) { $user_id = $this->get_user_id( $id_or_email ); if ( empty( $user_id ) ) { return ''; } // 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'] ) ) { return ''; } // check rating $avatar_rating = get_user_meta( $user_id, $this->rating_key, true ); $site_rating = get_option( 'avatar_rating' ); if ( ! empty( $avatar_rating ) && 'G' !== $avatar_rating && $site_rating ) { $ratings = array_keys( $this->avatar_ratings ); $site_rating_weight = array_search( $site_rating, $ratings, true ); $avatar_rating_weight = array_search( $avatar_rating, $ratings, true ); if ( false !== $avatar_rating_weight && $avatar_rating_weight > $site_rating_weight ) { return ''; } } // handle "real" media if ( ! empty( $local_avatars['media_id'] ) ) { // If using shared avatars, make sure we validate the URL on the main site. if ( $this->is_avatar_shared() ) { $origin_blog_id = isset( $local_avatars['blog_id'] ) && ! empty( $local_avatars['blog_id'] ) ? $local_avatars['blog_id'] : get_main_site_id(); switch_to_blog( $origin_blog_id ); } $avatar_full_path = get_attached_file( $local_avatars['media_id'] ); if ( $this->is_avatar_shared() ) { restore_current_blog(); } // has the media been deleted? if ( ! $avatar_full_path ) { return ''; } } $size = (int) $size; // Generate a new size. if ( ! array_key_exists( $size, $local_avatars ) ) { $local_avatars[ $size ] = $local_avatars['full']; // just in case of failure elsewhere // allow automatic rescaling to be turned off if ( apply_filters( 'simple_local_avatars_dynamic_resize', true ) ) : $upload_path = wp_upload_dir(); // get path for image by converting URL, unless its already been set, thanks to using media library approach if ( ! isset( $avatar_full_path ) ) { $avatar_full_path = str_replace( $upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full'] ); } // generate the new size $editor = wp_get_image_editor( $avatar_full_path ); if ( ! is_wp_error( $editor ) ) { $resized = $editor->resize( $size, $size, true ); if ( ! is_wp_error( $resized ) ) { $dest_file = $editor->generate_filename(); $saved = $editor->save( $dest_file ); if ( ! is_wp_error( $saved ) ) { // Transform the destination file path into URL. $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' ) ) { $dest_file_url = str_replace( ABSPATH . 'wp-content/uploads', network_site_url( '/wp-content/uploads' ), $dest_file ); } $local_avatars[ $size ] = $dest_file_url; } } } // save updated avatar sizes update_user_meta( $user_id, $this->user_key, $local_avatars ); endif; } if ( 'http' !== substr( $local_avatars[ $size ], 0, 4 ) ) { $local_avatars[ $size ] = home_url( $local_avatars[ $size ] ); } return esc_url( $local_avatars[ $size ] ); } /** * Get local avatar alt text. * * @since 2.5.0 * * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash, * user email, WP_User object, WP_Post object, or WP_Comment object. * @return string */ public function get_simple_local_avatar_alt( $id_or_email ) { $user_id = $this->get_user_id( $id_or_email ); if ( empty( $user_id ) ) { return ''; } // 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'] ) ) { return ''; } return esc_attr( get_post_meta( $local_avatars['media_id'], '_wp_attachment_image_alt', true ) ); } /** * Get default avatar url * * @since 2.2.0 * * @param int $size Requested avatar size. */ public function get_default_avatar_url( $size ) { if ( empty( $default ) ) { $avatar_default = get_option( 'avatar_default' ); if ( empty( $avatar_default ) ) { $default = 'mystery'; } else { $default = $avatar_default; } } $host = is_ssl() ? 'https://secure.gravatar.com' : 'http://0.gravatar.com'; if ( 'mystery' === $default ) { $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com') } elseif ( 'blank' === $default ) { $default = includes_url( 'images/blank.gif' ); } elseif ( 'gravatar_default' === $default ) { $default = "$host/avatar/?s={$size}"; } elseif ( 'simple_local_avatar' === $default ) { $default = "$host/avatar/?d=$default&s={$size}"; $default_avatar_id = get_option( 'simple_local_avatar_default', '' ); if ( ! empty( $default_avatar_id ) ) { $default = wp_get_attachment_image_url( $default_avatar_id ); } } else { $default = "$host/avatar/?d=$default&s={$size}"; } return $default; } /** * Register admin settings. */ public function admin_init() { // upgrade pre 2.0 option $old_ops = get_option( 'simple_local_avatars_caps' ); if ( $old_ops ) { if ( ! empty( $old_ops['simple_local_avatars_caps'] ) ) { update_option( 'simple_local_avatars', array( 'caps' => 1 ) ); } delete_option( 'simple_local_avatar_caps' ); } register_setting( 'discussion', 'simple_local_avatars', array( $this, 'sanitize_options' ) ); add_settings_field( 'simple-local-avatars-only', __( 'Local Avatars Only', 'simple-local-avatars' ), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars', array( 'class' => 'simple-local-avatars', 'key' => 'only', 'desc' => __( 'Only allow local avatars (still uses Gravatar for default avatars)', 'simple-local-avatars' ), ) ); add_settings_field( 'simple-local-avatars-caps', __( 'Local Upload Permissions', 'simple-local-avatars' ), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars', array( 'class' => 'simple-local-avatars', 'key' => 'caps', 'desc' => __( 'Only allow users with file upload capabilities to upload local avatars (Authors and above)', 'simple-local-avatars' ), ) ); if ( is_multisite() ) { add_settings_field( 'simple-local-avatars-shared', __( 'Shared network avatars', 'simple-local-avatars' ), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars', array( 'class' => 'simple-local-avatars', 'key' => 'shared', 'desc' => __( 'Uploaded avatars will be shared across the entire network, instead of being unique per site', 'simple-local-avatars' ), 'default' => 1, ) ); } add_action( 'load-options-discussion.php', array( $this, 'load_discussion_page' ) ); // This is for network site settings. if ( SLA_IS_NETWORK && is_network_admin() ) { add_action( 'load-settings.php', array( $this, 'load_network_settings' ) ); } add_settings_field( 'simple-local-avatars-migration', __( 'Migrate Other Local Avatars', 'simple-local-avatars' ), array( $this, 'migrate_from_wp_user_avatar_settings_field' ), 'discussion', 'avatars' ); add_settings_field( 'simple-local-avatars-clear', esc_html__( 'Clear local avatar cache', 'simple-local-avatars' ), array( $this, 'avatar_settings_field' ), 'discussion', 'avatars', array( 'key' => 'clear_cache', 'desc' => esc_html__( 'Clear cache of stored avatars', 'simple-local-avatars' ), ) ); // Save default avatar file. $this->save_default_avatar_file_id(); } /** * Fire code on the Discussion page */ public function load_discussion_page() { add_action( 'admin_print_styles', array( $this, 'admin_print_styles' ) ); add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); } /** * Load needed hooks to handle network settings */ public function load_network_settings() { $this->options = (array) get_site_option( 'simple_local_avatars', array() ); add_action( 'wpmu_options', array( $this, 'show_network_settings' ) ); add_action( 'update_wpmu_options', array( $this, 'save_network_settings' ) ); } /** * Show the network settings */ public function show_network_settings() { $mode = $this->get_network_mode(); ?>
avatar_settings_field( array( 'key' => 'only', 'desc' => __( 'Only allow local avatars (still uses Gravatar for default avatars) ', 'simple-local-avatars' ), ) ); ?> | |
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, ) ); ?> |
' . esc_html__( 'Simple Local Avatar settings are currently enforced across all sites on the network.', 'simple-local-avatars' ) . '
ID ) ); remove_filter( 'pre_option_avatar_rating', '__return_null' ); ?> |
options['caps'] );
}
if ( $upload_rights ) {
do_action( 'simple_local_avatar_notices' );
wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );
$remove_url = add_query_arg(
array(
'action' => 'remove-simple-local-avatar',
'user_id' => $profileuser->ID,
'_wpnonce' => $this->remove_nonce,
)
);
?>
simple_local_avatar ) ? ' style="display:none;"' : ''; ?>> 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' ) . ''; } } ?> |
|