<?php
/**
 * Core Version Switcher by Volade
 *
 * @package Core_Version_Switcher_By_Volade
 */
/*
Plugin Name: Core Version Switcher by Volade
Plugin URI: https://volade.com/cms/wordpress/extensions/core-version-switcher-by-volade/
Description: Pin WordPress to a specific core version — upgrade or downgrade via the native Updates screen. Fork of the abandoned WP Downgrade plugin with PHP 8.x hardening.
Version: 1.0.0
Requires at least: 6.0
Requires PHP: 7.4
Author: Volade
Author URI: https://volade.com
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: core-version-switcher-by-volade
Domain Path: /languages
 */

defined( 'ABSPATH' ) || exit;

define( 'CVS_VERSION', '1.0.0' );
define( 'CVS_FILE', __FILE__ );
define( 'CVS_DIR', plugin_dir_path( __FILE__ ) );
define( 'CVS_URL', plugin_dir_url( __FILE__ ) );
define( 'CVS_SLUG', 'core-version-switcher-by-volade' );

require_once CVS_DIR . 'includes/class-cvs-settings.php';
require_once CVS_DIR . 'includes/class-cvs-i18n.php';
require_once CVS_DIR . 'includes/class-cvs-releases.php';
require_once CVS_DIR . 'includes/class-cvs-presets.php';
require_once CVS_DIR . 'includes/class-cvs-diagnostics.php';
require_once CVS_DIR . 'includes/class-cvs-compat.php';
require_once CVS_DIR . 'includes/class-cvs-checksums.php';
require_once CVS_DIR . 'includes/class-cvs-preflight.php';
require_once CVS_DIR . 'includes/class-cvs-email.php';
require_once CVS_DIR . 'includes/class-cvs-migration.php';
require_once CVS_DIR . 'includes/class-cvs-core-switch.php';
require_once CVS_DIR . 'includes/class-cvs-site-health.php';
require_once CVS_DIR . 'includes/class-cvs-dashboard.php';
require_once CVS_DIR . 'includes/class-cvs-admin.php';
require_once CVS_DIR . 'includes/class-cvs-network-admin.php';
require_once CVS_DIR . 'includes/class-cvs-plugin.php';

if ( defined( 'WP_CLI' ) && WP_CLI ) {
	require_once CVS_DIR . 'includes/class-cvs-cli.php';
}

function cvs_load_volade_sdk() {
	$args = array(
		'plugin_file' => CVS_FILE,
		'plugin_slug' => CVS_SLUG,
		'plugin_name' => 'Core Version Switcher by Volade',
		'sdk_path'    => CVS_DIR . 'volade-sdk/class-volade-sdk.php',
	);

	if ( function_exists( 'volade_sdk_boot' ) ) {
		volade_sdk_boot( $args );
		return;
	}

	$boot = CVS_DIR . 'volade-sdk/boot.php';
	if ( ! is_readable( $boot ) ) {
		add_action(
			'admin_notices',
			function () {
				if ( ! current_user_can( 'update_core' ) ) {
					return;
				}
				echo '<div class="notice notice-error"><p>';
				echo '<strong>' . esc_html__( 'Core Version Switcher by Volade', 'core-version-switcher-by-volade' ) . ':</strong> ';
				esc_html_e( 'The volade-sdk folder is missing. Version settings remain under Settings → Core Version.', 'core-version-switcher-by-volade' );
				echo ' ';
				esc_html_e( 'Reinstall the complete plugin package — the volade-sdk folder must be included.', 'core-version-switcher-by-volade' );
				echo '</p></div>';
			}
		);
		return;
	}

	require_once $boot;

	volade_sdk_boot( $args );
}

cvs_load_volade_sdk();

CVS_Plugin::instance();

add_filter(
	'plugin_row_meta',
	function ( $links, $file ) {
		if ( plugin_basename( CVS_FILE ) !== $file ) {
			return $links;
		}
		$links[] = '<a href="https://wordpress.org/support/plugin/core-version-switcher-by-volade/" target="_blank" rel="noopener">' . esc_html__( 'Support', 'core-version-switcher-by-volade' ) . '</a>';
		$doc_url = function_exists( 'volade_extension_doc_url' ) ? volade_extension_doc_url( CVS_SLUG ) : 'https://volade.com/fr/cms/wordpress/extensions/' . CVS_SLUG;
		$links[] = '<a href="' . esc_url( $doc_url ) . '" target="_blank" rel="noopener">' . esc_html__( 'Documentation', 'core-version-switcher-by-volade' ) . '</a>';
		return $links;
	},
	10,
	2
);

register_activation_hook( __FILE__, array( 'CVS_Plugin', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'CVS_Plugin', 'deactivate' ) );

if ( is_multisite() ) {
	add_action( 'wp_initialize_site', 'cvs_init_new_site', 20, 1 );
}

/**
 * Nouveau site multisite : hérite du réseau par défaut.
 *
 * @param WP_Site $new_site Site créé.
 */
function cvs_init_new_site( $new_site ) {
	if ( ! $new_site instanceof WP_Site ) {
		return;
	}
	switch_to_blog( $new_site->blog_id );
	if ( false === get_option( CVS_Settings::SOURCE_OPTION_KEY, false ) ) {
		update_option( CVS_Settings::SOURCE_OPTION_KEY, 'network' );
	}
	restore_current_blog();
}
