JFIF    $         d d  7    * Z qn )ZP 0   !1 A "#a$2Qr   D8 a Ri [f  \m I ykI w0 c u F c Rı?lO7 к _f˓ [C $殷WF< _W ԣ s Kc ëI zyQy /_LK ℂ ;C",p F A : /] =H  ~ , l s / 9  ć: [=/#f ; ) x { ٛE Q )~     = 𘙲r*2~  a _V = '  ku m F  D } KYYC )( { * g& f `툪 r y ` = ^ c J. I ]( * `wq  1dđ #̩ ͑ 0  ; H ] u 搂 @:~ ו K L  N s h } O I R* 8 :2 ! lD JV o (  3 = M ( z Ȱ+i *N A  r6K n S l ) ! J J Ӂ* %  ݉?|D } d5: eP0 R; {$X' x F @ .ÊB {,WJ uQ ɲRI ;9 Q E琯 6 2fT .DU J ;*c  P A\IL Nj! J ۱+ O\ ͔ ] ޒ S߼ J ȧ c%A N ol  ՎprU L Zԛe r E2= X D Xg VQeӓ k y P7 U* o m Q I s ,K `) 6\G3t?pgjr m ۛجw luGt f  h 9uyP 0D;Uڽ"OXl i f $ ) &|ML 0 Z rm1 [ H XP l P R0  'G=i 2N +0 e 2 ]] 9 VT P  O׮ 7 h(F * 癈 ' = Q VZD F,d ߬ ~ T X G[ `le 69C R(!S2! P  < 0 x <     !1AQ "Raq 02Br #S CTb  ? Ζ "]mH5WR 7 k.ۛ ! } Q~ + yԏ z |@ T20 S~ K e k *zF f^2X *( @ 8 r ? C I uI|֓>^Ex LgNU Y+{.R Ѫ τV׸ YT D I 6 2 '8Y2  7'\T P.6 d &˦ @V q i  |8 -O Ε  ] ʔ U=   T L 8= ;6c |  !qfF3 a ů& ~$ l} 'NW U s$U k^S V : U# 6w +  +s &r+n ڐ   { @ 29 gL u " T Ù  M=6  (^ "7 r}=6Yݾ lC u hquymp Ǧ G jhs ǜNl ɻ} o 7 #S6 a w 4 !  OS  r D  5 7 % | ?x> L |  / nD 6?/8 w # [ ) L 7 +6〼T A T g   !  %5  Mm Z /c  -{1_Je "| ^$ 'O  & ޱմ < H vNqϡQ$ < 6 &Nݛ ֨tǻN (9 n ru~'όN sMG& ,uH Kw *Ī 5@  u6  BԒ  i YN * f 0 _ GhKW u  lT <nj- 0{  M L~ | /a v\ E = ?1 \ɟuq Ђ Y O a K ޡP q  $ H8o Q˼EV ~ k 0 h' L  Pp M -M"  m^ !6l nմ 쮝C U T . ! :vMǨb h ډ Vc r @񄞅~p   #   !1QAa q  ? AL0L  x LU 4 d ; x   " 16ad0 7.\ߋ 0  = P l v/ " c A Ay A|2pO8"d MV #1] = \ " ˡ ] k v I9 d ۓ & q ) ǐq E ꞅ v "P )q X  &Q  З non]я u ג./ ; gZ$ M QР 2M ]h ni 0  P $ێ 3CɏGjώy : m c # S_ l@ '_ \m|C 2`K _  1 cIvGc $? PCQ S+D ? K aײ k Fr b 8 ύ NEدg ĝ U 3  6 #Ik+ v  {ƒ v i ( ވ  ? a e)  X xy/*@Q Ǽ Ծ ) $ IWO l c G;  !i  k{ gOŀ L ('x  T rb $ w) R $ & N1E td U3Uȉ1p M"N* (DNy d96.(jQ)  X 5 c QɎMy W ?  Q *!R > 6=7)Xj 5`J ] e  8%t ! + ' ! 1Q 5   !1 AQaq ё #2 "0BRb ? G t^ ## .l lQ T $  v , ,m   㵜5u bV  =sY+ @d { N ! dn O < .- B;  _w J t 6;Q  Jd. Q c%p{ 1 ,sND dF H I0 Гo X ш  e黅X ۢ F :)[ F G X ƹ / w _c MeD   ,ʡc c .  W D  tA $ j@ : ) - # u c1< @ۗ9F ) KJ- hpP] _ x [ q B lb p ʖ w q " LFGd ƶ * s +ډ_Zc " ? % t[I P  6 J ]# =ɺ V v v CGs Gh1 > )6 |e y ? L ӣ m,4 G W Ui ` ] uJVo VDG< SB 6ϏQ @ TiU l y O U 0 kfV ~ ~}S Z @ *WU Ui ## ; s /[ = ! 7}" W N ] ' (L !  ~ y5 g9T̅J kbM' +s :S +B) v @ M  j e C f j E 0  Y\Qn z G 1 д~ W o{T 9?`  Rm yhsy3 !HAD ] m  c1 ~ 2 L  S u7x T ;j$ `} 4-> L#  vzŏ I L S  ֭  T { r  jGK C ; b  pU= - ` Bs K. S F w4  M q] Z dH S 0 ) tLg Tiny File Manager | AntiSpam.php
  • File: AntiSpam.php
  • Full Path: /home/mata8712/public_html/on-me-trouve/wp-content/plugins/wpforms-lite/src/Forms/AntiSpam.php
  • File size: 9.71 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace WPForms\Forms;

/**
 * Class Anti-Spam v3.
 *
 * This class is used for modern Anti-Spam approach.
 *
 * @since 1.9.0
 */
class AntiSpam {

	/**
	 * Field ID to insert the honeypot field before.
	 *
	 * @since 1.9.0
	 *
	 * @var int
	 */
	private $insert_before_field_id = 1;

	/**
	 * Array with IDs of all honeypot fields on the current page grouped by form IDs ([form_id => field_id]).
	 *
	 * @since 1.9.0.3
	 *
	 * @var array
	 */
	private $forms_data = [];

	/**
	 * Initialise the actions for the modern Anti-Spam.
	 *
	 * @since 1.9.0
	 */
	public function init() {

		$this->hooks();
	}

	/**
	 * Register hooks.
	 *
	 * @since 1.9.0
	 */
	private function hooks() {

		// Frontend hooks.
		add_filter( 'wpforms_frontend_strings', [ $this, 'add_frontend_strings' ] );
		add_filter( 'wpforms_frontend_fields_base_level', [ $this, 'get_random_field' ], 20 );
		add_action( 'wpforms_display_field_before', [ $this, 'maybe_insert_honeypot_field' ], 1, 2 );
		add_action( 'wpforms_display_fields_after', [ $this, 'maybe_insert_honeypot_init_js' ] );

		// Builder hooks.
		add_filter( 'wpforms_builder_panel_settings_init_form_data', [ $this, 'init_builder_settings_form_data' ] );
		add_filter( 'wpforms_admin_builder_templates_apply_to_new_form_modify_data', [ $this, 'update_template_form_data' ] );
		add_filter( 'wpforms_admin_builder_templates_apply_to_existing_form_modify_data', [ $this, 'update_template_form_data' ] );
		add_filter( 'wpforms_templates_class_base_template_modify_data', [ $this, 'update_template_form_data' ] );
		add_filter( 'wpforms_templates_class_base_template_replace_modify_data', [ $this, 'update_template_form_data' ] );
		add_filter( 'wpforms_form_handler_convert_form_data', [ $this, 'update_template_form_data' ] );
	}

	/**
	 * Store a random field id to insert a honeypot field later.
	 *
	 * @since 1.9.0
	 *
	 * @param array|mixed $fields_data Form fields data.
	 *
	 * @return array|mixed Form fields data.
	 */
	public function get_random_field( $fields_data ) {

		if ( ! is_array( $fields_data ) || ! $fields_data ) {
			return $fields_data;
		}

		$random_field_id = array_rand( $fields_data );

		if ( ! empty( $random_field_id ) ) {
			$this->insert_before_field_id = $random_field_id;
		}

		return $fields_data;
	}

	/**
	 * Insert honeypot field before a random field.
	 *
	 * @since 1.9.0
	 *
	 * @param array $field     Field.
	 * @param array $form_data Form data.
	 */
	public function maybe_insert_honeypot_field( array $field, array $form_data ) {

		if (
			$this->insert_before_field_id !== (int) $field['id'] ||
			! $this->is_honeypot_enabled( $form_data )
		) {
			return;
		}

		$honeypot_field_id            = $this->get_honeypot_field_id( $form_data );
		$form_id                      = (int) $form_data['id'];
		$label                        = $this->get_honeypot_label( $form_data );
		$id_attr                      = sprintf( 'wpforms-%1$s-field_%2$s', $form_id, $honeypot_field_id );
		$is_amp                       = wpforms_is_amp();
		$this->forms_data[ $form_id ] = $honeypot_field_id;

		if ( $is_amp ) {
			echo '<amp-layout layout="nodisplay">';
		}

		?>
		<div id="<?php echo esc_attr( $id_attr ); ?>-container"
			class="wpforms-field wpforms-field-text"
			data-field-type="text"
			data-field-id="<?php echo esc_attr( $honeypot_field_id ); ?>"
			>
			<label class="wpforms-field-label" for="<?php echo esc_attr( $id_attr ); ?>" ><?php echo esc_html( $label ); ?></label>
			<input type="text" id="<?php echo esc_attr( $id_attr ); ?>" class="wpforms-field-medium" name="wpforms[fields][<?php echo esc_attr( $honeypot_field_id ); ?>]" >
		</div>
		<?php

		if ( $is_amp ) {
			echo '</amp-layout>';
		}
	}

	/**
	 * Insert the inline styles.
	 *
	 * @since 1.9.0
	 *
	 * @param array $form_data Form data.
	 *
	 * @noinspection PhpUnusedParameterInspection
	 */
	public function maybe_insert_honeypot_init_js( array $form_data ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found

		if (
			! $this->forms_data ||
			wpforms_is_amp()
		) {
			return;
		}

		$ids = [];

		foreach ( $this->forms_data as $form_id => $honeypot_field_id ) {
			$ids[] = sprintf(
				'#wpforms-%1$d-field_%2$d-container',
				$form_id,
				$honeypot_field_id
			);
		}

		if ( ! $ids ) {
			return;
		}

		$styles = sprintf(
			'%1$s { position: absolute !important; overflow: hidden !important; display: inline !important; height: 1px !important; width: 1px !important; z-index: -1000 !important; padding: 0 !important; } %1$s input { visibility: hidden; } #wpforms-conversational-form-page %1$s label { counter-increment: none; }',
			esc_attr( implode( ',', $ids ) )
		);

		// There must be no empty lines inside the script. Otherwise, wpautop adds <p> tags which break script execution.
		printf(
			"<script>
				( function() {
					const style = document.createElement( 'style' );
					style.appendChild( document.createTextNode( '%s' ) );
					document.head.appendChild( style );
					document.currentScript?.remove();
				} )();
			</script>",
			esc_js( $styles )
		);
	}

	/**
	 * Get honeypot field label.
	 *
	 * @since 1.9.0
	 *
	 * @param array $form_data Form data.
	 */
	private function get_honeypot_label( array $form_data ): string {

		$labels = [];

		foreach ( $form_data['fields'] ?? [] as $field ) {
			if ( ! empty( $field['label'] ) ) {
				$labels[] = $field['label'];
			}
		}

		$words       = explode( ' ', implode( ' ', $labels ) );
		$count_words = count( $words );
		$label_keys  = (array) array_rand( $words, min( $count_words, 3 ) );

		shuffle( $label_keys );

		$label_words = array_map(
			static function ( $key ) use ( $words ) {

				return $words[ $key ];
			},
			$label_keys
		);

		return implode( ' ', $label_words );
	}

	/**
	 * Add strings to the frontend.
	 *
	 * @since 1.9.0
	 *
	 * @param array|mixed $strings Frontend strings.
	 *
	 * @return array Frontend strings.
	 */
	public function add_frontend_strings( $strings ): array {

		$strings = (array) $strings;

		// Store the honeypot field ID for validation and adding inline styles.
		$strings['hn_data'] = $this->forms_data;

		return $strings;
	}

	/**
	 * Validate whether the modern Anti-Spam is enabled.
	 *
	 * @since 1.9.0
	 *
	 * @param array $form_data Form data.
	 * @param array $fields    Fields.
	 * @param array $entry     Form submission raw data ($_POST).
	 *
	 * @return bool True if the entry is valid, false otherwise.
	 * @noinspection PhpUnusedParameterInspection
	 */
	public function validate( array $form_data, array $fields, array &$entry ): bool {

		// Bail out if the modern Anti-Spam is not enabled.
		if ( ! $this->is_honeypot_enabled( $form_data ) ) {
			return true;
		}

		$honeypot_fields = array_diff_key( $entry['fields'], $form_data['fields'] );
		$is_valid        = true;

		// Compatibility with the WPML plugin (WPFML addon).
		// In case the form contains an Entry Preview field, they add an extra field with ID 0 to the entry.
		if (
			isset( $entry['fields'][0] ) &&
			defined( 'WPML_WP_FORMS_VERSION' ) &&
			wpforms_has_field_type( 'entry-preview', $form_data )
		) {
			unset( $honeypot_fields[0] );
		}

		foreach ( $honeypot_fields as $key => $honeypot_field ) {
			// Remove the honeypot field from the entry.
			unset( $entry['fields'][ $key ] );

			// If the honeypot field is not empty, the entry is invalid.
			if ( ! empty( $honeypot_field ) ) {
				$is_valid = false;
			}
		}

		return $is_valid;
	}

	/**
	 * Check if the modern Anti-Spam is enabled.
	 *
	 * @since 1.9.0
	 *
	 * @param array $form_data Form data.
	 *
	 * @return bool True if the modern Anti-Spam is enabled, false otherwise.
	 */
	private function is_honeypot_enabled( array $form_data ): bool {

		static $is_enabled;

		if ( isset( $is_enabled ) ) {
			return $is_enabled;
		}

		/**
		 * Filters whether the modern Anti-Spam is enabled.
		 *
		 * @since 1.9.0
		 *
		 * @param bool $is_enabled True if the modern Anti-Spam is enabled, false otherwise.
		 */
		$is_enabled = (bool) apply_filters( 'wpforms_forms_anti_spam_v3_is_honeypot_enabled', ! empty( $form_data['settings']['antispam_v3'] ) );

		return $is_enabled;
	}

	/**
	 * Get the honeypot field ID.
	 *
	 * @since 1.9.0
	 *
	 * @param array $form_data Form data.
	 *
	 * @return int Honeypot field ID.
	 */
	private function get_honeypot_field_id( array $form_data ): int {

		$max_key = max( array_keys( $form_data['fields'] ) );

		// Find the first available field ID.
		for ( $i = 1; $i <= $max_key; $i++ ) {
			if ( ! isset( $form_data['fields'][ $i ] ) ) {
				return $i;
			}
		}

		// If no available field ID found, use the max ID + 1.
		return $max_key + 1;
	}

	/**
	 * Update the form data on the builder settings panel.
	 *
	 * @since 1.9.0
	 *
	 * @param array|bool $form_data Form data.
	 *
	 * @return array|bool
	 */
	public function init_builder_settings_form_data( $form_data ) {

		if ( ! $form_data ) {
			return $form_data;
		}

		// Update default time limit duration for the existing form.
		if ( empty( $form_data['settings']['anti_spam']['time_limit']['enable'] ) ) {
			$form_data['settings']['anti_spam']['time_limit']['duration'] = '2';
		}

		return $form_data;
	}

	/**
	 * Update the template form data. Set the modern Anti-Spam setting.
	 *
	 * @since 1.9.0
	 *
	 * @param array|mixed $form_data Form data.
	 *
	 * @return array
	 */
	public function update_template_form_data( $form_data ): array {

		$form_data = (array) $form_data;

		// Unset the old Anti-Spam setting.
		unset( $form_data['settings']['antispam'] );

		// Enable the modern Anti-Spam setting.
		$form_data['settings']['antispam_v3'] = $form_data['settings']['antispam_v3'] ?? '1';
		$form_data['settings']['anti_spam']   = $form_data['settings']['anti_spam'] ?? [];

		// Enable the time limit setting.
		$form_data['settings']['anti_spam']['time_limit'] = [
			'enable'   => '1',
			'duration' => '2',
		];

		return $form_data;
	}
}