status = $status; $this->connect = $connect; } /** * Register hooks. * * @return void */ public function init() { add_action( 'admin_menu', array( $this, 'register_menu' ) ); add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_notices', array( $this, 'render_notices' ) ); } /** * Add the settings page under the WooCommerce menu. * * @return void */ public function register_menu() { add_submenu_page( 'woocommerce', __( 'aisthetix Try-On', 'aisthetix-virtual-try-on' ), __( 'aisthetix Try-On', 'aisthetix-virtual-try-on' ), 'manage_woocommerce', self::PAGE_SLUG, array( $this, 'render_page' ) ); } /** * Register the settings field group with sanitisation. * * @return void */ public function register_settings() { register_setting( self::OPTION_GROUP, Plugin::OPTION_SETTINGS, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_settings' ), 'default' => array( 'placement' => 'after_add_to_cart', 'button_text' => '', 'enable_analytics' => false, ), ) ); } /** * Sanitise the settings array on save. * * @param array $input Raw input. * @return array Clean settings. */ public function sanitize_settings( $input ) { $clean = array(); $allowed_placements = array( 'after_add_to_cart', 'manual' ); $placement = isset( $input['placement'] ) ? sanitize_text_field( $input['placement'] ) : 'after_add_to_cart'; $clean['placement'] = in_array( $placement, $allowed_placements, true ) ? $placement : 'after_add_to_cart'; $clean['button_text'] = isset( $input['button_text'] ) ? sanitize_text_field( $input['button_text'] ) : ''; $clean['enable_analytics'] = ! empty( $input['enable_analytics'] ); return $clean; } /** * Render queued admin notices from the Connect flow. * * @return void */ public function render_notices() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only notice flag set by our own redirect. $notice = isset( $_GET['aisthetix_notice'] ) ? sanitize_text_field( wp_unslash( $_GET['aisthetix_notice'] ) ) : ''; if ( '' === $notice ) { return; } $map = array( 'connect_success' => array( 'success', __( 'Connected to aisthetix successfully.', 'aisthetix-virtual-try-on' ) ), 'disconnected' => array( 'success', __( 'Disconnected from aisthetix.', 'aisthetix-virtual-try-on' ) ), 'connect_state_error' => array( 'error', __( 'Connect failed: the security check did not match. Please try again.', 'aisthetix-virtual-try-on' ) ), 'connect_exchange_error' => array( 'error', __( 'Connect failed: could not exchange the code for keys. Please try again.', 'aisthetix-virtual-try-on' ) ), ); if ( ! isset( $map[ $notice ] ) ) { return; } list( $type, $message ) = $map[ $notice ]; printf( '
%2$s