示例#1
0
 /**
  * Process ajax user avatar upload request.
  * Sanitize file and pass to upload_file(). Rename image to md5 and store file
  * name in user meta. Also remove existing avtar if exists
  * @return void
  */
 public function avatar_upload()
 {
     if (ap_user_can_upload_avatar() && ap_verify_nonce('upload_avatar_' . get_current_user_id())) {
         $photo = $this->upload_photo('thumbnail');
         if (false === $photo) {
             ap_send_json(ap_ajax_responce(array('message' => $this->upload_error, 'message_type' => 'error')));
         }
         $file = str_replace('\\', '\\\\', $photo['file']);
         $photo['file'] = $file;
         $photo['small_url'] = str_replace(basename($photo['url']), 'small_' . basename($photo['url']), $photo['url']);
         $small_name = str_replace(basename($photo['file']), 'small_' . basename($photo['file']), $photo['file']);
         $photo['small_file'] = $small_name;
         $userid = get_current_user_id();
         // Remove previous image.
         $previous_avatar = get_user_meta($userid, '_ap_avatar', true);
         if ($previous_avatar['file'] && file_exists($previous_avatar['file'])) {
             unlink($previous_avatar['file']);
         }
         if ($previous_avatar['small_file'] && file_exists($previous_avatar['small_file'])) {
             unlink($previous_avatar['small_file']);
         }
         // Resize thumbnail.
         $image = wp_get_image_editor($file);
         if (!is_wp_error($image)) {
             $image->resize(200, 200, true);
             $image->save($file);
             $image->resize(50, 50, true);
             $image->save($small_name);
         }
         update_user_meta($userid, '_ap_avatar', $photo);
         do_action('ap_after_avatar_upload', $userid, $photo);
         ap_ajax_json(array('status' => true, 'action' => 'avatar_uploaded', 'user_id' => $userid, 'message' => __('Avatar uploaded successfully.', 'anspress-question-answer'), 'html' => get_avatar($userid, 150)));
     }
     ap_ajax_json(array('message' => __('There was an error while uploading avatar, please check your image', 'anspress-question-answer'), 'message_type' => 'error'));
 }
示例#2
0
文件: user.php 项目: Byrlyne/anspress
/**
 * Output user profile photo upload form
 * @return void
 * @since 2.1.5
 */
function ap_avatar_upload_form()
{
    if (ap_user_can_upload_avatar()) {
        ?>
            <form method="post" action="#" enctype="multipart/form-data" data-action="ap_upload_form" class="ap-avatar-upload-form">
            <div class="ap-btn ap-upload-o <?php 
        echo ap_icon('upload');
        ?>
" title="<?php 
        _e('Upload an avatar', 'ap');
        ?>
">
                <span><?php 
        _e('Upload avatar', 'ap');
        ?>
</span>
                <input type="file" name="thumbnail" class="ap-upload-input" data-action="ap_upload_field">
            </div>
            <input type='hidden' value='<?php 
        echo wp_create_nonce('upload_avatar_' . get_current_user_id());
        ?>
' name='__nonce' />
            <input type="hidden" name="action" id="action" value="ap_avatar_upload">
        </form>
        <?php 
    }
}