Each search triggers 10 calls? Why?

BP Profile Search Each search triggers 10 calls? Why?

  • Author
    Post
  • #12081 Reply
    Par
    Guest

    I’m reviewing the code in order to make some customisations. But there is a behaviour that bothers me. And that is that each time I make a search it will trigger 10 calls to the search function. Why is that?
    I have added a few error_log lines in the code.
    In bps-search.php

    function bps_filter_members ($querystring, $object)
    {
    if ($object != ‘members’) return $querystring;
    error_log(‘BPSFILTERMEMBERS:’.$querystring);

    function bps_get_request ($type, $form=0)
    {

    error_log(‘BPS_REQUEST-‘.$type.’-‘.$form.’:’);
    return apply_filters (‘bps_request’, $request, $type, $form);
    }

    function bps_search ($request, $users=null)
    {
    $results = array (‘users’ => array (0), ‘validated’ => true);
    error_log(‘BPSSEARCH’);

    The debug.log looks like this:
    [27-Sep-2023 11:18:55 UTC] BPSFILTERMEMBERS:page=1&scope=all&per_page=1&user_id=0
    [27-Sep-2023 11:18:55 UTC] BPS_REQUEST-search-0:
    [27-Sep-2023 11:18:55 UTC] BPSSEARCH
    [27-Sep-2023 11:18:58 UTC] BPSFILTERMEMBERS:type=active&action=active&page=1
    [27-Sep-2023 11:18:58 UTC] BPS_REQUEST-search-0:
    [27-Sep-2023 11:18:58 UTC] BPSSEARCH
    [27-Sep-2023 11:18:58 UTC] BPSFILTERMEMBERS:type=active&action=active&page=1&scope=all&per_page=1&user_id=0
    [27-Sep-2023 11:18:58 UTC] BPS_REQUEST-search-0:
    [27-Sep-2023 11:18:58 UTC] BPSSEARCH
    [27-Sep-2023 11:18:58 UTC] BPSFILTERMEMBERS:type=active&action=active&page=1&scope=personal&per_page=1&user_id=5
    [27-Sep-2023 11:18:58 UTC] BPS_REQUEST-search-0:
    [27-Sep-2023 11:18:58 UTC] BPSSEARCH
    [27-Sep-2023 11:18:58 UTC] BPSFILTERMEMBERS:type=active&action=active&page=1&include=3%2C6%2C106%2C228%2C953%2C3480%2C4234%2C4247%2C4540%2C5551%2C6455%2C7005%2C8977%2C9632%2C11347%2C23479%2C25848&scope=following&per_page=1
    [27-Sep-2023 11:18:58 UTC] BPS_REQUEST-search-0:
    [27-Sep-2023 11:18:58 UTC] BPSSEARCH
    [27-Sep-2023 11:18:59 UTC] BPSFILTERMEMBERS:type=active&action=active&page=1
    [27-Sep-2023 11:18:59 UTC] BPS_REQUEST-search-0:
    [27-Sep-2023 11:18:59 UTC] BPSSEARCH
    [27-Sep-2023 11:18:59 UTC] BPSFILTERMEMBERS:type=active&action=active&page=1&scope=all&per_page=1&user_id=0
    [27-Sep-2023 11:18:59 UTC] BPS_REQUEST-search-0:
    [27-Sep-2023 11:18:59 UTC] BPSSEARCH
    [27-Sep-2023 11:18:59 UTC] BPSFILTERMEMBERS:type=active&action=active&page=1&scope=personal&per_page=1&user_id=5
    [27-Sep-2023 11:18:59 UTC] BPS_REQUEST-search-0:
    [27-Sep-2023 11:18:59 UTC] BPSSEARCH
    [27-Sep-2023 11:18:59 UTC] BPSFILTERMEMBERS:type=active&action=active&page=1&include=3%2C6%2C106%2C228%2C953%2C3480%2C4234%2C4247%2C4540%2C5551%2C6455%2C7005%2C8977%2C9632%2C11347%2C23479%2C25848&scope=following&per_page=1
    [27-Sep-2023 11:18:59 UTC] BPS_REQUEST-search-0:
    [27-Sep-2023 11:18:59 UTC] BPSSEARCH

    I can’t figure out why these functions has to be called 10 times.

    #12082 Reply
    Par
    Guest

    After reviewing the code further I found that there was a javascript on our site that made the whole thing run twice. But still it triggering the same function 5 times. That seems odd to me but maybe it is some wordpress logic behind that?

    #12083 Reply
    andrea
    Plugin Author

    Hi Par,

    Thank you for your report!

    I’m not sure what’s going on here, but please note that BP Profile Search is only triggered by the ‘bp_ajax_querystring’ filter:

    add_filter ('bp_ajax_querystring', 'bps_filter_members', 99, 2);
    function bps_filter_members ($querystring, $object)
    {
    	if ($object != 'members')  return $querystring;
    
    	$request = bps_get_request ('search');
    	if (empty ($request))  return $querystring;
    
    	$results = bps_search ($request);
            ...
    

    and BuddyPress calls that filter only once, in the ‘members-loop.php’ directory page template:

    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) : ?>
    

    One possible guess is that the other calls are made by other plugins or by custom code. In that case, unfortunately, BP Profile Search would not be able to distinguish between calls and would be triggered multiple times.

    Please keep me informed of your further findings, I’m very interested in fixing any bugs you may discover. Thanks!

    #12088 Reply
    Par
    Guest

    Thanks for your swift reply.

    I reviewed this further and found that there is a filter in Buddyboss that triggered the bp_ajax_querystring another 3 times in order to count members for the tabs All, Following and My connections.

    I removed it in functions.php in my child theme like this:

    add_filter('bp_nouveau_object_template_result','remove_tabs_filter', 2, 2);
    function remove_tabs_filter($result, $object) {
    	if (has_filter('bp_nouveau_object_template_result', 'bp_nouveau_object_template_results_members_tabs'))
    	{
    		remove_filter( 'bp_nouveau_object_template_result', 'bp_nouveau_object_template_results_members_tabs', 10, 2 );
    		if (has_filter('bp_nouveau_object_template_result', 'bp_nouveau_object_template_results_members_tabs'))
    		{
    				remove_filter( 'bp_nouveau_object_template_result', 'bp_nouveau_object_template_results_members_tabs', 10, 2 );
    		}
    	}
    	return $result;
    }

    I have removed the tabs in question on the members page by overriding index.php in the members folder.

    My intention is to fetch the values of the fields in the form in order to save the search and monitor the for new members matching the search.
    I assume that using the filter hook ‘bps_request’ will be the best option to do that?

    Seems that it’s triggered several times but $type=’form’ seems to be only once.

    #12089 Reply
    andrea
    Plugin Author

    Hi Par,

    To check for new members who match a search, you could save $request from the actual search, then use it later, say once a day, with:

        $new_members = ... // get new members
        $results = bps_search ($saved_request, $new_members);
    

    The above call performs the saved search within $new_members.

    Of course this is just a basic idea, but I hope it helps!

Reply to: Each search triggers 10 calls? Why?
My Information