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

namespace WPForms\Admin\Builder\Settings;

use WPForms_Builder_Panel_Settings;

/**
 * QR Code setting in the General section of the Form Builder Settings panel.
 *
 * @since 2.0.1
 */
class QrCode {

	/**
	 * Bundled qr-code-styling library version.
	 *
	 * @since 2.0.1
	 */
	private const LIBRARY_VERSION = '1.9.2';

	/**
	 * Allowed destination types.
	 *
	 * @since 2.0.1
	 */
	private const DESTINATIONS = [ 'none', 'page', 'url' ];

	/**
	 * Allowed logo choices.
	 *
	 * Also read by usage tracking to whitelist per-form logo counters.
	 *
	 * @since 2.0.1
	 */
	public const LOGOS = [ 'none', 'wpforms', 'custom' ];

	/**
	 * License types unlocking the logo control.
	 *
	 * @since 2.0.1
	 */
	private const LOGO_LICENSES = [ 'pro', 'elite', 'agency', 'ultimate' ];

	/**
	 * Option holding the Pro logo upsell funnel counters.
	 *
	 * Written by the education class and read by usage tracking.
	 *
	 * @since 2.0.1
	 */
	public const LOGO_UPSELL_EVENTS_OPTION = 'wpforms_qr_code_logo_upsell_events';

	/**
	 * Pro logo upsell funnel event names.
	 *
	 * @since 2.0.1
	 */
	public const LOGO_UPSELL_EVENTS = [ 'shown', 'dismissed', 'upgraded' ];

	/**
	 * Initialize class.
	 *
	 * @since 2.0.1
	 */
	public function init(): void {

		$this->hooks();
	}

	/**
	 * Register hooks.
	 *
	 * @since 2.0.1
	 */
	private function hooks(): void {

		add_action( 'wpforms_form_settings_general_main_after', [ $this, 'render' ] );
		add_filter( 'wpforms_save_form_args', [ $this, 'sanitize_settings' ] );
		add_filter( 'wpforms_builder_strings', [ $this, 'builder_strings' ] );
	}

	/**
	 * Add the QR Code strings to the builder localized data.
	 *
	 * @since 2.0.1
	 *
	 * @param array $strings Builder strings.
	 *
	 * @return array
	 */
	public function builder_strings( $strings ): array {

		$strings = (array) $strings;

		$strings['qr_code'] = [
			'file_name'      => 'wpforms-qr-code',
			'lib_url'        => esc_url( add_query_arg( 'ver', self::LIBRARY_VERSION, WPFORMS_PLUGIN_URL . 'assets/lib/qr-code-styling.min.js' ) ),
			'logo_wpforms'   => esc_url( WPFORMS_PLUGIN_URL . 'assets/images/builder/qr-code-logo.svg' ),
			'generate'       => esc_html__( 'Generate QR Code', 'wpforms-lite' ),
			'regenerate'     => esc_html__( 'Regenerate QR Code', 'wpforms-lite' ),
			'error_generate' => esc_html__( 'Could not be generated. Please try again.', 'wpforms-lite' ),
			'error_copy'     => esc_html__( 'Could not be copied. Please use Download instead.', 'wpforms-lite' ),
			'error_logo'     => esc_html__( 'The logo image could not be loaded. Please choose another one.', 'wpforms-lite' ),
			'copied'         => esc_html__( 'QR code copied to clipboard.', 'wpforms-lite' ),
			'error_page'     => esc_html__( 'You must select a page first.', 'wpforms-lite' ),
			'error_url'      => esc_html__( 'You must enter a valid URL first.', 'wpforms-lite' ),
			'preview_label'  => $this->get_preview_label_template(),
		];

		return $strings;
	}

	/**
	 * Determine whether the current license unlocks the logo control.
	 *
	 * @since 2.0.1
	 *
	 * @return bool
	 */
	public static function is_logo_allowed(): bool {

		return in_array( wpforms_get_license_type(), self::LOGO_LICENSES, true );
	}

	/**
	 * Get the Pro logo upsell funnel counters, zero-filled for missing events.
	 *
	 * @since 2.0.1
	 *
	 * @return array
	 */
	public static function get_logo_upsell_events(): array {

		return wp_parse_args(
			(array) get_option( self::LOGO_UPSELL_EVENTS_OPTION, [] ),
			array_fill_keys( self::LOGO_UPSELL_EVENTS, 0 )
		);
	}

