<?php

function czarogrody_scripts() {
	wp_deregister_script( 'jquery' );
	wp_enqueue_style( 'czarogrody-styles', get_template_directory_uri() . '/dist/styles/style.min.css' );
	wp_enqueue_script( 'jquery', get_template_directory_uri() . '/dist/scripts/jquery.min.js' );
	wp_enqueue_script( 'czarogrody-scripts', get_template_directory_uri() . '/dist/scripts/scripts.min.js' );
}

add_action( 'wp_enqueue_scripts', 'czarogrody_scripts' );


if ( function_exists( 'add_image_size' ) )

add_theme_support('post-thumbnails');
	add_image_size('large', 620, 565, array( 'center', 'center' ) );
	add_image_size('small', 150, 150, array( 'center', 'center' ) );
	add_image_size('home-slider', 600, 450, false);
	add_image_size('post', 950, 950, array( 'center', 'center' ) );
	add_image_size('full', 1440, 615, false);
	add_image_size('post-intro', 415, 240, false);
	add_image_size('p-lists', 750, 450, array( 'center', 'center' ) );
	add_image_size('category', 800, 600, false );


function get_template_name() {
	$template = basename(get_page_template());
	$template = substr($template, 5);
	$template = substr($template, 0, -4);

	return $template;
}

$includes = array(
	'lib/setup.php',					// Theme setup
);
foreach ($includes as $file) {
	if (file_exists(__DIR__ . '/' . $file)) {
		require_once($file);
	}
}

add_action('after_setup_theme', 'czarogrody_languages');
function czarogrody_languages(){
    load_theme_textdomain('czarogrody', get_template_directory() . '/lang');
}


function czarogrody_menu() {
	register_nav_menus(
		array(
			'main-menu' => __( 'Main Menu' ),
			'main-menu-site' => __( 'Main Menu Site' ),
			'footer-menu' => __( 'Footer Menu' ),
 		)
	);
}
add_action( 'init', 'czarogrody_menu' );


function pagination_nav() {
    global $wp_query;

    if ( $wp_query->max_num_pages > 1 ) { ?>
        <nav class="pagination" role="navigation">
            <div class="nav-previous"><?php next_posts_link( '&larr; Older posts' ); ?></div>
            <div class="nav-next"><?php previous_posts_link( 'Newer posts &rarr;' ); ?></div>
        </nav>
<?php }
}


/**Start pagination_nav**/
function numeric_posts_nav() {

    if( is_singular() )
        return;

    global $wp_query;

    /** Stop execution if there's only 1 page */
    if( $wp_query->max_num_pages <= 1 )
        return;

    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $wp_query->max_num_pages );

    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;

    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }

    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }

    echo '<div class="navigation"><ul>' . "\n";

    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( '<li>%s</li>' . "\n", get_previous_posts_link() );

    /** Link to first page, plus ellipses if necessary */
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? ' class="active"' : '';

        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

        if ( ! in_array( 2, $links ) )
            echo '<li>…</li>';
    }

    /** Link to current page, plus 2 pages in either direction if necessary */
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    }

    /** Link to last page, plus ellipses if necessary */
    if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
            echo '<li>…</li>' . "\n";

        $class = $paged == $max ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }

    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '<li>%s</li>' . "\n", get_next_posts_link() );

    echo '</ul></div>' . "\n";

}

/**End Pagination**/
function custom_excerpt_length( $length ) {
	return 50;
}

add_filter( 'excerpt_length', 'custom_excerpt_length', 20 );

function et_excerpt_more($more) {
	global $post;
	return '<div class="post-more">
	<a href="'. get_permalink($post->ID) . '" class="link">' . __('Read More', 'czarogrody') . '</a>
	</div>';
}
add_filter('excerpt_more', 'et_excerpt_more');


/**
 * Disable the emoji's
 */
function disable_emojis() {
 remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
 remove_action( 'wp_print_styles', 'print_emoji_styles' );
 remove_action( 'admin_print_styles', 'print_emoji_styles' );
 remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
 remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
 remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
 add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
 add_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 );
}
// add_action( 'init', 'disable_emojis' );

/**
 * Filter function used to remove the tinymce emoji plugin.
 *
 * @param array $plugins
 * @return array Difference betwen the two arrays
 */
function disable_emojis_tinymce( $plugins ) {
 if ( is_array( $plugins ) ) {
 return array_diff( $plugins, array( 'wpemoji' ) );
 } else {
 return array();
 }
}


/**
 * Remove emoji CDN hostname from DNS prefetching hints.
 *
 * @param array $urls URLs to print for resource hints.
 * @param string $relation_type The relation type the URLs are printed for.
 * @return array Difference betwen the two arrays.
 */
function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
 if ( 'dns-prefetch' == $relation_type ) {
 /** This filter is documented in wp-includes/formatting.php */
 $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );

$urls = array_diff( $urls, array( $emoji_svg_url ) );
 }

return $urls;
}

add_filter('next_post_link', 'post_link_attributes');
add_filter('previous_post_link', 'post_link_attributes');

function post_link_attributes($output) {
    $code = 'class="button post-nav"';
    return str_replace('<a href=', '<a '.$code.' href=', $output);
}

?>
