<?php

/**
 * Handles basic WooCommerce functionality.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Helpers\Hook_Helpers;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Admin\Notices\Notice_Type_Enum;
defined('ABSPATH') || exit;
/**
 * WooCommerce Module.
 */
class WooCommerce_Module extends Abstract_Module
{
    /**
     * Whether WooCommerce is active.
     *
     * @var bool
     */
    private bool $is_woocommerce_active;
    /**
     * Check if WooCommerce is required and, if so, active.
     *
     * @return bool
     */
    public function is_woocommerce_status_valid(): bool
    {
        $is_woocommerce_required = Hook_Helpers::apply_scoped_filters('northmill_online_shared_is_woocommerce_required', \true);
        if ($is_woocommerce_required && !$this->is_woocommerce_active()) {
            $this->shared->admin->notices->register_notice('woocommerce_missing', Notice_Type_Enum::ERROR, function () {
                return sprintf(
                    // Translators: %s name of plugin being loaded.
                    __('WooCommerce is required for %s to work. Please install and activate WooCommerce', 'northmill-online-shared'),
                    $this->shared->package_info->package_data->get_name()
                );
            });
            return \false;
        } else {
            return \true;
        }
    }
    /**
     * Test to see if WooCommerce is active (including network activated).
     * However, it has not necessarily initialized (when that is important,
     * consider using the 'woocommerce_init' action).
     *
     * @link https://github.com/woocommerce/woocommerce/blob/trunk/docs/extension-development/extension-developer-handbook.md
     * @return bool
     */
    public function is_woocommerce_active(): bool
    {
        if (!isset($this->is_woocommerce_active)) {
            $plugin_path = trailingslashit(\WP_PLUGIN_DIR) . 'woocommerce/woocommerce.php';
            if (in_array($plugin_path, wp_get_active_and_valid_plugins(), \true) || is_multisite() && in_array($plugin_path, wp_get_active_network_plugins(), \true)) {
                $this->is_woocommerce_active = \true;
            } else {
                $this->is_woocommerce_active = \false;
            }
        }
        return $this->is_woocommerce_active;
    }
}
