<?php
add_action('wp_enqueue_scripts', 'artzone_script_enqueue'); //scripts
add_action('init', 'artzone_theme_setup'); //theme config
add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2); //highlight custom post in menu
add_action('init', 'artzone_custom_type'); //custom fields
//add_action('phpmailer_init', 'configure_smtp'); //configure smtp


/* ===============
 * Custom scripts
 * ===============*/
function artzone_script_enqueue() {
    //JS
    wp_enqueue_script('jquery-custom', get_template_directory_uri() . '/js/jquery.min.js', array(), '1.0', true);
    wp_enqueue_script('materialize', get_template_directory_uri() . '/js/materialize.min.js', array(), '1.0', true);
    wp_enqueue_script('owl', get_template_directory_uri() . '/js/owl.carousel.min.js', array(), '1.0', true);
    wp_enqueue_script('jq-isotope', get_template_directory_uri() . '/js/jquery.isotope.min.js', array(), '1.0', true);
    wp_enqueue_script('isotope-initialize', get_template_directory_uri() . '/js/isotope-initialize.js', array(), '1.0', true);
    wp_enqueue_script('enscroll', get_template_directory_uri() . '/js/enscroll-0.5.2.min.js', array(), '1.0', true);
    wp_enqueue_script('magnifcjs', get_template_directory_uri() . '/js/magnifc-popup.min.js', array(), '1.0', true);
    wp_enqueue_script('custom', get_template_directory_uri() . '/js/script.js', array(), '1.0', true);

    //REVOLUTION JS FILES
    wp_enqueue_script('themepunch-tools', get_template_directory_uri() . '/js/revolution/jquery.themepunch.tools.min.js', array(), '1.0', true);
    wp_enqueue_script('themepunch-revolution', get_template_directory_uri() . '/js/revolution/jquery.themepunch.revolution.min.js', array(), '1.0', true);

    //SLIDER REVOLUTION 5.0 EXTENSIONs
    wp_enqueue_script('extension-actions', get_template_directory_uri() . '/js/revolution/revolution.extension.actions.min.js', array(), '1.0', true);
    wp_enqueue_script('extension-carousel', get_template_directory_uri() . '/js/revolution/revolution.extension.carousel.min.js', array(), '1.0', true);
    wp_enqueue_script('extension-kenburn', get_template_directory_uri() . '/js/revolution/revolution.extension.kenburn.min.js', array(), '1.0', true);
    wp_enqueue_script('extension-layeranimation', get_template_directory_uri() . '/js/revolution/revolution.extension.layeranimation.min.js', array(), '1.0', true);
    wp_enqueue_script('extension-migration', get_template_directory_uri() . '/js/revolution/revolution.extension.migration.min.js', array(), '1.0', true);
    wp_enqueue_script('extension-navigation', get_template_directory_uri() . '/js/revolution/revolution.extension.navigation.min.js', array(), '1.0', true);
    wp_enqueue_script('extension-parallax', get_template_directory_uri() . '/js/revolution/revolution.extension.parallax.min.js', array(), '1.0', true);
    wp_enqueue_script('extension-slideanims', get_template_directory_uri() . '/js/revolution/revolution.extension.slideanims.min.js', array(), '1.0', true);
    wp_enqueue_script('extension-video', get_template_directory_uri() . '/js/revolution/revolution.extension.video.min.js', array(), '1.0', true);
    wp_enqueue_script('extension-initialize', get_template_directory_uri() . '/js/revolution/revolution.initialize.js', array(), '1.0', true);

    //CSS
    wp_enqueue_style('color', get_template_directory_uri() . '/css/color.css', array(), '1.0', 'all');
    wp_enqueue_style('icons', get_template_directory_uri() . '/css/icons.css', array(), '1.0', 'all');
    wp_enqueue_style('materialize', get_template_directory_uri() . '/css/materialize.min.css', array(), '1.0', 'all');
    wp_enqueue_style('style', get_template_directory_uri() . '/css/style.css', array(), '1.0', 'all');
    wp_enqueue_style('revolution-settings', get_template_directory_uri() . '/css/revolution/settings.css', array(), '1.0', 'all');
    wp_enqueue_style('revolution-navigation', get_template_directory_uri() . '/css/revolution/navigation.css', array(), '1.0', 'all');
    wp_enqueue_style('revolution-stroke', get_template_directory_uri() . '/css/revolution/pe-icon-7-stroke/css/pe-icon-7-stroke.css', array(), '1.0', 'all');
    wp_enqueue_style('responsive', get_template_directory_uri() . '/css/responsive.css', array(), '1.0', 'all');
    wp_enqueue_style('magnificcss', get_template_directory_uri() . '/css/magnific-popup.css', array(), '1.0', 'all');

}

