Domains: under * each domain; also in Install code > WordPress) and the plugin only starts once SaaSAudit confirms they are the ones of * this site's own domain, and the site has proved it controls its address (see domain-check.php). * The IDs are kept only after that, so a wrong or someone else's ID is never stored. * * @package SaaSAudit_Consent */ if ( ! defined( 'ABSPATH' ) ) { exit; } const SAASAUDIT_CONNECTION_OPTION = 'saasaudit_consent_connection'; const SAASAUDIT_ACTIVATION_REDIRECT = 'saasaudit_consent_activation_redirect'; // Where "Sign up" and "Sign in" lead. Defaults for the production service; a test environment sets // SAASAUDIT_SIGNUP_URL and SAASAUDIT_SIGNIN_URL in wp-config.php, so no plugin file is ever edited. const SAASAUDIT_DEFAULT_SIGNUP_URL = 'https://www.saasaudit.ai/pricing'; const SAASAUDIT_DEFAULT_SIGNIN_URL = 'https://consent.saasaudit.ai/administration/domains'; /** A configured link: `$value` when it is a plain https address (no credentials), otherwise `$default`. */ function saasaudit_consent_valid_link( $value, $default ) { if ( ! is_string( $value ) || false === filter_var( $value, FILTER_VALIDATE_URL ) ) { return $default; } $parts = wp_parse_url( $value ); if ( ! is_array( $parts ) || ( isset( $parts['scheme'] ) ? $parts['scheme'] : '' ) !== 'https' || empty( $parts['host'] ) || isset( $parts['user'] ) || isset( $parts['pass'] ) ) { return $default; } return $value; } /** Where a visitor with no SaaSAudit account is sent to sign up and choose a plan. */ function saasaudit_consent_signup_url() { return saasaudit_consent_valid_link( defined( 'SAASAUDIT_SIGNUP_URL' ) ? SAASAUDIT_SIGNUP_URL : '', SAASAUDIT_DEFAULT_SIGNUP_URL ); } /** Where an account holder signs in to copy the Tenant ID and Site ID. */ function saasaudit_consent_signin_url() { return saasaudit_consent_valid_link( defined( 'SAASAUDIT_SIGNIN_URL' ) ? SAASAUDIT_SIGNIN_URL : '', SAASAUDIT_DEFAULT_SIGNIN_URL ); } /** The IDs this site was connected with, as the lookup sends them, or null while it is not connected. */ function saasaudit_consent_connection_ids() { $saved = get_option( SAASAUDIT_CONNECTION_OPTION, null ); if ( ! is_array( $saved ) || empty( $saved['tenant_id'] ) || empty( $saved['site_id'] ) || ! saasaudit_consent_valid_token( (string) $saved['site_id'] ) ) { return null; } return array( 'tenantId' => (int) $saved['tenant_id'], 'siteId' => (string) $saved['site_id'], ); } /** * Checks the IDs against SaaSAudit and, only when they are this site's own and the site proves it * controls its address, keeps them and the answer. Returns [ 'ok' => true ] or * [ 'ok' => false, 'code' => ..., 'detail' => ... ] for the screen to explain. */ function saasaudit_consent_connect( $tenant_id, $site_id ) { $tenant_id = trim( (string) $tenant_id ); $site_id = trim( (string) $site_id ); if ( 1 !== preg_match( '/^[0-9]{1,10}$/', $tenant_id ) || (int) $tenant_id < 1 ) { return array( 'ok' => false, 'code' => 'invalid_tenant' ); } if ( ! saasaudit_consent_valid_token( $site_id ) ) { return array( 'ok' => false, 'code' => 'invalid_site' ); } $ids = array( 'tenantId' => (int) $tenant_id, 'siteId' => $site_id, ); $state = saasaudit_consent_lookup_remote( home_url(), $ids ); if ( ! empty( $state['error'] ) ) { return array( 'ok' => false, 'code' => 'unreachable' ); } if ( empty( $state['allowed'] ) ) { return array( 'ok' => false, 'code' => isset( $state['reason'] ) ? (string) $state['reason'] : 'not_registered', 'detail' => isset( $state['detail'] ) ? (string) $state['detail'] : '', ); } update_option( SAASAUDIT_CONNECTION_OPTION, array( 'tenant_id' => $ids['tenantId'], 'site_id' => $ids['siteId'], ), false ); $state['host'] = saasaudit_consent_site_host(); $state['checked'] = time(); update_option( SAASAUDIT_CONSENT_SITE_OPTION, $state, false ); // Always: pages cached before the connection carry no banner, whatever the earlier answer said. saasaudit_consent_purge_page_caches(); return array( 'ok' => true ); } /** Forgets the IDs and the answer: the plugin stops adding anything to the site. */ function saasaudit_consent_disconnect() { delete_option( SAASAUDIT_CONNECTION_OPTION ); delete_option( SAASAUDIT_CONSENT_SITE_OPTION ); saasaudit_consent_unschedule_daily_refresh(); saasaudit_consent_purge_page_caches(); } /** Where the outcome of the last "Connect" is kept for the page it redirects to. */ function saasaudit_consent_connect_notice_key() { return 'saasaudit_consent_connect_notice_' . get_current_user_id(); } /** The "Connect" button. */ function saasaudit_consent_handle_connect() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'saasaudit' ), 403 ); } check_admin_referer( 'saasaudit_consent_connect' ); $tenant_id = isset( $_POST['saasaudit_tenant_id'] ) ? sanitize_text_field( wp_unslash( $_POST['saasaudit_tenant_id'] ) ) : ''; $site_id = isset( $_POST['saasaudit_site_id'] ) ? sanitize_text_field( wp_unslash( $_POST['saasaudit_site_id'] ) ) : ''; set_transient( saasaudit_consent_connect_notice_key(), saasaudit_consent_connect( $tenant_id, $site_id ), 120 ); wp_safe_redirect( admin_url( 'options-general.php?page=saasaudit' ) ); exit; } add_action( 'admin_post_saasaudit_consent_connect', 'saasaudit_consent_handle_connect' ); /** The "Disconnect" button. */ function saasaudit_consent_handle_disconnect() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'saasaudit' ), 403 ); } check_admin_referer( 'saasaudit_consent_disconnect' ); saasaudit_consent_disconnect(); wp_safe_redirect( admin_url( 'options-general.php?page=saasaudit' ) ); exit; } add_action( 'admin_post_saasaudit_consent_disconnect', 'saasaudit_consent_handle_disconnect' ); /** Why a connection attempt failed, in words, and what to do. */ function saasaudit_consent_connect_error_text( $code, $detail = '' ) { $host = saasaudit_consent_site_host(); switch ( $code ) { case 'invalid_tenant': return __( 'The Tenant ID is a number. Copy it from your SaaSAudit console (Administration > Domains: it is shown under your domain).', 'saasaudit' ); case 'invalid_site': return __( 'The Site ID does not look right. Copy it again from your SaaSAudit console (Administration > Domains: it is shown under your domain), without spaces.', 'saasaudit' ); case 'id_mismatch': /* translators: %s: this site's hostname */ return sprintf( __( 'These IDs are not the ones of this site. Copy both from the domain that matches this site (%s) in your SaaSAudit console, under Install code > WordPress.', 'saasaudit' ), $host ); case 'ambiguous': return __( 'This domain is also configured in another SaaSAudit account, and neither has been confirmed yet. To confirm yours, copy the install snippet from your SaaSAudit console (Administration > Domains > Install code > Manual) into your site once, then connect again.', 'saasaudit' ); case 'not_verified': return saasaudit_consent_not_verified_text( $detail ); case 'unreachable': return __( 'Could not reach SaaSAudit. Try again in a moment.', 'saasaudit' ); default: /* translators: %s: this site's hostname */ return sprintf( __( '%s is not a domain configured in your SaaSAudit console. Add it under Administration > Domains, then connect again.', 'saasaudit' ), $host ); } } /** The Connect form, shown until the site is connected; or what it is connected to, with Disconnect. */ function saasaudit_consent_render_connection_panel() { $result = get_transient( saasaudit_consent_connect_notice_key() ); if ( false !== $result ) { delete_transient( saasaudit_consent_connect_notice_key() ); } if ( is_array( $result ) && empty( $result['ok'] ) ) { echo '

