<?php
if ( ! defined( 'ABSPATH' ) ) {
	die( 'You are not allowed to call this page directly.' );
}

class FormShieldQuformFormsIntegration extends FormShieldIntegration {

	public static $form_prefix = 'quf';

	/*
	 * This is where you will add filters and actions
	 */
	public function __construct() {
		add_filter( 'formshield_forms_list', array( __CLASS__, 'get_forms' ), 30, 1 );
		add_filter( 'quform_post_validate', array( __CLASS__, 'validate_forms' ), 10, 2 );
		add_action( 'formshield_autoprotect_forms', array( __CLASS__, 'enable_forms' ), 30, 1 );
	}

	/**
	 * Get Formidable Forms
	 * This should return the forms list
	 *
	 * @return Array $forms_list
	 */
	public static function get_forms( $forms_list ) {
		if ( is_callable( 'Quform_Repository::getForms' ) ) {
			$additional_forms     = array();
			$form_list = (new Quform_Repository)->getForms();
			if ( is_array( $form_list ) ) {
				foreach ( $form_list as $form ) {
					// Quform getForms() returns an array of arrays, not objects.
					$fid               = self::get_form_id( $form['id'] );
					$form_name         = isset( $form['name'] ) ? $form['name'] : '';
					$submissions_count = isset( $form['entries'] ) ? (int) $form['entries'] : 0;
					$updated_at        = isset( $form['updated_at'] ) ? $form['updated_at'] : null;
					$additional_forms[] = array(
						'id'          => $fid,
						'enabled'     => FormShieldSettings::is_form_enabled( $fid ),
						'name'        => $form_name,
						'type'        => 'Quform',
						'location'    => 'Quform: ' . $form_name . ' (ID: ' . $form['id'] . ')',
						'submissions' => FormShieldForms::get_form_submissions_ct( $fid, $submissions_count ),
						'activity'    => FormShieldForms::get_latest_form_activity( $fid, $updated_at ),
					);
				}
			}
			self::prune_disabled_forms( $additional_forms );
			$forms_list = array_merge( $forms_list, $additional_forms );
		}

		return $forms_list;
	}

	/**
	 * Validate Formidable forms
	 *
	 * @param Array $result - Array of error messages
	 * @param Object $form - Array of form value strings submitted
	 *
	 * @return Array - $result
	 */
	public static function validate_forms( $result, $form ) {
		// If HP protection enabled, validate the form
		$options = form_shield()->get_options();
		$fid     = self::get_form_id( absint( $form->getId() ) );
		if ( is_array( $options ) && array_key_exists( 'formshield_forms', $options ) && isset( $options['formshield_forms'][ $fid ] ) ) {
			$formshield_form_enabled = $options['formshield_forms'][ $fid ]['form_enabled'];
			if ( $formshield_form_enabled ) {
				// Check honeypot first.
				$honeypot_value = formshield_get_post_string( 'formshield_honeypot', '' );
				if ( ! empty( $honeypot_value ) ) {
					FormShieldForms::handle_suspicious_form_activity( $fid );
					$result['type']  = 'error';
					$result['error'] = array( 'enabled' => true, 'content' => FormShieldForms::fail_form_error_message() );
				} else {
					$validation_failed = FormShieldForms::fail_form_validation();
					if ( $validation_failed ) {
						FormShieldForms::handle_suspicious_form_activity( $fid );
						$result['type']  = 'error';
						$result['error'] = array( 'enabled' => true, 'content' => FormShieldForms::fail_form_error_message() );
					}
				}
			}
		}

		return $result;
	}

	/**
	 * Protect Existing Formidable forms
	 */
	public static function enable_forms( $unattended ) {
		if ( is_callable( 'Quform_Repository::getForms' ) ) {
			$form_list = (new Quform_Repository)->getForms();
			if ( is_array( $form_list ) ) {
				foreach ( $form_list as $form ) {
					// Assign unique form id
					$fid = self::get_form_id( $form['id'] );
					FormShieldForms::enable_forms( $fid, 1, $unattended );
				}
			}
		}
	}
}

add_action( 'plugins_loaded', function() { new FormShieldQuformFormsIntegration(); });
