<?php
/**
 * Plugin Name: 404Zen – Broken Link Fixer & Monitor
 * Plugin URI:  https://wordpress.org/plugins/404zen-broken-link-fixer/
 * Description: Fast, private broken link checker that runs on your server. Finds 404s, dead links and redirect chains in the background — no cloud, no account.
 * Version:     1.1.2
 * Author:      Zen Plugins
 * Author URI:  https://zenplugins.org/
 * License:     GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: 404zen-broken-link-fixer
 * Domain Path: /languages
 * Requires at least: 5.8
 * Requires PHP: 7.2
 *
 * 404Zen – Broken Link Fixer & Monitor is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the
 * License, or any later version.
 *
 * 404Zen – Broken Link Fixer & Monitor is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 */

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

define( 'LGBF_VERSION', '1.1.2' );
define( 'LGBF_PLUGIN_FILE', __FILE__ );
define( 'LGBF_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'LGBF_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'LGBF_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );

/**
 * Freemius integration.
 *
 * Product: 404Zen – Broken Link Fixer & Monitor (ID 33434), store: Zen Plugins (17664).
 * Plan "pro" annual pricing IDs: 72416 ($49) / 72422 ($99) / 72435 ($199).
 *
 * While the public key below is empty the plugin falls back to a local stub
 * and every feature check reports the free plan, so the plugin stays fully
 * functional either way.
 */
if ( ! defined( 'LGBF_FREEMIUS_ID' ) ) {
	define( 'LGBF_FREEMIUS_ID', 33434 );
}
if ( ! defined( 'LGBF_FREEMIUS_PUBLIC_KEY' ) ) {
	define( 'LGBF_FREEMIUS_PUBLIC_KEY', 'pk_4bc27cf83ffb91e30cf4d76d4fdf9' );
}

if ( ! function_exists( 'lgbf_fs' ) ) {
	/**
	 * Returns the Freemius instance, or a stub when Freemius is not configured.
	 *
	 * @return object
	 */
	function lgbf_fs() {
		global $lgbf_fs;

		if ( isset( $lgbf_fs ) ) {
			return $lgbf_fs;
		}

		if ( LGBF_FREEMIUS_ID && LGBF_FREEMIUS_PUBLIC_KEY && file_exists( LGBF_PLUGIN_DIR . 'freemius/start.php' ) ) {
			require_once LGBF_PLUGIN_DIR . 'freemius/start.php';

			$lgbf_fs = fs_dynamic_init(
				array(
					'id'             => (string) LGBF_FREEMIUS_ID,
					'slug'           => '404zen-broken-link-fixer',
					'type'           => 'plugin',
					'public_key'     => LGBF_FREEMIUS_PUBLIC_KEY,
					'is_premium'     => false,
					'has_addons'     => false,
					'has_paid_plans' => true,
					'is_org_compliant' => true,
					// No opt-in wizard between "Activate" and value: activation
					// lands straight on the dashboard. Checkout still works —
					// Freemius collects the email at purchase.
					'anonymous_mode' => true,
					'menu'           => array(
						'slug'    => 'lgbf-404zen',
						'support' => false,
						'pricing' => false,
						'contact' => false,
					),
				)
			);
		} else {
			require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-freemius-stub.php';
			$lgbf_fs = new LGBF_Freemius_Stub();
		}

		return $lgbf_fs;
	}

	lgbf_fs();
	do_action( 'lgbf_fs_loaded' );
}

/**
 * Whether premium (Pro) code may run.
 *
 * @return bool
 */
function lgbf_can_use_premium() {
	$fs = lgbf_fs();

	return method_exists( $fs, 'can_use_premium_code' ) && $fs->can_use_premium_code();
}

/**
 * Removes all 404Zen data for the current site.
 */
function lgbf_uninstall_site_data() {
	global $wpdb;

	// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.NoCaching
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}lgbf_links" );
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}lgbf_redirects" );
	// phpcs:enable

	delete_option( 'lgbf_settings' );
	delete_option( 'lgbf_db_version' );
	delete_option( 'lgbf_scan_state' );
	delete_option( 'lgbf_activated_at' );
	delete_option( 'lgbf_digest_last_sent' );
	delete_option( 'lgbf_last_cycle_completed' );
	delete_option( 'lgbf_attribution_reported' );

	wp_clear_scheduled_hook( 'lgbf_scan_batch' );
	wp_clear_scheduled_hook( 'lgbf_weekly_digest' );
}

/**
 * Full uninstall cleanup, multisite-aware. Runs on uninstall only (via the
 * Freemius after_uninstall hook), never on deactivation.
 */
function lgbf_uninstall_cleanup() {
	if ( is_multisite() ) {
		$lgbf_site_ids = get_sites( array( 'fields' => 'ids', 'number' => 0 ) );
		foreach ( $lgbf_site_ids as $lgbf_site_id ) {
			switch_to_blog( $lgbf_site_id );
			lgbf_uninstall_site_data();
			restore_current_blog();
		}
	} else {
		lgbf_uninstall_site_data();
	}
}

// Freemius owns the uninstall flow (no uninstall.php, per Freemius guidelines).
// If the SDK is ever absent (stub mode), fall back to WordPress's native
// uninstall hook so cleanup still runs.
if ( lgbf_fs() instanceof LGBF_Freemius_Stub ) {
	register_uninstall_hook( __FILE__, 'lgbf_uninstall_cleanup' );
} else {
	lgbf_fs()->add_action( 'after_uninstall', 'lgbf_uninstall_cleanup' );
}

require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-settings.php';
require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-activator.php';
require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-link-extractor.php';
require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-checker.php';
require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-scanner.php';
require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-links-store.php';
require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-grade.php';
require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-optin.php';
require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-fixer.php';
require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-cron.php';
require_once LGBF_PLUGIN_DIR . 'includes/class-lgbf-digest.php';

if ( is_admin() ) {
	require_once LGBF_PLUGIN_DIR . 'admin/class-lgbf-admin.php';
	require_once LGBF_PLUGIN_DIR . 'admin/class-lgbf-list-table.php';
	require_once LGBF_PLUGIN_DIR . 'admin/class-lgbf-ajax.php';
}

// Pro-only modules; these files are absent from the free build.
$lgbf_redirects_file = LGBF_PLUGIN_DIR . 'includes/class-lgbf-redirects__premium_only.php';
if ( file_exists( $lgbf_redirects_file ) ) {
	require_once $lgbf_redirects_file;
}
$lgbf_export_file = LGBF_PLUGIN_DIR . 'includes/class-lgbf-export__premium_only.php';
if ( file_exists( $lgbf_export_file ) ) {
	require_once $lgbf_export_file;
}

register_activation_hook( __FILE__, array( 'LGBF_Activator', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'LGBF_Activator', 'deactivate' ) );

/**
 * Boots the plugin.
 */
function lgbf_init() {
	LGBF_Cron::init();
	LGBF_Digest::init();
	LGBF_Scanner::init();

	if ( is_admin() ) {
		LGBF_Admin::init();
		LGBF_Ajax::init();
	}

	if ( lgbf_can_use_premium() ) {
		if ( class_exists( 'LGBF_Redirects' ) ) {
			LGBF_Redirects::init();
		}
		if ( is_admin() && class_exists( 'LGBF_Export' ) ) {
			LGBF_Export::init();
		}
	}
}
add_action( 'plugins_loaded', 'lgbf_init' );
