Restore to commit 74e578279624c6045ca440a3459ebfa1f8d54191

This commit is contained in:
southseact-3d
2026-02-07 20:32:41 +00:00
commit ed67b7741b
252 changed files with 99814 additions and 0 deletions

View File

@@ -0,0 +1,725 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class PC_Membership_Admin {
public static function init() {
add_action( 'admin_menu', array( __CLASS__, 'add_admin_menu' ) );
add_action( 'admin_init', array( __CLASS__, 'register_settings' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
add_action( 'wp_ajax_pc_membership_save_plan', array( __CLASS__, 'ajax_save_plan' ) );
add_action( 'wp_ajax_pc_membership_delete_plan', array( __CLASS__, 'ajax_delete_plan' ) );
add_action( 'wp_ajax_pc_membership_get_plan', array( __CLASS__, 'ajax_get_plan' ) );
add_action( 'wp_ajax_pc_membership_get_stats', array( __CLASS__, 'ajax_get_stats' ) );
add_action( 'wp_ajax_pc_membership_save_pages', array( __CLASS__, 'ajax_save_pages' ) );
add_action( 'wp_ajax_pc_membership_save_access_rule', array( __CLASS__, 'ajax_save_access_rule' ) );
add_action( 'wp_ajax_pc_membership_delete_access_rule', array( __CLASS__, 'ajax_delete_access_rule' ) );
add_action( 'wp_ajax_pc_membership_get_content_for_rule', array( __CLASS__, 'ajax_get_content_for_rule' ) );
}
public static function add_admin_menu() {
add_menu_page(
__( 'Membership', 'pc-membership-abc123' ),
__( 'Membership', 'pc-membership-abc123' ),
'manage_options',
'pc-membership',
array( __CLASS__, 'dashboard_page' ),
'dashicons-id',
81
);
add_submenu_page( 'pc-membership', __( 'Dashboard', 'pc-membership-abc123' ), __( 'Dashboard', 'pc-membership-abc123' ), 'manage_options', 'pc-membership', array( __CLASS__, 'dashboard_page' ) );
add_submenu_page( 'pc-membership', __( 'Plans', 'pc-membership-abc123' ), __( 'Plans', 'pc-membership-abc123' ), 'manage_options', 'pc-membership-plans', array( __CLASS__, 'plans_page' ) );
add_submenu_page( 'pc-membership', __( 'Pages', 'pc-membership-abc123' ), __( 'Pages', 'pc-membership-abc123' ), 'manage_options', 'pc-membership-pages', array( __CLASS__, 'pages_page' ) );
add_submenu_page( 'pc-membership', __( 'Access Rules', 'pc-membership-abc123' ), __( 'Access Rules', 'pc-membership-abc123' ), 'manage_options', 'pc-membership-access', array( __CLASS__, 'access_page' ) );
add_submenu_page( 'pc-membership', __( 'Settings', 'pc-membership-abc123' ), __( 'Settings', 'pc-membership-abc123' ), 'manage_options', 'pc-membership-settings', array( __CLASS__, 'settings_page' ) );
}
public static function register_settings() {
register_setting( 'pc_membership_options', 'pc_membership_options', array( __CLASS__, 'sanitize_options' ) );
add_settings_section( 'pc_membership_stripe', __( 'Stripe Configuration', 'pc-membership-abc123' ), '__return_false', 'pc-membership-settings' );
add_settings_field( 'test_publishable_key', __( 'Test Publishable Key', 'pc-membership-abc123' ), array( __CLASS__, 'field_publishable_key' ), 'pc-membership-settings', 'pc_membership_stripe', array( 'key_type' => 'test' ) );
add_settings_field( 'test_secret_key', __( 'Test Secret Key', 'pc-membership-abc123' ), array( __CLASS__, 'field_secret_key' ), 'pc-membership-settings', 'pc_membership_stripe', array( 'key_type' => 'test' ) );
add_settings_field( 'live_publishable_key', __( 'Live Publishable Key', 'pc-membership-abc123' ), array( __CLASS__, 'field_publishable_key' ), 'pc-membership-settings', 'pc_membership_stripe', array( 'key_type' => 'live' ) );
add_settings_field( 'live_secret_key', __( 'Live Secret Key', 'pc-membership-abc123' ), array( __CLASS__, 'field_secret_key' ), 'pc-membership-settings', 'pc_membership_stripe', array( 'key_type' => 'live' ) );
add_settings_field( 'webhook_secret', __( 'Webhook Secret', 'pc-membership-abc123' ), array( __CLASS__, 'field_webhook_secret' ), 'pc-membership-settings', 'pc_membership_stripe' );
add_settings_field( 'mode', __( 'Mode', 'pc-membership-abc123' ), array( __CLASS__, 'field_mode' ), 'pc-membership-settings', 'pc_membership_stripe' );
add_settings_section( 'pc_membership_general', __( 'General Settings', 'pc-membership-abc123' ), '__return_false', 'pc-membership-settings' );
add_settings_field( 'currency', __( 'Currency', 'pc-membership-abc123' ), array( __CLASS__, 'field_currency' ), 'pc-membership-settings', 'pc_membership_general' );
}
public static function sanitize_options( $input ) {
$output = array();
foreach ( array( 'test_publishable_key', 'test_secret_key', 'live_publishable_key', 'live_secret_key', 'webhook_secret' ) as $key ) {
if ( isset( $input[ $key ] ) ) {
$output[ $key ] = sanitize_text_field( $input[ $key ] );
}
}
if ( isset( $input['mode'] ) && in_array( $input['mode'], array( 'test', 'live' ) ) ) {
$output['mode'] = $input['mode'];
}
if ( isset( $input['currency'] ) && in_array( $input['currency'], array( 'usd', 'eur', 'gbp' ) ) ) {
$output['currency'] = $input['currency'];
}
return $output;
}
public static function field_publishable_key( $args ) {
$options = get_option( 'pc_membership_options' );
$key = $args['key_type'] . '_publishable_key';
$value = isset( $options[ $key ] ) ? esc_attr( $options[ $key ] ) : '';
echo '<input type="text" name="pc_membership_options[' . esc_attr( $key ) . ']" value="' . $value . '" class="regular-text" placeholder="pk_test_..." />';
echo '<p class="description">' . esc_html__( 'Your Stripe publishable key from the Stripe dashboard.', 'pc-membership-abc123' ) . '</p>';
}
public static function field_secret_key( $args ) {
$options = get_option( 'pc_membership_options' );
$key = $args['key_type'] . '_secret_key';
$value = isset( $options[ $key ] ) ? esc_attr( $options[ $key ] ) : '';
echo '<input type="password" name="pc_membership_options[' . esc_attr( $key ) . ']" value="' . $value . '" class="regular-text" autocomplete="new-password" placeholder="sk_test_..." />';
echo '<p class="description">' . esc_html__( 'Your Stripe secret key from the Stripe dashboard.', 'pc-membership-abc123' ) . '</p>';
}
public static function field_webhook_secret() {
$options = get_option( 'pc_membership_options' );
$value = isset( $options['webhook_secret'] ) ? esc_attr( $options['webhook_secret'] ) : '';
echo '<input type="text" name="pc_membership_options[webhook_secret]" value="' . $value . '" class="regular-text" placeholder="whsec_..." />';
echo '<p class="description">' . esc_html__( 'Webhook secret for handling Stripe events.', 'pc-membership-abc123' ) . '</p>';
}
public static function field_mode() {
$options = get_option( 'pc_membership_options' );
$mode = isset( $options['mode'] ) ? $options['mode'] : 'test';
echo '<select name="pc_membership_options[mode]">';
echo '<option value="test"' . selected( $mode, 'test', false ) . '>' . esc_html__( 'Test Mode', 'pc-membership-abc123' ) . '</option>';
echo '<option value="live"' . selected( $mode, 'live', false ) . '>' . esc_html__( 'Live Mode', 'pc-membership-abc123' ) . '</option>';
echo '</select>';
echo '<p class="description">' . esc_html__( 'Use test mode for development. Switch to live mode when ready for production.', 'pc-membership-abc123' ) . '</p>';
}
public static function field_currency() {
$options = get_option( 'pc_membership_options' );
$currency = isset( $options['currency'] ) ? $options['currency'] : 'usd';
echo '<select name="pc_membership_options[currency]">';
echo '<option value="usd"' . selected( $currency, 'usd', false ) . '>USD ($)</option>';
echo '<option value="eur"' . selected( $currency, 'eur', false ) . '>EUR (€)</option>';
echo '<option value="gbp"' . selected( $currency, 'gbp', false ) . '>GBP (£)</option>';
echo '</select>';
}
public static function dashboard_page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
?>
<div class="wrap pc-membership-admin-wrap">
<h1><?php esc_html_e( 'Membership Dashboard', 'pc-membership-abc123' ); ?></h1>
<div class="pc-membership-dashboard-cards">
<div class="pc-membership-card">
<div class="pc-membership-card-header">
<span class="dashicons dashicons-groups"></span>
<h3><?php esc_html_e( 'Active Members', 'pc-membership-abc123' ); ?></h3>
</div>
<div class="pc-membership-card-body">
<div class="pc-membership-stat" id="pc-members-count">-</div>
</div>
</div>
<div class="pc-membership-card">
<div class="pc-membership-card-header">
<span class="dashicons dashicons-cart"></span>
<h3><?php esc_html_e( 'Active Subscriptions', 'pc-membership-abc123' ); ?></h3>
</div>
<div class="pc-membership-card-body">
<div class="pc-membership-stat" id="pc-subscriptions-count">-</div>
</div>
</div>
<div class="pc-membership-card">
<div class="pc-membership-card-header">
<span class="dashicons dashicons-chart-area"></span>
<h3><?php esc_html_e( 'Revenue This Month', 'pc-membership-abc123' ); ?></h3>
</div>
<div class="pc-membership-card-body">
<div class="pc-membership-stat" id="pc-revenue">-</div>
</div>
</div>
<div class="pc-membership-card">
<div class="pc-membership-card-header">
<span class="dashicons dashicons-pressthis"></span>
<h3><?php esc_html_e( 'Total Plans', 'pc-membership-abc123' ); ?></h3>
</div>
<div class="pc-membership-card-body">
<div class="pc-membership-stat" id="pc-plans-count">-</div>
</div>
</div>
</div>
<div class="pc-membership-dashboard-section">
<h2><?php esc_html_e( 'Recent Subscriptions', 'pc-membership-abc123' ); ?></h2>
<div class="pc-membership-table-wrapper">
<table class="widefat fixed striped pc-membership-table" id="pc-recent-subscriptions">
<thead>
<tr>
<th><?php esc_html_e( 'User', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Plan', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Status', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Started', 'pc-membership-abc123' ); ?></th>
</tr>
</thead>
<tbody>
<tr><td colspan="4"><?php esc_html_e( 'Loading...', 'pc-membership-abc123' ); ?></td></tr>
</tbody>
</table>
</div>
</div>
</div>
<?php
}
public static function plans_page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
global $wpdb;
$plans = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}pc_membership_plans ORDER BY id DESC" );
?>
<div class="wrap pc-membership-admin-wrap">
<h1><?php esc_html_e( 'Membership Plans', 'pc-membership-abc123' ); ?>
<a href="#" class="page-title-action pc-membership-add-plan-btn"><?php esc_html_e( 'Add New Plan', 'pc-membership-abc123' ); ?></a>
</h1>
<?php if ( empty( $plans ) ) : ?>
<div class="pc-membership-empty-state">
<span class="dashicons dashicons-pressthis" style="font-size: 48px; height: 48px; width: 48px;"></span>
<h3><?php esc_html_e( 'No plans yet', 'pc-membership-abc123' ); ?></h3>
<p><?php esc_html_e( 'Create your first membership plan to get started.', 'pc-membership-abc123' ); ?></p>
<button class="button button-primary pc-membership-add-plan-btn"><?php esc_html_e( 'Add First Plan', 'pc-membership-abc123' ); ?></button>
</div>
<?php else : ?>
<table class="widefat fixed striped pc-membership-table">
<thead>
<tr>
<th><?php esc_html_e( 'ID', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Name', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Type', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Price', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Billing', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Members', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Actions', 'pc-membership-abc123' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $plans as $plan ) :
$type_label = $plan->is_subscription ? __( 'Subscription', 'pc-membership-abc123' ) : __( 'One-time', 'pc-membership-abc123' );
$billing_label = $plan->is_subscription ? sprintf( '%s / %s', pc_membership_format_price( $plan->price ), $plan->billing_interval ) : pc_membership_format_price( $plan->price );
$members_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}pc_membership_subscriptions WHERE plan_id = %d AND status = 'active'", $plan->id ) );
?>
<tr>
<td><?php echo esc_html( $plan->id ); ?></td>
<td><strong><?php echo esc_html( $plan->name ); ?></strong></td>
<td><?php echo esc_html( $type_label ); ?></td>
<td><?php echo esc_html( pc_membership_format_price( $plan->price ) ); ?></td>
<td><?php echo esc_html( $billing_label ); ?></td>
<td><?php echo esc_html( (string) $members_count ); ?></td>
<td>
<button class="button pc-membership-edit-plan" data-plan-id="<?php echo esc_attr( $plan->id ); ?>"><?php esc_html_e( 'Edit', 'pc-membership-abc123' ); ?></button>
<button class="button pc-membership-delete-plan" data-plan-id="<?php echo esc_attr( $plan->id ); ?>"><?php esc_html_e( 'Delete', 'pc-membership-abc123' ); ?></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<div id="pc-membership-plan-modal" class="pc-membership-modal" style="display: none;">
<div class="pc-membership-modal-content">
<div class="pc-membership-modal-header">
<h2><?php esc_html_e( 'Membership Plan', 'pc-membership-abc123' ); ?></h2>
<button class="pc-membership-modal-close">&times;</button>
</div>
<form id="pc-membership-plan-form" method="post">
<?php wp_nonce_field( 'pc_membership_save_plan', 'pc_membership_plan_nonce' ); ?>
<input type="hidden" name="plan_id" id="plan_id" value="">
<div class="pc-membership-form-row">
<label for="plan_name"><?php esc_html_e( 'Plan Name', 'pc-membership-abc123' ); ?> *</label>
<input type="text" name="name" id="plan_name" required class="regular-text">
</div>
<div class="pc-membership-form-row">
<label for="plan_description"><?php esc_html_e( 'Description', 'pc-membership-abc123' ); ?></label>
<textarea name="description" id="plan_description" rows="3" class="regular-text"></textarea>
</div>
<div class="pc-membership-form-row">
<label for="plan_type"><?php esc_html_e( 'Pricing Type', 'pc-membership-abc123' ); ?></label>
<select name="is_subscription" id="plan_type">
<option value="0"><?php esc_html_e( 'One-time Payment', 'pc-membership-abc123' ); ?></option>
<option value="1"><?php esc_html_e( 'Subscription', 'pc-membership-abc123' ); ?></option>
</select>
</div>
<div class="pc-membership-form-row">
<label for="plan_price"><?php esc_html_e( 'Price', 'pc-membership-abc123' ); ?> *</label>
<input type="number" name="price" id="plan_price" step="0.01" min="0" required class="regular-text">
</div>
<div class="pc-membership-form-row" id="billing_interval_row" style="display: none;">
<label for="plan_billing_interval"><?php esc_html_e( 'Billing Interval', 'pc-membership-abc123' ); ?></label>
<select name="billing_interval" id="plan_billing_interval">
<option value="day"><?php esc_html_e( 'Daily', 'pc-membership-abc123' ); ?></option>
<option value="week"><?php esc_html_e( 'Weekly', 'pc-membership-abc123' ); ?></option>
<option value="month" selected><?php esc_html_e( 'Monthly', 'pc-membership-abc123' ); ?></option>
<option value="year"><?php esc_html_e( 'Yearly', 'pc-membership-abc123' ); ?></option>
</select>
</div>
<div class="pc-membership-form-row">
<label for="plan_trial_days"><?php esc_html_e( 'Trial Period (Days)', 'pc-membership-abc123' ); ?></label>
<input type="number" name="trial_days" id="plan_trial_days" min="0" value="0" class="regular-text">
<p class="description"><?php esc_html_e( 'Number of free trial days before billing starts. Set to 0 for no trial.', 'pc-membership-abc123' ); ?></p>
</div>
<div class="pc-membership-form-row">
<label for="plan_benefits"><?php esc_html_e( 'Benefits', 'pc-membership-abc123' ); ?></label>
<textarea name="benefits" id="plan_benefits" rows="5" class="regular-text" placeholder="- Unlimited access&#10;- Priority support"></textarea>
<p class="description"><?php esc_html_e( 'Enter one benefit per line.', 'pc-membership-abc123' ); ?></p>
</div>
<div class="pc-membership-form-row">
<label for="plan_role"><?php esc_html_e( 'User Role', 'pc-membership-abc123' ); ?></label>
<select name="role" id="plan_role">
<?php foreach ( self::get_available_roles() as $role_key => $role_name ) : ?>
<option value="<?php echo esc_attr( $role_key ); ?>"><?php echo esc_html( $role_name ); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="pc-membership-form-actions">
<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Plan', 'pc-membership-abc123' ); ?></button>
<button type="button" class="button pc-membership-modal-cancel"><?php esc_html_e( 'Cancel', 'pc-membership-abc123' ); ?></button>
</div>
</form>
</div>
</div>
</div>
<?php
}
private static function get_available_roles() {
$roles = get_editable_roles();
$options = array();
foreach ( $roles as $role_key => $role_data ) {
$options[ $role_key ] = translate_user_role( $role_data['name'] );
}
return $options;
}
public static function ajax_save_plan() {
check_ajax_referer( 'pc_membership_save_plan', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
global $wpdb;
$plan_id = isset( $_POST['plan_id'] ) ? absint( $_POST['plan_id'] ) : 0;
$name = sanitize_text_field( wp_unslash( $_POST['name'] ) );
$description = isset( $_POST['description'] ) ? sanitize_textarea_field( wp_unslash( $_POST['description'] ) ) : '';
$price = isset( $_POST['price'] ) ? floatval( $_POST['price'] ) : 0;
$is_subscription = isset( $_POST['is_subscription'] ) ? absint( $_POST['is_subscription'] ) : 0;
$billing_interval = isset( $_POST['billing_interval'] ) ? sanitize_text_field( wp_unslash( $_POST['billing_interval'] ) ) : 'month';
$trial_days = isset( $_POST['trial_days'] ) ? absint( $_POST['trial_days'] ) : 0;
$benefits = isset( $_POST['benefits'] ) ? sanitize_textarea_field( wp_unslash( $_POST['benefits'] ) ) : '';
$role = isset( $_POST['role'] ) ? sanitize_text_field( wp_unslash( $_POST['role'] ) ) : 'subscriber';
if ( empty( $name ) || $price < 0 ) {
wp_send_json_error( __( 'Invalid input data', 'pc-membership-abc123' ) );
}
$data = array(
'name' => $name,
'description' => $description,
'price' => $price,
'is_subscription' => $is_subscription,
'billing_interval' => $is_subscription ? $billing_interval : '',
'trial_days' => $trial_days,
'benefits' => $benefits,
'role' => $role,
);
if ( $plan_id ) {
$result = $wpdb->update( $wpdb->prefix . 'pc_membership_plans', $data, array( 'id' => $plan_id ) );
} else {
$result = $wpdb->insert( $wpdb->prefix . 'pc_membership_plans', $data );
$plan_id = $wpdb->insert_id;
}
if ( $result === false ) {
wp_send_json_error( __( 'Failed to save plan', 'pc-membership-abc123' ) );
}
wp_send_json_success( array( 'plan_id' => $plan_id ) );
}
public static function ajax_get_plan() {
check_ajax_referer( 'pc_membership_save_plan', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
$plan_id = isset( $_POST['plan_id'] ) ? absint( $_POST['plan_id'] ) : 0;
if ( ! $plan_id ) {
wp_send_json_error( __( 'Invalid plan ID', 'pc-membership-abc123' ) );
}
global $wpdb;
$plan = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}pc_membership_plans WHERE id = %d", $plan_id ) );
if ( ! $plan ) {
wp_send_json_error( __( 'Plan not found', 'pc-membership-abc123' ) );
}
wp_send_json_success( array( 'plan' => $plan ) );
}
public static function ajax_delete_plan() {
check_ajax_referer( 'pc_membership_save_plan', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
$plan_id = isset( $_POST['plan_id'] ) ? absint( $_POST['plan_id'] ) : 0;
if ( ! $plan_id ) {
wp_send_json_error( __( 'Invalid plan ID', 'pc-membership-abc123' ) );
}
global $wpdb;
$wpdb->delete( $wpdb->prefix . 'pc_membership_subscriptions', array( 'plan_id' => $plan_id ) );
$result = $wpdb->delete( $wpdb->prefix . 'pc_membership_plans', array( 'id' => $plan_id ) );
if ( $result === false ) {
wp_send_json_error( __( 'Failed to delete plan', 'pc-membership-abc123' ) );
}
wp_send_json_success();
}
public static function ajax_get_stats() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
global $wpdb;
$active_members = $wpdb->get_var( "SELECT COUNT(DISTINCT user_id) FROM {$wpdb->prefix}pc_membership_subscriptions WHERE status = 'active'" );
$active_subscriptions = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}pc_membership_subscriptions WHERE status = 'active'" );
$total_plans = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}pc_membership_plans" );
$current_month = date( 'Y-m-01' );
$revenue = $wpdb->get_var( $wpdb->prepare( "SELECT COALESCE(SUM(amount), 0) FROM {$wpdb->prefix}pc_membership_payments WHERE status = 'succeeded' AND created_at >= %s", $current_month ) );
$recent_subs = $wpdb->get_results( "SELECT s.*, u.display_name, u.user_login, p.name as plan_name FROM {$wpdb->prefix}pc_membership_subscriptions s LEFT JOIN {$wpdb->users} u ON s.user_id = u.ID LEFT JOIN {$wpdb->prefix}pc_membership_plans p ON s.plan_id = p.id ORDER BY s.created_at DESC LIMIT 10" );
wp_send_json_success( array(
'active_members' => $active_members,
'active_subscriptions' => $active_subscriptions,
'total_plans' => $total_plans,
'revenue' => pc_membership_format_price( $revenue ),
'recent_subscriptions' => $recent_subs,
) );
}
public static function ajax_save_pages() {
check_ajax_referer( 'pc_membership_save_pages', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
if ( isset( $_POST['pc_membership_options'] ) ) {
$options = get_option( 'pc_membership_options', array() );
foreach ( $_POST['pc_membership_options'] as $key => $value ) {
$options[ $key ] = absint( $value );
}
update_option( 'pc_membership_options', $options );
}
wp_send_json_success();
}
public static function pages_page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
$options = get_option( 'pc_membership_options', array() );
$page_fields = array(
'checkout' => __( 'Checkout Page', 'pc-membership-abc123' ),
'login' => __( 'Login Page', 'pc-membership-abc123' ),
'register' => __( 'Registration Page', 'pc-membership-abc123' ),
'account' => __( 'Account Page', 'pc-membership-abc123' ),
'success' => __( 'Success Page', 'pc-membership-abc123' ),
'cancel' => __( 'Cancel Page', 'pc-membership-abc123' ),
);
$pages = get_pages( array( 'post_status' => 'publish' ) );
?>
<div class="wrap pc-membership-admin-wrap">
<h1><?php esc_html_e( 'Membership Pages', 'pc-membership-abc123' ); ?></h1>
<form method="post" action="<?php echo esc_url( admin_url( 'admin.php?page=pc-membership-pages' ) ); ?>">
<?php wp_nonce_field( 'pc_membership_save_pages', 'pc_membership_pages_nonce' ); ?>
<div class="pc-membership-page-settings">
<?php foreach ( $page_fields as $key => $label ) :
$selected = isset( $options[ $key . '_page_id' ] ) ? absint( $options[ $key . '_page_id' ] ) : 0;
?>
<div class="pc-membership-page-setting">
<label for="<?php echo esc_attr( 'page_' . $key ); ?>"><strong><?php echo esc_html( $label ); ?></strong></label>
<select name="pc_membership_options[<?php echo esc_attr( $key . '_page_id' ); ?>]" id="<?php echo esc_attr( 'page_' . $key ); ?>">
<option value="0"><?php esc_html_e( '-- Select --', 'pc-membership-abc123' ); ?></option>
<?php foreach ( $pages as $page ) : ?>
<option value="<?php echo esc_attr( $page->ID ); ?>" <?php selected( $selected, $page->ID ); ?>>
<?php echo esc_html( $page->post_title ); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<?php endforeach; ?>
</div>
<?php submit_button( __( 'Save Page Settings', 'pc-membership-abc123' ) ); ?>
</form>
<hr>
<h2><?php esc_html_e( 'Quick Create Pages', 'pc-membership-abc123' ); ?></h2>
<form method="post" action="<?php echo esc_url( admin_url( 'admin.php?page=pc-membership-pages' ) ); ?>">
<?php wp_nonce_field( 'pc_membership_create_pages', 'pc_membership_create_pages_nonce' ); ?>
<input type="hidden" name="pc_membership_create_pages" value="1">
<?php submit_button( __( 'Create All Pages', 'pc-membership-abc123' ), 'secondary' ); ?>
</form>
</div>
<?php
if ( isset( $_POST['pc_membership_create_pages'] ) && check_admin_referer( 'pc_membership_create_pages', 'pc_membership_create_pages_nonce' ) ) {
self::create_default_pages();
}
}
private static function create_default_pages() {
$pages = array(
'checkout' => array( 'title' => __( 'Membership Checkout', 'pc-membership-abc123' ), 'content' => '[pc_membership_checkout]' ),
'login' => array( 'title' => __( 'Member Login', 'pc-membership-abc123' ), 'content' => '[pc_membership_login]' ),
'register' => array( 'title' => __( 'Member Registration', 'pc-membership-abc123' ), 'content' => '[pc_membership_register]' ),
'account' => array( 'title' => __( 'My Account', 'pc-membership-abc123' ), 'content' => '[pc_membership_account]' ),
'success' => array( 'title' => __( 'Payment Successful', 'pc-membership-abc123' ), 'content' => '[pc_membership_success]' ),
'cancel' => array( 'title' => __( 'Payment Cancelled', 'pc-membership-abc123' ), 'content' => '[pc_membership_cancel]' ),
);
$options = get_option( 'pc_membership_options', array() );
foreach ( $pages as $key => $page_data ) {
$existing_id = isset( $options[ $key . '_page_id' ] ) ? absint( $options[ $key . '_page_id' ] ) : 0;
if ( ! $existing_id || ! get_post( $existing_id ) ) {
$page_id = wp_insert_post( array( 'post_title' => $page_data['title'], 'post_content' => $page_data['content'], 'post_status' => 'publish', 'post_type' => 'page' ) );
if ( $page_id && ! is_wp_error( $page_id ) ) {
$options[ $key . '_page_id' ] = $page_id;
}
}
}
update_option( 'pc_membership_options', $options );
echo '<div class="notice notice-success"><p>' . esc_html__( 'Membership pages created successfully!', 'pc-membership-abc123' ) . '</p></div>';
}
public static function access_page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
global $wpdb;
$plans = $wpdb->get_results( "SELECT id, name FROM {$wpdb->prefix}pc_membership_plans" );
$rules = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}pc_membership_access_rules ORDER BY id DESC" );
?>
<div class="wrap pc-membership-admin-wrap">
<h1><?php esc_html_e( 'Access Rules', 'pc-membership-abc123' ); ?></h1>
<div class="pc-membership-access-rules">
<h2><?php esc_html_e( 'Add New Rule', 'pc-membership-abc123' ); ?></h2>
<form id="pc-membership-access-form" class="pc-membership-access-form">
<?php wp_nonce_field( 'pc_membership_save_access_rule', 'pc_membership_access_nonce' ); ?>
<div class="pc-membership-form-row">
<label for="access_content_type"><?php esc_html_e( 'Content Type', 'pc-membership-abc123' ); ?></label>
<select name="content_type" id="access_content_type" required>
<option value="page"><?php esc_html_e( 'Page', 'pc-membership-abc123' ); ?></option>
<option value="post"><?php esc_html_e( 'Post', 'pc-membership-abc123' ); ?></option>
<option value="category"><?php esc_html_e( 'Category', 'pc-membership-abc123' ); ?></option>
</select>
</div>
<div class="pc-membership-form-row">
<label for="access_content_id"><?php esc_html_e( 'Select Content', 'pc-membership-abc123' ); ?></label>
<select name="content_id" id="access_content_id" required>
<option value=""><?php esc_html_e( '-- Select --', 'pc-membership-abc123' ); ?></option>
</select>
</div>
<div class="pc-membership-form-row">
<label for="access_plan_ids"><?php esc_html_e( 'Required Plans', 'pc-membership-abc123' ); ?></label>
<select name="plan_ids[]" id="access_plan_ids" multiple required>
<?php foreach ( $plans as $plan ) : ?>
<option value="<?php echo esc_attr( $plan->id ); ?>"><?php echo esc_html( $plan->name ); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="pc-membership-form-row">
<label for="access_redirect"><?php esc_html_e( 'Redirect Non-Members To', 'pc-membership-abc123' ); ?></label>
<select name="redirect" id="access_redirect">
<option value="checkout"><?php esc_html_e( 'Checkout Page', 'pc-membership-abc123' ); ?></option>
<option value="login"><?php esc_html_e( 'Login Page', 'pc-membership-abc123' ); ?></option>
<option value="custom"><?php esc_html_e( 'Custom URL', 'pc-membership-abc123' ); ?></option>
</select>
</div>
<div class="pc-membership-form-row" id="custom_redirect_row" style="display: none;">
<label for="access_custom_url"><?php esc_html_e( 'Custom URL', 'pc-membership-abc123' ); ?></label>
<input type="url" name="custom_url" id="access_custom_url" class="regular-text" placeholder="https://...">
</div>
<div class="pc-membership-form-actions">
<button type="submit" class="button button-primary"><?php esc_html_e( 'Add Rule', 'pc-membership-abc123' ); ?></button>
</div>
</form>
<h2><?php esc_html_e( 'Existing Rules', 'pc-membership-abc123' ); ?></h2>
<table class="widefat fixed striped pc-membership-table" id="pc-access-rules-table">
<thead>
<tr>
<th><?php esc_html_e( 'Content', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Required Plans', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Redirect', 'pc-membership-abc123' ); ?></th>
<th><?php esc_html_e( 'Actions', 'pc-membership-abc123' ); ?></th>
</tr>
</thead>
<tbody>
<?php if ( empty( $rules ) ) : ?>
<tr><td colspan="4"><?php esc_html_e( 'No access rules defined yet.', 'pc-membership-abc123' ); ?></td></tr>
<?php else : ?>
<?php foreach ( $rules as $rule ) :
$content_title = '';
if ( $rule->content_type === 'category' ) {
$cat = get_term( $rule->content_id );
$content_title = $cat ? $cat->name : sprintf( __( 'Category #%d', 'pc-membership-abc123' ), $rule->content_id );
} else {
$post = get_post( $rule->content_id );
$content_title = $post ? $post->post_title : sprintf( __( '%s #%d', 'pc-membership-abc123' ), $rule->content_type, $rule->content_id );
}
$plan_ids = maybe_unserialize( $rule->plan_ids );
if ( ! is_array( $plan_ids ) ) { $plan_ids = array( $plan_ids ); }
$plan_names = array();
foreach ( $plan_ids as $plan_id ) {
$plan = $wpdb->get_var( $wpdb->prepare( "SELECT name FROM {$wpdb->prefix}pc_membership_plans WHERE id = %d", $plan_id ) );
if ( $plan ) { $plan_names[] = $plan; }
}
$redirect_text = $rule->redirect_type === 'custom' ? esc_html__( 'Custom URL', 'pc-membership-abc123' ) : ( $rule->redirect_type === 'checkout' ? esc_html__( 'Checkout', 'pc-membership-abc123' ) : esc_html__( 'Login', 'pc-membership-abc123' ) );
?>
<tr>
<td><?php echo esc_html( $content_title ) . ' <span class="description">(' . esc_html( $rule->content_type ) . ')</span>'; ?></td>
<td><?php echo esc_html( implode( ', ', $plan_names ) ); ?></td>
<td><?php echo $redirect_text; ?></td>
<td><button class="button pc-membership-delete-rule" data-rule-id="<?php echo esc_attr( $rule->id ); ?>"><?php esc_html_e( 'Delete', 'pc-membership-abc123' ); ?></button></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<?php
}
public static function ajax_save_access_rule() {
check_ajax_referer( 'pc_membership_save_access_rule', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
$content_type = sanitize_text_field( wp_unslash( $_POST['content_type'] ) );
$content_id = absint( $_POST['content_id'] );
$plan_ids = array_map( 'absint', (array) $_POST['plan_ids'] );
$redirect = sanitize_text_field( wp_unslash( $_POST['redirect'] ) );
$custom_url = isset( $_POST['custom_url'] ) ? esc_url_raw( wp_unslash( $_POST['custom_url'] ) ) : '';
if ( ! in_array( $content_type, array( 'page', 'post', 'category' ) ) || empty( $plan_ids ) ) {
wp_send_json_error( __( 'Invalid input', 'pc-membership-abc123' ) );
}
global $wpdb;
$existing = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}pc_membership_access_rules WHERE content_type = %s AND content_id = %d", $content_type, $content_id ) );
if ( $existing ) {
wp_send_json_error( __( 'A rule for this content already exists.', 'pc-membership-abc123' ) );
}
$result = $wpdb->insert( $wpdb->prefix . 'pc_membership_access_rules', array(
'content_type' => $content_type,
'content_id' => $content_id,
'plan_ids' => maybe_serialize( $plan_ids ),
'redirect_type' => $redirect,
'custom_url' => $redirect === 'custom' ? $custom_url : '',
) );
if ( ! $result ) {
wp_send_json_error( __( 'Failed to save rule', 'pc-membership-abc123' ) );
}
wp_send_json_success();
}
public static function ajax_delete_access_rule() {
check_ajax_referer( 'pc_membership_save_access_rule', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
$rule_id = absint( $_POST['rule_id'] );
global $wpdb;
$result = $wpdb->delete( $wpdb->prefix . 'pc_membership_access_rules', array( 'id' => $rule_id ) );
if ( ! $result ) {
wp_send_json_error( __( 'Failed to delete rule', 'pc-membership-abc123' ) );
}
wp_send_json_success();
}
public static function ajax_get_content_for_rule() {
check_ajax_referer( 'pc_membership_admin_nonce', 'nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
$content_type = isset( $_POST['content_type'] ) ? sanitize_text_field( wp_unslash( $_POST['content_type'] ) ) : 'page';
$items = array();
if ( $content_type === 'page' ) {
$pages = get_pages( array( 'post_status' => 'publish' ) );
foreach ( $pages as $page ) { $items[] = array( 'id' => $page->ID, 'title' => $page->post_title ); }
} elseif ( $content_type === 'post' ) {
$posts = get_posts( array( 'post_status' => 'publish', 'posts_per_page' => -1 ) );
foreach ( $posts as $post ) { $items[] = array( 'id' => $post->ID, 'title' => $post->post_title ); }
} elseif ( $content_type === 'category' ) {
$categories = get_categories( array( 'hide_empty' => false ) );
foreach ( $categories as $cat ) { $items[] = array( 'id' => $cat->term_id, 'title' => $cat->name ); }
}
wp_send_json_success( array( 'items' => $items ) );
}
public static function settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
?>
<div class="wrap pc-membership-admin-wrap">
<h1><?php esc_html_e( 'Membership Settings', 'pc-membership-abc123' ); ?></h1>
<form method="post" action="options.php">
<?php settings_fields( 'pc_membership_options' ); do_settings_sections( 'pc-membership-settings' ); submit_button(); ?>
</form>
</div>
<?php
}
public static function enqueue_assets( $hook ) {
if ( strpos( $hook, 'pc-membership' ) === false ) {
return;
}
wp_enqueue_style( 'pc-membership-admin-style', PC_MEMBERSHIP_PLUGIN_URL . 'admin/css/admin-style.css', array(), PC_MEMBERSHIP_VERSION );
wp_enqueue_script( 'pc-membership-admin-script', PC_MEMBERSHIP_PLUGIN_URL . 'admin/js/admin-script.js', array( 'jquery', 'jquery-ui-dialog' ), PC_MEMBERSHIP_VERSION, true );
wp_localize_script( 'pc-membership-admin-script', 'pcMembershipAdmin', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'pc_membership_admin_nonce' ),
'i18n' => array(
'save' => __( 'Save', 'pc-membership-abc123' ),
'cancel' => __( 'Cancel', 'pc-membership-abc123' ),
'delete' => __( 'Delete', 'pc-membership-abc123' ),
'confirmDelete' => __( 'Are you sure you want to delete this plan?', 'pc-membership-abc123' ),
'loading' => __( 'Loading...', 'pc-membership-abc123' ),
'success' => __( 'Saved successfully', 'pc-membership-abc123' ),
'error' => __( 'An error occurred', 'pc-membership-abc123' ),
),
) );
}
}

