Support » Plugin: Members - Membership & User Role Editor Plugin » Support for Visual Composer (or any “the loop” processor)

  • Hello.
    I definitely enjoy your plugin, however it doesn’t work with things like Visual Composer, or Cornerstone. Since the page/post actually has just a shortcode in it, and the_content is generated dynamically. Such that any page listing-out information from an article which is blocked, results in a access denied message.

    So I ventured into your plugin and wrote some code to make it work properly for these. Hope it finds you well, and in any case I hope it helps others out there in the same boat!
    I didn’t document it, but basically it filters the WPQuery post resultants (shows the user only what they are allowed to see), and the taxonomy as well. That way categories are only shown if a post within it is actually accessible.
    There are some issues that need worked on to further this I’m sure, But it’s a heck of a good start!

    functions-content-permissions.php line #104

    function members_enable_content_permissions() {
        ....
        
        add_filter('get_terms','members_content_permissions_custom_get_terms',10,3);
        add_filter('the_posts','members_content_permissions_custom_the_posts',10,3);
    
        ....
    }
    function members_content_permissions_custom_the_posts($posts,$unk1=null,$unk2=null) {
    	if ( is_admin() ) return $posts;
    	if ( ! is_main_query() ) return $posts;
    	$resultant=array();
    	$uid=get_current_user_id();
    	foreach($posts as $post) {
    		if (members_can_user_view_post($uid,$post->ID)) {
    			$resultant[]=$post;
    		}
    	}
    	return $resultant;
    }
    function members_content_permissions_custom_get_terms($terms, $taxonomies=null, $args=null) {
    	if ( is_admin() ) return $terms;
    	global $wpdb;
    	if ( !is_array($terms) && count($terms) < 1 ) return $terms;
    	$taxonomy = $taxonomies[0];
    	$resultant=array();
    	
    	$uid=get_current_user_id();
    	foreach($terms as $term) {
    		$allow=false;
    		if ($term->taxonomy == "") {
    			$allow=true;
    		} else {
    			$post_ids_with_tax = $wpdb->get_var("SELECT GROUP_CONCAT(p.ID SEPARATOR ',') FROM ".$wpdb->posts." p JOIN ".$wpdb->term_relationships." rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = ".$term->term_id." AND p.post_status = 'publish' GROUP BY p.id"); // 'tis better than looping queries. I would prefer outer joins and case conditionals to make this 10x faster, but your function to verify access already has all the logic in place.. Thus the following...
    			if ($post_ids_with_tax!=null) {
    				$pids=explode(',',$post_ids_with_tax);
    				foreach($pids as $pid) {
    					if (members_can_user_view_post($uid,$pid)) {
    						$allow=true;
    						break; // no need to check additional posts, the user can see at least one post in the category.
    					}
    				}
    			}
    		}
    		if ($allow) $resultant[]=$term;
    	}
    	return $resultant;
    }
    • This topic was modified 4 years, 11 months ago by ensemblebd.
    • This topic was modified 4 years, 11 months ago by ensemblebd.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter ensemblebd

    (@ensemblebd)

    Also, I was able to make this work for ajax searches too. So outside of a pagination issue, I would consider this a solution. Definitely would appreciate your thoughts on this.

    new code follows..

    add_action('admin_init','members_enable_content_permissions_ajax',0);

    function members_enable_content_permissions_ajax() {
    	if ( !wp_doing_ajax() ) return; // make sure we aren't running the same code twice.
    	// "Only add filters if the content permissions feature is enabled and we're not in the admin."
    	// here we only add the filters to support ajax requests that process content, which we must ensure they have access to attain.
    	
    	if ( members_content_permissions_enabled() ) {
    		add_filter('get_terms','members_content_permissions_custom_get_terms',10,3);
    		add_filter('the_posts','members_content_permissions_custom_the_posts',10,3);
    	}
    }
    function members_content_permissions_custom_the_posts($posts,$self,$unk2=null) {
    	if ( is_admin() && !wp_doing_ajax() ) return $posts;
    	if ( !is_main_query() && !$self->is_search && !wp_doing_ajax() ) return $posts;
    	.......
    
    function members_content_permissions_custom_get_terms($terms, $taxonomies=null, $args=null) {
    	if ( is_admin() && !wp_doing_ajax() ) return $terms;
    	.......
    
    Thread Starter ensemblebd

    (@ensemblebd)

    Also have to manually take care of any filter suppression items.

    vc_basic_grid_filter_query_suppress_filters for example, disregards our filters.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Support for Visual Composer (or any “the loop” processor)’ is closed to new replies.