File: /home/globfdxw/www/wp-content/plugins/wpforms-pdf/src/Builder/Settings/Settings.php
<?php
namespace WPFormsPDF\Builder\Settings;
/**
* Class Settings handles general settings for PDF form builder.
*
* @since 1.0.0
*/
class Settings {
/**
* PDF ID.
*
* @since 1.0.0
*
* @var int|string
*/
private $pdf_id;
/**
* Settings.
*
* @since 1.0.0
*
* @var object
*/
private $settings;
/**
* User roles.
*
* @since 1.0.0
*
* @var array
*/
private $user_roles;
/**
* Constructor.
*
* @since 1.0.0
*
* @param int|string $pdf_id PDF ID.
* @param object $settings Settings object.
*/
public function __construct( $pdf_id, object $settings ) {
$this->pdf_id = $pdf_id;
$this->settings = $settings;
}
/**
* Get a core settings group.
*
* @since 1.0.0
*
* @return string
*/
public function get_core_settings_group(): string {
$settings = wpforms_panel_field(
'text',
'pdfs',
'file_name',
$this->settings->form_data,
esc_html__( 'File Name', 'wpforms-pdf' ) . ' <span class="required">*</span>',
[
'default' => 'Entry for {form_name}.pdf',
'tooltip' => esc_html__( 'The name of the PDF file that will be generated.', 'wpforms-pdf' ),
'smarttags' => [ 'type' => 'all' ],
'class' => 'wpforms-pdf-file-name',
'parent' => 'settings',
'subsection' => $this->pdf_id,
'input_class' => 'wpforms-smart-tags-enabled wpforms-required',
],
false
);
$settings .= wpforms_panel_field(
'select',
'pdfs',
'notifications',
$this->settings->form_data,
esc_html__( 'Notifications', 'wpforms-pdf' ),
[
'options' => $this->get_notifications(),
'default' => 1,
'multiple' => true,
'subsection' => $this->pdf_id,
'parent' => 'settings',
'tooltip' => esc_html__( 'Select which email notifications this PDF should be attached to.', 'wpforms-pdf' ),
'class' => 'wpforms-pdf-notifications',
'after' => '<p class="note">' . esc_html__( 'Select the notifications that you would like to attach this PDF to.', 'wpforms-pdf' ) . '</p>',
],
false
);
$settings .= sprintf(
'<input type="hidden" name="settings[pdfs][%1$s][notifications_json]" class="wpforms-panel-field-pdf-notifications-json" value="%2$s">',
$this->pdf_id,
esc_attr( $this->settings->form_data['settings']['pdfs'][ $this->pdf_id ]['notifications_json'] ?? '' )
);
if ( function_exists( 'wpforms_conditional_logic' ) ) {
$settings .= wpforms_conditional_logic()->builder_block(
[
'form' => $this->settings->form_data,
'type' => 'panel',
'panel' => 'pdfs',
'parent' => 'settings',
'subsection' => $this->pdf_id,
'field' => 'conditional_logic',
'actions' => [
'go' => esc_html__( 'Send', 'wpforms-pdf' ),
'stop' => esc_html__( 'Don\'t send', 'wpforms-pdf' ),
],
'action_desc' => esc_html__( 'this pdf if', 'wpforms-pdf' ),
'reference' => esc_html__( 'Form PDF', 'wpforms-pdf' ),
],
false
);
}
return $settings;
}
/**
* Get an advanced settings group.
*
* @since 1.0.0
*
* @return string
*/
public function get_advanced_settings_group(): string {
$advanced = wpforms_panel_field(
'select',
'pdfs',
'paper_size',
$this->settings->form_data,
esc_html__( 'Paper Size', 'wpforms-pdf' ),
[
'options' => [
'letter' => esc_html__( 'Letter (8.5" x 11")', 'wpforms-pdf' ),
'legal' => esc_html__( 'Legal (8.5" x 14")', 'wpforms-pdf' ),
'tabloid' => esc_html__( 'Tabloid (11" x 17")', 'wpforms-pdf' ),
'executive' => esc_html__( 'Executive (7.25" x 10.5")', 'wpforms-pdf' ),
'A3' => esc_html__( 'A3 (11.7" x 16.5")', 'wpforms-pdf' ),
'A4' => esc_html__( 'A4 (8.27" x 11.69")', 'wpforms-pdf' ),
'A5' => esc_html__( 'A5 (5.83" x 8.27")', 'wpforms-pdf' ),
'B5' => esc_html__( 'B5 (6.93" x 9.84")', 'wpforms-pdf' ),
],
'default' => 'A4',
'subsection' => $this->pdf_id,
'parent' => 'settings',
'tooltip' => esc_html__( 'Select the paper size for your PDF document.', 'wpforms-pdf' ),
'class' => 'col-half',
],
false
);
$advanced .= wpforms_panel_field(
'select',
'pdfs',
'orientation',
$this->settings->form_data,
esc_html__( 'Orientation', 'wpforms-pdf' ),
[
'options' => [
'portrait' => esc_html__( 'Portrait', 'wpforms-pdf' ),
'landscape' => esc_html__( 'Landscape', 'wpforms-pdf' ),
],
'default' => 'portrait',
'subsection' => $this->pdf_id,
'parent' => 'settings',
'tooltip' => esc_html__( 'Choose between portrait or landscape orientation for your PDF.', 'wpforms-pdf' ),
'class' => 'col-half',
],
false
);
$advanced .= '<div class="wpforms-pdf-access-restrictions-wrap">';
$advanced .= wpforms_panel_field(
'toggle',
'pdfs',
'access',
$this->settings->form_data,
esc_html__( 'Enable Access Restrictions', 'wpforms-pdf' ),
[
'subsection' => $this->pdf_id,
'parent' => 'settings',
'tooltip' => esc_html__( 'Enable this to restrict access to the PDF.', 'wpforms-pdf' ),
'class' => 'wpforms-pdf-access-restrictions',
],
false
);
// User Restrictions.
$advanced .= $this->get_user_restrictions_settings();
// Password Protection.
$advanced .= $this->get_password_protection_settings();
$advanced .= '</div>';
return wpforms_panel_fields_group(
$advanced,
[
'title' => esc_html__( 'Advanced', 'wpforms-pdf' ),
'group' => 'settings_pdf_advanced',
'class' => 'wpforms-pdf-advanced-group',
'borders' => [ 'top' ],
'unfoldable' => true,
],
false
);
}
/**
* Get notifications.
*
* @since 1.0.0
*
* @return array
*/
public function get_notifications(): array {
return array_map(
static function ( $data ) {
return $data['notification_name'] ?? '';
},
$this->settings->form_data['settings']['notifications'] ?? []
);
}
/**
* Get user restrictions settings.
*
* @since 1.0.0
*
* @return string
*/
private function get_user_restrictions_settings(): string {
$saved_user_roles = $this->get_saved_user_data( 'roles', [ 'administrator' ] );
$saved_user_names = $this->get_saved_user_data( 'names', [] );
// Convert user IDs to integers.
$saved_user_names = array_map( 'intval', $saved_user_names );
$user_restrictions = '<div class="wpforms-pdf-user-restrictions-wrap wpforms-pdf-restriction-settings">';
$user_restrictions .= wpforms_panel_field(
'select',
'pdfs',
'user_restrictions',
$this->settings->form_data,
esc_html__( 'User Restrictions', 'wpforms-pdf' ),
[
'options' => [
'none' => esc_html__( 'None', 'wpforms-pdf' ),
'loggedin' => esc_html__( 'Logged-in Users', 'wpforms-pdf' ),
],
'value' => $this->settings->form_data['settings']['pdfs'][ $this->pdf_id ]['user_restrictions'] ?? 'none',
'subsection' => $this->pdf_id,
'parent' => 'settings',
'tooltip' => esc_html__( 'Select whether this PDF is restricted to specific users.', 'wpforms-pdf' ),
'class' => 'wpforms-pdf-user-restrictions',
],
false
);
$user_restrictions .= '<div class="wpforms-pdf-user-roles-restrictions-wrap">';
$user_restrictions .= wpforms_panel_field(
'select',
'pdfs',
'user_roles_restrictions_display',
$this->settings->form_data,
esc_html__( 'User Roles', 'wpforms-pdf' ),
[
'options' => $this->get_user_roles(),
'value' => $saved_user_roles,
'subsection' => $this->pdf_id,
'parent' => 'settings',
'tooltip' => esc_html__( 'Select which user roles can access this PDF.', 'wpforms-pdf' ),
'after' => '<p class="note">' . esc_html__( 'All users with the selected roles will be able to access this PDF.', 'wpforms-pdf' ) . '</p>',
'class' => 'wpforms-pdf-user-roles-select',
'data' => [
'field-id' => $this->pdf_id,
'field-name' => 'user_roles_restrictions',
],
'multiple' => true,
'choicesjs' => false,
],
false
);
// Add hidden field for storing user roles as JSON.
$user_restrictions .= sprintf(
'<input type="hidden" name="settings[pdfs][%s][user_roles_restrictions]" value="%s" id="settings-pdfs-%s-user_roles_restrictions-options">',
$this->pdf_id,
esc_attr( wp_json_encode( $saved_user_roles ) ),
$this->pdf_id
);
$user_restrictions .= '</div>';
$user_restrictions .= '<div class="wpforms-pdf-user-names-restrictions-wrap">';
$user_restrictions .= wpforms_panel_field(
'select',
'pdfs',
'user_names_restrictions_display',
$this->settings->form_data,
esc_html__( 'Users', 'wpforms-pdf' ),
[
'options' => $this->get_selected_users( $saved_user_names ),
'value' => $saved_user_names,
'subsection' => $this->pdf_id,
'parent' => 'settings',
'tooltip' => esc_html__( 'Select which users can access this PDF.', 'wpforms-pdf' ),
'class' => 'wpforms-pdf-user-names-select',
'data' => [
'field-id' => $this->pdf_id,
'field-name' => 'user_names_restrictions',
],
'multiple' => true,
'placeholder' => esc_html__( 'Select users', 'wpforms-pdf' ),
'choicesjs' => false,
],
false
);
// Add hidden field for storing usernames as JSON.
$user_restrictions .= sprintf(
'<input type="hidden" name="settings[pdfs][%s][user_names_restrictions]" value="%s" id="settings-pdfs-%s-user_names_restrictions-options">',
$this->pdf_id,
esc_attr( wp_json_encode( $saved_user_names ) ),
$this->pdf_id
);
$user_restrictions .= '</div>';
$user_restrictions .= '</div>';
return $user_restrictions;
}
/**
* Get password protection settings.
*
* @since 1.0.0
*
* @return string
*/
private function get_password_protection_settings(): string {
$output = '<div class="wpforms-pdf-password-protection-wrap wpforms-pdf-restriction-settings">';
$output .= wpforms_panel_field(
'toggle',
'pdfs',
'is_protected',
$this->settings->form_data,
esc_html__( 'Password Protection', 'wpforms-pdf' ),
[
'subsection' => $this->pdf_id,
'parent' => 'settings',
'tooltip' => esc_html__( 'Enable password protection for this PDF.', 'wpforms-pdf' ),
'class' => 'wpforms-pdf-password-restrictions',
],
false
);
$output .= '<div class="wpforms-pdf-password-fields-wrap">';
$output .= wpforms_panel_field(
'text',
'pdfs',
'protection_password',
$this->settings->form_data,
esc_html__( 'Password', 'wpforms-pdf' ),
[
'subsection' => $this->pdf_id,
'parent' => 'settings',
'tooltip' => esc_html__( 'Enter a password for this PDF.', 'wpforms-pdf' ),
'type' => 'password',
'attrs' => [
'autocomplete' => 'new-password',
],
'class' => 'wpforms-pdf-password wpforms-pdf-password-field col-half',
'input_class' => 'wpforms-pdf-password-input',
'after' => '<div class="after-input sub-label">' . esc_html__( 'Enter Password', 'wpforms-pdf' ) . '</div><button type="button" class="wpforms-pdf-password-clean" title="' . esc_attr__( 'Clear password', 'wpforms-pdf' ) . '"><i class="fa fa-times-circle"></i></button>',
],
false
);
$output .= wpforms_panel_field(
'text',
'pdfs',
'protection_password_confirm',
$this->settings->form_data,
esc_html__( 'Confirm Password', 'wpforms-pdf' ),
[
'subsection' => $this->pdf_id,
'parent' => 'settings',
'type' => 'password',
'attrs' => [
'autocomplete' => 'new-password',
],
'class' => 'wpforms-pdf-password-confirm wpforms-pdf-password-field col-half',
'input_class' => 'wpforms-pdf-password-confirm-input',
'after' => '<div class="after-input sub-label">' . esc_html__( 'Confirm Password', 'wpforms-pdf' ) . '</div><div class="wpforms-pdf-password-error">' . esc_html__( 'Passwords do not match', 'wpforms-pdf' ) . '</div>',
],
false
);
$output .= '</div>';
$output .= '</div>';
return $output;
}
/**
* Get user roles.
*
* @since 1.0.0
*
* @return array
*/
private function get_user_roles(): array {
if ( empty( $this->user_roles ) ) {
$roles = get_editable_roles();
$this->user_roles = array_map(
static function ( $item ) {
return $item['name'];
},
$roles
);
}
return $this->user_roles;
}
/**
* Get selected users for display in the username field.
*
* @since 1.0.0
*
* @param array $user_ids Array of user IDs.
*
* @return array
*/
private function get_selected_users( array $user_ids ): array {
if ( empty( $user_ids ) ) {
return [];
}
$users = get_users(
[
'include' => $user_ids,
'fields' => [ 'ID', 'display_name' ],
]
);
return wp_list_pluck( $users, 'display_name', 'ID' );
}
/**
* Get saved user data based on type.
*
* @since 1.0.0
*
* @param string $type Type of user data (e.g., 'roles', 'names').
* @param array $default_data Default data if no data is saved.
*
* @return array
*/
protected function get_saved_user_data( string $type, array $default_data ): array {
$saved_user_data =
$this->settings->form_data['settings']['pdfs'][ $this->pdf_id ][ 'user_' . $type . '_restrictions' ]
?? $default_data;
// Handle both string (JSON) and array formats.
if ( is_string( $saved_user_data ) ) {
$decoded_user_data = json_decode( $saved_user_data, true );
$saved_user_data = $decoded_user_data ? $decoded_user_data : $default_data;
}
return $saved_user_data;
}
}