<?php
require_once 'navwalker.php';
include "functions/post-type.php";
function body()
{

//    wp_enqueue_style('style', get_stylesheet_url());
    wp_enqueue_style('bootstrap1', 'https://cdn.jsdelivr.net/npm/bootstrap@4.3.0/dist/css/bootstrap.min.css');
    wp_enqueue_style('fancybox-css ','https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css');
    wp_enqueue_style('animate','https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css');
    wp_enqueue_style('owl-carousel-css', get_template_directory_uri() . '/assets/css/owl.carousel.min.css');
    wp_enqueue_style('aos-css', 'https://unpkg.com/aos@2.3.1/dist/aos.css');
    wp_enqueue_style('fontawesome1','https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css');
    wp_enqueue_style('google-fonts-tajawal', 'https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;700;800;900&display=swap', array(), null);
    wp_enqueue_style('main_css', get_template_directory_uri()   . '/assets/css/style.css');


//    wp_enqueue_style('script', get_script_url());

    wp_enqueue_script('jquery1', get_template_directory_uri() . '/assets/js/jquery-3.4.1.js',array(),'null','true');
    wp_enqueue_script('popper-js','https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js',array(),'null','true');
    wp_enqueue_script('bootstrap-js','https://cdn.jsdelivr.net/npm/bootstrap@4.3.0/dist/js/bootstrap.min.js',array(),'null','true');
    wp_enqueue_script('owl-carousel-js', get_template_directory_uri() . '/assets/js/owl.carousel.min.js',array(),'null','true');
    wp_enqueue_script('fontawesome-js', get_template_directory_uri() . '/assets/js/fontawesome.js',array(),'null','true');
    wp_enqueue_script('aos-js',  'https://unpkg.com/aos@2.3.1/dist/aos.js',array(),'null','true');
    wp_enqueue_script('funcybox-js','https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js',array(),'null','true');
    wp_enqueue_script('script-main1', get_template_directory_uri() . '/assets/js/main.js',array(),'null','true');

}
add_action('wp_enqueue_scripts', 'body');

function menu(){
    register_nav_menus(array(
        'header-location' => 'header',
        //'footer-location' => 'footer',
    ));
}
add_action('init','menu');