	/**
	 * Render the QR Code setting in the General section.
	 *
	 * @since 2.0.1
	 *
	 * @param WPForms_Builder_Panel_Settings $settings Settings panel instance.
	 */
	public function render( WPForms_Builder_Panel_Settings $settings ): void {

		$form_data   = (array) $settings->form_data;
		$qr_settings = $form_data['settings'] ?? [];
		$pages       = $this->get_pages( absint( $qr_settings['qr_code_page_id'] ?? 0 ) );
		$options     = [ 'none' => esc_html__( 'None', 'wpforms-lite' ) ];

		// With no page to link to, the Page destination has nothing to offer.
		if ( $pages ) {
			$options['page'] = esc_html__( 'Page', 'wpforms-lite' );
		}

		$options['url'] = esc_html__( 'Custom URL', 'wpforms-lite' );

		wpforms_panel_field(
			'select',
			'settings',
			'qr_code',
			$settings->form_data,
			esc_html__( 'QR Code', 'wpforms-lite' ),
			[
				'default' => 'none',
				'options' => $options,
				'tooltip' => esc_html__( 'Generate a QR code that opens the page or URL you choose, ready to copy or download for print.', 'wpforms-lite' ),
			]
		);

		$this->render_content( $settings, $pages );
	}

	/**
	 * Render the QR Code content block: destination fields, preview, and actions.
	 *
	 * @since 2.0.1
	 *
	 * @param WPForms_Builder_Panel_Settings $settings Settings panel instance.
	 * @param array                          $pages    Pages available for the Destination Page select.
	 */
	private function render_content( WPForms_Builder_Panel_Settings $settings, array $pages ): void {

		$form_data   = (array) $settings->form_data;
		$qr_settings = $form_data['settings'] ?? [];
		$destination = $this->get_effective_destination( $qr_settings['qr_code'] ?? 'none', $pages );
		$logo_url    = $this->get_custom_logo_url( absint( $qr_settings['qr_code_logo_id'] ?? 0 ) );
		$generated   = esc_url_raw( $qr_settings['qr_code_generated'] ?? '' );
		$permalinks  = [];
		$is_hidden   = ! in_array( $destination, [ 'page', 'url' ], true );

		foreach ( array_keys( $pages ) as $id ) {
			/** This filter is documented in includes/functions/access.php. */
			$permalinks[ $id ] = esc_url_raw( (string) apply_filters( 'wpforms_search_pages_for_dropdown_permalink', get_permalink( $id ), get_post( $id ) ) ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
		}

		?>
		<div id="wpforms-panel-field-settings-qr_code-content"
			class="wpforms-panel-field wpforms-panel-field-qr-code-content<?php echo $is_hidden ? ' wpforms-hidden' : ''; ?>"
			data-logo-url="<?php echo esc_url( $logo_url ); ?>"
			data-pages="<?php echo esc_attr( (string) wp_json_encode( $permalinks ) ); ?>">

			<div class="wpforms-qr-code-preview-column">
				<div class="wpforms-qr-code-preview" role="img" aria-label="<?php echo esc_attr( $this->get_preview_label( $generated ) ); ?>">
					<div class="wpforms-qr-code-preview-canvas"></div>
					<div class="wpforms-qr-code-preview-status" aria-hidden="true"></div>
				</div>

				<div class="wpforms-qr-code-actions">
					<button type="button" class="wpforms-btn wpforms-btn-md wpforms-btn-blue wpforms-qr-code-generate">
						<?php esc_html_e( 'Generate QR Code', 'wpforms-lite' ); ?>
					</button>

					<div class="wpforms-qr-code-actions-generated wpforms-hidden">
						<button type="button" class="wpforms-btn wpforms-btn-md wpforms-btn-light-grey wpforms-qr-code-action" data-qr-action="copy" aria-expanded="false">
							<i class="fa fa-copy" aria-hidden="true"></i><?php esc_html_e( 'Copy', 'wpforms-lite' ); ?>
						</button>
						<button type="button" class="wpforms-btn wpforms-btn-md wpforms-btn-light-grey wpforms-qr-code-action" data-qr-action="download" aria-expanded="false">
							<i class="fa fa-download" aria-hidden="true"></i><?php esc_html_e( 'Download', 'wpforms-lite' ); ?>
						</button>
					</div>

					<div class="wpforms-qr-code-format-menu wpforms-context-menu-list wpforms-hidden">
						<button type="button" class="wpforms-qr-code-format wpforms-context-menu-list-item" data-qr-format="png">
							<span class="wpforms-context-menu-list-item-icon"><i class="fa fa-file-image-o" aria-hidden="true"></i></span>
							<span class="wpforms-context-menu-list-item-text"><?php esc_html_e( 'PNG Image', 'wpforms-lite' ); ?></span>
						</button>
						<button type="button" class="wpforms-qr-code-format wpforms-context-menu-list-item" data-qr-format="svg">
							<span class="wpforms-context-menu-list-item-icon"><i class="fa fa-code" aria-hidden="true"></i></span>
							<span class="wpforms-context-menu-list-item-text"><?php esc_html_e( 'SVG Vector', 'wpforms-lite' ); ?></span>
						</button>
					</div>
				</div>
			</div>

			<div class="wpforms-qr-code-settings-column">
				<?php
				wpforms_panel_field(
					'select',
					'settings',
					'qr_code_page_id',
					$settings->form_data,
					esc_html__( 'Destination Page', 'wpforms-lite' ),
					[
						'class'       => 'wpforms-panel-field-qr-code-page' . ( $destination === 'page' ? '' : ' wpforms-hidden' ),
						'options'     => $pages,
						'placeholder' => esc_html__( '--- Select a Page ---', 'wpforms-lite' ),
						'choicesjs'   => [
							'use_ajax'    => true,
							'callback_fn' => 'select_pages',
						],
						'after'       => '<p class="note">' . esc_html__( 'Choose the page for the QR code link.', 'wpforms-lite' ) . '</p>',
					]
				);

				wpforms_panel_field(
					'text',
					'settings',
					'qr_code_url',
					$settings->form_data,
					esc_html__( 'Destination URL', 'wpforms-lite' ),
					[
						'class'       => 'wpforms-panel-field-qr-code-url' . ( $destination === 'url' ? '' : ' wpforms-hidden' ),
						'placeholder' => 'https://',
						'after'       => '<p class="note">' . esc_html__( 'Enter the URL for the QR code link.', 'wpforms-lite' ) . '</p>',
					]
				);

				/**
				 * Fires where the QR Code Logo control is rendered: Pro and Elite hook the full control here, every
				 * lower plan a locked one.
				 *
				 * @since 2.0.1
				 *
				 * @param WPForms_Builder_Panel_Settings $settings Settings panel instance.
				 */
				do_action( 'wpforms_admin_builder_settings_qr_code_logo', $settings );
				?>
			</div>

			<input type="hidden" name="settings[qr_code_generated]" value="<?php echo esc_url( $generated ); ?>">
		</div>
		<?php
	}

	/**
	 * Resolve the destination to render: a saved Page has nothing to point at once the pages are gone.
	 *
	 * @since 2.0.1
	 *
	 * @param string $destination Saved destination.
	 * @param array  $pages       Pages available for the Destination Page select.
	 *
	 * @return string
	 */
	private function get_effective_destination( string $destination, array $pages ): string {

		return $destination === 'page' && ! $pages ? 'none' : $destination;
	}

	/**
	 * Get the preview text alternative: names the encoded destination once generated.
	 *
	 * @since 2.0.1
	 *
	 * @param string $generated Generated destination URL, empty when not generated.
	 *
	 * @return string
	 */
	private function get_preview_label( string $generated ): string {

		if ( ! $generated ) {
			return esc_html__( 'QR code preview', 'wpforms-lite' );
		}

		return sprintf( $this->get_preview_label_template(), $generated );
	}

	/**
	 * Get the preview label template shared by the PHP render and the JS aria-label update.
	 *
	 * @since 2.0.1
	 *
	 * @return string
	 */
	private function get_preview_label_template(): string {

		/* translators: %s - destination URL encoded in the QR code. */
		return esc_html__( 'QR code linking to %s', 'wpforms-lite' );
	}

	/**
	 * Get the custom logo attachment URL.
	 *
	 * @since 2.0.1
	 *
	 * @param int    $logo_id Logo attachment ID.
	 * @param string $size    Image size to return.
	 *
	 * @return string
	 */
	protected function get_custom_logo_url( int $logo_id, string $size = 'full' ): string {

		if ( ! $logo_id || ! wp_attachment_is_image( $logo_id ) ) {
			return '';
		}

		// Don't expose attachments the current user has no access to.
		if ( ! current_user_can( 'edit_post', $logo_id ) ) {
			return '';
		}

		$url = wp_get_attachment_image_url( $logo_id, $size );

		if ( ! $url ) {
			return '';
		}

		// Drop the scheme so the logo always follows the page. A stored http URL on an https builder is mixed
		// content: the browser blocks the library's fetch and the code never renders.
		return (string) preg_replace( '#^https?:#i', '', $url );
	}

	/**
	 * Get the list of pages for the Destination Page select.
	 *
	 * @since 2.0.1
	 *
	 * @param int $page_id Currently saved page ID.
	 *
	 * @return array
	 */
	private function get_pages( int $page_id ): array {

		$pages = [];

		foreach ( wpforms_search_posts() as $post ) {
			$pages[ $post->ID ] = esc_html( wpforms_get_post_title( $post ) );
		}

		// Keep the saved page selectable even when it is outside the default search results.
		$saved_page = $page_id && ! isset( $pages[ $page_id ] ) ? get_post( $page_id ) : null;

		if (
			$saved_page &&
			$saved_page->post_status === 'publish' &&
			$saved_page->post_type === 'page' &&
			current_user_can( 'read_post', $page_id )
		) {
			$pages[ $page_id ] = esc_html( wpforms_get_post_title( $saved_page ) );
		}

		return $pages;
	}

	/**
	 * Sanitize the QR Code settings on form save.
	 *
	 * @since 2.0.1
	 *
	 * @param array $args Form save arguments.
	 *
	 * @return array
	 */
	public function sanitize_settings( $args ): array {

		$args = (array) $args;

		if ( empty( $args['post_content'] ) ) {
			return $args;
		}

		$form_data = json_decode( stripslashes( $args['post_content'] ), true );

		if ( empty( $form_data['settings'] ) ) {
			return $args;
		}

		$settings = $form_data['settings'];

		$settings['qr_code']           = $this->whitelist( $settings['qr_code'] ?? 'none', self::DESTINATIONS, 'none' );
		$settings['qr_code_logo']      = $this->whitelist( $settings['qr_code_logo'] ?? 'wpforms', self::LOGOS, 'wpforms' );
		$settings['qr_code_page_id']   = absint( $settings['qr_code_page_id'] ?? 0 );
		$settings['qr_code_logo_id']   = absint( $settings['qr_code_logo_id'] ?? 0 );
		$settings['qr_code_url']       = $this->sanitize_url( $settings['qr_code_url'] ?? '' );
		$settings['qr_code_generated'] = $this->sanitize_url( $settings['qr_code_generated'] ?? '' );

		$form_data['settings'] = $this->clear_disabled_settings( $settings );
		$args['post_content']  = wpforms_encode( $form_data );

		return $args;
	}

	/**
	 * Reset the QR Code settings on save when the feature is off. None means nothing is configured, so keeping a
	 * destination and a logo around would resurface them the moment the setting is switched back on. This runs on
	 * save only: the builder leaves the fields alone so switching back and forth in one session is not a data loss.
	 *
	 * @since 2.0.1
	 *
	 * @param array $settings Sanitized settings.
	 *
	 * @return array
	 */
	private function clear_disabled_settings( array $settings ): array {

		if ( $settings['qr_code'] !== 'none' ) {
			return $settings;
		}

		$settings['qr_code_page_id']   = 0;
		$settings['qr_code_url']       = '';
		$settings['qr_code_logo']      = 'wpforms';
		$settings['qr_code_logo_id']   = 0;
		$settings['qr_code_generated'] = '';

		return $settings;
	}

	/**
	 * Whitelist a value against the allowed list.
	 *
	 * @since 2.0.1
	 *
	 * @param mixed  $value    Raw value.
	 * @param array  $allowed  Allowed values.
	 * @param string $fallback Fallback value.
	 *
	 * @return string
	 */
	private function whitelist( $value, array $allowed, string $fallback ): string {

		return in_array( $value, $allowed, true ) ? $value : $fallback;
	}

	/**
	 * Sanitize a destination URL: http/https schemes only.
	 *
	 * @since 2.0.1
	 *
	 * @param mixed $url Raw URL.
	 *
	 * @return string
	 */
	private function sanitize_url( $url ): string {

		return esc_url_raw( (string) $url, [ 'http', 'https' ] );
	}
}