+/**
+ * Executes a query for attachments. An array of WP_Query arguments
+ * can be passed in, which will override the arguments set by this function.
+ *
+ * @since 2.5.0
+ *
+ * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal.
+ * @return array
+ */
+function wp_edit_attachments_query( $q = false ) {
+ wp( wp_edit_attachments_query_vars( $q ) );
+
+ $post_mime_types = get_post_mime_types();
+ $avail_post_mime_types = get_available_post_mime_types( 'attachment' );
+
+ return array( $post_mime_types, $avail_post_mime_types );
+}
+
+/**
+ * Returns the list of classes to be used by a metabox
+ *
+ * @since 2.5.0
+ *
+ * @param string $id
+ * @param string $page
+ * @return string
+ */
+function postbox_classes( $id, $page ) {
+ if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) {
+ $classes = array( '' );
+ } elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) {
+ if ( !is_array( $closed ) ) {
+ $classes = array( '' );
+ } else {
+ $classes = in_array( $id, $closed ) ? array( 'closed' ) : array( '' );
+ }
+ } else {
+ $classes = array( '' );
+ }
+
+ /**
+ * Filter the postbox classes for a specific screen and screen ID combo.
+ *
+ * The dynamic portions of the hook name, `$page` and `$id`, refer to
+ * the screen and screen ID, respectively.
+ *
+ * @since 3.2.0
+ *
+ * @param array $classes An array of postbox classes.
+ */
+ $classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes );
+ return implode( ' ', $classes );
+}
+
+/**
+ * Get a sample permalink based off of the post name.
+ *
+ * @since 2.5.0
+ *
+ * @param int $id Post ID or post object.
+ * @param string $title Optional. Title. Default null.
+ * @param string $name Optional. Name. Default null.
+ * @return array Array with two entries of type string.
+ */
+function get_sample_permalink($id, $title = null, $name = null) {
+ $post = get_post( $id );
+ if ( ! $post )
+ return array( '', '' );
+
+ $ptype = get_post_type_object($post->post_type);
+
+ $original_status = $post->post_status;
+ $original_date = $post->post_date;
+ $original_name = $post->post_name;
+
+ // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
+ if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ) ) ) {
+ $post->post_status = 'publish';
+ $post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
+ }
+
+ // If the user wants to set a new name -- override the current one
+ // Note: if empty name is supplied -- use the title instead, see #6072
+ if ( !is_null($name) )
+ $post->post_name = sanitize_title($name ? $name : $title, $post->ID);
+
+ $post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
+
+ $post->filter = 'sample';
+
+ $permalink = get_permalink($post, true);
+
+ // Replace custom post_type Token with generic pagename token for ease of use.
+ $permalink = str_replace("%$post->post_type%", '%pagename%', $permalink);
+
+ // Handle page hierarchy
+ if ( $ptype->hierarchical ) {
+ $uri = get_page_uri($post);
+ if ( $uri ) {
+ $uri = untrailingslashit($uri);
+ $uri = strrev( stristr( strrev( $uri ), '/' ) );
+ $uri = untrailingslashit($uri);
+ }
+
+ /** This filter is documented in wp-admin/edit-tag-form.php */
+ $uri = apply_filters( 'editable_slug', $uri, $post );
+ if ( !empty($uri) )
+ $uri .= '/';
+ $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
+ }
+
+ /** This filter is documented in wp-admin/edit-tag-form.php */
+ $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) );
+ $post->post_status = $original_status;
+ $post->post_date = $original_date;
+ $post->post_name = $original_name;
+ unset($post->filter);
+
+ /**
+ * Filter the sample permalink.
+ *
+ * @since 4.4.0
+ *
+ * @param string $permalink Sample permalink.
+ * @param int $post_id Post ID.
+ * @param string $title Post title.
+ * @param string $name Post name (slug).
+ * @param WP_Post $post Post object.
+ */
+ return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post );
+}
+
+/**
+ * Returns the HTML of the sample permalink slug editor.
+ *
+ * @since 2.5.0
+ *
+ * @param int $id Post ID or post object.
+ * @param string $new_title Optional. New title. Default null.
+ * @param string $new_slug Optional. New slug. Default null.
+ * @return string The HTML of the sample permalink slug editor.
+ */
+function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
+ $post = get_post( $id );
+ if ( ! $post )
+ return '';
+
+ list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
+
+ $view_link = false;
+ $preview_target = '';
+
+ if ( current_user_can( 'read_post', $post->ID ) ) {
+ if ( 'draft' === $post->post_status ) {
+ $draft_link = set_url_scheme( get_permalink( $post->ID ) );
+ $view_link = get_preview_post_link( $post, array(), $draft_link );
+ $preview_target = " target='wp-preview-{$post->ID}'";
+ } else {
+ if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
+ $view_link = get_permalink( $post );
+ } else {
+ // Allow non-published (private, future) to be viewed at a pretty permalink.
+ $view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, urldecode( $permalink ) );
+ }
+ }
+ }
+
+ // Permalinks without a post/page name placeholder don't have anything to edit
+ if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
+ $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
+
+ if ( false !== $view_link ) {
+ $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $view_link . "</a>\n";
+ } else {
+ $return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
+ }
+
+ // Encourage a pretty permalink setting
+ if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) {
+ $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
+ }
+ } else {
+ if ( function_exists( 'mb_strlen' ) ) {
+ if ( mb_strlen( $post_name ) > 34 ) {
+ $post_name_abridged = mb_substr( $post_name, 0, 16 ) . '…' . mb_substr( $post_name, -16 );
+ } else {
+ $post_name_abridged = $post_name;
+ }
+ } else {
+ if ( strlen( $post_name ) > 34 ) {
+ $post_name_abridged = substr( $post_name, 0, 16 ) . '…' . substr( $post_name, -16 );
+ } else {
+ $post_name_abridged = $post_name;
+ }
+ }
+
+ $post_name_html = '<span id="editable-post-name">' . $post_name_abridged . '</span>';
+ $display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, urldecode( $permalink ) );
+
+ $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
+ $return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n";
+ $return .= '‎'; // Fix bi-directional text display defect in RTL languages.
+ $return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>\n";
+ $return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
+ }
+
+ /**
+ * Filter the sample permalink HTML markup.
+ *
+ * @since 2.9.0
+ * @since 4.4.0 Added `$post` parameter.
+ *
+ * @param string $return Sample permalink HTML markup.
+ * @param int $post_id Post ID.
+ * @param string $new_title New sample permalink title.
+ * @param string $new_slug New sample permalink slug.
+ * @param WP_Post $post Post object.
+ */
+ $return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post );
+
+ return $return;
+}
+
+/**
+ * Output HTML for the post thumbnail meta-box.
+ *
+ * @since 2.9.0
+ *
+ * @global int $content_width
+ * @global array $_wp_additional_image_sizes
+ *
+ * @param int $thumbnail_id ID of the attachment used for thumbnail
+ * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post.
+ * @return string html
+ */
+function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
+ global $content_width, $_wp_additional_image_sizes;
+
+ $post = get_post( $post );
+ $post_type_object = get_post_type_object( $post->post_type );
+ $set_thumbnail_link = '<p class="hide-if-no-js"><a title="%s" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>';
+ $upload_iframe_src = get_upload_iframe_src( 'image', $post->ID );
+
+ $content = sprintf( $set_thumbnail_link,
+ esc_attr( $post_type_object->labels->set_featured_image ),
+ esc_url( $upload_iframe_src ),
+ esc_html( $post_type_object->labels->set_featured_image )
+ );
+
+ if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
+ $size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : array( 266, 266 );
+
+ /**
+ * Filter the size used to display the post thumbnail image in the 'Featured Image' meta box.
+ *
+ * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail'
+ * image size is registered, which differs from the 'thumbnail' image size
+ * managed via the Settings > Media screen. See the `$size` parameter description
+ * for more information on default values.
+ *
+ * @since 4.4.0
+ *
+ * @param string|array $size Post thumbnail image size to display in the meta box. Accepts any valid
+ * image size, or an array of width and height values in pixels (in that order).
+ * If the 'post-thumbnail' size is set, default is 'post-thumbnail'. Otherwise,
+ * default is an array with 266 as both the height and width values.
+ * @param int $thumbnail_id Post thumbnail attachment ID.
+ * @param WP_Post $post The post object associated with the thumbnail.
+ */
+ $size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post );
+
+ $thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size );
+
+ if ( !empty( $thumbnail_html ) ) {
+ $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID );
+ $content = sprintf( $set_thumbnail_link,
+ esc_attr( $post_type_object->labels->set_featured_image ),
+ esc_url( $upload_iframe_src ),
+ $thumbnail_html
+ );
+ $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
+ }
+ }
+
+ /**
+ * Filter the admin post thumbnail HTML markup to return.
+ *
+ * @since 2.9.0
+ *
+ * @param string $content Admin post thumbnail HTML markup.
+ * @param int $post_id Post ID.
+ */
+ return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID );
+}