View File

@@ -0,0 +1,222 @@
.pc-membership-admin-wrap {
padding: 20px 0;
}
.pc-membership-admin-wrap h1 {
margin-bottom: 20px;
}
.pc-membership-dashboard-cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-bottom: 30px;
}
.pc-membership-card {
background: #fff;
border: 1px solid #c3c4c7;
border-radius: 4px;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
overflow: hidden;
}
.pc-membership-card-header {
background: #f0f0f1;
padding: 12px 15px;
border-bottom: 1px solid #c3c4c7;
display: flex;
align-items: center;
gap: 10px;
}
.pc-membership-card-header .dashicons {
color: #2271b1;
}
.pc-membership-card-header h3 {
margin: 0;
font-size: 14px;
font-weight: 600;
}
.pc-membership-card-body {
padding: 20px;
text-align: center;
}
.pc-membership-stat {
font-size: 32px;
font-weight: 700;
color: #2271b1;
}
.pc-membership-dashboard-section {
margin-top: 30px;
background: #fff;
border: 1px solid #c3c4c7;
border-radius: 4px;
padding: 20px;
}
.pc-membership-table-wrapper {
margin-top: 15px;
}
.pc-membership-empty-state {
text-align: center;
padding: 60px 20px;
background: #fff;
border: 1px solid #c3c4c7;
border-radius: 4px;
}
.pc-membership-empty-state h3 {
margin: 15px 0 10px;
}
.pc-membership-empty-state p {
color: #646970;
margin-bottom: 20px;
}
.pc-membership-page-settings {
background: #fff;
border: 1px solid #c3c4c7;
border-radius: 4px;
padding: 20px;
max-width: 600px;
}
.pc-membership-page-setting {
margin-bottom: 20px;
}
.pc-membership-page-setting label {
display: block;
margin-bottom: 5px;
}
.pc-membership-page-setting select {
width: 100%;
max-width: 400px;
}
.pc-membership-modal {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 999999;
display: flex;
align-items: center;
justify-content: center;
}
.pc-membership-modal-content {
background: #fff;
border-radius: 4px;
width: 90%;
max-width: 600px;
max-height: 90vh;
overflow-y: auto;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}
.pc-membership-modal-header {
padding: 15px 20px;
border-bottom: 1px solid #c3c4c7;
display: flex;
justify-content: space-between;
align-items: center;
background: #f0f0f1;
}
.pc-membership-modal-header h2 {
margin: 0;
font-size: 18px;
}
.pc-membership-modal-close {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
color: #646970;
padding: 0;
line-height: 1;
}
.pc-membership-modal-close:hover {
color: #23282d;
}
#pc-membership-plan-form,
.pc-membership-access-form {
padding: 20px;
}
.pc-membership-form-row {
margin-bottom: 20px;
}
.pc-membership-form-row label {
display: block;
font-weight: 600;
margin-bottom: 5px;
}
.pc-membership-form-row input[type="text"],
.pc-membership-form-row input[type="number"],
.pc-membership-form-row input[type="url"],
.pc-membership-form-row textarea,
.pc-membership-form-row select {
width: 100%;
max-width: 400px;
}
.pc-membership-form-actions {
padding-top: 15px;
border-top: 1px solid #e5e5e5;
margin-top: 20px;
}
.pc-membership-form-actions .button {
margin-right: 10px;
}
.pc-membership-access-rules {
background: #fff;
border: 1px solid #c3c4c7;
border-radius: 4px;
padding: 20px;
}
.pc-membership-access-rules h2 {
margin-top: 0;
margin-bottom: 20px;
font-size: 18px;
}
.pc-membership-access-form {
background: #f0f0f1;
padding: 20px;
border-radius: 4px;
margin-bottom: 30px;
}
.pc-membership-access-form .pc-membership-form-row {
margin-bottom: 15px;
}
.pc-membership-access-form select[multiple] {
height: 120px;
}
@media screen and (max-width: 782px) {
.pc-membership-dashboard-cards {
grid-template-columns: 1fr;
}
}

