Skip to content

Commit

Permalink
Added event listener for site_url change
Browse files Browse the repository at this point in the history
  • Loading branch information
GiuseppeArcifa committed Jan 14, 2025
1 parent c7fcd6f commit c80ba7b
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions includes/Listeners/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function register_hooks() {
add_action( 'admin_footer', array( $this, 'view' ), 9 );
add_action( 'customize_controls_print_footer_scripts', array( $this, 'view' ) );

// Site URL changes
add_action( 'update_option_siteurl', array( $this, 'site_url_change' ), 10, 2 );

// Login
add_action( 'wp_login', array( $this, 'login' ), 10, 2 );

Expand Down Expand Up @@ -45,18 +48,44 @@ public function view() {
);
}

/**
* Trigger event for Site URL change
*
* @param string $old_url The old url option value
* @param string $new_url The new url option value
*
* @return void
*/
public function site_url_change( $old_url, $new_url ) {
if ( $new_url !== $old_url ) {
$this->push(
'site_url_change',
array(
'action' => 'site_url_change',
'category' => 'admin',
'data' => array(
'label_key' => 'after',
'after' => $new_url,
'before' => $old_url,
'page' => get_site_url( null, $_SERVER['REQUEST_URI'] ),
),
)
);
}
}

/**
* Login
*
* @hooked wp_login
*
* @param string $user_login username
* @param \WP_User $user logged in user info
* @param \WP_User $user logged in user info
*/
public function login( $user_login, $user ): void {
$is_admin = array_key_exists( 'administrator', $user->get_role_caps() );
if ( ( $is_admin && $user->get_role_caps()['administrator'] ) || ( $user->get_role_caps() && $user->get_role_caps()['manage_options'] ) ) {
$this->push( 'login', ["user_email" => $user->user_email]);
$this->push( 'login', array( 'user_email' => $user->user_email ) );
}
}

Expand Down

0 comments on commit c80ba7b

Please sign in to comment.