public function do_save($return_val, $params, $metas, $module) { if (empty($params['attachment_url'])) { return false; } $bits = file_get_contents($params['attachment_url']); $filename = $this->faker->uuid() . '.jpg'; $upload = wp_upload_bits($filename, null, $bits); $params['guid'] = $upload['url']; $params['post_mime_type'] = 'image/jpeg'; // Insert the attachment. $attach_id = wp_insert_attachment($params, $upload['file'], 0); if (!is_numeric($attach_id)) { return false; } if (!function_exists('wp_generate_attachment_metadata')) { // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. require_once ABSPATH . 'wp-admin/includes/image.php'; } // Generate the metadata for the attachment, and update the database record. $metas['_wp_attachment_metadata'] = wp_generate_attachment_metadata($attach_id, $upload['file']); foreach ($metas as $key => $value) { update_post_meta($attach_id, $key, $value); } return $attach_id; }
/** * @param strin $file_url * * @return int */ private function upload_file($file_url) { if (!filter_var($file_url, FILTER_VALIDATE_URL)) { return false; } $contents = @file_get_contents($file_url); if ($contents === false) { return false; } $upload = wp_upload_bits(basename($file_url), null, $contents); if (isset($upload['error']) && $upload['error']) { return false; } $type = ''; if (!empty($upload['type'])) { $type = $upload['type']; } else { $mime = wp_check_filetype($upload['file']); if ($mime) { $type = $mime['type']; } } $attachment = array('post_title' => basename($upload['file']), 'post_content' => '', 'post_type' => 'attachment', 'post_mime_type' => $type, 'guid' => $upload['url']); $id = wp_insert_attachment($attachment, $upload['file']); wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file'])); return $id; }
/** * attachment uploader * @param $file * @param $parent_post_id */ public static function upload_attachment($file, $parent_post_id = 0) { #$file = dirname(__FILE__).'/images/2.jpg'; $filename = basename($file); $upload_file = wp_upload_bits($filename, null, file_get_contents($file)); if (!$upload_file['error']) { $wp_filetype = wp_check_filetype($filename, null); $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_parent' => $parent_post_id, 'post_title' => preg_replace('/\\.[^.]+$/', '', $filename), 'post_content' => '', 'post_status' => 'inherit'); $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], $parent_post_id); if (!is_wp_error($attachment_id)) { require_once ABSPATH . "wp-admin" . '/includes/image.php'; $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']); wp_update_attachment_metadata($attachment_id, $attachment_data); } } return isset($attachment_id) ? $attachment_id : 0; }
/** * Upload theme default site icon * * @return void */ function dswoddil_site_icon_upload() { $image_name = 'site-icon'; if (!function_exists('wp_update_attachment_metadata')) { require_once ABSPATH . 'wp-admin/includes/image.php'; } if (!dswoddil_get_attachment_by_post_name('dswoddil-' . $image_name)) { $site_icon = get_template_directory() . '/img/' . $image_name . '.png'; // create $file array with the indexes show below $file['name'] = $site_icon; $file['type'] = 'image/png'; // get image size $file['size'] = filesize($file['name']); $file['tmp_name'] = $image_name . '.png'; $file['error'] = 1; $file_content = file_get_contents($site_icon); $upload_image = wp_upload_bits($file['tmp_name'], null, $file_content); // Check the type of tile. We'll use this as the 'post_mime_type'. $filetype = wp_check_filetype(basename($upload_image['file']), null); $attachment = array('guid' => $upload_image['file'], 'post_mime_type' => $filetype['type'], 'post_title' => 'dswoddil-' . $image_name, 'post_content' => '', 'post_status' => 'inherit'); //insert wordpress attachment of uploaded image to get attachmen ID $attachment_id = wp_insert_attachment($attachment, $upload_image['file']); //generate attachment thumbnail wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $upload_image['file'])); add_post_meta($attachment_id, '_wp_attachment_context', $image_name); } }
public function import($attachment) { $saved_image = $this->_return_saved_image($attachment); if ($saved_image) { return $saved_image; } // Extract the file name and extension from the url $filename = basename($attachment['url']); if (function_exists('file_get_contents')) { $options = ['http' => ['user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686 on x86_64; rv:49.0) Gecko/20100101 Firefox/49.0']]; $context = stream_context_create($options); $file_content = file_get_contents($attachment['url'], false, $context); } else { $file_content = wp_remote_retrieve_body(wp_safe_remote_get($attachment['url'])); } if (empty($file_content)) { return false; } $upload = wp_upload_bits($filename, null, $file_content); $post = ['post_title' => $filename, 'guid' => $upload['url']]; $info = wp_check_filetype($upload['file']); if ($info) { $post['post_mime_type'] = $info['type']; } else { // For now just return the origin attachment return $attachment; //return new \WP_Error( 'attachment_processing_error', __( 'Invalid file type', 'elementor' ) ); } $post_id = wp_insert_attachment($post, $upload['file']); wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, $upload['file'])); update_post_meta($post_id, '_elementor_source_image_hash', $this->_get_hash_image($attachment['url'])); $new_attachment = ['id' => $post_id, 'url' => $upload['url']]; $this->_replace_image_ids[$attachment['id']] = $new_attachment; return $new_attachment; }
function set_ec_fun() { echo '<h2>Settings</h2>'; if (isset($_GET['msg']) && $_GET['msg'] == 1) { echo '<h4>Setttings saved !</h4>'; } echo '<form method="post" enctype="multipart/form-data"> <ul> <li><input type="file" name="add_to_cart" /></li> <li><img src="' . site_url('/') . 'wp-content/' . get_option('ec_cart_img') . '" id="img"/></li> <li><input type="submit" value="Upload" /></li> </ul> </form>'; if (!empty($_FILES['add_to_cart']['name'])) { global $wpdb; if (!empty($_FILES)) { $FName = time . '_' . $_FILES["add_to_cart"]["name"]; $upload = wp_upload_bits($FName, null, file_get_contents($_FILES["add_to_cart"]["tmp_name"])); $upload_dir = wp_upload_dir(); $img = 'uploads' . $upload_dir['subdir'] . '/' . $FName; update_option('ec_cart_img', $img); echo '<script type="text/javascript">window.location="admin.php?page=set_ec&msg=1"</script>'; } } }
/** * Receives and processes files uploaded through the personalization panel. * * @return void * @since 1.0 */ function ql_upload() { check_ajax_referer('quicklaunch-upload-file', '_wpnonce'); // Grab file details and content $filename = $_FILES['file']['name']; $bits = file_get_contents($_FILES['file']['tmp_name']); // Make sure we have no errors if ($_FILES['file']['error'] != 0) { $data = array('msg' => 'Upload Error. Code ' . $_FILES['file']['error']); $response = new QL_JSONResponse($data, false); $response->output(); } // Do upload $upload = wp_upload_bits($filename, null, $bits); // Delete the temp file @unlink($_FILES['file']['tmp_name']); // If we have no errors, return the URL of the image as a response if ($upload['error'] === false) { $data = array('url' => $upload['url']); $response = new QL_JSONResponse($data); $response->output(); } else { $data = array('msg' => $upload['error']); $response = new QL_JSONResponse($data, false); $response->output(); } }
public function do_save($return_val, $data, $module) { if (empty($data['attachment_url'])) { return false; } $bits = file_get_contents($data['attachment_url']); $filename = $this->faker->uuid() . '.jpg'; $upload = wp_upload_bits($filename, null, $bits); $data['guid'] = $upload['url']; $data['post_mime_type'] = 'image/jpeg'; // Insert the attachment. $attach_id = wp_insert_attachment($data, $upload['file'], 0); if (!is_numeric($attach_id)) { return false; } if (!function_exists('wp_generate_attachment_metadata')) { // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. require_once ABSPATH . 'wp-admin/includes/image.php'; } // Generate the metadata for the attachment, and update the database record. update_post_meta($attach_id, '_wp_attachment_metadata', wp_generate_attachment_metadata($attach_id, $upload['file'])); // Flag the Object as FakerPress update_post_meta($attach_id, self::$flag, 1); return $attach_id; }
function test_insert_image_no_thumb() { $filename = dirname(__FILE__) . '/../mock/img/cat.jpg'; $contents = file_get_contents($filename); $upload = wp_upload_bits(basename($filename), null, $contents); print_r($upload['error']); $this->assertTrue(empty($upload['error'])); $attachment_id = $this->_make_attachment($upload); $attachment_url = wp_get_attachment_image_src($attachment_id, "large"); $attachment_url = $attachment_url[0]; $c1 = '<p><img src="' . $attachment_url . '" alt="1559758083_cef4ef63d2_o" width="771" height="475" class="alignnone size-large" /></p> <h2>Headings</h2> <p>Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Sed posuere consectetur est at lobortis. Nulla vitae elit libero, a pharetra augue. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Donec sed odio dui.</p>'; $c1final = '<h2>Headings</h2> <p>Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Sed posuere consectetur est at lobortis. Nulla vitae elit libero, a pharetra augue. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Donec sed odio dui.</p>'; $c2 = '<p><img src="' . $attachment_url . '" alt="1559758083_cef4ef63d2_o" width="771" height="475" class="alignnone size-medium" /></p> <h2>Headings</h2> <p>Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Sed posuere consectetur est at lobortis. Nulla vitae elit libero, a pharetra augue. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Donec sed odio dui.</p>'; $c2final = '<p><img src="' . $attachment_url . '" alt="1559758083_cef4ef63d2_o" width="771" height="475" class="alignnone size-medium" /></p> <h2>Headings</h2> <p>Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Sed posuere consectetur est at lobortis. Nulla vitae elit libero, a pharetra augue. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Donec sed odio dui.</p>'; $post_id = $this->factory->post->create(); add_post_meta($post_id, '_thumbnail_id', $attachment_id); $this->go_to("/?p={$post_id}"); $final1 = largo_remove_hero($c1); $final2 = largo_remove_hero($c2); $this->assertEquals($c1final, $final1); $this->assertEquals($c2final, $final2); }
/** * @param strin $file_url * * @return int */ private function upload_file($file_url) { if (!filter_var($file_url, FILTER_VALIDATE_URL)) { return false; } $contents = @file_get_contents($file_url); if ($contents === false) { return false; } $upload = wp_upload_bits(basename($file_url), null, $contents); if (isset($upload['error']) && $upload['error']) { return false; } $type = ''; if (!empty($upload['type'])) { $type = $upload['type']; } else { $mime = wp_check_filetype($upload['file']); if ($mime) { $type = $mime['type']; } } $attachment = array('post_title' => basename($upload['file']), 'post_content' => '', 'post_type' => 'attachment', 'post_mime_type' => $type, 'guid' => $upload['url']); $id = wp_insert_attachment($attachment, $upload['file']); wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file'])); update_post_meta($id, '_tribe_importer_original_url', $file_url); $this->maybe_init_attachment_guids_cache(); $this->maybe_init_attachment_original_urls_cache(); self::$attachment_guids_cache[get_post($id)->guid] = $id; self::$original_urls_cache[$file_url] = $id; return $id; }
function fetch_remote_file($url, $post) { global $url_remap; // 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['upload_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', 'wordpress-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', 'wordpress-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', 'wordpress-importer')); } if (0 == $filesize) { @unlink($upload['file']); return new WP_Error('import_file_error', __('Zero size file downloaded', 'wordpress-importer')); } // keep track of the old and new urls so we can substitute them later $url_remap[$url] = $upload['url']; return $upload; }
/** * @ticket 22985 */ public function testCropImageThumbnail() { include_once( ABSPATH . 'wp-admin/includes/image-edit.php' ); $filename = DIR_TESTDATA . '/images/canola.jpg'; $contents = file_get_contents($filename); $upload = wp_upload_bits(basename($filename), null, $contents); $id = $this->_make_attachment($upload); $_REQUEST['action'] = 'image-editor'; $_REQUEST['context'] = 'edit-attachment'; $_REQUEST['postid'] = $id; $_REQUEST['target'] = 'thumbnail'; $_REQUEST['do'] = 'save'; $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]'; $media_meta = wp_get_attachment_metadata($id); $this->assertArrayHasKey('sizes', $media_meta, 'attachment should have size data'); $this->assertArrayHasKey('medium', $media_meta['sizes'], 'attachment should have data for medium size'); $ret = wp_save_image($id); $media_meta = wp_get_attachment_metadata($id); $this->assertArrayHasKey('sizes', $media_meta, 'cropped attachment should have size data'); $this->assertArrayHasKey('medium', $media_meta['sizes'], 'cropped attachment should have data for medium size'); }
/** * When a site is deleted with wpmu_delete_blog(), only the files associated with * that site should be removed. When wpmu_delete_blog() is run a second time, nothing * should change with upload directories. */ function test_upload_directories_after_multiple_wpmu_delete_blog_with_ms_files() { $filename = rand_str().'.jpg'; $contents = rand_str(); // Upload a file to the main site on the network. $file1 = wp_upload_bits( $filename, null, $contents ); $blog_id = $this->factory->blog->create(); switch_to_blog( $blog_id ); $file2 = wp_upload_bits( $filename, null, $contents ); restore_current_blog(); wpmu_delete_blog( $blog_id, true ); // The file on the main site should still exist. The file on the deleted site should not. $this->assertTrue( file_exists( $file1['file'] ) ); $this->assertFalse( file_exists( $file2['file'] ) ); wpmu_delete_blog( $blog_id, true ); // The file on the main site should still exist. The file on the deleted site should not. $this->assertTrue( file_exists( $file1['file'] ) ); $this->assertFalse( file_exists( $file2['file'] ) ); }
/** * @ticket 32171 */ public function testImageEditOverwriteConstant() { define('IMAGE_EDIT_OVERWRITE', true); include_once ABSPATH . 'wp-admin/includes/image-edit.php'; $filename = DIR_TESTDATA . '/images/canola.jpg'; $contents = file_get_contents($filename); $upload = wp_upload_bits(basename($filename), null, $contents); $id = $this->_make_attachment($upload); $_REQUEST['action'] = 'image-editor'; $_REQUEST['context'] = 'edit-attachment'; $_REQUEST['postid'] = $id; $_REQUEST['target'] = 'all'; $_REQUEST['do'] = 'save'; $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]'; $ret = wp_save_image($id); $media_meta = wp_get_attachment_metadata($id); $sizes1 = $media_meta['sizes']; $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":189,"h":322}}]'; $ret = wp_save_image($id); $media_meta = wp_get_attachment_metadata($id); $sizes2 = $media_meta['sizes']; $file_path = dirname(get_attached_file($id)); foreach ($sizes1 as $key => $size) { if ($sizes2[$key]['file'] !== $size['file']) { $files_that_shouldnt_exist[] = $file_path . '/' . $size['file']; } } foreach ($files_that_shouldnt_exist as $file) { $this->assertFileNotExists($file, 'IMAGE_EDIT_OVERWRITE is leaving garbage image files behind.'); } }
/** * @ticket 36578 */ public function test_wp_ajax_send_attachment_to_editor_should_return_a_link() { // Become an administrator $post = $_POST; $user_id = self::factory()->user->create(array('role' => 'administrator', 'user_login' => 'user_36578_administrator', 'user_email' => '*****@*****.**')); wp_set_current_user($user_id); $_POST = array_merge($_POST, $post); $filename = DIR_TESTDATA . '/formatting/entities.txt'; $contents = file_get_contents($filename); $upload = wp_upload_bits(basename($filename), null, $contents); $attachment = $this->_make_attachment($upload); // Set up a default request $_POST['nonce'] = wp_create_nonce('media-send-to-editor'); $_POST['html'] = 'Bar Baz'; $_POST['post_id'] = 0; $_POST['attachment'] = array('id' => $attachment, 'post_title' => 'Foo bar', 'url' => get_attachment_link($attachment)); // Make the request try { $this->_handleAjax('send-attachment-to-editor'); } catch (WPAjaxDieContinueException $e) { unset($e); } // Get the response. $response = json_decode($this->_last_response, true); $expected = sprintf('<a href="%s" rel="attachment wp-att-%d">Foo bar</a>', get_attachment_link($attachment), $attachment); // Ensure everything is correct $this->assertTrue($response['success']); $this->assertEquals($expected, $response['data']); }
/** * Helper function: insert an attachment to test properties of. * * @param int $parent_post_id * @param str path to image to use * @param array $post_fields Fields, in the format to be sent to `wp_insert_post()` * @return int Post ID of inserted attachment */ private function insert_attachment($parent_post_id = 0, $image = null, $post_fields = array()) { $filename = rand_str() . '.jpg'; $contents = rand_str(); if ($image) { // @codingStandardsIgnoreStart $filename = basename($image); $contents = file_get_contents($image); // @codingStandardsIgnoreEnd } $upload = wp_upload_bits($filename, null, $contents); $this->assertTrue(empty($upload['error'])); $type = ''; if (!empty($upload['type'])) { $type = $upload['type']; } else { $mime = wp_check_filetype($upload['file']); if ($mime) { $type = $mime['type']; } } $attachment = wp_parse_args($post_fields, array('post_title' => basename($upload['file']), 'post_content' => 'Test Attachment', 'post_type' => 'attachment', 'post_parent' => $parent_post_id, 'post_mime_type' => $type, 'guid' => $upload['url'])); // Save the data $id = wp_insert_attachment($attachment, $upload['file'], $parent_post_id); wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file'])); return $id; }
function ead_upload_file($fileinfo) { if ($fileinfo['ead-file']['name'] != '') { $file = file_get_contents($fileinfo['ead-file']['tmp_name']); $uploadinfo = wp_upload_bits($fileinfo['ead-file']['name'], null, $file); return $uploadinfo; } }
/** * Helper function to create an attachment from a file * * @uses _make_attachment * * @param string Optional. A path to a file. Default: DIR_TESTDATA.'/images/canola.JPG'. * @return int|bool An attachment ID or false. */ private function _test_img($file = null) { $filename = $file ? $file : dirname(__FILE__) . '/data/test-large.png'; $contents = file_get_contents($filename); $upload = wp_upload_bits(basename($filename), null, $contents); $this->assertTrue(empty($upload['error'])); $id = $this->_make_attachment($upload); return $id; }
function fpd_admin_upload_image_to_wp($name, $base64_image, $add_to_library = true) { //upload to wordpress $upload = wp_upload_bits($name, null, base64_decode($base64_image)); //add to media library if ($add_to_library && isset($upload['url'])) { media_sideload_image($upload['url'], 0); } return $upload['error'] === false ? $upload['url'] : false; }
/** * Set up the test fixture */ public function setUp() { parent::setUp(); $this->post_id = $this->factory->post->create(); $this->img_name = 'test-image.jpg'; $filename = CMB2_TESTDATA . '/images/test-image.jpg'; $contents = file_get_contents($filename); $upload = wp_upload_bits(basename($filename), null, $contents); $this->attachment_id = $this->_make_attachment($upload, $this->post_id); }
function setUp() { parent::setUp(); add_theme_support('post-thumbnails'); $filename = DIR_TESTDATA . '/images/waffles.jpg'; $contents = file_get_contents($filename); $upload = wp_upload_bits(basename($filename), null, $contents); $this->attachment_id = $this->_make_attachment($upload, self::$post_id); $this->attachment_data = get_post($this->attachment_id, ARRAY_A); set_post_thumbnail(self::$post_id, $this->attachment_id); }
function setUp() { parent::setUp(); add_theme_support('post-thumbnails'); $this->post_id = wp_insert_post(array('post_title' => rand_str(), 'post_content' => rand_str(), 'post_status' => 'publish')); $filename = DIR_TESTDATA . '/images/waffles.jpg'; $contents = file_get_contents($filename); $upload = wp_upload_bits(basename($filename), null, $contents); $this->attachment_id = $this->_make_attachment($upload, $this->post_id); $this->attachment_data = get_post($this->attachment_id, ARRAY_A); set_post_thumbnail($this->post_id, $this->attachment_id); }
/** * */ public static function do_files_upload($entity_data, $file_upload_param) { $count = 0; $entity_data['files_uploaded'] = array(); $current_user = wp_get_current_user(); /*$validation_errors = ContentFileUploadValidatorAPI::validate_file_upload($file_upload_param); if(!empty($validation_errors)) { $entity_data['has_errors'] = true; foreach ($validation_errors['file_upload_error_msg'] as $key => $value) { $entity_data['message'] = $value; LogUtils::shadow_log($entity_data['message']); } return $entity_data; } */ if (!empty($_FILES)) { foreach ($_FILES[$file_upload_param]['name'] as $filename) { if ($_FILES[$file_upload_param]['tmp_name'][$count] != '') { // Use the WordPress API to upload the file $upload = wp_upload_bits($_FILES[$file_upload_param]['name'][$count], null, file_get_contents($_FILES[$file_upload_param]['tmp_name'][$count])); if (isset($upload['error']) && $upload['error'] != 0) { wp_die('There was an error uploading your file. The error is: ' . $upload['error']); } else { $file_size = $_FILES[$file_upload_param]["size"][$count]; $file_size = $file_size / 1024; $file_size = number_format((double) $file_size, 2, '.', ''); $date_obj = new DateTime(); $file_obj = array('file_name' => $_FILES[$file_upload_param]['name'][$count], 'file_code' => $entity_data['entity_code'], 'file_url' => $upload['url'], 'file_size' => $file_size, 'file_owner' => $entity_data['id'], 'file_created_date' => $date_obj->format('M j, Y, H:i'), 'file_type' => 'FILE', 'file_mime_type' => '', 'file_description' => ''); // Post information $post_information = array('post_title' => $file_obj['file_name'], 'post_content' => $file_obj['file_description'], 'post_type' => 'content_file', 'post_status' => 'publish'); // Insert the order into the database $post_id = wp_insert_post($post_information); if ($entity_data['entity_artifact_name'] == 'party') { $image_entity_data = EntityAPIUtils::init_entity_data('partyimage'); $image_entity_data['file_party'] = $entity_data['id']; self::save_image($entity_data, $image_entity_data, $file_obj); } if ($entity_data['entity_artifact_name'] == 'contentorder') { $image_entity_data = EntityAPIUtils::init_entity_data('contentorderfile'); $image_entity_data['file_content_order'] = $entity_data['id']; self::save_image($entity_data, $image_entity_data, $file_obj); } array_push($entity_data['files_uploaded'], $file_obj); } // end } $count++; } } return $entity_data; }
function save_custom_meta_data($id) { /* --- security verification --- */ if (!wp_verify_nonce($_POST['wp_custom_plist_attachment_nonce'], plugin_basename(__FILE__))) { return $id; } // end if if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $id; } // end if if ('page' == $_POST['post_type']) { if (!current_user_can('edit_page', $id)) { return $id; } // end if } else { if (!current_user_can('edit_page', $id)) { return $id; } // end if } // end if /* - end security verification - */ // Make sure the file array isn't empty if (!empty($_FILES['wp_custom_plist_attachment']['name'])) { // Setup the array of supported file types. In this case, it's just PDF. $supported_types = array('application/ipa'); // Get the file type of the upload $arr_file_type = wp_check_filetype(basename($_FILES['wp_custom_plist_attachment']['name'])); $uploaded_type = $arr_file_type['type']; // Check if the type is supported. If not, throw an error. /* if(in_array($uploaded_type, $supported_types)) { */ // Use the WordPress API to upload the file $upload = wp_upload_bits($_FILES['wp_custom_plist_attachment']['name'], null, file_get_contents($_FILES['wp_custom_plist_attachment']['tmp_name'])); if (isset($upload['error']) && $upload['error'] != 0) { wp_die('There was an error uploading your file. The error is: ' . $upload['error']); } else { add_post_meta($id, 'wp_custom_plist_attachment', $upload); update_post_meta($id, 'wp_custom_plist_attachment', $upload); } // end if/else /* } else { wp_die("The file type that you've uploaded is not an IPA."); } // end if/else */ } // end if }
function pronamic_framework_maybe_save_post() { if (isset($_POST['pronamic_framework_edit_post_submit'])) { $post_ID = filter_input(INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT); // Post $post_title = filter_input(INPUT_POST, 'post_title', FILTER_SANITIZE_STRING); $post_content = filter_input(INPUT_POST, 'post_content', FILTER_UNSAFE_RAW); $post_content = wp_kses_post($post_content); $post = array('ID' => $post_ID, 'post_title' => $post_title, 'post_content' => $post_content); $result = wp_update_post($post); if (0 !== $result) { } else { } // Meta $meta = filter_input(INPUT_POST, 'post_meta', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY); foreach ($meta as $key => $value) { update_post_meta($post_ID, $key, $value); } // Attachments if (isset($_FILES['post_attachments'])) { require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/post.php'; $post_mime_types = get_post_mime_types(); foreach ($_FILES['post_attachments']['error'] as $key => $error) { if (UPLOAD_ERR_OK == $error) { // no error $tmp_name = $_FILES['post_attachments']['tmp_name'][$key]; $name = $_FILES['post_attachments']['name'][$key]; $bits = file_get_contents($tmp_name); $result = wp_upload_bits($name, null, $bits); if (false === $result['error']) { // no error $file_type = wp_check_filetype($result['file']); $keys = array_keys(wp_match_mime_types(array_keys($post_mime_types), $file_type)); $type = array_shift($keys); $attachment = array('post_title' => $name, 'post_mime_type' => $file_type['type'], 'guid' => $result['url'], 'post_parent' => $post_ID); $attachment_id = wp_insert_attachment($attachment, $result['file'], $post_ID); $meta_data = wp_generate_attachment_metadata($attachment_id, $result['file']); $updated = wp_update_attachment_metadata($attachment_id, $meta_data); if ('image' == $type) { update_post_meta($post_ID, '_thumbnail_id', $attachment_id); } } } } } } }
function setUp() { parent::setUp(); add_theme_support('post-thumbnails'); $this->post_id = wp_insert_post(array('post_title' => rand_str(), 'post_content' => rand_str(), 'post_status' => 'publish')); $filename = DIR_TESTDATA . '/images/waffles.jpg'; $contents = file_get_contents($filename); $upload = wp_upload_bits(basename($filename), null, $contents); $mime = wp_check_filetype($filename); $this->attachment_data = array('post_title' => basename($upload['file']), 'post_content' => '', 'post_type' => 'attachment', 'post_parent' => $this->post_id, 'post_mime_type' => $mime['type'], 'guid' => $upload['url']); $id = wp_insert_attachment($this->attachment_data, $upload['file'], $this->post_id); wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file'])); $this->attachment_id = $id; set_post_thumbnail($this->post_id, $this->attachment_id); }
function mediaSaver($postId) { return function ($object) use($postId) { $uri = $object->media->image; $url = $uri; if (preg_match('#^data:(?!//)#', $uri)) { $phpUri = 'data://' . substr($uri, 5); $fileInfo = wp_upload_bits('uploadedBits.png', null, file_get_contents($phpUri)); // TODO: Use image $url = $fileInfo['url']; } $object->media->image = $url; return $object; }; }
function save_images($image_url, $post_id, $i) { //set_time_limit(180); //每个图片最长允许下载时间,秒 $file = file_get_contents($image_url); $fileext = substr(strrchr($image_url, '.'), 1); $fileext = strtolower($fileext); if ($fileext == "" || strlen($fileext) > 4) { $fileext = "jpg"; } $savefiletype = array('jpg', 'gif', 'png', 'bmp'); if (!in_array($fileext, $savefiletype)) { $fileext = "jpg"; } $im_name = date('YmdHis', time()) . $i . mt_rand(10, 20) . '.' . $fileext; $res = wp_upload_bits($im_name, '', $file); if (isset($res['file'])) { $attach_id = insert_attachment($res['file'], $post_id); } else { return; } if (ot_get_option('auto_save_image_thumb') == 'on' && $i == 1) { set_post_thumbnail($post_id, $attach_id); } return $res; }
/** * Saves backup of WPP settings to uploads and to DB. * * @param $old_version * @param $new_version */ public static function do_backup($old_version, $new_version) { /* Do automatic Settings backup! */ $settings = get_option('wpp_settings'); if (!empty($settings)) { /** * Fixes allowed mime types for adding download files on Edit Product page. * * @see https://wordpress.org/support/topic/2310-download-file_type-missing-in-variations-filters-exe?replies=5 * @author peshkov@UD */ add_filter('upload_mimes', function ($t) { if (!isset($t['json'])) { $t['json'] = 'application/json'; } return $t; }, 99); $filename = md5('wpp_settings_backup') . '.json'; $upload = wp_upload_bits($filename, null, json_encode($settings)); if (!empty($upload) && empty($upload['error'])) { if (isset($upload['error'])) { unset($upload['error']); } $upload['version'] = $old_version; $upload['time'] = time(); update_option('wpp_settings_backup', $upload); } } }
/** * Directories of sub sites on a network should not count against the same spaced used total for * the main site. */ function test_get_space_used_main_site() { $space_used = get_space_used(); $blog_id = $this->factory->blog->create(); switch_to_blog($blog_id); // We don't rely on an initial value of 0 for space used, but should have a clean space available // so that we can remove any uploaded files and directories without concern of a conflict with // existing content directories in src. while (0 != get_space_used()) { restore_current_blog(); $blog_id = $this->factory->blog->create(); switch_to_blog($blog_id); } // Upload a file to the new site. $filename = rand_str() . '.jpg'; $contents = rand_str(); wp_upload_bits($filename, null, $contents); restore_current_blog(); delete_transient('dirsize_cache'); $this->assertEquals($space_used, get_space_used()); // Switch back to the new site to remove the uploaded file. switch_to_blog($blog_id); $upload_dir = wp_upload_dir(); $this->remove_added_uploads(); $this->delete_folders($upload_dir['basedir']); restore_current_blog(); }