<?php
/**
 * Plugin Name:       Site CliniQ
 * Plugin URI:        https://bilalmahmood.dev/projects/site-cliniq
 * Description:       Scans your WordPress site and explains every problem in plain English - PHP errors, SEO, broken links, images, and more.
 * Version:           1.0.3
 * Requires at least: 6.3
 * Requires PHP:      8.0
 * Author:            Bilal Mahmood
 * Author URI:        https://bilalmahmood.dev
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       site-cliniq
 * Domain Path:       /languages
 */

defined( 'ABSPATH' ) || exit;

define( 'SITECL_VERSION',     '1.0.3' );
define( 'SITECL_PLUGIN_DIR',  plugin_dir_path( __FILE__ ) );
define( 'SITECL_PLUGIN_URL',  plugin_dir_url( __FILE__ ) );
define( 'SITECL_PLUGIN_FILE', __FILE__ );

if ( function_exists( 'sitecl_fs' ) ) {
    sitecl_fs()->set_basename( true, __FILE__ );
} else {
    if ( ! function_exists( 'sitecl_fs' ) ) {
        function sitecl_fs() {
            global $sitecl_fs;

            if ( ! isset( $sitecl_fs ) ) {
                // Freemius SDK via Composer.
                require_once dirname( __FILE__ ) . '/vendor/freemius/wordpress-sdk/start.php';

                $sitecl_fs = fs_dynamic_init( [
                    'id'                  => '32256',
                    'slug'                => 'site-cliniq',
                    'premium_slug'        => 'site-cliniq-premium',
                    'type'                => 'plugin',
                    'public_key'          => 'pk_af6eb544f7f9da7a5cc3c8c76d9d9',
                    'is_premium'          => false,   // FREE version
                    'premium_suffix'      => 'Pro',
                    'has_premium_version' => true,
                    'has_addons'          => true,
                    'has_paid_plans'      => true,
                    'menu'                => [
                        'slug'       => 'sitecl',
                        'first-path' => 'plugins.php',
                        'contact'    => false,
                        'support'    => false,
                    ],
                ] );
            }

            return $sitecl_fs;
        }
    }

    sitecl_fs();
    do_action( 'sitecl_fs_loaded' );

    sitecl_fs()->add_action( 'after_uninstall', 'sitecl_fs_uninstall' );
}

if ( ! function_exists( 'sitecl_fs_uninstall' ) ) :
function sitecl_fs_uninstall() {
    global $wpdb;

    // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
    $wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->prefix . 'sitecl_health_scores`' );
    $wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->prefix . 'sitecl_broken_links`' );
    $wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->prefix . 'sitecl_scan_results`' );
    // phpcs:enable

    foreach ( [
        'sitecl_db_version', 'sitecl_last_scan', 'sitecl_last_report_sent',
        'sitecl_claude_api_key', 'sitecl_patchstack_api_key',
    ] as $option ) {
        delete_option( $option );
    }

    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $wpdb->query( $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
        $wpdb->esc_like( '_transient_sitecl_' ) . '%',
        $wpdb->esc_like( '_transient_timeout_sitecl_' ) . '%'
    ) );

    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $wpdb->query( $wpdb->prepare(
        "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
        $wpdb->esc_like( 'sitecl_' ) . '%'
    ) );
}
endif;

require_once SITECL_PLUGIN_DIR . 'includes/class-loader.php';

register_activation_hook( __FILE__,   [ 'SITECL_Loader', 'activate' ] );
register_deactivation_hook( __FILE__, [ 'SITECL_Loader', 'deactivate' ] );

SITECL_Loader::init();
