Last active
July 29, 2020 20:54
-
-
Save adamsilverstein/24ccb8be183df8d1fe1cdb3b53f610fd to your computer and use it in GitHub Desktop.
Enabling Analytics Step Mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Enable the 'analytics-step' setup mode. | |
* | |
* @copyright 2020 Google LLC | |
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 | |
* @link https://sitekit.withgoogle.com | |
* | |
* @wordpress-plugin | |
* Plugin Name: Analytics Step Setup | |
* Plugin URI: https://sitekit.withgoogle.com | |
* Description: Enable the 'analytics-step' setup mode. | |
* Version: 1.0.0 | |
* Author: Google | |
* Author URI: https://opensource.google.com | |
* License: Apache License 2.0 | |
* License URI: https://www.apache.org/licenses/LICENSE-2.0 | |
*/ | |
// Redirect to the wizard after completing setup. | |
define( 'WIZARD_REDIRECT_COMPLETE_KEY', 'wizard_redirect_completed' ); | |
// Add the mode query variable to the setup url. | |
// Note: don't add this filter/parameter once setup is complete. | |
if ( ! get_option( WIZARD_REDIRECT_COMPLETE_KEY ) ) { | |
add_filter( 'googlesitekit_proxy_setup_url_params', function( $query_params ) { | |
$query_params[ 'mode' ] = 'analytics-step'; | |
return $query_params; | |
} ); | |
} | |
// Ensure the Analytics module is enabled by default. | |
add_filter( 'default_option_googlesitekit_active_modules', function() { | |
return array( 'analytics' ); | |
} ); | |
// Ensure the setup return redirect goes to Analytics. | |
add_filter( 'default_option_googlesitekit_redirect_url', function() { | |
if ( is_multisite() ) { | |
$base_url = network_admin_url( 'admin.php' ); | |
} else { | |
$base_url = admin_url( 'admin.php' ); | |
} | |
return add_query_arg( | |
array( 'page' => 'googlesitekit-module-analytics' ), | |
$base_url | |
); | |
} ); | |
// Redirect the user back to the wizard the first time they | |
add_action( 'admin_init', function() { | |
global $pagenow; | |
if ( | |
'admin.php' === $pagenow && | |
isset( $_GET[ 'page' ] ) && | |
'googlesitekit-dashboard' === $_GET[ 'page' ] && | |
! get_option( WIZARD_REDIRECT_COMPLETE_KEY ) | |
) { | |
// Only perform the redirect once. | |
update_option( WIZARD_REDIRECT_COMPLETE_KEY, true ); | |
//optionally this could be set when returning to the WordPress site after | |
// The suer completes the wizard? | |
wp_redirect( 'https://wizardurl.com' ); | |
// Note if wizard URL is dynamic, suggest using `wp_safe_redirect` and | |
// adding wizardurl.com to an allowed-list. | |
} | |
} ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment