/*!
	Theme Name: Hello Academy
	Theme URI: https://academylms.net/hello-academy
	Description: Hello Academy is a lightweight theme powered by Academy LMS - a super fast next-gen WordPress LMS plugin. It was built to put Academy LMS in the spotlight while allowing you to create a unique eLearning platform. When it comes to speed and performance, the theme shows a fantastic score on Google PageSpeed insight. It's an Elementor and Gutenberg ready theme that aspires to serve a wide range of users. All the pages are Fully responsive, SEO-optimzied, major browser compatible, and ready with Schema, RTL, and translation. This fully customizable theme comes with homepage variations, header & fotter variations, advanced font, color, style, and page settings. There are two pre-built Homepage layouts named Single Instructor and Univesity. Also, you will get the course, About, registration page, etc. The blog pages of this theme are perfect for presenting any sort of content with enhanced readability. The theme synchronizes perfectly with the Academy LMS plugin and allows users to create a website for an educational organization, training center, bookselling, course hub, online exam center, and more.  
	Author: AcademyLMS Team
	Author URI: https://academylms.net/
	Version: 1.1.4
	Stable tag: 1.1.4
	Requires at least: 5.4
	Tested up to: 6.2
	Requires PHP: 5.6
	Text Domain: hello-academy
	License: GNU General Public License v2 or later
	License URI: http://www.gnu.org/licenses/gpl-2.0.html
	Tags: custom-menu, custom-logo, featured-images, threaded-comments, translation-ready,  E-Commerce, Education, eLearning, lms, left-sidebar, right-sidebar, blog
*/
/**
 * DO NOT CHANGE THIS FILE!
 * To override any of the settings in this section, add your styling code in the custom directory.
 * Loading first in the style.scss & classic-editor.scss
 */
/*!
 * Based on Normalize.css v8.0.0, HTML5 Boilerplate v6.1.0, & Bootstrap Reboot v4.1.0 | Under MIT License
*/
/**
 * Document basic styling
 * DO NOT CHANGE THIS FILE!
 * To override any of the settings in this section, add your styling code in the custom directory.
 */
/**
 * 1. Correct the line height in all browsers.
 * 2. Prevent adjustments of font size after orientation changes in iOS.
 * 3. Change from `box-sizing: content-box` so that `width` is not affected by `padding` or `border`.
 */
html {
  line-height: 1.15; /* 1 */
  -webkit-text-size-adjust: 100%; /* 2 */
}

*,
*::before,
*::after {
  -webkit-box-sizing: border-box;
          box-sizing: border-box; /* 3 */
}

/* Sections
 */
body {
  margin: 0;
  font-family: "Inter", sans-serif;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.8;
  color: #444444;
  background-color: #fff;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/**
 * Correct the font size and margin on `h1` elements within `section` and
 * `article` contexts in Chrome, Firefox, and Safari.
 */
h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: "Montserrat", sans-serif !important;
  margin-top: 0.5rem;
  margin-bottom: 1rem;
  font-family: inherit;
  font-weight: 600;
  line-height: 1.2;
  color: #111111;
}

h1 {
  font-size: 2.5rem;
}

h2 {
  font-size: 2rem;
}

h3 {
  font-size: 1.75rem;
}

h4 {
  font-size: 1.5rem;
}

h5 {
  font-size: 1.25rem;
}

h6 {
  font-size: 1rem;
}

p {
  margin-top: 0;
  margin-bottom: 0.9rem;
}

/* Grouping content
 */
/**
 * 1. Add the correct box sizing in Firefox.
 * 2. Show the overflow in Edge and IE.
 */
hr {
  -webkit-box-sizing: content-box;
          box-sizing: content-box; /* 1 */
  height: 0; /* 1 */
  overflow: visible; /* 2 */
}

/**
 * 1. Correct the inheritance and scaling of font size in all browsers.
 * 2. Correct the odd `em` font sizing in all browsers.
 */
