<?php
/**
 * Nimbus functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Nimbus
 * @since 1.0
 */

@ini_set( 'upload_max_size' , '64M' );
@ini_set( 'post_max_size', '64M');
@ini_set( 'max_execution_time', '300' );

/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 */
function nimbus_setup() {
    /*
     * Make theme available for translation.
     * Translations can be filed at WordPress.org. See: https://translate.wordpress.org/projects/wp-themes/nimbus
     * If you're building a theme based on Twenty Seventeen, use a find and replace
     * to change 'nimbus' to the name of your theme in all the template files.
     */
    load_theme_textdomain( 'nimbus' );

    // Add default posts and comments RSS feed links to head.
    add_theme_support( 'automatic-feed-links' );

    /*
     * Let WordPress manage the document title.
     * By adding theme support, we declare that this theme does not use a
     * hard-coded <title> tag in the document head, and expect WordPress to
     * provide it for us.
     */
    add_theme_support( 'title-tag' );

    /*
     * Enable support for Post Thumbnails on posts and pages.
     *
     * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
     */
    add_theme_support( 'post-thumbnails' );

    add_image_size( 'nimbus-featured-image', 2000, 1200, true );
    add_image_size( 'nimbus-grid', 770, 770, true );
    add_image_size( 'nimbus-auto', 770 );
    add_image_size( 'nimbus-blog', 1200 );

    // This theme uses wp_nav_menu() in two locations.
    register_nav_menus( array(
        'main'   => esc_attr__( 'Main Menu', 'nimbus' ),
        'footer' => esc_attr__( 'Footer Menu', 'nimbus' ),
    ) );

    /*
     * Switch default core markup for search form, comment form, and comments
     * to output valid HTML5.
     */
    add_theme_support( 'html5', array(
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
    ) );

    /*
     * Enable support for Post Formats.
     *
     * See: https://codex.wordpress.org/Post_Formats
     */
    add_theme_support( 'post-formats', array(
        'aside',
        'image',
        'video',
        'quote',
        'link',
        'gallery',
        'audio',
    ) );

    // Add theme support for selective refresh for widgets.
    add_theme_support( 'customize-selective-refresh-widgets' );

    // Add wooCommerce Support
    add_theme_support( 'woocommerce' );

}
add_action( 'after_setup_theme', 'nimbus_setup' );

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function nimbus_content_width() {

    $content_width = 700;

    if ( is_front_page() && ! is_home() ) {
        $content_width = 1170;
    }

    /**
     * Filter Twenty Seventeen content width of the theme.
     *
     * @since Nimbus 1.0
     *
     * @param $content_width integer
     */
    $GLOBALS['content_width'] = apply_filters( 'nimbus_content_width', $content_width );
}
add_action( 'after_setup_theme', 'nimbus_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function nimbus_widgets_init() {
    register_sidebar( array(
        'name'          => esc_attr__( 'Sidebar', 'nimbus' ),
        'id'            => 'sidebar',
        'description'   => esc_attr__( 'Add widgets here to appear in your sidebar.', 'nimbus' ),
        'before_widget' => '<section id="%1$s" class="nb-widget widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h4 class="nb-widget-title"><span>',
        'after_title'   => '</span></h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_attr__( 'Footer 1', 'nimbus' ),
        'id'            => 'footer-1',
        'description'   => esc_attr__( 'Add widgets here to appear in your footer.', 'nimbus' ),
        'before_widget' => '<section id="%1$s" class="nb-widget widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h4 class="nb-widget-title"><span>',
        'after_title'   => '</span></h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_attr__( 'Footer 2', 'nimbus' ),
        'id'            => 'footer-2',
        'description'   => esc_attr__( 'Add widgets here to appear in your footer.', 'nimbus' ),
        'before_widget' => '<section id="%1$s" class="nb-widget widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h4 class="nb-widget-title"><span>',
        'after_title'   => '</span></h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_attr__( 'Footer 3', 'nimbus' ),
        'id'            => 'footer-3',
        'description'   => esc_attr__( 'Add widgets here to appear in your footer.', 'nimbus' ),
        'before_widget' => '<section id="%1$s" class="nb-widget widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h4 class="nb-widget-title"><span>',
        'after_title'   => '</span></h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_attr__( 'Footer 4', 'nimbus' ),
        'id'            => 'footer-4',
        'description'   => esc_attr__( 'Add widgets here to appear in your footer.', 'nimbus' ),
        'before_widget' => '<section id="%1$s" class="nb-widget widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h4 class="nb-widget-title"><span>',
        'after_title'   => '</span></h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_attr__( 'Footer 5', 'nimbus' ),
        'id'            => 'footer-5',
        'description'   => esc_attr__( 'Add widgets here to appear in your footer.', 'nimbus' ),
        'before_widget' => '<section id="%1$s" class="nb-widget widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h4 class="nb-widget-title"><span>',
        'after_title'   => '</span></h4>',
    ) );

    register_sidebar( array(
        'name'          => esc_attr__( 'Footer 6', 'nimbus' ),
        'id'            => 'footer-6',
        'description'   => esc_attr__( 'Add widgets here to appear in your footer.', 'nimbus' ),
        'before_widget' => '<section id="%1$s" class="nb-widget widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h4 class="nb-widget-title"><span>',
        'after_title'   => '</span></h4>',
    ) );

    $nb_sidebar = nimbus_option_data('multi_text_sidebar');
    if ( $nb_sidebar && isset( $nb_sidebar )): foreach ($nb_sidebar as $nb_sidebar_item):
        if ( $nb_sidebar_item )
            register_sidebar( array(
                'name'          => $nb_sidebar_item,
                'id'            => strtolower( $nb_sidebar_item ),
                'description'   => esc_attr__( 'Add widgets here to appear in your sidebar.', 'nimbus' ),
                'before_widget' => '<section id="%1$s" class="nb-widget widget %2$s">',
                'after_widget'  => '</section>',
                'before_title'  => '<h4 class="nb-widget-title"><span>',
                'after_title'   => '</span></h4>',
            ) );
    endforeach;endif;
}
add_action( 'widgets_init', 'nimbus_widgets_init' );