' . esc_html( saasaudit_consent_connect_error_text( isset( $result['code'] ) ? $result['code'] : '', isset( $result['detail'] ) ? $result['detail'] : '' ) ) . '

'; } elseif ( is_array( $result ) ) { echo '

' . esc_html__( 'Connected. The consent banner is now on your site.', 'saasaudit' ) . '

'; } if ( null !== saasaudit_consent_connection_ids() ) { echo '
'; echo ''; wp_nonce_field( 'saasaudit_consent_disconnect' ); submit_button( __( 'Disconnect', 'saasaudit' ), 'secondary', 'submit', false ); echo '
'; return; } echo '

' . esc_html__( 'Connect your SaaSAudit account', 'saasaudit' ) . '

'; echo '

' . esc_html__( 'The plugin does nothing on your site until you connect it. Enter the two IDs shown in your SaaSAudit console for this site; SaaSAudit checks that they belong to this site before anything is added.', 'saasaudit' ) . '

'; echo '
'; echo ''; wp_nonce_field( 'saasaudit_consent_connect' ); echo ''; echo ''; echo ''; echo ''; submit_button( __( 'Connect', 'saasaudit' ), 'primary', 'submit', false ); echo '
'; echo '

' . esc_html__( 'Where do I find these IDs?', 'saasaudit' ) . ' ' . esc_html__( 'Open your SaaSAudit console, go to Administration > Domains > Install code > WordPress.', 'saasaudit' ) . '

