<?php

namespace NeurogenesisTheme\Core;

defined('ABSPATH') || exit;

class SiteState
{

    public static function is_fresh_install(): bool
    {

        // Published pages/posts/products/etc.
        $content_count = (int) wp_count_posts('post')->publish;

        $page_count = (int) wp_count_posts('page')->publish;

        // Ignore default "Hello world!" + "Sample Page"
        $total = $content_count + $page_count;

        // Count users
        $user_count = count_users();

        // Menus
        $menus = wp_get_nav_menus();

        // Widgets
        $sidebars = get_option('sidebars_widgets', array());

        $has_widgets = false;

        if (is_array($sidebars)) {
            foreach ($sidebars as $sidebar => $widgets) {
                if ('wp_inactive_widgets' === $sidebar) {
                    continue;
                }

                if (! empty($widgets)) {
                    $has_widgets = true;
                    break;
                }
            }
        }

        // Fresh install heuristic
        return (
            $total <= 2 &&
            $user_count['total_users'] <= 1 &&
            empty($menus) &&
            ! $has_widgets
        );
    }
}
