Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Automattic\Jetpack\Sync; |
||
| 4 | |||
| 5 | use Automattic\Jetpack\Connection\Manager as Jetpack_Connection; |
||
| 6 | use Automattic\Jetpack\Roles; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class Users |
||
| 10 | * |
||
| 11 | * Responsible for syncing user data changes. |
||
| 12 | */ |
||
| 13 | class Users { |
||
| 14 | static $user_roles = array(); |
||
| 15 | static $connection = null; |
||
| 16 | |||
| 17 | static function init() { |
||
| 18 | // TODO: Eventually, this needs to be instantiated at the top level in the sync package. |
||
| 19 | self::$connection = new Jetpack_Connection(); |
||
| 20 | if ( self::$connection->is_active() ) { |
||
| 21 | // Kick off synchronization of user role when it changes |
||
| 22 | add_action( 'set_user_role', array( __CLASS__, 'user_role_change' ) ); |
||
| 23 | } |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Synchronize connected user role changes |
||
| 28 | */ |
||
| 29 | static function user_role_change( $user_id ) { |
||
| 30 | if ( self::$connection->is_user_connected( $user_id ) ) { |
||
| 31 | self::update_role_on_com( $user_id ); |
||
| 32 | // try to choose a new master if we're demoting the current one |
||
| 33 | self::maybe_demote_master_user( $user_id ); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | static function get_role( $user_id ) { |
||
| 38 | if ( isset( self::$user_roles[ $user_id ] ) ) { |
||
| 39 | return self::$user_roles[ $user_id ]; |
||
| 40 | } |
||
| 41 | |||
| 42 | $current_user_id = get_current_user_id(); |
||
| 43 | wp_set_current_user( $user_id ); |
||
| 44 | $roles = new Roles(); |
||
| 45 | $role = $roles->translate_current_user_to_role(); |
||
| 46 | wp_set_current_user( $current_user_id ); |
||
| 47 | $user_roles[ $user_id ] = $role; |
||
|
0 ignored issues
–
show
|
|||
| 48 | |||
| 49 | return $role; |
||
| 50 | } |
||
| 51 | |||
| 52 | static function get_signed_role( $user_id ) { |
||
| 53 | return \Jetpack::sign_role( self::get_role( $user_id ), $user_id ); |
||
| 54 | } |
||
| 55 | |||
| 56 | static function update_role_on_com( $user_id ) { |
||
| 57 | $signed_role = self::get_signed_role( $user_id ); |
||
| 58 | \Jetpack::xmlrpc_async_call( 'jetpack.updateRole', $user_id, $signed_role ); |
||
| 59 | } |
||
| 60 | |||
| 61 | static function maybe_demote_master_user( $user_id ) { |
||
| 62 | $master_user_id = \Jetpack_Options::get_option( 'master_user' ); |
||
| 63 | $role = self::get_role( $user_id ); |
||
| 64 | if ( $user_id == $master_user_id && 'administrator' != $role ) { |
||
| 65 | $query = new \WP_User_Query( |
||
| 66 | array( |
||
| 67 | 'fields' => array( 'id' ), |
||
| 68 | 'role' => 'administrator', |
||
| 69 | 'orderby' => 'id', |
||
| 70 | 'exclude' => array( $master_user_id ), |
||
| 71 | ) |
||
| 72 | ); |
||
| 73 | $new_master = false; |
||
| 74 | foreach ( $query->results as $result ) { |
||
| 75 | $found_user_id = absint( $result->id ); |
||
| 76 | if ( $found_user_id && self::$connection->is_user_connected( $found_user_id ) ) { |
||
| 77 | $new_master = $found_user_id; |
||
| 78 | break; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | if ( $new_master ) { |
||
| 83 | \Jetpack_Options::update_option( 'master_user', $new_master ); |
||
| 84 | } |
||
| 85 | // else disconnect..? |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.