AffClix settings screen. */ defined('ABSPATH') || exit; /** * A single settings page: credentials, connection status, webhook status. * * Rendered by hand rather than through WC_Settings_Page because the screen is * mostly status readout — what the handshake returned, whether the webhooks are * still alive — which the settings API has no vocabulary for. */ class Affclix_Admin { const SLUG = 'affclix-settings'; const NONCE = 'affclix_save_settings'; /** * Hook up. */ public static function init() { add_action('admin_menu', [__CLASS__, 'menu']); add_action('admin_post_affclix_save', [__CLASS__, 'handle_save']); add_filter('plugin_action_links_'.plugin_basename(AFFCLIX_WC_FILE), [__CLASS__, 'action_links']); } /** * Register the submenu under WooCommerce. */ public static function menu() { add_submenu_page( 'woocommerce', __('AffClix', 'affclix-for-woocommerce'), __('AffClix', 'affclix-for-woocommerce'), 'manage_woocommerce', self::SLUG, [__CLASS__, 'render'] ); } /** * Add a Settings link on the plugins list. * * @param string[] $links Existing links. * @return string[] */ public static function action_links($links) { $url = admin_url('admin.php?page='.self::SLUG); array_unshift( $links, ''.esc_html__('Settings', 'affclix-for-woocommerce').'' ); return $links; } /** * Save credentials, then connect and sync webhooks in one round trip. */ public static function handle_save() { if (! current_user_can('manage_woocommerce')) { wp_die(esc_html__('You are not allowed to change these settings.', 'affclix-for-woocommerce')); } check_admin_referer(self::NONCE); $existing = Affclix_Settings::credentials(); // The secret field renders empty and is only overwritten when the // merchant actually types a new one — otherwise saving any other field // on this page would wipe the stored secret. $posted_secret = isset($_POST['api_secret']) ? sanitize_text_field(wp_unslash($_POST['api_secret'])) : ''; Affclix_Settings::save_credentials( [ 'api_host' => isset($_POST['api_host']) ? sanitize_text_field(wp_unslash($_POST['api_host'])) : 'api.affclix.com', 'domain_id' => isset($_POST['domain_id']) ? sanitize_text_field(wp_unslash($_POST['domain_id'])) : '', 'api_key' => isset($_POST['api_key']) ? sanitize_text_field(wp_unslash($_POST['api_key'])) : '', 'api_secret' => $posted_secret !== '' ? $posted_secret : $existing['api_secret'], 'enabled' => ! empty($_POST['enabled']), 'allow_rest' => ! empty($_POST['allow_rest']), 'load_sdk' => ! empty($_POST['load_sdk']), ] ); $notice = 'saved'; if (! empty($_POST['enabled'])) { $result = Affclix_Client::connect(); if ($result['ok']) { $sync = Affclix_Webhook::sync(); $notice = empty($sync['errors']) ? 'connected' : 'webhook_error'; } else { $notice = 'connect_error'; set_transient('affclix_last_error', $result['message'], 5 * MINUTE_IN_SECONDS); } } else { Affclix_Webhook::delete_all(); Affclix_Rest_Key::revoke(); } wp_safe_redirect(admin_url('admin.php?page='.self::SLUG.'&affclix_notice='.$notice)); exit; } /** * Render the settings screen. */ public static function render() { $creds = Affclix_Settings::credentials(); $manifest = Affclix_Settings::manifest(); $attr = Affclix_Settings::attribution(); $notice = isset($_GET['affclix_notice']) ? sanitize_key(wp_unslash($_GET['affclix_notice'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. ?>
|
|
|
|
|
|
|
✓ — |
%2$s