Crocoblock / suggestions

The suggestions for CrocoBlock project
195 stars 78 forks source link

🔥[jetengine] limit the media library choice #3422

Open nreljed opened 3 years ago

nreljed commented 3 years ago

I suggest that we add this "very popular" option from ACF.

Plan de travail – 1

this illustration will show you what problem it will solve. especially if the uploads to the posts are unique like real estate.

imagine you are a real estate agent and you want just upload images of a house. in backend if you click on "Choose Media" ths is will show you all your uploads and all other users uploads it's anarchy!!

Library option limit what you can see each agent must only and only

directly upload. [new post] see the images attached to the post. [edit post] insert images or delete some if necessary. [edit post]

Plan de travail – 4

Plan de travail – 2

Plan de travail – 3

one last request .

I suggest that we finally integrate the attachment plugin "Attach Media to Post" into the core of jetengine he is here to solve the upload attachment problem seen for the first time here

so each agent / user will have their uploads attached correctly by default.

And above all it is still well hidden and is quickly forgotten. I'm even sure that 90% of people are not yet aware that their uploads are not attached. its place is really in the jetengine core not in plugin

thank you for your attention

lifeact commented 3 years ago

follow

nreljed commented 2 years ago

@kaskad88 This will help you a lot and speed up the implementation of this primordial missing option this code seems to reproduce the limiting function we want

add_filter( 'ajax_query_attachments_args', 'filter_query_attachments_args' );

function filter_query_attachments_args( $query ) {
    // 1. Only users with access
    if ( ! current_user_can( 'upload_files' ) ) {
        wp_send_json_error();
    }

    // 2. No manipulation for admins.
    // After all they have access to all images.
    if ( current_user_can( 'administrator' ) ) {
        return $query;
    }

    // 3. No images, if the post_id is not provided
    if ( ! isset( $_REQUEST['post_id'] ) ) {
        wp_send_json_error();
    }

    // 4. No images, if you are not the post type manager or author
    $post = get_post( (int) $_REQUEST['post_id'] );
    if ( ! $post instanceof \WP_Post ) {
        return $query;
    }

    // 5. You can also restrict the changes to your custom post type
    if ( 'listing' != $post->post_type ) {
        // Only filter for our custom post types
        return $query;
    }

    // 6. Allow only post authors to open the uploader
    $current_user = wp_get_current_user();
    if ( $current_user->ID != $post->post_author ) {
        wp_send_json_error();
    }

    // 7. Filter to display only images
    $query['post_mime_type'] = array(
        'image/gif',
        'image/jpeg',
        'image/png',
        'image/bmp',
        'image/tiff',
        'image/x-icon'
    );

    // 8. Don't show private images
    $query['post_status'] = 'inherit';

    // 9. Filter to display only the images attached to the post
    $query['post_parent'] = $post->ID;

    // 10. Filter to display only the user uploaded image
    $query['author'] = $current_user->ID;

    return $query;
}

✔️tested and working

there may be too many or unnecessary codes but you get the idea

Each part is documented with its number and details.

the only problem ... it takes more than 5 to 7 seconds to display the images probably because of the nature of the ajax_query_attachments_args function ? or some adjustments that you can probably do, to make it compatible 100% with jetengine

gallery

additional data

these people seem to be talking about the same thing with a different approach. I did not understand how to test it. but i see ajax_query_attachments_args again.. so i imagine i will have the same loading time problem...

maybe it will help you better understand

I also tried to find the code of this function in ACF files but not succeeded .. if anyone understands how to find it.

Thank you for sharing it

we must help the dev to understand how to reproduce this function

lucasvlc commented 2 years ago

This would be great... how is it going with this request?