File: /home/globfdxw/public_html/wp-content/plugins/wpforms-pdf/src/Builder/FormBuilder.php
<?php
namespace WPFormsPDF\Builder;
use WPForms_Builder_Panel_Settings;
use WPFormsPDF\PDF\Renderer;
use WPFormsPDF\Plugin;
use WPFormsPDF\Builder\Settings\Appearance;
use WPFormsPDF\Builder\Settings\Templates;
use WPFormsPDF\Builder\Settings\Settings;
use WPFormsPDF\Templates\Themes;
/**
* Class FormBuilder handles functionality in the Form Builder.
*
* @since 1.0.0
*/
class FormBuilder {
/**
* Priority for a provider that will affect loading/placement order.
*
* @since 1.0.0
*/
public const PRIORITY = 10;
/**
* WPForms settings.
*
* @since 1.0.0
*
* @var array
*/
private $wpforms_settings;
/**
* Initialize class.
*
* @since 1.0.0
*/
public function __construct() {
if ( ! wpforms_is_admin_ajax() && ! wpforms_is_admin_page( 'builder' ) ) {
return;
}
$this->hooks();
}
/**
* Register hooks.
*
* @since 1.0.0
*/
private function hooks(): void {
add_filter( 'wpforms_builder_settings_sections', [ $this, 'panel_sidebar' ], self::PRIORITY, 2 );
add_filter( 'wpforms_builder_strings', [ $this, 'builder_strings' ], 30, 2 );
add_action( 'wpforms_form_settings_panel_content', [ $this, 'settings_panel_content' ], self::PRIORITY );
add_action( 'wpforms_builder_enqueues', [ $this, 'enqueue_assets' ] );
}
/**
* Add own localized strings to the Builder.
*
* @since 1.0.0
*
* @param array $strings Localized strings.
*
* @return array
* @noinspection PhpMissingParamTypeInspection
*/
public function builder_strings( $strings ): array {
$strings = (array) $strings;
$strings['pdf_delete'] = esc_html__( 'Are you sure you want to delete this PDF?', 'wpforms-pdf' );
$strings['pdf_prompt'] = esc_html__( 'Enter a PDF name', 'wpforms-pdf' );
$strings['pdf_ph'] = esc_html__( 'Eg: Entry PDF', 'wpforms-pdf' );
$strings['pdf_error'] = esc_html__( 'You must provide a PDF name', 'wpforms-pdf' );
$strings['pdf_clone'] = esc_html__( ' - clone', 'wpforms-pdf' );
// Preview strings.
$strings['pdf_preview_back'] = esc_html__( 'Back to Settings', 'wpforms-pdf' );
$strings['pdf_preview_title'] = esc_html__( 'PDF Preview', 'wpforms-pdf' );
$strings['pdf_preview_subtitle'] = esc_html__( 'This is a live preview of your PDF template.', 'wpforms-pdf' );
$strings['pdf_preview_error'] = esc_html__( 'There was an error retrieving your PDF preview.', 'wpforms-pdf' );
$strings['pdf_preview_zoom'] = esc_html__( 'Zoom', 'wpforms-pdf' );
// Access restriction strings.
$strings['pdf']['access_restrictions'] = [
'select_users' => esc_html__( 'Select users', 'wpforms-pdf' ),
'password_match_error_title' => esc_html__( 'Passwords Do Not Match', 'wpforms-pdf' ),
'password_empty_error_title' => esc_html__( 'Password Is Empty', 'wpforms-pdf' ),
'password_match_error_text' => esc_html__( 'Please check your password and confirm password for the PDF Password Protection.', 'wpforms-pdf' ),
'password_empty_error_text' => esc_html__( 'Please enter a password for the PDF Password Protection or remove the password protection.', 'wpforms-pdf' ),
'all_user_roles_selected' => esc_html__( 'All User Roles already selected', 'wpforms-pdf' ),
'no_user_roles_selected' => esc_html__( 'No user roles selected', 'wpforms-pdf' ),
];
// Appearance strings.
$strings['pdf_settings_theme_no_choices'] = esc_html__( 'There are no themes available.', 'wpforms-pdf' );
$strings['pdf_settings_theme_no_results'] = esc_html__( 'Themes not found.', 'wpforms-pdf' );
$strings['pdf']['settings'] = [
'file_name_duplicates_alert_title' => esc_html__( 'PDF Name Conflict', 'wpforms-pdf' ),
'file_name_duplicates_alert_content' => esc_html__( 'Multiple PDFs have the same file name, which will prevent them from being saved properly. Check the file names and make sure they\'re all unique.', 'wpforms-pdf' ),
'file_name_empty_alert_title' => esc_html__( 'PDF File Name Required', 'wpforms-pdf' ),
'file_name_empty_alert_content' => esc_html__( 'The File Name cannot be empty.', 'wpforms-pdf' ),
'notifications_no_choices' => esc_html__( 'There are no Notifications available.', 'wpforms-pdf' ),
'notifications_no_results' => esc_html__( 'Notifications not found.', 'wpforms-pdf' ),
'loading_error' => esc_html__( 'There is a problem with this template. Please select a different style.', 'wpforms-pdf' ),
];
$strings['pdf']['theme'] = [ // Translators: %1$s: Theme name.
'delete_confirm' => esc_html__( 'Are you sure you want to delete the %1$s theme?', 'wpforms-pdf' ),
'delete_cant_undone' => esc_html__( 'This cannot be undone.', 'wpforms-pdf' ),
'delete_yes' => esc_html__( 'Yes, Delete', 'wpforms-pdf' ),
'delete_title' => esc_html__( 'Delete PDF Theme', 'wpforms-pdf' ),
];
$strings['pdf']['template'] = [ // Translators: %1$s: Template Style name.
'delete_confirm' => esc_html__( 'Are you sure you want to delete the %1$s template?', 'wpforms-pdf' ),
'delete_title' => esc_html__( 'Delete PDF Template', 'wpforms-pdf' ),
'reset_warning' => esc_html__( 'If you switch to a different template, your changes will be lost. We’ve saved a copy of your template in case you change your mind.', 'wpforms-pdf' ),
];
$strings['pdf_text'] = esc_html__( 'Text', 'wpforms-pdf' );
$strings['pdf_server_error'] = esc_html__( 'Unfortunately, there was a problem connecting to the server. Please try again.', 'wpforms-pdf' );
return $strings;
}
/**
* Enqueue JavaScript and CSS files.
*
* @since 1.0.0
*/
public function enqueue_assets(): void {
$min = wpforms_get_min_suffix();
// Enqueue builder base styles.
wp_enqueue_style(
'wpforms-pdf-builder',
WPFORMS_PDF_URL . "assets/css/builder$min.css",
[],
WPFORMS_PDF_VERSION
);
// Enqueue builder base script.
wp_enqueue_script(
'wpforms-pdf-builder',
WPFORMS_PDF_URL . "assets/js/builder$min.js",
[ 'jquery' ],
WPFORMS_PDF_VERSION,
true
);
$themes = wpforms_pdf()->themes;
$templates = wpforms_pdf()->templates;
// Localize data for the base builder script.
wp_localize_script(
'wpforms-pdf-builder',
'wpformsPDF',
[
'themes' => $themes->get_themes(),
'templates' => $templates->get_templates(),
'customThemes' => $themes->get_custom_themes(),
'customTemplates' => $templates->get_custom_templates(),
'categories' => $templates->get_categories(),
'defaults' => $this->get_defaults(),
'modules' => $this->get_modules(),
'defaultTheme' => Themes::DEFAULT_THEME,
'siteUrl' => get_site_url(),
'pluginUrl' => WPFORMS_PDF_URL,
'version' => WPFORMS_PDF_VERSION,
'fonts' => Renderer::FONTS,
'tinymce_defaults' => [
'tinymce' => [
'toolbar1' => 'bold,italic,underline,blockquote,strikethrough,bullist,numlist,alignleft,aligncenter,alignright,undo,redo,link',
'toolbar2' => 'formatselect',
'toolbar' => 'toolbar1 | toolbar2',
'height' => 160,
],
'quicktags' => true,
],
]
);
}
/**
* Add a new item `PDF` to the panel sidebar.
*
* @since 1.0.0
*
* @param array|mixed $sections Registered sections.
*
* @return array
* @noinspection PhpMissingParamTypeInspection
*/
public function panel_sidebar( $sections ): array {
$sections = (array) $sections;
$sections[ Plugin::SLUG ] = esc_html__( 'PDF', 'wpforms-pdf' );
return $sections;
}
/**
* Render the settings panel content.
*
* @since 1.0.0
*
* @param WPForms_Builder_Panel_Settings|object $settings Settings object.
*/
public function settings_panel_content( object $settings ): void {
$form_settings = ! empty( $settings->form_data['settings'] ) ? $settings->form_data['settings'] : [];
$pdfs = is_array( $form_settings ) && isset( $form_settings['pdfs'] ) ? $form_settings['pdfs'] : [];
$next_pdf_id = $pdfs ? absint( max( array_keys( $pdfs ) ) ) + 1 : 1;
?>
<div class="wpforms-panel-content-section wpforms-panel-content-section-pdf">
<?php
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
echo $this->get_settings_title( $next_pdf_id );
echo $this->get_settings_empty( $pdfs );
echo $this->get_settings_block_template( $settings );
foreach ( $pdfs as $pdf_id => $pdf ) {
$is_opened = $this->is_section_opened( $settings, $pdf_id );
echo $this->get_settings_block( $settings, $pdf_id, $pdf, $is_opened );
}
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
?>
</div>
<?php
}
/**
* Get the settings block template.
*
* @since 1.0.0
*
* @param object $settings Settings object.
*
* @return string
*/
private function get_settings_block_template( object $settings ): string {
ob_start();
?>
<script type="text/html" id="wpforms-pdf-template-block">
<?php echo $this->get_settings_block( $settings, '{CLONE}', [], true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</script>
<?php
return ob_get_clean();
}
/**
* Get the settings block.
*
* @since 1.0.0
*
* @param object $settings Settings object.
* @param int|string $pdf_id PDF ID.
* @param array $pdf PDF data.
* @param bool $is_opened Whether the section is opened.
*
* @return string
*/
private function get_settings_block( object $settings, $pdf_id, array $pdf, bool $is_opened ): string {
ob_start();
?>
<div class="wpforms-pdf wpforms-builder-settings-block" data-block-type="pdf" data-block-id="<?php echo esc_attr( $pdf_id ); ?>">
<?php echo $this->get_settings_header( $pdf, $pdf_id, $is_opened ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo $this->get_settings_content( $is_opened, $pdf_id, $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
<?php
return ob_get_clean();
}
/**
* Get the settings content.
*
* @since 1.0.0
*
* @param bool $is_opened Whether the section is opened.
* @param int|string $pdf_id PDF ID.
* @param object $form_settings Settings object.
*
* @return string
*/
private function get_settings_content( bool $is_opened, $pdf_id, object $form_settings ): string {
$appearance = new Appearance( $pdf_id, $form_settings );
$settings = new Settings( $pdf_id, $form_settings );
$templates = new Templates( $pdf_id, $form_settings );
ob_start();
?>
<div class="wpforms-builder-settings-block-content" <?php echo $is_opened ? '' : 'style="display:none"'; ?>>
<?php
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
echo $settings->get_core_settings_group();
echo $templates->get_settings_group();
echo $appearance->get_settings_group();
echo $settings->get_advanced_settings_group();
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
?>
</div>
<?php
return ob_get_clean();
}
/**
* Get the settings empty.
*
* @since 1.0.0
*
* @param array $pdfs PDF data.
*
* @return string
*/
private function get_settings_empty( array $pdfs ): string {
$classes = ! empty( $pdfs ) ? ' wpforms-hidden' : '';
?>
<div class="wpforms-builder-provider-connections-default<?php echo esc_attr( $classes ); ?>">
<img src="<?php echo esc_url( WPFORMS_PDF_URL . 'assets/images/addon-icon.svg' ); ?>" alt="addon icon">
<div class="wpforms-builder-provider-settings-default-content">
<h4>
<?php esc_html_e( 'Easily generate PDFs that contain form data and attach them to notifications.', 'wpforms-pdf' ); ?>
</h4>
<p>
<a href="<?php echo esc_url( wpforms_utm_link( 'https://wpforms.com/docs/pdf-addon/', 'Builder Settings', 'PDF Addon Learn More' ) ); ?>" rel="noopener noreferrer" target="_blank">
<?php esc_html_e( 'Learn more about the PDF Addon', 'wpforms-pdf' ); ?>
</a>
</p>
</div>
</div>
<?php
return ob_get_clean() . $this->get_license_notice();
}
/**
* Get the license notice.
*
* @since 1.2.0
*
* @return string
* @noinspection HtmlUnknownTarget
*/
private function get_license_notice(): string {
if ( wpforms_pdf()->helpers->is_valid_license() ) {
return '';
}
$license_notice_text = sprintf( /* translators: %1$s - URL to the WPForms settings page. */
__( 'PDF generation requires an active WPForms license. Please <a href="%1$s" rel="noopener noreferrer" target="_blank">check your license status</a>.', 'wpforms-pdf' ),
esc_url( admin_url( 'admin.php?page=wpforms-settings' ) )
);
return sprintf(
'<div class="wpforms-alert wpforms-alert-danger">
%1$s
</div>',
wp_kses(
$license_notice_text,
[
'a' => [
'href' => [],
'rel' => [],
'target' => [],
],
]
)
);
}
/**
* Get the settings title.
*
* @since 1.0.0
*
* @param int $next_pdf_id Next PDF ID.
*
* @return string
*/
private function get_settings_title( int $next_pdf_id ): string {
ob_start();
?>
<div class="wpforms-panel-content-section-title">
<?php echo esc_html( Plugin::NAME ); ?>
<button class="wpforms-pdf-add wpforms-builder-settings-block-add"
data-provider="<?php echo esc_attr( Plugin::SLUG ); ?>"
data-block-type="pdf"
data-next-id="<?php echo esc_attr( $next_pdf_id ); ?>"
data-default-name="<?php echo esc_attr__( 'Default PDF', 'wpforms-pdf' ); ?>"
>
<?php esc_html_e( 'Add New PDF', 'wpforms-pdf' ); ?>
</button>
</div>
<?php
return ob_get_clean();
}
/**
* Get the settings header.
*
* @since 1.0.0
*
* @param array $pdf PDF data.
* @param int|string $pdf_id PDF ID.
* @param bool $is_opened Whether the section is opened.
*
* @return string
*/
private function get_settings_header( array $pdf, $pdf_id, bool $is_opened ): string {
ob_start();
$name = $pdf['name'] ?? esc_html__( 'Default PDF', 'wpforms-pdf' );
?>
<div class="wpforms-builder-settings-block-header">
<div class="wpforms-builder-settings-block-actions">
<?php $this->display_status_button( $pdf, $pdf_id ); ?>
<button class="wpforms-builder-settings-block-clone" title="<?php esc_attr_e( 'Clone', 'wpforms-pdf' ); ?>">
<i class="fa fa-copy"></i>
</button>
<button class="wpforms-builder-settings-block-delete" title="<?php esc_attr_e( 'Delete', 'wpforms-pdf' ); ?>">
<i class="fa fa-trash-o"></i>
</button>
<button class="wpforms-builder-settings-block-toggle" title="<?php esc_attr_e( 'Open / Close', 'wpforms-pdf' ); ?>">
<?php if ( $is_opened ) : ?>
<i class="fa fa-chevron-circle-up"></i>
<?php else : ?>
<i class="fa fa-chevron-circle-down"></i>
<?php endif; ?>
</button>
</div>
<div class="wpforms-builder-settings-block-name-holder">
<span class="wpforms-builder-settings-block-name">
<?php echo esc_html( $name ); ?>
</span>
<div class="wpforms-builder-settings-block-name-edit">
<input type="text" name="settings[pdfs][<?php echo wpforms_validate_field_id( $pdf_id ); // phpcs:ignore WordPress.Security.EscapeOutput ?>][name]" value="<?php echo esc_attr( $name ); ?>">
</div>
<button class="wpforms-builder-settings-block-edit" title="<?php esc_attr_e( 'Edit', 'wpforms-pdf' ); ?>">
<i class="fa fa-pencil"></i>
</button>
</div>
</div>
<?php
return ob_get_clean();
}
/**
* Display the PDF block status button.
*
* @since 1.0.0
*
* @param array $pdf PDF data.
* @param int|string $id PDF ID.
*/
private function display_status_button( array $pdf, $id ): void {
$is_active = ! isset( $pdf['enable'] ) || (int) $pdf['enable'] === 1;
if ( $is_active ) {
$label = esc_html__( 'Active', 'wpforms-pdf' );
$title = esc_html__( 'Deactivate', 'wpforms-pdf' );
} else {
$label = esc_html__( 'Inactive', 'wpforms-pdf' );
$title = esc_html__( 'Activate', 'wpforms-pdf' );
}
printf(
'<span class="wpforms-builder-settings-block-status wpforms-badge wpforms-badge-sm wpforms-badge-%1$s wpforms-status-button" title="%5$s" data-active="%2$s">%3$s<i class="wpforms-status-label">%4$s</i></span>',
$is_active ? 'green' : 'silver',
(int) $is_active,
$is_active ? '<i class="fa fa-check"></i>' : '<i class="fa fa-times"></i>',
esc_html( $label ),
esc_attr( $title )
);
printf(
'<input type="hidden" name="settings[pdfs][%1$s][enable]" id="wpforms-panel-field-pdfs-%1$s-enable" value="%2$d">',
esc_attr( $id ),
(int) $is_active
);
}
/**
* Get custom entity (template or theme) settings.
*
* @since 1.0.0
*
* @param int|string $pdf_id PDF ID.
* @param string $slug Slug.
* @param array $form_data Form data.
*
* @return string
*/
public function get_custom_entity_settings( $pdf_id, string $slug, array $form_data ): string {
// Support only theme and template.
$slug = $slug === 'theme' ? 'theme' : 'template';
$label = [
'template' => esc_html__( 'Template Name', 'wpforms-pdf' ),
'theme' => esc_html__( 'Theme Name', 'wpforms-pdf' ),
];
$tooltip = [
'template' => esc_html__( 'Define a custom name for your PDF template.', 'wpforms-pdf' ),
'theme' => esc_html__( 'Define a custom name for your PDF theme.', 'wpforms-pdf' ),
];
// Open wrapper.
$settings = sprintf(
'<div class="wpforms-pdf-custom-entity-settings" data-pdf-id="%2$s" data-entity="%1$s">',
esc_attr( $slug ),
$pdf_id
);
// Rename / Delete theme actions.
$settings .= sprintf(
'<div class="wpforms-panel-field wpforms-pdf-custom-entity-actions">
<a href="#" class="wpforms-pdf-custom-entity-rename" data-pdf-id="%2$s" data-entity="%1$s">%3$s</a>
<span></span>
<a href="#" class="wpforms-pdf-custom-entity-delete" data-pdf-id="%2$s" data-entity="%1$s">%4$s</a>
</div>',
esc_attr( $slug ),
$pdf_id,
esc_html__( 'Rename', 'wpforms-pdf' ),
esc_html__( 'Delete', 'wpforms-pdf' )
);
// Save / Cancel buttons.
$buttons = sprintf(
'<div class="wpforms-pdf-custom-entity-name-buttons">
<button type="button" class="wpforms-pdf-custom-entity-name-save wpforms-btn wpforms-btn-sm wpforms-btn-blue" data-pdf-id="%2$s" data-entity="%1$s">%3$s</button>
<button type="button" class="wpforms-pdf-custom-entity-name-cancel wpforms-btn wpforms-btn-sm wpforms-btn-light-grey" data-pdf-id="%2$s" data-entity="%1$s">%4$s</button>
</div>',
esc_attr( $slug ),
$pdf_id,
esc_html__( 'Save', 'wpforms-pdf' ),
esc_html__( 'Cancel', 'wpforms-pdf' )
);
$settings .= wpforms_panel_field(
'text',
'pdfs',
$slug . '_name',
$form_data,
$label[ $slug ],
[
'default' => '',
'subsection' => $pdf_id,
'parent' => 'settings',
'class' => 'wpforms-field-pdf-custom-entity-name-field wpforms-hidden',
'tooltip' => $tooltip[ $slug ],
'after' => $buttons,
],
false
);
$settings .= '</div>';
return $settings;
}
/**
* Check if the section is opened.
*
* @since 1.0.0
*
* @param object $settings Settings object.
* @param int|string $pdf_id PDF ID.
*
* @return bool
*/
private function is_section_opened( object $settings, $pdf_id ): bool {
return (
! empty( $settings->form_data['id'] ) &&
'opened' === wpforms_builder_settings_block_get_state( $settings->form_data['id'], $pdf_id, 'pdf' )
);
}
/**
* Get PDF default settings.
*
* @since 1.0.0
*
* @return array Default settings.
*/
public function get_defaults(): array {
static $defaults;
if ( $defaults ) {
return $defaults;
}
$this->wpforms_settings = (array) get_option( 'wpforms_settings', [] );
$defaults = [
'template' => 'notification-' . ( $this->wpforms_settings['email-template'] ?? 'modern' ),
'appearance' => [
'page_background_color' => $this->get_color_scheme_default(
'email_background_color',
'background_light'
),
'container_background_color' => $this->get_color_scheme_default(
'email_body_color',
'background'
),
'text_color' => $this->get_color_scheme_default(
'email_text_color',
'primary'
),
'link_color' => $this->get_color_scheme_default(
'email_links_color',
'accent'
),
'logo_url' => $this->wpforms_settings['email-header-image'] ?? '',
'logo_size' => $this->wpforms_settings['email-header-image-size'] ?? 'medium',
'font' => $this->wpforms_settings['email-typography'] ?? 'sans-serif',
],
];
return $defaults;
}
/**
* Get color scheme default value.
*
* @since 1.0.0
*
* @param string $setting_color_name Setting color name.
* @param string $theme_color_name Color name to get the default value for.
*
* @return string
*/
private function get_color_scheme_default( string $setting_color_name, string $theme_color_name ): string {
$theme = Themes::DEFAULT_COLORS;
$theme_color = $theme[ $theme_color_name ] ?? '';
return $this->wpforms_settings['email-color-scheme'][ $setting_color_name ] ?? $theme_color;
}
/**
* Get PDF default settings.
*
* @since 1.0.0
*
* @return array Default settings.
*/
public function get_modules(): array {
$min = wpforms_get_min_suffix();
return [
[
'name' => 'blocks',
'path' => "./modules/blocks{$min}.js",
],
[
'name' => 'notifications',
'path' => "./modules/notifications{$min}.js",
],
[
'name' => 'templates',
'path' => "./modules/templates{$min}.js",
],
[
'name' => 'previewReflector',
'path' => "./modules/preview-reflector{$min}.js",
],
[
'name' => 'preview',
'path' => "./modules/preview{$min}.js",
],
[
'name' => 'custom',
'path' => "./modules/custom{$min}.js",
],
[
'name' => 'appearance',
'path' => "./modules/appearance{$min}.js",
],
[
'name' => 'theme',
'path' => "./modules/theme{$min}.js",
],
[
'name' => 'accessRestrictions',
'path' => "./modules/access-restrictions{$min}.js",
],
];
}
}