Skip to content

Instantly share code, notes, and snippets.

@dcavins
dcavins / bp_docs_filter_default_access_options.php
Created March 27, 2024 13:49
Change the default access options for new BP Docs.
<?php
add_filter( 'bp_docs_get_default_access_options', 'my_change_docs_default_access_levels' );
function my_change_docs_default_access_levels( $defaults ) {
/**
* Possible access capabilities are 'read', 'edit', 'read_comments', 'post_comments',
* 'view_history', or 'manage'.
* Possible access options are 'loggedin', 'creator', or 'anyone'.
* If BuddyPress groups are activated, then additional access options are available
* to docs that are associated with a group:
* 'group-members' and 'admins-mods'.
@dcavins
dcavins / regexp-remove-prepositions
Last active May 30, 2023 21:02
RegExp for finding articles and prepositions in a string
<?php
$needles = array( 'a', 'an', 'the', 'and', 'or', 'but', 'aboard', 'about', 'above', 'across', 'after', 'against', 'along', 'amid', 'among', 'anti', 'around', 'as', 'at', 'before', 'behind', 'below', 'beneath', 'beside', 'besides', 'between', 'beyond', 'but', 'by', 'concerning', 'considering', 'despite', 'down', 'during', 'except', 'excepting', 'excluding', 'following', 'for', 'from', 'in', 'inside', 'into', 'like', 'minus', 'near', 'of', 'off', 'on', 'onto', 'opposite', 'outside', 'over', 'past', 'per', 'plus', 'regarding', 'round', 'save', 'since', 'than', 'through', 'to', 'toward', 'towards', 'under', 'underneath', 'unlike', 'until', 'up', 'upon', 'versus', 'via', 'with', 'within', 'without', 'long' );
$needles = implode('|', $needles);
// ?: signifies a non-capturing group
// http://www.regular-expressions.info/captureall.html
$pattern = "/(?:(?:\b(?:$needles)\b(?:\W(?:$needles)\b)*))/";
$haystack = 'By the Light of the Silvery Moon and Stars except during or until Dawn';
$replacement = '(r)$0(/r)';
@dcavins
dcavins / bp_docs_groups_allow_group_admins_to_manage_access.php
Created May 22, 2023 20:39
Allow group administrators and moderators to manage group-associated BuddyPress Docs.
<?php
add_filter( 'bp_docs_map_meta_caps', 'bp_docs_groups_allow_group_admins_to_manage_access', 20, 4 );
function bp_docs_groups_allow_group_admins_to_manage_access( $caps, $cap, $user_id, $args ) {
// We only want to act on the "manage" capability, in group situations.
if ( 'bp_docs_manage' !== $cap ) {
return $caps;
}
$doc = bp_docs_get_doc_for_caps( $args );
if ( empty( $doc ) ) {
return $caps;
@dcavins
dcavins / enable-global-folders-for-bpdocs.php
Last active November 9, 2022 19:24
Enable BuddyPress Docs folders for all contexts (not just groups which is the default)
<?php
// Enables folders for group, user, and global doc directories.
add_filter( 'bp_docs_enable_folders_for_current_context', '__return_true' );
// Shows the folder metabox on the doc edit screen.
add_filter( 'bp_docs_folders_force_metabox', '__return_true' );
@dcavins
dcavins / compose.php
Last active September 17, 2022 08:23
Limit access to the private message form if the user has sent too many messages today.
<?php
/**
* BuddyPress - Members Single Messages Compose
*
* @package BuddyPress
* @subpackage bp-legacy
*/
?>
@dcavins
dcavins / add-group-activity.php
Created October 19, 2017 20:52
Add an activity item for a new custom post type item creation.
<?php
/**
* Create an activity item to appear in a group. Fires once a post has been saved.
*
* @param int $post_ID Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated or not.
*/
function add_activity_item_for_group( $post_id, $post_object, $update ) {
@dcavins
dcavins / elevate-bp-docs-access-setttings.php
Last active February 21, 2022 18:58
Find all BP Docs that have perrmissive read settings ("anyone") and change them to "loggedin" or "group-members".
<?php
// Ignore folder association temporarily.
remove_filter( 'bp_docs_tax_query', 'bp_docs_folder_tax_query', 10, 2 );
$doc_args = array(
// 'folder' => ,
'posts_per_page' => 50,
'paged' => 1, // increment this to work through the docs.
);
if ( bp_docs_has_docs( $doc_args ) ) :
@dcavins
dcavins / auto-approve-some-bp-membership-requests.php
Created December 13, 2021 17:26
Automatically approve membership requests that satisfy some criteria.
<?php
/**
* If a user submits a site membership request, but has a
* 'my-school.edu' email address, bypass the manual approval of the request.
*
* @param bool $send Whether or not this membership request should be approved
* immediately and the activation email sent.
* Default is `false` meaning that the request should be
* manually approved by a site admin.
* @param array $details The details of the request.
@dcavins
dcavins / bp-docs-group-admins-can-read-and-edit.php
Last active June 17, 2021 15:52
Change BP Docs capabilities so that group admins and mods can read and edit any doc associated with the group.
<?php
/**
* Allow group admins and mods to read and edit any BP Doc
* associated with a group they moderate.
* Note: this will allow direct access at `/docs/doc-title/` but
* the docs will not be listed in the group docs directory if the
* access level "should" prevent the user from accessing it.
* So it's of limited usefulness.
*
* @param string[] $caps Primitive capabilities required of the user.
@dcavins
dcavins / disallow-creator-access-for-group-docs.php
Created June 16, 2021 14:24
BP Docs: Don't allow group docs to have "creator only" access
<?php
add_filter( 'bp_docs_get_access_options', 'remove_creator_option_for_group_docs', 10, 4 );
function remove_creator_option_for_group_docs( $options, $settings_field, $doc_id = 0, $group_id = 0 ) {
if ( ! $group_id ) {
$group_id = bp_docs_get_associated_group_id( $doc_id );
}
// If this is the Doc creation page, check to see whether a
// group id has been passed somewhere
if ( empty( $group_id ) ) {