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 | Addons.php
  • File: Addons.php
  • Full Path: /home/mata8712/public_html/on-me-trouve/wp-content/plugins/wpforms-lite/src/Admin/Builder/Addons.php
  • File size: 10.16 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace WPForms\Admin\Builder;

use WPForms\Requirements\Requirements;

/**
 * Addons class.
 *
 * @since 1.9.2
 */
class Addons {

	/**
	 * List of addon options.
	 *
	 * @since 1.9.2
	 */
	private const FIELD_OPTIONS = [
		'calculations'  => [
			'calculation_code',
			'calculation_code_js',
			'calculation_code_php',
			'calculation_is_enabled',
		],
		'form-locker'   => [
			'unique_answer',
		],
		'geolocation'   => [
			'display_map',
			'enable_address_autocomplete',
			'map_position',
		],
		'surveys-polls' => [
			'survey',
		],
		'quiz'          => [
			'quiz_enabled',
			'choices' => [
				'quiz_personality',
				'quiz_weight',
			],
		],
	];

	/**
	 * Initialize.
	 *
	 * @since 1.9.2
	 *
	 * @noinspection ReturnTypeCanBeDeclaredInspection
	 */
	public function init() {

		$this->hooks();
	}

	/**
	 * Add hooks.
	 *
	 * @since 1.9.2
	 */
	private function hooks(): void {

		add_filter( 'wpforms_save_form_args', [ $this, 'save_disabled_addons_options' ], 10, 3 );
	}


	/**
	 * Field's options added by an addon can be deleted when the addon is deactivated or have incompatible status.
	 * The options are fully controlled by the addon when addon is active and compatible.
	 *
	 * @since 1.9.2
	 *
	 * @param array|mixed $post_data Post data.
	 *
	 * @return array
	 */
	public function save_disabled_addons_options( $post_data ): array {

		$post_data = (array) $post_data;
		$form_obj  = wpforms()->obj( 'form' );
		$form_data = json_decode( stripslashes( $post_data['post_content'] ?? '' ), true );
		$form_id   = $form_data['id'] ?? '';

		if ( ! $form_obj || ! $form_id ) {
			return $post_data;
		}

		$previous_form_data   = $form_obj->get( $form_id, [ 'content_only' => true ] );
		$not_validated_addons = Requirements::get_instance()->get_not_validated_addons();

		if ( empty( $previous_form_data ) || empty( $not_validated_addons ) ) {
			return $post_data;
		}

		foreach ( $not_validated_addons as $path ) {
			$slug = str_replace( 'wpforms-', '', basename( $path, '.php' ) );

			$this->preserve_addon( $slug, $form_data, $previous_form_data );
		}

		$this->preserve_providers( $form_data, $previous_form_data );
		$this->preserve_payments( $form_data, $previous_form_data );

		$post_data['post_content'] = wpforms_encode( $form_data );

		return $post_data;
	}

	/**
	 * Preserve addon fields, settings, panels, notifications, etc.
	 *
	 * @since 1.9.3
	 *
	 * @param string $slug               Addon slug.
	 * @param array  $form_data          Form data.
	 * @param array  $previous_form_data Previous form data.
	 *
	 * @return void
	 */
	private function preserve_addon( string $slug, array &$form_data, array $previous_form_data ): void {

		if ( ! empty( $form_data['fields'] ) && ! empty( $previous_form_data['fields'] ) ) {
			$this->preserve_addon_fields_settings( $slug, $form_data['fields'], $previous_form_data['fields'] );
		}

		$this->preserve_addon_panel( $slug, $form_data, $previous_form_data );

		if ( ! empty( $form_data['settings'] ) && ! empty( $previous_form_data['settings'] ) ) {
			$this->preserve_addon_settings( $slug, $form_data['settings'], $previous_form_data['settings'] );
		}

		if ( ! empty( $form_data['settings']['notifications'] ) && ! empty( $previous_form_data['settings']['notifications'] ) ) {
			$this->preserve_addon_notifications(
				$slug,
				$form_data['settings']['notifications'],
				$previous_form_data['settings']['notifications']
			);
		}
	}

