<?php
add_action( 'after_setup_theme', 'fortune88_setup' );
function fortune88_setup()
{
load_theme_textdomain( 'fortune88', get_template_directory() . '/languages' );
add_theme_support( 'title-tag' );
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'post-thumbnails' );
global $content_width;
if ( ! isset( $content_width ) ) $content_width = 640;
register_nav_menus(
array(
'primary-menu' => __( 'Primary Menu', 'fortune88' ),
	'contact-menu' => __( 'Contact Menu', 'fortune88' ),
	'login-menu' => __( 'Login Menu', 'fortune88' ),
	'games-menu' => __( 'Games Menu', 'fortune88' ),
	'provider-menu' => __( 'Provider Menu', 'fortune88' ))
	
);
}

function prefix_nav_description( $item_output, $item, $depth, $args ) {
    if ( !empty( $item->description ) ) {
        $item_output = str_replace( $args->link_after . '</a>', '<span class="menu-item-description">' . $item->description . '</span>' . $args->link_after . '</a>', $item_output );
    }
 
    return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'prefix_nav_description', 10, 4 );

add_action( 'wp_enqueue_scripts', 'fortune88_load_scripts' );
function fortune88_load_scripts()
{
wp_enqueue_script( 'jquery' );
}
add_action( 'comment_form_before', 'fortune88_enqueue_comment_reply_script' );
function fortune88_enqueue_comment_reply_script()
{
if ( get_option( 'thread_comments' ) ) { wp_enqueue_script( 'comment-reply' ); }
}
add_filter( 'the_title', 'fortune88_title' );
function fortune88_title( $title ) {
if ( $title == '' ) {
return '&rarr;';
} else {
return $title;
}
}
add_filter( 'wp_title', 'fortune88_filter_wp_title' );
function fortune88_filter_wp_title( $title )
{
return $title . esc_attr( get_bloginfo( 'name' ) );
}
add_action( 'widgets_init', 'fortune88_widgets_init' );
function fortune88_widgets_init()
{
register_sidebar( array (
		'name' => __( 'Sidebar Widget Area', 'fortune88' ),
		'id' => 'primary-widget-area',
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => "</li>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
}
function fortune88_custom_pings( $comment )
{
$GLOBALS['comment'] = $comment;
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>"><?php echo comment_author_link(); ?></li>
<?php 
}
add_filter( 'get_comments_number', 'fortune88_comments_number' );
function fortune88_comments_number( $count )
{
if ( !is_admin() ) {
global $id;
$comments_by_type = &separate_comments( get_comments( 'status=approve&post_id=' . $id ) );
return count( $comments_by_type['comment'] );
} else {
return $count;
}
}
function wpdocs_excerpt_more( $more ) {
    return sprintf( '<a class="read-more" href="%1$s">%2$s</a>',
        get_permalink( get_the_ID() ),
        __( '&nbsp;Read Post', 'textdomain' )
    );
}
add_filter( 'excerpt_more', 'wpdocs_excerpt_more' );

// Remove jQuery Migrate Script from header and Load jQuery from Google API
function crunchify_stop_loading_wp_embed_and_jquery() {
	if (!is_admin()) {
		wp_deregister_script('wp-embed');
		wp_deregister_script('jquery');  // Bonus: remove jquery too if it's not required
	}
}
add_action('init', 'crunchify_stop_loading_wp_embed_and_jquery');

remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); 
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); 
remove_action( 'wp_print_styles', 'print_emoji_styles' ); 
remove_action( 'admin_print_styles', 'print_emoji_styles' );

function remove_recent_comments_style() {
    global $wp_widget_factory;
    remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
}
add_action('widgets_init', 'remove_recent_comments_style');

// Don't load Gutenberg-related stylesheets.
add_action( 'wp_enqueue_scripts', 'remove_block_css', 100 );
function remove_block_css() {
wp_dequeue_style( 'wp-block-library' ); // WordPress core
wp_dequeue_style( 'wp-block-library-theme' ); // WordPress core
wp_dequeue_style( 'wc-block-style' ); // WooCommerce
wp_dequeue_style( 'storefront-gutenberg-blocks' ); // Storefront theme
}



