init_hooks(); } /** * Initialize hooks. */ private function init_hooks() { add_action( 'admin_post_afridirect_switch_to_classic_checkout', array( $this, 'handle_switch_to_classic_checkout' ) ); } /** * Handle switching to classic checkout. */ public function handle_switch_to_classic_checkout() { // Check capability. $capability = class_exists( 'WooCommerce' ) ? 'manage_woocommerce' : 'manage_options'; if ( ! current_user_can( $capability ) ) { wp_die( esc_html__( 'You do not have permission to perform this action.', 'afridirect-payment' ) ); } // Verify nonce. if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'afridirect_switch_to_classic_checkout' ) ) { wp_die( esc_html__( 'Security check failed.', 'afridirect-payment' ) ); } // Check if WooCommerce is active. if ( ! class_exists( 'WooCommerce' ) ) { wp_die( esc_html__( 'WooCommerce is not active.', 'afridirect-payment' ) ); } // Find or create Checkout page. $checkout_page_id = get_option( 'woocommerce_checkout_page_id', 0 ); $checkout_page = null; if ( $checkout_page_id ) { $checkout_page = get_post( $checkout_page_id ); if ( ! $checkout_page || 'page' !== $checkout_page->post_type ) { $checkout_page = null; } } if ( ! $checkout_page ) { // Check if a page with slug 'checkout' exists. $existing = get_page_by_path( 'checkout', OBJECT, 'page' ); if ( $existing ) { $checkout_page = $existing; } else { // Create new checkout page. $checkout_page_id = wp_insert_post( array( 'post_title' => __( 'Checkout', 'afridirect-payment' ), 'post_name' => 'checkout', 'post_content' => '[woocommerce_checkout]', 'post_status' => 'publish', 'post_type' => 'page', ) ); if ( is_wp_error( $checkout_page_id ) ) { wp_die( esc_html__( 'Failed to create checkout page.', 'afridirect-payment' ) ); } $checkout_page = get_post( $checkout_page_id ); } } if ( ! $checkout_page ) { wp_die( esc_html__( 'Failed to find or create checkout page.', 'afridirect-payment' ) ); } // Update checkout page content to use classic shortcode if needed. if ( false === strpos( $checkout_page->post_content, '[woocommerce_checkout]' ) ) { wp_update_post( array( 'ID' => $checkout_page->ID, 'post_content' => '[woocommerce_checkout]', ) ); } // Set WooCommerce checkout page option. update_option( 'woocommerce_checkout_page_id', $checkout_page->ID ); // Find or create Cart page. $cart_page_id = get_option( 'woocommerce_cart_page_id', 0 ); if ( ! $cart_page_id ) { $existing_cart = get_page_by_path( 'cart', OBJECT, 'page' ); if ( $existing_cart ) { update_option( 'woocommerce_cart_page_id', $existing_cart->ID ); } else { $cart_page_id = wp_insert_post( array( 'post_title' => __( 'Cart', 'afridirect-payment' ), 'post_name' => 'cart', 'post_content' => '[woocommerce_cart]', 'post_status' => 'publish', 'post_type' => 'page', ) ); if ( ! is_wp_error( $cart_page_id ) ) { update_option( 'woocommerce_cart_page_id', $cart_page_id ); } } } // Redirect back to Overview with success message. $menu_manager = AfriDirect_Payment_Admin_Menu::instance(); $redirect_url = add_query_arg( array( 'page' => $menu_manager->get_menu_slug(), 'afridirect_switched' => '1', ), admin_url( 'admin.php' ) ); wp_safe_redirect( $redirect_url ); exit; } }