pre {
  font-family: monospace, monospace; /* 1 */
  font-size: 1em; /* 2 */
  white-space: pre-wrap;
}

/* Text-level semantics
 */
a {
  font-family: inherit;
  background-color: transparent;
  color: #111;
}
a:hover, a:active {
  color: #333366;
}
a:focus {
  outline: 1px dotted #7B68EE;
}
a:not([href]):not([tabindex]) {
  color: inherit;
}
a:not([href]):not([tabindex]):hover, a:not([href]):not([tabindex]):focus {
  color: inherit;
}
a:not([href]):not([tabindex]):focus {
  outline: 0;
}

/**
 * 1. Remove the bottom border in Chrome 57-
 * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
 */
abbr[title] {
  border-bottom: none; /* 1 */
  -webkit-text-decoration: underline dotted;
          text-decoration: underline dotted; /* 2 */
}

/**
 * Add the correct font weight in Chrome, Edge, and Safari.
 */
b,
strong {
  font-weight: bolder;
}

/**
 * 1. Correct the inheritance and scaling of font size in all browsers.
 * 2. Correct the odd `em` font sizing in all browsers.
 */
code,
kbd,
samp {
  font-family: monospace, monospace; /* 1 */
  font-size: 1em; /* 2 */
}

/**
 * Add the correct font size in all browsers.
 */
small {
  font-size: 80%;
}

/**
 * Prevent `sub` and `sup` elements from affecting the line height in
 * all browsers.
 */
sub,
sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}

sub {
  bottom: -0.25em;
}

sup {
  top: -0.5em;
}

/* Embedded content
 */
/**
 * Remove the border on images inside links in IE 10.
 */
img {
  border-style: none;
  height: auto;
  max-width: 100%;
}

/* Interactive
 */
/*
 * Add the correct display in Edge, IE 10+, and Firefox.
 */
details {
  display: block;
}

/*
 * Add the correct display in all browsers.
 */
summary {
  display: list-item;
}

figcaption {
  font-size: 16px;
  color: #333333;
  line-height: 1.4;
  font-style: italic;
  font-weight: 400;
}

/* Misc
 */
/**
 * Add the correct display in IE 10+.
 */
template {
  display: none;
}

/**
 * Add the correct display in IE 10.
 */