	/**
	 * Preserve addon fields.
	 *
	 * @since 1.9.5
	 *
	 * @param string $slug            Addon slug.
	 * @param array  $new_fields      Form fields settings.
	 * @param array  $previous_fields Previous form fields settings.
	 *
	 * @return void
	 */
	private function preserve_addon_fields_settings( string $slug, array &$new_fields, array $previous_fields ): void {

		foreach ( $previous_fields as $field_id => $previous_field ) {
			$new_field = $new_fields[ $field_id ] ?? [];

			if ( empty( $new_field ) ) {
				continue;
			}

			$this->preserve_addon_field_settings( $slug, $new_field, $previous_field );

			$new_fields[ $field_id ] = $new_field;
		}
	}

	/**
	 * Preserve addon field.
	 *
	 * @since 1.9.5
	 *
	 * @param string $slug           Addon slug.
	 * @param array  $new_field      Previous form fields settings.
	 * @param array  $previous_field Form fields settings.
	 *
	 * @return void
	 */
	private function preserve_addon_field_settings( string $slug, array &$new_field, array $previous_field ): void {

		$prefix           = $this->prepare_prefix( $slug );
		$changed_settings = array_diff_key( $previous_field, $new_field );
		$preserve_fields  = self::FIELD_OPTIONS[ $slug ] ?? [];

		foreach ( $changed_settings as $setting_name => $setting_value ) {
			if (
				strpos( $setting_name, $prefix ) === 0 ||
				in_array( $setting_name, $preserve_fields, true )
			) {
				$new_field[ $setting_name ] = $setting_value;
			}
		}

		if (
			! empty( $preserve_fields['choices'] ) &&
			is_array( $preserve_fields['choices'] ) &&
			! empty( $new_field['choices'] ) &&
			is_array( $new_field['choices'] )
		) {
			$this->preserve_addon_field_choices_settings( $preserve_fields['choices'], $new_field, $previous_field );
		}
	}

	/**
	 * Preserve addon field choices settings.
	 *
	 * @since 1.9.9
	 *
	 * @param array $choice_settings Choice settings.
	 * @param array $new_field       Previous form fields settings.
	 * @param array $previous_field  Form fields settings.
	 *
	 * @return void
	 */
	private function preserve_addon_field_choices_settings( array $choice_settings, array &$new_field, array $previous_field ): void {

		if ( ! isset( $previous_field['choices'] ) || ! is_array( $previous_field['choices'] ) ) {
			return;
		}

		$previous_choices = $previous_field['choices'];

		foreach ( $new_field['choices'] as $choice_id => $choice ) {
			foreach ( $choice_settings as $setting_name ) {
				if ( isset( $previous_choices[ $choice_id ][ $setting_name ] ) ) {
					$new_field['choices'][ $choice_id ][ $setting_name ] = $previous_choices[ $choice_id ][ $setting_name ];
				}
			}
		}
	}

	/**
	 * Preserve addon panel.
	 *
	 * @since 1.9.3
	 *
	 * @param string $slug               Addon slug.
	 * @param array  $form_data          Form data.
	 * @param array  $previous_form_data Previous form data.
	 */
	private function preserve_addon_panel( string $slug, array &$form_data, array $previous_form_data ): void {

		$panel = $this->prepare_prefix( $slug );

		// The addon settings stored its own panel, e.g., $form_data[lead_forms], $form_data[webhooks], etc.
		if ( ! empty( $previous_form_data[ $panel ] ) ) {
			$form_data[ $panel ] = $previous_form_data[ $panel ];
		}
	}

