Skip to content

Commit 201433c

Browse files
henccak1sul1
authored andcommitted
Optional media files (#103)
* Files upload into current upload folder * Added removal of uploads, in case they were not added to library conventionally * Made 'add files to library' on by default, so to keep functionality consistent after update
1 parent f4f535e commit 201433c

8 files changed

Lines changed: 111 additions & 26 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
/vendor/
33
!/lang/.gitkeep
4+
/uploads/

classes/class-cpt-wplf-form.php

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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',

classes/class-cpt-wplf-submission.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public function __construct() {
3434
// add custom bulk actions
3535
add_action( 'admin_notices', array( $this, 'wplf_submission_bulk_action_admin_notice' ) );
3636
add_filter( 'bulk_actions-edit-wplf-submission', array( $this, 'register_wplf_submission_bulk_actions' ) );
37-
add_filter( 'handle_bulk_actions-edit-wplf-submission',
38-
array( $this, 'wplf_submission_bulk_action_handler' ), 10, 3 );
37+
add_filter( 'handle_bulk_actions-edit-wplf-submission', array( $this, 'wplf_submission_bulk_action_handler' ), 10, 3 );
3938
}
4039

4140
public static function register_cpt() {
@@ -199,14 +198,16 @@ public function wplf_submission_bulk_action_admin_notice() {
199198
'<div id="wplf-submission-bulk-resend-message" class="notice notice-success"><p>' .
200199
esc_html(
201200
// translators: %s is number of submissions
202-
_n( 'Resent email copy of %s submission.',
203-
'Resent email copy of %s submissions.',
204-
$count,
201+
_n(
202+
'Resent email copy of %s submission.',
203+
'Resent email copy of %s submissions.',
204+
$count,
205205
'wp-libre-form'
206-
)
206+
)
207207
) .
208208
'</p></div>',
209-
intval( $count ) );
209+
intval( $count )
210+
);
210211
}
211212
}
212213

inc/wplf-ajax.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
add_action( 'wp_ajax_wplf_submit', 'wplf_ajax_submit_handler' );
77
add_action( 'wp_ajax_nopriv_wplf_submit', 'wplf_ajax_submit_handler' );
88
function wplf_ajax_submit_handler() {
9+
910
$return = new stdClass();
1011
$return->ok = 1;
1112

@@ -48,7 +49,7 @@ function wplf_ajax_submit_handler() {
4849
'post_title' => $post_title,
4950
'post_status' => 'publish',
5051
'post_type' => 'wplf-submission',
51-
));
52+
) );
5253

