// wp-includes/Requests/Utility/FilteredIterator.php classRequests_Utility_FilteredIteratorextendsArrayIterator{ /** * Callback to run as a filter * * @var callable */ protected$callback;
/** * Create a new iterator * * @param array $data * @param callable $callback Callback to be called on each value */ publicfunction__construct($data, $callback) { parent::__construct($data);
$this->callback = $callback; }
/** * Get the current item's value after filtering * * @return string */ publicfunctioncurrent() { $value = parent::current(); $value = call_user_func($this->callback, $value); return$value; } }
/** * Retrieve attached file path based on attachment ID. * * By default the path will go through the 'get_attached_file' filter, but * passing a true to the $unfiltered argument of get_attached_file() will * return the file path unfiltered. * * The function works by getting the single post meta name, named * '_wp_attached_file' and returning it. This is a convenience function to * prevent looking up the meta name and provide a mechanism for sending the * attached filename through a filter. * * @since 2.0.0 * * @param int $attachment_id Attachment ID. * @param bool $unfiltered Optional. Whether to apply filters. Default false. * @return string|false The file path to where the attached file should be, false otherwise. */ functionget_attached_file($attachment_id, $unfiltered = false) { $file = get_post_meta($attachment_id, '_wp_attached_file', true );
// If the file is relative, prepend upload dir. if ($file && 0 !== strpos($file, '/' ) && ! preg_match('|^.:\\\|', $file ) ) { $uploads = wp_get_upload_dir(); if (false === $uploads['error'] ) { $file = $uploads['basedir'] . "/$file"; } }
if ($unfiltered ) { return$file; }
/** * Filters the attached file based on the given ID. * * @since 2.1.0 * * @param string|false $file The file path to where the attached file should be, false otherwise. * @param int $attachment_id Attachment ID. */ returnapply_filters('get_attached_file', $file, $attachment_id ); }
/** * Retrieves attachment metadata for attachment ID. * * @since 2.1.0 * * @param int $attachment_id Attachment post ID. Defaults to global $post. * @param bool $unfiltered Optional. If true, filters are not run. Default false. * @return array|false { * Attachment metadata. False on failure. * * @type int $width The width of the attachment. * @type int $height The height of the attachment. * @type string $file The file path relative to `wp-content/uploads`. * @type array $sizes Keys are size slugs, each value is an array containing * 'file', 'width', 'height', and 'mime-type'. * @type array $image_meta Image metadata. * } */ functionwp_get_attachment_metadata($attachment_id = 0, $unfiltered = false) { $attachment_id = (int) $attachment_id;
/** * Filters the attachment meta data. * * @since 2.1.0 * * @param array|bool $data Array of meta data for the given attachment, or false * if the object does not exist. * @param int $attachment_id Attachment post ID. */ returnapply_filters('wp_get_attachment_metadata', $data, $post->ID ); }
/** * Scale an image to fit a particular size (such as 'thumb' or 'medium'). * * The URL might be the original image, or it might be a resized version. This * function won't create a new resized copy, it will just return an already * resized one if it exists. * * A plugin may use the {@see 'image_downsize'} filter to hook into and offer image * resizing services for images. The hook must return an array with the same * elements that are normally returned from the function. * * @since 2.5.0 * * @param int $id Attachment ID for image. * @param string|int[] $size Optional. Image size to scale to. Accepts any valid image size name, * or an array of width and height values in pixels (in that order). * Default 'medium'. * @return array|false { * Array of image data, or boolean false if no image is available. * * @type string $0 Image source URL. * @type int $1 Image width in pixels. * @type int $2 Image height in pixels. * @type bool $3 Whether the image is a resized image. * } */ functionimage_downsize($id, $size = 'medium') { // ... if ($intermediate ) { $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url ); $width = $intermediate['width']; $height = $intermediate['height']; $is_intermediate = true; } elseif ('thumbnail' === $size ) { // Fall back to the old thumbnail. $thumb_file = wp_get_attachment_thumb_file($id ); // ... } // ... }
/** * Returns only allowed post data fields * * @since 5.0.1 * * @param array $post_data Array of post data. Defaults to the contents of $_POST. * @return array|WP_Error Array of post data on success, WP_Error on failure. */ function_wp_get_allowed_postdata($post_data = null) { if (empty($post_data ) ) { $post_data = $_POST; }
// Pass through errors. if (is_wp_error($post_data ) ) { return$post_data; }
/** * Update an existing post with values provided in $_POST. * * If post data is passed as an argument, it is treated as an array of data * keyed appropriately for turning into a post object. * * If post data is not passed, the $_POST global variable is used instead. * * @since 1.5.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param array $post_data Optional. Defaults to the $_POST global. * @return int Post ID. */ functionedit_post($post_data = null) { // .. $post_data = _wp_translate_postdata(true, $post_data ); if (is_wp_error($post_data ) ) { wp_die($post_data->get_error_message()); } $translated = _wp_get_allowed_postdata($post_data ); // ... // Attachment stuff. if ('attachment' === $post_data['post_type'] ) { if (isset($post_data['_wp_attachment_image_alt'] ) ) { $image_alt = wp_unslash($post_data['_wp_attachment_image_alt'] );
/** * Update a post with new post data. * * The date does not have to be set for drafts. You can set the date and it will * not be overridden. * * @since 1.0.0 * * @param array|object $postarr Optional. Post data. Arrays are expected to be escaped, * objects are not. Default array. * @param bool $wp_error Optional. Allow return of WP_Error on failure. Default false. * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. */ functionwp_update_post($postarr = array(), $wp_error = false) { // ... // Merge old and new fields with new fields overwriting old ones. $postarr = array_merge($post, $postarr ); $postarr['post_category'] = $post_cats; if ($clear_date ) { $postarr['post_date'] = current_time('mysql' ); $postarr['post_date_gmt'] = ''; } // ... returnwp_insert_post($postarr, $wp_error ); }
/** * Insert or update a post. * * If the $postarr parameter has 'ID' set to a value, then post will be updated. * * You can set the post date manually, by setting the values for 'post_date' * and 'post_date_gmt' keys. You can close the comments or open the comments by * setting the value for 'comment_status' key. * * @since 1.0.0 * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt. * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data. * * ... */ functionwp_insert_post($postarr, $wp_error = false) { // ... if ('attachment' === $postarr['post_type'] ) { if (! empty($postarr['file'] ) ) { update_attached_file($post_ID, $postarr['file'] ); }
/** * Update attachment file path based on attachment ID. * * Used to update the file path of the attachment, which uses post meta name * '_wp_attached_file' to store the path of the attachment. * * @since 2.1.0 * * @param int $attachment_id Attachment ID. * @param string $file File path for the attachment. * @return bool True on success, false on failure. */ functionupdate_attached_file($attachment_id, $file) { if (! get_post($attachment_id ) ) { returnfalse; }
/** * Filters the path to the attached file to update. * * @since 2.1.0 * * @param string $file Path to the attached file to update. * @param int $attachment_id Attachment ID. */ $file = apply_filters('update_attached_file', $file, $attachment_id );