Skip to:
Content

BuddyPress.org

Changeset 12255


Ignore:
Timestamp:
10/20/2018 02:38:18 PM (6 years ago)
Author:
boonebgorges
Message:

bp-legacy: Disable cookie-based directory filter "preferences" for logged-out users.

Logged-out users who refresh the page will see the filters reset to their
default values, rather than the last-used value. Logged-in users continue
to have their preferences saved in a cookie.

This behavior is configurable with the 'bp_legacy_store_filter_settings'
filter.

See #7856.

Location:
trunk/src/bp-templates/bp-legacy
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-templates/bp-legacy/buddypress-functions.php

    r12198 r12255  
    284284
    285285        /**
     286         * Filters whether directory filter settings ('scope', etc) should be stored in a persistent cookie.
     287         *
     288         * @since 4.0.0
     289         *
     290         * @param bool $store_filter_settings Whether to store settings. Defaults to true for logged-in users.
     291         */
     292        $store_filter_settings = apply_filters( 'bp_legacy_store_filter_settings', is_user_logged_in() );
     293
     294        /**
    286295         * Filters core JavaScript strings for internationalization before AJAX usage.
    287296         *
     
    291300         */
    292301        $params = apply_filters( 'bp_core_get_js_strings', array(
     302            // Strings for display.
    293303            'accepted'            => __( 'Accepted', 'buddypress' ),
    294304            'close'               => __( 'Close', 'buddypress' ),
     
    304314            'unsaved_changes'     => __( 'Your profile has unsaved changes. If you leave the page, the changes will be lost.', 'buddypress' ),
    305315            'view'                => __( 'View', 'buddypress' ),
     316
     317            // Settings.
     318            'store_filter_settings' => $store_filter_settings,
    306319        ) );
    307320        wp_localize_script( $asset['handle'], 'BP_DTheme', $params );
  • trunk/src/bp-templates/bp-legacy/js/buddypress.js

    r12182 r12255  
    1010var newest_activities = '';
    1111var activity_last_recorded  = 0;
     12
     13var directoryPreferences = {}
    1214
    1315jq(document).ready( function() {
     
    849851            }
    850852
    851             bp_filter_request( object, jq.cookie('bp-' + object + '-filter'), jq.cookie('bp-' + object + '-scope') , 'div.' + object, search_terms, 1, jq.cookie('bp-' + object + '-extras'), null, template );
     853            var scope  = bp_get_directory_preference( object, 'scope' );
     854            var filter = bp_get_directory_preference( object, 'filter' );
     855            var extras = bp_get_directory_preference( object, 'extras' );
     856
     857            bp_filter_request( object, filter, scope, 'div.' + object, search_terms, 1, extras, null, template );
    852858
    853859            return false;
     
    884890            search_terms = jq('#' + object + '_search').val();
    885891
    886             bp_filter_request( object, filter, scope, 'div.' + object, search_terms, 1, jq.cookie('bp-' + object + '-extras') );
     892            var extras = bp_get_directory_preference( object, 'extras' );
     893
     894            bp_filter_request( object, filter, scope, 'div.' + object, search_terms, 1, extras );
    887895
    888896            return false;
     
    932940        }
    933941
    934         bp_filter_request( object, filter, scope, 'div.' + object, search_terms, 1, jq.cookie('bp-' + object + '-extras'), null, template );
     942        var extras = bp_get_directory_preference( object, 'extras' );
     943
     944        bp_filter_request( object, filter, scope, 'div.' + object, search_terms, 1, extras, null, template );
    935945
    936946        return false;
     
    10201030            }
    10211031
    1022             bp_filter_request( object, jq.cookie('bp-' + object + '-filter'), jq.cookie('bp-' + object + '-scope'), 'div.' + object, search_terms, page_number, jq.cookie('bp-' + object + '-extras'), caller, template );
     1032            var scope  = bp_get_directory_preference( object, 'scope' );
     1033            var filter = bp_get_directory_preference( object, 'filter' );
     1034            var extras = bp_get_directory_preference( object, 'extras' );
     1035
     1036            bp_filter_request( object, filter, scope, 'div.' + object, search_terms, page_number, extras, caller, template );
    10231037
    10241038            return false;
     
    13881402            object = 'messages';
    13891403
     1404            var scope  = bp_get_directory_preference( object, 'scope' );
     1405            var filter = bp_get_directory_preference( object, 'filter' );
     1406            var extras = bp_get_directory_preference( object, 'extras' );
     1407
    13901408            bp_filter_request(
    13911409                object,
    1392                 jq.cookie('bp-' + object + '-filter'),
    1393                 jq.cookie('bp-' + object + '-scope'),
     1410                filter,
     1411                scope,
    13941412                'div.' + object, jq('#messages_search').val(),
    13951413                1,
    1396                 jq.cookie('bp-' + object + '-extras')
     1414                extras
    13971415            );
    13981416
     
    17241742});
    17251743
     1744/**
     1745 * Gets the user's current preference for a directory option.
     1746 */
     1747function bp_get_directory_preference( directoryType, pref ) {
     1748    var defaultPrefs = {
     1749        filter: '',
     1750        scope: '',
     1751        extras: ''
     1752    }
     1753
     1754    if ( ! directoryPreferences.hasOwnProperty( directoryType ) ) {
     1755        var newPreferences = {};
     1756        for ( var prefName in defaultPrefs ) {
     1757            if ( defaultPrefs.hasOwnProperty( prefName ) ) {
     1758                newPreferences[ prefName ] = defaultPrefs[ prefName ];
     1759            }
     1760        }
     1761        directoryPreferences[ directoryType ] = newPreferences;
     1762    }
     1763
     1764    if ( BP_DTheme.store_filter_settings ) {
     1765        directoryPreferences[ directoryType ][ pref ] = jq.cookie( 'bp-' + directoryType + '-' + pref );
     1766    }
     1767
     1768    return directoryPreferences[ directoryType ][ pref ];
     1769}
     1770
     1771/**
     1772 * Sets the user's current preference for a directory option.
     1773 */
     1774function bp_set_directory_preference( directoryType, pref, value ) {
     1775    var defaultPrefs = {
     1776        filter: '',
     1777        scope: '',
     1778        extras: ''
     1779    }
     1780
     1781    if ( ! directoryPreferences.hasOwnProperty( directoryType ) ) {
     1782        var newPreferences = {};
     1783        for ( var prefName in defaultPrefs ) {
     1784            if ( defaultPrefs.hasOwnProperty( prefName ) ) {
     1785                newPreferences[ prefName ] = defaultPrefs[ prefName ];
     1786            }
     1787        }
     1788        directoryPreferences[ directoryType ] = newPreferences;
     1789    }
     1790
     1791    if ( BP_DTheme.store_filter_settings ) {
     1792        jq.cookie( 'bp-' + directoryType + '-' + pref, value, {
     1793            path: '/',
     1794            secure: ( 'https:' === window.location.protocol )
     1795        } );
     1796    }
     1797
     1798    directoryPreferences[ directoryType ][ pref ] = value
     1799}
     1800
    17261801/* Setup activity scope and filter based on the current cookie settings. */
    17271802function bp_init_activity() {
    1728     if ( undefined !== jq.cookie('bp-activity-filter') && jq('#activity-filter-select').length ) {
    1729         jq('#activity-filter-select select option[value="' + jq.cookie('bp-activity-filter') + '"]').prop( 'selected', true );
     1803    var scope  = bp_get_directory_preference( 'activity', 'scope' );
     1804    var filter = bp_get_directory_preference( 'activity', 'filter' );
     1805
     1806    if ( undefined !== filter && jq('#activity-filter-select').length ) {
     1807        jq('#activity-filter-select select option[value="' + filter + '"]').prop( 'selected', true );
    17301808    }
    17311809
    17321810    /* Activity Tab Set */
    1733     if ( undefined !== jq.cookie('bp-activity-scope') && jq('.activity-type-tabs').length ) {
     1811    if ( undefined !== scope && jq('.activity-type-tabs').length ) {
    17341812        jq('.activity-type-tabs li').each( function() {
    17351813            jq(this).removeClass('selected');
    17361814        });
    1737         jq('#activity-' + jq.cookie('bp-activity-scope') + ', .item-list-tabs li.current').addClass('selected');
     1815        jq('#activity-' + scope + ', .item-list-tabs li.current').addClass('selected');
    17381816    }
    17391817}
     
    17421820function bp_init_objects(objects) {
    17431821    jq(objects).each( function(i) {
    1744         if ( undefined !== jq.cookie('bp-' + objects[i] + '-filter') && jq('#' + objects[i] + '-order-select select').length ) {
    1745             jq('#' + objects[i] + '-order-select select option[value="' + jq.cookie('bp-' + objects[i] + '-filter') + '"]').prop( 'selected', true );
    1746         }
    1747 
    1748         if ( undefined !== jq.cookie('bp-' + objects[i] + '-scope') && jq('div.' + objects[i]).length ) {
     1822        var scope  = bp_get_directory_preference( objects[i], 'scope' );
     1823        var filter = bp_get_directory_preference( objects[i], 'filter' );
     1824
     1825        if ( undefined !== filter && jq('#' + objects[i] + '-order-select select').length ) {
     1826            jq('#' + objects[i] + '-order-select select option[value="' + filter + '"]').prop( 'selected', true );
     1827        }
     1828
     1829        if ( undefined !== scope && jq('div.' + objects[i]).length ) {
    17491830            jq('.item-list-tabs li').each( function() {
    17501831                jq(this).removeClass('selected');
    17511832            });
    1752             jq('#' + objects[i] + '-' + jq.cookie('bp-' + objects[i] + '-scope') + ', #object-nav li.current').addClass('selected');
     1833            jq('#' + objects[i] + '-' + scope + ', #object-nav li.current').addClass('selected');
    17531834        }
    17541835    });
     
    17651846    }
    17661847
    1767     /* Save the settings we want to remain persistent to a cookie */
    1768     jq.cookie( 'bp-' + object + '-scope', scope, {
    1769         path: '/',
    1770         secure: ( 'https:' === window.location.protocol )
    1771     } );
    1772     jq.cookie( 'bp-' + object + '-filter', filter, {
    1773         path: '/',
    1774         secure: ( 'https:' === window.location.protocol )
    1775     } );
    1776     jq.cookie( 'bp-' + object + '-extras', extras, {
    1777         path: '/',
    1778         secure: ( 'https:' === window.location.protocol )
    1779     } );
     1848    /* Save the settings we want to remain persistent */
     1849    bp_set_directory_preference( object, 'scope', scope );
     1850    bp_set_directory_preference( object, 'filter', filter );
     1851    bp_set_directory_preference( object, 'extras', extras );
    17801852
    17811853    /* Set the correct selected nav and filter */
Note: See TracChangeset for help on using the changeset viewer.