<?php
/**
 * The template for displaying the password form for protected LinkCentral links
 *
 * This template creates a custom password form page for LinkCentral protected links.
 */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly 

// Enqueue the password form styles
function linkcentral_enqueue_password_form_styles() {
    wp_enqueue_style('linkcentral-password-form-styles', LINKCENTRAL_PLUGIN_URL . 'assets/css/password-form.css', array(), LINKCENTRAL_VERSION);
}
add_action('wp_enqueue_scripts', 'linkcentral_enqueue_password_form_styles');

// Determine whether to use custom design (premium only)
$linkcentral_use_custom_password_design = function_exists( 'linkcentral_fs' )
    && linkcentral_fs()->can_use_premium_code__premium_only()
    && get_option( 'linkcentral_enable_password_page_custom_design', false );

// Prepare content and style options when custom design is enabled
$linkcentral_password_page_settings = array();
if ( $linkcentral_use_custom_password_design && function_exists( 'linkcentral_get_password_page_settings' ) ) {
    $linkcentral_password_page_settings = linkcentral_get_password_page_settings();

    $linkcentral_password_page_show_logo = ! empty( $linkcentral_password_page_settings['show_logo'] );

    $linkcentral_text = isset( $linkcentral_password_page_settings['text'] ) ? $linkcentral_password_page_settings['text'] : array();
    $linkcentral_password_page_title           = isset( $linkcentral_text['title'] ) ? $linkcentral_text['title'] : __( 'Password Protected', 'linkcentral' );
    $linkcentral_password_page_subtitle        = isset( $linkcentral_text['subtitle'] ) ? $linkcentral_text['subtitle'] : __( 'This link is password protected. To view it please enter your password below.', 'linkcentral' );
    $linkcentral_password_page_password_label  = isset( $linkcentral_text['password_label'] ) ? $linkcentral_text['password_label'] : __( 'Password', 'linkcentral' );
    $linkcentral_password_page_button_label    = isset( $linkcentral_text['button_label'] ) ? $linkcentral_text['button_label'] : __( 'Submit', 'linkcentral' );
    $linkcentral_password_page_error_message   = isset( $linkcentral_text['error_message'] ) ? $linkcentral_text['error_message'] : __( 'The password you entered is incorrect. Please try again.', 'linkcentral' );

    $linkcentral_colors = isset( $linkcentral_password_page_settings['colors'] ) ? $linkcentral_password_page_settings['colors'] : array();
    $linkcentral_password_page_background_color = isset( $linkcentral_colors['background'] ) ? $linkcentral_colors['background'] : '';
    $linkcentral_password_page_container_color  = isset( $linkcentral_colors['container'] ) ? $linkcentral_colors['container'] : '#FFFFFF';
    $linkcentral_password_page_button_color     = isset( $linkcentral_colors['button'] ) ? $linkcentral_colors['button'] : '#000000';

    $linkcentral_password_page_text_color        = isset( $linkcentral_password_page_settings['text_color'] ) ? $linkcentral_password_page_settings['text_color'] : '#000000';
    $linkcentral_password_page_button_text_color = isset( $linkcentral_password_page_settings['button_text_color'] ) ? $linkcentral_password_page_settings['button_text_color'] : '#FFFFFF';
}
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php wp_title('|', true, 'right'); ?></title>
    <?php wp_head(); ?>
    <?php if ( ! empty( $linkcentral_use_custom_password_design ) ) : ?>
        <style type="text/css">
            <?php if ( ! empty( $linkcentral_password_page_background_color ) ) : ?>
            body.linkcentral-password-container {
                background-color: <?php echo esc_html( $linkcentral_password_page_background_color ); ?>;
            }
            <?php endif; ?>

            .linkcentral-password-form {
                <?php if ( ! empty( $linkcentral_password_page_container_color ) ) : ?>
                background-color: <?php echo esc_html( $linkcentral_password_page_container_color ); ?>;
                <?php endif; ?>
                color: <?php echo esc_html( $linkcentral_password_page_text_color ); ?>;
            }

            .linkcentral-password-form h1,
            .linkcentral-password-form p,
            .linkcentral-password-form label {
                color: <?php echo esc_html( $linkcentral_password_page_text_color ); ?>;
            }

            .linkcentral-password-form input[type="submit"] {
                <?php if ( ! empty( $linkcentral_password_page_button_color ) ) : ?>
                background-color: <?php echo esc_html( $linkcentral_password_page_button_color ); ?>;
                <?php endif; ?>
                color: <?php echo esc_html( $linkcentral_password_page_button_text_color ); ?>;
            }
        </style>
    <?php endif; ?>
</head>
<body <?php body_class('linkcentral-password-container'); ?>>
    <div class="linkcentral-password-form">
        <?php if ( ! empty( $linkcentral_use_custom_password_design ) && ! empty( $linkcentral_password_page_show_logo ) && function_exists( 'get_custom_logo' ) ) : ?>
            <div class="linkcentral-password-form-logo">
                <?php echo wp_kses_post( get_custom_logo() ); ?>
            </div>
        <?php endif; ?>

        <?php if ( ! empty( $linkcentral_use_custom_password_design ) ) : ?>
            <h1><?php echo esc_html( $linkcentral_password_page_title ); ?></h1>
            <p><?php echo esc_html( $linkcentral_password_page_subtitle ); ?></p>
        <?php else : ?>
            <h1><?php esc_html_e('Password Protected', 'linkcentral'); ?></h1>
            <p><?php esc_html_e('This link is password protected. To view it please enter your password below.', 'linkcentral'); ?></p>
        <?php endif; ?>

        <?php if ( isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) : ?>
            <p class="linkcentral-password-error">
                <?php
                if ( ! empty( $linkcentral_use_custom_password_design ) ) {
                    echo esc_html( $linkcentral_password_page_error_message );
                } else {
                    esc_html_e( 'The password you entered is incorrect. Please try again.', 'linkcentral' );
                }
                ?>
            </p>
        <?php endif; ?>

        <?php
        $post = get_post();
        $linkcentral_label = 'pwbox-' . ( empty($post->ID) ? wp_rand() : $post->ID );
        ?>

        <form action="<?php echo esc_url(site_url('wp-login.php?action=postpass', 'login_post')); ?>" class="post-password-form" method="post">
            <p>
                <label for="<?php echo esc_attr($linkcentral_label); ?>">
                    <?php
                    if ( ! empty( $linkcentral_use_custom_password_design ) ) {
                        echo esc_html( $linkcentral_password_page_password_label ) . ': ';
                    } else {
                        esc_html_e( 'Password', 'linkcentral' );
                        echo ': ';
                    }
                    ?>
                    <input name="post_password" id="<?php echo esc_attr($linkcentral_label); ?>" type="password" spellcheck="false" size="20" />
                </label>
                <input type="submit" name="Submit" value="<?php echo esc_attr( ! empty( $linkcentral_use_custom_password_design ) ? $linkcentral_password_page_button_label : __( 'Submit', 'linkcentral' ) ); ?>" />
            </p>
        </form>

    </div>
    <?php wp_footer(); ?>
</body>
</html>