Array of page IDs. */ function bongoto_woocommerce_get_imported_demo_home_pages() { $ids = get_posts( array( 'post_type' => 'page', 'post_status' => array( 'publish', 'draft' ), 'fields' => 'ids', 'posts_per_page' => 50, 'meta_key' => '_bongoto_demo_home', 'meta_value' => '1', ) ); return is_array( $ids ) ? array_map( 'absint', $ids ) : array(); } add_action( 'customize_register', function ( WP_Customize_Manager $wp_customize ) { $section_id = 'bongoto_section_homepage'; if ( ! $wp_customize->get_section( $section_id ) ) { $wp_customize->add_section( $section_id, array( 'title' => __( 'Homepage', 'bongoto-woocommerce' ), 'priority' => 15, 'panel' => 'bongoto_general_panel', 'description' => __( 'Select which imported home page should be used as the Front Page. Import is done from Quick Setup → Demo Import.', 'bongoto-woocommerce' ), ) ); } $home_ids = bongoto_woocommerce_get_imported_demo_home_pages(); // If nothing imported yet, keep the section but show only an info notice-like description. $choices = array( 0 => __( '— Select —', 'bongoto-woocommerce' ) ); foreach ( $home_ids as $id ) { $title = get_the_title( $id ); $choices[ $id ] = $title ? $title : sprintf( __( 'Page #%d', 'bongoto-woocommerce' ), $id ); } $wp_customize->add_setting( 'bongoto_selected_homepage_id', array( 'default' => (int) get_option( 'page_on_front', 0 ), 'transport' => 'postMessage', 'sanitize_callback' => function ( $v ) use ( $choices ) { $v = absint( $v ); return array_key_exists( $v, $choices ) ? $v : 0; }, ) ); $wp_customize->add_control( 'bongoto_selected_homepage_id', array( 'label' => __( 'Front Page', 'bongoto-woocommerce' ), 'section' => $section_id, 'type' => 'select', 'choices' => $choices, 'description' => empty( $home_ids ) ? __( 'No imported home pages found yet. Use Quick Setup → Demo Import to import one or more home JSON files first.', 'bongoto-woocommerce' ) : __( 'Switch the site Front Page to one of your imported home pages.', 'bongoto-woocommerce' ), ) ); }, 40 ); /** * When Customizer settings are saved, apply the selected front page. */ add_action( 'customize_save_after', function () { $page_id = absint( get_theme_mod( 'bongoto_selected_homepage_id', 0 ) ); if ( $page_id > 0 && 'page' === get_post_type( $page_id ) ) { update_option( 'show_on_front', 'page' ); update_option( 'page_on_front', $page_id ); } } ); /** * Live preview: when selecting a Home page in Customizer, update the preview URL instantly. */ add_action( 'customize_controls_enqueue_scripts', function() { if ( ! function_exists( 'bongoto_woo_get_demo_homepage_ids' ) ) { return; } $home_ids = bongoto_woo_get_demo_homepage_ids(); if ( empty( $home_ids ) ) { return; } $urls = []; foreach ( $home_ids as $id ) { $id = absint( $id ); if ( $id > 0 ) { $urls[ $id ] = get_permalink( $id ); } } wp_enqueue_script( 'bongoto-homepage-preview', get_template_directory_uri() . '/inc/customizer/assets/homepage-preview.js', [ 'customize-controls', 'jquery' ], '1.0.0', true ); wp_localize_script( 'bongoto-homepage-preview', 'BONGOTO_HOME_PREVIEW', [ 'urls' => $urls, ] ); } );