Ticket #45640: get_blogs_of_users.diff
File get_blogs_of_users.diff, 16.0 KB (added by , 6 years ago) |
---|
-
src/wp-admin/includes/dashboard.php
diff --git src/wp-admin/includes/dashboard.php src/wp-admin/includes/dashboard.php index 5b13f686a1..2f3ba90bae 100644
function wp_dashboard_quick_press( $error_msg = false ) { 504 504 $post = get_default_post_to_edit( 'post', true ); 505 505 $user_id = get_current_user_id(); 506 506 // Don't create an option if this is a super admin who does not belong to this site. 507 if ( in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user_id ) ) ) ) {507 if ( in_array( get_current_blog_id(), get_blogs_of_user( $user_id, false, array( 'fields' => 'ids' ) ) ) ) 508 508 update_user_option( $user_id, 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID 509 509 } 510 510 } -
src/wp-admin/includes/ms.php
diff --git src/wp-admin/includes/ms.php src/wp-admin/includes/ms.php index 8672946da5..6e75ebfe25 100644
function wpmu_delete_user( $id ) { 165 165 */ 166 166 do_action( 'wpmu_delete_user', $id ); 167 167 168 $blogs = get_blogs_of_user( $id);168 $blogs_ids = get_blogs_of_user( $id, false, array( 'fields' => 'ids' ) ); 169 169 170 if ( ! empty( $blogs ) ) {171 foreach ( $blogs as $blog) {172 switch_to_blog( $blog ->userblog_id );173 remove_user_from_blog( $id, $blog ->userblog_id );170 if ( ! empty( $blogs_ids ) ) { 171 foreach ( $blogs_ids as $blog_id ) { 172 switch_to_blog( $blog_id ); 173 remove_user_from_blog( $id, $blog_id ); 174 174 175 175 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) ); 176 176 foreach ( (array) $post_ids as $post_id ) { … … function _access_denied_splash() { 626 626 return; 627 627 } 628 628 629 $blogs = get_blogs_of_user( get_current_user_id() ); 629 /** 630 * Filters arguments when getting user's blogs for access denied message. 631 * 632 * @param array $args An array of optional arguments used when getting blogs. 633 */ 634 $args = apply_filters( 'access_denied_splash_get_blogs_of_user_args', array() ); 635 $blogs = get_blogs_of_user( get_current_user_id(), false, $args ); 630 636 631 637 if ( wp_list_filter( $blogs, array( 'userblog_id' => get_current_blog_id() ) ) ) { 632 638 return; … … function choose_primary_blog() { 804 810 <th scope="row"><label for="primary_blog"><?php _e( 'Primary Site' ); ?></label></th> 805 811 <td> 806 812 <?php 807 $all_blogs = get_blogs_of_user( get_current_user_id() );808 $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true );809 if ( count( $all_blogs ) > 1 ) {813 $all_blogs_ids = get_blogs_of_user( get_current_user_id(), false, array( 'fields' => 'ids' ) ); 814 $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true ); 815 if ( count( $all_blogs_ids ) > 1 ) { 810 816 $found = false; 811 817 ?> 812 818 <select name="primary_blog" id="primary_blog"> -
src/wp-admin/network/users.php
diff --git src/wp-admin/network/users.php src/wp-admin/network/users.php index 7f212329f6..a4f5c19d1b 100644
if ( isset( $_GET['action'] ) ) { 75 75 } 76 76 77 77 $userfunction = 'all_spam'; 78 $blogs = get_blogs_of_user( $user_id, true);79 foreach ( (array) $blogs as $details) {80 if ( $ details->userblog_id != get_network()->site_id ) { // main blog not a spam !81 update_blog_status( $ details->userblog_id, 'spam', '1' );78 $blogs_ids = get_blogs_of_user( $user_id, 'all_ids' ); 79 foreach ( (array) $blogs_ids as $blog_id ) { 80 if ( $blog_id != get_network()->site_id ) { // main blog not a spam ! 81 update_blog_status( $blog_id, 'spam', '1' ); 82 82 } 83 83 } 84 84 update_user_status( $user_id, 'spam', '1' ); … … if ( isset( $_GET['action'] ) ) { 86 86 87 87 case 'notspam': 88 88 $userfunction = 'all_notspam'; 89 $blogs = get_blogs_of_user( $user_id, true);89 $blogs_ids = get_blogs_of_user( $user_id, 'all_ids' ); 90 90 foreach ( (array) $blogs as $details ) { 91 update_blog_status( $ details->userblog_id, 'spam', '0' );91 update_blog_status( $blog_id, 'spam', '0' ); 92 92 } 93 93 94 94 update_user_status( $user_id, 'spam', '0' ); -
src/wp-admin/user-new.php
diff --git src/wp-admin/user-new.php src/wp-admin/user-new.php index 48387b0373..14de89e1d7 100644
if ( isset( $_REQUEST['action'] ) && 'adduser' == $_REQUEST['action'] ) { 63 63 $redirect = 'user-new.php'; 64 64 $username = $user_details->user_login; 65 65 $user_id = $user_details->ID; 66 if ( $username != null && array_key_exists( $blog_id, get_blogs_of_user( $user_id) ) ) {66 if ( $username != null && in_array( $blog_id, get_blogs_of_user( $user_id, array( 'fields' => 'ids' ) ) ) ) { 67 67 $redirect = add_query_arg( array( 'update' => 'addexisting' ), 'user-new.php' ); 68 68 } else { 69 69 if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) { -
src/wp-includes/link-template.php
diff --git src/wp-includes/link-template.php src/wp-includes/link-template.php index a56c9635ee..a6452f59e9 100644
function set_url_scheme( $url, $scheme = null ) { 3600 3600 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) { 3601 3601 $user_id = $user_id ? (int) $user_id : get_current_user_id(); 3602 3602 3603 $blogs = get_blogs_of_user( $user_id);3604 if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs ) ) {3603 $blogs_exist = get_blogs_of_user( $user_id, false, array( 'number' => 1, 'fields' => 'ids' ) ); 3604 if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs_exist ) ) { 3605 3605 $url = user_admin_url( $path, $scheme ); 3606 3606 } elseif ( ! is_multisite() ) { 3607 3607 $url = admin_url( $path, $scheme ); 3608 3608 } else { 3609 3609 $current_blog = get_current_blog_id(); 3610 if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) { 3610 $blogs_ids = get_blogs_of_user( $user_id, 'all_ids' ); 3611 if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, $blogs_ids ) ) ) { 3611 3612 $url = admin_url( $path, $scheme ); 3612 3613 } else { 3613 3614 $active = get_active_blog_for_user( $user_id ); -
src/wp-includes/ms-functions.php
diff --git src/wp-includes/ms-functions.php src/wp-includes/ms-functions.php index eca785f201..f58b30ba8b 100644
function get_sitestats() { 38 38 * @return WP_Site|void The blog object 39 39 */ 40 40 function get_active_blog_for_user( $user_id ) { 41 $blogs = get_blogs_of_user( $user_id);42 if ( empty( $blogs ) ) {41 $blogs_ids = get_blogs_of_user( $user_id, false, array( 'fields' => 'ids' ) ); 42 if ( empty( $blogs_ids ) ) { 43 43 return; 44 44 } 45 45 46 46 if ( ! is_multisite() ) { 47 return $blogs[ get_current_blog_id() ];47 return get_site( get_current_blog_id() ); 48 48 } 49 49 50 $primary_blog = get_user_meta( $user_id, 'primary_blog', true );51 $first_blog = current( $blogs);50 $primary_blog = get_user_meta( $user_id, 'primary_blog', true ); 51 $first_blog_id = current($blogs_ids); 52 52 if ( false !== $primary_blog ) { 53 if ( ! i sset( $blogs[ $primary_blog ]) ) {54 update_user_meta( $user_id, 'primary_blog', $first_blog ->userblog_id );55 $primary = get_site( $first_blog ->userblog_id );53 if ( ! in_array( $primary_blog, $blogs_ids ) ) { 54 update_user_meta( $user_id, 'primary_blog', $first_blog_id ); 55 $primary = get_site( $first_blog_id ); 56 56 } else { 57 57 $primary = get_site( $primary_blog ); 58 58 } 59 59 } else { 60 60 //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog? 61 $result = add_user_to_blog( $first_blog ->userblog_id, $user_id, 'subscriber' );61 $result = add_user_to_blog( $first_blog_id, $user_id, 'subscriber' ); 62 62 63 63 if ( ! is_wp_error( $result ) ) { 64 update_user_meta( $user_id, 'primary_blog', $first_blog ->userblog_id );65 $primary = $first_blog;64 update_user_meta( $user_id, 'primary_blog', $first_blog_id ); 65 $primary = get_site( $first_blog_id ); 66 66 } 67 67 } 68 68 69 69 if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) { 70 $blogs = get_blogs_of_user( $user_id, true); // if a user's primary blog is shut down, check their other blogs.71 $ret = false;72 if ( is_array( $blogs ) && count( $blogs ) > 0 ) {73 foreach ( (array) $blogs as $blog_id => $blog) {70 $blogs_ids = get_blogs_of_user( $user_id, true, array( 'fields' => 'ids' ) ); // if a user's primary blog is shut down, check their other blogs. 71 $ret = false; 72 if ( is_array( $blogs_ids ) && count( $blogs_ids ) > 0 ) { 73 foreach ( (array) $blogs_ids as $blog_id ) { 74 74 if ( $blog->site_id != get_current_network_id() ) { 75 75 continue; 76 76 } … … function remove_user_from_blog( $user_id, $blog_id = '', $reassign = '' ) { 250 250 if ( $primary_blog == $blog_id ) { 251 251 $new_id = ''; 252 252 $new_domain = ''; 253 $ blogs = get_blogs_of_user( $user_id);254 foreach ( (array) $ blogs as $blog) {255 if ( $ blog->userblog_id == $blog_id ) {253 $user_blogs_ids = get_blogs_of_user($user_id, false, array( 'fields' => 'ids' )); 254 foreach ( (array) $user_blogs_ids as $user_blog_id ) { 255 if ( $user_blog_id == $blog_id ) { 256 256 continue; 257 257 } 258 258 $new_id = $blog->userblog_id; … … function remove_user_from_blog( $user_id, $blog_id = '', $reassign = '' ) { 273 273 274 274 $user->remove_all_caps(); 275 275 276 $blogs = get_blogs_of_user( $user_id);276 $blogs = get_blogs_of_user($user_id, false, array( 'fields' => 'ids' )); 277 277 if ( count( $blogs ) == 0 ) { 278 278 update_user_meta( $user_id, 'primary_blog', '' ); 279 279 update_user_meta( $user_id, 'source_domain', '' ); … … function get_current_site() { 1743 1743 function get_most_recent_post_of_user( $user_id ) { 1744 1744 global $wpdb; 1745 1745 1746 $user_blogs = get_blogs_of_user( (int) $user_id);1746 $user_blogs_ids = get_blogs_of_user( (int) $user_id, false, array( 'fields' => 'ids' ) ); 1747 1747 $most_recent_post = array(); 1748 1748 1749 1749 // Walk through each blog and get the most recent post 1750 1750 // published by $user_id 1751 foreach ( (array) $user_blogs as $blog) {1752 $prefix = $wpdb->get_blog_prefix( $blog ->userblog_id );1751 foreach ( (array) $user_blogs_ids as $blog_id ) { 1752 $prefix = $wpdb->get_blog_prefix( $blog_id ); 1753 1753 $recent_post = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_date_gmt FROM {$prefix}posts WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1", $user_id ), ARRAY_A ); 1754 1754 1755 1755 // Make sure we found a post … … function get_most_recent_post_of_user( $user_id ) { 1761 1761 // most recent post. 1762 1762 if ( ! isset( $most_recent_post['post_gmt_ts'] ) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) { 1763 1763 $most_recent_post = array( 1764 'blog_id' => $blog ->userblog_id,1764 'blog_id' => $blog_id, 1765 1765 'post_id' => $recent_post['ID'], 1766 1766 'post_date_gmt' => $recent_post['post_date_gmt'], 1767 1767 'post_gmt_ts' => $post_gmt_ts, -
src/wp-includes/user.php
diff --git src/wp-includes/user.php src/wp-includes/user.php index 09be161e15..85ca2d54a6 100644
function get_users( $args = array() ) { 597 597 * 598 598 * @global wpdb $wpdb WordPress database abstraction object. 599 599 * 600 * @param int $user_id User ID 601 * @param bool $all Whether to retrieve all sites, or only sites that are not 602 * marked as deleted, archived, or spam. 600 * @param int $user_id User ID 601 * @param bool|string $all Whether to retrieve all sites, or only sites that are not 602 * marked as deleted, archived, or spam. Alternatively can 603 * force function to return just ids of all sites by using 604 * 'all_ids' as value. 605 * @param array $args An array of optional arguments that will be passed to 606 * 'get_sites' function. 603 607 * @return array A list of the user's sites. An empty array if the user doesn't exist 604 608 * or belongs to no sites. 605 609 */ 606 function get_blogs_of_user( $user_id, $all = false ) {610 function get_blogs_of_user( $user_id, $all = false, $args = false ) { 607 611 global $wpdb; 608 612 609 613 $user_id = (int) $user_id; … … function get_blogs_of_user( $user_id, $all = false ) { 621 625 * 622 626 * @since 4.6.0 623 627 * 624 * @param null|array $sites An array of site objects of which the user is a member. 625 * @param int $user_id User ID. 626 * @param bool $all Whether the returned array should contain all sites, including 627 * those marked 'deleted', 'archived', or 'spam'. Default false. 628 * @param null|array $sites An array of site objects of which the user is a member. 629 * @param int $user_id User ID. 630 * @param bool|string $all Whether the returned array should contain all sites, including 631 * those marked 'deleted', 'archived', or 'spam'. Alternatively can 632 * force function to return just ids of all sites by using 633 * 'all_ids' as value. 634 * @param array $args An array of optional arguments that will be passed to 635 * 'get_sites' function. 628 636 */ 629 $sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all );637 $sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all, $args ); 630 638 631 639 if ( null !== $sites ) { 632 640 return $sites; … … function get_blogs_of_user( $user_id, $all = false ) { 676 684 $site_ids[] = (int) $site_id; 677 685 } 678 686 687 if( $all === 'all_ids' ) 688 return $site_ids; 689 679 690 $sites = array(); 680 691 681 692 if ( ! empty( $site_ids ) ) { 682 $ args = array(693 $default_args = array( 683 694 'number' => '', 684 695 'site__in' => $site_ids, 685 696 ); 686 697 if ( ! $all ) { 687 $ args['archived'] = 0;688 $ args['spam'] = 0;689 $ args['deleted'] = 0;698 $default_args['archived'] = 0; 699 $default_args['spam'] = 0; 700 $default_args['deleted'] = 0; 690 701 } 691 702 703 $args = wp_parse_args( $args, $default_args ); 704 /** 705 * Filters the list of arguments that are passed to get_sites function. 706 * 707 * @param array $args An array of optional arguments that will be passed to 708 * 'get_sites' function. 709 * @param int $user_id User ID. 710 * @param bool|string $all Whether the returned array should contain all sites, including 711 * those marked 'deleted', 'archived', or 'spam'. Alternatively can 712 * force function to return just ids of all sites by using 713 * 'all_ids' as value. 714 */ 715 $args = apply_filters( 'get_blogs_of_user_get_sites_args', $args, $user_id, $all ); 716 692 717 $_sites = get_sites( $args ); 693 718 694 foreach ( $_sites as $site ) { 695 $sites[ $site->id ] = (object) array( 696 'userblog_id' => $site->id, 697 'blogname' => $site->blogname, 698 'domain' => $site->domain, 699 'path' => $site->path, 700 'site_id' => $site->network_id, 701 'siteurl' => $site->siteurl, 702 'archived' => $site->archived, 703 'mature' => $site->mature, 704 'spam' => $site->spam, 705 'deleted' => $site->deleted, 706 ); 719 if( isset( $args[ 'fields' ] ) && $args[ 'fields' ] === 'ids' ) { 720 return $_sites; 721 } else { 722 foreach ( $_sites as $site ) { 723 $sites[ $site->id ] = (object) array( 724 'userblog_id' => $site->id, 725 'blogname' => $site->blogname, 726 'domain' => $site->domain, 727 'path' => $site->path, 728 'site_id' => $site->network_id, 729 'siteurl' => $site->siteurl, 730 'archived' => $site->archived, 731 'mature' => $site->mature, 732 'spam' => $site->spam, 733 'deleted' => $site->deleted, 734 ); 735 } 707 736 } 708 737 } 709 738 … … function get_blogs_of_user( $user_id, $all = false ) { 717 746 * @param bool $all Whether the returned sites array should contain all sites, including 718 747 * those marked 'deleted', 'archived', or 'spam'. Default false. 719 748 */ 720 return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all );749 return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all, $args ); 721 750 } 722 751 723 752 /**