説明
について deleteuploadedfiles
このクラスは、電子メール通知後にアップロードされたファイルを削除するように設計されています。この機能は、エントリーの保存とメディアライブラリへの保存が無効になっている場合に特に便利です。
WPFormsは親テーマのファイルを直接変更することを強く推奨しません。親テーマに直接行われた変更は、テーマの更新中に上書きされる危険性があり、その結果カスタマイズが失われます。子テーマを作成して使用することをお勧めします。
方法
- という名前のファイルを生成して、クラスをサポートするための依存関係を確立します。
class-delete-uploaded-files.php
をテーマのルート・ディレクトリに置く。 - このファイルを作成したら
functions.php
に以下のスニペットを挿入する。変更を関数ファイルに保存します。require __DIR__ .'/class-delete-uploaded-files.php'; ( new \WPFDeleteUploadedFiles() )->hooks();
ソース
ファイル 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; } }
使用方法
このクラスを利用するには、一定のセットアップ条件があります。
エントリーは無効
エントリーの保存を無効にするには 設定 " 全般.を確認する。 WordPressにエントリー情報を保存しない が有効になっている。
メディアライブラリへのアップロード保存を無効にする
次に、ファイルアップロードフィールドをクリックして、WordPressメディアライブラリにファイルを保存する設定が無効になっていることを確認し、詳細設定タブで、その設定が無効になっていることを確認する。
通知メールにファイルを添付
最後に、ファイル添付がEメール通知で有効になっていることを確認します。このステップを完了するには、設定 " 通知に移動し、詳細オプションの下にあるファイルのアップロード添付を有効にします。ファイルアップロードフィールドのドロップダウンからアップロードフィールドを選択します。
これらの設定が完了すると、フォームにファイルをアップロードしても、アップロードされたファイルは添付されますが、サーバーには保存されません。
追加(オプション)
さらに、このクラスは、WP Media Libraryからアップロードされたファイルを削除することを許可します。この機能はデフォルトではオフになっています。 投稿アドオン とサムネイル用のファイルストレージを必要とします。とはいえ、WP Media Libraryからアップロードされたファイルと関連ファイルの両方を削除したい場合は、WP Media Libraryの DELETE_MEDIA_FILES
の定数である。 class-delete-uploaded-files.php
ファイルを true
.