<?php
/**
 * Main function
 *
 * @author LeapIn Corp.
 * @since 1.0.0
 *
 */

/*********************
 * SCRIPTS & ENQUEUEING
 *********************/
function leapin_scripts_and_styles()
{

    if (!is_admin()) {
        /**
         * enqueue js
         */
        if (LEAPIN_DEV) {
            // sandbox
            wp_enqueue_script('leapin-modernizr', leapin_get_js_file('libs/modernizr.custom.js'), array(), '3.6.0', true);
            // change handle
            // @refer https://make.wordpress.org/themes/handbook/review/recommended/#stylesheets-and-scripts
            wp_enqueue_script('barba', leapin_get_js_file('libs/barba.js'), array(), '1.0.0', false);
            wp_enqueue_script('leapin-pagetransition', leapin_get_js_file('libs/pagetransitions.js'), array('jquery'), '1.0.0', true);
            wp_enqueue_script('leapin-scroll', leapin_get_js_file('libs/scroll.js'), array('leapin-scroll-polyfill'), '1.1.2', true);
            wp_enqueue_script('leapin-scroll-polyfill', leapin_get_js_file('libs/polyfill-intersection-observer.js'), array(), '1.1.2', true);
            wp_enqueue_script('leapin-script', leapin_get_js_file('scripts.js'), array('leapin-scroll', 'jquery',
                'barba', 'leapin-pagetransition'), '1.6.13', true);

        } else {
            // production
            wp_enqueue_script('leapin-script', leapin_get_js_file('bundle.js'), array('jquery'), '1.6.11', true);
        }

        /**
         * enqueue styles
         */
        wp_enqueue_style('leapin-font-awesome', leapin_get_css_file('libs/fontawesome-all.css'), array(), '5.3.1', 'all');
        wp_enqueue_style('leapin-stylesheet', leapin_get_css_file('style.css'), array(), '1.6.13', 'all');

        // comment reply script for threaded comments
        if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1)) {
            wp_enqueue_script('comment-reply');
        }
    }
}

/*********************
 * ADMIN SCRIPTS & ENQUEUEING
 *********************/
function leapin_admin_scripts_and_styles()
{
    // admin and customizer preview styles
    wp_enqueue_style('wp-color-picker');
    wp_enqueue_style('leapin-admin-css', leapin_get_css_file('admin.css'), false, '1.0.0');

    // admin and customizer preview scripts
    wp_enqueue_script('wp-color-picker');

    // only in preview
    if (is_customize_preview()) {
        wp_enqueue_script('leapin-alpha-color-picker', leapin_get_js_file('libs/alpha-color-picker.js'), array('jquery', 'wp-color-picker'), '1.0.0', true);

        // only in wp-admin
    } else {
        wp_enqueue_script('leapin-admin-script', leapin_get_js_file('admin.js'), array('wp-color-picker', 'jquery', 'jquery-ui-dialog'), false, true);
        wp_enqueue_script('leapin-alpha-color-picker-admin', leapin_get_js_file('libs/alpha-color-picker-admin.js'), array('jquery', 'wp-color-picker'), '1.0.0', true);
        wp_enqueue_style('wp-jquery-ui-dialog');
    }
}

/*********************
 * ASYNCRONOUS SCRIPTS & STYLES LOADING
 *********************/
function leapin_add_id_to_script_tag($tag, $handle, $href)
{
    if (is_admin()) return $tag;

    $id = 'id="' . $handle . '"';
    $n = 0;
    if ((leapin_is_pro() && $handle === "leapin-script-pro")
        || (!leapin_is_pro() && $handle === "leapin-script")) {
        $n = 1;
    }
    $tag = leapin_str_replace_n("/type='text\/javascript'/", apply_filters('leapin_script_tag_attr', $id, $handle), $tag, $n);
    return $tag;
}

add_filter('script_loader_tag', 'leapin_add_id_to_script_tag', 9999, 3);

/*********************
 * THEME SUPPORT
 *********************/
// Adding WP 3+ Functions & Theme Support
function leapin_theme_support()
{

    // wp thumbnails (sizes handled in functions.php)
    add_theme_support('post-thumbnails');

    // default thumb size
    set_post_thumbnail_size(700, 700, true);

    // wp custom background
    add_theme_support('custom-background',
        array(
            'wp-head-callback' => 'leapin_custom_background_cb',
        )
    );

    // custom logo
    add_theme_support('custom-logo', array(
        'height' => 70,
        'width' => 300,
        'flex-width' => true,
    ));

    // wp custom header
    add_theme_support('custom-header', array(
        'header-text' => false,
    ));

    // title support
    add_theme_support('title-tag');

    // rss thingy
    add_theme_support('automatic-feed-links');

    // Enable support for HTML5 markup.
    add_theme_support('html5', array(
        'comment-list',
        'search-form',
        'comment-form'
    ));


} /* end pjax blog theme support */


/*********************
 * EXCERPT SETTING
 *********************/