/* ===============
 * Theme setup
 * ===============*/
function artzone_theme_setup() {
    add_theme_support('menus');
    register_nav_menus(array('main' => 'Menu Główne'));

    //acf options support
    if (function_exists('acf_add_options_page')) {
        acf_add_options_page();
    }
}

function add_current_nav_class($classes, $item) {
    global $post;
    $id = (isset($post->ID) ? get_the_ID() : NULL);

    if (isset($id)) {
        $current_post_type = get_post_type_object(get_post_type($post->ID));
        $current_post_type_slug = $current_post_type->rewrite['slug'];
        $menu_slug = strtolower(trim($item->url));

        if (strpos($menu_slug, $current_post_type_slug) !== false) {

            $classes[] = 'current-menu-item';
        }
    }
    return $classes;
}

/* ================
 * Custom functions
 * ===============*/
function configure_smtp(PHPMailer $phpmailer) {
    $phpmailer->isSMTP();
    $phpmailer->Host = 'avantura.nazwa.pl';
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = 465;
    $phpmailer->Username = 'noreply@avantura.nazwa.pl';
    $phpmailer->Password = '6CEWrLWamj';
    $phpmailer->SMTPSecure = 'ssl';
    $phpmailer->From = 'noreply@avantura.nazwa.pl';
    $phpmailer->FromName = '';
    //$phpmailer->SMTPDebug  = 2;
    $phpmailer->isHTML(true);
    //var_dump($phpmailer); exit;
}


/* ===============
 * Custom types (sklepy, uslugi, prmocje)
 * ===============*/
function artzone_custom_type() {
    $taxonomyLabels = [
        'name' => '',
        'singular_name' => 'Kategoria',
        'search_items' =>  'Znajdź',
        'all_items' => 'Wszystkie kategorie',
        'parent_item' => 'Kategoria nadrzędna',
        'parent_item_colon' => 'Kategoria nadrzędna',
        'edit_item' => 'Edytuj kategorię',
        'update_item' => 'Aktualizuj kategorię',
        'add_new_item' => 'Dodaj nową kategorię',
        'new_item_name' => 'Nazwa kategorii',
        'menu_name' => 'Kategorie',
    ];

    //portfolio
    $customPostLabels = array(
        'name' => 'Portfolio',
        'singular_name' => 'Portfolio',
        'menu_name' => 'Portfolio',
        'name_admin_bar' => 'Portfolio',
        'add_new' => 'Dodaj Portfolio',
        'add_new_item' => 'Dodaj Portfolio',
        'new_item' => 'Nowe portfolio',
        'edit_item' => 'Edytuj Portfolio',
        'view_item' => 'Zobacz Portfolio',
        'all_items' => 'Wszystkie portfolia',
        'search_items' => 'Wyszukaj portfolio',
        'not_found' => 'Brak elementów do wyświetlenia',
        'not_found_in_trash' => 'Nie znaleziono portfolio w koszu',
        'parent_item_colon' => 'Parent colon',
    );

    $customPostArgs = array(
        'labels' => $customPostLabels,
        'public' => true,
        'description' => 'Portfolio',
        'has_archive' => true,
        'show_in_menu' => true,
        'publicly_queryable' => true,
        'query_var' => true,
        'capability_type' => 'post',
        //'hierarchical' => false,
        'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
        'menu_position' => 5,
        'menu_icon' => 'dashicons-art',
        'taxonomies' => array('portfolio_cat'),
        'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
    );

    register_post_type('portfolio', $customPostArgs);
    $taxonomyLabels['name'] = 'Kategorie - portfolio';
    register_taxonomy('portfolio_cat',array('portfolio'), array(
        'hierarchical' => true,
        'labels' => $taxonomyLabels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'portfolio'),
    ));

    //service
    $customPostLabels = array(
        'name' => 'Usługa',
        'singular_name' => 'Usługa',
        'menu_name' => 'Usługi',
        'name_admin_bar' => 'Usługi',
        'add_new' => 'Dodaj usługę',
        'add_new_item' => 'Dodaj usługę',
        'new_item' => 'Nowa usługa',
        'edit_item' => 'Edytuj usługę',
        'view_item' => 'Zobacz usługę',
        'all_items' => 'Wszystkie usługi',
        'search_items' => 'Wyszukaj usługę',
        'not_found' => 'Brak elementów do wyświetlenia',
        'not_found_in_trash' => 'Nie znaleziono usługi w koszu',
        'parent_item_colon' => 'Parent colon',
    );

    $customPostArgs = array(
        'labels' => $customPostLabels,
        'public' => true,
        'description' => 'Usługa',
        'has_archive' => true,
        'show_in_menu' => true,
        'publicly_queryable' => true,
        'query_var' => true,
        'capability_type' => 'post',
        //'hierarchical' => false,
        'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
        'menu_position' => 6,
        'menu_icon' => 'dashicons-star-filled',
        'taxonomies'  => array('category'),
        'rewrite' => array('slug' => 'service'),
    );

    register_post_type('service', $customPostArgs);

    flush_rewrite_rules();
}

