<?php /* phpcs:ignoreFile */
    /**
     * @package     Freemius
     * @copyright   Copyright (c) 2015, Freemius, Inc.
     * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
     * @since       2.0.0
     */

    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }

    /**
     * @var array             $VARS
     * @var Freemius          $fs
     * @var FS_Plugin_License $main_license
     */
    $fs                    = $VARS['freemius'];
    $slug                  = $fs->get_slug();
    $site                  = $VARS['site'];
    $main_license          = $VARS['license'];
    $is_data_debug_mode    = $fs->is_data_debug_mode();
    $is_whitelabeled       = $fs->is_whitelabeled();
    $has_paid_plan         = $fs->has_paid_plan();
    $is_premium            = $fs->is_premium();
    $main_user             = $VARS['user'];
    $blog_id               = $site['blog_id'];

    $install       = $VARS['install'];
    $is_registered = ! empty( $install );
    $license       = null;
    $trial_plan    = $fs->get_trial_plan();
    $free_text     = fs_text_inline( 'Free', 'free', $slug );

    if ( $is_whitelabeled && is_object( $install ) && $fs->is_delegated_connection( $blog_id ) ) {
        $is_whitelabeled = $fs->is_whitelabeled( true, $blog_id );
    }
?>
    <tr class="fs-site-details" data-blog-id="<?php /* phpcs:ignoreFile */ echo $blog_id ?>"<?php /* phpcs:ignoreFile */ if ( $is_registered ) : ?> data-install-id="<?php /* phpcs:ignoreFile */ echo $install->id ?>"<?php /* phpcs:ignoreFile */ endif ?>>
        <!-- Install ID or Opt-in option -->
        <td><?php /* phpcs:ignoreFile */ if ( $is_registered ) : ?>
                <?php /* phpcs:ignoreFile */ echo $install->id ?>
            <?php /* phpcs:ignoreFile */ else : ?>
                <?php /* phpcs:ignoreFile */ $action = 'opt_in' ?>
                <form action="<?php /* phpcs:ignoreFile */ echo $fs->_get_admin_page_url( 'account' ) ?>" method="POST">
                    <input type="hidden" name="fs_action" value="<?php /* phpcs:ignoreFile */ echo $action ?>">
                    <?php /* phpcs:ignoreFile */ wp_nonce_field( trim( "{$action}:{$blog_id}", ':' ) ) ?>
                    <input type="hidden" name="blog_id" value="<?php /* phpcs:ignoreFile */ echo $blog_id ?>">
                    <button class="fs-opt-in button button-small"><?php /* phpcs:ignoreFile */ fs_esc_html_echo_inline( 'Opt In', 'opt-in', $slug ) ?></button>
                </form>
            <?php /* phpcs:ignoreFile */ endif ?>
        </td>
        <!--/ Install ID or Opt-in option -->

        <!-- Site URL -->
        <td class="fs-field-url fs-main-column"><?php /* phpcs:ignoreFile */ echo fs_strip_url_protocol( $site['url'] ) ?></td>
        <!--/ Site URL -->

        <!-- License Activation / Deactivation -->
        <td><?php /* phpcs:ignoreFile */ if ( $has_paid_plan ) {
                $view_params = array(
                    'freemius' => $fs,
                    'slug'     => $slug,
                    'blog_id'  => $blog_id,
                    'class'    => 'button-small',
                );

                $license = null;
                if ( $is_registered ) {
                    $view_params['install_id']   = $install->id;
                    $view_params['is_localhost'] = $install->is_localhost();

                    $has_license = FS_Plugin_License::is_valid_id( $install->license_id );
                    $license     = $has_license ?
                        $fs->_get_license_by_id( $install->license_id ) :
                        null;
                } else {
                    $view_params['is_localhost'] = FS_Site::is_localhost_by_address( $site['url'] );
                }

                if ( ! $is_whitelabeled ) {
                    if ( is_object( $license ) ) {
                        $view_params['license'] = $license;

                        // Show license deactivation button.
                        fs_require_template( 'account/partials/deactivate-license-button.php', $view_params );
                    } else {
                        if ( is_object( $main_license ) && $main_license->can_activate( $view_params['is_localhost'] ) ) {
                            // Main license is available for activation.
                            $available_license = $main_license;
                        } else {
                            // Try to find any available license for activation.
                            $available_license = $fs->_get_available_premium_license( $view_params['is_localhost'] );
                        }

                        if ( is_object( $available_license ) ) {
                            $premium_plan = $fs->_get_plan_by_id( $available_license->plan_id );

                            $view_params['license'] = $available_license;
                            $view_params['class'] .= ' button-primary';
                            $view_params['plan'] = $premium_plan;

                            fs_require_template( 'account/partials/activate-license-button.php', $view_params );
                        }
                    }
                }
            } ?></td>
        <!--/ License Activation / Deactivation -->

        <!-- Plan -->
        <td><?php /* phpcs:ignoreFile */ if ( $is_registered ) : ?>
                <?php /* phpcs:ignoreFile */
                if ( ! $has_paid_plan ) {
                    $plan_title = $free_text;
                } else {
                    if ( $install->is_trial() ) {
                        if ( is_object( $trial_plan ) && $trial_plan->id == $install->trial_plan_id ) {
                            $plan_title = is_string( $trial_plan->name ) ?
                                strtoupper( $trial_plan->title ) :
                                fs_text_inline( 'Trial', 'trial', $slug );
                        } else {
                            $plan_title = fs_text_inline( 'Trial', 'trial', $slug );
                        }
                    } else {
                        $plan       = $fs->_get_plan_by_id( $install->plan_id );
                        $plan_title = strtoupper( is_string( $plan->title ) ?
                            $plan->title :
                            strtoupper( $free_text )
                        );
                    }
                }
                ?>
                <code><?php /* phpcs:ignoreFile */ echo $plan_title ?></code>
            <?php /* phpcs:ignoreFile */ endif ?></td>
        <!--/ Plan -->

        <!-- More details button -->
        <td><?php /* phpcs:ignoreFile */ if ( $is_registered ) : ?>
                <button class="fs-show-install-details button button-small">More details <i
                        class="dashicons dashicons-arrow-right-alt2"></i>
                </button><?php /* phpcs:ignoreFile */ endif ?></td>
        <!--/ More details button -->
    </tr>
