'string', 'sanitize_callback' => 'esc_url_raw', 'default' => '', ) ); register_setting( 'ai_summary_generator_settings', 'ai_summary_generator_api_token', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => '', ) ); register_setting( 'ai_summary_generator_settings', 'ai_summary_generator_model_name', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => '', ) ); register_setting( 'ai_summary_generator_settings', 'ai_summary_generator_prompt', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_textarea_field', 'default' => __( 'Your task is to generate an English summary of the article. Please generate a summary of 100 words or less based on the content of the following article, and output the summary directly. Only Plain Text is allowed.', 'ai-summary-generator' ), ) ); register_setting( 'ai_summary_generator_settings', 'ai_summary_generator_homepage_override', array( 'type' => 'string', 'sanitize_callback' => array( __CLASS__, 'sanitize_yes_no' ), 'default' => 'no', ) ); register_setting( 'ai_summary_generator_settings', 'ai_summary_generator_update_on_post_update', array( 'type' => 'string', 'sanitize_callback' => array( __CLASS__, 'sanitize_yes_no' ), 'default' => 'no', ) ); register_setting( 'ai_summary_generator_settings', 'ai_summary_generator_max_retries', array( 'type' => 'integer', 'sanitize_callback' => array( __CLASS__, 'sanitize_retry_count' ), 'default' => 3, ) ); add_settings_section( 'ai_summary_generator_main_section', __( 'API Configuration', 'ai-summary-generator' ), null, 'ai-summary-generator-settings' ); // Add settings fields with individual callbacks. add_settings_field( 'ai_summary_generator_api_url', __( 'API URL', 'ai-summary-generator' ), array( __CLASS__, 'api_url_callback' ), 'ai-summary-generator-settings', 'ai_summary_generator_main_section' ); add_settings_field( 'ai_summary_generator_api_token', __( 'API Token', 'ai-summary-generator' ), array( __CLASS__, 'api_token_callback' ), 'ai-summary-generator-settings', 'ai_summary_generator_main_section' ); add_settings_field( 'ai_summary_generator_model_name', __( 'Model Name', 'ai-summary-generator' ), array( __CLASS__, 'model_name_callback' ), 'ai-summary-generator-settings', 'ai_summary_generator_main_section' ); add_settings_field( 'ai_summary_generator_prompt', __( 'Prompt', 'ai-summary-generator' ), array( __CLASS__, 'prompt_callback' ), 'ai-summary-generator-settings', 'ai_summary_generator_main_section' ); add_settings_field( 'ai_summary_generator_css_classes_info', __( 'CSS Styling Information', 'ai-summary-generator' ), array( __CLASS__, 'css_classes_info_callback' ), 'ai-summary-generator-settings', 'ai_summary_generator_main_section' ); add_settings_field( 'ai_summary_generator_homepage_override', __( 'Override Homepage Summary', 'ai-summary-generator' ), array( __CLASS__, 'homepage_override_callback' ), 'ai-summary-generator-settings', 'ai_summary_generator_main_section' ); add_settings_field( 'ai_summary_generator_update_on_post_update', __( 'Update Summary on Post Update', 'ai-summary-generator' ), array( __CLASS__, 'update_on_post_update_callback' ), 'ai-summary-generator-settings', 'ai_summary_generator_main_section' ); add_settings_field( 'ai_summary_generator_max_retries', __( 'Max Retry Attempts', 'ai-summary-generator' ), array( __CLASS__, 'max_retries_callback' ), 'ai-summary-generator-settings', 'ai_summary_generator_main_section' ); } /** * Settings field callbacks */ public static function api_url_callback() { $value = get_option( 'ai_summary_generator_api_url', '' ); echo ''; echo '

' . esc_html__( 'Enter the API endpoint URL for your AI service (e.g., https://api.openai.com/v1/chat/completions)', 'ai-summary-generator' ) . '

'; } /** * Callback for API token setting field. */ public static function api_token_callback() { $value = get_option( 'ai_summary_generator_api_token', '' ); echo ''; echo '

' . esc_html__( 'Your API authentication token or key. Keep this secure and do not share it publicly.', 'ai-summary-generator' ) . '

'; } /** * Callback for model name setting field. */ public static function model_name_callback() { $value = get_option( 'ai_summary_generator_model_name', '' ); echo ''; echo '

' . esc_html__( 'Specify the AI model to use (e.g., gpt-3.5-turbo, gpt-4, claude-3, etc.)', 'ai-summary-generator' ) . '

'; } /** * Callback for prompt setting field. */ public static function prompt_callback() { $value = get_option( 'ai_summary_generator_prompt' ); $default_prompt = __( 'Your task is to generate an English summary of the article. Please generate a summary of 100 words or less based on the content of the following article, and output the summary directly. Only Plain Text is allowed.', 'ai-summary-generator' ); echo ''; echo '
'; echo '

' . esc_html__( 'Custom system prompt for generating summaries. Click "Reset to Default" to restore the original prompt.', 'ai-summary-generator' ) . '

'; // Add JavaScript for the reset functionality. echo ''; } /** * Callback for CSS classes information field. */ public static function css_classes_info_callback() { echo '
'; echo '

' . esc_html__( 'CSS Classes for Custom Styling:', 'ai-summary-generator' ) . '

'; echo '

' . sprintf( /* translators: %1$s and %2$s are opening and closing tags, %3$s and %4$s are opening and closing tags for the first link, %5$s and %6$s are opening and closing tags for the second link */ esc_html__( 'You can customize the appearance of AI summaries by adding CSS in %1$sAppearance → Customize → Additional CSS%2$s (located at %3$s/wp-admin/customize.php%4$s) or if you use a block theme, please add it in %1$sAppearance → Editor%2$s (located at %5$s/wp-admin/site-editor.php%6$s).', 'ai-summary-generator' ), '', '', '', '', '', '' ) . '

'; echo '
    '; echo '
  • .ai-summary - ' . esc_html__( 'Main container for the AI summary', 'ai-summary-generator' ) . '
  • '; echo '
  • .ai-summary-quote - ' . esc_html__( 'The blockquote element', 'ai-summary-generator' ) . '
  • '; echo '
  • .ai-summary-title - ' . esc_html__( 'The "AI Summary" title paragraph', 'ai-summary-generator' ) . '
  • '; echo '
  • .ai-summary-content - ' . esc_html__( 'The summary content paragraph', 'ai-summary-generator' ) . '
  • '; echo '
  • .ai-summary-excerpt - ' . esc_html__( 'Summary on homepage/archive pages', 'ai-summary-generator' ) . '
  • '; echo '
'; echo '

' . esc_html__( 'CSS Example:', 'ai-summary-generator' ) . '

'; echo '
';
		echo esc_html( self::get_example_css() );
		echo '
'; echo '
'; } /** * Callback for homepage override setting field. */ public static function homepage_override_callback() { $value = get_option( 'ai_summary_generator_homepage_override', 'no' ); echo ''; echo '

' . sprintf( /* translators: %1$s and %2$s are opening and closing tags, %3$s and %4$s are opening and closing tags */ esc_html__( 'Enable this to display AI summaries on your homepage instead of full post content. %1$sNote:%2$s This may not work with all themes as it depends on the theme calling %3$sget_the_excerpt()%4$s. If this doesn\'t work, use %3$sai_summary_generator_get_summary($post_id)%4$s function in your theme templates.', 'ai-summary-generator' ), '', '', '', '' ) . '