View File

@@ -0,0 +1,251 @@
(function($) {
'use strict';
var PCMembershipAdmin = {
init: function() {
this.bindEvents();
this.loadStats();
this.initPlanModal();
this.initAccessForm();
},
bindEvents: function() {
$(document).on('click', '.pc-membership-add-plan-btn', $.proxy(this.openPlanModal, this));
$(document).on('click', '.pc-membership-edit-plan', $.proxy(this.editPlan, this));
$(document).on('click', '.pc-membership-delete-plan', $.proxy(this.deletePlan, this));
$(document).on('click', '.pc-membership-modal-close, .pc-membership-modal-cancel', $.proxy(this.closePlanModal, this));
$(document).on('submit', '#pc-membership-plan-form', $.proxy(this.savePlan, this));
$(document).on('change', '#plan_type', $.proxy(this.toggleBillingInterval, this));
$(document).on('click', '.pc-membership-delete-rule', $.proxy(this.deleteAccessRule, this));
$(document).on('submit', '#pc-membership-access-form', $.proxy(this.saveAccessRule, this));
$(document).on('change', '#access_redirect', $.proxy(this.toggleCustomRedirect, this));
$(document).on('change', '#access_content_type', $.proxy(this.loadContentOptions, this));
},
loadStats: function() {
$.ajax({
url: pcMembershipAdmin.ajax_url,
type: 'POST',
data: {
action: 'pc_membership_get_stats',
nonce: pcMembershipAdmin.nonce
},
success: function(response) {
if (response.success) {
var data = response.data;
$('#pc-members-count').text(data.active_members);
$('#pc-subscriptions-count').text(data.active_subscriptions);
$('#pc-revenue').text(data.revenue);
$('#pc-plans-count').text(data.total_plans);
if (data.recent_subscriptions && data.recent_subscriptions.length > 0) {
var tbody = $('#pc-recent-subscriptions tbody');
tbody.empty();
$.each(data.recent_subscriptions, function(i, sub) {
var display_name = sub.display_name || sub.user_login || 'User #' + sub.user_id;
tbody.append(
'<tr>' +
'<td>' + display_name + '</td>' +
'<td>' + sub.plan_name + '</td>' +
'<td>' + sub.status + '</td>' +
'<td>' + sub.started_at.substring(0, 10) + '</td>' +
'</tr>'
);
});
}
}
}
});
},
initPlanModal: function() {
$('#pc-membership-plan-modal').dialog({
autoOpen: false,
modal: true,
width: '90%',
maxWidth: 600,
closeText: ''
});
},
openPlanModal: function(e) {
if (e) e.preventDefault();
$('#pc-membership-plan-form')[0].reset();
$('#plan_id').val('');
$('#pc-membership-plan-modal').dialog('open');
this.toggleBillingInterval();
},
editPlan: function(e) {
e.preventDefault();
var planId = $(e.currentTarget).data('plan-id');
$.ajax({
url: pcMembershipAdmin.ajax_url,
type: 'POST',
data: {
action: 'pc_membership_get_plan',
plan_id: planId,
nonce: pcMembershipAdmin.nonce
},
success: $.proxy(function(response) {
if (response.success && response.data.plan) {
var plan = response.data.plan;
$('#plan_id').val(plan.id);
$('#plan_name').val(plan.name);
$('#plan_description').val(plan.description || '');
$('#plan_price').val(plan.price);
$('#plan_type').val(plan.is_subscription);
$('#plan_billing_interval').val(plan.billing_interval || 'month');
$('#plan_trial_days').val(plan.trial_days || 0);
$('#plan_benefits').val(plan.benefits || '');
$('#plan_role').val(plan.role || 'subscriber');
$('#pc-membership-plan-modal').dialog('open');
this.toggleBillingInterval();
}
}, this)
});
},
savePlan: function(e) {
e.preventDefault();
var $form = $('#pc-membership-plan-form');
var formData = $form.serialize();
$.ajax({
url: pcMembershipAdmin.ajax_url,
type: 'POST',
data: formData + '&action=pc_membership_save_plan&nonce=' + pcMembershipAdmin.nonce,
success: function(response) {
if (response.success) {
$('#pc-membership-plan-modal').dialog('close');
window.location.reload();
} else {
alert(pcMembershipAdmin.i18n.error + ': ' + (response.data || 'Unknown error'));
}
},
error: function() {
alert(pcMembershipAdmin.i18n.error);
}
});
},
deletePlan: function(e) {
e.preventDefault();
if (!confirm(pcMembershipAdmin.i18n.confirmDelete)) return;
var planId = $(e.currentTarget).data('plan-id');
$.ajax({
url: pcMembershipAdmin.ajax_url,
type: 'POST',
data: {
action: 'pc_membership_delete_plan',
plan_id: planId,
nonce: pcMembershipAdmin.nonce
},
success: function(response) {
if (response.success) {
window.location.reload();
} else {
alert(pcMembershipAdmin.i18n.error);
}
}
});
},
closePlanModal: function(e) {
if (e) e.preventDefault();
$('#pc-membership-plan-modal').dialog('close');
},
toggleBillingInterval: function() {
var isSubscription = $('#plan_type').val() === '1';
$('#billing_interval_row').toggle(isSubscription);
},
initAccessForm: function() {
this.loadContentOptions();
},
loadContentOptions: function() {
var contentType = $('#access_content_type').val();
var $select = $('#access_content_id');
$select.html('<option value="">Loading...</option>');
var data = {
action: 'pc_membership_get_content_for_rule',
content_type: contentType,
nonce: pcMembershipAdmin.nonce
};
$.ajax({
url: pcMembershipAdmin.ajax_url,
type: 'POST',
data: data,
success: function(response) {
$select.empty();
$select.append('<option value="">-- Select --</option>');
if (response.success && response.data.items) {
$.each(response.data.items, function(i, item) {
$select.append('<option value="' + item.id + '">' + item.title + '</option>');
});
}
}
});
},
saveAccessRule: function(e) {
e.preventDefault();
var $form = $('#pc-membership-access-form');
$.ajax({
url: pcMembershipAdmin.ajax_url,
type: 'POST',
data: $form.serialize() + '&action=pc_membership_save_access_rule&nonce=' + pcMembershipAdmin.nonce,
success: function(response) {
if (response.success) {
window.location.reload();
} else {
alert(pcMembershipAdmin.i18n.error + ': ' + (response.data || 'Unknown error'));
}
}
});
},
deleteAccessRule: function(e) {
e.preventDefault();
if (!confirm('Are you sure you want to delete this rule?')) return;
var ruleId = $(e.currentTarget).data('rule-id');
$.ajax({
url: pcMembershipAdmin.ajax_url,
type: 'POST',
data: {
action: 'pc_membership_delete_access_rule',
rule_id: ruleId,
nonce: pcMembershipAdmin.nonce
},
success: function(response) {
if (response.success) {
window.location.reload();
} else {
alert(pcMembershipAdmin.i18n.error);
}
}
});
},
toggleCustomRedirect: function() {
var redirect = $('#access_redirect').val();
$('#custom_redirect_row').toggle(redirect === 'custom');
}
};
$(document).ready(function() {
PCMembershipAdmin.init();
});
})(jQuery);