<?php
/**
 * darklowpress functions and definitions
 *
 * @package darklowpress
 */

if ( ! function_exists( 'darklowpress_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.
 */
function darklowpress_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on darklowpress, use a find and replace
	 * to change 'darklowpress' to the name of your theme in all the template files
	 */
	load_theme_textdomain( 'darklowpress', get_template_directory() . '/languages' );

	// 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 http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
	 */
	 add_theme_support( 'post-thumbnails' );



	

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => esc_html__( 'Primary Menu', 'darklowpress' ),
		'secondary' => esc_html__( 'Footer Menu', 'darklowpress' ),
	) );

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

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

	// Set up the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'darklowpress_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );

	add_theme_support( "custom-header", 
		array(
		'default-color' => 'ffffff',
		'default-image' => '',
			)  
		);
	add_theme_support( 'custom-logo', array(
    'height'      => 70,
    'width'       => 250,
    'flex-height' => true,
    'flex-width'  => true,
		) );
	add_editor_style() ;
}
endif; // darklowpress_setup
add_action( 'after_setup_theme', 'darklowpress_setup' );




/**
 * Enqueue scripts and styles.
 */
function darklowpress_scripts() {
	wp_enqueue_style( 'darklowpress-bootstrap', get_template_directory_uri().'/css/bootstrap.min.css' );	
	wp_enqueue_style( 'darklowpress-fontawesome', get_template_directory_uri().'/css/font-awesome.min.css' );
	wp_enqueue_style( 'darklowpress-style', get_stylesheet_uri() );

	wp_enqueue_script('jquery');
	wp_enqueue_script( 'darklowpress-bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array(), '1.0.0', true );
	wp_enqueue_script( 'darklowpress-scripts', get_template_directory_uri() . '/js/script.js', array(), '1.0.0', true );


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





/**
 * 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
 */
if ( ! isset( $content_width ) ) $content_width = 900;
function darklowpress_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'darklowpress_content_width', 640 );

}
add_action( 'after_setup_theme', 'darklowpress_content_width', 0 );


function darklowpress_filter_front_page_template( $template ) {
    return is_home() ? '' : $template;
}
add_filter( 'front_page_template', 'darklowpress_filter_front_page_template' );





/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function darklowpress_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar', 'darklowpress' ),
		'id'            => 'sidebar-1',
		'description'   => '',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
}
add_action( 'widgets_init', 'darklowpress_widgets_init' );





if ( ! function_exists( 'darklowpress_pagination_bars' ) ) :
	/**
	 * Display navigation to next/previous set of posts when applicable.
	 */
	function darklowpress_pagination_bars() {
		// Don't print empty markup if there's only one page.
		if ( $GLOBALS['wp_query']->max_num_pages < 1 ) {
			return;
		}

		$paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
		$pagenum_link = html_entity_decode( get_pagenum_link() );
		$query_args   = array();
		$url_parts    = explode( '?', $pagenum_link );

		if ( isset( $url_parts[1] ) ) {
			wp_parse_str( $url_parts[1], $query_args );
		}

		$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
		$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';

		$format  = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
		$format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';

		// Set up paginated links.
		$links = paginate_links( array(
			'base'     => $pagenum_link,
			'format'   => $format,
			'total'    => $GLOBALS['wp_query']->max_num_pages,
			'current'  => $paged,
			'mid_size' => 2,
			'add_args' => array_map( 'urlencode', $query_args ),
			'prev_text' => __( '<span aria-hidden="true">&laquo;</span>', 'darklowpress' ),
			'next_text' => __( '<span aria-hidden="true">&raquo;</span>', 'darklowpress' ),
	        'type'      => 'list',
		) );

		if ( $links ) :

		?>
		<ul class="pagination" role="navigation">
			<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'darklowpress' ); ?></h1>
				<?php echo $links; ?>
		</nav><!-- .navigation -->
		<?php
		endif;
	}
endif;





if ( ! function_exists( 'darklowpress_post_nav' ) ) :
	/**
	 * Display navigation to next/previous post when applicable.
	 */
	function darklowpress_post_nav() {
		// Don't print empty markup if there's nowhere to navigate.
		$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
		$next     = get_adjacent_post( false, '', false );

		if ( ! $next && ! $previous ) {
			return;
		}
		?>
		<nav class="navigation post-navigation" role="navigation">
			<h1 class="screen-reader-text"><?php _e( 'Post navigation', 'darklowpress' ); ?></h1>
			<div class="nav-links">
				<?php
					previous_post_link( '<div class="nav-previous">%link</div>', _x( '<span class="meta-nav">&larr;</span>&nbsp;%title', 'Previous post link', 'darklowpress' ) );
					next_post_link(     '<div class="nav-next">%link</div>',     _x( '%title&nbsp;<span class="meta-nav">&rarr;</span>', 'Next post link',     'darklowpress' ) );
				?>
			</div><!-- .nav-links -->
		</nav><!-- .navigation -->
		<?php
	}
