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

namespace WPForms\Forms\Fields\Addons\Coupon;

use WPForms\Forms\Fields\Traits\ProField as ProFieldTrait;
use WPForms_Field;

/**
 * Coupon Field class.
 *
 * @since 1.0.0
 */
class Field extends WPForms_Field {

	use ProFieldTrait;

	/**
	 * Whether the addon is active.
	 *
	 * @since 1.9.4
	 *
	 * @var bool
	 */
	private $is_addon_active = false;

	/**
	 * Define field type information.
	 *
	 * @since 1.9.4
	 */
	public function init() {

		// Define field type information.
		$this->name       = esc_html__( 'Coupon', 'wpforms-lite' );
		$this->keywords   = esc_html__( 'discount, sale', 'wpforms-lite' );
		$this->type       = 'payment-coupon';
		$this->icon       = 'fa-ticket';
		$this->order      = 100;
		$this->group      = 'payment';
		$this->addon_slug = 'coupons';

		$this->is_addon_active = function_exists( 'wpforms_' . $this->addon_slug );

		$this->init_pro_field();
		$this->hooks();
	}

	/**
	 * Define field hooks.
	 *
	 * @since 1.9.4
	 */
	protected function hooks() {

		add_filter( 'wpforms_field_new_display_duplicate_button', [ $this, 'field_display_duplicate_button' ], 20, 2 );
		add_filter( 'wpforms_field_preview_display_duplicate_button', [ $this, 'field_display_duplicate_button' ], 20, 2 );
	}

	/**
	 * Disallow field preview "Duplicate" button.
	 *
	 * @since 1.9.4
	 *
	 * @param bool|mixed $display Display switch.
	 * @param array      $field   Field settings.
	 *
	 * @return bool
	 */
	public function field_display_duplicate_button( $display, array $field ) {

		return $field['type'] === $this->type ? false : $display;
	}

	/**
	 * Define additional field options.
	 *
	 * @since 1.9.4
	 *
	 * @param array $field Field data and settings.
	 */
	public function field_options( $field ) {

		// Options open markup.
		$this->field_option(
			'basic-options',
			$field,
			[
				'markup'      => 'open',
				'after_title' => $this->get_field_options_notice(),
			]
		);

		$this->field_option( 'label', $field );

		$this->field_option( 'description', $field );

		$coupons      = [];
		$form_coupons = [];

		if ( $this->is_addon_active ) {
			$coupons      = wpforms_coupons()->get( 'repository' )->get_coupons(
				[
					'limit'  => -1,
					'fields' => 'id=>name',
				]
			);
			$form_coupons = wpforms_coupons()->get( 'repository' )->get_form_coupons( $this->get_form_id() );
		}

		$warning = sprintf(
			'<p class="wpforms-alert wpforms-alert-warning%1$s">%2$s</p>',
			empty( $form_coupons ) && empty( $this->is_disabled_field ) ? '' : ' wpforms-hidden',
			esc_html__( 'You haven\'t selected any coupons that can be used with this form. Please choose at least one coupon.', 'wpforms-lite' )
		);

		$coupons_field_label = $this->field_element(
			'label',
			$field,
			[
				'slug'    => 'allowed_coupons',
				'value'   => esc_html__( 'Allowed Coupons', 'wpforms-lite' ),
				'tooltip' => esc_html__( 'Choose coupons that can be used in the field.', 'wpforms-lite' ),
			],
			false
		);
		$coupons_field       = $this->get_allowed_coupons_field( $coupons, $form_coupons, $field );
		$allowed_forms_json  = sprintf(
			'<input type="hidden" name="fields[%1$s][allowed_coupons_json]" class="wpforms-coupons-allowed_coupons_json" value="%2$s">',
			$field['id'],
			wp_json_encode( $form_coupons )
		);

		$this->field_element(
			'row',
			$field,
			[
				'slug'    => 'allowed_coupons',
				'content' => $coupons_field_label . $coupons_field . $allowed_forms_json . $warning,
			]
		);

		$this->coupon_scope_options( $field );

		$this->field_option( 'required', $field );

		$this->field_option( 'basic-options', $field, [ 'markup' => 'close' ] );

		$this->field_option( 'advanced-options', $field, [ 'markup' => 'open' ] );

		$this->field_option( 'button_text', $field );

		$button_text_label = $this->field_element(
			'label',
			$field,
			[
				'slug'    => 'button_text',
				'value'   => esc_html__( 'Button Text', 'wpforms-lite' ),
				'tooltip' => esc_html__( 'Change button text.', 'wpforms-lite' ),
			],
			false
		);
		$button_text_field = $this->field_element(
			'text',
			$field,
			[
				'slug'  => 'button_text',
				'value' => isset( $field['button_text'] ) && ! wpforms_is_empty_string( $field['button_text'] )
					? $field['button_text']
					: esc_html__( 'Apply', 'wpforms-lite' ),
			],
			false
		);

		$this->field_element(
			'row',
			$field,
			[
				'slug'    => 'button_text',
				'content' => $button_text_label . $button_text_field,
			]
		);
		$this->field_option( 'css', $field );
		$this->field_option( 'label_hide', $field );
		$this->field_option( 'advanced-options', $field, [ 'markup' => 'close' ] );
	}

