WEBMENTION_AVATAR_SIZE, 'd' => $default, // Replace with our site default. 'r' => $rating, ), $url ); $file = download_url( $url, 300 ); if ( is_wp_error( $file ) ) { return false; } @move_uploaded_file( $file, $filepath ); return self::upload_directory( $filehandle, true ); } // Allow for common query parameters in image APIs to get a better quality image. $query = array(); wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query ); if ( array_key_exists( 's', $query ) && is_numeric( $query['s'] ) ) { $url = str_replace( 's=' . $query['s'], 's=' . WEBMENTION_AVATAR_SIZE, $url ); } if ( array_key_exists( 'width', $query ) && array_key_exists( 'height', $query ) ) { $url = str_replace( 'width=' . $query['width'], 'width=' . WEBMENTION_AVATAR_SIZE, $url ); $url = str_replace( 'height=' . $query['height'], 'height=' . WEBMENTION_AVATAR_SIZE, $url ); } // Download Profile Picture and add as attachment $file = wp_get_image_editor( download_url( $url, 300 ) ); if ( is_wp_error( $file ) ) { return false; } $file->resize( null, WEBMENTION_AVATAR_SIZE, true ); $file->set_quality( WEBMENTION_AVATAR_QUALITY ); $file->save( $filepath, 'image/jpg' ); return self::upload_directory( $filehandle, true ); } /** * Given an Avatar URL return the filepath. * * @param string $url URL. * @return string Filepath. */ public static function avatar_url_to_filepath( $url ) { if ( ! str_contains( self::upload_directory( '', true ), $url ) ) { return false; } $path = str_replace( self::upload_directory( '', true ), '', $url ); return self::upload_directory( $path ); } /** * Delete Avatar File. * * @param string $url Avatar to Delete. * @return boolean True if successful. False if not. */ public static function delete_avatar_file( $url ) { $filepath = self::avatar_url_to_filepath( $url ); if ( empty( $filepath ) ) { return false; } if ( file_exists( $filepath ) ) { wp_delete_file( $filepath ); return true; } return false; } /** * Delete Avatar. * * @param int $comment_ID */ public static function delete_avatar( $comment_id ) { $comment = get_comment( $comment_id ); if ( ! $comment ) { return false; } $url = get_comment_meta( $comment_id, 'avatar', true ); self::delete_avatar_file( $url ); } /** * Store Avatars locally * * @param int $comment_ID */ public static function store_avatar( $comment_id ) { $comment = get_comment( $comment_id ); if ( ! $comment ) { return false; } // Do not try to store the avatar if there is a User ID. Let something else handle that. if ( $comment->user_id ) { return false; } $avatar = \Webmention\Avatar::get_avatar_meta( $comment ); if ( ! $avatar ) { return false; } $author = normalize_url( get_comment_author_url( $comment ) ); // Do not try to store if no author URL. if ( empty( $author ) ) { return false; } $host = webmention_extract_domain( get_url_from_webmention( $comment ) ); $avatar_url = self::sideload_avatar( $avatar, $host, $author ); if ( $avatar_url ) { delete_comment_meta( $comment->comment_ID, 'semantic_linkbacks_avatar' ); update_comment_meta( $comment->comment_ID, 'avatar', $avatar_url ); } } }