add_action('wp_head', 'head_meta', 1);
function head_meta() {

    if (is_admin() || is_feed()) return;

    // ============================================================
    // 1. بيانات أساسية
    // ============================================================
    $site_name = get_bloginfo('name');
    $site_desc = get_bloginfo('description');
    $title     = wp_get_document_title();
    $og_type   = 'website';

    // Canonical URL
    if (is_singular()) {
        $url     = get_permalink();
        $og_type = 'article';
    } elseif (is_home() || is_front_page()) {
        $url = home_url('/');
    } else {
        $url = home_url('/');
    }

    // ============================================================
    // 2. الـ Description
    // ============================================================
    $description = $site_desc;

    if (is_singular()) {
        global $post;
        if ($post instanceof WP_Post) {
            if (has_excerpt($post)) {
                $raw = get_the_excerpt($post);
            } else {
                $raw = wp_strip_all_tags(get_post_field('post_content', $post));
            }
            $description = trim(preg_replace('/\s+/', ' ', $raw));
        }
    } elseif (is_category() || is_tag() || is_tax()) {
        $term = get_queried_object();
        $raw  = wp_strip_all_tags(term_description($term));
        if ($raw) $description = trim(preg_replace('/\s+/', ' ', $raw));
    }

    if (function_exists('mb_substr')) {
        $description = mb_substr($description, 0, 160);
    }

    // ============================================================
    // 3. الـ OG Image
    // ============================================================
    $image = '';

    if (is_singular() && has_post_thumbnail()) {
        $image = wp_get_attachment_image_url(get_post_thumbnail_id(), 'full');
    } elseif (has_custom_logo()) {
        $logo_id = get_theme_mod('custom_logo');
        $image   = wp_get_attachment_image_url($logo_id, 'full');
    } elseif (function_exists('get_field') && get_field('logo', 'option')) {
        $image = get_field('logo', 'option');
    } else {
        $image = get_site_icon_url(512);
    }

    // ============================================================
    // 4. الـ Favicon
    // ============================================================
    $favicon = '';

    if (function_exists('get_field') && get_field('favicon', 'option')) {
        $favicon = get_field('favicon', 'option');
    } else {
        $favicon = get_site_icon_url(32);
    }

    // ============================================================
    // 5. فحص SEO Plugins
    // ============================================================
    $has_seo = defined('WPSEO_VERSION')
            || defined('RANK_MATH_VERSION')
            || class_exists('All_in_One_SEO_Pack');

    ?>

    <meta charset="<?php bloginfo('charset'); ?>" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <!-- Preconnect: Google Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />

    <!-- Favicon -->
    <?php if ($favicon): ?>
        <link rel="shortcut icon" href="<?php echo esc_url($favicon); ?>" type="image/x-icon" />
    <?php endif; ?>

    <!-- Canonical -->
    <link rel="canonical" href="<?php echo esc_url($url); ?>" />

    <!-- Profile & Pingback -->
    <link rel="profile" href="https://gmpg.org/xfn/11" />
    <?php if (get_option('default_pingback_flag')): ?>
        <link rel="pingback" href="<?php echo esc_url(get_bloginfo('pingback_url')); ?>" />
    <?php endif; ?>

    <!-- IE Conditionals -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

    <?php if (!$has_seo): ?>

        <!-- Meta Description -->
        <meta name="description" content="<?php echo esc_attr($description); ?>" />

        <!-- Open Graph -->
        <meta property="og:site_name"   content="<?php echo esc_attr($site_name); ?>" />
        <meta property="og:type"        content="<?php echo esc_attr($og_type); ?>" />
        <meta property="og:title"       content="<?php echo esc_attr($title); ?>" />
        <meta property="og:description" content="<?php echo esc_attr($description); ?>" />
        <meta property="og:url"         content="<?php echo esc_url($url); ?>" />
        <?php if (!empty($image)): ?>
            <meta property="og:image" content="<?php echo esc_url($image); ?>" />
        <?php endif; ?>

        <!-- Twitter Cards -->
        <meta name="twitter:card"        content="summary_large_image" />
        <meta name="twitter:title"       content="<?php echo esc_attr($title); ?>" />
        <meta name="twitter:description" content="<?php echo esc_attr($description); ?>" />
        <?php if (!empty($image)): ?>
            <meta name="twitter:image" content="<?php echo esc_url($image); ?>" />
        <?php endif; ?>

    <?php endif; ?>

    <?php
}






// Register Custom Navigation Walker

// Adding excerpt for page
add_post_type_support( 'page', 'excerpt' );

/************************************************************************/
/* Setup Theme */
/************************************************************************/

function m7md_setup()
{
    add_theme_support('automatic-feed-links');
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');

}

add_action('after_setup_theme', 'm7md_setup');

/************************************************************************/
/*  Define Constants */
/************************************************************************/

define('theme_name', 'm7md');
define('THEME_ROOT', esc_url(get_template_directory_uri()));
define('THEME_IMAGES_DIR', THEME_ROOT . '/assets/img/');
/************************************************************************/
/* option page */
/************************************************************************/


