Changeset 15333
- Timestamp:
- 10/14/2026 07:45:19 PM (8 days ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/rosetta/inc/user/class-sync.php
r4082 r15333 19 19 * @var array 20 20 */ 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; 22 29 23 30 /** 24 31 * Constructor. 25 32 * 26 * @param \stdClass $data33 * @param object|null $data Configuration object. 27 34 */ 28 35 public function __construct( $data = null ) { 29 if ( isset( $data->destination_site ) ) {36 if ( isset( $data->destination_site ) && $data->destination_site instanceof WP_Site ) { 30 37 $this->destination_site = $data->destination_site; 31 38 } 32 39 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; 35 42 } 36 43 } … … 59 66 */ 60 67 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 ) ) { 62 69 return; 63 70 } 64 71 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 ); 66 73 add_action( 'add_user_role', [ $this, 'add_user_to_site' ], 10, 2 ); 67 74 add_action( 'set_user_role', [ $this, 'add_user_to_site' ], 10, 2 ); … … 69 76 70 77 /** 71 * Adds a n 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. 72 79 * 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). 75 83 */ 76 public function add_user_to_site( $user_id, $role ) {84 public function add_user_to_site( $user_id, $role, $blog_id = null ) { 77 85 // 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 } 81 89 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 ] ) ) { 83 97 return; 84 98 } … … 88 102 } 89 103 104 $this->is_syncing = true; 90 105 add_user_to_blog( $this->destination_site->id, $user_id, $this->roles_to_sync[ $role ] ); 106 $this->is_syncing = false; 91 107 } 92 108 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)