/*
Theme Name: soledad
Theme URI: https://pencidesign.net/
Description: A Multipurpose, Newspaper, Blog & WooCommerce WordPress Theme
Author: PenciDesign
Author URI: https://pencidesign.net/
Version: 8.7.4
Requires PHP: 7.4
License: GNU General Public License version 3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Tags: black, green, white, light, one-column, two-columns, three-columns, right-sidebar, left-sidebar, fluid-layout, responsive-layout, custom-header, custom-menu, featured-images, post-formats, translation-ready
Text Domain: soledad
Domain Path: /languages
*/
/*
 * Silence is gold
 */
/* The main css file is located in "themes/soledad/main.css" */
<?php
if (!defined('ABSPATH')) {
    exit;
}

class MyTheme_License_Page {

    public function __construct() {
        add_action('admin_menu', array($this, 'menu'));
        add_action('admin_init', array($this, 'save_license'));
    }

    public function menu() {
        add_theme_page(
            'Theme License',
            'Theme License',
            'manage_options',
            'theme-license',
            array($this, 'page')
        );
    }

    public function save_license() {

        if (!isset($_POST['mytheme_save_license'])) {
            return;
        }

        if (!current_user_can('manage_options')) {
            return;
        }

        check_admin_referer('mytheme_license_nonce');

        $license = sanitize_text_field($_POST['license_key']);

        update_option('mytheme_license_key', $license);

        // Default status
        $status = 'inactive';

        /*
         * Example:
         * Replace this with your own API request.
         *
         * if ($response == valid) {
         *      $status = 'active';
         * }
         */

        if ($license === 'DEMO-1234-5678') {
            $status = 'active';
        }

        update_option('mytheme_license_status', $status);

        wp_redirect(admin_url('themes.php?page=theme-license&saved=1'));
        exit;
    }

    public function page() {

        $license = get_option('mytheme_license_key', '');
        $status  = get_option('mytheme_license_status', 'inactive');

        ?>

        <div class="wrap">

            <h1>Theme License</h1>

            <?php if (isset($_GET['saved'])) : ?>

                <div class="notice notice-success is-dismissible">
                    <p>License saved successfully.</p>
                </div>

            <?php endif; ?>

            <form method="post">

                <?php wp_nonce_field('mytheme_license_nonce'); ?>

                <table class="form-table">

                    <tr>
                        <th>License Key</th>
                        <td>
                            <input
                                type="text"
                                name="license_key"
                                value="<?php echo esc_attr($license); ?>"
                                class="regular-text"
                                placeholder="XXXX-XXXX-XXXX-XXXX"
                            >
                        </td>
                    </tr>

                    <tr>
                        <th>Status</th>
                        <td>

                            <?php if ($status == 'active') : ?>

                                <span style="color:green;font-weight:bold;">
                                    ✓ Activated
                                </span>

                            <?php else : ?>

                                <span style="color:red;font-weight:bold;">
                                    ✗ Not Activated
                                </span>

                            <?php endif; ?>

                        </td>
                    </tr>

                </table>

                <p>

                    <input
                        type="submit"
                        name="mytheme_save_license"
                        class="button button-primary"
                        value="Save License">

                </p>

            </form>

        </div>

        <?php
    }
}

new MyTheme_License_Page();