if (function_exists('acf_add_options_page')) {

    acf_add_options_page(array(
        'page_title' => __('إعدادت القالب', 'm7md'),
        'menu_title' => __('إعدادت القالب', 'm7md'),
        'menu_slug' => 'theme-settings',
        'capability' => 'edit_posts',
        'parent_slug' => '',
        'position' => false,
        'icon_url' => false,
    ));
    acf_add_options_sub_page(array(
        'page_title' => __("الاقسام الرئيسية", 'm7md'),
        'menu_title' => __("الاقسام الرئيسية", 'm7md'),
        'menu_slug' => 'home-section',
        'capability' => 'edit_posts',
        'parent_slug' => 'theme-settings',
        'position' => false,
        'icon_url' => false
    ));

    acf_add_options_sub_page(array(
        'page_title' => __('الاعدادات العامة', 'm7md'),
        'menu_title' => __('الاعدادات العامة', 'm7md'),
        'menu_slug' => 'contact-info',
        'capability' => 'edit_posts',
        'parent_slug' => 'theme-settings',
        'position' => false,
        'icon_url' => false
    ));







}

/***********************************************************************************************/

/* the numbers of litter to show */

/***********************************************************************************************/
function get_excerpt($count)
{
    global $post;

    $permalink = get_permalink($post->ID);

    $excerpt = get_the_content();

    $excerpt = strip_tags($excerpt);

    $excerpt = substr($excerpt, 0, $count);

    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));

    $excerpt = $excerpt;

    return $excerpt;
}

/************************************************************************/
/* change admin panel theme style*/
/************************************************************************/

function adminStyle()
{
    ?>
    <style>
        @import url('https://fonts.googleapis.com/css?family=Tajawal:400,700&subset=arabic');

        body {
            font-family: 'Tajawal', sans-serif !important;
        }
    </style>
    <?php
}

add_action('admin_notices', 'adminStyle');

/************************************************************************/
/* custom login */
/************************************************************************/

function customLogin()
{
    ?>
    <style>
       /* body {
            background-image: linear-gradient(to left, #005b70, #6cb867) !important;
        }*/

        .login #backtoblog a,
        .login #nav a {
            /*color: #fff !important;*/
        }

        #login h1 a,
        .login h1 a {
            background-image: none;
        }

        #login h1,
        .login h1 {
            background-image: url(<?php the_field('logo', 'option'); ?>);
            background-position: center top;
            background-repeat: no-repeat;
            background-size: contain;
            padding: 20px;
           /* background-color: #FFF;*/
        }
    </style>
    <?php
}

add_action('login_enqueue_scripts', 'customLogin');

// change url of login logo link
add_filter( 'login_headerurl', 'my_custom_login_url' );
function my_custom_login_url($url) {
    return home_url();
}

/************************************************************************/
/* display favicon in admin dashboard */
/************************************************************************/

if (class_exists('acf')) {
    function add_favicon()
    {
        $favicon = get_field('favicon', 'option');
        echo '<link rel="shortcut icon" href="' . $favicon . '" />';
    }

    add_action('admin_head', 'add_favicon');
}

/************************************************************************/
/* language switcher */
/************************************************************************/

/*
 * get lang
 */

function get_lang()
{
    global $q_config;
    return $q_config['language'];
}

/*
 * switch language .
 */

function the_switch_language($ar, $en)
{

    if (get_lang() == 'ar') {

        echo $ar;

    } elseif (get_lang() == 'en') {

        echo $en;

    }
}

function get_switch_language($ar, $en)
{
    if (get_lang() == 'ar') {

        return $ar;

    } elseif (get_lang() == 'en') {

        return $en;
    }
}

/***********************************************/

/*Hide the ACF menu*/
function remove_acf_menu()
{
    remove_menu_page('edit.php?post_type=acf-field-group');
    remove_menu_page('plugins.php');
}
//add_action('admin_menu', 'remove_acf_menu', 999);


//*************************//
//*************************//

/* remove sub menu */

function my_remove_menu_elements()
{
    remove_submenu_page('themes.php', 'theme-editor.php');
    remove_submenu_page('themes.php', 'themes.php');
    //remove_submenu_page('plugins.php', 'plugins.php');
    remove_submenu_page('plugins.php', 'plugin-editor.php');
}

add_action('admin_menu', 'my_remove_menu_elements', 102);










