Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image upload with gallery extension #1741

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions assets/js/add-listing.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions assets/js/all-listings.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 29 additions & 16 deletions assets/src/js/global/add-listing.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,6 @@ $(document).ready(function () {
// -----------------------------
// Submit The Form
// -----------------------------
let uploadedImages = [];

$('body').on('submit', '#directorist-add-listing-form', function (e) {
e.preventDefault();

Expand All @@ -467,6 +465,7 @@ $(document).ready(function () {

// images
let selectedImages = [];
let uploadedImages = [];

if (mediaUploaders.length) {
for (var uploader of mediaUploaders) {
Expand All @@ -486,7 +485,12 @@ $(document).ready(function () {
break;
}

selectedImages = uploader.media_uploader.getTheFiles();
uploader.media_uploader.getTheFiles().forEach( function( file ) {
selectedImages.push( {
field: uploader.uploaders_data.meta_name,
file: file
} );
} );
}
}

Expand All @@ -498,7 +502,8 @@ $(document).ready(function () {

formData.append( 'action', 'directorist_upload_listing_image' );
formData.append( 'directorist_nonce', directorist.directorist_nonce );
formData.append( 'image', selectedImages[ counter ] );
formData.append( 'image', selectedImages[ counter ].file );
formData.append( 'field', selectedImages[ counter ].field );

$.ajax( {
method: 'POST',
Expand Down Expand Up @@ -530,7 +535,10 @@ $(document).ready(function () {
return;
}

uploadedImages.push( response.data );
uploadedImages.push( {
field: selectedImages[ counter ].field,
file: response.data
} );

counter++;

Expand Down Expand Up @@ -564,7 +572,6 @@ $(document).ready(function () {

form_data.append('action', 'add_listing_action');
form_data.append('directorist_nonce', directorist.directorist_nonce);
form_data.append('listing_img', uploadedImages );

disableSubmitButton();

Expand All @@ -575,20 +582,19 @@ $(document).ready(function () {
form_data.append( field.name, field.value );
}

//images
if (mediaUploaders.length) {
for (var uploader of mediaUploaders) {
if (!uploader.media_uploader || $(uploader.media_uploader.container).parents('form').get(0) !== $form.get(0)) {
// Upload existing image
if ( mediaUploaders.length ) {
for ( let uploader of mediaUploaders ) {
if ( ! uploader.media_uploader || $(uploader.media_uploader.container).parents('form').get(0) !== $form.get(0) ) {
continue;
}

if (uploader.media_uploader.hasValidFiles()) {
var files_meta = uploader.media_uploader.getFilesMeta();
if (files_meta) {
for (var i = 0; i < files_meta.length; i++) {
form_data.append(`listing_img_old[${i}]`, files_meta[i].attachmentID);
if ( uploader.media_uploader.hasValidFiles() ) {
uploader.media_uploader.getFilesMeta().forEach( function( file_meta ) {
if ( file_meta.attachmentID ) {
form_data.append(`${uploader.uploaders_data.meta_name}_old[]`, file_meta.attachmentID);
}
}
} );
} else {
err_log.listing_gallery = {
msg: uploader.uploaders_data['error_msg']
Expand All @@ -603,6 +609,13 @@ $(document).ready(function () {
}
}

// Upload new image
if ( uploadedImages.length ) {
uploadedImages.forEach( function( image ) {
form_data.append(`${image.field}[]`, image.file);
} );
}

// categories
const categories = $form.find('#at_biz_dir-categories').val();
if ( Array.isArray( categories ) && categories.length ) {
Expand Down
11 changes: 8 additions & 3 deletions includes/classes/class-add-listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function upload_listing_image() {
throw new Exception( sprintf( __( 'Could not upload (%s), please try again.', 'directorist' ), $image['name'] ), 500 );
}

wp_send_json_success( explode( 'directorist_temp_uploads/', $status['url'] )[1] );
wp_send_json_success( basename( $status['url'] ) );

} catch ( Exception $e ) {

Expand All @@ -100,7 +100,7 @@ public static function upload_listing_image() {
}

public static function set_temporary_upload_dir( $upload ) {
$upload['subdir'] = '/directorist_temp_uploads';
$upload['subdir'] = '/directorist_temp_uploads/' . date( 'nj' );
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];

Expand Down Expand Up @@ -518,6 +518,11 @@ public static function upload_images( $listing_id, $posted_data ) {
$selected_images = Fields::create( $image_upload_field )->get_value( $posted_data );

if ( is_null( $selected_images ) ) {
// Cleanup listing meta when images field is empty.
delete_post_thumbnail( $listing_id );
delete_post_meta( $listing_id, '_listing_img' );
delete_post_meta( $listing_id, '_listing_prv_img' );

return;
}

Expand All @@ -532,7 +537,7 @@ public static function upload_images( $listing_id, $posted_data ) {

try {
$upload_dir = wp_get_upload_dir();
$temp_dir = $upload_dir['basedir'] . '/directorist_temp_uploads/';
$temp_dir = $upload_dir['basedir'] . '/directorist_temp_uploads/' . date( 'nj' ) . '/';
$target_dir = trailingslashit( $upload_dir['path'] );
$uploaded_images = $old_images;
$background_processable_images = array();
Expand Down
14 changes: 14 additions & 0 deletions includes/classes/class-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function __construct() {
add_filter( 'cron_schedules', array( $this, 'atbdp_cron_init' ) );

add_action( 'edit_post', array( $this, 'update_atbdp_schedule_tasks' ), 10, 2 );

add_action( 'directorist_cleanup_temporary_uploads', array( $this, 'cleanup_temporary_uploads' ) );
}

// update_atbdp_schedule_tasks
Expand Down Expand Up @@ -85,6 +87,10 @@ public function atbdp_custom_schedule_cron() {
if ( ! wp_next_scheduled( 'directorist_hourly_scheduled_events' ) ) {
wp_schedule_event( time(), 'atbdp_listing_manage', 'directorist_hourly_scheduled_events' );
}

if ( ! wp_next_scheduled( 'directorist_cleanup_temporary_uploads' ) ) {
wp_schedule_event( time(), 'daily', 'directorist_cleanup_temporary_uploads' );
}
}

/**
Expand Down Expand Up @@ -463,6 +469,14 @@ private function delete_expired_listings() {
}
}
}

public function cleanup_temporary_uploads() {
directorist_delete_temporary_upload_dirs();

if ( ! wp_next_scheduled( 'directorist_cleanup_temporary_uploads' ) ) {
wp_schedule_event( time(), 'daily', 'directorist_cleanup_temporary_uploads' );
}
}
}

endif;
2 changes: 2 additions & 0 deletions includes/fields/class-directorist-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public static function translate_key_to_field( $type ) {
'zip' => 'text',
);

$map = apply_filters( 'directorist_listing_form_fields_class_map', $map );

return isset( $map[ $type ] ) ? $map[ $type ] : $type;
}
}
4 changes: 2 additions & 2 deletions includes/fields/class-directorist-image-upload-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function get_value( $posted_data ) {
return null;
}

$new_images = directorist_get_var( $posted_data[ $this->get_key() ], '' );
$new_images = (array) directorist_get_var( $posted_data[ $this->get_key() ], array() );
$old_images = (array) directorist_get_var( $posted_data[ $this->get_key() . '_old' ], array() );

return array(
'new' => array_filter( explode( ',', $new_images ) ),
'new' => array_filter( $new_images ),
'old' => array_filter( wp_parse_id_list( $old_images ) ),
);
}
Expand Down
Loading