<?php

/**
 * 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 bone_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'bone_content_width', 954 );
}
add_action( 'after_setup_theme', 'bone_content_width', 0 );

/**
 * Bone only works in WordPress 4.1 or later.
 */
if ( version_compare( $GLOBALS['wp_version'], '4.1-alpha', '<' ) ) {
  require get_template_directory() . '/inc/back-compat.php';
}

/*
 * Add SNS options to the user settings.
 */
function bone_add_user_contact_form() {
	return array(
		'twitter'   => 'Twitter',
		'facebook'  => 'Facebook',
		'plus'      => 'Google+',
		'youtube'   => 'YouTube',
		'line'      => 'LINE',
		'instagram' => 'Instagram',
	);
}
add_filter( 'user_contactmethods', 'bone_add_user_contact_form' );

if ( ! function_exists( 'bone_setup' ) ) :
/**
 * 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.
 *
 * @since Bone 1.0
 */
function Bone_setup() {

	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on Bone, use a find and replace
	 * to change 'Bone' to the name of your theme in all the template files
	 */
	load_theme_textdomain( 'bone', get_template_directory() . '/languages' );

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

	/*
	 * For Jetpack Infinite Scroll
	 */
	if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'infinite-scroll' ) ) {
		add_theme_support( 'infinite-scroll', array(
			'container' => 'content',
			'wrapper' => false,
		) );
	}

	/*
	 * 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');

	/*
	 * Add Excerpt to Page
	 */
	add_post_type_support( 'page', 'excerpt' );

	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 */
	function bone_custom_excerpt_more( $more ) {
		return __( '...', 'bone' );
	}
	add_filter( 'excerpt_more', 'bone_custom_excerpt_more' );

	/*
	 * Add Thumbnail size for article header.
	 */
	add_image_size( 'article-header', 1050, 280, true );

	/*
	 * Add Thumbnail size for list card.
	 */
	set_post_thumbnail_size( '330, 150, true' );

	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 */
	add_theme_support( 'post-thumbnails' );

	// This theme uses wp_nav_menu() in two locations.
	register_nav_menus( array(
		'side-nav' => __( 'Side Navigation',      'bone' ),
		'footer-nav'  => __( 'Footer Navigation', 'bone' ),
	) );

	/**
	 * Enable support for Post Tags.
	 */
	function bone_add_page_post_tag() {
		register_taxonomy_for_object_type( 'post_tag', 'page' );
	}
	add_action( 'init', 'bone_add_page_post_tag' );

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

	/*
	 * Setup the WordPress core custom background feature.
	 */
	add_theme_support( 'custom-background', array(
		'default-attachment' => 'fixed',
		'default-color' => 'fafafa',
	) );

	/*
	 * Hide the default widget.
	 * The need to avoid a conflict with the header search form.
	 */
	function unregister_default_widget() {
		unregister_widget( 'WP_Widget_Search' );
	}
	add_action( 'widgets_init', 'unregister_default_widget' );

	/*
	 * This theme styles the visual editor to resemble the theme style,
	 * specifically font, colors, icons, and column width.
	 */
	add_editor_style();

}
endif;
add_action( 'after_setup_theme', 'bone_setup' );

/**
 * Register widget area.
 *
 * @since Bone 1.0
 *
 * @link https://codex.wordpress.org/Function_Reference/register_sidebar
 */
function bone_widgets_init() {
	register_sidebar([
	  'name'          => __( 'Widget Area', 'bone' ),
	  'id'            => 'sidebar-1',
	  'description'   => __( 'Add widgets here to appear in your sidebar.', 'bone' ),
	  'before_widget' => '<aside class="bone-card mdl-card mdl-shadow--2dp bone-widget__item %1$s %2$s">',
	  'after_widget'  => '</aside>',
	  'before_title'  => '<h2 class="bone-widget__title">',
	  'after_title'   => '</h2>'
	]);
}
add_action( 'widgets_init', 'bone_widgets_init' );

/**
 * Enqueue scripts and styles.
 *
 * @since Bone 1.0
 */
function bone_scripts() {

	// Add custom style, used in highlight.js.
  wp_enqueue_style( 'bone-syntax-style', get_template_directory_uri() . '/styles/github.css' );

	// Add MDL stylesheet.
  wp_register_style( 'bone-mdl-style', get_template_directory_uri() . '/styles/material.min.css' );

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

  // Load Our Main Style
	wp_enqueue_style( 'bone-main-style', get_stylesheet_uri(), array(
		'bone-syntax-style',
		'bone-mdl-style',
		));

  // Add mdl framework script.
  wp_enqueue_script( 'bone-mdl-script', get_template_directory_uri() . '/scripts/material.min.js', array(), false, true );

  // Add highlight script.
  wp_register_script( 'bone-highlight-script', get_template_directory_uri() . '/scripts/highlight.pack.js', array(), false, true );

  // Load our main script.
  wp_enqueue_script( 'bone-main-script', get_stylesheet_directory_uri() . '/scripts/run.js', array('jquery', 'bone-highlight-script'), false, true );
}
add_action( 'wp_enqueue_scripts', 'bone_scripts' );

/**
 * Implement the Custom Header feature.
 *
 * @since Bone 1.0
 */
require get_template_directory() . '/inc/custom-header.php';

/**
 * Custom template tags for this theme.
 *
 * @since Bone 1.0
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Customizer additions.
 *
 * @since Bone 1.0
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Custom Widget
 *
 * @since Bone 1.0
 */
require get_template_directory() . '/inc/widget.php';