function artzone_dump($var) {
    echo '<pre>';
    print_r($var);
    echo '</pre>';
}

add_filter('acf/settings/google_api_key', function () {
    return 'AIzaSyBF7r8pfir1EWwkhZ86JZJjnrFMgOxLy_I';
});


function artzone_get_posts_by_type($type, $limit = -1, $offset = null, $orderBy = 'title', $orderType = 'ASC') {
    $args = array(
        'post_type' => $type,
        'post_status' => 'publish',
        'posts_per_page' => $limit,
        'caller_get_posts'=> 1,
        'orderby' => $orderBy,
        'order' => $orderType
    );

    if ($offset) { $args['offset'] = $offset; }

    $q = new WP_Query($args);
    return $q->get_posts();
}

function artzone_get_portfolio_categories() {
    $portfolioCats = get_terms('portfolio_cat', ['hide_empty' => false, 'order_by' => 'id']);
    $categories = [];

    foreach ($portfolioCats as $cat) {
        $categories[] = $cat->name;
    }

    //foreach (artzone_get_posts_by_type('portfolio') as $item ) {
    //    $categories[] = get_field('category_name', $item->ID);
    //}

    return array_unique($categories);
}

function artzone_get_portfolio_category_classes($categories) {
    $categoryString = '';

    if ($categories) {
        foreach ($categories as $category) {
            $categoryString .=' ' . str_replace(' ', '_', $category->name);
        }
    }

    return $categoryString;
}

/**
 * This function is in test. Allows to create taxonomies with same slug as custom post
 *
 * @param $wp_rewrite
 */
function taxonomy_slug_rewrite($wp_rewrite) {
    //exit();
    $rules = array();
    // get all custom taxonomies
    $taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
    // get all custom post types
    $post_types = get_post_types(array('public' => true, '_builtin' => false), 'objects');

    foreach ($post_types as $post_type) {
        foreach ($taxonomies as $taxonomy) {

            // go through all post types which this taxono07/my is assigned to
            foreach ($taxonomy->object_type as $object_type) {

                // check if taxonomy is registered for this custom type
                if ($object_type == $post_type->rewrite['slug']) {

                    // get category objects
                    $terms = get_categories(array('type' => $object_type, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));

                    // make rules
                    foreach ($terms as $term) {
                        $rules[$object_type . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
                    }
                }
            }
        }
    }
    // merge with global rules
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'taxonomy_slug_rewrite');


function lot_get_last_portfolio() {
    $all = artzone_get_posts_by_type('portfolio', -1, null, 'date');
    return $all[\count($all) -1];
}

function lot_get_first_portfolio() {
    $all = artzone_get_posts_by_type('portfolio', -1, null, 'date');
    return $all[0];
}