5354
// add submission data as meta values
5455
foreach ( $_POST as $key => $value ) {
@@ -60,16 +61,27 @@ function wplf_ajax_submit_handler() {
6061
}
6162

6263
// handle files
64+
$uploads_path = wp_upload_dir();
65+
$should_store_images_in_medialibrary = get_post_meta( $form->ID, '_wplf_media_library', true );
66+
$counter = 0;
6367
foreach ( $_FILES as $key => $file ) {
6468
// Is this enough security wise?
6569
// Currenly only supports 1 file per input
66-
$attach_id = media_handle_upload( $key, 0, array(), array(
67-
'test_form' => false,
68-
) );
70+
if ( $should_store_images_in_medialibrary ) {
71+
$attach_id = media_handle_upload( $key, 0, array(), array(
72+
'test_form' => false,
73+
) );
74+
75+
if ( ! is_wp_error( $attach_id ) ) {
76+
add_post_meta( $post_id, $key, wp_get_attachment_url( $attach_id ) );
77+
add_post_meta( $post_id, $key . '_attachment', $attach_id );
78+
}
79+
} else {
80+
$name = 'lf_' . date( 'ymdhs' ) . '-' . $counter . '-' . sanitize_file_name( $file['name'] );
6981

70-
if ( ! is_wp_error( $attach_id ) ) {
71-
add_post_meta( $post_id, $key, wp_get_attachment_url( $attach_id ) );
72-
add_post_meta( $post_id, $key . '_attachment', $attach_id );
82+
move_uploaded_file( $file['tmp_name'], $uploads_path['path'] . '/' . $name );
83+
add_post_meta( $post_id, $key . '_attachment', $uploads_path['url'] . '/' . $name );
84+
$counter++;
7385
}
7486
}
7587

inc/wplf-form-actions.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,14 @@ function wplf_email_copy_replace_tags( $content, $form = null, $submission_id =
119119

120120
$defaults_store = array(
121121
'submission-id' => $submission_id,
122-
'referrer' => esc_url_raw( ( null !== $submission_id ) ? get_post_meta( $submission_id, 'referrer', true ) : $_POST['referrer'] ),
122+
'referrer' => esc_url_raw( ( null !== $submission_id )
123+
? get_post_meta( $submission_id, 'referrer', true )
124+
: $_POST['referrer'] ),
123125
'form-title' => esc_html( get_the_title( $form ) ),
124126
'form-id' => $form->ID,
125-
'user-id' => ( null !== get_current_user_id() ) ? wp_get_current_user()->display_name . ' (ID ' . get_current_user_id() . ')' : __( 'No user logged in', 'wp-libre-form' ),
127+
'user-id' => ( null !== get_current_user_id() )
128+
? wp_get_current_user()->display_name . ' (ID ' . get_current_user_id() . ')'
129+
: __( 'No user logged in', 'wp-libre-form' ),
126130
'timestamp' => current_time( 'mysql' ),
127131
'datetime' => current_time( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ),
128132
'language' => ( function_exists( 'pll_current_language' ) ) ? pll_current_language( 'locale' ) : get_locale(),
@@ -141,7 +145,7 @@ function wplf_email_copy_replace_tags( $content, $form = null, $submission_id =
141145

142146
if ( isset( $fields[ $string ] ) ) {
143147
$value = $fields[ $string ][0];
144-
} else if ( isset( $defaults_store[ $string ] ) ) {
148+
} elseif ( isset( $defaults_store[ $string ] ) ) {
145149
$value = $defaults_store[ $string ];
146150
}
147151

lang/wp-libre-form-fi.mo

134 Bytes
Binary file not shown.

lang/wp-libre-form-fi.po

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ msgid ""
22
msgstr ""
33
"Project-Id-Version: WP Libre Form\n"
44
"POT-Creation-Date: 2017-10-25 12:40+0300\n"
5-
"PO-Revision-Date: 2017-10-25 12:46+0300\n"
5+
"PO-Revision-Date: 2018-03-14 09:47+0200\n"
66
"Last-Translator: Timi Wahalahti <timi@dude.fi>\n"
77
"Language-Team: Antti Kuosmanen <antti@seravo.fi>\n"
88
"Language: fi\n"
99
"MIME-Version: 1.0\n"
1010
"Content-Type: text/plain; charset=UTF-8\n"
1111
"Content-Transfer-Encoding: 8bit\n"
1212
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
13-
"X-Generator: Poedit 2.0.4\n"
13+
"X-Generator: Poedit 1.8.7\n"
1414
"X-Poedit-Basepath: ..\n"
1515
"X-Poedit-WPHeader: wp-libre-form.php\n"
1616
"X-Poedit-SourceCharset: UTF-8\n"
@@ -138,6 +138,10 @@ msgstr "Onnistumisviesti"
138138
msgid "Form Fields Detected"
139139
msgstr "Lomakekentät"
140140

141+
#: classes/class-cpt-wplf-form.php:279
142+
msgid "Files"
143+
msgstr "Tiedostot"
144+
141145
#: classes/class-cpt-wplf-form.php:283
142146
msgid "Emails"
143147
msgstr "Sähköpostit"
@@ -155,6 +159,10 @@ msgstr "Kiitos!"
155159
msgid "Fields marked with * are required"
156160
msgstr "Tähdelliset * kentät on pakollisia"
157161

162+
#: classes/class-cpt-wplf-form.php:355
163+
msgid "Add files to media library"
164+
msgstr "Lisää tiedostoja mediakirjastoon"
165+
158166
#: classes/class-cpt-wplf-form.php:374
159167
msgid "Send an email copy when a form is submitted?"
160168
msgstr "Lähetä kopio sähköpostiin kun lomake lähetetään?"

wp-libre-form.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* Plugin name: WP Libre Form
4-
* Plugin URI: https://github.com/anttiviljami/wp-libre-form
4+
* Plugin URI: https://github.com/hencca/wp-libre-form
55
* Description: A minimal HTML form builder for WordPress; made for developers
66
* Version: 1.4.3
77
* Author: @anttiviljami
@@ -29,6 +29,7 @@
2929
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3030
*/
3131

32+
3233
if ( ! class_exists( 'WP_Libre_Form' ) ) :
3334

3435
define( 'WPLF_VERSION', '1.4.3' );

0 commit comments

Comments
 (0)