• Hi David-

    I’m having a problem while trying to do something somewhat out of the ordinary. I’m attempting to use the wp_editor and WordPress media uploader on a group admin page (on the front end). The problem is that when WP makes the AJAX request to admin-ajax.php to get the images to display in the media library, group hierarchy is jumping in and dropping a 404. From my debug log, it looks like I’m causing the cascade by doing a bp_is_current_action check which is being caught by group_hierarchy_override_current_action which is failing at check_slug (it appears that the function can’t work out the groups table name). My debug log looks like:

    [03-Dec-2013 22:50:40 UTC] BP Group Hierarchy - Routing request
    [03-Dec-2013 22:50:40 UTC] BP Group Hierarchy - Current component: groups
    [03-Dec-2013 22:50:40 UTC] BP Group Hierarchy - Current action: category-test
    [03-Dec-2013 22:50:40 UTC] BP Group Hierarchy - Groups slug:
    [03-Dec-2013 22:50:40 UTC] BP Group Hierarchy - Are we on a user profile page?: N
    [03-Dec-2013 22:50:40 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE slug = 'category-test' AND parent_id = 0' at line 1 for query SELECT id FROM  WHERE slug = 'category-test' AND parent_id = 0 made by require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('init'), call_user_func_array, bp_init, do_action('bp_init'), call_user_func_array, bp_setup_globals, do_action('bp_setup_globals'), call_user_func_array, BP_Core->setup_globals, bp_user_has_access, bp_current_user_can, current_user_can_for_blog, call_user_func_array, WP_User->has_cap, call_user_func_array, map_meta_cap, apply_filters('map_meta_cap'), call_user_func_array, cc_group_home_setup_map_meta_cap, bp_is_current_action, bp_current_action, apply_filters('bp_current_action'), call_user_func_array, group_hierarchy_override_current_action, BP_Groups_Hierarchy->__construct, BP_Groups_Hierarchy::group_exists, BP_Groups_Hierarchy::check_slug, QueryMonitorDB->query
    [03-Dec-2013 22:50:40 UTC] BP Group Hierarchy - Group not found - returning 404.

    If I add

    if( strpos( admin_url('admin-ajax.php'), $_SERVER['REQUEST_URI'] ) ) {
    		return $current_action;
    	}

    to group_hierarchy_override_current_action it works, but that could cause heartache other places, I suspect.

    Thanks for any troubleshooting help you can give,

    -David

    https://www.ads-software.com/plugins/bp-group-hierarchy/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author David Dean

    (@ddean)

    Hi dcavins –

    IIRC, group pagination over AJAX uses ‘admin-ajax.php’, so you’ll probably see issues there. The current check in group_hierarchy_override_current_action specifically excludes it for that reason.

    Is there an opportunity to remove the group_hierarchy_override_current_action function from the bp_current_action filter before it’s been called? Maybe on the action bp_group_hierarchy_components_loaded?

    The exception for admin-ajax.php could probably be tightened to only catch requests for group lists that group hierarchy needs to intercept.

    I’m also surprised the check for whether the current request is in the groups component is not catching this and returning the action as is. Maybe that could use some fine-tuning.

    Please let me know if you have any other questions!

    – David

    Thread Starter David Cavins

    (@dcavins)

    Hi David-

    Thanks for your quick reply. I did some further testing, and I discovered that using a bp_is_current_action( ) check inside of a filter on map_meta_cap will upset BP Group Hierarchy every time. Example:

    add_filter( 'map_meta_cap', 'testing_bp_hierarchy', 8, 4 );
    function testing_bp_hierarchy( $primitive_caps, $meta_cap, $user_id, $args ) {	
    
    	// Only do this when on the group admin's group home edit page
    	if( ( bp_is_current_component( 'groups' ) && bp_is_current_action( 'admin' ) &&  bp_is_action_variable( 'group-home', 0 ) )
    		) {
    
    		if ( !in_array( $meta_cap, array( 'upload_files','edit_post' ) ) ) {
    	        return $primitive_caps;
    	    }
    
    	    $primitive_caps = array();
    	}
    
    	return $primitive_caps;
    }

    Which is where I’m seeing

    [04-Dec-2013 16:55:38 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE slug = 'category-test' AND parent_id = 0' at line 1 for query SELECT id FROM  WHERE slug = 'category-test' AND parent_id = 0 made by require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('init'), call_user_func_array, bp_init, do_action('bp_init'), call_user_func_array, bp_setup_globals, do_action('bp_setup_globals'), call_user_func_array, BP_Core->setup_globals, bp_user_has_access, bp_current_user_can, current_user_can_for_blog, call_user_func_array, WP_User->has_cap, call_user_func_array, map_meta_cap, apply_filters('map_meta_cap'), call_user_func_array, testing_bp_hierarchy, bp_is_current_action, bp_current_action, apply_filters('bp_current_action'), call_user_func_array, group_hierarchy_override_current_action_dc, BP_Groups_Hierarchy->__construct, BP_Groups_Hierarchy::group_exists, BP_Groups_Hierarchy::check_slug, QueryMonitorDB->query
    [04-Dec-2013 16:55:38 UTC] BP Group Hierarchy - Group not found - returning 404.

    This problem affects the “load more” call on the group activity page, too.

    However, if I remove the filter sort of like you suggest:

    add_filter( 'map_meta_cap', 'testing_bp_hierarchy', 8, 4 );
    function testing_bp_hierarchy( $primitive_caps, $meta_cap, $user_id, $args ) {	
    
    	remove_filter('bp_current_action', 'group_hierarchy_override_current_action_dc');
    
    	// Only do this when on the group admin's group home edit page
    	if( ( bp_is_current_component( 'groups' ) && bp_is_current_action( 'admin' ) &&  bp_is_action_variable( 'group-home', 0 ) )
    		) {
    
    		if ( !in_array( $meta_cap, array( 'upload_files','edit_post' ) ) ) {
    	        return $primitive_caps;
    	    }
    
    	    $primitive_caps = array();
    	}
    
    	return $primitive_caps;
    }

    Both AJAX requests work as expected. Am I using bp_is_current_action in a bizarre way, or can we tighten up the action rewriting to handle this edge case?

    Thanks again for taking the time to reply,

    -David

    Thread Starter David Cavins

    (@dcavins)

    By the way, this request returns true to both is_admin and bp_is_groups_component().

    Thanks again for your help.

    Plugin Author David Dean

    (@ddean)

    Hi dcavins –

    I think the issue with map_meta_cap may have a simple resolution. The group_hierarchy_override_current_action function is supposed to unhook itself after it runs during the initial request. But it looks like I’ve called remove_action instead of remove_filter so that probably isn’t happening.

    bp_is_current_action should be idempotent and safe to call once the group is selected on page load, and this is probably why it’s not working right. I’ll get a fix in the development version right away. This is distinct from the admin-ajax.php issue, right?

    Thanks for your help troubleshooting this!

    – David

    Plugin Author David Dean

    (@ddean)

    Nevermind they are the same function. I hoped you had found something that would resolve all of this. ??

    I’ll keep looking. Can you provide some more context for your map_meta_cap call? Is it on your AJAX request or on a regular page?

    – David

    Plugin Author David Dean

    (@ddean)

    Hi dcavins –

    At what point in WP’s loading process are you hooking map_meta_cap? I put your test function in the theme’s functions.php file and it loaded WAY before BuddyPress was ready. Engaging that filter only after bp_init has fired may resolve that issue.

    – David

    Thread Starter David Cavins

    (@dcavins)

    Hi David-

    Thanks for continuing to look into this. I’m using the map_meta_cap filter to allow users to upload files in one certain case (and since they’re subscriber-level users, they don’t have that ability, usually). I’m allowing BuddyPress group admins to upload images to a post that serves as the group’s home page.

    Your most recent post is interesting to me, because I didn’t think about adding the filter conditionally. I added the map_meta_cap filter (and I can say from my logs that map_meta_cap gets called all the time) and then used the bit

    if( ( bp_is_current_component( 'groups' ) && bp_is_current_action( 'admin' ) && bp_is_action_variable( 'group-home', 0 ) ) )

    to only apply my rule in one instance. I learned a lot about using the filter from Andy Nacin’s presentation https://wordpress.tv/2013/08/10/andrew-nacin-current-user-can-watch-this-talk/

    Are you thinking trying something like:

    add_action('bp_init','add_mmc_filter');
    function add_mmc_filter() {
          if( ( bp_is_current_component( 'groups' ) && bp_is_current_action( 'admin' ) &&          bp_is_action_variable( 'group-home', 0 ) ) ) {
                add_filter( 'map_meta_cap', 'testing_bp_hierarchy', 8, 4 );
          }
    }

    Thanks again for your help,

    -David

    Thread Starter David Cavins

    (@dcavins)

    Hi David-

    Your suggestion appears to work properly:

    add_action('bp_init','add_mmc_filter');
    function add_mmc_filter() {
          if( ( bp_is_current_component( 'groups' ) && bp_is_current_action( 'admin' ) && bp_is_action_variable( 'group-home', 0 ) ) || ( isset( $_POST['action'] ) && $_POST['action'] == 'upload-attachment' )
          	) {
                add_filter( 'map_meta_cap', 'cc_group_home_setup_map_meta_cap', 14, 4 );
          }
    }
    
    function cc_group_home_setup_map_meta_cap( $primitive_caps, $meta_cap, $user_id, $args ) {	
    
    	if ( !in_array( $meta_cap, array( 'upload_files','edit_post' ) ) ) {
            return $primitive_caps;
        }
    
        $primitive_caps = array();
    
    	return $primitive_caps;
    }

    Is that what you were thinking? I guess I’m surprised that it works, given that the filter needs to be applied to the AJAX requests for the media uploader as well, and I didn’t realize that bp_init would occur in that load.

    Thanks again,

    -David

    Plugin Author David Dean

    (@ddean)

    Hi David-

    Yep that’s what I had in mind. ?? Glad we could resolve this issue, and to hear the same hook works for both cases.

    Please let me know if you run into anything else!

    – David

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Trouble with 'group_hierarchy_override_current_action'’ is closed to new replies.