<?php
/**
 * Plugin Name: JJ's Post and Product Scheduler – Record Your Changes
 * Plugin URI:  https://jjwp.net/
 * Description: Record updates to posts, pages, and WooCommerce products, choose when they go live, and keep a clear history with rollback.
 * Version:     1.1.0
 * Requires at least: 6.8
 * Requires PHP: 7.4
 * Author:      JJWP
 * License:     GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: jjs-post-scheduler
 * Domain Path: /languages
 *
 * @package ScheduledChanges
 */

defined( 'ABSPATH' ) || exit;

if ( defined( 'SCHEDULED_CHANGES_VERSION' ) ) {
	return;
}

define( 'SCHEDULED_CHANGES_VERSION', '1.1.0' );
define( 'SCHEDULED_CHANGES_SCHEMA_VERSION', '5' );
define( 'SCHEDULED_CHANGES_PLUGIN_FILE', __FILE__ );
define( 'SCHEDULED_CHANGES_PLUGIN_BASENAME', plugin_basename( SCHEDULED_CHANGES_PLUGIN_FILE ) );
define( 'SCHEDULED_CHANGES_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'SCHEDULED_CHANGES_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

if ( ! function_exists( 'scheduled_changes_register_bootstrap_status' ) ) {
	/**
	 * Registers an operational status row before the autoloader is available.
	 *
	 * @param callable():array<int,string> $messages Callback that returns messages to render.
	 */
	function scheduled_changes_register_bootstrap_status( callable $messages ): void {
		add_action(
			'after_plugin_row_' . SCHEDULED_CHANGES_PLUGIN_BASENAME,
			static function () use ( $messages ): void {
				if ( ! current_user_can( 'activate_plugins' ) ) {
					return;
				}

				foreach ( $messages() as $message ) {
					printf(
						'<tr class="plugin-update-tr active" data-plugin="%1$s"><td colspan="4" class="plugin-update colspanchange"><div class="notice inline notice-error notice-alt"><p>%2$s</p></div></td></tr>',
						esc_attr( SCHEDULED_CHANGES_PLUGIN_BASENAME ),
						esc_html( (string) $message )
					);
				}
			}
		);
	}
}

if ( ! function_exists( 'scheduled_changes_bootstrap_exception_status' ) ) {
	/**
	 * Formats a bounded bootstrap dependency diagnostic.
	 */
	function scheduled_changes_bootstrap_exception_status( Throwable $exception ): string {
		$message = trim( wp_strip_all_tags( $exception->getMessage() ) );
		$message = str_replace( SCHEDULED_CHANGES_PLUGIN_DIR, '', $message );

		if ( '' === $message ) {
			$message = 'unexpected dependency loading failure';
		}

		if ( strlen( $message ) > 180 ) {
			$message = substr( $message, 0, 177 ) . '...';
		}

		return sprintf(
			/* translators: %s: dependency loading diagnostic. */
			__( "JJ's Post and Product Scheduler cannot start because Action Scheduler could not be loaded: %s", 'jjs-post-scheduler' ),
			$message
		);
	}
}

$scheduled_changes_autoloader = SCHEDULED_CHANGES_PLUGIN_DIR . 'vendor/autoload.php';

if ( is_readable( $scheduled_changes_autoloader ) ) {
	require_once $scheduled_changes_autoloader;
} else {
	scheduled_changes_register_bootstrap_status(
		static function (): array {
			return array(
				__(
					"JJ's Post and Product Scheduler cannot start because Composer dependencies are missing. Install the release package or run composer install.",
					'jjs-post-scheduler'
				),
			);
		}
	);

	return;
}

if ( ! defined( 'SCHEDULED_CHANGES_PACKAGED_MODULE_PROFILE' ) ) {
	define( 'SCHEDULED_CHANGES_PACKAGED_MODULE_PROFILE', 'lite' );
}

$scheduled_changes_action_scheduler = SCHEDULED_CHANGES_PLUGIN_DIR . 'vendor/woocommerce/action-scheduler/action-scheduler.php';

if ( is_readable( $scheduled_changes_action_scheduler ) ) {
	try {
		require_once $scheduled_changes_action_scheduler;
	} catch ( Throwable $exception ) {
		scheduled_changes_register_bootstrap_status(
			static function () use ( $exception ): array {
				return array( scheduled_changes_bootstrap_exception_status( $exception ) );
			}
		);

		return;
	}
} else {
	scheduled_changes_register_bootstrap_status(
		static function (): array {
			return array(
				__(
					"JJ's Post and Product Scheduler cannot start because Action Scheduler is missing from the release package. Install the release package or reinstall the plugin.",
					'jjs-post-scheduler'
				),
			);
		}
	);

	return;
}

$scheduled_changes_woocommerce_bundle = SCHEDULED_CHANGES_PLUGIN_DIR . 'extensions/scheduled-changes-for-woocommerce/scheduled-changes-for-woocommerce.php';

if ( is_readable( $scheduled_changes_woocommerce_bundle ) ) {
	if ( ! defined( 'SCHEDULED_CHANGES_WC_BUNDLED' ) ) {
		define( 'SCHEDULED_CHANGES_WC_BUNDLED', true );
	}
	require_once $scheduled_changes_woocommerce_bundle;
}

register_activation_hook(
	SCHEDULED_CHANGES_PLUGIN_FILE,
	array( ScheduledChanges\Bootstrap\Activator::class, 'activate' )
);

register_deactivation_hook(
	SCHEDULED_CHANGES_PLUGIN_FILE,
	array( ScheduledChanges\Bootstrap\Deactivator::class, 'deactivate' )
);

add_action(
	'plugins_loaded',
	static function (): void {
		ScheduledChanges\Bootstrap\Plugin::instance()->boot();
	},
	20
);
