Skip to:
Content

BuddyPress.org

Changeset 10110


Ignore:
Timestamp:
09/14/2015 03:39:38 PM (11 years ago)
Author:
boonebgorges
Message:

When a copmonent directory is set to the front page, don't interfere with blog searches.

WP's default search query arg is 's', so that URLs like example.com?s=foo lead
to a search of blog posts on the term 'foo'. When a BuddyPress component page
like Members is set to the front page, it interferes with this behavior in two
ways: (1) BuddyPress interprets example.com?s=foo as a Members directory
request, and (2) the 's' query arg is interpreted as a Members search term.

We fix this conflict as follows:

  • During the URL parsing process, don't interpret requests of the form example.com?s=foo as being BP component requests, even if a BP component is set to the front page.
  • In order to make BuddyPress component searches continue to work, we change the default search query arguments for each component to be unique. For example, member directory searches look like example.com?members_search=foo. Components register their own 'search_query_arg', and these args can be filtered with the new 'bp_core_get_component_search_query_arg' filter.

Fixes #5087.

Location:
trunk/src
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-loader.php

    r10077 r10110  
    3030                        buddypress()->plugin_dir,
    3131                        array(
    32                                 'adminbar_myaccount_order' => 10
     32                                'adminbar_myaccount_order' => 10,
     33                                'search_query_arg' => 'activity_search',
    3334                        )
    3435                );
  • trunk/src/bp-activity/bp-activity-template.php

    r10077 r10110  
    642642                : false;
    643643
     644        $search_terms_default = false;
     645        $search_query_arg = bp_core_get_component_search_query_arg( 'activity' );
     646        if ( ! empty( $_REQUEST[ $search_query_arg ] ) ) {
     647                $search_terms_default = stripslashes( $_REQUEST[ $search_query_arg ] );
     648        }
     649
    644650        /** Parse Args ************************************************************/
    645651
    … …  
    677683
    678684                // Searching
    679                 'search_terms'      => false,        // specify terms to search on
     685                'search_terms'      => $search_terms_default,
    680686                'update_meta_cache' => true,
    681687        ), 'has_activities' );
  • trunk/src/bp-blogs/bp-blogs-loader.php

    r10100 r10110  
    2727                        buddypress()->plugin_dir,
    2828                        array(
    29                                 'adminbar_myaccount_order' => 30
     29                                'adminbar_myaccount_order' => 30,
     30                                'search_query_arg' => 'sites_search',
    3031                        )
    3132                );
  • trunk/src/bp-blogs/bp-blogs-template.php

    r10100 r10110  
    385385 *     @type array    $include_blog_ids Array of blog IDs to limit results to.
    386386 *     @type string   $sort             'ASC' or 'DESC'. Default: 'DESC'.
    387  *     @type string   $search_terms     Limit results by a search term. Default: the
    388  *                                      value of $_REQUEST['s'], if present.
     387 *     @type string   $search_terms     Limit results by a search term. Default: the value of `$_REQUEST['s']` or
     388 *                                      `$_REQUEST['sites_search']`, if present.
    389389 *     @type int      $user_id          The ID of the user whose blogs should be retrieved.
    390390 *                                      When viewing a user profile page, 'user_id' defaults to the
    … …  
    396396        global $blogs_template;
    397397
    398         // Check for and use search terms
    399         $search_terms = ! empty( $_REQUEST['s'] )
    400                 ? $_REQUEST['s']
    401                 : false;
     398        // Check for and use search terms.
     399        $search_terms_default = false;
     400        $search_query_arg = bp_core_get_component_search_query_arg( 'blogs' );
     401        if ( ! empty( $_REQUEST[ $search_query_arg ] ) ) {
     402                $search_terms_default = stripslashes( $_REQUEST[ $search_query_arg ] );
     403        } elseif ( ! empty( $_REQUEST['s'] ) ) {
     404                $search_terms_default = stripslashes( $_REQUEST['s'] );
     405        }
    402406
    403407        // Parse arguments
    … …  
    410414                'user_id'           => bp_displayed_user_id(), // Pass a user_id to limit to only blogs this user is a member of
    411415                'include_blog_ids'  => false,
    412                 'search_terms'      => $search_terms,          // Pass search terms to filter on the blog title or description.
     416                'search_terms'      => $search_terms_default,
    413417                'update_meta_cache' => true
    414418        ), 'has_blogs' );
    … …  
    14501454 */
    14511455function bp_directory_blogs_search_form() {
    1452         $default_search_value = bp_get_search_default_text();
    1453         $search_value         = !empty( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : $default_search_value;
     1456
     1457        $query_arg = bp_core_get_component_search_query_arg( 'blogs' );
     1458
     1459        if ( ! empty( $_REQUEST[ $query_arg ] ) ) {
     1460                $search_value = stripslashes( $_REQUEST[ $query_arg ] );
     1461        } else {
     1462                $search_value = bp_get_search_default_text( 'blogs' );
     1463        }
    14541464
    14551465        $search_form_html = '<form action="" method="get" id="search-blogs-form">
    1456                 <label><input type="text" name="s" id="blogs_search" placeholder="'. esc_attr( $search_value ) .'" /></label>
     1466                <label><input type="text" name="' . esc_attr( $query_arg ) . '" id="blogs_search" placeholder="'. esc_attr( $search_value ) .'" /></label>
    14571467                <input type="submit" id="blogs_search_submit" name="blogs_search_submit" value="' . __( 'Search', 'buddypress' ) . '" />
    14581468        </form>';
  • trunk/src/bp-core/bp-core-catchuri.php

    r10108 r10110  
    322322        }
    323323
    324         // Set the current action
    325         $bp->current_action = isset( $bp_uri[$uri_offset + 1] ) ? $bp_uri[$uri_offset + 1] : '';
     324        // Determine the current action.
     325        $current_action = isset( $bp_uri[ $uri_offset + 1 ] ) ? $bp_uri[ $uri_offset + 1 ] : '';
     326
     327        /*
     328         * If a BuddyPress directory is set to the WP front page, URLs like example.com/members/?s=foo
     329         * shouldn't interfere with blog searches.
     330         */
     331        if ( empty( $current_action) && ! empty( $_GET['s'] ) && 'page' == get_option( 'show_on_front' ) && ! empty( $match->id ) ) {
     332                $page_on_front = (int) get_option( 'page_on_front' );
     333                if ( (int) $match->id === $page_on_front ) {
     334                        $bp->current_component = '';
     335                        return false;
     336                }
     337        }
     338
     339        $bp->current_action = $current_action;
    326340
    327341        // Slice the rest of the $bp_uri array and reset offset
  • trunk/src/bp-core/bp-core-component.php

    r10108 r10110  
    127127        public $global_tables = array();
    128128
     129        /**
     130         * Query argument for component search URLs.
     131         *
     132         * @since 2.4.0
     133         * @var string
     134         */
     135        public $search_query_arg = 's';
     136
    129137        /** Methods ***************************************************************/
    130138
    … …  
    135143         * @since 1.9.0 Added $params as a parameter.
    136144         * @since 2.3.0 Added $params['features'] as a configurable value.
     145         * @since 2.4.0 Added $params['search_query_arg'] as a configurable value.
    137146         *
    138147         * @param string $id   Unique ID. Letters, numbers, and underscores only.
    … …  
    142151         * @param array  $params {
    143152         *     Additional parameters used by the component.
    144          *     @type int   $adminbar_myaccount_order Set the position for our menu under the WP Toolbar's "My Account menu".
    145          *     @type array $features                 An array of feature names. This is used to load additional files from your
    146          *                                           component directory and for feature active checks. eg. array( 'awesome' )
    147          *                                           would look for a file called "bp-{$this->id}-awesome.php" and you could use
    148          *                                           bp_is_active( $this->id, 'awesome' ) to determine if the feature is active.
     153         *     @type int    $adminbar_myaccount_order Set the position for our menu under the WP Toolbar's "My Account menu".
     154         *     @type array  $features                 An array of feature names. This is used to load additional files from your
     155         *                                            component directory and for feature active checks. eg. array( 'awesome' )
     156         *                                            would look for a file called "bp-{$this->id}-awesome.php" and you could use
     157         *                                            bp_is_active( $this->id, 'awesome' ) to determine if the feature is active.
     158         *     @type string $search_query_arg         String to be used as the query argument in component search URLs.
    149159         * }
    150160         */
    … …  
    170180                        if ( ! empty( $params['features'] ) ) {
    171181                                $this->features = array_map( 'sanitize_title', (array) $params['features'] );
     182                        }
     183
     184                        if ( ! empty( $params['search_query_arg'] ) ) {
     185                                $this->search_query_arg = sanitize_title( $params['search_query_arg'] );
    172186                        }
    173187
  • trunk/src/bp-core/bp-core-functions.php

    r10108 r10110  
    852852function bp_core_add_illegal_names() {
    853853        update_site_option( 'illegal_names', get_site_option( 'illegal_names' ), array() );
     854}
     855
     856/**
     857 * Get the 'search' query argument for a given component.
     858 *
     859 * @since 2.4.0
     860 *
     861 * @param string $component Component name.
     862 * @return string|bool Query argument on success. False on failure.
     863 */
     864function bp_core_get_component_search_query_arg( $component ) {
     865        $query_arg = false;
     866        if ( isset( buddypress()->{$component}->search_query_arg ) ) {
     867                $query_arg = sanitize_title( buddypress()->{$component}->search_query_arg );
     868        }
     869
     870        /**
     871         * Filters the query arg for a component search string.
     872         *
     873         * @since 2.4.0
     874         *
     875         * @param string $query_arg Query argument.
     876         * @param string $component Component name.
     877         */
     878        return apply_filters( 'bp_core_get_component_search_query_arg', $query_arg, $component );
    854879}
    855880
  • trunk/src/bp-groups/bp-groups-loader.php

    r9936 r10110  
    9191                        buddypress()->plugin_dir,
    9292                        array(
    93                                 'adminbar_myaccount_order' => 70
     93                                'adminbar_myaccount_order' => 70,
     94                                'search_query_arg' => 'groups_search',
    9495                        )
    9596                );
  • trunk/src/bp-groups/bp-groups-template.php

    r10075 r10110  
    487487 *                                         to groups of which the specified user
    488488 *                                         is a member. Default: null.
    489  *     @type string       $search_terms    If provided, only groups whose names or descriptions
    490  *                                         match the search terms will be returned. Default: false.
     489 *     @type string       $search_terms    If provided, only groups whose names or descriptions match the search terms
     490 *                                         will be returned. Default: value of `$_REQUEST['groups_search']` or
     491 *                                         `$_REQUEST['s']`, if present. Otherwise false.
    491492 *     @type array        $meta_query      An array of meta_query conditions.
    492493 *                                         See {@link WP_Meta_Query::queries} for description.
    … …  
    534535
    535536        // Default search string (too soon to escape here)
    536         if ( ! empty( $_REQUEST['group-filter-box'] ) ) {
     537        $search_query_arg = bp_core_get_component_search_query_arg( 'groups' );
     538        if ( ! empty( $_REQUEST[ $search_query_arg ] ) ) {
     539                $search_terms = stripslashes( $_REQUEST[ $search_query_arg ] );
     540        } elseif ( ! empty( $_REQUEST['group-filter-box'] ) ) {
    537541                $search_terms = $_REQUEST['group-filter-box'];
    538542        } elseif ( !empty( $_REQUEST['s'] ) ) {
    … …  
    49794983function bp_directory_groups_search_form() {
    49804984
    4981         $default_search_value = bp_get_search_default_text( 'groups' );
    4982         $search_value         = !empty( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : $default_search_value;
     4985        $query_arg = bp_core_get_component_search_query_arg( 'groups' );
     4986
     4987        if ( ! empty( $_REQUEST[ $query_arg ] ) ) {
     4988                $search_value = stripslashes( $_REQUEST[ $query_arg ] );
     4989        } else {
     4990                $search_value = bp_get_search_default_text( 'groups' );
     4991        }
    49834992
    49844993        $search_form_html = '<form action="" method="get" id="search-groups-form">
    4985                 <label><input type="text" name="s" id="groups_search" placeholder="'. esc_attr( $search_value ) .'" /></label>
     4994                <label><input type="text" name="' . esc_attr( $query_arg ) . '" id="groups_search" placeholder="'. esc_attr( $search_value ) .'" /></label>
    49864995                <input type="submit" id="groups_search_submit" name="groups_search_submit" value="'. __( 'Search', 'buddypress' ) .'" />
    49874996        </form>';
  • trunk/src/bp-members/bp-members-loader.php

    r9936 r10110  
    3535                        buddypress()->plugin_dir,
    3636                        array(
    37                                 'adminbar_myaccount_order' => 20
     37                                'adminbar_myaccount_order' => 20,
     38                                'search_query_arg' => 'members_s',
    3839                        )
    3940                );
  • trunk/src/bp-members/bp-members-template.php

    r9977 r10110  
    532532 *     @type string|array          $member_type__not_in Array or comma-separated list of member types to exclude
    533533 *                                                      from results.
    534  *     @type string                $search_terms        Limit results by a search term. Default: null.
     534 *     @type string                $search_terms        Limit results by a search term. Default: value of
     535 *                                                      `$_REQUEST['members_search']` or `$_REQUEST['s']`, if present.
     536 *                                                      Otherwise false.
    535537 *     @type string                $meta_key            Limit results by the presence of a usermeta key.
    536538 *                                                      Default: false.
    … …  
    563565        }
    564566
     567        $search_terms_default = null;
     568        $search_query_arg = bp_core_get_component_search_query_arg( 'members' );
     569        if ( ! empty( $_REQUEST[ $search_query_arg ] ) ) {
     570                $search_terms_default = stripslashes( $_REQUEST[ $search_query_arg ] );
     571        }
     572
    565573        // type: active ( default ) | random | newest | popular | online | alphabetical
    566574        $r = bp_parse_args( $args, array(
    … …  
    579587                'member_type__in'     => '',
    580588                'member_type__not_in' => '',
    581                 'search_terms'        => null,     // Pass search_terms to filter users by their profile data
     589                'search_terms'        => $search_terms_default,
    582590
    583591                'meta_key'            => false,    // Only return users with this usermeta
    … …  
    13351343function bp_directory_members_search_form() {
    13361344
    1337         $default_search_value = bp_get_search_default_text( 'members' );
    1338         $search_value         = !empty( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : $default_search_value;
     1345        $query_arg = bp_core_get_component_search_query_arg( 'members' );
     1346
     1347        if ( ! empty( $_REQUEST[ $query_arg ] ) ) {
     1348                $search_value = stripslashes( $_REQUEST[ $query_arg ] );
     1349        } else {
     1350                $search_value = bp_get_search_default_text( 'members' );
     1351        }
    13391352
    13401353        $search_form_html = '<form action="" method="get" id="search-members-form">
    1341                 <label><input type="text" name="s" id="members_search" placeholder="'. esc_attr( $search_value ) .'" /></label>
     1354                <label><input type="text" name="' . esc_attr( $query_arg ) . '" id="members_search" placeholder="'. esc_attr( $search_value ) .'" /></label>
    13421355                <input type="submit" id="members_search_submit" name="members_search_submit" value="' . __( 'Search', 'buddypress' ) . '" />
    13431356        </form>';
Note: See TracChangeset for help on using the changeset viewer.