// cleaning up excerpt
function leapin_excerpt_more($more)
{
    // edit here if you like to change more link description
    return '...';
}

add_filter('excerpt_more', 'leapin_excerpt_more');

function leapin_excerpt_mblength($length)
{
    return 100;
}

add_filter('excerpt_mblength', 'leapin_excerpt_mblength');

function leapin_excerpt_length($length)
{
    return 300;
}

add_filter('excerpt_length', 'leapin_excerpt_length', 999);

/*********************
 * INSERT CUSTOM JS AND CSS
 *********************/
function leapin_inline_js_settings()
{
    $custom_js = LEAPIN_Theme_Mod::get_instance()->get_script(false);
    if (LEAPIN_DEV) {
        $is_pro = leapin_is_pro() ? '-pro' : '';
        wp_add_inline_script('leapin-script' . $is_pro, $custom_js, 'before');
    } else {
        wp_add_inline_script('leapin-script', $custom_js, 'before');
    }
}

function leapin_inline_css_settings()
{
    $custom_css = LEAPIN_Theme_Mod::get_instance()->get_style(false);
    wp_add_inline_style('leapin-stylesheet', $custom_css);
}

/*********************
 * SET CUSTOM ARCHIVE TITLE
 *********************/
function leapin_get_the_archive_title($title)
{
    if (is_category()) {
        $title = single_cat_title('', false);
    } elseif (is_tag()) {
        $title = single_tag_title('', false);
    } elseif (is_tag()) {
        $title = single_tag_title('', false);
    } elseif (is_author()) {
        $title = '<span class="vcard">' . get_the_author() . '</span>';
    }
    return $title;
}

add_filter('get_the_archive_title', 'leapin_get_the_archive_title');

/*********************
 * SET CUSTOM BACKGROUND
 *********************/
function leapin_custom_background_cb()
{
    // $background is the saved custom image, or the default image.
    $background = set_url_scheme(get_background_image());

    // $color is the saved custom color.
    // A default has to be specified in style.css. It will not be printed here.
    $color = get_background_color();

    if ($color === get_theme_support('custom-background', 'default-color')) {
        $color = false;
    }

    if (!$background && !$color) {
        if (is_customize_preview()) {
            echo '<style type="text/css" id="custom-background-css"></style>';
        }
        return;
    }

    $style = $color ? "background-color: #$color;" : '';

    if ($background) {
        $image = ' background-image: url("' . esc_url($background) . '");';

        // Background Position.
        $position_x = get_theme_mod('background_position_x', get_theme_support('custom-background', 'default-position-x'));
        $position_y = get_theme_mod('background_position_y', get_theme_support('custom-background', 'default-position-y'));

        if (!in_array($position_x, array('left', 'center', 'right'), true)) {
            $position_x = 'left';
        }

        if (!in_array($position_y, array('top', 'center', 'bottom'), true)) {
            $position_y = 'top';
        }

        $position = " background-position: $position_x $position_y;";

        // Background Size.
        $size = get_theme_mod('background_size', get_theme_support('custom-background', 'default-size'));

        if (!in_array($size, array('auto', 'contain', 'cover'), true)) {
            $size = 'auto';
        }

        $size = " background-size: $size;";

        // Background Repeat.
        $repeat = get_theme_mod('background_repeat', get_theme_support('custom-background', 'default-repeat'));

        if (!in_array($repeat, array('repeat-x', 'repeat-y', 'repeat', 'no-repeat'), true)) {
            $repeat = 'repeat';
        }

        $repeat = " background-repeat: $repeat;";

        // Background Scroll.
        $attachment = get_theme_mod('background_attachment', get_theme_support('custom-background', 'default-attachment'));

        if ('fixed' !== $attachment) {
            $attachment = 'scroll';
        }

        $attachment = " background-attachment: $attachment;";

        $style .= $image . $position . $size . $repeat . $attachment;
    }
    ?>
    <style type="text/css" id="custom-background-css">
        .barba-container.custom-background {
        <?php echo trim( $style ); ?>
        }
    </style>
    <?php
}

/*********************
 * SET CUSTOM LOGO
 *********************/
add_filter('get_custom_logo', 'leapin_get_custom_logo_url');
function leapin_get_custom_logo_url($html)
{
    $match = false;
    preg_match('/<img.+src="(.+?)"/', $html, $match);
    if (!empty($match)) {
        return esc_url($match[1]);
    }
    return false;
}

/************* OEMBED SIZE OPTIONS *************/
if (!isset($content_width)) {
    $content_width = 1200;
}

/************* THUMBNAIL SIZE OPTIONS *************/

// Thumbnail sizes
add_image_size('leapin-medium', 700, 700, false);

function leapin_custom_image_sizes($sizes)
{
    return array_merge($sizes, array(
        'leapin-medium' => __('700px by 700px', 'pjax-blog')
    ));
}

add_filter('image_size_names_choose', 'leapin_custom_image_sizes');


?>
