Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter TheTechnoMan

    (@thetechnoman)

    I would appreciate it if i could get a answer.

    Thanks

    Hello,
    I noticed a similar issue recently: The topics & replies under private forums, were publicly visible in widgets (recents topics, recents replies, etc…).

    That’s because posts (and custom post types like topics & replies) does not inherit the parents restrictions.

    Here is my fix. Seems to work but no guarantee. Plugin author or skilled people opinion could be useful on this one ??

    function force_access_restriction_inheritance( $post_id, $post ) {
    
        $post_caps = Groups_Post_Access::get_read_post_capabilities( $post_id );
    
        // Force topic, reply & attachments inheritance.
        if ( !in_array( $post->post_type, array( 'topic', 'reply', 'attachment' ) ) || 'publish' != $post->post_status || wp_is_post_revision($post_id) )
            return;
    
        $caps = Groups_Post_Access::get_read_post_capabilities( $post->post_parent );
        $diff = array_diff( $caps, Groups_Post_Access::get_read_post_capabilities( $post_id ) );
    
        if ( empty( $diff ) ) // Capabality already exists.
            return;
    
        $return = array();
    
        foreach ( $caps as $cap )
            $return[] = Groups_Post_Access::create( array( 'post_id' => $post_id, 'capability' => $cap ));
    
        // do not save post if capability add failed.
        if ( in_array( false, $return ) )
            wp_die( 'Something went wrong.' );
    }
    add_action( 'save_post', 'force_access_restriction_inheritance', 99, 2 );

    ps: please excuse my poor english.

    Thread Starter TheTechnoMan

    (@thetechnoman)

    Hey Kyle,

    thank you very much.

    Could you tell me where i have to put this code?

    I placed it in my themes functions.php but i can still access the threads without the permission.

    // EDIT: add_action( ‘save_post’, ‘force_access_restriction_inheritance’, 99, 2 );

    I need to check if a visitor has the needed role for a forums in order to reads the containing threads ??

    Thanks!

    Hello,

    You have two ways to use this code :

    1. You can create a plugin
    2. You can add it in the theme functions.php, but in that case I strongly recommend to create a child theme

    In both cases, remember that the code is hooked on the “save_post” action, meaning you’ll have to update existing posts to trigger the action:
    In admin, set a capability to a forum, then use the batch edit to update every topics once then do the same for every replies.

    Hope that help.

    Thread Starter TheTechnoMan

    (@thetechnoman)

    Ah okay thanks.

    So if a new post is created in inhertis the required capabilieties which i have set for the forums? Correct?

    Correct. That’s the purpose of the code.

    Thread Starter TheTechnoMan

    (@thetechnoman)

    Thank you very much. I will try to create the plugin. Not sure if i am able to do that lol.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘bbPress topic direct link’ is closed to new replies.