__( 'Please select a file.', 'attachment-importer' ), 'noAttachments' => __( 'There were no attachment files found in the import file.', 'attachment-importer' ), 'parsing' => __( 'Parsing the file.', 'attachment-importer' ), 'importing' => __( 'Importing file ', 'attachment-importer' ), 'progress' => __( 'Overall progress: ', 'attachment-importer' ), 'retrying' => __( 'An error occured. In 5 seconds, retrying file ', 'attachment-importer' ), 'done' => __( 'All done!', 'attachment-importer' ), 'ajaxFail' => __( 'There was an error connecting to the server.', 'attachment-importer' ), 'pbAjaxFail' => __( 'The program could not run. Check the error log below or your JavaScript console for more information', 'attachment-importer' ), 'fatalUpload' => __( 'There was a fatal error. Check the last entry in the error log below.', 'attachment-importer' ) ) ); wp_localize_script( 'attachment-importer-js', 'aiSecurity', array( 'nonce' => wp_create_nonce( 'import-attachment-plugin' ) ) ); wp_enqueue_style( 'jquery-ui', plugins_url( 'inc/jquery-ui.css', __FILE__ ) ); wp_enqueue_style( 'attachment-importer', plugins_url( 'inc/style.css', __FILE__ ) ); ?>


 
 
 

  

outdated browser that doesn\'t support the features required to use this plugin.', 'attachment-importer' ); ?>

upgrade your browser in order to use this plugin.', 'attachment-importer' ), 'http://browsehappy.com' ); ?>

