customize-controls.js – extend api.previewer

From your comment, I think you are wrong in the concept. You want to save theme options after first activation. Activate a theme doesn’t mean that the user will use the theme customizer, so I think there is nothing to do with the customizer javascript because it will be loaded only if the user go to the customizer screen and because is a client-side code. There are better ways.

My suggestion is to use after_switch_theme and switch_theme actions hooks:

  • after_switch_theme to save theme options after the theme is activated.
  • switch_theme to remove theme options if the theme is deactivated (optional)

Example:

add_action("after_switch_theme", "cyb_save_theme_options_on_activation");
function cyb_save_theme_options_on_activation() {
    set_theme_mod('background-color', '#FFF');
}

I think it would be good to check if the option being added/update has a user custom value; if so I think it shouldn’t be changed. For example:

add_action("after_switch_theme", "cyb_save_theme_options_on_activation");
function cyb_save_theme_options_on_activation() {
    if( get_theme_mod('background-color') == '' ) {
        set_theme_mod('background-color', '#FFF');
    }
}

If you are using theme options instead of theme mods:

add_action("after_switch_theme", "cyb_save_theme_options_on_activation");
function cyb_save_theme_options_on_activation() {
    //add_option adds the option if it doesn't exist in the database
    //Use update_option if you want to override existing option in the database or add it if doesn't exist
    add_option('background-color', '#FFF');
}

I’m still think that you are wrong in the approach. From your comment, you want to save all options into database, even options with default values, when the use click on “Save and Publish” and not on theme activation. To do that, you could use customize_save_after action hook:

add_action( 'customize_save_after', 'customize_save_after' );
function customize_save_after( $wp_customize ) {
    //Save the theme options here
}