• Resolved Dekadinious

    (@dekadinious)


    I am allowing uploads for contributors because I want them to be able to add images to their posts.

    I have been able to limit the shown media objects to those that the current logged in user has uploaded.

    I would also like to limit the shown media objects to only those that have been uploaded on the current post that is being edited.

    That way the contributors can’t delete media objects of posts that have been published since they are not allowed to edit those posts.

    I want them to be able to delete media objects before the posts are published, while they are editing them and preparing them for submission.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hey Dekadinious,

    What method have you used to limit the media objects shown to be only those of the uploader? Just asking here as it’d probably be nice to have a solution for the next part implemented in a consistent way.

    One idea that pops to mind is that, depending on your php skills, you could update the author of the attachment posts to an administrator when a post is published, that way they’d likely automatically lose all privileges related to the posts at that point.

    Something like:

    add_action( 'save_post' , function( $post_id ) {
    
        if( wp_is_post_revision( $post_id )) {
            return;
        }
    
        if( get_post_status( $post_id ) !== 'publish' ) {
            return;
        }
    
        $attachments = get_attached_media( 'image' , $post_id );
    
        foreach( $attachments as $attachment ) {
            wp_update_post( array(
                'ID' => $attachment->ID,
                'post_author' => <some-admin-id>,
            ));
        }
    
    }, 10 , 1 );

    Haven’t tested this btw, just an idea.

    • This reply was modified 4 years, 4 months ago by dominic_ks.
    Thread Starter Dekadinious

    (@dekadinious)

    Thank you for that code!

    That exact idea has struck my mind. But I want to make it future-proof so that I later can filter on media from certain contributors etc. So I think I would like the author of the media to stay intact.

    This is my work in progress. The commented line will show the media for only the author. I am trying to incorporate post-ID into this to query only media associated with the post.

    I have no idea if this is correct. Currently, I am struggling with global $post not being defined. Therefore I can’t get the post ID.

    
    function wpb_show_current_user_attachments( $query ) {
    	$referer = parse_url(wp_get_referer());
    	parse_str($referer['query'], $params);
    
    	if (isset($params['post'])){
    		$post_type = get_post_type($params['post']);
    	} else if (strpos($referer['path'], 'post-new.php') !== false && !isset($params['post_type'])){
    		$post_type = 'post';
    	} else {
    		$post_type = '';
    	}
    
    	if ( $post_type == 'post' ) {  
    		global $post;
    		$id = $post->ID;
    		write_log($id);
    	}
    	
        $user_id = get_current_user_id();
        if ( $user_id && check_user_role(array('contributor')) ) {
            //$query['author'] = $user_id;
    	$query['post_parent'] = $id;
        }
        return $query;
    }
    add_filter( 'ajax_query_attachments_args', 'wpb_show_current_user_attachments' );
    
    Thread Starter Dekadinious

    (@dekadinious)

    I actually had an aha-moment. I did not need the global $post at all. Just take it from the $_POST:

    $query['post_parent'] = $_POST['post_id'];

    Now it works, and I don’t need to filter on the author at all ??

    Yeah that’s probably better, preserves the data as you say. Was just having a fish around for pure interest, was wondering if there was some filter in the delete_post type functions where you could dynamically prevent someone deleting a post that was in use on a post, but, I don’t think there’s a filter that you can use to prevent something being deleted.

    Only other thought that came up was that you could use the user_has_cap filter and deny access to a user to delete an attachment based on some conditions.

    My thinking on all of that was that a user could still see and see and reuse their previous media items, just not delete them if they were in use. Would also potentially prevent users needing to upload duplicate images.

    The other issue here could be that user uploads, and attaches image to post X, but then only uses it on post Y, so you would just be able to rely on the status of the post it was attached to, and would probably need some custom SQL function to check if the image was actually present in the body of any posts…

    Thread Starter Dekadinious

    (@dekadinious)

    Yeah, it’s a complicated issue for sure!

    I think I’ll just take my chances with duplicate image uploads for now. A couple of megabytes of data here and there is not too expensive these days ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Limit queried media library objects to that of current post’ is closed to new replies.