Пример #1
0
 public function avatar_upload()
 {
     if (ap_user_can_upload_cover() && wp_verify_nonce($_POST['nonce'], 'upload')) {
         $attach_id = $this->upload_file();
         $userid = get_current_user_id();
         $previous_avatar = get_user_meta($userid, '_ap_avatar', true);
         wp_delete_attachment($previous_avatar, true);
         update_user_meta($userid, '_ap_avatar', $attach_id);
         $result = array('status' => true, 'message' => __('Avatar uploaded successfully.', 'ap'), 'view' => '[data-view="avatar-main"]', 'image' => get_avatar($userid, 105));
         do_action('ap_after_avatar_upload', $userid, $attach_id);
     } else {
         $result = array('status' => false, 'message' => __('Unable to upload cover.', 'ap'));
     }
     die(json_encode($result));
 }
Пример #2
0
 /**
  * Process cover upload form
  */
 public function cover_upload()
 {
     if (ap_user_can_upload_cover() && ap_verify_nonce('upload_cover_' . get_current_user_id())) {
         $photo = $this->upload_photo('image');
         if ($photo === false) {
             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 cover image.
         $previous_cover = get_user_meta($userid, '_ap_cover', true);
         if ($previous_cover['file'] && file_exists($previous_cover['file'])) {
             unlink($previous_cover['file']);
         }
         // Delete previous image.
         if ($previous_cover['small_file'] && file_exists($previous_cover['small_file'])) {
             unlink($previous_cover['small_file']);
         }
         // Resize thumbnail.
         $image = wp_get_image_editor($file);
         if (!is_wp_error($image)) {
             $image->resize(960, 250, true);
             $image->save($file);
             $image->resize(350, 95, true);
             $image->save($small_name);
         }
         // Update new photo link.
         update_user_meta($userid, '_ap_cover', $photo);
         do_action('ap_after_cover_upload', $userid, $photo);
         ap_ajax_json(array('action' => 'cover_uploaded', 'status' => true, 'message' => __('Cover photo uploaded successfully.', 'anspress-question-answer'), 'user_id' => $userid, 'image' => ap_get_cover_src($userid)));
     }
     ap_ajax_json(array('message' => __('There was an error while uploading cover photo, please check your image and try again.', 'anspress-question-answer'), 'message_type' => 'error'));
 }
Пример #3
0
/**
 * Output user cover upload form
 */
function ap_cover_upload_form()
{
    if (ap_user_can_upload_cover()) {
        ?>
            <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 a cover photo', 'ap');
        ?>
">
                <span><?php 
        _e('Upload cover', 'ap');
        ?>
</span>
                <input type="file" name="image" class="ap-upload-input" data-action="ap_upload_field">
            </div>
            <input type='hidden' value='<?php 
        echo wp_create_nonce('upload_cover_' . get_current_user_id());
        ?>
' name='__nonce' />
            <input type="hidden" name="action" id="action" value="ap_cover_upload">
        </form>
        <?php 
    }
}