Hugging Face admin page. */ class AdminPage { const OPTION_DEFAULT_MODEL = 'hugging_face_default_model'; const OPTION_CUSTOM_MODELS = 'hugging_face_custom_models'; const OPTION_DEFAULT_IMAGE_MODEL = 'hugging_face_default_image_model'; const OPTION_CUSTOM_IMAGE_MODELS = 'hugging_face_custom_image_models'; const OPTION_GROUP = 'hugging_face_settings'; const PAGE_SLUG = 'hugging-face-settings'; /** * Register hooks for the admin page. */ public static function register(): void { add_action( 'admin_menu', array( __CLASS__, 'add_menu_page' ) ); add_action( 'admin_init', array( __CLASS__, 'register_settings' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_admin_assets' ) ); } /** * Add the submenu page under Settings. */ public static function add_menu_page(): void { add_options_page( __( 'Hugging Face', 'ai-provider-for-hugging-face' ), __( 'Hugging Face', 'ai-provider-for-hugging-face' ), 'manage_options', self::PAGE_SLUG, array( __CLASS__, 'render_page' ) ); } /** * Enqueue admin CSS and JS on the plugin settings page only. * * @param string $hook_suffix The current admin page hook suffix. */ public static function enqueue_admin_assets( string $hook_suffix ): void { if ( 'settings_page_' . self::PAGE_SLUG !== $hook_suffix ) { return; } wp_enqueue_style( 'aiprfohu-admin-settings', plugins_url( 'css/admin-settings.css', HUGGING_FACE_PROVIDER_FILE ), array(), HUGGING_FACE_PROVIDER_VERSION ); wp_enqueue_script( 'aiprfohu-admin-settings', plugins_url( 'js/admin-settings.js', HUGGING_FACE_PROVIDER_FILE ), array(), HUGGING_FACE_PROVIDER_VERSION, true ); } /** * Register the settings and fields. */ public static function register_settings(): void { register_setting( self::OPTION_GROUP, self::OPTION_DEFAULT_MODEL, array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ) ); register_setting( self::OPTION_GROUP, self::OPTION_CUSTOM_MODELS, array( 'type' => 'string', 'default' => '', 'sanitize_callback' => array( __CLASS__, 'sanitize_custom_models' ), ) ); register_setting( self::OPTION_GROUP, self::OPTION_DEFAULT_IMAGE_MODEL, array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ) ); register_setting( self::OPTION_GROUP, self::OPTION_CUSTOM_IMAGE_MODELS, array( 'type' => 'string', 'default' => '', 'sanitize_callback' => array( __CLASS__, 'sanitize_custom_models' ), ) ); // Text generation section. add_settings_section( 'hugging_face_model_section', __( 'Text Generation Settings', 'ai-provider-for-hugging-face' ), array( __CLASS__, 'render_section_description' ), self::PAGE_SLUG ); add_settings_field( self::OPTION_DEFAULT_MODEL, __( 'Default Model', 'ai-provider-for-hugging-face' ), array( __CLASS__, 'render_model_field' ), self::PAGE_SLUG, 'hugging_face_model_section' ); add_settings_field( self::OPTION_CUSTOM_MODELS, __( 'Custom Models', 'ai-provider-for-hugging-face' ), array( __CLASS__, 'render_custom_models_field' ), self::PAGE_SLUG, 'hugging_face_model_section' ); // Image generation section. add_settings_section( 'hugging_face_image_model_section', __( 'Image Generation Settings', 'ai-provider-for-hugging-face' ), array( __CLASS__, 'render_image_section_description' ), self::PAGE_SLUG ); add_settings_field( self::OPTION_DEFAULT_IMAGE_MODEL, __( 'Default Image Model', 'ai-provider-for-hugging-face' ), array( __CLASS__, 'render_image_model_field' ), self::PAGE_SLUG, 'hugging_face_image_model_section' ); add_settings_field( self::OPTION_CUSTOM_IMAGE_MODELS, __( 'Custom Image Models', 'ai-provider-for-hugging-face' ), array( __CLASS__, 'render_custom_image_models_field' ), self::PAGE_SLUG, 'hugging_face_image_model_section' ); } /** * Render the text section description. */ public static function render_section_description(): void { echo '
' . esc_html__( 'Choose a default text generation model and manage custom models. Models labeled [Free tier] fit within free allowances; models with pricing per million tokens require account credits.', 'ai-provider-for-hugging-face' ) . '
'; } /** * Render the image section description. */ public static function render_image_section_description(): void { echo '' . esc_html__( 'Choose a default image generation model and manage custom models. Image generation models run on third-party inference providers (such as Fal AI and Replicate) and require pre-paid credits or a card on file on Hugging Face.', 'ai-provider-for-hugging-face' ) . '
'; } /** * Render a model dropdown with custom model optgroup. * * @param string $name The select field name attribute. * @param string $current The currently selected value. * @param array[] $models The model list (custom first, then built-in). * @param string $id The select field id attribute. */ private static function render_model_dropdown( string $name, string $current, array $models, string $id ): void { echo ''; } /** * Render the custom model input + tag list UI. * * @param string $hidden_name The hidden input name for form submission. * @param string $hidden_id The hidden input id. * @param string $select_id The associated dropdown id to sync with. * @param string $browse_url URL to browse models on HuggingFace. * @param string $placeholder Placeholder text for the input field. */ private static function render_custom_model_input( string $hidden_name, string $hidden_id, string $select_id, string $browse_url, string $placeholder ): void { $value = get_option( $hidden_name, '' ); $models = array_filter( array_map( 'trim', explode( "\n", (string) $value ) ) ); ?>Browse models on HuggingFace', 'ai-provider-for-hugging-face' ), esc_url( $browse_url ) ), array( 'a' => array( 'href' => array(), 'target' => array() ) ) ); ?>
' . esc_html__( 'Showing top 20 most popular models. Add more via Custom Image Models below.', 'ai-provider-for-hugging-face' ) . '
'; } /** * Render the custom image models field. */ public static function render_custom_image_models_field(): void { self::render_custom_model_input( self::OPTION_CUSTOM_IMAGE_MODELS, 'aiprfohu-custom-image-models', 'aiprfohu-image-model-select', 'https://huggingface.co/models?inference_provider=all&pipeline_tag=text-to-image&sort=likes&direction=-1', 'org/model-name' ); } /** * Render the settings page. */ public static function render_page(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } echo '' . esc_html__( 'Getting Started with Hugging Face Inference:', 'ai-provider-for-hugging-face' ) . '
'; echo '[Free tier] run within free quotas. Models marked [Paid] or displaying per-token pricing require an active balance or card on file at Hugging Face Billing.', 'ai-provider-for-hugging-face' ),
array(
'code' => array(),
'a' => array(
'href' => array(),
'target' => array(),
'rel' => array(),
),
)
),
'https://huggingface.co/settings/billing'
) . '