<?php 
/**
 *  Require all the functions 
 */
require_once('wp-bootstrap-navwalker.php');
require_once('inc/form-functions.php');


// Custom javascript
function wpbootstrap_scripts_with_jquery() {
	// Register boostrap javascript
	wp_register_script( 'bootstrap-script', get_template_directory_uri() . '/bootstrap/js/bootstrap.js', array( 'jquery' ) );
	// Register the icon script to be able to use icons across the site
	wp_register_script( 'fontawesome-script', 'https://use.fontawesome.com/0fde72e8c9.js');
	wp_enqueue_script( 'fontawesome-script' );
	// Register the slick script for sliders
	wp_register_script( 'slick-script', get_template_directory_uri() . '/slick/slick.min.js' );
	wp_enqueue_script( 'slick-script' );
	// Register our own javascript
	wp_register_script( 'custom-script', get_template_directory_uri() . '/js/functions.js' , array('bootstrap-script'));
	wp_enqueue_script( 'custom-script' );
}

add_action( 'wp_enqueue_scripts', 'wpbootstrap_scripts_with_jquery' );

// Custom cadcading style sheets
function enqueue_styles() {
	// Boostrap style
   	wp_enqueue_style( 'bootstrap-css', get_template_directory_uri() . '/bootstrap/css/bootstrap.min.css');
   	// Add click theme and style
    // wp_enqueue_style( 'slick-theme', get_template_directory_uri() . '/slick/slick-theme.css');
   	wp_enqueue_style( 'slick-style', get_template_directory_uri() . '/slick/slick.css');
   	// Custom Style Sheet
    wp_enqueue_style( 'stylesheet', get_template_directory_uri() . '/style.css');
    // Google fonts
    wp_enqueue_style( 'roboto-font', 'https://fonts.googleapis.com/css?family=Roboto:100,300,400');
    wp_enqueue_style( 'varela-font', 'https://fonts.googleapis.com/css?family=Varela+Round' );
}

add_action( 'wp_enqueue_scripts', 'enqueue_styles' );

// Custom menu
function wpb_custom_new_menu() {
	register_nav_menu('header-nav',__( 'Header Nav' ));
	register_nav_menu('footer-nav',__( 'Footer Nav' ));
}

add_action( 'init', 'wpb_custom_new_menu' );



/** 
 *  Displays the recent posts for given post type
 */
function ms_recent_posts( $atts = array(), $content = null, $tag = null ) {

	shortcode_atts(array(
        'post_type' => 'post',
        'numberposts' => 4,
        'heading_text' => 'Recent Posts' 
    ), $atts);

    $args = array(
    	'post_status' => 'publish', 
        'post_type' => $atts['post_type'],
        'orderby' => 'date',
        'posts_per_page' => $atts['numberposts'],
        'order' => 'ASC'
    );

    $out =  '<div class="ms-background light-grey">';
    $out .= '<div class="container">';
    $out .= '<div class="row ms-recent-posts-row">';
    $out .= '<h2 class="col-12">' . $atts['heading_text'] . '</h2>';
    $the_query2 = new WP_Query( $args ); 

 	if ( $the_query2->have_posts() ) {
		while ( $the_query2->have_posts() ) {
			$the_query2->the_post();
			$img = get_field("image_1"); 
			$thumb = $img['sizes']['medium'];
			
			$out .= '<a href="' . get_permalink() . '" class="col-12 col-sm-6 col-md-4 col-lg-3 ms-post-multi">';

	        $out .= 	'<div class="ms-post-thumbnail">';

	        $out .= 		'<img src="' . $thumb . '" alt="' . $img['alt'] . '">';

	        $out .= 	'</div>';

	        $out .= 	'<div class="ms-post-info">';

	        $out .= 		'<h5>' . get_the_title() . '</h5>';

	       	$out .= 		'<p class="ms-post-caption">' . get_field("thumbnail_description") .' </p>';

	        $out .= 	'</div>'; 

			$out .= '</a>'; // end .ms-post-info
		}
		wp_reset_postdata(); 
	} else {
 		_e( 'Sorry, no posts matched your criteria.' );
	}

	$out .= '</div>'; // end .ms-recent-posts-row
	$out .= '</div>'; // end .container
	$out .= '</div>'; // end .full-background

	if ( $tag ) {
        return $out;
    } else {
        echo $out;
    }
}
// link to shortcode [ms_recent_posts post_type="post" numberposts=4 heading_text="Recent Posts"]
add_shortcode( 'ms_recent_posts', 'ms_recent_posts' );


// Allow uploading of SVGs
function add_svg_to_upload_mimes( $upload_mimes ) {
	$upload_mimes['svg'] = 'image/svg+xml';
	$upload_mimes['svgz'] = 'image/svg+xml';
	return $upload_mimes;
}
add_filter( 'upload_mimes', 'add_svg_to_upload_mimes', 10, 1 );


// Stop wordpress adding <p></p> tags automatically
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
// Add HTML tags to taxonomies
remove_filter( 'pre_term_description', 'wp_filter_kses' );
remove_filter( 'pre_link_description', 'wp_filter_kses' );
remove_filter( 'pre_link_notes', 'wp_filter_kses' );
remove_filter( 'term_description', 'wp_kses_data' );

?>