<?php
/**
 * Plugin Name:       BackReload – Backup, Restore & Migration
 * Plugin URI:        https://wordpress.org/plugins/backreload
 * Description:       Modern WordPress backup, restore & migration — FTP, SFTP, Google Drive, OneDrive, AWS S3, Wasabi & S3-compatible storage, with direct site-to-site migration.
 * Version:           1.0.6
 * Requires at least: 6.0
 * Tested up to:      7.0
 * Requires PHP:      7.4
 * Author:            Shipon Karmakar
 * Author URI:        https://profiles.wordpress.org/shiponkarmakar/
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       backreload
 * Domain Path:       /languages
 *
 * @package Backreload
 */

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

/* ─────────────────────────  Constants  ───────────────────────── */

define( 'BACKRELOAD_VERSION', '1.0.6' );
define( 'BACKRELOAD_FILE',    __FILE__ );
define( 'BACKRELOAD_DIR',     plugin_dir_path( __FILE__ ) );
define( 'BACKRELOAD_URL',     plugin_dir_url( __FILE__ ) );
define( 'BACKRELOAD_DB_VER',  '9' );

/* ─────────────────────────  PHP 8.0 polyfills  ─────────────────
 *
 * The plugin targets PHP 7.4 (see "Requires PHP" above) but the codebase
 * uses str_starts_with(). Provide the PHP 8.0 string helpers on older
 * runtimes so they don't fatal with "Call to undefined function".
 * ─────────────────────────────────────────────────────────────── */

if ( ! function_exists( 'str_starts_with' ) ) {
	function str_starts_with( $haystack, $needle ) {
		return '' === $needle || 0 === strncmp( $haystack, $needle, strlen( $needle ) );
	}
}

if ( ! function_exists( 'str_ends_with' ) ) {
	function str_ends_with( $haystack, $needle ) {
		return '' === $needle || ( strlen( $haystack ) >= strlen( $needle )
			&& 0 === substr_compare( $haystack, $needle, -strlen( $needle ) ) );
	}
}

if ( ! function_exists( 'str_contains' ) ) {
	function str_contains( $haystack, $needle ) {
		return '' === $needle || false !== strpos( $haystack, $needle );
	}
}

/* ─────────────────────────  Autoloader  ────────────────────────
 *
 * Convention:
 *   namespace prefix: Backreload\
 *   path root:        includes/
 *   class names:      kebab-cased filenames, e.g. Backreload\Core\Logger -> includes/core/class-logger.php
 *
 * Sub-namespaces map to sub-directories (lower-case).
 * ─────────────────────────────────────────────────────────────── */
// Composer-bundled third-party libraries (phpseclib — pure-PHP SFTP).
if ( is_readable( BACKRELOAD_DIR . 'vendor/autoload.php' ) ) {
	require_once BACKRELOAD_DIR . 'vendor/autoload.php';
}

require_once BACKRELOAD_DIR . 'includes/class-autoloader.php';
( new Backreload\Autoloader( 'Backreload\\', BACKRELOAD_DIR . 'includes/' ) )->register();

/* ─────────────────────────  Bootstrap  ────────────────────────── */

/**
 * Service container accessor.
 *
 * @return \Backreload\Plugin
 */
function backreload() {
	static $instance = null;
	if ( null === $instance ) {
		$instance = new \Backreload\Plugin();
	}
	return $instance;
}

add_action( 'plugins_loaded', function () {
	// Translations: WordPress.org auto-loads bundled `.mo` files for plugins
	// hosted in the directory since WP 4.6, so no explicit
	// `load_plugin_textdomain()` call is needed (and including one triggers
	// the `PluginCheck.CodeAnalysis.DiscouragedFunctions` warning during
	// submission review). If this plugin is ever distributed off-org and
	// the auto-load doesn't kick in, re-add the call here.
	backreload()->boot();
} );

/* ─────────────────────────  WP-CLI integration  ──────────────────
 * Lets users drive backups from `wp backreload …`, bypassing PHP's web-request
 * lifecycle entirely. The recommended setup for big sites on shared hosting
 * is to wire host-level cron to call WP-CLI rather than relying on wp-cron.
 */
if ( defined( 'WP_CLI' ) && WP_CLI ) {
	\WP_CLI::add_command( 'backreload', \Backreload\CLI\CliCommands::class );
}

/* ─────────────────────────  Activation / Deactivation  ───────── */

register_activation_hook( __FILE__, function () {
	\Backreload\Installer::activate();
} );

register_deactivation_hook( __FILE__, function () {
	\Backreload\Installer::deactivate();
} );
