<?php



defined("ABSPATH") or die("");

/**
 * Variables
 *
 * @var Duplicator\Core\Controllers\ControllersManager $ctrlMng
 * @var Duplicator\Core\Views\TplMng $tplMng
 */

use Duplicator\Models\GlobalEntity;
use Duplicator\Libs\Snap\SnapIO;
use Duplicator\Libs\WpUtils\WpArchiveUtils;

$global = GlobalEntity::getInstance();

$prefillAuth         = $tplMng->getDataValueBool('prefillAuth');
$showMismatchWarning = $tplMng->getDataValueBool('showMismatchWarning');
$detectedAuthUser    = $tplMng->getDataValueString('detectedAuthUser');
$savedAuthUser       = $tplMng->getDataValueString('savedAuthUser');
$authChecked         = $tplMng->getDataValueBool('authChecked');
$authUser            = $tplMng->getDataValueString('authUser');
$authPass            = $tplMng->getDataValueString('authPass');
$authFieldsDisabled  = $tplMng->getDataValueBool('authFieldsDisabled');

?>
<div class="dup-accordion-wrapper display-separators close">
    <div class="accordion-header">
        <h3 class="title">
            <?php esc_html_e("Advanced", 'duplicator-pro'); ?>
        </h3>
    </div>
    <div class="accordion-content">
        <label class="lbl-larger">
            <?php esc_html_e('Root path', 'duplicator-pro') ?>
        </label>
        <div class="margin-bottom-1">
            <input
                type="checkbox"
                name="homepath_as_abspath"
                id="homepath_as_abspath"
                class="margin-0"
                <?php disabled(WpArchiveUtils::isAbspathHomepathEquivalent()); ?>
                <?php checked($global->homepath_as_abspath); ?>
                value="1">
            <label for="homepath_as_abspath">
                <?php
                printf(
                    esc_html_x(
                        'Use ABSPATH %s as root path.',
                        '%s represents the ABSPATH surrounded with bold (<b>) tags',
                        'duplicator-pro'
                    ),
                    '<b>' . esc_html(WpArchiveUtils::getArchiveListPaths('abs')) . '</b>'
                );
                ?>
                <br>
            </label>
            <p class="description">
                <?php
                if (WpArchiveUtils::isAbspathHomepathEquivalent()) {
                    esc_html_e('Abspath and home path are equivalent so this option is disabled', 'duplicator-pro');
                } else {
                    ?>
                    <?php
                    printf(
                        esc_html_x(
                            'In this installation the default root path is %s.',
                            '%s represents the root path surrounded with bold (<b>) tags',
                            'duplicator-pro'
                        ),
                        '<b>' . esc_html(SnapIO::safePathUntrailingslashit(get_home_path(), true)) . '</b>'
                    ); ?><br>
                    <?php
                    esc_html_e(
                        'The path of the WordPress core is different. Activate this option if you want to consider ABSPATH as root path.',
                        'duplicator-pro'
                    );
                }
                ?>

            </p>
        </div>

        <label class="lbl-larger">
            <?php esc_html_e('Scan File Checks', 'duplicator-pro'); ?>
        </label>
        <div class="margin-bottom-1">
            <input
                type="checkbox"
                name="_skip_archive_scan"
                id="_skip_archive_scan"
                class="margin-0"
                <?php checked($global->skip_archive_scan); ?>>
            <label for="_skip_archive_scan">
                <?php esc_html_e("Skip", 'duplicator-pro') ?>
            </label><br />
            <p class="description">
                <?php
                esc_html_e(
                    'If enabled all file checks on scan will be skipped before Backup creation.
                    In some cases, this option can be beneficial if the scan process is having issues running or returning errors.',
                    'duplicator-pro'
                );
                ?>
            </p>
        </div>

        <label class="lbl-larger">
            <?php esc_html_e("Password-Protected Access", 'duplicator-pro'); ?>
        </label>
        <?php if ($prefillAuth) : ?>
            <div class="notice notice-info inline dupli-notice-inline-gap">
                <p>
                    <?php
                    printf(
                        esc_html_x(
                            'Basic authentication detected on this server (user: %s).
                            The fields below have been pre-filled with the detected credentials. Review and click Save Backup Settings to enable.',
                            '%s is the detected username',
                            'duplicator-pro'
                        ),
                        '<code>' . esc_html($detectedAuthUser) . '</code>'
                    );
                    ?>
                </p>
            </div>
        <?php elseif ($showMismatchWarning) : ?>
            <div class="notice notice-warning inline dupli-notice-inline-gap">
                <p>
                    <?php
                    printf(
                        esc_html_x(
                            'Detected credentials differ from saved ones. Detected user: %1$s, Saved user: %2$s.
                            If builds are failing, consider updating the saved credentials.',
                            '%1$s is the detected username, %2$s is the saved username',
                            'duplicator-pro'
                        ),
                        '<code>' . esc_html($detectedAuthUser) . '</code>',
                        '<code>' . esc_html($savedAuthUser) . '</code>'
                    );
                    ?>
                </p>
            </div>
        <?php endif; ?>
        <div class="margin-bottom-1">
            <input
                type="checkbox"
                name="_basic_auth_enabled"
                id="_basic_auth_enabled"
                value="1"
                class="margin-0"
                <?php checked($authChecked); ?>>
            <label for="_basic_auth_enabled">
                <?php esc_html_e("Enabled", 'duplicator-pro') ?>
            </label>
            <i class="fa-solid fa-question-circle fa-sm dark-gray-color"
                data-tooltip-title="<?php esc_attr_e("HTTP Basic authentication", 'duplicator-pro'); ?>"
                data-tooltip="<?php esc_attr_e(
                    "When HTTP Basic authentication is applied Duplicator needs to attach the login credentials to each request to the server.
                        This is required for the build process to work properly. If you see a browser popup login window when accessing the admin area,
                        then basic authentication should be enabled. If you are not sure, please consult your hosting provider.",
                    'duplicator-pro'
                ); ?>"></i>
            <div id="dup-basic-auth-login-wrapper">
                <input
                    autocomplete="off"
                    placeholder="<?php esc_attr_e('User', 'duplicator-pro'); ?>"
                    type="text"
                    name="basic_auth_user"
                    id="basic_auth_user"
                    class="margin-0"
                    value="<?php echo esc_attr($authUser); ?>"
                    <?php disabled($authFieldsDisabled); ?>>
                <span class="dup-password-toggle">
                    <input
                        autocomplete="off"
                        placeholder="<?php esc_attr_e('Password', 'duplicator-pro'); ?>"
                        type="password"
                        name="basic_auth_password"
                        id="basic_auth_password"
                        class="margin-0"
                        value="<?php echo esc_attr($authPass); ?>"
                        <?php disabled($authFieldsDisabled); ?>>
                    <button type="button">
                        <i class="fas fa-eye fa-sm"></i>
                    </button>
                </span>
            </div>
            <p class="description">
                <?php esc_html_e(
                    'Enable this function and provide username and password in case of a Backup creation error.
                    Essential for making authorized HTTP requests in a server protected folder.',
                    'duplicator-pro'
                ); ?>
            </p>
        </div>
    </div>
</div>
<script>
    (function($) {
        $('#_basic_auth_enabled').on('change', function() {
            if ($(this).is(':checked')) {
                $('#dup-basic-auth-login-wrapper input').prop('disabled', false);
            } else {
                $('#dup-basic-auth-login-wrapper input').prop('disabled', true);
            }
        });
    }(window.jQuery || jQuery))
</script>
