<?php
if ( !defined( 'ABSPATH' ) ) exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- These are local variables in a template file, not global variables
$campaign_params = '?utm_source=wpsl-add-ons&utm_medium=banner&utm_campaign=add-ons';

// Load the add-on data from an existing transient, or grab new data from the remote URL.
if ( false === ( $add_ons = get_transient( 'wpsl_addons' ) ) ) {
    $response = wp_remote_get( 'https://s3.amazonaws.com/wpsl/add-ons.json' );

    if ( !is_wp_error( $response ) ) {
        $add_ons = json_decode( wp_remote_retrieve_body( $response ) );

        if ( $add_ons ) {
            set_transient( 'wpsl_addons', $add_ons, WEEK_IN_SECONDS );
        }
    }
}
?>

<div class="wrap wpsl-add-ons">
    <h2><?php esc_html_e( 'WP Store Locator Add-Ons', 'wp-store-locator' ); ?></h2>

    <?php
    if ( $add_ons ) {
        foreach ( $add_ons as $add_on ) {
    ?>
        <div class="wpsl-add-on">
            <?php if ( !empty( $add_on->url ) ) { ?>
            <a title="<?php echo esc_attr( $add_on->name ); ?>" href="<?php echo esc_url( $add_on->url . $campaign_params ); ?>">
                <img src="<?php echo esc_url( $add_on->img ); ?>"/>
            </a>
            <?php } else { ?>
            <img src="<?php echo esc_url( $add_on->img ); ?>"/>
            <?php } ?>

            <div class="wpsl-add-on-desc">
                <p><?php echo esc_html( $add_on->desc ); ?></p>
                
                <div class="wpsl-add-on-status">
                    <?php if ( !empty( $add_on->class ) && class_exists( $add_on->class ) ) { ?>
                    <p><strong><?php esc_html_e( 'Already Installed.', 'wp-store-locator' ); ?></strong></p>
                    <?php } else if ( isset( $add_on->soon ) && $add_on->soon ) { ?>
                    <p><strong><?php esc_html_e( 'Coming soon!', 'wp-store-locator' ); ?></strong></p>
                    <?php } else { ?>
                    <a class="button-primary" href="<?php echo esc_url( $add_on->url . $campaign_params ); ?>">
                        <?php esc_html_e( 'Get This Add-On', 'wp-store-locator' ); ?>
                    </a>
                    <?php } ?>
                </div>
            </div>
        </div>
    <?php 
        }
    } else {
        echo '<p>'. esc_html__( 'Failed to load the add-on list from the server.', 'wp-store-locator' ) . '</p>';
        echo '<p>'. esc_html__( 'Please try again later!', 'wp-store-locator' ) . '</p>';
    }
    ?>  
</div>