php – Jetengine Forms – Media upload path

Question

Need a piece of code in functions.php that redefines the path Jetengine Form saves media files to…

Ideally into the following path AND with another meta field entry in the path.

/wordpress folder/crewmembers/username/’meta field’/filename

Tried these

How Can I Organize the Uploads Folder by Slug (or ID, or FileType, or Author)?
How to set custom upload directory per user after moving content directory

I can get WordPress to save the files to the new path but I can’t seem to change the part from Jetengine code.

Background:

I am building a members site using Elementor and Jetengine.
Using Jetengine Forms, users can enter their info and upload files to their user profile.

Per design (hardcoded – see below) Jetengine Forms stores uploaded media files into the following folder:

/wp-content/uploads/’User ID”Filename’

(I’ve unticked the ‘Organize my uploads into month- and year-based folders.’ in WP > Settings > Media)

Having asked Crocoblock Support if there is any way to change this. The answer unfortunately is no 🙁

The following code is from the Jetengine plugin.
/wp-content/plugins/jet-engine/includes/modules/forms/file-upload.php

    /**
     * Process files upload
     *
     * @param  boolean $files [description]
     * @return [type]         [description]
     */
    public function process_upload( $files = false, $settings = array() ) {

        $settings = wp_parse_args( $settings, array(
            'max_size'          => wp_max_upload_size(),
            'max_files'         => 1,
            'insert_attachment' => false,
        ) );

        $insert_attachment = filter_var( $settings['insert_attachment'], FILTER_VALIDATE_BOOLEAN );

        if ( ! $files ) {
            $files = $_FILES;
        }

        if ( empty( $files ) || ! is_array( $files ) ) {
            return false;
        }

        if ( count( $files ) > $settings['max_files'] ) {
            wp_send_json_error( $settings['messages']['upload_max_files'] );
        }

        $result = array();
        $index  = 0;

        foreach ( $files as $file ) {

            if ( ! $file['size'] > $settings['max_size'] ) {
                wp_send_json_error( $settings['messages']['upload_max_size'] );
            }

            if ( ! empty( $settings['mime_types'] ) && ! in_array( $file['type'], $settings['mime_types'] ) ) {
                wp_send_json_error( $settings['messages']['upload_mime_types'] );
            }

            $result[] = $this->upload_file( $file, $insert_attachment );

        }

        return $result;

    }

    /**
     * Upload file
     *
     * @return [type] [description]
     */
    public function upload_file( $file = array(), $insert_attachment = false ) {

        $result = array();

        if ( ! function_exists( 'wp_handle_upload' ) ) {
            include_once ABSPATH . 'wp-admin/includes/file.php';
            include_once ABSPATH . 'wp-admin/includes/media.php';
        }

        add_filter( 'upload_dir', array( $this, 'apply_upload_dir' ) );

        $upload = wp_handle_upload(
            $file,
            array( 'test_form' => false )
        );

        if ( empty( $upload['error'] ) && $insert_attachment ) {

            $filepath   = $upload['file'];
            $attachment = wp_insert_attachment(
                array(
                    'guid'           => $upload['url'],
                    'post_mime_type' => $upload['type'],
                    'post_title'     => preg_replace( '/.[^.]+$/', '', basename( $filepath ) ),
                    'post_content'   => '',
                    'post_status'    => 'publish'
                ),
                $filepath,
                0,
                true
            );

            if ( ! is_wp_error( $attachment ) ) {
                $metadata = wp_generate_attachment_metadata( $attachment, $filepath );
                wp_update_attachment_metadata( $attachment, $metadata );
            } else {
                $this->errors[] = $attachment->get_error_message();
            }

            $upload['attachment'] = $attachment;

        } elseif ( ! empty( $upload['error'] ) ) {
            $this->errors[] = $upload['error'];
        }

        remove_filter( 'upload_dir', array( $this, 'apply_upload_dir' ) );

        return $upload;

    }

    /**
     * Returns upload subdirectory
     *
     * @return [type] [description]
     */
    public function get_upload_dir() {

        $user_id       = get_current_user_id();
        $user_dir_name = $user_id ? $user_id : 'guest';
        $user_dir_name = apply_filters( 'jet-engine/forms/file-upload/user-dir-name', $user_dir_name );

        return $this->upload_base() . "https://wordpress.stackexchange.com/" . $user_dir_name;
    }

    /**
     * Returns upload base directory
     *
     * @return [type] [description]
     */
    public function upload_base() {
        return apply_filters( 'jet-engine/forms/file-upload/dir', 'jet-engine-forms' );
    }

    /**
     * Change upload directory for JetEngine uploads
     *
     * @param  [type] $pathdata [description]
     * @return [type]           [description]
     */
    public function apply_upload_dir( $pathdata ) {

        $dir = $this->get_upload_dir();

        if ( empty( $pathdata['subdir'] ) ) {
            $pathdata['path']   = $pathdata['path'] . "https://wordpress.stackexchange.com/" . $dir;
            $pathdata['url']    = $pathdata['url'] . "https://wordpress.stackexchange.com/" . $dir;
            $pathdata['subdir'] = "https://wordpress.stackexchange.com/" . $dir;
        } else {
            $new_subdir="https://wordpress.stackexchange.com/" . $dir . $pathdata['subdir'];
            $pathdata['path']   = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
            $pathdata['url']    = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
            $pathdata['subdir'] = $new_subdir;
        }

        return $pathdata;

    }

0
wpdemic 4 months 2022-01-23T06:49:12-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse