Descrizione
Il deleteuploadedfiles
La classe è progettata per eliminare i file caricati in seguito alle notifiche via e-mail. Questa funzionalità è particolarmente utile quando la memorizzazione dei dati e il salvataggio nella Libreria multimediale sono disabilitati.
WPForms sconsiglia vivamente di modificare direttamente i file del tema padre. Qualsiasi modifica apportata direttamente al tema principale rischia di essere sovrascritta durante gli aggiornamenti del tema, con conseguente perdita delle personalizzazioni. Si consiglia di creare e utilizzare un tema figlio per apportare le modifiche desiderate.
Metodi
- Stabilire le dipendenze per supportare la classe, generando un file chiamato
class-delete-uploaded-files.php
nella cartella principale del tema. - Dopo aver creato questo file, aprire il file
functions.php
e inserire il seguente snippet. Salvare le modifiche nel file delle funzioni.richiedere __DIR__ . '/class-delete-uploaded-files.php'; ( new \WPF\DeleteUploadedFiles() )->hooks();
Fonte
File: class-delete-uploaded-files.php
/** * Class deleteuploadedfiles to delete uploaded files from the server * * @link https://wpforms.com/developers/class-deleteuploadedfiles/ */ <?php namespace WPF; class DeleteUploadedFiles { /** * Should we delete files from the WordPress Media Library? * Change the constant to true to delete files form the WordPress Media Library. */ const DELETE_MEDIA_FILES = false; /** * Add hooks. */ public function hooks() { add_action( 'wpforms_process_complete', [ $this, 'delete_attached_files' ], 10, 4 ); add_filter( 'wpforms_html_field_value', [ $this, 'replace_html_field_value' ], 100, 4 ); add_filter( 'wpforms_emails_notifications_plaintext_field_value', [ $this, 'replace_plaintext_field_value' ], 100, 3 ); add_filter( 'wpforms_smarttags_process_field_id_value', [ $this, 'replace_field_id_smart_tags_value' ], 10, 5 ); add_filter( 'wpforms_smarttags_process_field_value_id_value', [ $this, 'replace_field_value_id_smart_tags_value' ], 10, 5 ); } /** * Delete attached files after emails are sent if these files aren't stored in entries. * * @param array $fields Fields data. * @param array $entry Form submission raw data ($_POST). * @param array $form_data Form data and settings. * @param int $entry_id Entry ID. */ public function delete_attached_files( $fields, $entry, $form_data, $entry_id ) { if ( ! $this->is_allowed_delete_files_after_sending_email( $form_data ) ) { return; } $delete_files_fields = $this->get_filtered_attached_files_fields( $form_data ); if ( empty( $delete_files_fields ) ) { return; } foreach ( $delete_files_fields as $field_id ) { if ( empty( $fields[ $field_id ] ) ) { continue; } $field = $fields[ $field_id ]; if ( self::DELETE_MEDIA_FILES ) { if ( ! empty( $field[ 'style' ] ) && $field[ 'style' ] === 'modern' ) { $is_media_field = false; foreach ( $field[ 'value_raw' ] as $file ) { if ( ! empty( $file[ 'attachment_id' ] ) ) { wp_delete_attachment( $file[ 'attachment_id' ], true ); $is_media_field = true; } } if ( $is_media_field ) { continue; } } if ( ! empty( $field[ 'attachment_id' ] ) ) { wp_delete_attachment( $field[ 'attachment_id' ], true ); continue; } } $files_paths = \WPForms_Field_File_Upload::get_entry_field_file_paths( $form_data[ 'id' ], $field ); if ( empty( $files_paths ) ) { continue; } foreach ( $files_paths as $path ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.unlink_unlink @unlink( $path ); } } } /** * Replace field value with (attached) text if file is attached * to email and will be deleted after sending HTML email. * * @param string $field_val Field value. * @param array $field The field. * @param array $form_data Form data and settings. * @param string $context Context usage. * * @return string */ public function replace_html_field_value( $field_val, $field, $form_data, $context ) { if ( $context !== 'email-html' ) { return $field_val; } if ( empty( $field[ 'type' ] ) || $field[ 'type' ] !== 'file-upload' ) { return $field_val; } if ( ! $this->is_allowed_delete_files_after_sending_email( $form_data ) ) { return $field_val; } $delete_files_fields = $this->get_filtered_attached_files_fields( $form_data ); if ( empty( $field[ 'id' ] ) || ! in_array( $field[ 'id' ], $delete_files_fields, true ) ) { return $field_val; } if ( ! empty( $field[ 'style' ] ) && $field[ 'style' ] === 'modern' ) { $files = []; foreach ( $field['value_raw'] as $file ) { $file_name = $file['file'] ?? ''; $files[] = sprintf( '%1$s (%2$s)', $file_name, __( 'attached', 'wpforms' ) ); } return implode( "\n", $files ); } $file_name = $field[ 'file' ] ?? ''; return sprintf( '%1$s (%2$s)', $file_name, __( 'attached', 'wpforms' ) ); } /** * Replace field value with (attached) text if file is attached * to email and will be deleted after sending text email. * * @param string $field_val Field value. * @param array $field The field. * @param array $form_data Form data and settings. * * @return string */ public function replace_plaintext_field_value( $field_val, $field, $form_data ) { $replaced_value = $this->replace_html_field_value( $field_val, $field, $form_data, 'email-html' ); if ( $replaced_value !== $field ) { return $replaced_value . "\r\n\r\n"; } return $field_val; } /** * Replace {field_id} smart tag value with (attached) text if file is attached * to email and will be deleted after sending text email. * * @param scalar|null $value Smart Tag value. * @param array $form_data Form data. * @param array $fields List of fields. * @param int $entry_id Entry ID. * @param SmartTag $smart_tag_object The smart tag object or the Generic object for those cases when class unregistered. * * @return string */ public function replace_field_id_smart_tags_value( $value, $form_data, $fields, $entry_id, $smart_tag_object ) { $attributes = $smart_tag_object->get_attributes(); if ( empty( $attributes[ 'field_id' ] ) ) { return $value; } if ( empty( $fields[ $attributes[ 'field_id' ] ] ) ) { return $value; } return $this->replace_html_field_value( $value, $fields[ $attributes[ 'field_id' ] ], $form_data, 'email-html' ); } /** * Replace {field_value_id} smart tag value with (attached) text if file is attached * to email and will be deleted after sending text email. * * @param scalar|null $value Smart Tag value. * @param array $form_data Form data. * @param array $fields List of fields. * @param int $entry_id Entry ID. * @param SmartTag $smart_tag_object The smart tag object or the Generic object for those cases when class unregistered. * * @return string */ public function replace_field_value_id_smart_tags_value( $value, $form_data, $fields, $entry_id, $smart_tag_object ) { $attributes = $smart_tag_object->get_attributes(); if ( empty( $attributes[ 'field_value_id' ] ) ) { return $value; } if ( empty( $fields[ $attributes[ 'field_value_id' ] ] ) ) { return $value; } return $this->replace_html_field_value( $value, $fields[ $attributes[ 'field_value_id' ] ], $form_data, 'email-html' ); } /** * Determine if the files should be deleted after sending email. * * @param array $form_data Form data and settings. * * @return bool */ private function is_allowed_delete_files_after_sending_email( $form_data ) { return ! empty( $form_data[ 'settings' ][ 'disable_entries' ] ) && ! empty( $form_data[ 'settings' ][ 'notifications' ] ); } /** * Get fields IDs that are attached to email and will be deleted after sending email. * * @param array $form_data Form data and settings. * * @return array */ private function get_filtered_attached_files_fields( $form_data ) { if ( empty( $form_data[ 'fields' ] ) || empty( $form_data[ 'settings' ][ 'notifications' ] ) ) { return []; } $fields = $this->get_all_attached_files_fields( $form_data ); foreach ( $fields as $key => $field_id ) { if ( empty( $form_data[ 'fields' ][ $field_id ][ 'type' ] ) || $form_data['fields'][ $field_id ][ 'type' ] !== 'file-upload' ) { unset( $fields[ $key ] ); } if ( ! self::DELETE_MEDIA_FILES && ! empty( $form_data[ 'fields' ][ $field_id ][ 'media_library' ] ) ) { unset( $fields[ $key ] ); } } return $fields; } /** * Get fields IDs that are attached to email. * * @param array $form_data Form data and settings. * * @return array */ private function get_all_attached_files_fields( $form_data ) { $is_first = true; $fields = []; foreach ( $form_data[ 'settings' ][ 'notifications' ] as $notification ) { if ( empty( $notification[ 'file_upload_attachment_enable' ] ) || empty( $notification[ 'file_upload_attachment_fields' ] ) ) { return []; } if ( $is_first ) { $fields = $notification[ 'file_upload_attachment_fields' ]; $is_first = false; continue; } $fields = array_intersect( $fields, $notification[ 'file_upload_attachment_fields' ] ); } return $fields; } }
Utilizzo
Per utilizzare questa Classe, sono necessari alcuni requisiti di configurazione.
Le voci sono disabilitate
Per disattivare la memorizzazione delle voci, selezionare Impostazioni " Generale. Confermare il Disattivare la memorizzazione delle informazioni sulle voci in WordPress è abilitato.
Disattivare la memorizzazione dei caricamenti nella Libreria multimediale
Quindi, assicuratevi che nel campo Caricamento file sia disattivata l'opzione Memorizza file nella libreria multimediale di WordPress, facendo clic sul campo e nella scheda Avanzate, assicuratevi che l'impostazione sia disattivata.
Allegare un file alle notifiche via e-mail
Infine, l'ultimo passo consiste nell'assicurarsi che gli allegati dei file siano abilitati alla notifica via e-mail. Per completare questo passaggio, andare su Impostazioni " Notifiche e, nelle opzioni Avanzate, abilitare l'opzione Abilita allegati di caricamento file. Dal menu a tendina Campi di caricamento file selezionare il campo di caricamento.
Una volta completate queste impostazioni, qualsiasi caricamento di file sui moduli allegherà i file caricati, ma non li memorizzerà sul server.
Aggiuntivo (facoltativo)
Inoltre, questa classe permette di rimuovere i file caricati dalla libreria multimediale di WP. Questa funzione è disattivata per impostazione predefinita, in quanto alcuni utenti potrebbero preferire mantenere i file caricati in WP Media, soprattutto quando si utilizza Aggiunta di contributi per i post e che richiede l'archiviazione dei file per le miniature. Tuttavia, se gli utenti desiderano eliminare sia i file caricati che quelli associati dalla libreria multimediale di WP, devono modificare il parametro DELETE_MEDIA_FILES
costante nel class-delete-uploaded-files.php
a true
.