## Filter: wpd_ai_save_settings

Filter the result of settings save operations. This allows you to hook into settings saves, modify save behavior, or perform additional actions when settings are saved.

### Description

When Alpha Insights settings are saved, this filter is called with the save result. You can use this to perform additional actions, modify the save result, or trigger custom logic after settings are saved.

#### Location

File: `includes/admin/wpd-settings.php`

Function: Settings save handler

Line: ~603

### Parameters

Parameter | Type | Description
--- | --- | ---
$saved | bool | Whether the settings were successfully saved (default: true/false from save operation)

#### Return

Type: bool

Description: Modified save result (true for success, false for failure)

### Example Usage

### Clear Cache After Settings Save

    add_filter( 'wpd_ai_save_settings', 'clear_cache_on_settings_save' );
    function clear_cache_on_settings_save( $saved ) {
        if ( $saved ) {
            // Clear relevant caches when settings are saved
            wp_cache_flush();
            // Or clear specific transients
            delete_transient( 'wpd_ai_report_*' );
        }
        return $saved;
    }

### Log Settings Changes

    add_filter( 'wpd_ai_save_settings', 'log_settings_changes' );
    function log_settings_changes( $saved ) {
        if ( $saved ) {
            $user = wp_get_current_user();
            error_log( sprintf(
                'Alpha Insights settings saved by user: %s (ID: %d) at %s',
                $user->user_login,
                $user->ID,
                current_time( 'mysql' )
            ) );
        }
        return $saved;
    }

### Send Notification on Settings Save

    add_filter( 'wpd_ai_save_settings', 'notify_on_settings_save' );
    function notify_on_settings_save( $saved ) {
        if ( $saved ) {
            // Send email notification to admin
            $admin_email = get_option( 'admin_email' );
            wp_mail(
                $admin_email,
                'Alpha Insights Settings Updated',
                'Alpha Insights settings have been updated on ' . get_site_url()
            );
        }
        return $saved;
    }

### Sync Settings to External System

    add_filter( 'wpd_ai_save_settings', 'sync_settings_to_external' );
    function sync_settings_to_external( $saved ) {
        if ( $saved ) {
            // Sync settings to external CRM or system
            $settings = get_option( 'wpd_alpha_insights_settings', array() );
            // Make API call to sync
            wp_remote_post( 'https://your-api.com/sync-settings', array(
                'body' => json_encode( $settings ),
                'headers' => array( 'Content-Type' => 'application/json' )
            ) );
        }
        return $saved;
    }

### Validate Settings Before Save

    add_filter( 'wpd_ai_save_settings', 'validate_settings_before_save', 5 );
    function validate_settings_before_save( $saved ) {
        // This runs after save, but you can still check and potentially revert
        if ( $saved ) {
            $settings = get_option( 'wpd_alpha_insights_settings', array() );

            // Validate specific setting
            if ( isset( $settings['some_setting'] ) && $settings['some_setting'] < 0 ) {
                // Invalid value - you could update_option to fix it
                $settings['some_setting'] = 0;
                update_option( 'wpd_alpha_insights_settings', $settings );
            }
        }
        return $saved;
    }

### Best Practices

- Return the original $saved value unless you need to change it
- Perform lightweight operations in this filter
- Use background processing for heavy operations
- Log important actions for debugging
- Don't modify settings directly in this filter (use hooks instead)
- Consider using specific setting hooks if available

### Important Notes

- This filter runs after settings are saved
- Returning false doesn't undo the save - it only affects the return value
- This is a general settings save hook - not specific to individual settings
- Use this for post-save actions, not validation
- For validation, use settings-specific hooks or pre-save filters
- This filter runs for all settings saves, not individual setting changes

### When to Use This Filter

Good Use Cases:

- Clearing caches after settings change
- Logging settings changes
- Sending notifications
- Syncing to external systems
- Triggering background processes

Not Recommended For:

- Validating settings (use pre-save hooks)
- Modifying settings (use specific setting hooks)
- Heavy database operations (use background processing)
- Blocking saves (use validation hooks instead)

### Debugging

    add_filter( 'wpd_ai_save_settings', 'debug_settings_save', 999 );
    function debug_settings_save( $saved ) {
        error_log( 'Alpha Insights Settings Save Result: ' . ( $saved ? 'SUCCESS' : 'FAILED' ) );
        if ( $saved ) {
            $settings = get_option( 'wpd_alpha_insights_settings', array() );
            error_log( 'Current Settings: ' . print_r( $settings, true ) );
        }
        return $saved;
    }

### Related Functions

- `update_option()` - WordPress function for saving options
- `get_option()` - WordPress function for retrieving options