<?php
// phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment

// Don't load directly
if (!defined('ABSPATH')) { exit; }

// Reminder: Use r34ics_admin_access() and r34ics_admin_full_access() as needed!

add_action('r34ics_admin_utilities_cache', function() {
	return; // @todo Pre-caching temporarily removed in v. 6.0.0 pending needed performance improvements
	if (get_option('r34icspro_precache')) {
		?>
		<p><em>
			<?php

			printf(esc_html__('Pre-caching is currently %1$son%2$s. Last pre-cached:', 'ics-calendar-pro'), '<strong style="color: var(--r34ics--color--forestgreen);">', '</strong>');
			echo ' ';
			if ($r34icspro_precache_completed = get_option('r34icspro_precache_completed')) {
				echo wp_kses_post(r34ics_date(get_option('date_format') . ' ' . get_option('time_format'), $r34icspro_precache_completed));
			}
			else {
				esc_html_e('never', 'ics-calendar-pro');
			}
			?> 
		</em></p>
		<?php
	}
	else {
		?>
		<p><em>
			<?php

			printf(esc_html__('Pre-caching is currently %1$soff%2$s.', 'ics-calendar-pro'), '<strong style="color: var(--r34ics--color--orangered);">', '</strong>');
			?> 
		</em></p>
		<?php
	}
});

add_action('r34ics_admin_utilities_more', function() {
	global $r34icspro_acf_blocks_step, $r34icspro_acf_blocks_status, $r34icspro_acf_blocks_affected_posts;

	// Convert ACF-based ICS Calendar blocks to saved calendars
	if (function_exists('r34ics_admin_access') && r34ics_admin_access() && !is_plugin_active('classic-editor/classic-editor.php')) {
		?>
		<div id="convert-acf-blocks" class="r34ics-inner-box">
		
			<h3><?php

			printf(esc_html__('Convert %1$s Blocks', 'ics-calendar-pro'), 'ICS Calendar');
			?></h3>
			
			<form id="r34icspro-convert-acf-blocks" method="post" action="#utilities">
				<?php
				wp_nonce_field('r34icspro', 'r34icspro-convert-acf-blocks-nonce');

				// Step 1: Choose to see the list of affected pages and posts
				if (empty($r34icspro_acf_blocks_step)) {
					?>
					<p><mark class="alert">
						<?php

						printf(esc_html__('Due to performance and compatibility issues, the previous %2$s Block was removed in version 6.0.', 'ics-calendar-pro'), 'ACF Blocks', 'ICS Calendar');
						?>
					</mark></p>
					<p>
						<?php

						printf(esc_html__('This utility searches your pages and posts for each instance of the previous %1$s Block, creates a new saved calendar in Calendar Builder with the same configuration, updates the blocks within your pages to the new configuration.', 'ics-calendar-pro'), 'ICS Calendar', 'ICS Calendar', 'ICS Calendar Pro');
						?>
					</p>
		
					<input type="hidden" name="step" value="1" />
					<p><input type="submit" class="button" value="<?php

					echo esc_attr(sprintf(esc_html__('Show Existing %1$s Blocks', 'ics-calendar-pro'), 'ICS Calendar'));
					?>" /></p>
					<p><?php

					printf(esc_html__('Click the button to see a list of the pages and posts on your site that currently contain %1$s Blocks. You will then have the opportunity to decide whether to convert them or leave them as-is. If you do not convert the blocks, they will no longer display.', 'ics-calendar-pro'), 'ICS Calendar');
					?></p>
					<?php
				}
				
				// Step 2: Show the list of affected posts and choose to convert
				elseif ($r34icspro_acf_blocks_step == 1) {
					if (empty($r34icspro_acf_blocks_affected_posts)) {
						?>
						<p><mark class="info"><?php

						printf(esc_html__('Your site does not contain any %1$s Blocks that need to be converted.', 'ics-calendar-pro'), 'ICS Calendar');
						?></mark></p>
						<input type="hidden" name="step" value="1" />
						<p><input type="submit" class="button" value="<?php echo esc_attr(__('Check Again', 'ics-calendar-pro')); ?>" /></p>
						<?php
					}
					else {
						?>
						<div style="max-height: 70vh; overflow-y: auto;">
							<table class="wo-list-table widefat striped table-view-list posts">
								<thead>
									<tr>
										<th><?php esc_html_e('ID', 'ics-calendar-pro'); ?></th>
										<th><?php esc_html_e('Title', 'ics-calendar-pro'); ?></th>
										<th><?php esc_html_e('Post Type', 'ics-calendar-pro'); ?></th>
										<th><?php esc_html_e('Status', 'ics-calendar-pro'); ?></th>
									</tr>
								</thead>
								<tbody>
									<?php
									foreach ((array)$r34icspro_acf_blocks_affected_posts as $item) {
										?>
										<tr>
											<td>
												<input type="hidden" name="affected_posts[]" value="<?php echo intval($item->ID); ?>" />
												<?php echo intval($item->ID); ?>
											</td>
											<td><strong><a href="<?php echo esc_url(admin_url('post.php?post=' . intval($item->ID) . '&action=edit')); ?>" target="_blank"><?php echo wp_kses_post($item->post_title ?: '&hellip;'); ?></a></strong></td>
											<td><?php echo wp_kses_post($item->post_type); ?></td>
											<td><?php echo wp_kses_post($item->post_status); ?></td>
										</tr>
										<?php
									}
									?>
								</tbody>
							</table>
						</div>
						<input type="hidden" name="step" value="2" />
						<p><input type="submit" class="button button-primary" value="<?php

						echo esc_attr(sprintf(esc_html__('Convert %1$s Blocks', 'ics-calendar-pro'), 'ICS Calendar'));
						?>" onclick="if (!confirm('<?php echo esc_attr(__('This will make permanent changes and cannot be undone. Do you wish to continue?', 'ics-calendar-pro')); ?>')) { return false; }" /></p>
						<p><?php

						printf(esc_html__('This will make permanent changes to each of the pages/posts listed above. Each old %1$s Block found will be converted to a saved calendar in Calendar Builder, and the page/post will be edited to use the new block. (Note: This utility skips posts with the status of %2$s or %3$s. If the status of those pages/posts is changed, the utility may need to be run again.) %4$sPlease make sure no one is editing the pages/posts listed above before continuing.%5$s', 'ics-calendar-pro'), 'ICS Calendar', '<code>inherit</code>', '<code>trash</code>', '<strong style="color: var(--r34ics--color--ics-red);">', '</strong>');
						?></p>
						<?php
					}
				}
				
				else {
					?>
					<p><mark class="info"><?php

					printf(esc_html__('Converted %1$s block(s) with %2$s errors.', 'ics-calendar-pro'), count($r34icspro_acf_blocks_status['processed']), count($r34icspro_acf_blocks_status['errors']));
					?></mark></p>
					<?php
				}
				?>
				
			</form>
		
		</div>
		<?php
	}

});

/**
 * Load Utilities from free version
 * Includes r34ics_admin_utilities_more action
 * Also loads System Report
 */
include_once($R34ICSPro->r34ics_path . '/templates/admin/utilities.php');
