<?php
/**
 * Plugin Name: Speedlyst - Web Speed Analytics
 * Plugin URI: https://speedlyst.org/
 * Description: Local WordPress speed diagnostics with PageSpeed Insights measurement.
 * Version: 0.52.1
 * Requires PHP: 7.4
 * Requires at least: 5.8
 * Author: PPC ONE
 * Author URI: https://ppcone.cz/
 * License: GPL-2.0-or-later
 * Text Domain: speedlyst
 * Domain Path: /languages
 */

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

define( 'MARKETING_CONSOLE_VERSION', '0.52.1' );
define( 'MARKETING_CONSOLE_FILE', __FILE__ );
define( 'MARKETING_CONSOLE_DIR', __DIR__ );


if ( version_compare( PHP_VERSION, '7.4', '<' ) ) {
	add_action(
		'admin_notices',
		static function () {
			if ( ! current_user_can( 'activate_plugins' ) ) {
				return;
			}

			echo '<div class="notice notice-error"><p>';
			echo esc_html__( 'Speedlyst requires PHP 7.4 or newer.', 'speedlyst' );
			echo '</p></div>';
		}
	);

	return;
}

spl_autoload_register(
	static function ( $class ) {
		$prefix = 'MarketingConsole\\';

		if ( 0 !== strpos( $class, $prefix ) ) {
			return;
		}

		$special_classes = array(
			'MarketingConsole\\Modules\\Speed\\SpeedModuleContract' => 'src/Modules/Speed/SpeedModule.php',
		);

		if ( isset( $special_classes[ $class ] ) ) {
			require_once MARKETING_CONSOLE_DIR . '/' . $special_classes[ $class ];
			return;
		}

		$relative = substr( $class, strlen( $prefix ) );

		if ( ! preg_match( '/^[A-Za-z_][A-Za-z0-9_]*(?:\\\\[A-Za-z_][A-Za-z0-9_]*)*$/D', $relative ) ) {
			return;
		}

		$file     = MARKETING_CONSOLE_DIR . '/src/' . str_replace( '\\', '/', $relative ) . '.php';

		if ( is_readable( $file ) ) {
			require_once $file;
		}
	}
);

/*
 * Aktivační a deaktivační hooky MUSÍ být na top-levelu. Při aktivačním
 * requestu WordPress soubor pluginu teprve načítá a plugins_loaded už dávno
 * proběhl, takže registrace schovaná v jeho callbacku by nikdy neproběhla
 * a první cron by se naplánoval až náhodným pozdějším načtením.
 */
register_activation_hook( __FILE__, array( 'MarketingConsole\\Modules\\Speed\\LocalHistory', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'MarketingConsole\\Modules\\Speed\\LocalHistory', 'deactivate' ) );

add_action(
	'plugins_loaded',
	static function () {
		$speed           = new MarketingConsole\Modules\Speed\SpeedModule();
		$diagnostics     = new MarketingConsole\Modules\Speed\LocalDiagnostics();
		$history         = new MarketingConsole\Modules\Speed\LocalHistory( $speed, $diagnostics );
		$recommendations = new MarketingConsole\Modules\Speed\RecommendationEngine();
		$psi             = new MarketingConsole\Admin\Free\PsiClient();
		$psi_history     = new MarketingConsole\Admin\Free\PsiHistory();
		$language        = new MarketingConsole\Core\Language();
		$free_screen     = new MarketingConsole\Admin\Free\DashboardScreen( $history, $recommendations, $psi, $diagnostics, $psi_history, $language );
		$menu            = new MarketingConsole\Admin\Menu( $free_screen );
		$notice_isolation = new MarketingConsole\Admin\NoticeIsolation();
		$privacy_content = new MarketingConsole\Admin\PrivacyContent();

		$menu->register();
		$notice_isolation->register();
		$privacy_content->register();
		$free_screen->register();
		$diagnostics->register();
		$history->register();
		$language->register();
		$speed->register();
		// Vlastní PSI klíč se od 0.9.0 nepoužívá; zbytek po starší verzi uklidíme
		// v denním cronu, ne při každém requestu.
		add_action(
			MarketingConsole\Modules\Speed\LocalHistory::CRON_HOOK,
			static function () {
				delete_option( 'speedlyst_free_psi_api_key' );
			}
		);
	}
);

add_filter(
	'plugin_action_links_' . plugin_basename( MARKETING_CONSOLE_FILE ),
	static function ( array $links ): array {
		$settings = sprintf(
			'<a href="%s">%s</a>',
			esc_url( admin_url( 'admin.php?page=' . MarketingConsole\Admin\Free\DashboardScreen::PAGE_SLUG ) ),
			esc_html__( 'Settings', 'speedlyst' )
		);
		array_unshift( $links, $settings );
		return $links;
	}
);

add_filter(
	'plugin_row_meta',
	static function ( array $links, string $file ): array {
		if ( plugin_basename( MARKETING_CONSOLE_FILE ) !== $file ) {
			return $links;
		}

		$links[] = sprintf(
			'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
			esc_url( 'https://speedlyst.org/' ),
			esc_html( 'Speedlyst.org' )
		);
		$links[] = sprintf(
			'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
			esc_url( 'https://speedlyst.org/#faq' ),
			esc_html( __( 'Documentation', 'speedlyst' ) )
		);

		return $links;
	},
	10,
	2
);
