Support » Plugin: Custom Upload Dir » Gravity Form users, please test development version for me

Viewing 12 replies - 16 through 27 (of 27 total)
  • Marcel

    (@falconwhite)

    As I said, Gravity is a great plugin, but it can be a real pain in the… 😉

    You’re right. It’s only used to create new posts, not to edit existing posts. I also had problems with advanced custom fields and Gravity as it, as you said, does it’s own kind of upload thing.

    But maybe this can help: http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/filters/gform_post_data/

    The post_data is an array that contains the same info as wp_insert_post()?! The filter is executed right before the post gets saved. Maybe this contains all the necessary info?

    If this won’t do it either, let people know, that they need to use the Advanced file uploader. 🙂

    Plugin Author ulfben

    (@ulfben)

    Yeah, if post_data is the closest we get to an actual post, we’re still out of luck. We don’t know what the post_id or slug will end up being until it’s been recorded in the database. :/

    Thanks for all the help so far Marcel!

    I’ll keep subscribing to this thread, hoping someone comes up with a solution. 🙂

    Hi Ulf,
    I think there is a way, but I will need your help!

    This is the crucial bit of code in forms_model.php:

    private static function copy_post_image( $url, $post_id ) {
    		$time = current_time( 'mysql' );
    
    		if ( $post = get_post( $post_id ) ) {
    			if ( substr( $post->post_date, 0, 4 ) > 0 ) {
    				$time = $post->post_date;
    			}
    		}
    
    		//making sure there is a valid upload folder
    		if ( ! ( ( $uploads = wp_upload_dir( $time ) ) && false === $uploads['error'] ) ) {
    			return false;
    		}
    
    		$name = basename( $url );
    		GFCommon::log_debug( 'GFFormsModel::copy_post_image: Name: ' . $name );
    
    		$filename = wp_unique_filename( $uploads['path'], $name );
    
    		// Move the file to the uploads dir
    		$new_file = $uploads['path'] . "/$filename";
    
                    // this always returns the standard wpupload dir, because CUD is not hooked in
    		$uploaddir = wp_upload_dir();
                    // need to create a way to pass the newly created post_id to CUD
                    // GF needs to add a filter like this, so it can be manipulated!!!
    		$uploaddir = apply_filters( 'gform_media_upload_dir_filter', $url, $post_id );

    Now the question is how does CUD hook in once it knows the $post_id? That’s where I need your help. Is there a way to get the upload directory based on a $post_id? Could you update your plugin to do this?

    I guess all you need to do is:
    1. Ask GF to add this filter (I added this above!!!:
    $uploaddir = apply_filters( 'gform_media_upload_dir_filter', $url, $post_id ); )

    2. Add the filter to your plugin

    3. Have that function that handles that filter and returns a good CUD uploaddir from a post_id!

    Hope that helps and that it is possible like this!

    There is no other way with existing GF hooks/filters. I checked through the source etc.
    Have you got a recent copy of GF?

    Ok, stupid GF! This is the GF code now to get it working. Had to change some variablenames and the order of code:

    private static function copy_post_image( $url, $post_id ) {
    		$time = current_time( 'mysql' );
    
    		if ( $post = get_post( $post_id ) ) {
    			if ( substr( $post->post_date, 0, 4 ) > 0 ) {
    				$time = $post->post_date;
    			}
    		}
    
    		//making sure there is a valid upload folder
    		if ( ! ( ( $uploads = wp_upload_dir( $time ) ) && false === $uploads['error'] ) ) {
    			return false;
    		}
    
    		$uploaddir = wp_upload_dir();
    		// letting plugns modify the uploaddir
    		$uploaddir = apply_filters( 'gform_media_upload_dir_filter', $uploads, $post_id );
    
    		$path      = str_replace( $uploaddir['baseurl'], $uploaddir['basedir'], $url );
    
    		$name = basename( $url );
    
    		$filename = wp_unique_filename( $uploaddir['path'], $name );
    
    		// Move the file to the uploads dir
    		$new_file = $uploaddir['path'] . "/$filename";
    
    		if ( ! copy( $path, $new_file ) ) {
    			return false;
    		}
    
    		// Set correct file permissions
    		$stat  = stat( dirname( $new_file ) );
    		$perms = $stat['mode'] & 0000666;
    		@ chmod( $new_file, $perms );
    
    		// Compute the URL
    		$url = $uploaddir['url'] . "/$filename";
    
    		if ( is_multisite() ) {
    			delete_transient( 'dirsize_cache' );
    		}
    
    		$type = wp_check_filetype( $new_file );
    
    		return array( 'file' => $new_file, 'url' => $url, 'type' => $type['type'] );
    
    	}

    Then I did some hacking/adding filters and functions to get your plugin working like so. But I would rather you would do it properly 🙂
    I just had no clue how else to do it right now….

    add_filter('gform_media_upload_dir_filter', 'cud_gf_upload', 2, 10);
    ...
    function cud_gf_upload($path, $post_id ) {
    	return gf_cud_custom_upload_dir($path, $post_id);
    }
    ...
    function gf_cud_custom_upload_dir($path, $post_id){
    	if(!empty($path['error'])) { return $path; } //error; do nothing.
    	$customdir = cud_generate_path($post_id);
    	$path['path'] 	 = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
    	$path['url']	 = str_replace($path['subdir'], '', $path['url']);
    	$path['subdir']  = $customdir;
    	$path['path'] 	.= $customdir;
    	$path['url'] 	.= $customdir;
    	return $path;
    }
    ...
    function cud_generate_path($thepostid = NULL){

    I tested this only for post creation right now, so when you create a new post via GF and add a featured image to that post via GF at the same time.
    Not sure how other file uploads are handled – there also is a file field in GF. But this is for a image field called “Post Image”, which is a standard GF field.
    I also use the GFCPT plugin, so any post type is supported. That is set on the gform_post_data hook, just before the hook I am proposing to add to GF.

    Sorry, feel like I am spamming here 🙂
    Made a few mistakes still ( I had to check for the directory before the copy command) , which were not obvious at first as I also changed the gform_upload_path to the same directory as CUD. But that is not a good idea, as you then get filenames with a “1” at the end because the file already exists (one GF and one WP).
    Anyway, here is the code I used in the end. Maybe it helps somebody. Obviously it’s hacking GF, which I do not like. SO best would be to contact the GF team to get a hook or two put in.

    private static function copy_post_image( $url, $post_id ) {
    		$time = current_time( 'mysql' );
    
    		if ( $post = get_post( $post_id ) ) {
    			if ( substr( $post->post_date, 0, 4 ) > 0 ) {
    				$time = $post->post_date;
    			}
    		}
    
    		//making sure there is a valid upload folder
    		if ( ! ( ( $uploads = wp_upload_dir( $time ) ) && false === $uploads['error'] ) ) {
    			return false;
    		}
    		//GFCommon::log_debug( print_r($uploads, true) );
    		$uploaddir = wp_upload_dir();
    		//GFCommon::log_debug( print_r($uploaddir, true) );
    		// letting plugns modify the uploaddir
    		$uploaddir = apply_filters( 'gform_media_upload_dir_filter', $uploaddir, $post_id );
    
    		$path      = str_replace( $uploaddir['baseurl'], $uploaddir['basedir'], $url );
    
    		$name = basename( $url );
    		//GFCommon::log_debug( 'GFFormsModel::copy_post_image: Name: ' . $name );
    
    		// make filename unique if there is a file with that name already
    		$filename = wp_unique_filename( $uploaddir['path'], $name );
    
    		// Move the file to the uploads dir
    		$new_file = $uploaddir['path'] . "/$filename";
    
    		// Create the directory if necessary
    		if ( ! is_dir( $uploaddir['path'] ) ) {
    			if ( ! wp_mkdir_p( $uploaddir['path'] ) ) {
    				GFCommon::log_debug( 'GFFormsModel::copy_post_image: Directory could not be created!!!');
    				return false;
    			}
    		}
    
    		if ( ! copy( $path, $new_file ) ) {
    			GFCommon::log_debug( 'GFFormsModel::copy_post_image: File could not be copied!!!');
    			return false;
    		}
    
    		// Set correct file permissions
    		$stat  = stat( dirname( $new_file ) );
    		$perms = $stat['mode'] & 0000666;
    		@ chmod( $new_file, $perms );
    
    		// Compute the URL
    		$url = $uploaddir['url'] . "/$filename";
    
    		if ( is_multisite() ) {
    			delete_transient( 'dirsize_cache' );
    		}
    
    		$type = wp_check_filetype( $new_file );
    		//GFCommon::log_debug( 'GFFormsModel::copy_post_image: $new_file: ' . $new_file );
    		//GFCommon::log_debug( 'GFFormsModel::copy_post_image: $url: ' . $url );
    		//GFCommon::log_debug( 'GFFormsModel::copy_post_image: $type[type]: ' . $type['type'] );
    		return array( 'file' => $new_file, 'url' => $url, 'type' => $type['type'] );
    
    	}

    Ulfben, GF now hosts the plugin on github (link) to try/use now. Anyway, looks like the three of you have done some good work with this. I’m uploading the development version now to test. Will post back if I’m able to offer anything useful..

    HI Guys
    Great plugin,
    Anyway to get gravity forms to send uploaded files to Author OR Username?
    Currently this plugin works with year then month, however I need it to go threw author.

    As follows would be excellent!

    /%author%/%year%/%monthnum%/filename.jpg

    Thanks

    Well you can do it together with the CUD plugin with the changes above / basically you have to hack the core GF files and the CUD plugin. But it depends a bit what you want to do.

    There is also a filter to change the GF upload: https://www.gravityhelp.com/documentation/article/gform_upload_path/

    Just try them both out and see which one works for you.
    S

    Thanks Ianwire.
    Ahh I had this working.. however using /%author%/ doesn’t work with
    Gravity Forms Advanced File Uploader & Custom Upload Dir

    I now have it working with the /%current_user%/ field.
    Wonder why author won’t work?

    Thanks for you help

    Plugin Author ulfben

    (@ulfben)

    Wonder why author won’t work?

    Because author is a property of the post, a context that Custom Upload Dir does not have access too when using Gravity Forms (see higher up in the thread).

    Current user, date, time etc are global contexts and always available.

    Ok, I am only using a “post image” GF field and as a post is created in the process I gain access to the newly created post ID (which I then pass to CUD as above). That is my use case and the only time I use GF to upload an image to the post.

    For all the rest of adding images to a post, I created a front end image uploader with the native wordpress uploader (there are many tutorials) and CUD works absolutely fine with that.

Viewing 12 replies - 16 through 27 (of 27 total)
  • The topic ‘Gravity Form users, please test development version for me’ is closed to new replies.