	/**
	 * Get allowed coupons' field.
	 *
	 * @since 1.9.4
	 *
	 * @param array $coupons      Coupons.
	 * @param array $form_coupons Form coupons.
	 * @param array $field        Field data.
	 *
	 * @return string
	 * @noinspection HtmlUnknownAttribute
	 */
	private function get_allowed_coupons_field( array $coupons, array $form_coupons, array $field ): string {

		$output = sprintf( '<select id="wpforms-field-option-%1$d-%2$s" name="fields[%1$d][%2$s]" multiple>', $field['id'], 'allowed_coupons' );

		foreach ( $coupons as $arg_key => $arg_option ) {
			$selected = selected( true, in_array( $arg_key, $form_coupons, true ), false );

			$output .= sprintf( '<option value="%s" %s>%s</option>', esc_attr( $arg_key ), $selected, $arg_option );
		}

		$output .= '</select>';

		return $output;
	}

	/**
	 * Add the "Coupons Discount" options: the scope mode and the payment fields it applies to.
	 *
	 * @since 2.0.2.2
	 *
	 * @param array $field Field data and settings.
	 */
	private function coupon_scope_options( array $field ) {

		// An older initialized addon cannot store the scope, so the options would be dead
		// controls. Without a working addon the rows render as the usual educational preview.
		if ( wpforms_is_addon_initialized( $this->addon_slug ) && ! method_exists( $this, 'sanitize_coupon_scopes' ) ) {
			return;
		}

		$scope        = $field['coupon_scopes']['default'] ?? [];
		$mode         = in_array( $scope['mode'] ?? '', [ 'except', 'only' ], true ) ? $scope['mode'] : 'all';
		$scope_fields = array_map( 'absint', (array) ( $scope['fields'] ?? [] ) );
		$options      = $this->get_scope_field_options();
		$field_labels = [
			'except' => esc_html__( 'Excluded Payment Fields', 'wpforms-lite' ),
			'only'   => esc_html__( 'Included Payment Fields', 'wpforms-lite' ),
		];

		$mode_label    = $this->field_element(
			'label',
			$field,
			[
				'slug'    => 'coupon_scope_mode',
				'value'   => esc_html__( 'Coupons Discount', 'wpforms-lite' ),
				'tooltip' => esc_html__( 'Choose which payment fields the applied coupons discount.', 'wpforms-lite' ),
			],
			false
		);
		$mode_select   = $this->field_element(
			'select',
			$field,
			[
				'slug'    => 'coupon_scope_mode',
				'value'   => $mode,
				'class'   => 'wpforms-coupons-coupon_scope_mode',
				'data'    => [
					'scope-target' => sprintf( 'wpforms-field-option-row-%d-coupon_scope_fields', $field['id'] ),
					'label-except' => $field_labels['except'],
					'label-only'   => $field_labels['only'],
				],
				'options' => [
					'all'    => esc_html__( 'All Payment Fields', 'wpforms-lite' ),
					'except' => esc_html__( 'All Except Selected Fields', 'wpforms-lite' ),
					'only'   => esc_html__( 'Only Selected Fields', 'wpforms-lite' ),
				],
			],
			false
		);
		$fields_label  = $this->field_element(
			'label',
			$field,
			[
				'slug'    => 'coupon_scope_fields',
				'value'   => $field_labels[ $mode ] ?? $field_labels['only'],
				'tooltip' => esc_html__( 'Choose the payment fields the selected option applies to.', 'wpforms-lite' ),
			],
			false
		);
		$fields_select = $this->field_element(
			'select-multiple',
			$field,
			[
				'slug'      => 'coupon_scope_fields',
				'value'     => $scope_fields,
				'options'   => $options,
				// Choices.js is initialized by the addon builder script, or by the
				// core disabled-fields fallback when no working addon is present.
				'choicesjs' => false,
				'class'     => 'wpforms-coupons-coupon_scope_fields',
			],
			false
		);

		$this->field_element(
			'row',
			$field,
			[
				'slug'    => 'coupon_scope_mode',
				'content' => $mode_label . $mode_select,
			]
		);

		$this->field_element(
			'row',
			$field,
			[
				'slug'    => 'coupon_scope_fields',
				'content' => $fields_label . $fields_select . $this->get_scope_alerts( $mode, $scope_fields, $options ),
				'class'   => $mode === 'all' ? 'wpforms-hidden' : '',
			]
		);
	}

