@@ -56,6 +56,9 @@ public function __construct() {
5656 // Removing wpautop isn't enough if form is used inside a ACF field or so.
5757 // Fitting the output to one line prevents <br> tags from appearing.
5858 add_filter ( 'wplf_form ' , array ( $ this , 'minify_html ' ) );
59+
60+ // before delete, remove the possible uploads
61+ add_action ( 'before_delete_post ' , array ( $ this , 'clean_up_entry ' ) );
5962 }
6063
6164 public static function register_cpt () {
@@ -127,10 +130,30 @@ public function disable_tinymce( $default ) {
127130 if ( 'wplf-form ' === get_post_type ( $ post ) ) {
128131 return false ;
129132 }
130-
131133 return $ default ;
132134 }
133135
136+ /**
137+ * Berore permanently deleting form entry, remove attachments
138+ * in the case they were not added to media library
139+ */
140+ public function clean_up_entry ( $ id ) {
141+ $ type = get_post_type ( $ id );
142+ if ( 'wplf-submission ' === $ type ) {
143+ $ postmeta = get_post_meta ( $ id );
144+
145+ foreach ( $ postmeta as $ key => $ meta ) {
146+ $ m = $ meta [0 ];
147+ if ( strpos ( $ key , 'attachment ' ) !== false ) {
148+ $ path = str_replace ( WP_HOME . '/ ' , get_home_path (), $ m );
149+ unlink ( $ path );
150+
151+ }
152+ }
153+ }
154+
155+ }
156+
134157 /**
135158 * Include custom JS and CSS on the edit screen
136159 */
@@ -274,6 +297,15 @@ public function add_meta_boxes_cpt() {
274297 'high '
275298 );
276299
300+ // Media library meta box
301+ add_meta_box (
302+ 'wplf-media ' ,
303+ __ ( 'Files ' , 'wp-libre-form ' ),
304+ array ( $ this , 'metabox_media_library ' ),
305+ 'wplf-form ' ,
306+ 'side '
307+ );
308+
277309 // Form Fields meta box
278310 add_meta_box (
279311 'wplf-fields ' ,
@@ -320,7 +352,8 @@ function metabox_thank_you( $post ) {
320352 // get post meta
321353 $ meta = get_post_meta ( $ post ->ID );
322354 $ message = isset ( $ meta ['_wplf_thank_you ' ] ) ?
323- $ meta ['_wplf_thank_you ' ][0 ] : _x ( 'Success! ' , 'Default success message ' , 'wp-libre-form ' );
355+ $ meta ['_wplf_thank_you ' ][0 ]
356+ : _x ( 'Success! ' , 'Default success message ' , 'wp-libre-form ' );
324357?>
325358<p>
326359<?php wp_editor ( esc_textarea ( $ message ), 'wplf_thank_you ' , array (
@@ -329,12 +362,28 @@ function metabox_thank_you( $post ) {
329362 'textarea_name ' => 'wplf_thank_you ' ,
330363 'textarea_rows ' => 6 ,
331364 'teeny ' => true ,
332- )); ?>
365+ ) ); ?>
333366</p>
334367<?php
335368 wp_nonce_field ( 'wplf_form_meta ' , 'wplf_form_meta_nonce ' );
336369 }
337370
371+ /**
372+ * Meta box callback for should files end up in media library
373+ */
374+ public function metabox_media_library ( $ post ) {
375+ $ meta = get_post_meta ( $ post ->ID );
376+ $ checked = 'checked ' ;
377+
378+ if ( isset ( $ meta ['_wplf_media_library ' ] ) && empty ( $ meta ['_wplf_media_library ' ][0 ] ) ) {
379+ $ checked = '' ;
380+ }
381+
382+ echo "<input type='checkbox' " . esc_html ( $ checked ) . " name='wplf_media_library'> " ;
383+ ?>
384+ <label><?php esc_attr_e ( 'Add files to media library ' , 'wp-libre-form ' ); ?> </label>
385+ <?php
386+ }
338387
339388 /**
340389 * Meta box callback for form fields meta box
@@ -585,10 +634,12 @@ protected function maybe_persist_override_template( $template, $form_id, $force
585634 // Safe-guard to prevent accidental infinite loops.
586635 remove_action ( 'save_post ' , array ( $ this , 'save_cpt ' ) );
587636
588- $ updated = wp_update_post ( array (
637+ $ updated = wp_update_post (
638+ array (
589639 'ID ' => (int ) $ form_id ,
590640 'post_content ' => $ template ,
591- ) );
641+ )
642+ );
592643
593644 add_action ( 'save_post ' , array ( $ this , 'save_cpt ' ) );
594645
@@ -619,6 +670,13 @@ public function save_cpt( $post_id ) {
619670 return ;
620671 }
621672
673+ // save media checkbox
674+ if ( isset ( $ _POST ['wplf_media_library ' ] ) ) {
675+ update_post_meta ( $ post_id , '_wplf_media_library ' , $ _POST ['wplf_media_library ' ] );
676+ } else {
677+ update_post_meta ( $ post_id , '_wplf_media_library ' , '' );
678+ }
679+
622680 // save success message
623681 if ( isset ( $ _POST ['wplf_thank_you ' ] ) ) {
624682 $ success = wp_kses_post ( $ _POST ['wplf_thank_you ' ] );
@@ -815,7 +873,7 @@ public function maybe_enqueue_frontend_script() {
815873 );
816874
817875 // add dynamic variables to the script's scope
818- wp_localize_script ( 'wplf-form-js ' , 'ajax_object ' , apply_filters ( 'wplf_ajax_object ' , array (
876+ wp_localize_script ('wplf-form-js ' , 'ajax_object ' , apply_filters ( 'wplf_ajax_object ' , array (
819877 'ajax_url ' => admin_url ( 'admin-ajax.php ' ),
820878 'ajax_credentials ' => apply_filters ( 'wplf_ajax_fetch_credentials_mode ' , 'same-origin ' ),
821879 'wplf_assets_dir ' => plugin_dir_url ( realpath ( __DIR__ . '/../wp-libre-form.php ' ) ) . 'assets ' ,
0 commit comments