Skip to:
Content

BuddyPress.org

Ticket #5272: bp-blogs-classes.php.patch

File bp-blogs-classes.php.patch, 12.6 KB (added by lenasterg, 10 years ago)

Patched file /bp-blogs/bp-blogs-classes.php

  • bp-blogs-classes.php

     
    66 * @package BuddyPress
    77 * @subpackage BlogsClasses
    88 */
    9 
    109// Exit if accessed directly
    1110if ( !defined( 'ABSPATH' ) ) exit;
    1211
     12
    1313/**
    1414 * The main BuddyPress blog class.
    1515 *
     
    1919 * @since BuddyPress (1.0.0)
    2020 */
    2121class BP_Blogs_Blog {
     22
    2223        public $id;
    2324        public $user_id;
    2425        public $blog_id;
     
    103104
    104105        /**
    105106         * Retrieve a set of blog-user associations.
    106          *
     107     * @param array $args {
     108     *     An array of arguments. All items are optional.
    107109         * @param string $type The order in which results should be returned.
    108110         *        'active', 'alphabetical', 'newest', or 'random'.
    109111         * @param int|bool $limit Optional. The maximum records to return.
     
    114116         *        retrieved. Default: 0.
    115117         * @param string|bool $search_terms Optional. Search by text stored in
    116118         *        blogmeta (such as the blog name). Default: false.
     119     * @param bool $show_hidden. Whether to include private blogs of user
     120     * }
    117121         * @return array Multidimensional results array, structured as follows:
    118122         *           'blogs' - Array of located blog objects
    119123         *           'total' - A count of the total blogs matching the filter params
    120124         */
    121         public static function get( $type, $limit = false, $page = false, $user_id = 0, $search_terms = false ) {
     125    public static function get($args = array()) {
    122126                global $bp, $wpdb;
     127        // Backward compatibility with old method of passing arguments
     128        if (!is_array($args) || func_num_args() > 1) {
     129            _deprecated_argument(__METHOD__, '2', sprintf(__('Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress'), __METHOD__, __FILE__));
     130            $old_args_keys = array(
     131                0 => 'type',
     132                1 => 'per_page',
     133                2 => 'page',
     134                3 => 'user_id',
     135                4 => 'search_terms',
     136                5 => 'show_hidden',
     137            );
    123138
    124                 if ( !is_user_logged_in() || ( !bp_current_user_can( 'bp_moderate' ) && ( $user_id != bp_loggedin_user_id() ) ) )
     139            $func_args = func_get_args();
     140            $args = bp_core_parse_args_array($old_args_keys, $func_args);
     141        }
     142
     143        $defaults = array(
     144            'type' => 'active',
     145            'per_page' => false,
     146            'page' => false,
     147            'user_id' => 0,
     148            'search_terms' => false, // Terms to search by
     149            'show_hidden' => false, // Show blogs marked hidden
     150        );
     151        $r = wp_parse_args($args, $defaults);
     152        extract($r);
     153
     154        $hidden_sql = '';
     155        if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate')) {
    125156                        $hidden_sql = "AND wb.public = 1";
    126                 else
    127                         $hidden_sql = '';
     157        }
    128158
    129                 $pag_sql = ( $limit && $page ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : '';
     159        $pag_sql = ( $per_page && $page ) ? $wpdb->prepare(" LIMIT %d, %d", intval(( $page - 1 ) * $per_page), intval($per_page)) : '';
    130160
    131161                $user_sql = !empty( $user_id ) ? $wpdb->prepare( " AND b.user_id = %d", $user_id ) : '';
    132162
     
    234264                if ( !$user_id )
    235265                        $user_id = bp_displayed_user_id();
    236266
    237                 // Show logged in users their hidden blogs.
    238                 if ( !bp_is_my_profile() && !$show_hidden )
     267// Show logged in users their hidden blogs or user can moderate
     268        if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate'))
    239269                        $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id, b.id, bm1.meta_value as name, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND bm1.meta_key = 'name' AND wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ORDER BY b.blog_id", $user_id ) );
    240270                else
    241271                        $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id, b.id, bm1.meta_value as name, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND bm1.meta_key = 'name' AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ORDER BY b.blog_id", $user_id ) );
    242272
    243                 $total_blog_count = BP_Blogs_Blog::total_blog_count_for_user( $user_id );
     273        $total_blog_count = BP_Blogs_Blog::total_blog_count_for_user($user_id, $show_hidden);
    244274
    245275                $user_blogs = array();
     276
    246277                foreach ( (array) $blogs as $blog ) {
    247278                        $user_blogs[$blog->blog_id] = new stdClass;
    248279                        $user_blogs[$blog->blog_id]->id = $blog->id;
     
    294325         *
    295326         * @param int $user_id Optional. ID of the user whose blogs are being
    296327         *        queried. Defaults to logged-in user.
     328     * @param bool $show_hidden. Whether to count also private blogs
    297329         * @return int Blog count for the user.
    298330         */
    299         public static function total_blog_count_for_user( $user_id = null ) {
     331    public static function total_blog_count_for_user($user_id = null, $show_hidden = false) {
    300332                global $bp, $wpdb;
    301333
    302334                if ( !$user_id )
    303335                        $user_id = bp_displayed_user_id();
    304336
    305                 // If the user is logged in return the blog count including their hidden blogs.
    306                 if ( ( is_user_logged_in() && $user_id == bp_loggedin_user_id() ) || bp_current_user_can( 'bp_moderate' ) ) {
     337        // If the user is logged in return the blog count including their hidden blogs or is show hidden user blogs is set.
     338        if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate')) {
     339            return $wpdb->get_var($wpdb->prepare("SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d", $user_id));
     340        } else {
    307341                        return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d", $user_id ) );
    308                 } else {
    309                         return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d", $user_id ) );
    310342                }
    311343        }
    312344
     
    317349         * blogmeta table.
    318350         *
    319351         * @param string $filter The search term.
    320          * @param int $limit Optional. The maximum number of items to return.
     352     * @param int $per_page Optional. The maximum number of items per page to return.
    321353         *        Default: null (no limit).
    322354         * @param int $page Optional. The page of results to return. Default:
    323355         *        null (no limit).
     356     * @param bool $show_hidden Optional. Whether to search in private blogs. Default:
     357     *        FALSE.
    324358         * @return array Multidimensional results array, structured as follows:
    325359         *           'blogs' - Array of located blog objects
    326360         *           'total' - A count of the total blogs matching the query.
    327361         */
    328         public static function search_blogs( $filter, $limit = null, $page = null ) {
     362    public static function search_blogs($filter, $per_page = null, $page = null, $show_hidden = FALSE) {
    329363                global $wpdb, $bp;
    330364
    331365                $filter = esc_sql( like_escape( $filter ) );
    332366
    333367                $hidden_sql = '';
    334                 if ( !bp_current_user_can( 'bp_moderate' ) )
     368
     369
     370        if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate'))
    335371                        $hidden_sql = "AND wb.public = 1";
    336372
    337                 if ( $limit && $page ) {
    338                         $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
     373        if ($per_page && $page) {
     374            $pag_sql = $wpdb->prepare(" LIMIT %d, %d", intval(( $page - 1 ) * $per_page), intval($per_page));
    339375                }
    340376
    341377                $paged_blogs = $wpdb->get_results( "SELECT DISTINCT bm.blog_id FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE ( ( bm.meta_key = 'name' OR bm.meta_key = 'description' ) AND bm.meta_value LIKE '%%$filter%%' ) {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY meta_value ASC{$pag_sql}" );
     
    350386         * Query will include hidden blogs if the logged-in user has the
    351387         * 'bp_moderate' cap.
    352388         *
    353          * @param int $limit Optional. The maximum number of items to return.
     389     * @param int $per_page Optional. The maximum number of items to return per page.
    354390         *        Default: null (no limit).
    355391         * @param int $page Optional. The page of results to return. Default:
    356392         *        null (no limit).
     393     * @param bool $show_hidden Optional. Whether to search in private blogs. Default:
     394     *        FALSE.
    357395         * @return array Multidimensional results array, structured as follows:
    358396         *           'blogs' - Array of located blog objects
    359397         *           'total' - A count of the total blogs.
    360398         */
    361         public static function get_all( $limit = null, $page = null ) {
     399    public static function get_all($per_page = null, $page = null, $show_hidden = false) {
    362400                global $bp, $wpdb;
     401        $hidden_sql = '';
     402        if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate')) {
     403            $hidden_sql = "AND wb.public = 1";
     404        }
     405        $pag_sql = ( $per_page && $page ) ? $wpdb->prepare(" LIMIT %d, %d", intval(( $page - 1 ) * $per_page), intval($per_page)) : '';
    363406
    364                 $hidden_sql = !bp_current_user_can( 'bp_moderate' ) ? "AND wb.public = 1" : '';
    365                 $pag_sql = ( $limit && $page ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : '';
    366 
    367407                $paged_blogs = $wpdb->get_results( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql} {$pag_sql}" );
    368408                $total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql}" );
    369409
     
    377417         * 'bp_moderate' cap.
    378418         *
    379419         * @param string $letter. The letter you're looking for.
    380          * @param int $limit Optional. The maximum number of items to return.
     420     * @param int $per_page Optional. The maximum number of items to return.
    381421         *        Default: null (no limit).
    382422         * @param int $page Optional. The page of results to return. Default:
    383423         *        null (no limit).
     424     * @param bool $show_hidden Optional. Whether to search in private blogs. Default:
     425     *        FALSE.
    384426         * @return array Multidimensional results array, structured as follows:
    385427         *           'blogs' - Array of located blog objects.
    386428         *           'total' - A count of the total blogs matching the query.
    387429         */
    388         public static function get_by_letter( $letter, $limit = null, $page = null ) {
     430    public static function get_by_letter($letter, $per_page = null, $page = null, $show_hidden = false) {
    389431                global $bp, $wpdb;
    390 
    391432                $letter = esc_sql( like_escape( $letter ) );
    392 
    393433                $hidden_sql = '';
    394                 if ( !bp_current_user_can( 'bp_moderate' ) )
     434        if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate')) {
    395435                        $hidden_sql = "AND wb.public = 1";
    396 
    397                 if ( $limit && $page )
    398                         $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    399 
     436        }
     437        if ($per_page && $page) {
     438            $pag_sql = $wpdb->prepare(" LIMIT %d, %d", intval(( $page - 1 ) * $per_page), intval($per_page));
     439        }
    400440                $paged_blogs = $wpdb->get_results( "SELECT DISTINCT bm.blog_id FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND bm.meta_value LIKE '$letter%%' {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC{$pag_sql}" );
    401441                $total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT bm.blog_id) FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND bm.meta_value LIKE '$letter%%' {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC" );
    402442
     
    488528
    489529                return false;
    490530        }
    491 }
     531}
     532 No newline at end of file