/**
 * Replaces "[...]" (appended to automatically generated excerpts) with ... and
 * a 'Continue reading' link.
 *
 * @since Nimbus 1.0
 *
 * @return string 'Continue reading' link prepended with an ellipsis.
 */
function nimbus_excerpt_more( $link ) {
    if ( is_admin() ) {
        return $link;
    }

    $link = sprintf( '<p class="link-more"><a href="%1$s" class="more-link">%2$s</a></p>',
        esc_url( get_permalink( get_the_ID() ) ), sprintf( esc_attr__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'nimbus' ), get_the_title( get_the_ID() ) )
    );
    return ' &hellip;';
}
add_filter( 'excerpt_more', 'nimbus_excerpt_more' );

/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */
function nimbus_pingback_header() {
    if ( is_singular() && pings_open() ) {
        printf( '<link rel="pingback" href="%s">' . "\n", get_bloginfo( 'pingback_url' ) );
    }
}
add_action( 'wp_head', 'nimbus_pingback_header' );

/**
 * Enqueue scripts and styles.
 */
function nimbus_scripts() {

    // Theme stylesheet.
    wp_enqueue_style( 'nimbus-fonts', get_theme_file_uri( '/assets/css/fonts.css' ), array(), '1.0' );
    wp_enqueue_style( 'nimbus-grid', get_theme_file_uri( '/assets/css/grid.css' ), array(), '1.0' );
    wp_enqueue_style( 'nimbus-thirdparty', get_theme_file_uri( '/assets/css/thirdparty.css' ), array(), '1.0' );
    wp_enqueue_style( 'nimbus-site', get_theme_file_uri( '/assets/css/site.css' ), array(), '1.0' );

    // Load the html5 shiv.
    wp_enqueue_script( 'html5', get_theme_file_uri( '/assets/js/html5.js' ), array(), '3.7.3' );
    wp_script_add_data( 'html5', 'conditional', 'lt IE 9' );

    // Load wooCoomerce
    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
        wp_enqueue_style( 'nimbus-woo', get_template_directory_uri().'/assets/css/woo.css', array(), '1.0' );
        wp_enqueue_style( 'nimbus-woocommerce', get_template_directory_uri().'/assets/css/woocommerce.css', array(), '1.0' );
    }

    // Load theme scripts
    wp_enqueue_script( 'nimbus-plugin', get_theme_file_uri( '/assets/js/plugins.js'), array( 'jquery' ), '1.0', true );
    wp_enqueue_script( 'nimbus-functions', get_theme_file_uri( '/assets/js/functions.js'), array( 'jquery','imagesloaded' ), '1.0', true );

    // Register scripts
    wp_register_script( 'nimbus-mousetip', get_theme_file_uri( '/assets/js/tooltips.js' ), array( 'jquery' ), '1.0', true );
    wp_register_script( 'nimbus-skill', get_theme_file_uri( '/assets/js/skill.js' ), array( 'jquery' ), '1.0', true );
    wp_register_script( 'nimbus-tab', get_theme_file_uri( '/assets/js/tab.js' ), array( 'jquery', 'jquery-ui-tabs' ), '1.0', true );
    wp_register_script( 'nimbus-odemeter', get_theme_file_uri( '/assets/js/odometer.min.js' ), array( 'jquery' ), '1.0', true );
    wp_register_script( 'nimbus-tweetie', get_theme_file_uri( '/assets/js/tweetie.min.js' ), array( 'jquery' ), '1.0', true );
    wp_register_script( 'nimbus-icon', get_theme_file_uri( '/assets/js/icon.js' ), array( 'jquery' ), '1.0', true );


    // Comment reply script
    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }

    wp_dequeue_script('select2');
}
add_action( 'wp_enqueue_scripts', 'nimbus_scripts' );

