Making WordPress.org

Changeset 15333


Ignore:
Timestamp:
10/14/2026 07:45:19 PM (8 days ago)
Author:
obenland
Message:

Rosetta: Fix recursion handling, site ID usage, and property assignment in User Sync

Props timse201, obenland.
Closes ​https://github.com/WordPress/wordpress.org/pull/1006.
See #1983, #6326.
Fixes #8455.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/rosetta/inc/user/class-sync.php

    r4082 r15333  
    1919         * @var array
    2020         */
    21         private $roles_to_sync;
     21        private $roles_to_sync = [];
     22
     23        /**
     24         * Flag to prevent recursion.
     25         *
     26         * @var bool
     27         */
     28        private $is_syncing = false;
    2229
    2330        /**
    2431         * Constructor.
    2532         *
    26          * @param \stdClass $data
     33         * @param object|null $data Configuration object.
    2734         */
    2835        public function __construct( $data = null ) {
    29                 if ( isset( $data->destination_site ) ) {
     36                if ( isset( $data->destination_site ) && $data->destination_site instanceof WP_Site ) {
    3037                        $this->destination_site = $data->destination_site;
    3138                }
    3239
    33                 if ( isset( $data->roles_to_sync ) ) {
    34                         $this->destination_site = $data->roles_to_sync;
     40                if ( isset( $data->roles_to_sync ) && is_array( $data->roles_to_sync ) ) {
     41                        $this->roles_to_sync = $data->roles_to_sync;
    3542                }
    3643        }
    … …  
    5966         */
    6067        public function setup() {
    61                 if ( ! $this->destination_site || ! $this->roles_to_sync ) {
     68                if ( ! $this->destination_site instanceof WP_Site || empty( $this->roles_to_sync ) ) {
    6269                        return;
    6370                }
    6471
    65                 add_action( 'add_user_to_blog', [ $this, 'add_user_to_site' ], 10, 2 );
     72                add_action( 'add_user_to_blog', [ $this, 'add_user_to_site' ], 10, 3 );
    6673                add_action( 'add_user_role',    [ $this, 'add_user_to_site' ], 10, 2 );
    6774                add_action( 'set_user_role',    [ $this, 'add_user_to_site' ], 10, 2 );
    … …  
    6976
    7077        /**
    71          * Adds an user to another site with the same role.
     78         * Adds a user to another site with the same role if they are not already a member.
    7279         *
    73          * @param int    $user_id User ID.
    74          * @param string $role    User role.
     80         * @param int      $user_id User ID.
     81         * @param string   $role    User role.
     82         * @param int|null $blog_id Blog ID (passed by add_user_to_blog).
    7583         */
    76         public function add_user_to_site( $user_id, $role ) {
     84        public function add_user_to_site( $user_id, $role, $blog_id = null ) {
    7785                // Avoid recursion.
    78                 remove_action( 'add_user_to_blog', [ $this, 'add_user_to_site' ], 10 );
    79                 remove_action( 'add_user_role',    [ $this, 'add_user_to_site' ], 10 );
    80                 remove_action( 'set_user_role',    [ $this, 'add_user_to_site' ], 10 );
     86                if ( $this->is_syncing ) {
     87                        return;
     88                }
    8189
    82                 if ( ! in_array( $role, array_keys( $this->roles_to_sync ), true ) ) {
     90                // Prevent execution if triggered directly on the destination site.
     91                $source_id = $blog_id ? (int) $blog_id : get_current_blog_id();
     92                if ( $source_id === $this->destination_site->id ) {
     93                        return;
     94                }
     95
     96                if ( ! isset( $this->roles_to_sync[ $role ] ) ) {
    8397                        return;
    8498                }
    … …  
    88102                }
    89103
     104                $this->is_syncing = true;
    90105                add_user_to_blog( $this->destination_site->id, $user_id, $this->roles_to_sync[ $role ] );
     106                $this->is_syncing = false;
    91107        }
    92108}
Note: See TracChangeset for help on using the changeset viewer.