public function upload($target = null, $append_sub_dirs = false)
    {
        if (!\function_exists('wp_handle_upload')) {
            require_once(ABSPATH . 'wp-admin/includes/file.php');
        }

        if ($target !== null) {
            $closure = function ($upload_dir) use ($target, $append_sub_dirs) {
                $upload_dir['subdir'] = '/' . \untrailingslashit($target);

                if ($append_sub_dirs !== false) {
                    $upload_dir['subdir'] = '/' . \untrailingslashit($target) . $upload_dir['subdir'];
                }

                $upload_dir['path'] = $upload_dir['basedir'] . $upload_dir['subdir'];
                $upload_dir['url'] = $upload_dir['baseurl'] . $upload_dir['subdir'];

                return $upload_dir;
            };

            \add_filter('upload_dir', $closure);
        }

        $data = $this->get_original_data();

        $result = \wp_handle_upload($data, ['test_form' => false]);

        if ($target !== null) {
            \remove_filter('upload_dir', $closure);
        }

        if (isset($result['file'])) {
            $this->upload_path = $result['file'];
            $this->upload_url = $result['url'];
            $this->upload_type = $result['type'];

            return true;
        }

        if (isset($result['error'])) {
            $this->upload_error = $result['error'];
        }

        return false;
    }
public function upload($target = null, $append_sub_dirs = false)
    {
        if (!\function_exists('wp_handle_upload')) {
            require_once(ABSPATH . 'wp-admin/includes/file.php');
        }

        if ($target !== null) {
            $closure = function ($upload_dir) use ($target, $append_sub_dirs) {
                $upload_dir['subdir'] = '/' . \untrailingslashit($target);

                if ($append_sub_dirs !== false) {
                    $upload_dir['subdir'] = '/' . \untrailingslashit($target) . $upload_dir['subdir'];
                }

                $upload_dir['path'] = $upload_dir['basedir'] . $upload_dir['subdir'];
                $upload_dir['url'] = $upload_dir['baseurl'] . $upload_dir['subdir'];

                return $upload_dir;
            };

            \add_filter('upload_dir', $closure);
        }

        $data = $this->get_original_data();

        $result = \wp_handle_upload($data, ['test_form' => false]);

        if ($target !== null) {
            \remove_filter('upload_dir', $closure);
        }

        if (isset($result['file'])) {
            $this->upload_path = $result['file'];
            $this->upload_url = $result['url'];
            $this->upload_type = $result['type'];

            return true;
        }

        if (isset($result['error'])) {
            $this->upload_error = $result['error'];
        }

        return false;
    }
public function upload ( $ target = null , $ append_sub_dirs = false ) { if ( ! \ function_exists ( 'wp_handle_upload' ) ) { require_once ( ABSPATH . 'wp-admin/includes/file.php' ) ; } if ( $ target !== null ) { $ closure = function ( $ upload_dir ) use ( $ target , $ append_sub_dirs ) { $ upload_dir [ 'subdir' ] = '/' . \ untrailingslashit ( $ target ) ; if ( $ append_sub_dirs !== false ) { $ upload_dir [ 'subdir' ] = '/' . \ untrailingslashit ( $ target ) . $ upload_dir [ 'subdir' ] ; } $ upload_dir [ 'path' ] = $ upload_dir [ 'basedir' ] . $ upload_dir [ 'subdir' ] ; $ upload_dir [ 'url' ] = $ upload_dir [ 'baseurl' ] . $ upload_dir [ 'subdir' ] ; return $ upload_dir ; } ; \ add_filter ( 'upload_dir' , $ closure ) ; } $ data = $ this -> get_original_data ( ) ; $ result = \ wp_handle_upload ( $ data , [ 'test_form' => false ] ) ; if ( $ target !== null ) { \ remove_filter ( 'upload_dir' , $ closure ) ; } if ( isset ( $ result [ 'file' ] ) ) { $ this -> upload_path = $ result [ 'file' ] ; $ this -> upload_url = $ result [ 'url' ] ; $ this -> upload_type = $ result [ 'type' ] ; return true ; } if ( isset ( $ result [ 'error' ] ) ) { $ this -> upload_error = $ result [ 'error' ] ; } return false ; }
Move the file to the uploads folder.

Defaults to standard WP behavior, such as uploading the file to the year/month
subfolder.

@param  string  $target          Optional. Subfolder within the uploads directory to upload to.
Will be created if it doesn't exist.
@param  boolean $append_sub_dirs Optional. Whether to include the year/month folders when using
a custom target directory.
Defaults to false.
@return bool                     Whether the upload was successful.
Move the file to the uploads folder .