array( self::class, 'sanitize_widget_preferences' ), ) ); } /** * Sanitize widget preferences * * @param array $input Input data. * @return array Sanitized data. */ public static function sanitize_widget_preferences( $input ) { $sanitized = array(); if ( is_array( $input ) ) { foreach ( $input as $key => $value ) { $sanitized_key = sanitize_text_field( $key ); $sanitized_value = is_bool( $value ) ? (bool) $value : sanitize_text_field( $value ); $sanitized[ $sanitized_key ] = $sanitized_value; } } return $sanitized; } /** * Get widget preferences */ public static function get_widget_preferences() { $defaults = array( 'data_cards_enabled' => true, 'story_calendar_enabled' => true, 'posts_activity_enabled' => true, 'widget_refresh_interval' => 300, // 5 minutes ); return wp_parse_args( get_option( 'aistma_widget_preferences', array() ), $defaults ); } /** * Handle AJAX widget toggle */ public static function handle_widget_toggle() { // Verify nonce and capabilities if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'aistma_widget_toggle' ) || ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Security check failed', 'ai-story-maker' ) ); } $widget_id = isset( $_POST['widget_id'] ) ? sanitize_text_field( wp_unslash( $_POST['widget_id'] ) ) : ''; $enabled = isset( $_POST['enabled'] ) ? (bool) $_POST['enabled'] : false; $preferences = self::get_widget_preferences(); $preferences[ $widget_id . '_enabled' ] = $enabled; update_option( 'aistma_widget_preferences', $preferences ); wp_send_json_success( array( 'message' => __( 'Widget preference updated', 'ai-story-maker' ), 'widget_id' => $widget_id, 'enabled' => $enabled ) ); } /** * Get widget configuration for admin interface */ public static function get_widget_config() { return array( 'data_cards' => array( 'title' => __( 'Data Overview Cards', 'ai-story-maker' ), 'description' => __( 'Key statistics and metrics in card format', 'ai-story-maker' ), 'class' => 'AISTMA_Data_Cards_Widget', 'widget_id' => 'aistma_data_cards_widget' ), 'story_calendar' => array( 'title' => __( 'Story Generation Calendar', 'ai-story-maker' ), 'description' => __( '6-month heatmap of story generation activity', 'ai-story-maker' ), 'class' => 'AISTMA_Story_Calendar_Widget', 'widget_id' => 'aistma_story_calendar_widget' ), 'posts_activity' => array( 'title' => __( 'Recent Posts Activity', 'ai-story-maker' ), 'description' => __( '14-day activity heatmap for recent posts', 'ai-story-maker' ), 'class' => 'AISTMA_Posts_Activity_Widget', 'widget_id' => 'aistma_posts_activity_widget' ) ); } /** * Render widget management interface */ public static function render_widget_management() { $preferences = self::get_widget_preferences(); $widgets = self::get_widget_config(); ?>