<?php
/**
 * Plugin Name: PC Membership Plugin
 * Plugin URI: https://plugincompass.com/plugins/pc-membership-abc123
 * Description: Complete membership system with custom pages, Stripe payments (one-time & subscriptions), plan management, and access control.
 * Version: 1.0.0
 * Author: Plugin Compass
 * Author URI: https://plugincompass.com
 * Text Domain: pc-membership-abc123
 * Domain Path: /languages
 * Update URI: false
 * License: GPL v2 or later
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

define( 'PC_MEMBERSHIP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'PC_MEMBERSHIP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'PC_MEMBERSHIP_SLUG', 'pc-membership-abc123' );
define( 'PC_MEMBERSHIP_VERSION', '1.0.0' );

spl_autoload_register( function( $class ) {
    if ( strpos( $class, 'PC_Membership_' ) !== 0 ) {
        return;
    }
    $relative_class = substr( $class, strlen( 'PC_Membership_' ) );
    $file = PC_MEMBERSHIP_PLUGIN_DIR . 'includes/' . strtolower( str_replace( '_', '-', $relative_class ) ) . '.php';
    if ( file_exists( $file ) ) {
        require_once $file;
    }
} );

require_once PC_MEMBERSHIP_PLUGIN_DIR . 'includes/activator.php';
require_once PC_MEMBERSHIP_PLUGIN_DIR . 'includes/deactivator.php';

register_activation_hook( __FILE__, array( 'PC_Membership_Activator', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'PC_Membership_Deactivator', 'deactivate' ) );

require_once PC_MEMBERSHIP_PLUGIN_DIR . 'admin/admin.php';
require_once PC_MEMBERSHIP_PLUGIN_DIR . 'public/public.php';
require_once PC_MEMBERSHIP_PLUGIN_DIR . 'includes/access-control.php';
require_once PC_MEMBERSHIP_PLUGIN_DIR . 'includes/user-roles.php';
require_once PC_MEMBERSHIP_PLUGIN_DIR . 'includes/helpers.php';

function pc_membership_init() {
    load_plugin_textdomain( 'pc-membership-abc123', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

    PC_Membership_Admin::init();
    PC_Membership_Public::init();
    PC_Membership_Access_Control::init();
    PC_Membership_User_Roles::init();
}
add_action( 'plugins_loaded', 'pc_membership_init' );

add_action( 'init', 'pc_membership_register_webhook_endpoint' );
function pc_membership_register_webhook_endpoint() {
    add_rewrite_rule( '^pc-membership-webhook/?$', 'index.php?pc_membership_webhook=1', 'top' );
}

add_filter( 'query_vars', 'pc_membership_webhook_query_vars' );
function pc_membership_webhook_query_vars( $vars ) {
    $vars[] = 'pc_membership_webhook';
    return $vars;
}

add_action( 'template_redirect', 'pc_membership_handle_webhook' );
function pc_membership_handle_webhook() {
    if ( intval( get_query_var( 'pc_membership_webhook' ) ) === 1 ) {
        if ( ! class_exists( 'PC_Membership_Stripe' ) ) {
            require_once PC_MEMBERSHIP_PLUGIN_DIR . 'includes/stripe-handler.php';
        }
        PC_Membership_Stripe::handle_webhook();
        exit;
    }
}

register_activation_hook( __FILE__, 'pc_membership_flush_rewrites' );
function pc_membership_flush_rewrites() {
    pc_membership_register_webhook_endpoint();
    flush_rewrite_rules();
}

register_deactivation_hook( __FILE__, 'pc_membership_flush_rewrites' );
function pc_membership_deactivation_flush() {
    flush_rewrite_rules();
}

add_filter( 'site_transient_update_plugins', function( $value ) {
    $plugin_file = plugin_basename( __FILE__ );
    if ( isset( $value->response[ $plugin_file ] ) ) {
        unset( $value->response[ $plugin_file ] );
    }
    return $value;
} );
