<?php
/**
 * warego functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package warego
 */

if ( ! defined( '_S_VERSION' ) ) {
	// Replace the version number of the theme on each release.
	define( '_S_VERSION', '0.20' );
}
include('functions/enqueue-dequeue.php');

define( 'MAIN_ABSPATH', rtrim(explode('customer-support/', ABSPATH)[0], '/\\') );

include MAIN_ABSPATH . '/wp-content/themes/waregowp/functions/head.php';


function register_my_menu() {
    register_nav_menu('footer-menu', __('Footer Menu'));
}
add_action('init', 'register_my_menu');


function footer_register_my_menu() {
    register_nav_menu('primary-menu', __('Primary Menu'));
}
add_action('init', 'footer_register_my_menu');

function theme_setup() {
    add_theme_support('custom-logo', array(
        'height'      => 100,
        'width'       => 300,
        'flex-height' => true,
        'flex-width'  => true,
    ));
    add_theme_support('post-thumbnails');

    add_theme_support('title-tag');
}
add_action('after_setup_theme', 'theme_setup');

if( function_exists('acf_add_options_page') ) {
    acf_add_options_page(array(
        'page_title'    => 'Theme Footer Settings',
        'menu_title'    => 'Footer Settings',
        'menu_slug'     => 'footer-settings',
        'capability'    => 'edit_posts',
        'redirect'      => false
    ));
}

function mytheme_widgets_init() {
    register_sidebar(array(
        'name'          => 'Footer Widget Area',
        'id'            => 'footer-widget',
        'before_widget' => '<div class="footer-widget">',
        'after_widget'  => '</div>',
        'before_title'  => '<h3>',
        'after_title'   => '</h3>',
    ));
}
add_action('widgets_init', 'mytheme_widgets_init');



function subscribe_newsletter_ajax() {
    if (isset($_POST['email']) && is_email($_POST['email'])) {
        $email = sanitize_email($_POST['email']);
        
        // Assuming you are using the Newsletter plugin
        if (function_exists('newsletter_subscribe')) {
            newsletter_subscribe(array('email' => $email));
            echo '<p>Thank you for subscribing!</p>';
        } else {
            echo '<p>Subscription failed. Please try again later.</p>';
        }
    } else {
        echo '<p>Invalid email address.</p>';
    }
    wp_die(); // this is required to terminate immediately and return a response
}
add_action('wp_ajax_subscribe_newsletter', 'subscribe_newsletter_ajax');
add_action('wp_ajax_nopriv_subscribe_newsletter', 'subscribe_newsletter_ajax');



// nav menu icon
add_filter('wp_nav_menu_objects', 'navigation_wp_nav_menu_objects', 10, 2);

function navigation_wp_nav_menu_objects( $nav__items, $args ) {
    
    // loop
    foreach( $nav__items as &$nav__item ) {
        
        // vars
        $nav__icon = get_field('navigation__icon', $nav__item);
        
        
        // append icon
        if( $nav__icon ) {
            $nav__item->title = $nav__icon . ' ' . $nav__item->title;
        }
        
    }
    
    
    // return
    return $nav__items;
    
}


// Add Reactions
function add_reaction() {
    $post_reactions = get_post_meta($_POST['post'], 'reaction');

    if (empty($post_reactions)) {
        $post_reactions = [
            'dislike' => 0,
            'neutral' => 0,
            'like'    => 0
        ];
    }

    $post_reactions[$_POST['reaction']] = ($post_reactions[$_POST['reaction']] + 1);

    update_post_meta($_POST['post'], 'reaction', $post_reactions);
    
    wp_send_json_success();
}
add_action('wp_ajax_add_reaction', 'add_reaction' );
add_action('wp_ajax_nopriv_add_reaction', 'add_reaction' );

if( function_exists('acf_add_options_page') ) {
    
    acf_add_options_page(array(
        'page_title'    => 'Schema Settings', 
        'menu_title'    => 'Schema',          
        'menu_slug'     => 'schema-settings', 
        'capability'    => 'edit_posts',      
        'redirect'      => false              
    ));

}