init_hooks(); } /** * Initialize hooks. */ private function init_hooks() { add_action( 'admin_menu', array( $this, 'add_admin_menu' ), 20 ); // Priority 20 to run after other submenus. add_action( 'admin_post_afridirect_save_settings', array( $this, 'handle_save' ) ); } /** * Add admin menu. */ public function add_admin_menu() { $menu_manager = AfriDirect_Payment_Admin_Menu::instance(); add_submenu_page( $menu_manager->get_menu_slug(), __( 'Settings', 'afridirect-payment' ), __( 'Settings', 'afridirect-payment' ), $menu_manager->get_capability(), 'afridirect-settings', array( $this, 'render_page' ) ); } /** * The WooCommerce gateway instance, if WooCommerce is active. * * @return WC_Payment_Gateway|null */ private function get_gateway() { if ( ! class_exists( 'WooCommerce' ) || ! function_exists( 'WC' ) || ! WC()->payment_gateways() ) { return null; } $gateways = WC()->payment_gateways()->payment_gateways(); return isset( $gateways['afridirect_payment'] ) ? $gateways['afridirect_payment'] : null; } /** * Render the settings page. */ public function render_page() { $menu_manager = AfriDirect_Payment_Admin_Menu::instance(); if ( ! current_user_can( $menu_manager->get_capability() ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'afridirect-payment' ) ); } $gateway = $this->get_gateway(); $gateway_values = array( 'enabled' => false, 'title' => '', 'description' => '', 'form_id' => 0, ); if ( $gateway ) { $gateway_values = array( 'enabled' => 'yes' === $gateway->get_option( 'enabled' ), 'title' => $gateway->get_option( 'title' ), 'description' => $gateway->get_option( 'description' ), 'form_id' => (int) $gateway->get_option( 'default_form_id' ), ); } $forms = AfriDirect_Payment_Dashboard_Data::get_forms( 100 ); $delete_data = (bool) get_option( 'afridirect_delete_data_on_uninstall', false ); $notify_admin = (bool) get_option( 'afridirect_notify_admin', 1 ); $notify_email = (string) get_option( 'afridirect_notify_email', '' ); $notify_started = (bool) get_option( 'afridirect_notify_started', 1 ); $blocks_active = function_exists( 'afridirect_is_checkout_blocks_active' ) && afridirect_is_checkout_blocks_active(); $switch_url = wp_nonce_url( admin_url( 'admin-post.php?action=afridirect_switch_to_classic_checkout' ), 'afridirect_switch_to_classic_checkout' ); // One-time "saved" confirmation after the redirect from handle_save(). Display only. // phpcs:ignore WordPress.Security.NonceVerification.Recommended $saved = isset( $_GET['afd_saved'] ) && '1' === $_GET['afd_saved']; include AFRIDIRECT_PAYMENT_PLUGIN_DIR . 'admin/views/settings.php'; } /** * Save the settings form. */ public function handle_save() { $menu_manager = AfriDirect_Payment_Admin_Menu::instance(); if ( ! current_user_can( $menu_manager->get_capability() ) ) { wp_die( esc_html__( 'You do not have permission to change these settings.', 'afridirect-payment' ), '', array( 'response' => 403 ) ); } check_admin_referer( 'afridirect_save_settings', 'afridirect_settings_nonce' ); $gateway = $this->get_gateway(); if ( $gateway ) { $enabled = isset( $_POST['afridirect_gateway_enabled'] ) ? 'yes' : 'no'; $title = isset( $_POST['afridirect_gateway_title'] ) ? sanitize_text_field( wp_unslash( $_POST['afridirect_gateway_title'] ) ) : ''; $description = isset( $_POST['afridirect_gateway_description'] ) ? sanitize_textarea_field( wp_unslash( $_POST['afridirect_gateway_description'] ) ) : ''; $form_id = isset( $_POST['afridirect_gateway_form_id'] ) ? absint( wp_unslash( $_POST['afridirect_gateway_form_id'] ) ) : 0; // Only accept an existing payment form. if ( $form_id && 'afridirect_form' !== get_post_type( $form_id ) ) { $form_id = 0; } if ( '' === $title ) { $title = __( 'Pay directly', 'afridirect-payment' ); } $gateway->update_option( 'enabled', $enabled ); $gateway->update_option( 'title', $title ); $gateway->update_option( 'description', $description ); $gateway->update_option( 'default_form_id', $form_id ? (string) $form_id : '' ); } update_option( 'afridirect_delete_data_on_uninstall', isset( $_POST['afridirect_delete_data'] ) ? 1 : 0 ); update_option( 'afridirect_notify_admin', isset( $_POST['afridirect_notify_admin'] ) ? 1 : 0 ); update_option( 'afridirect_notify_started', isset( $_POST['afridirect_notify_started'] ) ? 1 : 0 ); $notify_email = isset( $_POST['afridirect_notify_email'] ) ? sanitize_email( wp_unslash( $_POST['afridirect_notify_email'] ) ) : ''; update_option( 'afridirect_notify_email', is_email( $notify_email ) ? $notify_email : '' ); wp_safe_redirect( add_query_arg( 'afd_saved', '1', AfriDirect_Payment_Admin_UI::page_url( 'afridirect-settings' ) ) ); exit; } }