Skip to content
Open
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
79 changes: 45 additions & 34 deletions includes/forms/class-wp-job-manager-form-submit-job.php
Original file line number Diff line number Diff line change
Expand Up @@ -912,12 +912,14 @@ protected function save_job( $post_title, $post_content, $status = 'preview', $v
}

/**
* Creates a file attachment.
* Creates an attachment for the given URL and attaches it to the current job.
*
* @param string $attachment_url
* @return int attachment id.
* @param string $attachment_url URL of the file to attach.
* @param string|null $actual_url Set to the canonical attachment URL after creation.
* On WP 5.3+, may differ from $attachment_url if the image was scaled.
* @return int Attachment post ID, or 0 on failure.
*/
protected function create_attachment( $attachment_url ) {
protected function create_attachment( $attachment_url, &$actual_url = null ) {
include_once ABSPATH . 'wp-admin/includes/image.php';
include_once ABSPATH . 'wp-admin/includes/media.php';

Expand Down Expand Up @@ -957,7 +959,15 @@ protected function create_attachment( $attachment_url ) {
$attachment_id = wp_insert_attachment( $attachment, $attachment_url, $this->job_id );

if ( ! is_wp_error( $attachment_id ) ) {
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $attachment_url ) );
$metadata = wp_generate_attachment_metadata( $attachment_id, $attachment_url );
wp_update_attachment_metadata( $attachment_id, $metadata );

// If the image was scaled (WP 5.3+), original_image is set and
// _wp_attached_file now points to the -scaled version.
if ( isset( $metadata['original_image'] ) ) {
$actual_url = wp_get_attachment_url( $attachment_id );
}

return $attachment_id;
}

Expand All @@ -974,7 +984,14 @@ protected function update_job_data( $values ) {
add_post_meta( $this->job_id, '_filled', 0, true );
add_post_meta( $this->job_id, '_featured', 0, true );

$maybe_attach = [];
// Pre-load existing attachment URLs to avoid duplicates.
$attach_uploads = apply_filters( 'job_manager_attach_uploaded_files', true );
$attachment_urls = [];
if ( $attach_uploads ) {
foreach ( get_posts( 'post_parent=' . $this->job_id . '&post_type=attachment&fields=ids&numberposts=-1' ) as $existing_id ) {
$attachment_urls[] = wp_get_attachment_url( $existing_id );
}
}

// Loop fields and save meta and term data.
foreach ( $this->fields as $group_key => $group_fields ) {
Expand All @@ -999,38 +1016,32 @@ protected function update_job_data( $values ) {

// Save meta data.
} else {
update_post_meta( $this->job_id, '_' . $key, $values[ $group_key ][ $key ] );

// Handle attachments.
if ( 'file' === $field['type'] ) {
if ( is_array( $values[ $group_key ][ $key ] ) ) {
foreach ( $values[ $group_key ][ $key ] as $file_url ) {
$maybe_attach[] = $file_url;
$meta_value = $values[ $group_key ][ $key ];

// Create the attachment before saving meta so wp_generate_attachment_metadata
// runs first; on WP 5.3+, this resolves any scaled URL before it is persisted.
if ( 'file' === $field['type'] && $attach_uploads ) {
$is_array = is_array( $meta_value );
$urls = array_filter( $is_array ? $meta_value : [ $meta_value ] );

foreach ( $urls as &$url ) {
if ( ! in_array( $url, $attachment_urls, true ) ) {
$actual_url = null;
$this->create_attachment( $url, $actual_url );
if ( $actual_url ) {
$attachment_urls[] = $actual_url;
$url = $actual_url;
}
}
} else {
$maybe_attach[] = $values[ $group_key ][ $key ];
}
}
}
}
}

$maybe_attach = array_filter( $maybe_attach );
unset( $url );

// Handle attachments.
if ( count( $maybe_attach ) && apply_filters( 'job_manager_attach_uploaded_files', true ) ) {
// Get attachments.
$attachments = get_posts( 'post_parent=' . $this->job_id . '&post_type=attachment&fields=ids&numberposts=-1' );
$attachment_urls = [];

// Loop attachments already attached to the job.
foreach ( $attachments as $attachment_id ) {
$attachment_urls[] = wp_get_attachment_url( $attachment_id );
}
if ( ! empty( $urls ) ) {
$meta_value = $is_array ? $urls : reset( $urls );
}
}

foreach ( $maybe_attach as $attachment_url ) {
if ( ! in_array( $attachment_url, $attachment_urls, true ) ) {
$this->create_attachment( $attachment_url );
update_post_meta( $this->job_id, '_' . $key, $meta_value );
}
}
}
Expand Down
Loading