<?php
/**
 * Remove PufferPDF-owned internal data while preserving user content.
 *
 * @package PufferPDF
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

$pufferpdf_internal_post_types = array(
	'pufferpdf_record',
	'pufferpdf_manifest',
);
$pufferpdf_meta_keys           = array(
	'_pufferpdf_attachment_id',
	'_pufferpdf_content_hash',
	'_pufferpdf_converted_at_gmt',
	'_pufferpdf_created_at_gmt',
	'_pufferpdf_engine_version',
	'_pufferpdf_has_encoding_issues',
	'_pufferpdf_hash_verification',
	'_pufferpdf_manifest_id',
	'_pufferpdf_manifest_kind',
	'_pufferpdf_manifest_objects',
	'_pufferpdf_manifest_report',
	'_pufferpdf_manifest_source',
	'_pufferpdf_manifest_toc',
	'_pufferpdf_page_count',
	'_pufferpdf_page_dividers',
	'_pufferpdf_pages_needing_ocr',
	'_pufferpdf_pdf_type',
	'_pufferpdf_plan_hash',
	'_pufferpdf_plan_item_key',
	'_pufferpdf_previous_record_id',
	'_pufferpdf_profile',
	'_pufferpdf_request_id',
	'_pufferpdf_reversal_for',
	'_pufferpdf_source_bytes',
	'_pufferpdf_source_file',
	'_pufferpdf_source_hash',
	'_pufferpdf_source_mtime',
	'_pufferpdf_source_pages',
	'_pufferpdf_target_modified_gmt',
	'_pufferpdf_target_post_id',
);

/**
 * Remove PufferPDF data from the current site's tables.
 *
 * The progress guard prevents an uninstall hook or database failure from
 * trapping WordPress in an infinite loop on one undeletable internal post.
 *
 * @param string[] $internal_post_types Internal post types to remove.
 * @param string[] $meta_keys           PufferPDF post meta keys to remove.
 * @return void
 */
function pufferpdf_delete_current_site_data( $internal_post_types, $meta_keys ) {
	$pufferpdf_batches_remaining = 1000;

	do {
		$pufferpdf_internal_ids = get_posts(
			array(
				'post_type'              => $internal_post_types,
				'post_status'            => 'any',
				'posts_per_page'         => 100,
				'orderby'                => 'ID',
				'order'                  => 'ASC',
				'fields'                 => 'ids',
				'no_found_rows'          => true,
				'update_post_meta_cache' => false,
				'update_post_term_cache' => false,
			)
		);

		$pufferpdf_deleted_count = 0;
		foreach ( $pufferpdf_internal_ids as $pufferpdf_internal_id ) {
			wp_delete_post( $pufferpdf_internal_id, true );

			// A pre_delete_post filter may return a WP_Post without deleting it.
			// Count only IDs that WordPress confirms are no longer present.
			if ( null === get_post( $pufferpdf_internal_id ) ) {
				++$pufferpdf_deleted_count;
			}
		}

		--$pufferpdf_batches_remaining;
	} while (
		! empty( $pufferpdf_internal_ids )
		&& $pufferpdf_deleted_count > 0
		&& $pufferpdf_batches_remaining > 0
	);

	foreach ( $meta_keys as $pufferpdf_meta_key ) {
		delete_post_meta_by_key( $pufferpdf_meta_key );
	}
}

if ( is_multisite() ) {
	$pufferpdf_site_offset = 0;
	do {
		$pufferpdf_site_ids = get_sites(
			array(
				'fields'  => 'ids',
				'number'  => 100,
				'offset'  => $pufferpdf_site_offset,
				'orderby' => 'id',
				'order'   => 'ASC',
			)
		);

		foreach ( $pufferpdf_site_ids as $pufferpdf_site_id ) {
			switch_to_blog( $pufferpdf_site_id );
			pufferpdf_delete_current_site_data(
				$pufferpdf_internal_post_types,
				$pufferpdf_meta_keys
			);
			restore_current_blog();
		}

		$pufferpdf_site_count   = count( $pufferpdf_site_ids );
		$pufferpdf_site_offset += $pufferpdf_site_count;
	} while ( 100 === $pufferpdf_site_count );
} else {
	pufferpdf_delete_current_site_data(
		$pufferpdf_internal_post_types,
		$pufferpdf_meta_keys
	);
}
