<?php
/**
 * Plugin Name:       SiteFort Security - Malware Scanner, Firewall, Login Security & Hardening
 * Plugin URI:        https://securewp.net/wordpress-security-plugin/
 * Description:       Prevention-first WordPress security. Hardening, firewall, bot filter, and 2FA close the doors; cloud-assisted malware scanning keeps your site fast.
 * Version:           1.7.7
 * Author:            Securewp
 * Author URI:        https://securewp.net
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       sitefort
 * Domain Path:       /languages
 * Requires at least: 6.0
 * Requires PHP:      7.4
 */

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

// PHP 7.4 compatibility polyfills (str_contains, str_starts_with, str_ends_with).
require_once __DIR__ . '/includes/compat.php';
require_once __DIR__ . '/includes/RequestContext.php';
require_once __DIR__ . '/includes/AdminFilesystemCredentials.php';
require_once __DIR__ . '/includes/FilesystemMutation.php';
require_once __DIR__ . '/includes/RemoteUrlValidator.php';
require_once __DIR__ . '/includes/SafeUnserializer.php';

define( 'SITEFORT_VERSION', '1.7.7' );
define( 'SITEFORT_PLUGIN_SLUG', 'sitefort' );
define( 'SITEFORT_TEXT_DOMAIN', 'sitefort' );
define( 'SITEFORT_REST_NAMESPACE', 'sitefort/v1' );
define( 'SITEFORT_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'SITEFORT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'SITEFORT_PLUGIN_FILE', __FILE__ );

/**
 * Write a debug message to the PHP error log when debugging is enabled.
 *
 * @param string $message The message to log.
 */
function sitefort_log( string $message ): void {
	if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
		error_log( $message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
	}
}

require_once __DIR__ . '/vendor/autoload.php';

/**
 * Get the stored SiteFort plan for admin UI decisions.
 */
function sitefort_get_stored_license_plan(): string {
	if ( ! class_exists( '\SiteFort\Modules\Cloud\Service\CloudSettings' ) ) {
		return 'free';
	}

	$cloud = \SiteFort\Modules\Cloud\Service\CloudSettings::get_all();
	return strtolower( (string) ( $cloud['license_plan'] ?? 'free' ) );
}

/**
 * Add SiteFort-specific row meta links on the Plugins screen.
 *
 * @param string[] $plugin_meta Plugin row meta links.
 * @param string   $plugin_file Plugin basename being rendered.
 * @return string[]
 */
function sitefort_filter_plugin_row_meta( array $plugin_meta, string $plugin_file ): array {
	if ( $plugin_file !== plugin_basename( SITEFORT_PLUGIN_FILE ) ) {
		return $plugin_meta;
	}

	$plan = sitefort_get_stored_license_plan();
	if ( ! in_array( $plan, [ 'pro', 'managed' ], true ) ) {
		$plugin_meta[] = sprintf(
			'<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>',
			esc_url( 'https://securewp.net/pricing/' ),
			esc_html__( 'Upgrade to Pro', 'sitefort' )
		);
	}

	return $plugin_meta;
}

function sitefort_run_plugin(): void {
	if ( class_exists( 'SiteFort\\Core\\Core' ) ) {
		$plugin = new \SiteFort\Core\Core();
		$plugin->run();
	}
}

if ( class_exists( 'SiteFort\\Core\\Installer' ) ) {
	register_activation_hook( __FILE__, [ 'SiteFort\\Core\\Installer', 'activate' ] );
	register_deactivation_hook( __FILE__, [ 'SiteFort\\Core\\Installer', 'deactivate' ] );
	add_action( 'wp_initialize_site', [ 'SiteFort\\Core\\Installer', 'initialize_new_site' ], 20, 2 );
}

// WP-CLI commands.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
	\WP_CLI::add_command( 'sitefort', \SiteFort\Includes\CLI\ActivateCommand::class );
}

add_action( 'plugins_loaded', 'sitefort_run_plugin' );
add_filter( 'plugin_row_meta', 'sitefort_filter_plugin_row_meta', 10, 2 );
