<?php
	if(!isset($content_width)) $content_width = 1440;

	// Remove admin bar from site when user is logged in.
	function remove_admin_bar(){
		show_admin_bar(false);
	}
	add_action('after_setup_theme', 'remove_admin_bar');

	// Disable RSS
	remove_action('do_feed_rdf', 'do_feed_rdf', 10, 1);
	remove_action('do_feed_rss', 'do_feed_rss', 10, 1);
	remove_action('do_feed_rss2', 'do_feed_rss2', 10, 1);
	remove_action('do_feed_atom', 'do_feed_atom', 10, 1);

	// Remove RSS links from wp_head.
	function remove_rss_from_wp_head(){
		remove_action('wp_head', 'feed_links', 2);
		remove_action('wp_head', 'feed_links_extra', 3);
	}
	add_action('wp_head', 'remove_rss_from_wp_head', 1);

	// Remove XMLRPC
	remove_action('wp_head', 'rsd_link');
	remove_action('wp_head', 'wlwmanifest_link');
	add_filter('xmlrpc_enabled', '__return_false');

	// Remove REST API (wp-json)
	remove_action('wp_head', 'rest_output_link_wp_head', 10);
	remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);

	// Remove WP Emoji
	remove_action('wp_head', 'print_emoji_detection_script', 7);
	remove_action('wp_print_styles', 'print_emoji_styles');
	remove_action('admin_print_scripts', 'print_emoji_detection_script');
	remove_action('admin_print_styles', 'print_emoji_styles');

	// Turn off oEmbed auto discovery.
	remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10); // Don't filter oEmbed results.
	remove_action('wp_head', 'wp_oembed_add_discovery_links'); // Remove oEmbed discovery links.
	remove_action('wp_head', 'wp_oembed_add_host_js'); // Remove oEmbed-specific JavaScript from the front-end and back-end.

	// Change the text at the of the excerpt
	function new_excerpt_more($more){
		global $post;
		return '...';
	}
	add_filter('excerpt_more', 'new_excerpt_more');
	add_filter('excerpt_length', 'my_excerpt_length');
	function my_excerpt_length($len){return 40;}

	// Remove posts and pages
	add_action('admin_menu', 'my_remove_menu_pages');
	function my_remove_menu_pages(){
		//remove_menu_page('edit.php');
		//remove_menu_page('edit.php?post_type=page');
		remove_menu_page('edit-comments.php');
	}

	// Add thumbs
	if(function_exists('add_theme_support')){
		add_theme_support('post-thumbnails');
		set_post_thumbnail_size(1060, 655, true);
		//add_image_size('##nomeaqui', 1120, 810, true);
	}

	// Get the first image from a post
	function catch_that_image(){
		global $post, $posts;
		$first_img = '';
		ob_start();
		ob_end_clean();
		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
		$first_img = $matches [1] [0];
		if(empty($first_img)) $first_img = ''; //Defines a default image
		return $first_img;
	}

	// Se a busca só retornar um resultado, ir direto para o post
	function single_result(){  
		if(is_search()){  
			global $wp_query;  
			if($wp_query->post_count == 1){  
				wp_redirect(get_permalink($wp_query->posts['0']->ID));  
			}
		}
	}
	add_action('template_redirect', 'single_result');

	// Mostrar apenas posts no resultado da busca
	function searchfilter($query){
		if ($query->is_search && !is_admin()){
			$query->set('post_type',array('post'));
		}
		return $query;
	}
	add_filter('pre_get_posts','searchfilter');

	// Compressing JPEG Files to 80% quality
	// add_filter('jpeg_quality', create_function('', 'return 80;'));

	// Redirecionamentos, caso os custom post types não tenham internas
	/*function custom_redirects() {
		if(is_post_type_archive(array('banner')) || is_singular(array('banner'))){
			wp_redirect(home_url());
			exit;
		}
	}
	add_action('template_redirect', 'custom_redirects');*/
	
	if ( ! function_exists( 'royal_setup' ) ) :

		function royal_setup() {
	

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


			//Images featured blog
			add_image_size( 'featured-index', 1000, 811, true );
			add_image_size( 'related-index', 500, 284, true );
			add_image_size( 'featured-archive', 1200, 800, true );
			add_image_size( 'related-archive', 600, 600, true );
			add_image_size( 'list-archive', 800, 600, true );
			add_image_size( 'featured-single', 1600, 900, true );
			add_image_size( 'featured-faq', 700, 700, true );
			add_image_size( 'featured-faq-small', 820, 640, true );
			add_image_size( 'featured-faq-learn', 1500, 940, true );
			add_image_size( 'featured-faq-list', 450, 300, true );
			

			// This theme uses wp_nav_menu() in one location.
			register_nav_menus( array(
				'brands' => __( 'Brands', 'royal' ),
				'news' => __( 'News', 'royal' ),
				'engage' => __( 'Engage', 'royal' ),
				'footer' => __( 'Footer', 'royal' ),
			) );

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

        /*
         * Enable support for Post Formats.
         * See http://codex.wordpress.org/Post_Formats
         */
        add_theme_support( 'post-formats', array('video', 'audio' ) );
	
		}
	endif; // infinity_setup
	add_action( 'after_setup_theme', 'royal_setup' );

	if( function_exists('acf_add_options_page') ) {
		// add parent
		$parent = acf_add_options_page(array(
			'page_title' 		=> 'Social Network',
			'menu_title' 		=> 'Social Network',
			'menu_slug' 		=> 'theme',
			'icon_url' 			=> 'dashicons-share'
		));
	}

