<?php

/**
 * Theme Setup
 *
 * @package Viralike
 */
/**
 * Skip link embed
 */
function viralike_skip_link()
{
    // Skip link
    echo  '<a class="screen-reader-text skip-link" href="#main">' . esc_html__( 'Skip to content', 'viralike' ) . '</a>' ;
}

add_action( 'wp_body_open', 'viralike_skip_link', 5 );
/**
 * Sets up theme defaults and registers support for various WordPress features.
 */
function viralike_setup()
{
    // Make theme available for translation
    load_theme_textdomain( 'viralike' );
    // Add default posts and comments RSS feed links to head.
    add_theme_support( 'automatic-feed-links' );
    // Let WordPress manage the document title
    add_theme_support( 'title-tag' );
    // Enable support for Post Thumbnails on posts and pages.
    add_theme_support( 'post-thumbnails' );
    // This theme uses wp_nav_menu() in one location.
    register_nav_menus( array(
        'viralike-primary-menu' => esc_html__( 'Primary menu', 'viralike' ),
    ) );
    /*
     * Switch default core markup for search form, comment form, and comments
     * to output valid HTML5.
     */
    add_theme_support( 'html5', [
        'comment-list',
        'comment-form',
        'search-form',
        'gallery',
        'caption',
        'style',
        'script'
    ] );
    // Add theme support for selective refresh for widgets.
    add_theme_support( 'customize-selective-refresh-widgets' );
    /**
     * Add support for core custom logo.
     *
     * @link https://codex.wordpress.org/Theme_Logo
     */
    add_theme_support( 'custom-logo', [
        'width'       => 180,
        'height'      => 50,
        'flex-width'  => true,
        'flex-height' => true,
    ] );
}

add_action( 'after_setup_theme', 'viralike_setup' );
/**
 * Enqueue scripts and styles.
 */
function viralike_scripts()
{
    wp_enqueue_style(
        'viralike-style',
        get_stylesheet_uri(),
        array(),
        wp_get_theme()->get( 'Version' ),
        false
    );
    // Main scripts include
    wp_enqueue_script(
        'viralike-scripts',
        get_template_directory_uri() . '/src/js/main.js',
        array( 'jquery' ),
        wp_get_theme()->get( 'Version' ),
        true
    );
    // WordPress comments
    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
}

add_action( 'wp_enqueue_scripts', 'viralike_scripts' );
/**
 * Get sidebar
 */
function viralike_sidebar()
{
    register_sidebar( [
        'name'          => esc_html__( 'Sidebar', 'viralike' ),
        'id'            => 'viralike-sidebar',
        'description'   => esc_html__( 'Add widgets to the posts sidebar.', 'viralike' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ] );
}

add_action( 'widgets_init', 'viralike_sidebar' );
/**
 * Remove "category" from categories title
 */
function viralike_category_title( $title )
{
    // Returns only the category name on the category page
    if ( is_category() ) {
        $title = single_cat_title( '', false );
    }
    return $title;
}

add_filter( 'get_the_archive_title', 'viralike_category_title' );
/**
 * Limit excerpt to a number of characters
 */
function viralike_excerpt_length( $length )
{
    // Return up to 25 words for any abstract
    return 25;
}

add_filter( 'excerpt_length', 'viralike_excerpt_length' );
/**
 * Change "Read more" for a text ellipsis
 */
function viralike_excerpt_more( $more )
{
    // Any abstract will have a sequence ...
    return '...';
}

add_filter( 'excerpt_more', 'viralike_excerpt_more' );