id(); $pdf_url = get_transient('uacf7_pdf_url_' . $form_id); if ($pdf_url) { add_filter('wpcf7_ajax_json_echo', function($response) use ($pdf_url) { if (isset($response['status']) && $response['status'] === 'mail_sent') { $response['pdf_url'] = $pdf_url; } return $response; }, 10, 2); delete_transient('uacf7_pdf_url_' . $form_id); } } /* * Enqueue script Backend */ public function wp_enqueue_admin_script( $hook ) { // Only enqueue on Database page if ( $hook !== 'uacf7_page_ultimate-addons-db' ) { return; } wp_enqueue_script( 'uacf7-pdf-generator-admin', UACF7_ADDONS . '/pdf-generator/assets/js/pdf-generator-admin.js', array( 'jquery' ), '1.0.0', true ); $uacf7_pdf_settings = array(); $uacf7_pdf_settings['codeEditor'] = wp_enqueue_code_editor( array( 'type' => 'text/css' ) ); $uacf7_pdf_settings['ajaxurl'] = admin_url( 'admin-ajax.php' ); $uacf7_pdf_settings['nonce'] = wp_create_nonce( 'uacf7-pdf-generator' ); wp_localize_script( 'uacf7-pdf-generator-admin', 'uacf7_pdf_settings', $uacf7_pdf_settings ); } public function uacf7_post_meta_options_pdf_generator( $value, $post_id ) { $fields = array( 'uacf7_pdf_label' => array( 'id' => 'uacf7_pdf_label', 'type' => 'heading', 'label' => __( 'PDF Generator Settings', 'ultimate-addons-for-contact-form-7' ), 'subtitle' => sprintf( /* translators: %1$s: demo link */ __( 'Generate a PDF from submissions and send it to admin and the submitter\'s email. See Demo %1$s.', 'ultimate-addons-for-contact-form-7' ), ''.esc_html__('Example', 'ultimate-addons-for-contact-form-7').'' ) ), 'pdf_generator_docs' => array( 'id' => 'pdf_generator_docs', 'type' => 'notice', 'style' => 'success', 'content' => sprintf( /* translators: %1$s: documentation link */ __( 'Confused? Check our Documentation on %1$s.', 'ultimate-addons-for-contact-form-7' ), ''.esc_html__('PDF Generator', 'ultimate-addons-for-contact-form-7').'' ) ), 'uacf7_enable_pdf_generator' => array( 'id' => 'uacf7_enable_pdf_generator', 'type' => 'switch', 'label' => __( ' Enable PDF Generator ', 'ultimate-addons-for-contact-form-7' ), 'label_on' => __( 'Yes', 'ultimate-addons-for-contact-form-7' ), 'label_off' => __( 'No', 'ultimate-addons-for-contact-form-7' ), 'default' => false, 'field_width' => 100, ), 'pdf_generator_form_options_heading' => array( 'id' => 'pdf_generator_form_options_heading', 'type' => 'heading', 'label' => __( 'PDF Option ', 'ultimate-addons-for-contact-form-7' ), ), 'uacf7_pdf_disable_header_footer' => array( 'id' => 'uacf7_pdf_disable_header_footer', 'type' => 'checkbox', 'label' => __( 'Disable Header and Footer of PDF', 'ultimate-addons-for-contact-form-7' ), 'options' => array( 'header' => 'Disable Header', 'footer' => 'Disable Footer' ), 'field_width' => 100, 'inline' => true ), 'uacf7_pdf_name' => array( 'id' => 'uacf7_pdf_name', 'type' => 'text', 'label' => __( 'PDF Name ', 'ultimate-addons-for-contact-form-7' ), 'subtitle' => __( "For instance, if you enter 'website-submission' as the file name, the resulting PDF will be named 'website-submission.pdf'.", 'ultimate-addons-for-contact-form-7' ), 'placeholder' => __( 'E.g. website-submission', 'ultimate-addons-for-contact-form-7' ), 'field_width' => 50, ), 'pdf_send_to' => array( 'id' => 'pdf_send_to', 'type' => 'select', 'label' => __( 'PDF Send To ', 'ultimate-addons-for-contact-form-7' ), 'subtitle' => __( 'Choose whether you want both Mail 1 and Mail 2 users to receive the PDF as an attachment, or just one of them.', 'ultimate-addons-for-contact-form-7' ), 'options' => array( 'both' => 'Both', 'mail-1' => 'Mail 1', 'mail-2' => 'Mail 2', ), 'field_width' => '50' ), 'uacf7_customize_pdf_header' => array( 'id' => 'uacf7_customize_pdf_header', 'type' => 'heading', 'label' => __( 'PDF Header Settings', 'ultimate-addons-for-contact-form-7' ), ), // 'uacf7_pdf_generator_mpdf_tags' => array( // 'id' => 'uacf7_pdf_generator_mpdf_tags', // 'type' => 'notice', // 'label' => __( 'm-PDF Tags ', 'ultimate-addons-for-contact-form-7' ), // 'class' => 'tf-field-class', // 'content' => ' // {PAGENO}, {DATE j-m-Y}, {nb}, {nbpg} // ', // ), 'pdf_header_upload_image' => array( 'id' => 'pdf_header_upload_image', 'type' => 'image', 'label' => __( 'Header Image ', 'ultimate-addons-for-contact-form-7' ), ), 'pdf_header_color' => array( 'id' => 'pdf_header_color', 'type' => 'color', 'label' => __( 'Header Content Color ', 'ultimate-addons-for-contact-form-7' ), 'field_width' => 50, 'class' => 'tf-field-class', // 'default' => '#ffffff', 'multiple' => false, 'inline' => true, ), 'pdf_header_bg_color' => array( 'id' => 'pdf_header_bg_color', 'type' => 'color', 'label' => __( 'Header Background Color ', 'ultimate-addons-for-contact-form-7' ), 'field_width' => 50, 'class' => 'tf-field-class', // 'default' => '#ffffff', 'multiple' => false, 'inline' => true, ), 'customize_pdf_header' => array( 'id' => 'customize_pdf_header', 'label' => __( 'Header Content', 'ultimate-addons-for-contact-form-7' ), 'subtitle' => __( 'Some tags you can use - Page numbers & date Tags : {PAGENO}, {DATE j-m-Y}, {nb}, {nbpg}.
Use any php Date format for date shortcode {DATE format} ', 'ultimate-addons-for-contact-form-7' ), 'type' => 'editor', ), 'uacf7_customize_pdf_body' => array( 'id' => 'uacf7_customize_pdf_body', 'type' => 'heading', 'label' => __( 'PDF Body Settings', 'ultimate-addons-for-contact-form-7' ), ), 'pdf_bg_upload_image' => array( 'id' => 'pdf_bg_upload_image', 'type' => 'image', 'label' => __( 'Body Background Image ', 'ultimate-addons-for-contact-form-7' ), ), 'pdf_content_color' => array( 'id' => 'pdf_content_color', 'type' => 'color', 'label' => __( 'Body Content Color ', 'ultimate-addons-for-contact-form-7' ), 'field_width' => 50, 'class' => 'tf-field-class', // 'default' => '#ffffff', 'multiple' => false, 'inline' => true, ), 'pdf_content_bg_color' => array( 'id' => 'pdf_content_bg_color', 'type' => 'color', 'label' => __( 'Body Background Color ', 'ultimate-addons-for-contact-form-7' ), 'field_width' => 50, 'class' => 'tf-field-class', // 'default' => '#ffffff', 'multiple' => false, 'inline' => true, ), 'customize_pdf' => array( 'id' => 'customize_pdf', 'label' => __( 'Body Content ', 'ultimate-addons-for-contact-form-7' ), 'subtitle' => __( 'If you wish to include extra content in the body of the PDF.', 'ultimate-addons-for-contact-form-7' ), 'type' => 'editor', ), 'uacf7_pdf_form_tags' => array( 'id' => 'uacf7_pdf_form_tags', 'type' => 'callback', 'function' => 'uacf7_pdf_form_tags_callback', 'argument' => $post_id, ), 'uacf7_customize_pdf_footer' => array( 'id' => 'uacf7_customize_pdf_footer', 'type' => 'heading', 'label' => __( 'PDF Footer Settings', 'ultimate-addons-for-contact-form-7' ), ), // 'uacf7_pdf_footer_background_image' => array( // 'id' => 'uacf7_pdf_footer_background_image', // 'type' => 'image', // 'label' => __( 'PDF Footer Background Image ', 'ultimate-addons-for-contact-form-7' ), // ), 'pdf_footer_color' => array( 'id' => 'pdf_footer_color', 'type' => 'color', 'label' => __( 'Footer Content Color ', 'ultimate-addons-for-contact-form-7' ), 'class' => 'tf-field-class', // 'default' => '#ffffff', 'multiple' => false, 'inline' => true, 'field_width' => 50, ), 'pdf_footer_bg_color' => array( 'id' => 'pdf_footer_bg_color', 'type' => 'color', 'label' => __( 'Footer Background Color ', 'ultimate-addons-for-contact-form-7' ), 'field_width' => 50, 'class' => 'tf-field-class', // 'default' => '#ffffff', 'multiple' => false, 'inline' => true, ), 'customize_pdf_footer' => array( 'id' => 'customize_pdf_footer', 'label' => __( 'Footer Content', 'ultimate-addons-for-contact-form-7' ), 'subtitle' => __( 'Some tags you can use - Page numbers & date Tags : {PAGENO}, {DATE j-m-Y}, {nb}, {nbpg}.
Use any php Date format for date shortcode {DATE format} ', 'ultimate-addons-for-contact-form-7' ), 'type' => 'editor', ), 'uacf7_pdf_custom_css' => array( 'id' => 'uacf7_pdf_custom_css', 'type' => 'heading', 'label' => __( 'Custom CSS for PDF', 'ultimate-addons-for-contact-form-7' ), ), 'custom_pdf_css' => array( 'id' => 'custom_pdf_css', 'type' => 'code_editor', ), ); /** * Allow extensions to add/modify Webhook fields. */ $fields = apply_filters( 'uacf7_pdf_generator_fields', $fields, $post_id ); $pdf_generator = apply_filters( 'uacf7_post_meta_options_pdf_generator_pro', $data = array( 'title' => __( 'PDF Generator', 'ultimate-addons-for-contact-form-7' ), 'icon' => 'fa-solid fa-file-pdf', 'checked_field' => 'uacf7_enable_pdf_generator', 'fields' => $fields ), $post_id ); $value['pdf_generator'] = $pdf_generator; return $value; } // Generate PDF and export form ultimate db public function uacf7_get_generated_pdf() { if ( ! isset( $_POST ) || empty( $_POST ) ) { return; } // Capability check if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( 'Unauthorized', 403 ); } $ajax_nonce = isset( $_POST['ajax_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['ajax_nonce'] ) ) : ''; if ( empty( $ajax_nonce ) || ! wp_verify_nonce( $ajax_nonce, 'uacf7-pdf-generator' ) ) { wp_send_json_error( 'Security check failed', 403 ); } $form_id = ! empty( $_POST['form_id'] ) ? sanitize_text_field( wp_unslash( $_POST['form_id'] ) ) : ''; $data_id = ! empty( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : ''; require UACF7_PATH . 'third-party/vendor/autoload.php'; // Pdf get Meta Option $pdf = uacf7_get_form_option( $form_id, 'pdf_generator' ); $enable_pdf = isset( $pdf['uacf7_enable_pdf_generator'] ) ? $pdf['uacf7_enable_pdf_generator'] : 0; if ( $enable_pdf != true ) { die; } $upload_dir = wp_upload_dir(); $dir = $upload_dir['basedir']; $url = $upload_dir['baseurl']; global $wpdb; $uacf7_db = $wpdb; $data = $uacf7_db->get_row( $uacf7_db->prepare( "SELECT * FROM " . $uacf7_db->prefix . "uacf7_form WHERE id = %s AND form_id = %s", $data_id, $form_id ) ); $uacf7_pdf_name = ! empty( $pdf['uacf7_pdf_name'] ) ? $pdf['uacf7_pdf_name'] : get_the_title( $form_id ); $disable_header = ! empty( $pdf['uacf7_pdf_disable_header_footer'] ) && in_array( 'header', $pdf['uacf7_pdf_disable_header_footer'] ) ? true : false; $disable_footer = ! empty( $pdf['uacf7_pdf_disable_header_footer'] ) && in_array( 'footer', $pdf['uacf7_pdf_disable_header_footer'] ) ? true : false; $customize_pdf = ! empty( $pdf['customize_pdf'] ) ? $pdf['customize_pdf'] : ''; $pdf_bg_upload_image = ! empty( $pdf['pdf_bg_upload_image'] ) ? $pdf['pdf_bg_upload_image'] : ''; $customize_pdf_header = ! empty( $pdf['customize_pdf_header'] ) ? $pdf['customize_pdf_header'] : ''; $pdf_header_upload_image = ! empty( $pdf['pdf_header_upload_image'] ) ? $pdf['pdf_header_upload_image'] : ''; $pdf_header_img_height = ! empty( $pdf['pdf_header_img_height'] ) ? $pdf['pdf_header_img_height'] : ''; $pdf_header_img_width = ! empty( $pdf['pdf_header_img_width'] ) ? $pdf['pdf_header_img_width'] : ''; $pdf_header_img_aline = ! empty( $pdf['pdf_header_img_aline'] ) ? $pdf['pdf_header_img_aline'] : ''; $customize_pdf_footer = ! empty( $pdf['customize_pdf_footer'] ) ? $pdf['customize_pdf_footer'] : ''; $custom_pdf_css = ! empty( $pdf['custom_pdf_css'] ) ? $pdf['custom_pdf_css'] : ''; $pdf_content_color = ! empty( $pdf['pdf_content_color'] ) ? $pdf['pdf_content_color'] : '000'; $pdf_content_bg_color = ! empty( $pdf['pdf_content_bg_color'] ) ? $pdf['pdf_content_bg_color'] : ''; $pdf_header_color = ! empty( $pdf['pdf_header_color'] ) ? $pdf['pdf_header_color'] : '#fff'; $pdf_header_bg_color = ! empty( $pdf['pdf_header_bg_color'] ) ? $pdf['pdf_header_bg_color'] : '#382673'; $pdf_footer_color = ! empty( $pdf['pdf_footer_color'] ) ? $pdf['pdf_footer_color'] : '#fff'; $pdf_footer_bg_color = ! empty( $pdf['pdf_footer_bg_color'] ) ? $pdf['pdf_footer_bg_color'] : '#5D5676'; $pdf_bg_upload_image = ! empty( $pdf_bg_upload_image ) ? 'background-image: url("' . esc_attr( $pdf_bg_upload_image ) . '");' : ''; $pdf_header_upload_image = ! empty( $pdf_header_upload_image ) ? '' : ''; $mpdf = new \Mpdf\Mpdf( [ 'fontdata' => [ // lowercase letters only in font key 'dejavuserifcond' => [ 'R' => 'DejaVuSansCondensed.ttf', ] ], 'mode' => 'utf-8', 'default_font' => 'dejavusanscond', 'margin_header' => 0, 'margin_footer' => 0, 'format' => 'A4', 'margin_left' => 0, 'margin_right' => 0, 'margin_top' => 25 ] ); // PDF Style $pdf_style = ' '; // PDF Header checked( 'on', $disable_header ); if ( $disable_header != true ) { $mpdf->SetHTMLHeader( '
' . $customize_pdf_header . '
' ); } // PDF Footer if ( $disable_footer != true ) { $mpdf->SetHTMLFooter( '' ); } $replace_key = []; $repeaters = []; $repeater_value = []; $replace_value = []; $uploaded_files = []; // Call UACF7_DATABASE Class $uacf7_DB = null; $ContactForm = WPCF7_ContactForm::get_instance( $form_id ); $form_fields = $ContactForm->scan_form_tags(); $encryptionKey = 'AES-256-CBC'; $uacf7_signature_tag = []; $uacf7_file_tag = []; if ( class_exists( 'UACF7_DATABASE' ) ) { $uacf7_DB = new UACF7_DATABASE(); } // Get and store all uacf7_signature tags foreach ( $form_fields as $field ) { if ( $field->type == 'uacf7_signature*' || $field->type == 'uacf7_signature' ) { $uacf7_signature_tag[] = $field->name; } } // Get and store all CF7 File tags foreach ( $form_fields as $field ) { if ( $field->type == 'file*' || $field->type == 'file' ) { $uacf7_file_tag[] = $field->name; } } $form_value = json_decode( $data->form_value ); foreach ( $form_value as $key => $value ) { // Repeater value gate if ( strpos( $key, '__' ) !== false ) { $name_parts = explode( '__', $key ); if ( is_array( $name_parts ) ) { $repeater_value[ $name_parts[0] ][ $name_parts[1] ] = $name_parts[0]; } } if ( strpos( $key, "_count" ) !== false ) { $repeaters[] = str_replace( '_count', '', $key ); } // Signature Image Decrypt form Database Addon if ( in_array( $key, $uacf7_signature_tag ) && $uacf7_DB != null ) { $pathInfo = pathinfo( $value ); $extension = strtolower( $pathInfo['extension'] ); ob_start(); echo wp_kses_post( $uacf7_DB->decrypt_and_display( $dir . $value, $encryptionKey ) ); $decryptedData = ob_get_clean(); if ( $decryptedData !== null ) { $value = 'data:image/png;base64,' . base64_encode( $decryptedData ); } } //File tags URL set in the value if ( in_array( $key, $uacf7_file_tag ) ) { $value = $url . $value; } $replace_key[] = '[' . $key . ']'; if ( is_array( $value ) ) { $data = ''; $count_value = count( $value ); for ( $x = 0; $x < $count_value; $x++ ) { if ( $x == 0 ) { $data .= $value[ $x ]; } else { $data .= ', ' . $value[ $x ]; } } $value = $data; } $replace_value[] = $value; } // Repeater value if ( ! empty( $repeaters ) && is_array( $repeaters ) ) { $repeater_data = apply_filters( 'uacf7_pdf_generator_replace_data', $repeater_value, $repeaters, $customize_pdf ); $customize_pdf = str_replace( $repeater_data['replace_re_key'], $repeater_data['replace_re_value'], $customize_pdf ); } $pdf_content = str_replace( $replace_key, $replace_value, $customize_pdf ); $pdf_content = apply_filters( 'uacf7_pdf_generator_replace_condition_data', $pdf_content, $form_id, $form_value ); $mpdf->SetTitle( $uacf7_pdf_name ); $mpdf->WriteHTML( $pdf_style, \Mpdf\HTMLParserMode::HEADER_CSS ); // PDF Footer Content $mpdf->WriteHTML( '
' . nl2br( $this->makeLinksClickable($pdf_content) ) . '
' ); // make directory if ( ! file_exists( $dir . '/uacf7-uploads' ) ) { wp_mkdir_p( $dir . '/uacf7-uploads' ); } $pdf_dir = $dir . '/uacf7-uploads/' . $uacf7_pdf_name . '_db_.pdf'; $pdf_url = $url . '/uacf7-uploads/' . $uacf7_pdf_name . '_db_.pdf'; $mpdf->Output( $pdf_dir, 'F' ); // Dwonload wp_send_json( array( 'status' => 'success', 'url' => $pdf_url ) ); die(); } function uacf7_wpcf7_mail_components( $components, $form = null, $mail = null ) { $wpcf7 = WPCF7_ContactForm::get_current(); // Pdf get Meta Option $pdf = uacf7_get_form_option( $wpcf7->id(), 'pdf_generator' ); $enable_pdf = isset( $pdf['uacf7_enable_pdf_generator'] ) ? $pdf['uacf7_enable_pdf_generator'] : 0; $enable_pdf_download = isset( $pdf['uacf7_enable_pdf_form_download'] ) ? $pdf['uacf7_enable_pdf_form_download'] : 0; $pdf_send_to = isset( $pdf['pdf_send_to'] ) ? $pdf['pdf_send_to'] : ''; if ( ( $pdf_send_to == 'mail-1' && $mail->name() == 'mail_2' ) || ( $pdf_send_to == 'mail-2' && $mail->name() == 'mail' ) ) { return $components; } if ( $enable_pdf == true ) { $submission = WPCF7_Submission::get_instance(); $contact_form_data = $submission->get_posted_data(); $files = $submission->uploaded_files(); require UACF7_PATH . 'third-party/vendor/autoload.php'; $upload_dir = wp_upload_dir(); $time_now = time(); $dir = $upload_dir['basedir']; $upload_dir = wp_upload_dir(); $url = $upload_dir['baseurl']; $uploaded_files = []; $uacf7_dirname = $upload_dir['basedir'] . '/uacf7-uploads'; if ( ! file_exists( $uacf7_dirname ) ) { wp_mkdir_p( $uacf7_dirname ); } global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); } foreach ( array_keys( $files ) as $file_key ) { $uploaded_files[] = $file_key; } // $uacf7_pdf_name = ! empty( $pdf['uacf7_pdf_name'] ) ? $pdf['uacf7_pdf_name'] : get_the_title( $wpcf7->id() ); $disable_header = ! empty( $pdf['uacf7_pdf_disable_header_footer'] ) && in_array( 'header', $pdf['uacf7_pdf_disable_header_footer'] ) ? true : false; $disable_footer = ! empty( $pdf['uacf7_pdf_disable_header_footer'] ) && in_array( 'footer', $pdf['uacf7_pdf_disable_header_footer'] ) ? true : false; $customize_pdf = ! empty( $pdf['customize_pdf'] ) ? $pdf['customize_pdf'] : ''; $pdf_bg_upload_image = ! empty( $pdf['pdf_bg_upload_image'] ) ? $pdf['pdf_bg_upload_image'] : ''; $customize_pdf_header = ! empty( $pdf['customize_pdf_header'] ) ? $pdf['customize_pdf_header'] : ''; $pdf_header_upload_image = ! empty( $pdf['pdf_header_upload_image'] ) ? $pdf['pdf_header_upload_image'] : ''; $pdf_header_img_height = ! empty( $pdf['pdf_header_img_height'] ) ? $pdf['pdf_header_img_height'] : ''; $pdf_header_img_width = ! empty( $pdf['pdf_header_img_width'] ) ? $pdf['pdf_header_img_width'] : ''; $pdf_header_img_aline = ! empty( $pdf['pdf_header_img_aline'] ) ? $pdf['pdf_header_img_aline'] : ''; $customize_pdf_footer = ! empty( $pdf['customize_pdf_footer'] ) ? $pdf['customize_pdf_footer'] : ''; $custom_pdf_css = ! empty( $pdf['custom_pdf_css'] ) ? $pdf['custom_pdf_css'] : ''; $pdf_content_color = ! empty( $pdf['pdf_content_color'] ) ? $pdf['pdf_content_color'] : '#000'; $pdf_content_bg_color = ! empty( $pdf['pdf_content_bg_color'] ) ? $pdf['pdf_content_bg_color'] : ''; $pdf_header_color = ! empty( $pdf['pdf_header_color'] ) ? $pdf['pdf_header_color'] : '#fff'; $pdf_header_bg_color = ! empty( $pdf['pdf_header_bg_color'] ) ? $pdf['pdf_header_bg_color'] : '#382673'; $pdf_footer_color = ! empty( $pdf['pdf_footer_color'] ) ? $pdf['pdf_footer_color'] : '#fff'; $pdf_footer_bg_color = ! empty( $pdf['pdf_footer_bg_color'] ) ? $pdf['pdf_footer_bg_color'] : '#5D5676'; $pdf_bg_upload_image = ! empty( $pdf_bg_upload_image ) ? 'background-image: url("' . esc_attr( $pdf_bg_upload_image ) . '");' : ''; $pdf_header_upload_image = ! empty( $pdf_header_upload_image ) ? '' : ''; $mpdf = new \Mpdf\Mpdf( [ 'fontdata' => [ // lowercase letters only in font key 'dejavuserifcond' => [ 'R' => 'DejaVuSansCondensed.ttf', ] ], 'mode' => 'utf-8', 'default_font' => 'dejavusanscond', 'margin_header' => 0, 'margin_footer' => 0, 'format' => 'A4', 'margin_left' => 0, 'margin_right' => 0, 'margin_top' => 25, ] ); $replace_key = []; // PDF Style $pdf_style = ' '; $replace_value = []; // PDF Header if ( $disable_header != true ) { $mpdf->SetHTMLHeader( '
' . $customize_pdf_header . '
' ); } // PDF Footer if ( $disable_footer != true ) { $mpdf->SetHTMLFooter( '' ); } $repeater_value = []; foreach ( $contact_form_data as $key => $value ) { if ( ! in_array( $key, $uploaded_files ) ) { $replace_key[] = '[' . $key . ']'; // Repeater value gate if ( strpos( $key, '__' ) !== false ) { $name_parts = explode( '__', $key ); if ( is_array( $name_parts ) ) { $repeater_value[ $name_parts[0] ][ $name_parts[1] ] = $name_parts[0]; } } if ( is_array( $value ) ) { $data = ''; $count_value = count( $value ); for ( $x = 0; $x < $count_value; $x++ ) { if ( $x == 0 ) { $data .= $value[ $x ]; } else { $data .= ', ' . $value[ $x ]; } } $value = $data; } $replace_value[] = $value; } } foreach ( $files as $file_key => $file ) { if ( ! empty( $file ) ) { if ( in_array( $file_key, $uploaded_files ) ) { $file = is_array( $file ) ? reset( $file ) : $file; if ( ! uacf7_is_safe_uploaded_file( $file ) ) { continue; } if ( ! is_string( $file ) || ! file_exists( $file ) || ! is_readable( $file ) ) { continue; } $filename = wp_basename( $file ); /* * Sanitize the filename before making the security decision. */ $safe_filename = sanitize_file_name( $filename ); if ( empty( $safe_filename ) ) { continue; } /* * Validate the final filename that will actually be written. */ $file_type = wp_check_filetype( $safe_filename, array( 'jpg|jpeg|jpe' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'webp' => 'image/webp', 'pdf' => 'application/pdf', 'txt' => 'text/plain', 'doc' => 'application/msword', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', ) ); if ( empty( $file_type['ext'] ) || empty( $file_type['type'] ) ) { continue; } $dir_link = '/uacf7-uploads/' . $time_now . '-' . $file_key . '-' . $safe_filename; $destination_path = $dir . $dir_link; // Prefer WordPress filesystem methods for managed uploads; PHP copy() is kept only as a fallback // when WP_Filesystem is unavailable for direct temp or remote paths. if ( ! function_exists( 'WP_Filesystem' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $copy_success = false; if ( WP_Filesystem() ) { global $wp_filesystem; if ( $wp_filesystem instanceof WP_Filesystem_Base ) { $copy_success = $wp_filesystem->copy( $file, $destination_path, true, FS_CHMOD_FILE ); } } if ( ! $copy_success && file_exists( $file ) ) { $copy_success = copy( $file, $destination_path ); } if ( ! $copy_success ) { continue; } $file_url = $upload_dir['baseurl'] . $dir_link; $file_path = $destination_path; $replace_key[] = '[' . $file_key . ']'; // Detect extension $ext = strtolower( pathinfo( $file_url, PATHINFO_EXTENSION ) ); $image_types = [ 'jpg', 'jpeg', 'png', 'gif', 'webp' ]; if ( in_array( $ext, $image_types ) ) { // Show image preview in PDF $replace_value[] = ''; } else { // File info $filename = basename( $file_url ); $filesize = file_exists( $file_path ) ? size_format( filesize( $file_path ) ) : ''; $replace_value[] = '
' . esc_html( $filename ) . '' . ( $filesize ? ' (' . esc_html( $filesize ) . ')' : '' ) . '
Download File
'; } } } } // Repeater value $uacf7_repeaters = isset( $contact_form_data['_uacf7_repeaters'] ) ? $contact_form_data['_uacf7_repeaters'] : ''; $uacf7_repeaters = is_array( $uacf7_repeaters ) ? reset( $uacf7_repeaters ) : $uacf7_repeaters; $uacf7_repeaters = is_string( $uacf7_repeaters ) ? sanitize_text_field( $uacf7_repeaters ) : ''; if ( ! empty( $uacf7_repeaters ) ) { $repeaters = json_decode( $uacf7_repeaters ); if ( is_array( $repeaters ) || is_object( $repeaters ) ) { $repeater_data = apply_filters( 'uacf7_pdf_generator_replace_data', $repeater_value, $repeaters, $customize_pdf ); $customize_pdf = str_replace( $repeater_data['replace_re_key'], $repeater_data['replace_re_value'], $customize_pdf ); } } $pdf_content = str_replace( $replace_key, $replace_value, $customize_pdf ); // Replace extranal data using this content; $pdf_content = apply_filters( 'uacf7_pdf_generator_replace_condition_data', $pdf_content, $wpcf7->id(), $contact_form_data ); // Replace PDF Name $uacf7_pdf_name = str_replace( $replace_key, $replace_value, $uacf7_pdf_name ); $mpdf->SetTitle( $uacf7_pdf_name ); $mpdf->WriteHTML( $pdf_style, \Mpdf\HTMLParserMode::HEADER_CSS ); // PDF Footer Content $mpdf->WriteHTML( '
' . nl2br( $pdf_content) . '
' ); $pdf_dir = $dir . '/uacf7-uploads/' . $uacf7_pdf_name . '.pdf'; $pdf_url = $url . '/uacf7-uploads/' . $uacf7_pdf_name . '.pdf'; $mpdf->Output( $pdf_dir, 'F' ); if($enable_pdf_download){ // Store the PDF URL temporarily (transient or global variable) set_transient('uacf7_pdf_url_' . $wpcf7->id(), $pdf_url, 60); } $components['attachments'][] = $pdf_dir; } return $components; } public function makeLinksClickable($text) { $pattern = '/(https?:\/\/[^\s]+)/'; return preg_replace($pattern, '$1', $text); } // Import Export public function uacf7_post_meta_import_export_pdf_generator( $imported_data, $form_id ) { if ( isset( $imported_data['pdf_generator'] ) && function_exists( 'uacf7_import_export_file_upload' ) ) { $imported_data['pdf_generator']['pdf_bg_upload_image'] = uacf7_import_export_file_upload( $imported_data['pdf_generator']['pdf_bg_upload_image'] ); } if ( isset( $imported_data['pdf_generator'] ) && function_exists( 'uacf7_import_export_file_upload' ) ) { $imported_data['pdf_generator']['pdf_header_upload_image'] = uacf7_import_export_file_upload( $imported_data['pdf_generator']['pdf_header_upload_image'] ); } return $imported_data; } } new UACF7_PDF_GENERATOR();