/**
 * Enqueue scripts and styles.
 * ==========================
 */
define( 'THEME_ROOT', get_template_directory_uri() );

	function safari_scripts() {

		$version = rand(0, 9999);

		wp_enqueue_script('safari-functions', THEME_ROOT .'/ajax/ajax.js',   array( 'jquery' ), $version, true);


		$wpVars = [
			'ajaxurl' => admin_url('admin-ajax.php')
		];

		wp_localize_script('safari-functions', 'wp',$wpVars);

	}

add_action( 'wp_enqueue_scripts', 'safari_scripts' );





	/**
	* Custon Post Brands
	* ==========================
	*/
	function brands(){
		$labels = array(
		
		'name' => "Brands",
		'singular_name' => "Brands",
		'add_new'=> "Add New Brand",
		'add_new_label' => "Add New Brand",
		'all_item' => "All Brands",
		'add_new_item'=> "Add New Brand",
		'edit_item' => "Edit Brand",
		'new_item' =>  "New Brand",
		'view_item' => "View Brand",
		'search_item' => "Search Brand",
		'not_found' => "No Brand Found",
		'not_found_in_trash' => "No Brand Found in Trash"
		);
		$args = array(
		
		'labels' => $labels,
		'public' => true,
		'has_archive' => true,
		'publicly_queryable' => true,
		'query_var' => true,
		'rewrite' => true,
		'capability_type' => 'post',
		'supports' => array(
		'title'
		),
		'menu_position' => 5,
		'exclude_from_search' => false,
		'menu_icon' => 'dashicons-star-filled'
		);
		register_post_type('brands',$args);
	}
	add_action('init','brands');

	/**
	* Custon Post Ships
	* ==========================
	*/
	function ships(){
		$labels = array(
		
		'name' => "Ships",
		'singular_name' => "Ships",
		'add_new'=> "Add New Ship",
		'add_new_label' => "Add New Ship",
		'all_item' => "All Ships",
		'add_new_item'=> "Add New Ship",
		'edit_item' => "Edit Ship",
		'new_item' =>  "New Ship",
		'view_item' => "View Ship",
		'search_item' => "Search Ship",
		'not_found' => "No Ship Found",
		'not_found_in_trash' => "No Ship Found in Trash"
		);
		$args = array(
		
		'labels' => $labels,
		'public' => true,
		'has_archive' => true,
		'publicly_queryable' => true,
		'query_var' => true,
		'rewrite' => true,
		'capability_type' => 'post',
		'supports' => array(
		'title'
		),
		'menu_position' => 5,
		'exclude_from_search' => false,
		'menu_icon' => 'dashicons-flag'
		);
		register_post_type('ships',$args);
	}
	add_action('init','ships');

/*-------------------------------------------------------------------------------
	Custom Columns Ships Admin
-------------------------------------------------------------------------------*/

function my_page_columns($columns)
{
	$columns = array(
		'cb'	 	=> '<input type="checkbox" />',
		'title' 	=> 'Title',
		'brand' 	=> 'Brand',
		'date'		=>	'Date',
	);
	return $columns;
}

function my_custom_columns($column)
{
	global $post;
	if($column == 'brand')
	{
		$brands = get_field('select_brand');
		echo $brands->post_title;
	}
}

add_action("manage_posts_custom_column", "my_custom_columns");
add_filter("manage_ships_posts_columns", "my_page_columns");

/**
 * Force size of crop images
 * ==========================
 */

class rd_ThumbnailUpscaler
{

