$items['customer-logout'] ) : array(); unset( $items['customer-logout'] ); return $items + array( self::ENDPOINT => __( 'Subscriptions', 'agentaos-payments' ) ) + $logout; } public static function render() { $orders = self::subscriptions( get_current_user_id() ); if ( ! $orders ) { echo '

' . esc_html__( 'You have no subscriptions.', 'agentaos-payments' ) . '

'; return; } echo ''; foreach ( $orders as $order ) { $items = $order->get_items(); $item = reset( $items ); $status = (string) $order->get_meta( '_agentaos_subscription_status' ); $status = '' === $status ? 'active' : $status; $canceling = 'yes' === $order->get_meta( '_agentaos_cancel_at_period_end' ); $period = (string) $order->get_meta( '_agentaos_period_end' ); $date = '' === $period ? '' : date_i18n( get_option( 'date_format' ), strtotime( $period ) ); echo ''; echo ''; echo ''; } echo '
' . esc_html__( 'Plan', 'agentaos-payments' ) . '' . esc_html__( 'Status', 'agentaos-payments' ) . '' . esc_html__( 'Renews or ends', 'agentaos-payments' ) . '
' . esc_html( $item ? $item->get_name() : '' ) . '' . esc_html( $canceling ? __( 'Cancels at period end', 'agentaos-payments' ) : ucfirst( str_replace( '_', ' ', $status ) ) ) . '' . esc_html( $date ) . ''; if ( ! $canceling && in_array( $status, array( 'active', 'trialing', 'past_due' ), true ) ) { echo '
'; wp_nonce_field( 'agentaos_cancel_' . $order->get_id() ); echo ''; echo '
'; } echo '
'; } public static function handle_cancel() { if ( empty( $_POST['agentaos_cancel'] ) || ! is_user_logged_in() ) { return; } $order_id = absint( $_POST['agentaos_cancel'] ); check_admin_referer( 'agentaos_cancel_' . $order_id ); $order = wc_get_order( $order_id ); if ( ! $order || (int) $order->get_customer_id() !== get_current_user_id() || '' === (string) $order->get_meta( AgentaOS_Subscriptions::META_SUB_ID ) ) { wc_add_notice( __( 'That subscription was not found.', 'agentaos-payments' ), 'error' ); return; } $gateways = WC()->payment_gateways()->payment_gateways(); try { $gateways['agentaos']->api()->cancel_subscription( (string) $order->get_meta( AgentaOS_Subscriptions::META_SUB_ID ) ); } catch ( Exception $e ) { wc_add_notice( __( 'We could not cancel the subscription. Please try again or contact the store.', 'agentaos-payments' ), 'error' ); return; } $order->update_meta_data( '_agentaos_cancel_at_period_end', 'yes' ); $order->save(); $order->add_order_note( __( 'The customer canceled the subscription from My Account; it ends at the end of the current period.', 'agentaos-payments' ) ); wc_add_notice( __( 'Your subscription is canceled. You keep access until the end of the current period.', 'agentaos-payments' ) ); wp_safe_redirect( wc_get_account_endpoint_url( self::ENDPOINT ) ); exit; } /** * @param int $customer_id Customer. * @return WC_Order[] The orders that started this customer's subscriptions. */ private static function subscriptions( $customer_id ) { if ( ! $customer_id ) { return array(); } $orders = wc_get_orders( array( 'limit' => 50, 'customer_id' => $customer_id, 'payment_method' => 'agentaos', 'meta_key' => AgentaOS_Subscriptions::META_SUB_ID, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_compare' => 'EXISTS', ) ); return array_values( array_filter( $orders, static function ( $order ) { return '' === (string) $order->get_meta( '_agentaos_parent_order' ); } ) ); } }