<?php
/**
 * Built-in CDN section template.
 *
 * Displays the page list for built-in CDN with add page functionality.
 *
 * @since 3.22
 *
 * @param array $data {
 *     Section data.
 *
 *     @type string $id          Section identifier.
 *     @type string $title       Section title.
 *     @type string $description Section description.
 *     @type string $class       Section classes.
 *     @type string $help        Data to pass to beacon.
 *     @type string $page        Page section identifier.
 *     @type array  $status_indicator Data for the CDN status indicator partial.
 *     @type array  $cta_data    Data for the CTA banner.
 *     @type string $renewal_url    URL for renewing the license.
 *     @type bool   $active_subscription Whether the user has an active subscription or not.
 *     @type bool   $is_active    Whether RocketCDN Free is the currently applied CDN mode.
 *     @type bool   $is_forced_off Whether the mode toggle must be disabled (subscription loading, expired/banned licence).
 *     @type string $toggle_tooltip Tooltip shown on the mode toggle when $is_forced_off is true.
 * }
 */

defined( 'ABSPATH' ) || exit;

$rocket_rocketcdn_free_active = $data['is_active'];
?>

<?php // wpr-cdn-active-indicator is kept in sync with JS (cdn-driver.js), which reads it off .wpr-optionHeader to find the previously-active mode on a failed switch — it no longer drives any visible styling on this header itself. ?>
<div class="wpr-optionHeader wpr-optionHeader--cdn-driver <?php echo esc_attr( $data['class'] ); ?><?php echo $rocket_rocketcdn_free_active ? ' wpr-cdn-active-indicator' : ''; ?>">
	<div class="wpr-optionHeader__title-group">
		<h3 class="wpr-title2 wpr-title2--orange"><?php echo esc_html( $data['title'] ); ?></h3>
		<span class="wpr-badge wpr-badge--grey"><?php esc_html_e( 'Free', 'rocket' ); ?></span>
		<?php
		$this->render_parts_with_data(
			'cdn/cdn-mode-toggle',
			[
				'id'            => 'wpr-rocketcdn-free-toggle',
				'cdn_mode'      => 'rocketcdn_free',
				'checked'       => $rocket_rocketcdn_free_active,
				'is_forced_off' => $data['is_forced_off'],
				'label'         => __( 'Enable RocketCDN Free', 'rocket' ),
				'tooltip'       => $data['toggle_tooltip'],
			]
		);
		?>
	</div>
	<?php // Temporarily hidden for the next release; remove the `false &&` guard to restore the Upgrade to PRO button. ?>
	<?php if ( false && ! empty( $data['upgrade_url'] ) ) : ?>
		<button type="button" class="rocketcdn--upgrade_btn wpr-rocketcdn-open"<?php echo empty( $data['upgrade_url'] ) ? ' data-micromodal-trigger="wpr-rocketcdn-modal"' : ''; ?>>
			<?php esc_html_e( 'Upgrade to PRO', 'rocket' ); ?>
		</button>
	<?php endif; ?>

	<?php if ( ! empty( $data['help'] ) ) : ?>
	<a href="<?php echo esc_url( $data['help']['url'] ); ?>" data-beacon-id="<?php echo esc_attr( $data['help']['id'] ); ?>" data-wpr_track_button="Need Help" data-wpr_track_context="Settings" class="wpr-infoAction wpr-infoAction--help wpr-icon-help" target="_blank"><?php esc_html_e( 'Need Help?', 'rocket' ); ?></a>
	<?php endif; ?>
</div>

<div class="<?php echo esc_attr( $data['class'] ); ?>">
	<?php
	/**
	 * Fires before the RocketCDN free status indicator is displayed.
	 *
	 * @since 3.22
	 * @param array $cta_data CTA banner data.
	 */
	do_action( 'rocket_cdn_free_before_status_indicator', $data['cta_data'] );
	?>
</div>

<div class="wpr-cdn-built-in <?php echo esc_attr( $data['class'] ); ?>">
	<?php
	$this->render_parts_with_data( 'cdn/cdn-status-indicator', $data['status_indicator'] );
	?>
	<?php if ( ! $data['status_indicator']['no_status_indicator'] ) : ?>
	<div class="wpr-cdn-built-in__separator"></div>
	<?php endif; ?>

	<?php
	/**
	 * Fires to render the built-in CDN page list table.
	 *
	 * @since 3.22
	 */
	do_action( 'rocket_cdn_free_page_list' );
	?>

	<div class="wpr-cdn-add-page" id="wpr_add_page_component">
		<div class="wpr-cdn-add-page__input-wrap">
			<input type="text" id="wpr_cdn_add_page_input" placeholder="<?php esc_attr_e( 'Enter a page URL to add to RocketCDN', 'rocket' ); ?>" />
			<button type="button" class="wpr-cdn-add-page__homepage<?php echo $data['status_indicator']['pages_count'] > 0 ? ' wpr-isHidden' : ''; ?>">
				<span class="wpr-cdn-add-page__icon"></span>
				<?php esc_html_e( 'ADD HOMEPAGE', 'rocket' ); ?>
			</button>
		</div>
		<div class="wpr-cdn-add-page__button-wrapper<?php echo ! empty( $data['limit_reached'] ) ? ' wpr-btn-with-tool-tip' : ''; ?>">
			<button type="button" class="wpr-cdn-add-page__button"<?php echo ! empty( $data['limit_reached'] ) ? ' disabled="disabled"' : ''; ?>>
				<span class="wpr-icon-plus"></span>
				<?php esc_html_e( 'ADD PAGE', 'rocket' ); ?>
			</button>
			<div class="wpr-tooltip<?php echo empty( $data['limit_reached'] ) ? ' wpr-isHidden' : ''; ?>">
				<div class="wpr-tooltip-content">
					<?php esc_html_e( 'You have reached the limit of 3 free pages.', 'rocket' ); ?>
				</div>
			</div>
		</div>
	</div>

	<?php
	/**
	 * Fires after the built-in CDN page list.
	 *
	 * Used to display the upsell banner.
	 *
	 * @since 3.22
	 */
	do_action( 'rocket_after_built_in_cdn_list' );
	?>
</div>
