<?php
/**
 * Plugin.php
 *
 * @since      1.0.0
 * @author     POP <info@popapi.io>

 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2
 *
 * Copyright 2025 POP <info@popapi.io>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace WooPoPToSdIPec;

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

/**
 * Class Plugin
 *
 * @since   1.0.0
 * @author  POP <info@popapi.io>
 */
final class Plugin
{
    /**
     * Load Localisation files.
     *
     * Note: the first-loaded translation file overrides any following ones if the same translation is present.
     *
     * Locales found in:
     *      - WP_LANG_DIR/PLUGIN_LANGUAGE_DIR_PATH/TEXT_DOMAIN-LOCALE.mo
     *      - WP_LANG_DIR/plugins/TEXT_DOMAIN-LOCALE.mo
     */
    public static function load_plugin_textdomain()
    {
        $locale = determine_locale();

        /**
         * Filter to adjust the WP_FATT_SDI_PEC_TEXTDOMAIN locale to use for translations.
         */
        $locale = apply_filters('plugin_locale', $locale, WP_FATT_SDI_PEC_TEXTDOMAIN);

        unload_textdomain(WP_FATT_SDI_PEC_TEXTDOMAIN, true);
        load_textdomain(WP_FATT_SDI_PEC_TEXTDOMAIN,
            WP_LANG_DIR . '/woopop-electronic-invoice/addon/to/' . WP_FATT_SDI_PEC_TEXTDOMAIN . '-' . $locale . '.mo');
        // Load plugin text-domain.
        load_plugin_textdomain(WP_FATT_SDI_PEC_TEXTDOMAIN, false,
            '/' . WC_EL_INV_PLUGIN_DIR . '/addon/to/' . WP_FATT_SDI_PLUGIN_DIR . '/languages/');
    }

    /**
     * Capability
     *
     * @since 1.0.0
     *
     * @var string The capability required for this menu to be displayed to the user.
     */
    public static $capability = 'manage_options';

    /**
     * Sanitize path
     *
     * @param string $path The path to sanitize
     *
     * @return string The sanitized path.
     * @since 1.0.0
     *
     */
    private static function sanitizePath($path)
    {
        while (false !== strpos($path, '..')) {
            $path = str_replace('..', '', $path);
        }

        $path = ('/' !== $path) ? $path : '';

        return $path;
    }

    /**
     * Get Plugin Dir Path
     *
     * @param string $path     The path to append to the plugin dir path. Optional. Default to '/'.
     * @param bool   $override If override from theme true o false
     *
     * @return string The plugin dir path
     * @since  1.0.0
     *
     */
    public static function getPluginDirPath($path = '/', $override = false)
    {
        $path    = untrailingslashit(plugin_dir_path(__DIR__)) . '/' . trim($path, '/');
        $located = realpath($path);

        $locatedTheme = get_theme_file_path($path);

        if (file_exists($locatedTheme) && $override) {
            $located = $locatedTheme;
        }

        return self::sanitizePath($located);
    }

    /**
     * Get Plugin Dir Url
     *
     * @param string $path     The path to append to the plugin dir url. Optional. Default to '/'.
     * @param bool   $override If override from theme true o false
     *
     * @return string The plugin dir url
     * @since  1.0.0
     *
     */
    public static function getPluginDirUrl($path = '/', $override = false)
    {
        $located = untrailingslashit(plugin_dir_url(__DIR__)) . '/' . trim($path, '/');

        $locatedTheme = get_theme_file_path($path);

        if (file_exists($locatedTheme) && $override) {
            $located = $locatedTheme;
        }

        return self::sanitizePath($located);
    }

}