'; echo '

' . esc_html__( 'No SaaSAudit account yet?', 'saasaudit' ) . ' ' . esc_html__( 'Sign up and choose a plan', 'saasaudit' ) . ', ' . esc_html__( 'add this site as a domain, then come back here to connect.', 'saasaudit' ) . '

'; } /** Until the site is connected, one notice, only on the Dashboard and Plugins screens (not on every admin page). */ function saasaudit_consent_connect_admin_notice() { if ( null !== saasaudit_consent_connection_ids() || ! current_user_can( 'manage_options' ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || ! in_array( $screen->id, array( 'dashboard', 'plugins' ), true ) ) { return; } echo '

' . esc_html__( 'SaaSAudit is not connected yet, so it does nothing on your site.', 'saasaudit' ) . ' ' . esc_html__( 'Finish setup', 'saasaudit' ) . '

'; } add_action( 'admin_notices', 'saasaudit_consent_connect_admin_notice' ); /** On activation: take the owner to the Connect screen (a site already connected is just checked). */ function saasaudit_consent_on_activation() { if ( function_exists( 'saasaudit_consent_enable_auto_updates' ) ) { saasaudit_consent_enable_auto_updates(); } if ( null === saasaudit_consent_connection_ids() ) { set_transient( SAASAUDIT_ACTIVATION_REDIRECT, 1, 60 ); return; } saasaudit_consent_check_site(); } /** The redirect itself, on the next admin page load, never for several plugins activated at once or in AJAX. */ function saasaudit_consent_redirect_after_activation() { if ( false === get_transient( SAASAUDIT_ACTIVATION_REDIRECT ) ) { return; } delete_transient( SAASAUDIT_ACTIVATION_REDIRECT ); if ( wp_doing_ajax() || is_network_admin() || ! current_user_can( 'manage_options' ) || null !== filter_input( INPUT_GET, 'activate-multi' ) ) { return; } wp_safe_redirect( admin_url( 'options-general.php?page=saasaudit' ) ); exit; } add_action( 'admin_init', 'saasaudit_consent_redirect_after_activation' ); /** An earlier build saved script and API addresses and extra cookies from a settings form that is gone: drop what it left. */ function saasaudit_consent_drop_legacy_settings() { if ( false !== get_option( SAASAUDIT_CONSENT_OPTION, false ) ) { delete_option( SAASAUDIT_CONSENT_OPTION ); } } add_action( 'admin_init', 'saasaudit_consent_drop_legacy_settings' );