    static function rd_image_crop_dimensions($default, $orig_w, $orig_h, $new_w, $new_h, $crop)
    {
        if(!$crop)
            return null;

        $aspect_ratio = $orig_w / $orig_h;
        $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);

        $crop_w = round($new_w / $size_ratio);
        $crop_h = round($new_h / $size_ratio);

        $s_x = floor( ($orig_w - $crop_w) / 2 );
        $s_y = floor( ($orig_h - $crop_h) / 2 );

        return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
    }
}

add_filter('image_resize_dimensions', array('rd_ThumbnailUpscaler', 'rd_image_crop_dimensions'), 10, 6);


/**
 * Estimates the reading time for a given piece of $content.
 * ==========================
 */


function prefix_estimated_reading_time( $content = '', $wpm = 300 ) {
	$clean_content = strip_shortcodes( $content );
	$clean_content = strip_tags( $clean_content );
	$word_count = str_word_count( $clean_content );
	$time = ceil( $word_count / $wpm );
	return $time;
}

/**
 * Function FAQ
 * ==========================
 */

function faq(){
    $labels = array(

        'name' => "FAQ",
        'singular_name' => "FAQ",
        'add_new'=> "Add New Question",
        'add_new_label' => "Add New Question",
        'all_item' => "All Question",
        'add_new_item'=> "Add New Question",
        'edit_item' => "Edit Quastion",
        'new_item' =>  "New Question",
        'view_item' => "View Question",
        'search_item' => "Search Question",
        'not_found' => "Not Found",
        'not_found_in_trash' => "Not Found in Trash"
    );
    $args = array(

        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'publicly_queryable' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'supports' => array(
            'title', 'thumbnail', 'post-formats'
        ),
        'menu_position' => 5,
        'exclude_from_search' => false,
        'menu_icon' => 'dashicons-welcome-comments'
    );
    register_post_type('faq',$args);
}
add_action('init','faq');

function create_taxonomy_faq() {
    $singular = 'Category';
    $plural = 'Categories';

    $labels = array(
        'name' => $plural,
        'singular_name' => $singular,
		'view_item' => 'View ' . $singular,
		'all_items' => 'All' . $plural,
        'edit_item' => 'Edit ' . $singular,
        'new_item' => 'New ' . $singular,
		'add_new_item' => 'Add new ' . $singular,
		'update_item' => 'Update' . $singular
        );

    $args = array(
        'labels' => $labels,
        'public' => true,
		'hierarchical' => true,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'category-faq' ),
        );

    register_taxonomy('create_taxonomy_faq', 'faq', $args);
}

add_action( 'init' , 'create_taxonomy_faq' );

function create_taxonomy_tag_faq() {
    $singular = 'Tag';
    $plural = 'Tags';

    $labels = array(
        'name' => $plural,
        'singular_name' => $singular,
		'view_item' => 'View ' . $singular,
		'all_items' => 'All' . $plural,
        'edit_item' => 'Edit ' . $singular,
        'new_item' => 'New ' . $singular,
		'add_new_item' => 'Add new ' . $singular,
		'update_item' => 'Update' . $singular
        );

    $args = array(
        'labels' => $labels,
        'public' => true,
		'hierarchical' => false,
		'show_ui' => true,
    	'query_var' => true,
    	'rewrite' => array( 'slug' => 'tag-faq' ),
        );

    register_taxonomy('faq-tag', 'faq', $args);
}

add_action( 'init' , 'create_taxonomy_tag_faq' );

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


/**
 * Filter the upload size limit for non-administrators.
 *
 * @param string $size Upload size limit (in bytes).
 * @return int (maybe) Filtered size limit.
 */
function filter_site_upload_size_limit( $size ) {
    // Set the upload size limit to 10 MB for users lacking the 'manage_options' capability.
    if ( ! current_user_can( 'manage_options' ) ) {
        // 10 MB.
        $size = 1024 * 10000;
    }
    return $size;
}
add_filter( 'upload_size_limit', 'filter_site_upload_size_limit', 20 );


/**
 * 
 * Upload SVG in wp admin
 * 
 */

function add_file_types_to_uploads($file_types){

    $new_filetypes = array();
    $new_filetypes['svg'] = 'image/svg';
    $file_types = array_merge($file_types, $new_filetypes );

    return $file_types;
}
add_action('upload_mimes', 'add_file_types_to_uploads');



/**
 * Includes Posts Ajax
 * ==========================
 */

include ('ajax/list-post.php');
include ('ajax/list-faq.php');
include ('ajax/list-news.php');