/home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Admin/Builder/Settings
Edit: /home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Admin/Builder/Settings/QrCode.php (15342B)
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
}
?>
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' ] );
}
}