Created
April 15, 2020 18:33
-
-
Save apurbajnu/b29a4bef01c13a0b5cf867b0034bcbab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function do_job_report() | |
{ | |
// First check the nonce, if it fails the function will break | |
// check_ajax_referer( 'expert-report-nonce', '_wpnonce' ); | |
$img_id = 0; | |
$img_ids = []; | |
if(is_uploaded_file($_FILES['report_image']['tmp_name'][0])): | |
foreach($_FILES["report_image"]["tmp_name"] as $key=>$tmp_name) { | |
$file_name=$_FILES["report_image"]["name"][$key]; | |
if($key>0){ | |
continue; | |
} | |
// WordPress environment | |
$wordpress_upload_dir = wp_upload_dir(); | |
// $wordpress_upload_dir['path'] is the full server path to wp-content/uploads/2017/05, for multisite works good as well | |
// $wordpress_upload_dir['url'] the absolute URL to the same folder, actually we do not need it, just to show the link to file | |
$i = 1; // number of tries when the file with the same name is already exists | |
$report_image = $_FILES['report_image'][$key]; | |
$new_file_path = $wordpress_upload_dir['path'] . '/' . $file_name; | |
$new_file_mime = mime_content_type( $_FILES['report_image']['tmp_name'][$key] ); | |
if( $report_image['error'][$key] ){ | |
print_r($report_image['error']); | |
die(); | |
} | |
if( $report_image['size'] > wp_max_upload_size() ) | |
die( 'It is too large than expected.' ); | |
if( !in_array( $new_file_mime, get_allowed_mime_types() ) ) | |
die( 'WordPress doesn\'t allow this type of uploads.' ); | |
while( file_exists( $new_file_path ) ) { | |
$new_file_path = $wordpress_upload_dir['path'] . '/' . $key . '_' . $report_image['name']; | |
} | |
// looks like everything is OK | |
if( move_uploaded_file( $_FILES['report_image']['tmp_name'][$key], $new_file_path ) ) { | |
$img_id = wp_insert_attachment( array( | |
'guid' => $new_file_path, | |
'post_mime_type' => $new_file_mime, | |
'post_title' => preg_replace( '/\.[^.]+$/', '', $report_image['name'] ), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
), $new_file_path ); | |
// wp_generate_attachment_metadata() won't work if you do not include this file | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
// Generate and save the attachment metas into the database | |
wp_update_attachment_metadata( $img_id, wp_generate_attachment_metadata( $img_id, $new_file_path ) ); | |
$img_ids[]= $img_id; | |
} | |
} | |
endif; | |
$form_data = sanitize( $_POST ); | |
$report_data = array( | |
'title' => $form_data['title'], | |
'description' => $form_data['description'], | |
'img_id' => serialize($img_ids), | |
'job_id' => $form_data['job_id'], | |
'author_id' => get_current_user_id(), | |
); | |
$result = $this->create($report_data); | |
if ( is_wp_error( $result ) ) { | |
$message = $result->get_error_message(); | |
$error = true; | |
} else { | |
$message = 'Report successfully submitted!'; | |
$error = false; | |
} | |
$response = array( | |
'title' => 'Progress Report', | |
'message' => $message, | |
'report_id' => $report_data, | |
'status' => $error ? 'error' : 'success' | |
); | |
echo json_encode($response); | |
wp_die(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment