<?php

/**
 * Plugin Builder data cleanup when LolaCore is uninstalled.
 *
 * @package LolaCore\PluginBuilder
 * @license GPL-2.0-or-later
 */

declare(strict_types=1);

namespace LolaCore\PluginBuilder;

use LolaCore\PluginBuilder\PostType\PluginPostType;

if (! defined('ABSPATH')) {
	exit;
}

/**
 * Removes built-plugin library entries (CPT posts, meta, and revisions).
 *
 * Does NOT delete plugins installed under wp-content/plugins/{slug}/ — those belong to the site owner.
 */
class PluginBuilderUninstall {

	/**
	 * Delete all lolacore_plugin posts and their metadata.
	 */
	public static function deleteLibrary(): void {
		$posts = get_posts(
			array(
				'post_type'      => PluginPostType::POST_TYPE,
				'post_status'    => 'any',
				'posts_per_page' => -1,
				'fields'         => 'ids',
			)
		);

		if (! is_array($posts)) {
			return;
		}

		foreach ($posts as $post_id) {
			wp_delete_post((int) $post_id, true);
		}
	}
}
