<?php
/**
 * Plugin Name: SQLite integration (Drop-in)
 * Version: 1.0.0
 * Author: WordPress Performance Team
 * Author URI: https://make.wordpress.org/performance/
 *
 * @package wp-sqlite-integration
 */

define( 'SQLITE_DB_DROPIN_VERSION', '1.8.0' );

// Dapatkan path absolut ke folder plugin berdasarkan lokasi file ini
$sqlite_plugin_implementation_folder_path = realpath( dirname( __FILE__ ) . '/plugins/sqlite-database-integration' );

// Jika tidak ditemukan, coba fallback ke wp-content/plugins
if ( ! $sqlite_plugin_implementation_folder_path || ! file_exists( $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/db.php' ) ) {
	$sqlite_plugin_implementation_folder_path = realpath( dirname( __FILE__ ) . '/../plugins/sqlite-database-integration' );
}

// Jika tetap gagal, hentikan eksekusi
if ( ! $sqlite_plugin_implementation_folder_path || ! file_exists( $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/db.php' ) ) {
	error_log( '[SQLite] Plugin implementation not found.' );
	return;
}

// Define constants
if ( ! defined( 'DATABASE_TYPE' ) ) {
	define( 'DATABASE_TYPE', 'sqlite' );
}
if ( ! defined( 'DB_ENGINE' ) ) {
	define( 'DB_ENGINE', 'sqlite' );
}

// Load SQLite DB handler
require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/db.php';

// Optional: Auto-activate plugin jika belum aktif
add_action( 'admin_footer', function () use ( $sqlite_plugin_implementation_folder_path ) {
	if ( defined( 'SQLITE_MAIN_FILE' ) ) {
		return;
	}
	if ( ! function_exists( 'activate_plugin' ) ) {
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
	}

	$plugin_loader_path = $sqlite_plugin_implementation_folder_path . '/load.php';
	$plugin_relative_path = str_replace( realpath( WP_CONTENT_DIR . '/plugins' ) . DIRECTORY_SEPARATOR, '', $plugin_loader_path );

	if ( is_plugin_inactive( $plugin_relative_path ) ) {
		activate_plugin( $plugin_relative_path, '', false, true );
	}
} );