	/**
	 * Preserve addon settings stored inside the settings panel with a specific prefix.
	 * e.g. $form_data[settings][{$prefix}_enabled], $form_data[settings][{$prefix}_email], etc.
	 *
	 * @since 1.9.4
	 *
	 * @param string $slug              Addon option prefix.
	 * @param array  $new_settings      Form settings.
	 * @param array  $previous_settings Previous form settings.
	 */
	private function preserve_addon_settings( string $slug, array &$new_settings, array $previous_settings ): void {

		$prefix = $this->prepare_prefix( $slug );

		static $legacy_options = [
			'offline_forms'     => [ 'offline_form' ],
			'user_registration' => [ 'user_login_hide', 'user_reset_hide' ],
			'surveys_polls'     => [ 'survey_enable', 'poll_enable' ],
		];

		// BC: User Registration addon has `registration_` prefix instead of `user_registration`.
		if ( $prefix === 'user_registration' ) {
			$prefix = 'registration';
		}

		foreach ( $previous_settings as $setting_name => $value ) {
			if ( strpos( $setting_name, $prefix ) === 0 ) {
				$new_settings[ $setting_name ] = $value;

				continue;
			}

			// BC: The options don't have a prefix and hard-coded in the `$legacy_options` variable.
			if ( isset( $legacy_options[ $prefix ] ) && in_array( $setting_name, $legacy_options[ $prefix ], true ) ) {
				$new_settings[ $setting_name ] = $value;
			}
		}
	}

	/**
	 * Preserve addon notifications.
	 *
	 * @since 1.9.4
	 *
	 * @param string $slug                   Addon slug.
	 * @param array  $new_notifications      List of form notifications.
	 * @param array  $previous_notifications Previously saved list of form notifications.
	 *
	 * @return void
	 */
	private function preserve_addon_notifications( string $slug, array &$new_notifications, array $previous_notifications ): void {

		$prefix = $this->prepare_prefix( $slug );

		foreach ( $previous_notifications as $notification_id => $notification_settings ) {
			if ( empty( $new_notifications[ $notification_id ] ) ) {
				continue;
			}

			$changed_notification_settings = array_diff_key( $notification_settings, $new_notifications[ $notification_id ] );

			foreach ( $changed_notification_settings as $setting_name => $value ) {
				if ( strpos( $setting_name, $prefix ) === 0 ) {
					$new_notifications[ $notification_id ][ $setting_name ] = $value;
				}
			}
		}
	}

	/**
	 * Preserve Providers that are not active.
	 *
	 * @since 1.9.4
	 *
	 * @param array $form_data          Form data.
	 * @param array $previous_form_data Previous form data.
	 */
	private function preserve_providers( array &$form_data, array $previous_form_data ): void {

		if ( empty( $previous_form_data['providers'] ) ) {
			return;
		}

		$active_providers = wpforms_get_providers_available();

		foreach ( $previous_form_data['providers'] as $slug => $provider ) {
			if ( ! empty( $active_providers[ $slug ] ) ) {
				continue;
			}

			$form_data['providers'][ $slug ] = $provider;
		}
	}

	/**
	 * Preserve Payments providers that are not active.
	 *
	 * @since 1.9.4
	 *
	 * @param array $form_data          Form data.
	 * @param array $previous_form_data Previous form data.
	 */
	private function preserve_payments( array &$form_data, array $previous_form_data ): void {

		if ( empty( $previous_form_data['payments'] ) ) {
			return;
		}

		foreach ( $previous_form_data['payments'] as $slug => $value ) {
			if ( ! empty( $form_data['payments'][ $slug ] ) ) {
				continue;
			}

			$form_data['payments'][ $slug ] = $value;
		}
	}

	/**
	 * Convert slug to a addon prefix.
	 *
	 * @since 1.9.4
	 *
	 * @param string $slug Addon slug.
	 *
	 * @return string
	 */
	private function prepare_prefix( string $slug ): string {

		return str_replace( '-', '_', $slug );
	}
}