[] ], $pass_inside ) ); } /** * Implement customizer controls * * @param WP_Customize_Manager $wp_customize Theme Customizer object. */ function ct_customize_register( $wp_customize ) { require get_template_directory() . '/inc/classes/class-ct-group-title.php'; global $ct_wp_customize; $ct_wp_customize = $wp_customize; $wp_customize->get_setting( 'blogname' )->transport = 'postMessage'; $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage'; $wp_customize->remove_section( 'header_image' ); $wp_customize->remove_section( 'colors' ); $wp_customize->add_section( new Ct_Group_Title( $wp_customize, 'core', [ 'title' => esc_html__( 'Core', 'ct' ), 'priority' => 10, ] ) ); ct_customizer_register_options( $wp_customize, ct_get_options( 'customizer' ) ); if ( isset( $wp_customize->selective_refresh ) ) { $wp_customize->selective_refresh->add_partial( 'blogname', [ 'selector' => '.site-title a', 'render_callback' => 'ct_customize_partial_blogname', ] ); $wp_customize->selective_refresh->add_partial( 'blogdescription', [ 'selector' => '.site-description', 'render_callback' => 'ct_customize_partial_blogdescription', ] ); } } add_action( 'customize_register', 'ct_customize_register' ); /** * Render the site title for the selective refresh partial. * * @return void */ function ct_customize_partial_blogname() { bloginfo( 'name' ); } /** * Render the site tagline for the selective refresh partial. * * @return void */ function ct_customize_partial_blogdescription() { bloginfo( 'description' ); } function ct_add_customizer_preview_cache( $content ) { add_action( 'ct:customizer:preview:cache', function () use ( $content ) { echo $content; } ); } add_action( 'wp_footer', function () { if ( ! is_customize_preview() ) { return; } ob_start(); echo '
'; do_action( 'ct:customizer:preview:cache' ); echo '
'; $html = ob_get_clean(); // $html = str_replace(' ', '', $html); $search = array( '/\>[^\S ]+/s', // strip whitespaces after tags, except space '/[^\S ]+\/', // Remove HTML comments ); $replace = array( '>', '<', '\\1', '', ); // $html = preg_replace($search, $replace, $html); echo ''; }, 3000, 0 ); /** * Binds JS handlers to make Theme Customizer preview reload changes asynchronously. */ add_action( 'customize_preview_init', function () { wp_enqueue_script( 'ct-customizer', get_template_directory_uri() . '/static/bundle/sync.js', array( 'customize-preview' ), '20151215', true ); } ); function ct_customizer_sync_data() { $location = null; if ( is_front_page() ) { $location = 'home'; } if ( is_page() ) { $location = 'page'; } if ( get_post_type() === 'post' && is_single() ) { $location = 'post'; } return [ 'future_location' => $location, ]; } /** * Enqueue JavaScripts & CSS */ add_action( 'customize_controls_enqueue_scripts', function () { $theme = wp_get_theme(); wp_enqueue_script( 'ct-customizer-controls', get_template_directory_uri() . '/static/bundle/customizer-controls.js', array( 'customize-controls', 'underscore', 'wp-color-picker' ), $theme->get( 'Version' ), false ); wp_enqueue_style( 'ct-customizer-controls-styles', get_template_directory_uri() . '/static/bundle/customizer-controls.css', array(), $theme->get( 'Version' ) ); } ); add_action( 'wp_ajax_ct_customizer_reset', function () { global $ct_wp_customize; if ( ! $ct_wp_customize ) { return; } if ( ! $ct_wp_customize->is_preview() ) { wp_send_json_error(); } if ( ! check_ajax_referer( 'ct-customizer-reset', 'nonce', false ) ) { wp_send_json_error( 'nonce' ); } $settings = $ct_wp_customize->settings(); foreach ( $settings as $single_setting ) { if ( 'theme_mod' !== $single_setting->type ) { continue; } remove_theme_mod( $single_setting->id ); } wp_send_json_success(); } ); /** * Register customizer options recursively * * @param WP_Customize_Manager $wp_customize Theme Customizer object. * @param array $options All the theme options. * @param array $parent_data recursive data. */ function ct_customizer_register_options( $wp_customize, $options, $parent_data = array(), $has_controls = true ) { $collected = array(); ct_collect_options( $collected, $options, array( 'limit_option_types' => false, 'limit_level' => 1, 'info_wrapper' => true, ) ); if ( empty( $collected ) ) { return; } foreach ( $collected as &$opt ) { if ( isset( $opt['option']['type'] ) && 'ct-group-title' === $opt['option']['type'] ) { $wp_customize->add_section( new Ct_Group_Title( $wp_customize, $opt['id'], $opt['option'] ) ); continue; } if ( 'container' === $opt['group'] ) { // Check if has container options. $_collected = array(); ct_collect_options( $_collected, $opt['option']['options'], array( 'limit_option_types' => array(), 'limit_level' => 1, 'info_wrapper' => false, ) ); $has_containers = ! empty( $_collected ); unset( $_collected ); $children_data = array( 'group' => 'container', 'id' => $opt['id'], ); $args = array( 'title' => empty( $opt['option']['title'] ) ? ct_id_to_title( $opt['id'] ) : $opt['option']['title'], 'description' => empty( $opt['option']['desc'] ) ? '' : $opt['option']['desc'], ); if ( isset( $opt['option']['container'] ) && is_array( $opt['option']['container'] ) ) { $args = array_merge( $opt['option']['container'], $args ); } if ( $has_containers ) { if ( $parent_data ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error trigger_error( esc_html( $opt['id'] ) . ' panel can\'t have a parent (' . esc_html( $parent_data['id'] ) . ')', E_USER_WARNING ); break; } $wp_customize->add_panel( $opt['id'], $args ); $children_data['customizer_type'] = 'panel'; } else { if ( $parent_data ) { if ( 'panel' === $parent_data['customizer_type'] ) { $args['panel'] = $parent_data['id']; } else { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error trigger_error( esc_html( $opt['id'] ) . ' section can have only panel parent (' . esc_html( $parent_data['id'] ) . ')', E_USER_WARNING ); break; } } $wp_customize->add_section( $opt['id'], $args ); $children_data['customizer_type'] = 'section'; } ct_customizer_register_options( $wp_customize, $opt['option']['options'], $children_data ); unset( $children_data ); continue; } if ( 'option' === $opt['group'] ) { if ( ( $opt['option']['type'] === 'ct-panel' || $opt['option']['type'] === 'ct-options' ) && isset( $opt['option']['inner-options'] ) ) { $options_to_send = null; ct_collect_options( $options_to_send, $opt['option']['inner-options'], [ 'include_container_types' => false, ] ); $options_to_send = json_decode( json_encode( $options_to_send ), true ); ct_customizer_register_options( $wp_customize, $options_to_send, [], false ); } $args_control = array( 'label' => empty( $opt['option']['label'] ) ? ct_id_to_title( $opt['id'] ) : $opt['option']['label'], 'description' => empty( $opt['option']['desc'] ) ? '' : $opt['option']['desc'], 'settings' => $opt['id'], 'type' => $opt['option']['type'], ); if ( isset( $opt['option']['control'] ) && is_array( $opt['option']['control'] ) ) { $args_control = array_merge( $opt['option']['control'], $args_control ); } $args_control = array_merge( $opt['option'], $args_control ); if ( $parent_data ) { if ( 'section' === $parent_data['customizer_type'] ) { $args_control['section'] = $parent_data['id']; } else { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error trigger_error( 'Invalid control parent: ' . esc_html( $parent_data['customizer_type'] ), E_USER_WARNING ); break; } } else { // the option is not placed in a section, create a section automatically. $args_control['section'] = 'fw_option_auto_section_' . $opt['id']; $wp_customize->add_section( $args_control['section'], array( 'title' => empty( $opt['option']['label'] ) ? ct_id_to_title( $opt['id'] ) : $opt['option']['label'], ) ); } $args_setting = array( 'default' => isset( $opt['option']['value'] ) ? $opt['option']['value'] : ' ', ); if ( isset( $opt['option']['setting'] ) && is_array( $opt['option']['setting'] ) ) { $args_setting = array_merge( $opt['option']['setting'], $args_setting ); } $wp_customize->add_setting( $opt['id'], array_merge($args_setting, [ 'sanitize_callback' => function ($input, $setting) { return $input; } ]) ); unset( $args_setting ); if ( isset( $opt['option']['selective_refresh'] ) && isset( $opt['option']['selective_refresh']['selector'] ) ) { $wp_customize->selective_refresh->add_partial( $opt['id'], $opt['option']['selective_refresh'] ); } if ( $has_controls ) { $our_control = new WP_Customize_Control( $wp_customize, $opt['id'], $args_control ); if ( isset( $opt['option']['choices'] ) ) { $our_control->json['choices'] = $opt['option']['choices']; } if ( isset( $opt['option']['condition'] ) ) { $our_control->json['condition'] = $opt['option']['condition']; } $our_control->json['option'] = $opt['option']; $wp_customize->add_control( $our_control ); } continue; } // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error trigger_error( 'Unknown group: ' . esc_html( $opt['group'] ), E_USER_WARNING ); } }