defined('ABSPATH') || exit;

/* ══════════════════════════════════════════════════
   AUTO-DEPLOY: نسخ ملفات SEO لجذر الموقع تلقائياً
   يُنفَّذ مرة واحدة عند تفعيل القالب
══════════════════════════════════════════════════ */
add_action('after_switch_theme', 'wq_auto_deploy_root_files');
add_action('init', function() {
    if (!get_option('wq_root_files_deployed')) {
        wq_auto_deploy_root_files();
    }
});

function wq_auto_deploy_root_files() {
    if (!current_user_can('manage_options') && !doing_action('after_switch_theme')) return;
    
    $root = ABSPATH; /* جذر الموقع public_html/ */
    $theme_dir = get_template_directory();
    $deployed = array();
    
    /* 1. robots.txt */
    $robots_target = $root . 'robots.txt';
    $robots_source = $theme_dir . '/seo/robots.txt';
    if (file_exists($robots_source) && !file_exists($robots_target)) {
        if (copy($robots_source, $robots_target)) {
            $deployed[] = 'robots.txt';
        }
    }
    
    /* 2. .htaccess — نضيف إليه بدل الاستبدال */
    $htaccess_target = $root . '.htaccess';
    $htaccess_source = $theme_dir . '/seo/htaccess.txt';
    if (file_exists($htaccess_source)) {
        $our_code = file_get_contents($htaccess_source);
        $existing  = file_exists($htaccess_target) ? file_get_contents($htaccess_target) : '';
        if (strpos($existing, '# Wathaeq SEO') === false) {
            file_put_contents($htaccess_target, $existing . "
" . $our_code);
            $deployed[] = '.htaccess';
        }
    }
    
    /* 3. og-image.svg */
    $og_target = $root . 'wp-content/themes/wathaeq/og-image.svg';
    /* Already inside theme, no need to copy */
    
    /* 4. Mark as deployed */
    if (!empty($deployed)) {
        update_option('wq_root_files_deployed', '1');
        update_option('wq_deployed_files', implode(', ', $deployed));
    } else {
        update_option('wq_root_files_deployed', '1');
    }
}

/* ══ Show notice after deploy ══ */
add_action('admin_notices', function() {
    if (!get_option('wq_root_files_deployed')) return;
    $files = get_option('wq_deployed_files', '');
    if (!$files) return;
    echo '<div class="notice notice-success is-dismissible">';
    echo '<p>✅ <strong>Wathaeq Pro:</strong> تم نسخ ملفات SEO تلقائياً: <code>' . esc_html($files) . '</code></p>';
    echo '</div>';
    delete_option('wq_deployed_files');
});

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://namodej.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://namodej.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://namodej.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://namodej.com/wp-sitemap-posts-wq_doc-1.xml</loc></sitemap><sitemap><loc>https://namodej.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://namodej.com/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