endif;





if ( ! function_exists( 'darklowpress_entry_meta' ) ) :
	function darklowpress_entry_meta() {
		if ( is_sticky() && is_home() && ! is_paged() )
			echo '<span class="featured-post">' . __( 'Sticky', 'darklowpress' ) . '</span>';

		if ( ! has_post_format( 'link' ) && 'post' == get_post_type() )
			darklowpress_entry_meta();

		// Translators: used between list items, there is a space after the comma.
		$categories_list = get_the_category_list( __( ', ', 'darklowpress' ) );
		if ( $categories_list ) {
			echo '<span class="categories-links">' . $categories_list . '</span>';
		}

		// Translators: used between list items, there is a space after the comma.
		$tag_list = get_the_tag_list( '', __( ', ', 'darklowpress' ) );
		if ( $tag_list ) {
			echo '<span class="tags-links">' . $tag_list . '</span>';
		}

		// Post author
		if ( 'post' == get_post_type() ) {
			printf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
				esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
				esc_attr( sprintf( __( 'View all posts by %s', 'darklowpress' ), get_the_author() ) ),
				get_the_author()
			);
		}
	}
endif;





if ( ! function_exists( 'darklowpress_posted_on' ) ) :
	function darklowpress_posted_on( $echo = true ) {
		if ( has_post_format( array( 'chat', 'status' ) ) )
			$format_prefix = _x( '%1$s on %2$s', '1: post format name. 2: date', 'darklowpress' );
		else
			$format_prefix = '%2$s';

		$date = sprintf( '<span class="date"><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a></span>',
			esc_url( get_permalink() ),
			esc_attr( sprintf( __( 'Permalink to %s', 'darklowpress' ), the_title_attribute( 'echo=0' ) ) ),
			esc_attr( get_the_date( 'c' ) ),
			esc_html( sprintf( $format_prefix, get_post_format_string( get_post_format() ), get_the_date() ) )
		);

		if ( $echo )
			echo $date;

		return $date;
	}
endif;




if ( ! function_exists( 'darklowpress_get_link_url' ) ) :
function darklowpress_get_link_url() {
	$content = get_the_content();
	$has_url = get_url_in_content( $content );

	return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
}
endif;




// Add it to a column in WP-Admin
add_filter('manage_posts_columns', 'darklowpress_posts_column_views');
add_action('manage_posts_custom_column', 'darklowpress_posts_custom_column_views',5,2);
function darklowpress_posts_column_views($defaults){
    $defaults['post_views'] = __('', 'darklowpress');
    return $defaults;
}
function darklowpress_posts_custom_column_views($column_name, $id){
	if($column_name === 'post_views'){
        echo darklowpress_getPostViews(get_the_ID());
    }
}




add_filter( 'widget_tag_cloud_args', 'darklowpress_tag_cloud_args' );
function darklowpress_tag_cloud_args( $args ) {
	$args['number'] = 10; // Your extra arguments go here
	$args['largest'] = 18;
	$args['smallest'] = 12;
	$args['unit'] = 'px';
	return $args;
}




function darklowpress_excerpt($limit) {
  	$excerpt = explode(' ', get_the_excerpt(), $limit);
  	if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    	$excerpt = implode(" ",$excerpt).'...';
  	} else {
    	$excerpt = implode(" ",$excerpt);
  	} 
  		$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
  	return $excerpt;
}

function darklowpress_content($limit) {
    $content = explode(' ', get_the_content(), $limit);
    if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content).'...';
    } else {
        $content = implode(" ",$content);
    } 
	    $content = preg_replace('/\[.+\]/','', $content);
	    $content = apply_filters('the_content', $content); 
	    $content = str_replace(']]>', ']]&gt;', $content);
    return $content;
}






/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/class.php';

//require get_template_directory() . '/inc/wp_bootstrap_navwalker.php';

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

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

/**
 * Custom functions that act independently of the theme templates.
 */
require get_template_directory() . '/inc/extras.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Load Jetpack compatibility file.
 */
require get_template_directory() . '/inc/jetpack.php';

// Register Custom Navigation Walker
require get_template_directory() . '/inc/wp_bootstrap_navwalker.php';