	/**
	 * Get the warnings shown while a discount scope covers no payment field.
	 *
	 * The dead configuration differs per mode: an empty selection in the "only" mode,
	 * or every payment field selected in the "except" mode.
	 *
	 * @since 2.0.2.2
	 *
	 * @param string $mode         Discount scope mode.
	 * @param array  $scope_fields Payment field IDs the scope covers.
	 * @param array  $options      Discountable payment fields, keyed by field ID.
	 *
	 * @return string
	 */
	private function get_scope_alerts( string $mode, array $scope_fields, array $options ): string {

		$alert    = '<p class="wpforms-alert wpforms-alert-warning %1$s%2$s">%3$s</p>';
		$is_empty = $mode === 'only' && empty( $scope_fields );
		$is_all   = $mode === 'except' && $options && count( array_intersect( array_keys( $options ), $scope_fields ) ) === count( $options );

		return sprintf(
			$alert,
			'wpforms-coupons-scope-empty-alert',
			$is_empty ? '' : ' wpforms-hidden',
			esc_html__( 'You haven\'t selected any payment fields. Discount will not be applied until you choose at least one.', 'wpforms-lite' )
		) . sprintf(
			$alert,
			'wpforms-coupons-scope-all-alert',
			$is_all ? '' : ' wpforms-hidden',
			esc_html__( 'You\'ve selected every payment field, so the discount has nothing to apply to. Leave at least one field unselected.', 'wpforms-lite' )
		);
	}

	/**
	 * Get the form's discountable payment fields as select options, keyed by field ID.
	 *
	 * @since 2.0.2.2
	 *
	 * @return array
	 */
	private function get_scope_field_options(): array {

		// The coupon field itself carries no amount, so it is never discountable.
		$types  = array_diff( wpforms_payment_fields(), [ $this->type ] );
		$fields = wpforms_get_form_fields( $this->get_form_id(), $types );

		if ( ! is_array( $fields ) ) {
			return [];
		}

		$labels = [];

		foreach ( $fields as $payment_field ) {
			$field_id = absint( $payment_field['id'] );

			$labels[ $field_id ] = ! wpforms_is_empty_string( $payment_field['label'] ?? '' )
				? $payment_field['label']
				: sprintf( /* translators: %d - payment field ID. */ esc_html__( 'Field #%d', 'wpforms-lite' ), $field_id );
		}

		return $labels;
	}

	/**
	 * Field preview inside the builder.
	 *
	 * @since 1.9.4
	 *
	 * @param array $field Field data.
	 */
	public function field_preview( $field ) {

		// Label.
		$this->field_preview_option(
			'label',
			$field,
			[
				'label_badge' => $this->get_field_preview_badge(),
			]
		);

		$allowed_coupons = [];

		if ( $this->is_addon_active ) {
			$allowed_coupons = wpforms_coupons()->get( 'repository' )->get_form_coupons( $this->get_form_id() );
		}

		printf(
			'<div class="wpforms-field-payment-coupon-wrapper">
				<input type="text" class="wpforms-field-payment-coupon-input">
				<button type="button" aria-live="assertive" class="wpforms-field-payment-coupon-button">%1$s</button>
				<i class="fa fa-exclamation-triangle%2$s"></i>
			</div>',
			esc_html( $this->get_button_text( $field ) ),
			empty( $allowed_coupons ) && empty( $this->is_disabled_field ) ? '' : ' wpforms-hidden'
		);

		// Description.
		$this->field_preview_option( 'description', $field );

		// Hide remaining elements.
		$this->field_preview_option( 'hide-remaining', $field );
	}

	/**
	 * Get form ID. In AJAX requests the $form_id property doesn't exist.
	 *
	 * @since 1.9.4
	 *
	 * @return bool|int
	 */
	protected function get_form_id() {

		if ( $this->form_id ) {
			return $this->form_id;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Missing
		$this->form_id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : false;

		return $this->form_id;
	}

	/**
	 * Get the apply button text.
	 *
	 * @since 1.9.4
	 *
	 * @param array $field Field data.
	 *
	 * @return string
	 */
	protected function get_button_text( array $field ): string {

		return isset( $field['button_text'] ) && ! wpforms_is_empty_string( $field['button_text'] )
			? $field['button_text']
			: __( 'Apply', 'wpforms-lite' );
	}

	/**
	 * Field display on the frontend.
	 *
	 * @since 1.9.4
	 *
	 * @param array $field      Field data.
	 * @param array $deprecated Field attributes.
	 * @param array $form_data  Form data.
	 */
	public function field_display( $field, $deprecated, $form_data ) {
	}
}