class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
   
	 function start_lvl( &$output, $depth = 0, $args = null ) {
        // Start of a submenu
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<div class='subcontainer'><ul class='sub-menu'>\n";
    }

    function end_lvl( &$output, $depth = 0, $args = null ) {
        // End of a submenu
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul></div>\n";
    }
	 function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
        // Retrieve image URL from menu item description
        $description = $item->description;
        $image_url = '';

        // Check if description contains a valid URL
        if (filter_var($description, FILTER_VALIDATE_URL)) {
            $image_url = $description;
        }

        // Output menu item with image
        $indent = ($depth) ? str_repeat("\t", $depth) : '';

        // Add classes to the current menu item
        $classes = empty($item->classes) ? array() : (array)$item->classes;
        $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args, $depth));
        $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';

        // Add ID to the current menu item
        $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth);
        $id = $id ? ' id="' . esc_attr($id) . '"' : '';

        // Output the current menu item
        $output .= $indent . '<li' . $id . $class_names .'>';

        // Link attributes
        $attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
        $attributes .= !empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
        $attributes .= !empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
        $attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';
        $attributes = trim($attributes);

        // Link output with image
        $item_output = $args->before;
        $item_output .= '<a href="'.$item->url.'">'.$item->title;
       if (!empty($image_url)) {
            $item_output .= '<img src="' . esc_url($image_url) . '" alt="' . esc_attr($item->title) . '">';
        }
        
        $item_output .= '</a>';
     $item_output .= $args->after;

        // Output the menu item
        $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
    }
}
function theme_custom_logo_setup() {
    add_theme_support('custom-logo', array(
        'height'      => 100,
        'width'       => 400,
        'flex-height' => true,
        'flex-width'  => true,
    ));
}
add_action('after_setup_theme', 'theme_custom_logo_setup');



add_filter( 'nav_menu_css_class', 'special_nav_class', 10, 3 );
function special_nav_class( $classes, $item, $args ) {
    if ( 'login-menu' === $args->theme_location ) {
        $classes[] = 'button';
    }

    return $classes;
}

add_action('customize_register', 'add_second_tagline');
function add_second_tagline($wp_customize) {

 $wp_customize->add_setting( 'login_link', array(
 'default' => '',
 'capability' => 'edit_theme_options'
 ) );
 $wp_customize->add_control( 'login_link', array(
 'label' => 'DAFTAR Button Link',
 'section' => 'title_tagline',
 'type' => 'text'
 ) );
 
 /* === */
 
 
  $wp_customize->add_setting( 'signup_link', array(
 'default' => '',
 'capability' => 'edit_theme_options'
 ) );

 
  $wp_customize->add_control( 'signup_link', array(
 'label' => 'LOGIN Button Link',
 'section' => 'title_tagline',
 'type' => 'text'
 ) );
 
 
 $wp_customize->add_setting( 'ALTERNATIF_link', array(
 'default' => '',
 'capability' => 'edit_theme_options'
 ) );

 
  $wp_customize->add_control( 'ALTERNATIF_link', array(
 'label' => 'Home Botton Link',
 'section' => 'title_tagline',
 'type' => 'text'
 ) );
 
 
  $wp_customize->add_setting( 'apk_link', array(
 'default' => '',
 'capability' => 'edit_theme_options'
 ) );

 
  $wp_customize->add_control( 'apk_link', array(
 'label' => 'APK ICON Link',
 'section' => 'title_tagline',
 'type' => 'text'
 ) );
 
  $wp_customize->add_setting( 'daftar_link', array(
 'default' => '',
 'capability' => 'edit_theme_options'
 ) );

 
  $wp_customize->add_control( 'daftar_link', array(
 'label' => 'Daftar ICON Link',
 'section' => 'title_tagline',
 'type' => 'text'
 ) );
 
 
   $wp_customize->add_setting( 'whatsapp_link', array(
 'default' => '',
 'capability' => 'edit_theme_options'
 ) );

 
  $wp_customize->add_control( 'whatsapp_link', array(
 'label' => 'BONUS ICON Link',
 'section' => 'title_tagline',
 'type' => 'text'
 ) );	
 
 
  $wp_customize->add_setting( 'livechat_link', array(
 'default' => '',
 'capability' => 'edit_theme_options'
 ) );

 
  $wp_customize->add_control( 'livechat_link', array(
 'label' => 'Livechat ICON Link',
 'section' => 'title_tagline',
 'type' => 'text'
 ) );	
 
 
}