function nb_custom_heading_style() {
    if ( is_singular() ) {
        $nb_page_thumbnail = wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) );
        $nb_heading_bg_img = nimbus_meta_data('custom_heading_bg_img') ? nimbus_meta_data('custom_heading_bg_img') : $nb_page_thumbnail;
        $nb_heading_bg_color = nimbus_meta_data('custom_heading_bg_color') ? nimbus_meta_data('custom_heading_bg_color') : 'rgba(55,55,55,0.5)';
        if ( $nb_heading_bg_img || $nb_heading_bg_color ) {
            $nb_custom_css = "
                .nb-site-heading .nb-background {
                        background-color:{$nb_heading_bg_color};
                }
                .nb-site-heading .nb-background {
                        background-image:url('{$nb_heading_bg_img}');
                }";
            wp_add_inline_style( 'nimbus-site', $nb_custom_css );
        }
    }
}
add_action( 'wp_enqueue_scripts', 'nb_custom_heading_style' );

/**
 * Add custom image sizes attribute to enhance responsive image functionality
 * for content images.
 *
 * @since Nimbus 1.0
 *
 * @param string $sizes A source size value for use in a 'sizes' attribute.
 * @param array  $size  Image size. Accepts an array of width and height
 *                      values in pixels (in that order).
 * @return string A source size value for use in a content image 'sizes' attribute.
 */
function nimbus_content_image_sizes_attr( $sizes, $size ) {
    $width = $size[0];

    if ( 740 <= $width ) {
        $sizes = '(max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px';
    }

    if ( is_active_sidebar( 'sidebar-1' ) || is_archive() || is_search() || is_home() || is_page() ) {
        if ( ! ( is_page() && 'one-column' === get_theme_mod( 'page_options' ) ) && 767 <= $width ) {
             $sizes = '(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px';
        }
    }

    return $sizes;
}
add_filter( 'wp_calculate_image_sizes', 'nimbus_content_image_sizes_attr', 10, 2 );

/**
 * Filter the `sizes` value in the header image markup.
 *
 * @since Nimbus 1.0
 *
 * @param string $html   The HTML image tag markup being filtered.
 * @param object $header The custom header object returned by 'get_custom_header()'.
 * @param array  $attr   Array of the attributes for the image tag.
 * @return string The filtered header image HTML.
 */
function nimbus_header_image_tag( $html, $header, $attr ) {
    if ( isset( $attr['sizes'] ) ) {
        $html = str_replace( $attr['sizes'], '100vw', $html );
    }
    return $html;
}
add_filter( 'get_header_image_tag', 'nimbus_header_image_tag', 10, 3 );

/**
 * Add custom image sizes attribute to enhance responsive image functionality
 * for post thumbnails.
 *
 * @since Nimbus 1.0
 *
 * @param array $attr       Attributes for the image markup.
 * @param int   $attachment Image attachment ID.
 * @param array $size       Registered image size or flat array of height and width dimensions.
 * @return string A source size value for use in a post thumbnail 'sizes' attribute.
 */
function nimbus_post_thumbnail_sizes_attr( $attr, $attachment, $size ) {
    if ( is_archive() || is_search() || is_home() ) {
        $attr['sizes'] = '(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px';
    } else {
        $attr['sizes'] = '100vw';
    }

    $attr['class'] = 'nb-img-responsive';

    return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'nimbus_post_thumbnail_sizes_attr', 10, 3 );

require get_parent_theme_file_path('/framework/theme/theme_hook.php');
require get_parent_theme_file_path('/framework/theme/theme_functions.php');
require get_parent_theme_file_path('/framework/theme/theme_admin.php');
require get_parent_theme_file_path('/framework/theme/theme_plugins.php');
if ( class_exists( 'CMB2_Bootstrap_225_Trunk' ) ) {
    require get_parent_theme_file_path('/framework/theme/theme_metaboxed.php');
}
if ( class_exists( 'ReduxFramework' ) ) {
    require get_parent_theme_file_path('/framework/theme/theme_options.php');
}

/**
 * WooCommerce Compatible
 *
 *
 * @package Nimbus
 * @since 1.0
 */
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    require get_parent_theme_file_path('/framework/theme/theme_woocommerce.php');
}

/**
 * Visual Composer Templates
 *
 *
 * @package Nimbus
 * @since 1.0
 */
if ( in_array( 'nimbus/main.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    $dir = WP_PLUGIN_DIR.'/'.plugin_dir_path( 'nimbus/main.php' ). 'vc_templates';
    if ( function_exists('vc_set_shortcodes_templates_dir'))
        vc_set_shortcodes_templates_dir( $dir );
    if ( function_exists( 'vc_set_default_editor_post_types' ))
        vc_set_default_editor_post_types( array( 'page', 'portfolio','nb_templates' ) );
    add_action( 'vc_before_init', 'nimbus_vcsetastheme' );
    function nimbus_vcsetastheme() {
        if ( function_exists( 'vc_set_as_theme') )
            vc_set_as_theme();
    }
}