From 31e9fb864c1550e17602454f1fcc436548de95bd Mon Sep 17 00:00:00 2001 From: David Parker Date: Thu, 24 Oct 2024 10:14:52 -0400 Subject: [PATCH] Improved error handling --- adminpages/subscriptions.php | 158 ++++++++++++++++++++++++++--------- 1 file changed, 118 insertions(+), 40 deletions(-) diff --git a/adminpages/subscriptions.php b/adminpages/subscriptions.php index 76af9fecc..6564b50ae 100644 --- a/adminpages/subscriptions.php +++ b/adminpages/subscriptions.php @@ -27,46 +27,96 @@ // Process linking a subscription. if ( isset( $_REQUEST['action'] ) && 'link' === $_REQUEST['action'] ) { if ( ! empty( $_POST ) && check_admin_referer( 'link', 'pmpro_subscriptions_nonce' ) ) { - // Create a new subscription. - $create_subscription_args = array( - 'user_id' => sanitize_text_field( $_POST['user_id'] ), - 'membership_level_id' => sanitize_text_field( $_POST['membership_level_id'] ), - 'gateway' => sanitize_text_field( $_POST['gateway'] ), - 'gateway_environment' => sanitize_text_field( $_POST['gateway_environment'] ), - 'subscription_transaction_id' => sanitize_text_field( $_POST['subscription_transaction_id'] ), - 'status' => 'active', - ); - $new_subscription = PMPro_Subscription::create( $create_subscription_args ); - - if ( ! empty( $new_subscription ) ) { - // Show a success message. - $pmpro_msg = esc_html__( 'Subscription linked successfully.', 'paid-memberships-pro' ); - $pmpro_msgt = 'pmpro_success'; + // Make sure all required fields are set. + if ( empty( $_POST['subscription_transaction_id'] ) || empty( $_POST['gateway'] ) || empty( $_POST['gateway_environment'] ) || empty( $_POST['user_id'] ) || empty( $_POST['membership_level_id'] ) ) { + $pmpro_msg = esc_html__( 'All fields are required.', 'paid-memberships-pro' ); + $pmpro_msgt = 'pmpro_error'; + } - // Go to the "view" page. - unset( $_REQUEST['action'] ); - } else { - // Show an error message. - $pmpro_msg = esc_html__( 'Error linking subscription. This subscription may already exist on your website.', 'paid-memberships-pro' ); + // Make sure that the user ID is valid. + if ( ! get_userdata( sanitize_text_field( $_POST['user_id'] ) ) ) { + $pmpro_msg = esc_html__( 'Invalid user ID.', 'paid-memberships-pro' ); $pmpro_msgt = 'pmpro_error'; } + + // Make sure that the membership level ID is valid. + if ( ! pmpro_getLevel( sanitize_text_field( $_POST['membership_level_id'] ) ) ) { + $pmpro_msg = esc_html__( 'Invalid membership level ID.', 'paid-memberships-pro' ); + $pmpro_msgt = 'pmpro_error'; + } + + // Check if this subscription already exists. + if ( 'pmpro_error' !== $pmpro_msgt ) { + $test_subscription = PMPro_Subscription::get_subscription_from_subscription_transaction_id( sanitize_text_field( $_POST['subscription_transaction_id'] ), sanitize_text_field( $_POST['gateway'] ), sanitize_text_field( $_POST['gateway_environment'] ) ); + + if ( ! empty( $test_subscription ) ) { + $pmpro_msg = esc_html__( 'This subscription already exists on your website.', 'paid-memberships-pro' ); + $pmpro_msgt = 'pmpro_error'; + } + } + + // Create a new subscription. + if ( 'pmpro_error' !== $pmpro_msgt ) { + $create_subscription_args = array( + 'user_id' => sanitize_text_field( $_POST['user_id'] ), + 'membership_level_id' => sanitize_text_field( $_POST['membership_level_id'] ), + 'gateway' => sanitize_text_field( $_POST['gateway'] ), + 'gateway_environment' => sanitize_text_field( $_POST['gateway_environment'] ), + 'subscription_transaction_id' => sanitize_text_field( $_POST['subscription_transaction_id'] ), + 'status' => 'active', + ); + $subscription = PMPro_Subscription::create( $create_subscription_args ); + + if ( ! empty( $subscription ) ) { + // Show a success message. + $pmpro_msg = esc_html__( 'Subscription linked successfully.', 'paid-memberships-pro' ); + $pmpro_msgt = 'pmpro_success'; + + // Go to the "view" page. + unset( $_REQUEST['action'] ); + } else { + // Show an error message. + $pmpro_msg = esc_html__( 'Error linking subscription.', 'paid-memberships-pro' ); + $pmpro_msgt = 'pmpro_error'; + } + } } } // Process editing a subscription. if ( ! empty( $subscription ) && isset( $_REQUEST['action'] ) && 'edit' === $_REQUEST['action'] ) { if ( ! empty( $_POST ) && check_admin_referer( 'edit', 'pmpro_subscriptions_nonce' ) ) { + // Make sure all required fields are set. + if ( empty( $_POST['user_id'] ) || empty( $_POST['membership_level_id'] ) ) { + $pmpro_msg = esc_html__( 'All fields are required.', 'paid-memberships-pro' ); + $pmpro_msgt = 'pmpro_error'; + } + + // Make sure that the user ID is valid. + if ( ! get_userdata( sanitize_text_field( $_POST['user_id'] ) ) ) { + $pmpro_msg = esc_html__( 'Invalid user ID.', 'paid-memberships-pro' ); + $pmpro_msgt = 'pmpro_error'; + } + + // Make sure that the membership level ID is valid. + if ( ! pmpro_getLevel( sanitize_text_field( $_POST['membership_level_id'] ) ) ) { + $pmpro_msg = esc_html__( 'Invalid membership level ID.', 'paid-memberships-pro' ); + $pmpro_msgt = 'pmpro_error'; + } + // Update the subscription. - $subscription->set( 'user_id', sanitize_text_field( $_POST['user_id'] ) ); - $subscription->set( 'membership_level_id', sanitize_text_field( $_POST['membership_level_id'] ) ); - $subscription->save(); + if ( 'pmpro_error' !== $pmpro_msgt ) { + $subscription->set( 'user_id', sanitize_text_field( $_POST['user_id'] ) ); + $subscription->set( 'membership_level_id', sanitize_text_field( $_POST['membership_level_id'] ) ); + $subscription->save(); - // Show a success message. - $pmpro_msg = esc_html__( 'Subscription updated successfully.', 'paid-memberships-pro' ); - $pmpro_msgt = 'pmpro_success'; + // Show a success message. + $pmpro_msg = esc_html__( 'Subscription updated successfully.', 'paid-memberships-pro' ); + $pmpro_msgt = 'pmpro_success'; - // Go back to the "view" page. - unset( $_REQUEST['action'] ); + // Go back to the "view" page. + unset( $_REQUEST['action'] ); + } } } @@ -78,15 +128,33 @@ if ( isset( $_REQUEST['action'] ) && 'link' === $_REQUEST['action'] ) { // Link a subscription. + $subscription_transaction_id = ! empty( $_POST['subscription_transaction_id'] ) ? sanitize_text_field( $_POST['subscription_transaction_id'] ) : ''; + $gateway = ! empty( $_POST['gateway'] ) ? sanitize_text_field( $_POST['gateway'] ) : get_option( 'pmpro_gateway', '' ); + $gateway_environment = ! empty( $_POST['gateway_environment'] ) ? sanitize_text_field( $_POST['gateway_environment'] ) : get_option( 'pmpro_gateway_environment', '' ); + $user_id = ! empty( $_POST['user_id'] ) ? sanitize_text_field( $_POST['user_id'] ) : ''; + $membership_level_id = ! empty( $_POST['membership_level_id'] ) ? sanitize_text_field( $_POST['membership_level_id'] ) : ''; ?>

+ + + + +
@@ -95,13 +163,12 @@ @@ -140,7 +203,7 @@ - + @@ -156,6 +219,8 @@ get_user_id() : sanitize_text_field( $_POST['user_id'] ); + $membership_level_id = empty( $_POST['membership_level_id'] ) ? $subscription->get_membership_level_id() : sanitize_text_field( $_POST['membership_level_id'] ); ?>

+ + + + +
- +
-
- +
@@ -186,7 +264,7 @@ class="page-title-action pmpro-has-icon pmpro-has-icon-visibility"> @@ -202,7 +280,7 @@ class="page-title-action pmpro-has-icon pmpro-has-icon-visibility"> - +
- +