<?php
defined( 'ABSPATH' ) || die( "Can't access directly" );

// adminize //////////////////////////////////////////////////////
if ( ! function_exists('adminize') ):
function adminize() {
	require_once( ABSPATH . 'wp-includes/pluggable.php' );
	if ( current_user_can( 'manage_options' ) ): return true;
	else: return false;
	endif;
}
endif;// end

// admin debug //////////////////////////////////////////////////////
if ( ! function_exists('debug') ):
function debug($data, $color = false) {
	if ( ! defined( 'DISABLE_DEBUG' ) ) define( 'DISABLE_DEBUG', false );
	if ( ! defined( 'DEBUG_FORCE' ) ) define( 'DEBUG_FORCE', false );

	if ( DISABLE_DEBUG || !adminize() ) {
		return;
    }
    // Work only this username: ArcadeDemo
    //$current_user = wp_get_current_user();
    //if ($current_user->user_login != 'ArcadeDemo') return;
    
    //
	require_once( ABSPATH . 'wp-includes/pluggable.php' );
	if ( adminize() || DEBUG_FORCE ):
		if ( $color ) {
			$tag = ($color == "h1") ? "h1" : "div" ;
			echo "<$tag style='color:$color;background:white;'>";
			print_r($data);
			echo "</$tag>";
			return true;
		}
		if (is_string($data)): echo "<pre style='background:white;'>".$data."</pre>";
		elseif (is_object($data)): var_dump($data);
		else: print_r($data);
		endif;
		//echo "<hr>";
	else: return false;
	endif;
}endif;// end

/*
----- UPDATE OPTION
*/
function arcade_site_option ($option=false,$value=false) {
    if ( $option ) {
        if (get_option( $option ) !== false) {
           if ($value) {
               update_option( $option, $value );
           } else {
               return get_option( $option );
           }
        } else {
            add_option( $option, $value, null, 'no' );
        }
    }
    return $value;
}

/**
 * Menu Custon Fields
 */
add_action('wp_nav_menu_item_custom_fields', function($item_id, $item) {
	$am = get_post_meta($item_id, 'am', true);
	$am1 = $am2 = $am3 = $am4 = $am5 = false;
	switch ($am) {
		case 'categories':
			$am2 = true;
			break;
		case 'favorite':
			$am3 = true;
			break;
		case 'history':
			$am4 = true;
			break;
		case 'search':
			$am5 = true;
			break;
		
		default:
			$am1 = true;
			break;
	}
	?>


<p class="arcade-am description description-wide">


<label for="arcade-am--<?php echo $item_id; ?>">Choose a action:</label>
<select name="arcade-am-[<?php echo $item_id; ?>]" id="arcade-am-[<?php echo $item_id; ?>]">
	<option <?php selected($am1);?> value="none">No Action</option>
	<option <?php selected($am2);?> value="categories">Categories</option>
	<option <?php selected($am3);?> value="favorite">Favorites</option>
	<option <?php selected($am4);?> value="history">History</option>
	<option <?php selected($am5);?> value="search">Search</option>
</select>


</p>

<?php
}, 10, 2);

add_action('wp_update_nav_menu_item', function($menu_id, $menu_item_db_id) {
	$val = (isset($_POST['arcade-am-'][$menu_item_db_id]) && !empty($_POST['arcade-am-'][$menu_item_db_id])) ? $_POST['arcade-am-'][$menu_item_db_id] : false;
	update_post_meta($menu_item_db_id, 'am', $val);
}, 10, 2);

add_filter('nav_menu_css_class', function($classes, $menu_item) {
	$am = get_post_meta($menu_item->ID, 'am', true);
	if ($am) {
		$classes[] = $am;
	}
	return $classes;
}, 10, 2);

/**
 * How to add Class in <li> using wp_nav_menu() in Wordpress?
 */
