403 ) ); } check_admin_referer( 'aivastark_personalize' ); $enable = ! empty( $_POST['enable'] ); Aivastark_Widget_Options::update( array( 'personalize_users' => $enable ) ); wp_safe_redirect( add_query_arg( 'aivastark_notice', $enable ? 'personalize_on' : 'personalize_off', admin_url( 'admin.php?page=' . self::SLUG_HOME ) ) ); exit; } public static function register() { add_menu_page( __( 'Aivastark', 'aivastark-widget' ), __( 'Aivastark', 'aivastark-widget' ), 'manage_options', self::SLUG_HOME, array( __CLASS__, 'render_home' ), 'dashicons-format-chat', 58 ); add_submenu_page( self::SLUG_HOME, __( 'Home', 'aivastark-widget' ), __( 'Home', 'aivastark-widget' ), 'manage_options', self::SLUG_HOME, array( __CLASS__, 'render_home' ) ); add_submenu_page( self::SLUG_HOME, __( 'Customize Widget', 'aivastark-widget' ), __( 'Customize Widget', 'aivastark-widget' ), 'manage_options', self::SLUG_CUSTOMIZE, array( __CLASS__, 'render_customize' ) ); add_submenu_page( self::SLUG_HOME, __( 'Settings', 'aivastark-widget' ), __( 'Settings', 'aivastark-widget' ), 'manage_options', self::SLUG_SETTINGS, array( __CLASS__, 'render_settings' ) ); } /** * Only load the admin CSS/JS on our own screens. */ public static function enqueue_assets( $hook ) { if ( false === strpos( (string) $hook, 'aivastark' ) ) { return; } wp_enqueue_style( 'aivastark-admin', AIVASTARK_WIDGET_URL . 'admin/assets/admin.css', array(), AIVASTARK_WIDGET_VERSION ); wp_enqueue_script( 'aivastark-admin', AIVASTARK_WIDGET_URL . 'admin/assets/admin.js', array(), AIVASTARK_WIDGET_VERSION, true ); } /** * Surface connect/disconnect outcomes as dismissible notices on our pages. */ public static function maybe_notices() { $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; if ( false === strpos( $page, 'aivastark' ) ) { return; } if ( isset( $_GET['aivastark_notice'] ) ) { $notice = sanitize_text_field( wp_unslash( $_GET['aivastark_notice'] ) ); $msg = ''; if ( 'connected' === $notice ) { $msg = __( 'Connected to Aivastark. You can now customize your widget below.', 'aivastark-widget' ); } elseif ( 'disconnected' === $notice ) { $msg = __( 'Disconnected from Aivastark.', 'aivastark-widget' ); } elseif ( 'personalize_on' === $notice ) { $msg = __( 'Personalization is on. The assistant now knows who is logged in.', 'aivastark-widget' ); } elseif ( 'personalize_off' === $notice ) { $msg = __( 'Personalization is off. Logged-in visitors are treated as anonymous again.', 'aivastark-widget' ); } if ( $msg ) { printf( '

%s

', esc_html( $msg ) ); } } if ( isset( $_GET['aivastark_error'] ) ) { $code = sanitize_text_field( wp_unslash( $_GET['aivastark_error'] ) ); $map = array( 'denied' => __( 'Connection cancelled.', 'aivastark-widget' ), 'missing' => __( 'The connection response was incomplete. Please try again.', 'aivastark-widget' ), 'state' => __( 'The connection link expired or was invalid. Please click Connect again.', 'aivastark-widget' ), 'exchange' => __( 'Could not complete the connection with Aivastark. Please try again.', 'aivastark-widget' ), ); $msg = isset( $map[ $code ] ) ? $map[ $code ] : __( 'Something went wrong connecting to Aivastark.', 'aivastark-widget' ); printf( '

%s

', esc_html( $msg ) ); } } public static function render_home() { if ( ! current_user_can( 'manage_options' ) ) { return; } require AIVASTARK_WIDGET_PATH . 'admin/views/page-home.php'; } public static function render_customize() { if ( ! current_user_can( 'manage_options' ) ) { return; } require AIVASTARK_WIDGET_PATH . 'admin/views/page-customize.php'; } public static function render_settings() { if ( ! current_user_can( 'manage_options' ) ) { return; } require AIVASTARK_WIDGET_PATH . 'admin/views/page-settings.php'; } }