| @@ -22,8 +22,10 @@ | ||
| 22 | 22 | if ( !defined( 'ABSPATH' ) ) { |
| 23 | 23 | exit; |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | +// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 27 | + | |
| 26 | 28 | /** |
| 27 | 29 | * "Registered" group automation. |
| 28 | 30 | */ |
| 29 | 31 | class Groups_Registered { |
| @@ -34,8 +36,10 @@ | ||
| 34 | 36 | |
| 35 | 37 | /** |
| 36 | 38 | * Creates groups for registered users. |
| 37 | 39 | * Must be called explicitly or hooked into activation. |
| 40 | + * | |
| 41 | + * As of Groups 2.2.0 this does not trigger the 'groups_created_user_group' action for each entry. | |
| 38 | 42 | */ |
| 39 | 43 | public static function activate() { |
| 40 | 44 | |
| 41 | 45 | global $wpdb; |
| @@ -41,27 +45,21 @@ | ||
| 41 | 45 | global $wpdb; |
| 42 | 46 | |
| 43 | 47 | // create a group for the blog if it doesn't exist |
| 44 | 48 | if ( !( $group = Groups_Group::read_by_name( self::REGISTERED_GROUP_NAME ) ) ) { |
| 45 | - $group_id = Groups_Group::create( array( "name" => self::REGISTERED_GROUP_NAME ) ); | |
| 49 | + $group_id = Groups_Group::create( array( 'name' => self::REGISTERED_GROUP_NAME ) ); | |
| 46 | 50 | } else { |
| 47 | 51 | $group_id = $group->group_id; |
| 48 | 52 | } |
| 49 | 53 | if ( $group_id ) { |
| 50 | - $n = $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->users" ); | |
| 51 | - for ( $i = 0; $i < $n; $i += self::BATCH_LIMIT ) { | |
| 52 | - $users = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->users LIMIT %d, %d", $i, self::BATCH_LIMIT ) ); | |
| 53 | - foreach( $users as $user ) { | |
| 54 | - // add the user to the group | |
| 55 | - if ( !Groups_User_Group::read( $user->ID, $group_id ) ) { | |
| 56 | - Groups_User_Group::create( array( "user_id" => $user->ID, "group_id" => $group_id ) ); | |
| 57 | - } | |
| 58 | - } | |
| 59 | - unset( $users ); | |
| 60 | - if ( function_exists( 'gc_collect_cycles' ) ) { | |
| 61 | - gc_collect_cycles(); | |
| 62 | - } | |
| 63 | - } | |
| 54 | + $user_group_table = _groups_get_tablename( 'user_group' ); | |
| 55 | + $query = $wpdb->prepare( | |
| 56 | + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 57 | + "INSERT IGNORE INTO $user_group_table " . | |
| 58 | + "SELECT ID, %d FROM $wpdb->users", | |
| 59 | + Groups_Utility::id( $group_id ) | |
| 60 | + ); | |
| 61 | + $rows = $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 64 | 62 | } |
| 65 | 63 | } |
| 66 | 64 | |
| 67 | 65 | /** |
| @@ -68,11 +66,11 @@ | ||
| 68 | 66 | * Initialize hooks that handle addition and removal of users and blogs. |
| 69 | 67 | */ |
| 70 | 68 | public static function init() { |
| 71 | 69 | |
| 72 | - // For translation of the "Registered" group(s) | |
| 73 | - __( 'Registered', GROUPS_PLUGIN_DOMAIN ); | |
| 74 | - | |
| 70 | + // @since 3.3.1 | |
| 71 | + add_action( 'init', array( __CLASS__, 'wp_init' ) ); | |
| 72 | + | |
| 75 | 73 | // When a blog is added, create a new "Registered" group for that blog. |
| 76 | 74 | add_action( 'wpmu_new_blog', array( __CLASS__, 'wpmu_new_blog' ), 10, 2 ); |
| 77 | 75 | |
| 78 | 76 | // Remove group when a blog is deleted? When a blog is deleted, |
| @@ -89,12 +87,23 @@ | ||
| 89 | 87 | // Note : When a user is removed from a blog it's handled from core. |
| 90 | 88 | } |
| 91 | 89 | |
| 92 | 90 | /** |
| 91 | + * Hooked on the init action. | |
| 92 | + * | |
| 93 | + * @since 3.3.1 | |
| 94 | + */ | |
| 95 | + public static function wp_init() { | |
| 96 | + // For translation of the "Registered" group(s) | |
| 97 | + // @since 3.3.1 postponed to after the init action fired | |
| 98 | + __( 'Registered', 'groups' ); | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 93 | 102 | * Create "Registered" group for new blog and add its admin user. |
| 94 | - * | |
| 103 | + * | |
| 95 | 104 | * @see Groups_Controller::wpmu_new_blog() |
| 96 | - * | |
| 105 | + * | |
| 97 | 106 | * @param int $blog_id |
| 98 | 107 | * @param int $user_id blog's admin user's id |
| 99 | 108 | * @param string $domain (optional) |
| 100 | 109 | * @param string $path (optional) |
| @@ -105,16 +114,17 @@ | ||
| 105 | 114 | if ( is_multisite() ) { |
| 106 | 115 | Groups_Controller::switch_to_blog( $blog_id ); |
| 107 | 116 | } |
| 108 | 117 | if ( !( $group = Groups_Group::read_by_name( self::REGISTERED_GROUP_NAME ) ) ) { |
| 109 | - $group_id = Groups_Group::create( array( "name" => self::REGISTERED_GROUP_NAME ) ); | |
| 118 | + $group_id = Groups_Group::create( array( 'name' => self::REGISTERED_GROUP_NAME ) ); | |
| 110 | 119 | } else { |
| 111 | 120 | $group_id = $group->group_id; |
| 112 | 121 | } |
| 113 | 122 | // add the blog's admin user to the group |
| 114 | 123 | if ( $group_id ) { |
| 124 | + // Do NOT use Groups_User::user_is_member( ... ) here as we do not allow the result of this to be filtered: | |
| 115 | 125 | if ( !Groups_User_Group::read( $user_id, $group_id ) ) { |
| 116 | - Groups_User_Group::create( array( "user_id" => $user_id, "group_id" => $group_id ) ); | |
| 126 | + Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group_id ) ); | |
| 117 | 127 | } |
| 118 | 128 | } |
| 119 | 129 | if ( is_multisite() ) { |
| 120 | 130 | Groups_Controller::restore_current_blog(); |
| @@ -122,9 +132,9 @@ | ||
| 122 | 132 | } |
| 123 | 133 | |
| 124 | 134 | /** |
| 125 | 135 | * Assign a newly created user to its "Registered" group. |
| 126 | - * | |
| 136 | + * | |
| 127 | 137 | * @param int $user_id |
| 128 | 138 | */ |
| 129 | 139 | public static function user_register( $user_id ) { |
| 130 | 140 | |
| @@ -129,9 +139,9 @@ | ||
| 129 | 139 | public static function user_register( $user_id ) { |
| 130 | 140 | |
| 131 | 141 | $registered_group = Groups_Group::read_by_name( self::REGISTERED_GROUP_NAME ); |
| 132 | 142 | if ( !$registered_group ) { |
| 133 | - $registered_group_id = Groups_Group::create( array( "name" => self::REGISTERED_GROUP_NAME ) ); | |
| 143 | + $registered_group_id = Groups_Group::create( array( 'name' => self::REGISTERED_GROUP_NAME ) ); | |
| 134 | 144 | } else { |
| 135 | 145 | $registered_group_id = $registered_group->group_id; |
| 136 | 146 | } |
| 137 | 147 | if ( $registered_group_id ) { |
| @@ -180,12 +190,12 @@ | ||
| 180 | 190 | // later when wpmu_new_blog is triggered, the activation sequence has |
| 181 | 191 | // created the tables and all users of the new blog are added to |
| 182 | 192 | // that blog's "Registered" group. |
| 183 | 193 | $group_table = _groups_get_tablename( 'group' ); |
| 184 | - if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $group_table . "'" ) == $group_table ) { | |
| 194 | + if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $group_table . "'" ) == $group_table ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 185 | 195 | $registered_group = Groups_Group::read_by_name( self::REGISTERED_GROUP_NAME ); |
| 186 | 196 | if ( !$registered_group ) { |
| 187 | - $registered_group_id = Groups_Group::create( array( "name" => self::REGISTERED_GROUP_NAME ) ); | |
| 197 | + $registered_group_id = Groups_Group::create( array( 'name' => self::REGISTERED_GROUP_NAME ) ); | |
| 188 | 198 | } else { |
| 189 | 199 | $registered_group_id = $registered_group->group_id; |
| 190 | 200 | } |
| 191 | 201 | if ( $registered_group_id ) { |