<?php

defined('ABSPATH') || exit();

class ASG_Menu_Item_Post_Type
{
	public function __construct()
	{
		add_action('init', array($this, 'register_post_types'));
		add_action('template_include', array($this, 'template_include'), 99);
	}
	public function register_post_types()
	{
		$labels = array(
			'name'               => _x('Menu Item', 'post type general name', 'liosa'),
			'singular_name'      => _x('Menu', 'post type singular name', 'liosa'),
			'menu_name'          => _x('Menu Items', 'admin menu', 'liosa'),
			'name_admin_bar'     => _x('Menu', 'add new on admin bar', 'liosa'),
			'add_new'            => _x('Add New', 'book', 'liosa'),
			'add_new_item'       => __('Add New Menu', 'liosa'),
			'new_item'           => __('New Menu', 'liosa'),
			'edit_item'          => __('Edit Menu', 'liosa'),
			'view_item'          => __('View Menu', 'liosa'),
			'all_items'          => __('All Menu', 'liosa'),
			'search_items'       => __('Search Items', 'liosa'),
			'parent_item_colon'  => __('Parent Items:', 'liosa'),
			'not_found'          => __('No menus found.', 'liosa'),
			'not_found_in_trash' => __('No menus found in Trash.', 'liosa')
		);

		$args = array(
			'labels'              => $labels,
			'hierarchical'        => false,
			'description'         => 'description',
			'taxonomies'          => array(),
			'public'              => true,
			'show_ui'             => true,
			'show_in_menu'        => false,
			'show_in_admin_bar'   => true,
			'menu_position'       => null,
			'menu_icon'           => true,
			'show_in_nav_menus'   => false,
			'publicly_queryable'  => true,
			'exclude_from_search' => true,
			'has_archive'         => false,
			'query_var'           => true,
			'can_export'          => true,
			'capability_type'     => 'post',
			'supports'            => array('title', 'editor'),
			'rewrite'            => array('slug' => 'asgh-menus')
		);

		register_post_type('asgh_menu_item', apply_filters('asgh_elementor_menu_item_post_type', $args));

		add_post_type_support('asgh_menu_item', 'elementor');
	}

	public function template_include($template)
	{
		if (get_query_var('post_type') !== 'asgh_menu_item') {
			return $template;
		}
		$template = locate_template($this->get_templates());
		if (!$template) {
			$template =   'single-menu.php';
		}

		return $template;
	}

	public function get_templates()
	{
		$templates = array();
		$object       = get_queried_object();
		$templates[] = 'single-menu-' . $object->post_name . '.php';
		$templates[] = 'single-menu.php';
		return $templates;
	}
}

new ASG_Menu_Item_Post_Type();