true, 'type' => 'error', 'code' => $nonce_error->get_error_code(), 'message' => $nonce_error->get_error_message(), 'text' => sprintf( __( 'The security key provided with this request is invalid. Is someone trying to trick you to upload something you don\'t want to? If you really meant to take this action, reload your browser window and try again. (%2$s: %3$s)', 'attachment-importer' ), 'http://codex.wordpress.org/WordPress_Nonces', $nonce_error->get_error_code(), $nonce_error->get_error_message() ) ) ); die(); } $parameters = array( 'url' => $_POST['url'], 'post_title' => $_POST['title'], 'link' => $_POST['link'], 'pubDate' => $_POST['pubDate'], 'post_author' => $_POST['creator'], 'guid' => $_POST['guid'], 'import_id' => $_POST['post_id'], 'post_date' => $_POST['post_date'], 'post_date_gmt' => $_POST['post_date_gmt'], 'comment_status' => $_POST['comment_status'], 'ping_status' => $_POST['ping_status'], 'post_name' => $_POST['post_name'], 'post_status' => $_POST['status'], 'post_parent' => $_POST['post_parent'], 'menu_order' => $_POST['menu_order'], 'post_type' => $_POST['post_type'], 'post_password' => $_POST['post_password'], 'is_sticky' => $_POST['is_sticky'], 'attribute_author1' => $_POST['author1'], 'attribute_author2' => $_POST['author2'] ); function process_attachment( $post, $url ) { $pre_process = pre_process_attachment( $post, $url ); if( is_wp_error( $pre_process ) ) return array( 'fatal' => false, 'type' => 'error', 'code' => $pre_process->get_error_code(), 'message' => $pre_process->get_error_message(), 'text' => sprintf( __( '%1$s was not uploaded. (%2$s: %3$s)', 'attachment-importer' ), $post['post_title'], $pre_process->get_error_code(), $pre_process->get_error_message() ) ); // if the URL is absolute, but does not contain address, then upload it assuming base_site_url if ( preg_match( '|^/[\w\W]+$|', $url ) ) $url = rtrim( $this->base_url, '/' ) . $url; $upload = fetch_remote_file( $url, $post ); if ( is_wp_error( $upload ) ) return array( 'fatal' => ( $upload->get_error_code() == 'upload_dir_error' && $upload->get_error_message() != 'Invalid file type' ? true : false ), 'type' => 'error', 'code' => $upload->get_error_code(), 'message' => $upload->get_error_message(), 'text' => sprintf( __( '%1$s could not be uploaded because of an error. (%2$s: %3$s)', 'attachment-importer' ), $post['post_title'], $upload->get_error_code(), $upload->get_error_message() ) ); if ( $info = wp_check_filetype( $upload['file'] ) ) $post['post_mime_type'] = $info['type']; else { $upload = new WP_Error( 'attachment_processing_error', __('Invalid file type', 'attachment-importer') ); return array( 'fatal' => false, 'type' => 'error', 'code' => $upload->get_error_code(), 'message' => $upload->get_error_message(), 'text' => sprintf( __( '%1$s could not be uploaded because of an error. (%2$s: %3$s)', 'attachment-importer' ), $post['post_title'], $upload->get_error_code(), $upload->get_error_message() ) ); } $post['guid'] = $upload['url']; // Set author per user options. switch( $post['attribute_author1'] ){ case 1: // Attribute to current user. $post['post_author'] = (int) wp_get_current_user()->ID; break; case 2: // Attribute to user in import file. if( !username_exists( $post['post_author'] ) ) wp_create_user( $post['post_author'], wp_generate_password() ); $post['post_author'] = (int) username_exists( $post['post_author'] ); break; case 3: // Attribute to selected user. $post['post_author'] = (int) $post['attribute_author2']; break; } // as per wp-admin/includes/upload.php $post_id = wp_insert_attachment( $post, $upload['file'] ); wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) ); // remap image URL's backfill_attachment_urls( $url, $upload['url'] ); return array( 'fatal' => false, 'type' => 'updated', 'text' => sprintf( __( '%s was uploaded successfully', 'attachment-importer' ), $post['post_title'] ) ); } function pre_process_attachment( $post, $url ){ global $wpdb; $imported = $wpdb->get_results( $wpdb->prepare( " SELECT ID, post_date_gmt, guid FROM $wpdb->posts WHERE post_type = 'attachment' AND post_title = %s ", $post['post_title'] ) ); if( $imported ){ foreach( $imported as $attachment ){ if( basename( $url ) == basename( $attachment->guid ) ){ if( $post['post_date_gmt'] == $attachment->post_date_gmt ){ $headers = wp_get_http( $url ); if( filesize( get_attached_file( $attachment->ID ) ) == $headers['content-length'] ){ return new WP_Error( 'duplicate_file_notice', __( 'File already exists', 'attachment-importer' ) ); } } } } } return false; } function fetch_remote_file( $url, $post ) { // extract the file name and extension from the url $file_name = basename( $url ); // get placeholder file in the upload dir with a unique, sanitized filename $upload = wp_upload_bits( $file_name, 0, '', $post['post_date'] ); if ( $upload['error'] ) return new WP_Error( 'upload_dir_error', $upload['error'] ); // fetch the remote url and write it to the placeholder file $headers = wp_get_http( $url, $upload['file'] ); // request failed if ( ! $headers ) { @unlink( $upload['file'] ); return new WP_Error( 'import_file_error', __('Remote server did not respond', 'attachment-importer') ); } // make sure the fetch was successful if ( $headers['response'] != '200' ) { @unlink( $upload['file'] ); return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'attachment-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) ); } $filesize = filesize( $upload['file'] ); if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) { @unlink( $upload['file'] ); return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'attachment-importer') ); } if ( 0 == $filesize ) { @unlink( $upload['file'] ); return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'attachment-importer') ); } return $upload; } function backfill_attachment_urls( $from_url, $to_url ) { global $wpdb; // remap urls in post_content $wpdb->query( $wpdb->prepare( " UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s) ", $from_url, $to_url ) ); // remap enclosure urls $result = $wpdb->query( $wpdb->prepare( " UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure' ", $from_url, $to_url ) ); } $remote_url = ! empty($parameters['attachment_url']) ? $parameters['attachment_url'] : $parameters['guid']; echo json_encode( process_attachment( $parameters, $remote_url ) ); die();}