'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 ' ';
echo '
' . esc_html__( 'Your Stripe publishable key from the Stripe dashboard.', 'pc-membership-abc123' ) . '
';
}
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 ' ';
echo '' . esc_html__( 'Your Stripe secret key from the Stripe dashboard.', 'pc-membership-abc123' ) . '
';
}
public static function field_webhook_secret() {
$options = get_option( 'pc_membership_options' );
$value = isset( $options['webhook_secret'] ) ? esc_attr( $options['webhook_secret'] ) : '';
echo ' ';
echo '' . esc_html__( 'Webhook secret for handling Stripe events.', 'pc-membership-abc123' ) . '
';
}
public static function field_mode() {
$options = get_option( 'pc_membership_options' );
$mode = isset( $options['mode'] ) ? $options['mode'] : 'test';
echo '';
echo '' . esc_html__( 'Test Mode', 'pc-membership-abc123' ) . ' ';
echo '' . esc_html__( 'Live Mode', 'pc-membership-abc123' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Use test mode for development. Switch to live mode when ready for production.', 'pc-membership-abc123' ) . '
';
}
public static function field_currency() {
$options = get_option( 'pc_membership_options' );
$currency = isset( $options['currency'] ) ? $options['currency'] : 'usd';
echo '';
echo 'USD ($) ';
echo 'EUR (€) ';
echo 'GBP (£) ';
echo ' ';
}
public static function dashboard_page() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'Unauthorized', 'pc-membership-abc123' ) );
}
?>
get_results( "SELECT * FROM {$wpdb->prefix}pc_membership_plans ORDER BY id DESC" );
?>
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 ) );
?>
id ); ?>
name ); ?>
price ) ); ?>
$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' ) );
?>
$label ) :
$selected = isset( $options[ $key . '_page_id' ] ) ? absint( $options[ $key . '_page_id' ] ) : 0;
?>
ID ); ?>>
post_title ); ?>
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 '' . esc_html__( 'Membership pages created successfully!', 'pc-membership-abc123' ) . '
';
}
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" );
?>
name ); ?>
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' ) );
?>
(' . esc_html( $rule->content_type ) . ')'; ?>
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' ) );
}
?>
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' ),
),
) );
}
}