<?php
/**
 * Plugin Name:       SecurWP – Advanced Security, Firewall & Malware Scanner
 * Plugin URI:        https://securewp.net/wordpress-security-plugin/
 * Description:       Cloud-powered malware scanning, firewall rules, login security with 2FA, vulnerability alerts, and essential WordPress security hardening.
 * Version:           1.0.165
 * Author:            SecurWP
 * Author URI:        https://securewp.net
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       securwp
 * 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';

define( 'SECURWP_VERSION', '1.0.165');
define( 'SECURWP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'SECURWP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'SECURWP_PLUGIN_FILE', __FILE__ );

/**
 * Write a debug message to the PHP error log when debugging is enabled.
 *
 * @param string $message The message to log.
 */
function securwp_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 SecurWP plan for admin UI decisions.
 */
function securwp_get_stored_license_plan(): string {
	if ( ! class_exists( '\SecurWP\Modules\Cloud\Service\CloudSettings' ) ) {
		return 'free';
	}

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

/**
 * Add SecurWP-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 securwp_filter_plugin_row_meta( array $plugin_meta, string $plugin_file ): array {
	if ( $plugin_file !== plugin_basename( SECURWP_PLUGIN_FILE ) ) {
		return $plugin_meta;
	}

	$plan = securwp_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', 'securwp' )
		);
	}

	return $plugin_meta;
}

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

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

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

add_action( 'plugins_loaded', 'securwp_run_plugin' );
add_filter( 'plugin_row_meta', 'securwp_filter_plugin_row_meta', 10, 2 );
