Skip to content

Commit

Permalink
fix: add helper method to handle connected admin option
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Oct 15, 2024
1 parent 023544e commit fc6877b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions includes/class-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public static function error( $payload, $header = 'NEWSPACK' ) {
* @param string $code The log code (i.e. newspack_google_login).
* @param string $message The message to log.
* @param array $data The data to log.
* Optional. Additional parameters.
* @type string $user_email The current users email address.
* @type file $file The name of the file to write the local log to.
* @param string $type The type of log. Defaults to 'error'.
*/
public static function newspack_log( $code, $message, $data, $type = 'error' ) {
Expand Down
19 changes: 15 additions & 4 deletions includes/plugins/google-site-kit/class-googlesitekit.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function init() {
add_action( 'wp_footer', [ __CLASS__, 'insert_ga4_analytics' ] );
add_filter( 'option_googlesitekit_analytics_settings', [ __CLASS__, 'filter_ga_settings' ] );
add_filter( 'googlesitekit_gtag_opt', [ __CLASS__, 'add_ga_custom_parameters' ] );
add_action( 'delete_option_' . Has_Connected_Admins::OPTION, [ __CLASS__, 'maybe_log_disconnect' ] );
add_action( 'delete_option_' . self::get_sitekit_ga4_has_connected_admin_option_name(), [ __CLASS__, 'log_disconnect' ] );
}

/**
Expand Down Expand Up @@ -98,6 +98,16 @@ private static function get_sitekit_ga4_settings_option_name() {
return false;
}

/**
* Get the name of the option under which Site Kit's GA4 has connected admin flag is stored.
*/
private static function get_sitekit_ga4_has_connected_admin_option_name() {
if ( class_exists( 'Google\Site_Kit\Core\Authentication\Has_Connected_Admins' ) ) {
return Has_Connected_Admin::OPTION;
}
return 'googlesitekit_has_connected_admins';
}

/**
* Get Site Kit's GA4 settings.
*/
Expand Down Expand Up @@ -178,14 +188,15 @@ public static function add_ga_custom_parameters( $gtag_opt ) {
*
* @param string $option Option being deleted.
*/
public static function maybe_log_disconnect( $option ) {
public static function log_disconnect( $option ) {
$code = 'newspack_googlesitekit_disconnect';
$message = 'Google Site Kit has been disconnected.';
// TODO: Determine what data needs to be logged.
$data = [
'user_email' => wp_get_current_user()->user_email,
'file' => 'class-googlesitekit.php',
'file' => $code,
];
Logger::newspack_log( 'newspack_googlesitekit_disconnect', $message, $data );
Logger::newspack_log( $code, $message, $data );
}
}
GoogleSiteKit::init();

0 comments on commit fc6877b

Please sign in to comment.