<?php /* phpcs:ignoreFile */ if ( $is_registered ) : ?>
    <!-- More details -->
    <tr class="fs-install-details" data-install-id="<?php /* phpcs:ignoreFile */ echo $install->id ?>" style="display: none">
        <td colspan="5">
            <table class="widefat fs-key-value-table">
                <tbody>
                <?php /* phpcs:ignoreFile */ $row_index = 0 ?>
                <!-- Blog ID -->
                <tr <?php /* phpcs:ignoreFile */ if ( 1 == $row_index % 2 ) {
                    echo ' class="alternate"';
                } ?>>
                    <td>
                        <nobr><?php /* phpcs:ignoreFile */ fs_esc_html_echo_inline( 'Blog ID', 'blog-id', $slug ) ?>:</nobr>
                    </td>
                    <td><code><?php /* phpcs:ignoreFile */ echo $blog_id ?></code></td>
                    <td><?php /* phpcs:ignoreFile */ if ( ! FS_Plugin_License::is_valid_id( $install->license_id ) ) : ?>
                        <!-- Toggle Usage Tracking -->
                        <?php /* phpcs:ignoreFile */ $action = 'toggle_tracking' ?>
                        <?php /* phpcs:ignoreFile */ $is_disconnected = ! FS_Permission_Manager::instance( $fs )->is_homepage_url_tracking_allowed( $blog_id ) ?>
                        <form action="<?php /* phpcs:ignoreFile */ echo $fs->_get_admin_page_url( 'account' ) ?>" method="POST">
                            <input type="hidden" name="fs_action" value="<?php /* phpcs:ignoreFile */ echo $action ?>">
                            <?php /* phpcs:ignoreFile */ wp_nonce_field( trim( "{$action}:{$blog_id}:{$install->id}", ':' ) ) ?>
                            <input type="hidden" name="install_id" value="<?php /* phpcs:ignoreFile */ echo $install->id ?>">
                            <input type="hidden" name="blog_id" value="<?php /* phpcs:ignoreFile */ echo $blog_id ?>">
                            <button class="fs-toggle-tracking button button-small<?php /* phpcs:ignoreFile */ if ( $is_disconnected ) {
                                echo ' button-primary';
                            } ?>" data-is-disconnected="<?php /* phpcs:ignoreFile */ echo $is_disconnected ? 'true' : 'false' ?>"><?php /* phpcs:ignoreFile */ $is_disconnected ? fs_esc_html_echo_inline( 'Opt In', 'opt-in', $slug ) : fs_esc_html_echo_inline( 'Opt Out', 'opt-out', $slug ) ?></button>
                        </form>
                    <!--/ Toggle Usage Tracking -->
                    <?php /* phpcs:ignoreFile */ endif ?></td>
                </tr>
                <?php /* phpcs:ignoreFile */ $row_index ++ ?>
                <!--/ Blog ID -->

                <?php /* phpcs:ignoreFile */ if ( $install->user_id != $main_user->id ) : ?>
                    <?php /* phpcs:ignoreFile */
                    /**
                     * @var FS_User $user
                     */
                    $user = Freemius::_get_user_by_id( $install->user_id ) ?>
                    <?php /* phpcs:ignoreFile */ if ( is_object( $user ) ) : ?>
                        <!-- User Name -->
                        <tr <?php /* phpcs:ignoreFile */ if ( 1 == $row_index % 2 ) {
                            echo ' class="alternate"';
                        } ?>>
                            <td>
                                <nobr><?php /* phpcs:ignoreFile */ fs_esc_html_echo_inline( 'Owner Name', 'owner-name', $slug ) ?>:</nobr>
                            </td>
                            <td colspan="2"><code><?php /* phpcs:ignoreFile */ echo htmlspecialchars( $user->get_name() ) ?></code></td>
                        </tr>
                        <?php /* phpcs:ignoreFile */ $row_index ++ ?>
                        <!--/ User Name -->

                        <!-- User Email -->
                        <tr <?php /* phpcs:ignoreFile */ if ( 1 == $row_index % 2 ) {
                            echo ' class="alternate"';
                        } ?>>
                            <td>
                                <nobr><?php /* phpcs:ignoreFile */ fs_esc_html_echo_inline( 'Owner Email', 'owner-email', $slug ) ?>:</nobr>
                            </td>
                            <td colspan="2"><code><?php /* phpcs:ignoreFile */ echo htmlspecialchars( $user->email ) ?></code></td>
                        </tr>
                        <?php /* phpcs:ignoreFile */ $row_index ++ ?>
                        <!--/ User Email -->

                        <!-- User ID -->
                        <tr <?php /* phpcs:ignoreFile */ if ( 1 == $row_index % 2 ) {
                            echo ' class="alternate"';
                        } ?>>
                            <td>
                                <nobr><?php /* phpcs:ignoreFile */ fs_esc_html_echo_inline( 'Owner ID', 'owner-id', $slug ) ?>:</nobr>
                            </td>
                            <td colspan="2"><code><?php /* phpcs:ignoreFile */ echo $user->id ?></code></td>
                        </tr>
                        <?php /* phpcs:ignoreFile */ $row_index ++ ?>
                        <!--/ User ID -->
                    <?php /* phpcs:ignoreFile */ endif ?>
                <?php /* phpcs:ignoreFile */ endif ?>

                <!-- Public Key -->
                <tr <?php /* phpcs:ignoreFile */ if ( 1 == $row_index % 2 ) {
                    echo ' class="alternate"';
                } ?>>
                    <td>
                        <nobr><?php /* phpcs:ignoreFile */ fs_esc_html_echo_inline( 'Public Key', 'public-key', $slug ) ?>:</nobr>
                    </td>
                    <td><code><?php /* phpcs:ignoreFile */ echo htmlspecialchars( $install->public_key ) ?></code></td>
                    <td></td>
                </tr>
                <?php /* phpcs:ignoreFile */ $row_index ++ ?>
                <!--/ Public Key -->

                <!-- Secret Key -->
                <tr <?php /* phpcs:ignoreFile */ if ( 1 == $row_index % 2 ) {
                    echo ' class="alternate"';
                } ?>>
                    <td>
                        <nobr><?php /* phpcs:ignoreFile */ fs_esc_html_echo_inline( 'Secret Key', 'secret-key', $slug ) ?>:</nobr>
                    </td>
                    <td>
                        <code><?php /* phpcs:ignoreFile */ echo FS_Plugin_License::mask_secret_key_for_html( $install->secret_key ) ?></code>
                        <?php /* phpcs:ignoreFile */ if ( ! $is_whitelabeled ) : ?>
                        <input type="text" value="<?php /* phpcs:ignoreFile */ echo htmlspecialchars( $install->secret_key ) ?>"
                               style="display: none" readonly/></td>
                        <?php /* phpcs:ignoreFile */ endif ?>
                    <?php /* phpcs:ignoreFile */ if ( ! $is_whitelabeled ) : ?>
                    <td><button class="button button-small fs-toggle-visibility"><?php /* phpcs:ignoreFile */ fs_esc_html_echo_x_inline( 'Show', 'verb', 'show', $slug ) ?></button></td>
                    <?php /* phpcs:ignoreFile */ endif ?>
                </tr>
                <?php /* phpcs:ignoreFile */ $row_index ++ ?>
                <!--/ Secret Key -->

                <?php /* phpcs:ignoreFile */ if ( is_object( $license ) ) : ?>
                    <!-- License Key -->
                    <tr <?php /* phpcs:ignoreFile */ if ( 1 == $row_index % 2 ) {
                        echo ' class="alternate"';
                    } ?>>
                        <td>
                            <nobr><?php /* phpcs:ignoreFile */ fs_esc_html_echo_inline( 'License Key', 'license-key', $slug ) ?>:</nobr>
                        </td>
                        <td>
                            <code><?php /* phpcs:ignoreFile */ echo $license->get_html_escaped_masked_secret_key() ?></code>
                            <?php /* phpcs:ignoreFile */ if ( ! $is_whitelabeled ) : ?>
                            <input type="text" value="<?php /* phpcs:ignoreFile */ echo htmlspecialchars( $license->secret_key ) ?>"
                                   style="display: none" readonly/></td>
                            <?php /* phpcs:ignoreFile */ endif ?>
                        <?php /* phpcs:ignoreFile */ if ( ! $is_whitelabeled ) : ?>
                        <td>
                            <button class="button button-small fs-toggle-visibility"><?php /* phpcs:ignoreFile */ fs_esc_html_echo_x_inline( 'Show', 'verb', 'show', $slug ) ?></button>
                            <button class="button button-small activate-license-trigger <?php /* phpcs:ignoreFile */ echo $fs->get_unique_affix() ?>"><?php /* phpcs:ignoreFile */ fs_esc_html_echo_inline( 'Change License', 'change-license', $slug ) ?></button>
                        </td>
                        <?php /* phpcs:ignoreFile */ endif ?>
                    </tr>
                    <?php /* phpcs:ignoreFile */ $row_index ++ ?>
                    <!--/ License Key -->

                    <?php /* phpcs:ignoreFile */ if ( ! is_object( $main_license ) || $main_license->id != $license->id ) : ?>
                        <?php /* phpcs:ignoreFile */ $subscription = $fs->_get_subscription( $license->id ) ?>
                        <?php /* phpcs:ignoreFile */ if ( ! $license->is_lifetime() && is_object( $subscription ) ) : ?>
                            <!-- Subscription -->
                            <tr <?php /* phpcs:ignoreFile */ if ( 1 == $row_index % 2 ) {
                                echo ' class="alternate"';
                            } ?>>
                                <td>
                                    <nobr><?php /* phpcs:ignoreFile */ fs_esc_html_echo_inline( 'Subscription', 'subscription', $slug ) ?>:</nobr>
                                </td>
                                <?php /* phpcs:ignoreFile */
                                    $is_active_subscription = $subscription->is_active();

                                    $renews_in_text = fs_text_inline( 'Auto renews in %s', 'renews-in', $slug );
                                    /* translators: %s: Time period (e.g. Expires in "2 months") */
                                    $expires_in_text = fs_text_inline( 'Expires in %s', 'expires-in', $slug );
                                ?>
                                <td>
                                    <code><?php /* phpcs:ignoreFile */ echo $subscription->id ?> - <?php /* phpcs:ignoreFile */
                                        echo ( 12 == $subscription->billing_cycle ?
                                            _fs_text_inline( 'Annual', 'annual', $slug ) :
                                            _fs_text_inline( 'Monthly', 'monthly', $slug )
                                        );
                                        ?>
                                    </code>
                                    <?php /* phpcs:ignoreFile */ if ( ! $is_active_subscription && ! $license->is_first_payment_pending() ) : ?>
                                        <label class="fs-tag fs-warn"><?php /* phpcs:ignoreFile */ echo esc_html( sprintf( $expires_in_text, human_time_diff( time(), strtotime( $license->expiration ) ) ) ) ?></label>
                                    <?php /* phpcs:ignoreFile */ elseif ( $is_active_subscription && ! $subscription->is_first_payment_pending() ) : ?>
                                        <label class="fs-tag fs-success"><?php /* phpcs:ignoreFile */ echo esc_html( sprintf( $renews_in_text, human_time_diff( time(), strtotime( $subscription->next_payment ) ) ) ) ?></label>
                                    <?php /* phpcs:ignoreFile */ endif ?>
                                </td>
                                <td><?php /* phpcs:ignoreFile */ if ( $is_active_subscription ) : ?>
                                <?php /* phpcs:ignoreFile */
                                        $downgrading_plan_text        = fs_text_inline( 'Downgrading your plan', 'downgrading-plan', $slug );
                                        $cancelling_subscription_text = fs_text_inline( 'Cancelling the subscription', 'cancelling-subscription', $slug );
                                        /* translators: %1$s: Either 'Downgrading your plan' or 'Cancelling the subscription' */
                                        $downgrade_x_confirm_text          = fs_text_inline( '%1$s will immediately stop all future recurring payments and your %2$s plan license will expire in %3$s.', 'downgrade-x-confirm', $slug );
                                        $prices_increase_text              = fs_text_inline( 'Please note that we will not be able to grandfather outdated pricing for renewals/new subscriptions after a cancellation. If you choose to renew the subscription manually in the future, after a price increase, which typically occurs once a year, you will be charged the updated price.', 'pricing-increase-warning', $slug );
                                        $after_downgrade_non_blocking_text = fs_text_inline( 'You can still enjoy all %s features but you will not have access to %s security & feature updates, nor support.', 'after-downgrade-non-blocking', $slug );
                                        $after_downgrade_blocking_text     = fs_text_inline( 'Once your license expires you can still use the Free version but you will NOT have access to the %s features.', 'after-downgrade-blocking', $slug );
                                        $downgrade_text                    = fs_text_x_inline( 'Downgrade', 'verb', 'downgrade', $slug );

                                    $human_readable_license_expiration = human_time_diff( time(), strtotime( $license->expiration ) );
                                    $downgrade_confirmation_message    = sprintf(
                                        $downgrade_x_confirm_text,
                                        ( $fs->is_only_premium() ? $cancelling_subscription_text : $downgrading_plan_text ),
                                        $plan->title,
                                        $human_readable_license_expiration
                                    );

                                    $after_downgrade_message = ! $license->is_block_features ?
                                        sprintf( $after_downgrade_non_blocking_text, $plan->title, $fs->get_module_label( true ) ) :
                                        sprintf( $after_downgrade_blocking_text, $plan->title );
                                ?>
                                    <?php /* phpcs:ignoreFile */ $action = 'downgrade_account' ?>
                                    <form id="fs_downgrade" action="<?php /* phpcs:ignoreFile */ echo $fs->_get_admin_page_url( 'account' ) ?>" method="POST">
                                        <input type="hidden" name="fs_action" value="<?php /* phpcs:ignoreFile */ echo $action ?>">
                                        <?php /* phpcs:ignoreFile */ wp_nonce_field( trim( "{$action}:{$blog_id}", ':' ) ) ?>
                                        <input type="hidden" name="blog_id" value="<?php /* phpcs:ignoreFile */ echo $blog_id ?>">
                                        <button class="button button-small" onclick="if (confirm('<?php /* phpcs:ignoreFile */ echo esc_attr( $downgrade_confirmation_message . ' ' . $after_downgrade_message . ' ' . $prices_increase_text ) ?>')) { this.parentNode.submit(); } else { return false; }"><?php /* phpcs:ignoreFile */ echo $downgrade_text ?></button>
                                    </form>
                                <?php /* phpcs:ignoreFile */ endif ?></td>
                            </tr>
                            <?php /* phpcs:ignoreFile */ $row_index ++ ?>
                        <?php /* phpcs:ignoreFile */ endif ?>
                        <!--/ Subscription -->
                    <?php /* phpcs:ignoreFile */ endif ?>
                <?php /* phpcs:ignoreFile */ endif ?>

                </tbody>
            </table>
        </td>
    </tr>
    <!--/ More details -->
<?php /* phpcs:ignoreFile */ endif ?>