[hidden] {
  display: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Print
 */
@media print {
  *,
*:before,
*:after {
    background: transparent !important;
    color: #000 !important; /* Black prints faster */
    -webkit-box-shadow: none !important;
    box-shadow: none !important;
    text-shadow: none !important;
  }
  a,
a:visited {
    text-decoration: underline;
  }
  a[href]:after {
    content: " (" attr(href) ")";
  }
  abbr[title]:after {
    content: " (" attr(title) ")";
  }
  a[href^="#"]:after,
a[href^="javascript:"]:after {
    content: "";
  }
  pre {
    white-space: pre-wrap !important;
  }
  pre,
blockquote {
    page-break-inside: avoid;
    border: 1px solid #cccccc;
  }
  thead {
    display: table-header-group;
  }
  tr,
img {
    page-break-inside: avoid;
  }
  p,
h2,
h3 {
    orphans: 3;
    widows: 3;
  }
  h2,
h3 {
    page-break-after: avoid;
  }
}
/**
 * Form styling
 * DO NOT CHANGE THIS FILE!
 * To override any of the settings in this section, add your styling code in the custom directory.
 */
label {
  display: inline-block;
  line-height: 1;
  vertical-align: middle;
}

/**
 * 1. Change the font styles in all browsers.
 * 2. Remove the margin in Firefox and Safari.
 */
button,
input,
optgroup,
select,
textarea {
  font-family: inherit; /* 1 */
  font-size: 1rem; /* 1 */
  line-height: 1.5; /* 1 */
  margin: 0; /* 2 */
}

input[type=text],
input[type=date],
input[type=email],
input[type=number],
input[type=password],
input[type=search],
input[type=tel],
input[type=url],
select,
textarea {
  width: 100%;
  border: solid 1px #666666;
  border-radius: 3px;
  padding: 0.5rem 1rem;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}
input[type=text]:focus,
input[type=date]:focus,
input[type=email]:focus,
input[type=number]:focus,
input[type=password]:focus,
input[type=search]:focus,
input[type=tel]:focus,
input[type=url]:focus,
select:focus,
textarea:focus {
  border-color: #333333;
}

/**
 * Show the overflow in IE.
 * 1. Show the overflow in Edge.
 */
button,
input { /* 1 */
  overflow: visible;
}

/**
 * Remove the inheritance of text transform in Edge, Firefox, and IE.
 * 1. Remove the inheritance of text transform in Firefox.
 */
button,
select { /* 1 */
  text-transform: none;
}

/**
 * Correct the inability to style clickable types in iOS and Safari.
 */
button,
[type=button],
[type=reset],
[type=submit] {
  width: auto;
  -webkit-appearance: button;
}

/**
 * Remove the inner border and padding in Firefox.
 */
button::-moz-focus-inner,
[type=button]::-moz-focus-inner,
[type=reset]::-moz-focus-inner,
[type=submit]::-moz-focus-inner {
  border-style: none;
  padding: 0;
}

/**
 * Restore the focus styles unset by the previous rule.
 */
button:-moz-focusring,
[type=button]:-moz-focusring,
[type=reset]:-moz-focusring,
[type=submit]:-moz-focusring {
  outline: 1px dotted ButtonText;
}

button,
[type=submit],
[type=button] {
  display: inline-block;
  font-weight: 400;
  color: #7b68ee;
  text-align: center;
  white-space: nowrap;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
  background-color: transparent;
  border: 1px solid #7b68ee;
  padding: 0.5rem 1rem;
  font-size: 1rem;
  border-radius: 3px;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
}
button:focus,
[type=submit]:focus,
[type=button]:focus {
  outline: 5px auto -webkit-focus-ring-color;
}
button:hover, button:focus,
[type=submit]:hover,
[type=submit]:focus,
[type=button]:hover,
[type=button]:focus {
  color: #ffffff;
  background-color: #7b68ee;
  text-decoration: none;
}
button:not(:disabled),
[type=submit]:not(:disabled),
[type=button]:not(:disabled) {
  cursor: pointer;
}

/**
 * Correct the padding in Firefox.
 */
fieldset {
  padding: 0.35em 0.75em 0.625em;
}

/**
 * 1. Correct the text wrapping in Edge and IE.
 * 2. Correct the color inheritance from `fieldset` elements in IE.
 * 3. Remove the padding so developers are not caught out when they zero out
 *    `fieldset` elements in all browsers.
 */
legend {
  -webkit-box-sizing: border-box;
          box-sizing: border-box; /* 1 */
  color: inherit; /* 2 */
  display: table; /* 1 */
  max-width: 100%; /* 1 */
  padding: 0; /* 3 */
  white-space: normal; /* 1 */
}

/**
 * Add the correct vertical alignment in Chrome, Firefox, and Opera.
 */
progress {
  vertical-align: baseline;
}

/**
 * Remove the default vertical scrollbar in IE 10+.
 */
textarea {
  overflow: auto;
  resize: vertical;
}

/**
 * 1. Add the correct box sizing in IE 10.
 * 2. Remove the padding in IE 10.
 */
[type=checkbox],
[type=radio] {
  -webkit-box-sizing: border-box;
          box-sizing: border-box; /* 1 */
  padding: 0; /* 2 */
}

/**
 * Correct the cursor style of increment and decrement buttons in Chrome.
 */
[type=number]::-webkit-inner-spin-button,
[type=number]::-webkit-outer-spin-button {
  height: auto;
}

/**
 * 1. Correct the odd appearance in Chrome and Safari.
 * 2. Correct the outline style in Safari.
 */
[type=search] {
  -webkit-appearance: textfield; /* 1 */
  outline-offset: -2px; /* 2 */
}

/**
 * Remove the inner padding in Chrome and Safari on macOS.
 */
[type=search]::-webkit-search-decoration {
  -webkit-appearance: none;
}

/**
 * 1. Correct the inability to style clickable types in iOS and Safari.
 * 2. Change font properties to `inherit` in Safari.
 */
::-webkit-file-upload-button {
  -webkit-appearance: button; /* 1 */
  font: inherit; /* 2 */
}

select {
  display: block;
}

/**
 * Table styling
 * DO NOT CHANGE THIS FILE!
 * To override any of the settings in this section, add your styling code in the custom directory.
 */
table {
  background-color: transparent;
  width: 100%;
  margin-bottom: 15px;
  font-size: 0.9em;
  border-spacing: 0;
  border-collapse: collapse;
}
table th,
table td {
  vertical-align: middle;
  line-height: 2;
  text-align: center;
  border: 1px solid #cccccc;
}
table th {
  font-weight: bold;
}
table thead th,
table tfoot th {
  font-size: 1em;
}
table caption + thead tr:first-child th,
table caption + thead tr:first-child td,
table caption + thead tr:first-child th,
table caption + thead tr:first-child td,
table colgroup + thead tr:first-child th,
table colgroup + thead tr:first-child td,
table colgroup + thead tr:first-child th,
table colgroup + thead tr:first-child td,
table thead:first-child tr:first-child th,
table thead:first-child tr:first-child td,
table thead:first-child tr:first-child th,
table thead:first-child tr:first-child td {
  border-top: 1px solid #cccccc;
}
table tbody > tr:nth-child(odd) > td,
table tbody > tr:nth-child(odd) > th {
  background-color: #f7f7f7;
}
table tbody tr:hover > td,
table tbody tr:hover > th {
  background-color: #e9e9e9;
}
table tbody + tbody {
  border-top: 2px solid #cccccc;
}
@media (max-width: 767px) {
  table table {
    font-size: 0.8em;
  }
  table table th,
table table td {
    padding: 7px;
    line-height: 1.3;
  }
  table table th {
    font-weight: 400;
  }
}

/**
 * List styling
 * DO NOT CHANGE THIS FILE!
 * To override any of the settings in this section, add your styling code in the custom directory.
 */
dl,
dt,
dd,
ol,
ul,
li {
  margin-top: 0;
  margin-bottom: 0;
  border: 0;
  outline: 0;
  font-size: 100%;
  vertical-align: baseline;
  background: transparent;
}

ol,
ul {
  padding-left: 18px;
}













/**
 * Plugin Name: WP User Email Verification & Roles
 * Description: Menambahkan role Instruktur, User Verified, User Unverified. Otomatis assign Unverified pada registrasi, dan ubah ke Verified setelah klik link verifikasi.
 * Version: 1.0
 * Author: You
 * License: GPLv2+
 */

/* ========== Konfigurasi ========== */
if (!defined('WP_EV_PLUGIN_VER')) define('WP_EV_PLUGIN_VER','1.0');
if (!defined('WP_EV_VERIFY_EXPIRY')) define('WP_EV_VERIFY_EXPIRY', 7 * DAY_IN_SECONDS); // token valid 7 hari
if (!defined('WP_EV_FROM_NAME')) define('WP_EV_FROM_NAME', get_bloginfo('name'));
if (!defined('WP_EV_FROM_EMAIL')) define('WP_EV_FROM_EMAIL', get_bloginfo('admin_email'));

/* ========== Aktivasi / Deaktivasi ========== */
register_activation_hook(__FILE__, 'wp_ev_on_activate');
register_deactivation_hook(__FILE__, 'wp_ev_on_deactivate');

function wp_ev_on_activate() {
    // buat role
    add_role('instruktur', 'Instruktur', array(
        'read' => true,
        'edit_posts' => true,
        'edit_others_posts' => true,
        'publish_posts' => true,
        'upload_files' => true,
        // tambahkan capability lain sesuai kebutuhan
    ));

    add_role('user_verified', 'User Verified', array(
        'read' => true,
    ));

    add_role('user_unverified', 'User Unverified', array(
        'read' => true,
    ));
}

function wp_ev_on_deactivate() {
    // Tidak otomatis menghapus role agar aman; jika mau hapus, uncomment di bawah:
    // remove_role('instruktur');
    // remove_role('user_verified');
    // remove_role('user_unverified');
}

/* ========== Hook pendaftaran: assign unverified & kirim email ========== */
add_action('user_register', 'wp_ev_handle_new_registration', 10, 1);
function wp_ev_handle_new_registration($user_id) {
    // set role jadi user_unverified
    $user = get_user_by('id', $user_id);
    if ($user) {
        $user->set_role('user_unverified');
    }

    // bikin token verifikasi dan simpan
    $key = wp_generate_password(20, false, false) . wp_generate_password(8, false, false);
    $data = array(
        'key' => wp_hash($key), // simpan hashed
        'created' => time()
    );
    update_user_meta($user_id, '_ev_token', $data);

    // kirim email verifikasi
    wp_ev_send_verification_email($user_id, $key);
}

/* ========== Fungsi kirim email verifikasi ========== */
function wp_ev_send_verification_email($user_id, $plain_key = '') {
    $user = get_user_by('id', $user_id);
    if (!$user) return false;

    // jika plain_key kosong tapi ada token tersimpan, kita tidak bisa kirim ulang hashed => buat token baru
    if (empty($plain_key)) {
        $plain_key = wp_generate_password(20, false, false) . wp_generate_password(8, false, false);
        $data = array('key' => wp_hash($plain_key), 'created' => time());
        update_user_meta($user_id, '_ev_token', $data);
    }

    $uid = $user_id;
    $key = rawurlencode($plain_key);
    $verify_url = add_query_arg(array(
        'ev_verify' => '1',
        'uid' => $uid,
        'key' => $key
    ), site_url('/'));

    $subject = sprintf('[%s] Verifikasi Email Anda', get_bloginfo('name'));
    $message = "Halo " . $user->display_name . ",\n\n";
    $message .= "Silakan klik tautan di bawah untuk memverifikasi alamat email Anda:\n\n";
    $message .= $verify_url . "\n\n";
    $message .= "Tautan ini berlaku selama " . (WP_EV_VERIFY_EXPIRY / DAY_IN_SECONDS) . " hari.\n\n";
    $message .= "Jika Anda tidak mendaftar, abaikan email ini.\n\n";
    $headers = array('Content-Type: text/plain; charset=UTF-8',
        'From: ' . WP_EV_FROM_NAME . ' <' . WP_EV_FROM_EMAIL . '>'
    );

    // gunakan wp_mail
    return wp_mail($user->user_email, $subject, $message, $headers);
}

/* ========== Verifikasi endpoint: tangani klik link ========== */
add_action('init', 'wp_ev_handle_verification_request');
function wp_ev_handle_verification_request() {
    if (!isset($_GET['ev_verify'])) return;

    $uid = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
    $key = isset($_GET['key']) ? sanitize_text_field($_GET['key']) : '';

    if (!$uid || empty($key)) {
        wp_die('Permintaan verifikasi tidak valid.');
    }

    $meta = get_user_meta($uid, '_ev_token', true);
    if (empty($meta) || !is_array($meta) || empty($meta['key']) || empty($meta['created'])) {
        wp_die('Token verifikasi tidak ditemukan atau sudah digunakan.');
    }

    // cek expired
    if ((int)$meta['created'] + WP_EV_VERIFY_EXPIRY < time()) {
        // token kadaluarsa
        delete_user_meta($uid, '_ev_token');
        wp_die('Token verifikasi sudah kadaluarsa. Silakan kirim ulang email verifikasi melalui halaman login atau permintaan kirim ulang.');
    }

    // cocokkan key
    // kita menyimpan hashed versi; bandingkan dengan wp_hash()
    if (!wp_check_hash($meta['key'], $key)) {
        // fallback: jika token disimpan hash dengan wp_hash (kita simpan memang), kita gunakan pendekatan lain:
        if (!wp_hash_equals($meta['key'], wp_hash($key))) {
            wp_die('Token verifikasi tidak cocok.');
        }
    }

    // jika cocok -> ubah role menjadi user_verified
    $user = get_user_by('id', $uid);
    if (!$user) wp_die('User tidak ditemukan.');

    // ubah role
    $user->set_role('user_verified');

    // set meta email_verified
    update_user_meta($uid, 'email_verified', 1);
    delete_user_meta($uid, '_ev_token');

    // redirect ke halaman sukses (bisa ganti ke page custom)
    $redirect = apply_filters('wp_ev_after_verify_redirect', site_url('/?verify=success'));
    wp_safe_redirect($redirect);
    exit;
}

/* ========== Shortcode: resend verification ========== */
/**
 * [ev_resend] menampilkan form untuk kirim ulang token verifikasi (masukkan email)
 */
add_shortcode('ev_resend', 'wp_ev_shortcode_resend');

function wp_ev_shortcode_resend($atts) {
    ob_start();

    if (is_user_logged_in()) {
        $current = wp_get_current_user();
        $uid = $current->ID;
        $role = (array) $current->roles;
        if (in_array('user_verified', $role)) {
            echo '<div>Anda sudah terverifikasi.</div>';
            return ob_get_clean();
        }
    }

    // proses form submit
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ev_resend_email'])) {
        $email = sanitize_email($_POST['ev_resend_email']);
        if (!is_email($email)) {
            echo '<div>Email tidak valid.</div>';
        } else {
            $user = get_user_by('email', $email);
            if (!$user) {
                echo '<div>Email tidak ditemukan.</div>';
            } else {
                $uid = $user->ID;
                // buat token baru & kirim
                $plain_key = wp_generate_password(20, false, false) . wp_generate_password(8, false, false);
                $data = array('key' => wp_hash($plain_key), 'created' => time());
                update_user_meta($uid, '_ev_token', $data);
                if (wp_ev_send_verification_email($uid, $plain_key)) {
                    echo '<div>Email verifikasi telah dikirim ke ' . esc_html($email) . '.</div>';
                } else {
                    echo '<div>Gagal mengirim email. Pastikan konfigurasi email di server benar.</div>';
                }
            }
        }
    }

    // tampilkan form
    ?>
    <form method="post">
        <label>Email untuk kirim ulang verifikasi:</label><br>
        <input type="email" name="ev_resend_email" required />
        <button type="submit">Kirim Ulang</button>
    </form>
    <?php

    return ob_get_clean();
}

/* ========== Helper functions ========== */
/**
 * Cek apakah user sudah verified
 */
function wp_ev_is_user_verified($user_id = null) {
    if (!$user_id) {
        if (!is_user_logged_in()) return false;
        $user_id = get_current_user_id();
    }
    $meta = get_user_meta($user_id, 'email_verified', true);
    return (bool)$meta;
}

/* ========== Utility kecil untuk hash checking (agar kompatibel) ========== */
/**
 * Simpel wrapper: hash dan check; kita gunakan wp_hash untuk menyimpan hashed token
 */
if (!function_exists('wp_check_hash')) {
    function wp_check_hash($stored_hash, $plain) {
        // perbandingan sederhana
        if (empty($stored_hash) || empty($plain)) return false;
        return hash_equals($stored_hash, wp_hash($plain));
    }
}

if (!function_exists('wp_hash')) {
    function wp_hash($input) {
        // gunakan salt WordPress jika tersedia
        $salt = defined('AUTH_SALT') ? AUTH_SALT : 'wp_ev_default_salt';
        return hash_hmac('sha256', $input, $salt);
    }
}
