<?php
/**
 * Uninstall script for BW Guides.
 *
 * Removes plugin settings, the sync cron, and hub-delivered guides (Bowden
 * Works redistributable content). Guides the client created themselves — and
 * all guide tags — are deliberately KEPT: deleting a client's own writing on
 * uninstall would be hostile.
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

// Remove the capabilities this plugin added to roles, so uninstalling leaves
// nothing behind. Safe to run even if they were never granted.
require_once __DIR__ . '/includes/class-bw-guides-caps.php';
if ( class_exists( 'BW_Guides_Caps' ) && function_exists( 'wp_roles' ) ) {
	BW_Guides_Caps::remove_all();
}

// Delete plugin options.
delete_option( 'bw_guides_settings' );
delete_option( 'bw_guides_version' );

// Delete site meta on multisite.
if ( is_multisite() ) {
	delete_site_option( 'bw_guides_settings' );
}

// Clear the sync cron.
wp_clear_scheduled_hook( 'bw_guides_sync_event' );

// Delete hub-sourced guides (their notes meta goes with them).
$bw_guides_hub_ids = get_posts(
	array(
		'post_type'      => 'bw_guide',
		'post_status'    => array( 'publish', 'draft', 'pending', 'private', 'future', 'trash' ),
		'numberposts'    => -1,
		'fields'         => 'ids',
		'meta_key'       => '_bw_guides_source',
		'meta_value'     => 'hub',
	)
);
foreach ( $bw_guides_hub_ids as $bw_guides_hub_id ) {
	wp_delete_post( $bw_guides_hub_id, true );
}