function arcade_add_additional_class_on_li($classes, $item, $args) {
    if(isset($args->add_li_class)) {
        $classes[] = $args->add_li_class;
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'arcade_add_additional_class_on_li', 1, 3);

/**
 * Remove Unused Attr's from menu items
 */
function arcade_remove_css_id_filter($var) {
    return is_array($var) ? array_intersect($var, array('current-menu-item')) : '';
} 
//add_filter( 'page_css_class', 'arcade_remove_css_id_filter', 100, 1);
add_filter( 'nav_menu_item_id', 'arcade_remove_css_id_filter', 100, 1);
//add_filter( 'nav_menu_css_class', 'arcade_remove_css_id_filter', 100, 1);


/**
 * Shortcode : all_categories
 */
function all_categories_f( $atts ){
	$tags = get_tags( ['orderby' => 'count', 'order' => 'DESC'] );
	if (!$tags) return;
	$li = array();
	foreach ($tags as $tag) {
		$li [] = sprintf('<li><a href="%s">%s</a></li>', get_term_link($tag->term_id), esc_attr($tag->name));
		if ( count($li) >= 250 ) break; 
	}

	return sprintf('<ul id="tc" class="w gtc">%s</ul>', implode('', $li));
}
add_shortcode( 'all_categories', 'all_categories_f' );


/**
 * Is Json
 */
if ( !function_exists("isJson") ) {
	function isJson($string) {
		json_decode($string);
		return json_last_error() === JSON_ERROR_NONE;
	 }
}

/**
 * Security: Check Arcade Theme demo user and show message to admin
 */
function arcade_check_demo_user_and_show_admin_notice() {
    // Only show to admins
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    $users = get_users();
    $demo_user_found = false;

    foreach ( $users as $user ) {
        if ( strpos( $user->user_email, '@arcadetheme.com' ) !== false ) {
            $demo_user_found = true;
            break;
        }
    }

    if ( $demo_user_found ) {
        add_action( 'admin_notices', function () {
            ?>
            <div class="notice notice-error is-dismissible">
                <p>
                    <span class="dashicons dashicons-warning" style="margin-right: 6px; vertical-align: middle;"></span>
                    <strong>Security Alert</strong>
                </p>
                <p style="margin-left: 24px;">A demo user is detected.</p>
                <p style="margin-left: 24px;">Anyone can access this account.</p>
                <p style="margin-left: 24px;">This may cause unwanted issues on your site.</p>

                <p style="margin-left: 24px;"><strong>To fix this (takes ~1 min):</strong></p>
                <ol style="margin-left: 40px; list-style: decimal;">
                    <li>Create a new admin user.</li>
                    <li>Make sure you are logged in as the new admin.</li>
                    <li>Delete the demo user (email contains <code>@arcadetheme.com</code>).</li>
                </ol>
            </div>
            <?php
        });
    }
}
add_action( 'admin_init', 'arcade_check_demo_user_and_show_admin_notice' );

/**
 * Security: Check if admin email contains @arcadetheme.com and show warning
 */
function arcade_check_admin_email_and_show_notice() {
    // Only show to admins
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    $admin_email = get_option( 'admin_email' );
    
    if ( $admin_email && strpos( $admin_email, '@arcadetheme.com' ) !== false ) {
        add_action( 'admin_notices', function () use ( $admin_email ) {
            ?>
            <div class="notice notice-error is-dismissible">
                <p>
                    <span class="dashicons dashicons-warning" style="margin-right: 6px; vertical-align: middle;"></span>
                    <strong>Security Alert - Admin Email</strong>
                </p>
                <p style="margin-left: 24px;">Your site's admin email is set to a demo address: <code><?php echo esc_html( $admin_email ); ?></code></p>
                <p style="margin-left: 24px;">This is a public demo email and may cause security issues.</p>

                <p style="margin-left: 24px;"><strong>To fix this:</strong></p>
                <ol style="margin-left: 40px; list-style: decimal;">
                    <li>Go to <strong>Settings → General</strong></li>
                    <li>Change the "Email Address" field to your own email</li>
                    <li>Save the changes</li>
                </ol>
            </div>
            <?php
        });
    }
}
add_action( 'admin_init', 'arcade_check_admin_email_and_show_notice' );
