<?php
/**
 * Integrations for cf7
 *
 * @package   PopupMaker
 * @copyright Copyright (c) 2024, Code Atlantic LLC
 */

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

/**
 * Class PUM_CF7_Integration
 */
class PUM_CF7_Integration {

	/**
	 * Initialize if CF7 is active.
	 */
	public static function init() {
		add_filter( 'pum_get_cookies', [ __CLASS__, 'register_cookies' ] );
		add_filter( 'wpcf7_editor_panels', [ __CLASS__, 'editor_panels' ] );
		add_action( 'wpcf7_after_save', [ __CLASS__, 'save' ] );
		add_filter( 'wpcf7_form_elements', [ __CLASS__, 'form_elements' ] );
		add_action( 'popmake_preload_popup', [ __CLASS__, 'preload' ] );
	}

	/**
	 * Check if the popups use CF7 Forms and force enqueue their assets.
	 *
	 * @param $popup_id
	 */
	public static function preload( $popup_id ) {
		$popup = pum_get_popup( $popup_id );

		if ( has_shortcode( $popup->post_content, 'contact-form-7' ) ) {
			if ( defined( 'WPCF7_LOAD_JS' ) && ! WPCF7_LOAD_JS ) {
				return;
			}

			if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
				wpcf7_enqueue_scripts();
			}

			if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
				wpcf7_enqueue_styles();
			}
		}
	}

	/**
	 * Append a hidden meta html element with the forms popup settings.
	 *
	 * @param $elements
	 *
	 * @return string
	 */
	public static function form_elements( $elements ) {
		$form = wpcf7_get_current_contact_form();

		$settings = wp_json_encode( self::form_options( $form->id() ) );

		return $elements . "<input type='hidden' class='wpcf7-pum' value='$settings' />";
	}

	/**
	 * Get a specific forms options.
	 *
	 * @param $id
	 *
	 * @return array
	 */
	public static function form_options( $id ) {
		$settings = get_option( 'cf7_pum_' . $id, self::defaults() );

		return wp_parse_args( $settings, self::defaults() );
	}

	/**
	 * Get default values.
	 *
	 * @return array
	 */
	public static function defaults() {
		return [
			'closepopup'   => false,
			'closedelay'   => 0,
			'openpopup'    => false,
			'openpopup_id' => 0,
		];
	}

	/**
	 * Registers new cookie events.
	 *
	 * @param array $cookies
	 *
	 * @return array
	 */
	public static function register_cookies( $cookies = [] ) {
		$cookies['cf7_form_success'] = [
			'labels' => [
				'name' => __( 'Contact Form 7 Success (deprecated. Use Form Submission instead.)', 'popup-maker' ),
			],
			'fields' => pum_get_cookie_fields(),
		];

		return $cookies;
	}

	/**
	 * Register new CF7 form editor tab.
	 *
	 * @param array $panels
	 *
	 * @return array
	 */
	public static function editor_panels( $panels = [] ) {
		return array_merge(
			$panels,
			[
				'popups' => [
					'title'    => __( 'Popup Settings', 'popup-maker' ),
					'callback' => [ __CLASS__, 'editor_panel' ],
				],
			]
		);
	}

	/**
	 * Render the popup tab.
	 *
	 * @param object $args
	 */
	public static function editor_panel( $args ) {

		$settings = self::form_options( $args->id() ); ?>
		<h2><?php esc_html_e( 'Popup Settings', 'popup-maker' ); ?></h2>
		<p class="description"><?php esc_html_e( 'These settings control popups after successful form submissions.', 'popup-maker' ); ?></p>
		<table class="form-table">
			<tbody>
			<tr>
				<th scope="row">
					<label for="wpcf7-pum-closepopup"><?php esc_html_e( 'Close Popup', 'popup-maker' ); ?></label>
				</th>
				<td>
					<input type="checkbox" id="wpcf7-pum-closepopup" name="wpcf7-pum[closepopup]" value="true" <?php checked( $settings['closepopup'], true ); ?> />
				</td>
			</tr>
			<tr id="wpcf7-pum-closedelay-wrapper">
				<th scope="row">
					<label for="wpcf7-pum-closedelay"><?php esc_html_e( 'Delay', 'popup-maker' ); ?></label>
				</th>
				<td>
					<?php
					if ( strlen( $settings['closedelay'] ) >= 3 ) {
						$settings['closedelay'] = $settings['closedelay'] / 1000;
					}
					?>

					<input type="number" id="wpcf7-pum-closedelay" min="0" step="1" name="wpcf7-pum[closedelay]" style="width: 100px;" value="<?php echo esc_attr( $settings['closedelay'] ); ?>" /><?php esc_html_e( 'seconds', 'popup-maker' ); ?>
				</td>
			</tr>
			<tr>
				<th scope="row">
					<label for="wpcf7-pum-openpopup"><?php esc_html_e( 'Open Popup', 'popup-maker' ); ?></label>
				</th>
				<td>
					<input type="checkbox" id="wpcf7-pum-openpopup" name="wpcf7-pum[openpopup]" value="true" <?php checked( $settings['openpopup'], true ); ?> />
				</td>
			</tr>
			<tr id="wpcf7-pum-openpopup_id-wrapper">
				<th scope="row">
					<label for="wpcf7-pum-openpopup_id"><?php esc_html_e( 'Popup', 'popup-maker' ); ?></label>
				</th>
				<td>
					<select id="wpcf7-pum-openpopup_id" name="wpcf7-pum[openpopup_id]">
						<?php foreach ( self::get_popup_list() as $option ) { ?>
							<option value="<?php echo esc_attr( $option['value'] ); ?>" <?php selected( $settings['openpopup_id'], $option['value'] ); ?>><?php echo esc_html( $option['label'] ); ?></option>
						<?php } ?>
					</select>
				</td>
			</tr>

			</tbody>
		</table>
		<script>
			(function ($) {
				var $open = $('#wpcf7-pum-openpopup'),
					$close = $('#wpcf7-pum-closepopup'),
					$popup_id_wrapper = $('#wpcf7-pum-openpopup_id-wrapper'),
					$delay_wrapper = $('#wpcf7-pum-closedelay-wrapper');

				function check_open() {
					if ($open.is(':checked')) {
						$popup_id_wrapper.show();
					} else {
						$popup_id_wrapper.hide();
					}
				}

				function check_close() {
					if ($close.is(':checked')) {
						$delay_wrapper.show();
					} else {
						$delay_wrapper.hide();
					}
				}

				check_open();
				check_close();

				$open.on('click', check_open);
				$close.on('click', check_close);
			}(jQuery));
		</script>
		<?php
	}

	/**
	 * Get a list of popups for a select box.
	 *
	 * @return array
	 */
	public static function get_popup_list() {
		$popup_list = [
			[
				'value' => 0,
				'label' => __( 'Select a popup', 'popup-maker' ),
			],
		];

		$popups = get_posts(
			[
				'post_type'      => 'popup',
				'post_status'    => [ 'publish' ],
				'posts_per_page' => - 1,
			]
		);

		foreach ( $popups as $popup ) {
			$popup_list[] = [
				'value' => $popup->ID,
				'label' => $popup->post_title,
			];
		}

		return $popup_list;
	}

	/**
	 * Save form popup options.
	 *
	 * @param $args
	 */
	public static function save( $args ) {
		// Ignored because this is part of nonced form submission.
		// phpcs:disable WordPress.Security.NonceVerification.Missing

		// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		$pum_settings = isset( $_POST['wpcf7-pum'] ) ? wp_unslash( $_POST['wpcf7-pum'] ) : [];
		if ( ! empty( $pum_settings ) ) {
			$settings = $pum_settings;

			// Sanitize values.
			$settings['openpopup']    = ! empty( $settings['openpopup'] );
			$settings['openpopup_id'] = ! empty( $settings['openpopup_id'] ) ? absint( $settings['openpopup_id'] ) : 0;
			$settings['closepopup']   = ! empty( $settings['closepopup'] );
			$settings['closedelay']   = ! empty( $settings['closedelay'] ) ? absint( $settings['closedelay'] ) : 0;

			update_option( 'cf7_pum_' . $args->id(), $settings );
		} else {
			delete_option( 'cf7_pum_' . $args->id() );
		}
	}
}
