' . esc_html__( 'Two copies of Advocate MCP Structured Data are installed, in the plugin folders "advocate-agent" and "advocatemcp-structured-data": deactivate and delete the one that is not active — your settings carry over, because both copies store them under the same option names.', 'advocatemcp-structured-data' ) . '
';
} );
// ---------------------------------------------------------------------------
// Activation / deactivation
// ---------------------------------------------------------------------------
register_activation_hook( __FILE__, function ( $network_wide = false ) {
if ( $network_wide ) {
wp_die( esc_html__( 'Activate Advocate separately on each site. Network activation cannot configure one business for every site.', 'advocatemcp-structured-data' ) );
}
// Both copies must never be recorded active (see advocate_other_copy_active); refuse before any write.
if ( advocate_other_copy_active() ) {
wp_die( esc_html__( 'Another copy of Advocate MCP Structured Data is already active, in the other plugin folder ("advocate-agent" or "advocatemcp-structured-data"). Deactivate the other copy, activate this one, and only then delete the other copy; your settings carry over.', 'advocatemcp-structured-data' ) );
}
// A new generation fences workers admitted before deactivation/reactivation.
$generation = wp_generate_uuid4();
update_option( ADVOCATE_LIFECYCLE_OPTION, $generation, 'no' );
if ( get_option( ADVOCATE_LIFECYCLE_OPTION ) !== $generation ) {
wp_die( esc_html__( 'Advocate could not save its activation state. Resolve the WordPress database error and activate again.', 'advocatemcp-structured-data' ) );
}
advocate_import_packaged_setup();
if ( ! wp_next_scheduled( ADVOCATE_CRON_HOOK ) ) {
wp_schedule_event( time() + 60, 'hourly', ADVOCATE_CRON_HOOK );
}
} );
register_deactivation_hook( __FILE__, function ( $network_wide = false ) {
if ( ! $network_wide ) { advocate_deactivate_site(); return; }
$ids = get_sites( [ 'fields' => 'ids', 'number' => 101, 'network_id' => get_current_network_id(), 'orderby' => 'id', 'order' => 'ASC' ] );
if ( count( $ids ) > 100 ) {
wp_die( esc_html__( 'Automatic Advocate network cleanup supports at most 100 sites. No sites were cleaned. Ask your network administrator to follow the per-site WP-CLI cleanup instructions in the plugin readme before removing the plugin files.', 'advocatemcp-structured-data' ) );
}
foreach ( $ids as $id ) {
switch_to_blog( $id );
try { advocate_deactivate_site(); }
finally { restore_current_blog(); }
}
} );
/** Keep intentional configuration, but remove publishable bytes and old claims. */
function advocate_deactivate_site() {
update_option( ADVOCATE_LIFECYCLE_OPTION, 'inactive', 'no' );
if ( 'inactive' !== get_option( ADVOCATE_LIFECYCLE_OPTION ) ) {
wp_die( esc_html__( 'Advocate could not save its inactive state. Resolve the WordPress database error and retry cleanup.', 'advocatemcp-structured-data' ) );
}
$cleared = wp_clear_scheduled_hook( ADVOCATE_CRON_HOOK );
if ( false === $cleared || is_wp_error( $cleared ) ) {
wp_die( esc_html__( 'Advocate could not clear its scheduled tasks. Resolve the WordPress scheduling error and retry cleanup.', 'advocatemcp-structured-data' ) );
}
delete_option( ADVOCATE_CONNECT_OPTION );
if ( false !== get_option( ADVOCATE_CONNECT_OPTION, false ) ) {
wp_die( esc_html__( 'Advocate could not clear its pending connection. Publishing remains inactive. Resolve the WordPress database error and retry cleanup.', 'advocatemcp-structured-data' ) );
}
$slug = advocate_business_slug();
if ( '' === $slug ) { return; }
delete_option( advocate_payload_key( $slug ) );
delete_option( advocate_lock_key( $slug ) );
delete_option( advocate_status_key( $slug ) );
delete_transient( advocate_fresh_key( $slug ) );
delete_transient( 'advocate_jsonld_' . $slug );
$missing = new stdClass();
foreach ( [ advocate_payload_key( $slug ), advocate_lock_key( $slug ), advocate_status_key( $slug ) ] as $key ) {
if ( get_option( $key, $missing ) !== $missing ) {
wp_die( esc_html__( 'Advocate could not remove its cached profile. Publishing remains inactive. Resolve the WordPress database error and retry cleanup.', 'advocatemcp-structured-data' ) );
}
}
if ( false !== get_transient( advocate_fresh_key( $slug ) ) || false !== get_transient( 'advocate_jsonld_' . $slug ) ) {
wp_die( esc_html__( 'Advocate could not remove its cached profile. Publishing remains inactive. Resolve the WordPress cache error and retry cleanup.', 'advocatemcp-structured-data' ) );
}
}
require_once __DIR__ . '/connect.php';
// Replacing an already-active plugin ZIP does not run its activation hook.
// Import on the next administrator request too, never on a visitor's request.
add_action( 'admin_init', 'advocate_import_packaged_setup', 5 );
// ---------------------------------------------------------------------------
// Optional setup included in an owner-specific download
// ---------------------------------------------------------------------------
/**
* Reduce an HTTP(S) website URL to the identity of a WordPress installation.
*
* WordPress's public home URL can differ from site_url(), where its core files
* live. Compare home_url() and retain the public path so a multisite sibling
* under /another-business does not receive this profile. www and HTTPS upgrades
* are normal aliases; different subdomains and non-default ports are not.
*/
function advocate_setup_website_identity( $website ) {
if ( ! is_string( $website ) || strlen( $website ) > 2048 ) {
return null;
}
$parts = wp_parse_url( $website );
if ( ! is_array( $parts ) || ! isset( $parts['scheme'], $parts['host'] )
|| ! in_array( strtolower( $parts['scheme'] ), [ 'http', 'https' ], true )
|| isset( $parts['user'] ) || isset( $parts['pass'] )
|| isset( $parts['query'] ) || isset( $parts['fragment'] ) ) {
return null;
}
$host = advocate_normalize_host( $parts['host'] );
if ( ! preg_match( '/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i', $host ) ) {
return null;
}
$port = $parts['port'] ?? null;
if ( ( 'http' === strtolower( $parts['scheme'] ) && 80 === $port )
|| ( 'https' === strtolower( $parts['scheme'] ) && 443 === $port ) ) {
$port = null;
}
return [
'host' => $host,
'port' => $port,
'path' => rtrim( $parts['path'] ?? '', '/' ),
];
}
/** Validate data, not executable PHP. The archive never carries credentials. */
function advocate_parse_packaged_setup( $raw ) {
if ( ! is_string( $raw ) || strlen( $raw ) > 4096 ) {
return null;
}
$config = json_decode( $raw, true );
if ( ! is_array( $config ) || 1 !== ( $config['version'] ?? null )
|| ! isset( $config['businessSlug'] ) || ! is_string( $config['businessSlug'] )
|| ! preg_match( '/^[A-Za-z0-9][A-Za-z0-9_-]{0,99}$/D', $config['businessSlug'] )
|| null === advocate_setup_website_identity( $config['website'] ?? null ) ) {
return null;
}
return $config;
}
/**
* One-time import. Existing choices always win, including a disabled checkbox.
*
* The marker is deliberately independent of the packaged bytes: clearing or
* changing the Business ID must never make an old download configure it again,
* even after a plugin update. Deletion removes both this state and the plugin's
* files; deliberately installing a newly downloaded package can start afresh.
* Invalid/wrong-site packages remain retryable after the owner fixes the source
* website and downloads again. No profile request runs within this function.
*
* The optional file path is an internal testing seam, never a request value.
*/
function advocate_import_packaged_setup( $file = null ) {
if ( ! current_user_can( 'manage_options' ) || get_option( ADVOCATE_SETUP_IMPORT_OPTION, false ) ) {
return;
}
// WordPress passes an empty string to no-argument action callbacks.
$file = is_string( $file ) && '' !== $file ? $file : __DIR__ . '/advocate-setup.json';
if ( ! file_exists( $file ) ) {
// The generic download intentionally retains the manual settings flow.
delete_option( ADVOCATE_SETUP_NOTICE_OPTION );
return;
}
$raw = is_file( $file ) && is_readable( $file ) ? file_get_contents( $file, false, null, 0, 4097 ) : false;
$config = advocate_parse_packaged_setup( $raw );
if ( null === $config ) {
update_option( ADVOCATE_SETUP_NOTICE_OPTION, 'invalid', 'no' );
return;
}
if ( advocate_setup_website_identity( $config['website'] ) !== advocate_setup_website_identity( home_url() ) ) {
update_option( ADVOCATE_SETUP_NOTICE_OPTION, 'website_mismatch', 'no' );
return;
}
// Also acts as an atomic claim if activation and admin_init overlap.
if ( ! add_option( ADVOCATE_SETUP_IMPORT_OPTION, 'processed', '', 'no' ) ) {
return;
}
$settings = get_option( ADVOCATE_OPTION, [] );
$has_business = is_array( $settings ) && array_key_exists( 'agent_id', $settings )
&& ( ! is_string( $settings['agent_id'] ) || '' !== trim( $settings['agent_id'] ) );
if ( ! is_array( $settings ) || $has_business
|| ( array_key_exists( 'inject_jsonld', $settings ) && empty( $settings['inject_jsonld'] ) ) ) {
$notice = is_array( $settings ) && ( $settings['agent_id'] ?? '' ) === $config['businessSlug']
&& advocate_jsonld_enabled() ? 'already_configured' : 'settings_preserved';
update_option( ADVOCATE_SETUP_NOTICE_OPTION, $notice, 'no' );
return;
}
$settings['agent_id'] = $config['businessSlug'];
$settings['inject_jsonld'] = true;
// Normalised to what the settings sanitizer stores, so the read-back below
// compares like with like whether or not the sanitizer runs on this save.
$settings['disable_webmcp'] = ! empty( $settings['disable_webmcp'] );
$GLOBALS['advocate_importing_packaged_setup'] = true;
try {
update_option( ADVOCATE_OPTION, $settings );
} finally {
unset( $GLOBALS['advocate_importing_packaged_setup'] );
}
if ( get_option( ADVOCATE_OPTION, [] ) !== $settings ) {
delete_option( ADVOCATE_SETUP_IMPORT_OPTION );
update_option( ADVOCATE_SETUP_NOTICE_OPTION, 'save_failed', 'no' );
return;
}
update_option( ADVOCATE_SETUP_NOTICE_OPTION, 'configured', 'no' );
advocate_schedule_background_refresh();
}
add_action( 'admin_notices', function () {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$screen = get_current_screen();
if ( ! $screen || ! in_array( $screen->id, [ 'plugins', 'settings_page_' . ADVOCATE_SETTINGS_PAGE ], true ) ) {
return;
}
$notice = get_option( ADVOCATE_SETUP_NOTICE_OPTION, '' );
$messages = [
'configured' => __( 'Advocate is configured for your business. Return to Advocate and select Check connection.', 'advocatemcp-structured-data' ),
'already_configured' => __( 'This business is already configured. Return to Advocate and select Check connection.', 'advocatemcp-structured-data' ),
'settings_preserved' => __( 'Your existing Advocate settings were kept. Open Settings > Advocate to review the business and whether publishing is enabled.', 'advocatemcp-structured-data' ),
'invalid' => __( 'This Advocate download could not be configured automatically. Download the plugin again from Advocate, or open Settings > Advocate to connect manually.', 'advocatemcp-structured-data' ),
'website_mismatch' => __( 'This Advocate download was prepared for a different website. Check your website in Advocate and download again, or open Settings > Advocate to connect manually.', 'advocatemcp-structured-data' ),
'save_failed' => __( 'Advocate could not save the setup. Reload this page to retry, or open Settings > Advocate to connect manually.', 'advocatemcp-structured-data' ),
];
if ( ! isset( $messages[ $notice ] ) ) {
return;
}
$level = in_array( $notice, [ 'configured', 'already_configured' ], true ) ? 'success' : 'warning';
echo '