'; } /** * Callback for update on post update setting field. */ public static function update_on_post_update_callback() { $value = get_option( 'ai_summary_generator_update_on_post_update', 'no' ); echo ''; echo '

' . esc_html__( 'Automatically regenerate AI summary when a post is updated or modified.', 'ai-summary-generator' ) . '

'; } /** * Callback for max retries setting field. */ public static function max_retries_callback() { $value = get_option( 'ai_summary_generator_max_retries', 3 ); echo ''; echo '

' . esc_html__( 'Number of retry attempts for failed API requests. Uses exponential backoff (1s, 2s, 4s, etc.)', 'ai-summary-generator' ) . '

'; } /** * Get default CSS styles */ public static function get_example_css() { return '.ai-summary { background: #f8f9fa; border-radius: 8px; margin: 20px 0; padding: 0; } .ai-summary-quote { margin: 0; padding: 20px; border-left: 4px solid #0073aa; background: transparent; } .ai-summary-title { color: #0073aa; font-size: 16px; margin-bottom: 10px; } .ai-summary-content { line-height: 1.6; color: #333; margin: 0; } .ai-summary-excerpt { font-style: italic; color: #666; }'; } /** * Sanitize yes/no values. * * @param string $input Input value to sanitize. * @return string Sanitized yes/no value. */ public static function sanitize_yes_no( $input ) { return in_array( $input, array( 'yes', 'no' ), true ) ? $input : 'no'; } /** * Sanitize retry count. * * @param mixed $input Input value to sanitize. * @return int Sanitized retry count. */ public static function sanitize_retry_count( $input ) { $value = intval( $input ); return ( $value >= 1 && $value <= 5 ) ? $value : 3; } }