Ticket #45640: get_blogs_of_users_new.patch
File get_blogs_of_users_new.patch, 15.7 KB (added by , 6 years ago) |
---|
-
src/wp-admin/includes/dashboard.php
diff --git a/src/wp-admin/includes/dashboard.php b/src/wp-admin/includes/dashboard.php index e3160bb390..4220e9ea3b 100644
a b function wp_dashboard_quick_press( $error_msg = false ) { 502 502 $post = get_default_post_to_edit( 'post', true ); 503 503 $user_id = get_current_user_id(); 504 504 // Don't create an option if this is a super admin who does not belong to this site. 505 if ( in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user_id ) ) ) ) {505 if ( in_array( get_current_blog_id(), get_blogs_of_user( $user_id, false, array( 'fields' => 'ids' ) ) ) ) 506 506 update_user_option( $user_id, 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID 507 507 } 508 508 } -
src/wp-admin/includes/ms.php
diff --git a/src/wp-admin/includes/ms.php b/src/wp-admin/includes/ms.php index f8e3fc09e9..68f78af7b9 100644
a b 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() { 630 630 return; 631 631 } 632 632 633 $blogs = get_blogs_of_user( get_current_user_id() ); 633 /** 634 * Filters arguments when getting user's blogs for access denied message. 635 * 636 * @param array $args An array of optional arguments used when getting blogs. 637 */ 638 $args = apply_filters( 'access_denied_splash_get_blogs_of_user_args', array() ); 639 $blogs = get_blogs_of_user( get_current_user_id(), false, $args ); 634 640 635 641 if ( wp_list_filter( $blogs, array( 'userblog_id' => get_current_blog_id() ) ) ) { 636 642 return; … … function choose_primary_blog() { 808 814 <th scope="row"><label for="primary_blog"><?php _e( 'Primary Site' ); ?></label></th> 809 815 <td> 810 816 <?php 811 $all_blogs = get_blogs_of_user( get_current_user_id() );812 $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true );813 if ( count( $all_blogs ) > 1 ) {817 $all_blogs_ids = get_blogs_of_user( get_current_user_id(), false, array( 'fields' => 'ids' ) ); 818 $primary_blog = get_user_meta( get_current_user_id(), 'primary_blog', true ); 819 if ( count( $all_blogs_ids ) > 1 ) { 814 820 $found = false; 815 821 ?> 816 822 <select name="primary_blog" id="primary_blog"> -
src/wp-admin/network/users.php
diff --git a/src/wp-admin/network/users.php b/src/wp-admin/network/users.php index 7f212329f6..40561263ef 100644
a b 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, true, array( 'fields' => '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);90 foreach ( (array) $blogs as $details) {91 update_blog_status( $ details->userblog_id, 'spam', '0' );89 $blogs_ids = get_blogs_of_user( $user_id, true, array( 'fields' => 'ids' ) ); 90 foreach ( (array) $blogs_ids as $blog_id ) { 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 a/src/wp-admin/user-new.php b/src/wp-admin/user-new.php index a63a329d6d..fae976a41e 100644
a b 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, true, 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 a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 20d04e1a85..cf5ad6d2fc 100644
a b function set_url_scheme( $url, $scheme = null ) { 3601 3601 function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) { 3602 3602 $user_id = $user_id ? (int) $user_id : get_current_user_id(); 3603 3603 3604 $blogs = get_blogs_of_user( $user_id);3605 if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs ) ) {3604 $blogs_exist = get_blogs_of_user( $user_id, false, array( 'number' => 1, 'fields' => 'ids' ) ); 3605 if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs_exist ) ) { 3606 3606 $url = user_admin_url( $path, $scheme ); 3607 3607 } elseif ( ! is_multisite() ) { 3608 3608 $url = admin_url( $path, $scheme ); 3609 3609 } else { 3610 3610 $current_blog = get_current_blog_id(); 3611 if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) { 3611 $blogs_ids = get_blogs_of_user( $user_id, true, array( 'fields' => 'ids' ) ); 3612 if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, $blogs_ids ) ) ) { 3612 3613 $url = admin_url( $path, $scheme ); 3613 3614 } else { 3614 3615 $active = get_active_blog_for_user( $user_id ); -
src/wp-includes/ms-functions.php
diff --git a/src/wp-includes/ms-functions.php b/src/wp-includes/ms-functions.php index d27f71954c..daaf9bef89 100644
a b 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() { 1745 1745 function get_most_recent_post_of_user( $user_id ) { 1746 1746 global $wpdb; 1747 1747 1748 $user_blogs = get_blogs_of_user( (int) $user_id);1748 $user_blogs_ids = get_blogs_of_user( (int) $user_id, false, array( 'fields' => 'ids' ) ); 1749 1749 $most_recent_post = array(); 1750 1750 1751 1751 // Walk through each blog and get the most recent post 1752 1752 // published by $user_id 1753 foreach ( (array) $user_blogs as $blog) {1754 $prefix = $wpdb->get_blog_prefix( $blog ->userblog_id );1753 foreach ( (array) $user_blogs_ids as $blog_id ) { 1754 $prefix = $wpdb->get_blog_prefix( $blog_id ); 1755 1755 $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 ); 1756 1756 1757 1757 // Make sure we found a post … … function get_most_recent_post_of_user( $user_id ) { 1763 1763 // most recent post. 1764 1764 if ( ! isset( $most_recent_post['post_gmt_ts'] ) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) { 1765 1765 $most_recent_post = array( 1766 'blog_id' => $blog ->userblog_id,1766 'blog_id' => $blog_id, 1767 1767 'post_id' => $recent_post['ID'], 1768 1768 'post_date_gmt' => $recent_post['post_date_gmt'], 1769 1769 'post_gmt_ts' => $post_gmt_ts, -
src/wp-includes/user.php
diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 9696789dd5..b9fc415e4b 100644
a b function get_users( $args = array() ) { 593 593 * 594 594 * @global wpdb $wpdb WordPress database abstraction object. 595 595 * 596 * @param int $user_id User ID 597 * @param bool $all Whether to retrieve all sites, or only sites that are not 598 * marked as deleted, archived, or spam. 596 * @param int $user_id User ID 597 * @param bool|string $all Whether to retrieve all sites, or only sites that are not 598 * marked as deleted, archived, or spam. 599 * @param array $args An array of optional arguments that will be passed to 600 * 'get_sites' function. 599 601 * @return array A list of the user's sites. An empty array if the user doesn't exist 600 602 * or belongs to no sites. 601 603 */ 602 function get_blogs_of_user( $user_id, $all = false ) {604 function get_blogs_of_user( $user_id, $all = false, $args = false ) { 603 605 global $wpdb; 604 606 605 607 $user_id = (int) $user_id; … … function get_blogs_of_user( $user_id, $all = false ) { 617 619 * 618 620 * @since 4.6.0 619 621 * 620 * @param null|array $sites An array of site objects of which the user is a member. 621 * @param int $user_id User ID. 622 * @param bool $all Whether the returned array should contain all sites, including 623 * those marked 'deleted', 'archived', or 'spam'. Default false. 622 * @param null|array $sites An array of site objects of which the user is a member. 623 * @param int $user_id User ID. 624 * @param bool|string $all Whether to retrieve all sites, or only sites that are not 625 * marked as deleted, archived, or spam. 626 * @param array $args An array of optional arguments that will be passed to 627 * 'get_sites' function. 624 628 */ 625 $sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all );629 $sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all, $args ); 626 630 627 631 if ( null !== $sites ) { 628 632 return $sites; … … function get_blogs_of_user( $user_id, $all = false ) { 675 679 $sites = array(); 676 680 677 681 if ( ! empty( $site_ids ) ) { 678 $ args = array(682 $default_args = array( 679 683 'number' => '', 680 684 'site__in' => $site_ids, 681 685 'update_site_meta_cache' => false, 682 686 ); 683 687 if ( ! $all ) { 684 $ args['archived'] = 0;685 $ args['spam'] = 0;686 $ args['deleted'] = 0;688 $default_args['archived'] = 0; 689 $default_args['spam'] = 0; 690 $default_args['deleted'] = 0; 687 691 } 688 692 693 $args = wp_parse_args( $args, $default_args ); 694 695 /** 696 * Filters the list of arguments that are passed to get_sites function. 697 * 698 * @param array $args An array of optional arguments that will be passed to 699 * 'get_sites' function. 700 * @param int $user_id User ID. 701 * @param bool|string $all Whether to retrieve all sites, or only sites that are not 702 * marked as deleted, archived, or spam. 703 */ 704 $args = apply_filters( 'get_blogs_of_user_get_sites_args', $args, $user_id, $all ); 705 689 706 $_sites = get_sites( $args ); 690 707 691 foreach ( $_sites as $site ) { 692 $sites[ $site->id ] = (object) array( 693 'userblog_id' => $site->id, 694 'blogname' => $site->blogname, 695 'domain' => $site->domain, 696 'path' => $site->path, 697 'site_id' => $site->network_id, 698 'siteurl' => $site->siteurl, 699 'archived' => $site->archived, 700 'mature' => $site->mature, 701 'spam' => $site->spam, 702 'deleted' => $site->deleted, 703 ); 708 if( isset( $args[ 'fields' ] ) && $args[ 'fields' ] === 'ids' ) { 709 return $_sites; 710 } else { 711 foreach ( $_sites as $site ) { 712 $sites[ $site->id ] = (object) array( 713 'userblog_id' => $site->id, 714 'blogname' => $site->blogname, 715 'domain' => $site->domain, 716 'path' => $site->path, 717 'site_id' => $site->network_id, 718 'siteurl' => $site->siteurl, 719 'archived' => $site->archived, 720 'mature' => $site->mature, 721 'spam' => $site->spam, 722 'deleted' => $site->deleted, 723 ); 724 } 704 725 } 705 726 } 706 727 … … function get_blogs_of_user( $user_id, $all = false ) { 714 735 * @param bool $all Whether the returned sites array should contain all sites, including 715 736 * those marked 'deleted', 'archived', or 'spam'. Default false. 716 737 */ 717 return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all );738 return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all, $args ); 718 739 } 719 740 720 741 /**