]+)>[\s\S]+?<\/a>/', $content, $link_matches ) )
return;
- $i = 0;
- $search = "#[\s]+rel=(\"|')(.*?)wp-att-(\d+)\\1#i";
- foreach ( $anchor_matches[0] as $anchor ) {
- if ( 0 == preg_match( $search, $anchor, $id_matches ) )
- continue;
+ $site_url = get_bloginfo('url');
+ $site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s)
+ $replace = '';
- $id = (int) $id_matches[3];
+ foreach ( $link_matches[1] as $key => $value ) {
+ if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-')
+ || !preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
+ || !preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) )
+ continue;
- // While we have the attachment ID, let's adopt any orphans.
- $attachment = & get_post( $id, ARRAY_A );
- if ( ! empty( $attachment) && ! is_object( get_post( $attachment['post_parent'] ) ) ) {
- $attachment['post_parent'] = $post_ID;
- // Escape data pulled from DB.
- $attachment = add_magic_quotes( $attachment);
- wp_update_post( $attachment);
- }
+ $quote = $url_match[1]; // the quote (single or double)
+ $url_id = (int) $url_match[2];
+ $rel_id = (int) $rel_match[1];
- $post_search[$i] = $anchor;
- $post_replace[$i] = preg_replace( "#href=(\"|')[^'\"]*\\1#e", "stripslashes( 'href=\\1' ).get_attachment_link( $id ).stripslashes( '\\1' )", $anchor );
- ++$i;
- }
+ if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false )
+ continue;
- $post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] );
+ $link = $link_matches[0][$key];
+ $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
- // Escape data pulled from DB.
- $post = add_magic_quotes( $post);
+ $content = str_replace( $link, $replace, $content );
+ }
- return wp_update_post( $post);
+ if ( $replace ) {
+ $post['post_content'] = $content;
+ // Escape data pulled from DB.
+ $post = add_magic_quotes($post);
+
+ return wp_update_post($post);
+ }
}
-// Move child posts to a new parent
+/**
+ * Move child posts to a new parent.
+ *
+ * @since 2.3.0
+ * @access private
+ *
+ * @param unknown_type $old_ID
+ * @param unknown_type $new_ID
+ * @return unknown
+ */
function _relocate_children( $old_ID, $new_ID ) {
global $wpdb;
$old_ID = (int) $old_ID;
$new_ID = (int) $new_ID;
- return $wpdb->query( "UPDATE $wpdb->posts SET post_parent = $new_ID WHERE post_parent = $old_ID" );
+
+ $children = $wpdb->get_col( $wpdb->prepare("
+ SELECT post_id
+ FROM $wpdb->postmeta
+ WHERE meta_key = '_wp_attachment_temp_parent'
+ AND meta_value = %d", $old_ID) );
+
+ foreach ( $children as $child_id ) {
+ $wpdb->update($wpdb->posts, array('post_parent' => $new_ID), array('ID' => $child_id) );
+ delete_post_meta($child_id, '_wp_attachment_temp_parent');
+ }
+}
+
+/**
+ * Get all the possible statuses for a post_type
+ *
+ * @since 2.5.0
+ *
+ * @param string $type The post_type you want the statuses for
+ * @return array As array of all the statuses for the supplied post type
+ */
+function get_available_post_statuses($type = 'post') {
+ $stati = wp_count_posts($type);
+
+ return array_keys(get_object_vars($stati));
+}
+
+/**
+ * Run the wp query to fetch the posts for listing on the edit posts page
+ *
+ * @since 2.5.0
+ *
+ * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
+ * @return array
+ */
+function wp_edit_posts_query( $q = false ) {
+ if ( false === $q )
+ $q = $_GET;
+ $q['m'] = isset($q['m']) ? (int) $q['m'] : 0;
+ $q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0;
+ $post_stati = get_post_stati();
+
+ if ( isset($q['post_type']) && in_array( $q['post_type'], get_post_types() ) )
+ $post_type = $q['post_type'];
+ else
+ $post_type = 'post';
+
+ $avail_post_stati = get_available_post_statuses($post_type);
+
+ if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) {
+ $post_status = $q['post_status'];
+ $perm = 'readable';
+ }
+
+ if ( isset($q['orderby']) )
+ $orderby = $q['orderby'];
+ elseif ( isset($q['post_status']) && in_array($q['post_status'], array('pending', 'draft')) )
+ $orderby = 'modified';
+
+ if ( isset($q['order']) )
+ $order = $q['order'];
+ elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] )
+ $order = 'ASC';
+
+ $per_page = 'edit_' . $post_type . '_per_page';
+ $posts_per_page = (int) get_user_option( $per_page );
+ if ( empty( $posts_per_page ) || $posts_per_page < 1 )
+ $posts_per_page = 20;
+
+ $posts_per_page = apply_filters( $per_page, $posts_per_page );
+ $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
+
+ $query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page');
+
+ // Hierarchical types require special args.
+ if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) {
+ $query['orderby'] = 'menu_order title';
+ $query['order'] = 'asc';
+ $query['posts_per_page'] = -1;
+ $query['posts_per_archive_page'] = -1;
+ }
+
+ if ( ! empty( $q['show_sticky'] ) )
+ $query['post__in'] = (array) get_option( 'sticky_posts' );
+
+ wp( $query );
+
+ return $avail_post_stati;
+}
+
+/**
+ * Get default post mime types
+ *
+ * @since 2.9.0
+ *
+ * @return array
+ */
+function get_post_mime_types() {
+ $post_mime_types = array( // array( adj, noun )
+ 'image' => array(__('Images'), __('Manage Images'), _n_noop('Image (%s)', 'Images (%s)')),
+ 'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio (%s)', 'Audio (%s)')),
+ 'video' => array(__('Video'), __('Manage Video'), _n_noop('Video (%s)', 'Video (%s)')),
+ );
+
+ return apply_filters('post_mime_types', $post_mime_types);
+}
+
+/**
+ * {@internal Missing Short Description}}
+ *
+ * @since 2.5.0
+ *
+ * @param unknown_type $type
+ * @return unknown
+ */
+function get_available_post_mime_types($type = 'attachment') {
+ global $wpdb;
+
+ $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
+ return $types;
+}
+
+/**
+ * {@internal Missing Short Description}}
+ *
+ * @since 2.5.0
+ *
+ * @param unknown_type $q
+ * @return unknown
+ */
+function wp_edit_attachments_query( $q = false ) {
+ if ( false === $q )
+ $q = $_GET;
+
+ $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0;
+ $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
+ $q['post_type'] = 'attachment';
+ $post_type = get_post_type_object( 'attachment' );
+ $states = 'inherit';
+ if ( current_user_can( $post_type->cap->read_private_posts ) )
+ $states .= ',private';
+
+ $q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states;
+ $media_per_page = (int) get_user_option( 'upload_per_page' );
+ if ( empty( $media_per_page ) || $media_per_page < 1 )
+ $media_per_page = 20;
+ $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
+
+ $post_mime_types = get_post_mime_types();
+ $avail_post_mime_types = get_available_post_mime_types('attachment');
+
+ if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) )
+ unset($q['post_mime_type']);
+
+ if ( isset($q['detached']) )
+ add_filter('posts_where', '_edit_attachments_query_helper');
+
+ wp( $q );
+
+ if ( isset($q['detached']) )
+ remove_filter('posts_where', '_edit_attachments_query_helper');
+
+ return array($post_mime_types, $avail_post_mime_types);
+}
+
+function _edit_attachments_query_helper($where) {
+ global $wpdb;
+ return $where .= " AND {$wpdb->posts}.post_parent < 1";
+}
+
+/**
+ * Returns the list of classes to be used by a metabox
+ *
+ * @uses get_user_option()
+ * @since 2.5.0
+ *
+ * @param unknown_type $id
+ * @param unknown_type $page
+ * @return unknown
+ */
+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( '' );
+ }
+
+ $classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes );
+ return implode( ' ', $classes );
+}
+
+/**
+ * {@internal Missing Short Description}}
+ *
+ * @since 2.5.0
+ *
+ * @param int|object $id Post ID or post object.
+ * @param string $title (optional) Title
+ * @param string $name (optional) Name
+ * @return array With two entries of type string
+ */
+function get_sample_permalink($id, $title = null, $name = null) {
+ $post = &get_post($id);
+ if ( !$post->ID )
+ 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')) ) {
+ $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);
+ $uri = untrailingslashit($uri);
+ $uri = strrev( stristr( strrev( $uri ), '/' ) );
+ $uri = untrailingslashit($uri);
+ $uri = apply_filters( 'editable_slug', $uri );
+ if ( !empty($uri) )
+ $uri .= '/';
+ $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
+ }
+
+ $permalink = array($permalink, apply_filters('editable_slug', $post->post_name));
+ $post->post_status = $original_status;
+ $post->post_date = $original_date;
+ $post->post_name = $original_name;
+ unset($post->filter);
+
+ return $permalink;
}
-?>
\ No newline at end of file
+/**
+ * sample permalink html
+ *
+ * intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor.
+ *
+ * @since 2.5.0
+ *
+ * @param int|object $id Post ID or post object.
+ * @param string $new_title (optional) New title
+ * @param string $new_slug (optional) New slug
+ * @return string intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor.
+ */
+function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
+ global $wpdb;
+ $post = &get_post($id);
+
+ list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
+
+ if ( 'publish' == $post->post_status ) {
+ $ptype = get_post_type_object($post->post_type);
+ $view_post = $ptype->labels->view_item;
+ $title = __('Click to edit this part of the permalink');
+ } else {
+ $title = __('Temporary permalink. Click to edit this part.');
+ }
+
+ if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) {
+ $return = '' . __('Permalink:') . "\n" . '' . $permalink . "\n";
+ if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) )
+ $return .= '' . __('Change Permalinks') . "\n";
+ if ( isset($view_post) )
+ $return .= "$view_post\n";
+
+ $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
+
+ return $return;
+ }
+
+ if ( function_exists('mb_strlen') ) {
+ if ( mb_strlen($post_name) > 30 ) {
+ $post_name_abridged = mb_substr($post_name, 0, 14). '…' . mb_substr($post_name, -14);
+ } else {
+ $post_name_abridged = $post_name;
+ }
+ } else {
+ if ( strlen($post_name) > 30 ) {
+ $post_name_abridged = substr($post_name, 0, 14). '…' . substr($post_name, -14);
+ } else {
+ $post_name_abridged = $post_name;
+ }
+ }
+
+ $post_name_html = '' . $post_name_abridged . '';
+ $display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink);
+ $view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
+ $return = '' . __('Permalink:') . "\n";
+ $return .= '' . $display_link . "\n";
+ $return .= ''; // Fix bi-directional text display defect in RTL languages.
+ $return .= '' . __('Edit') . "\n";
+ $return .= '' . $post_name . "\n";
+ if ( isset($view_post) )
+ $return .= "$view_post\n";
+
+ $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
+
+ return $return;
+}
+
+/**
+ * Output HTML for the post thumbnail meta-box.
+ *
+ * @since 2.9.0
+ *
+ * @param int $thumbnail_id ID of the attachment used for thumbnail
+ * @param int $post_id ID of the post associated with the thumbnail, defaults to global $post_ID
+ * @return string html
+ */
+function _wp_post_thumbnail_html( $thumbnail_id = null, $post_id = null ) {
+ global $content_width, $_wp_additional_image_sizes, $post_ID;
+
+ if ( empty( $post_id ) )
+ $post_id = $post_ID;
+
+ $upload_iframe_src = esc_url( get_upload_iframe_src('image', $post_id) );
+ $set_thumbnail_link = '%s
';
+ $content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) );
+
+ if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
+ $old_content_width = $content_width;
+ $content_width = 266;
+ if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
+ $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
+ else
+ $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
+ if ( !empty( $thumbnail_html ) ) {
+ $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$post_id" );
+ $content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
+ $content .= '' . esc_html__( 'Remove featured image' ) . '
';
+ }
+ $content_width = $old_content_width;
+ }
+
+ return apply_filters( 'admin_post_thumbnail_html', $content );
+}
+
+/**
+ * Check to see if the post is currently being edited by another user.
+ *
+ * @since 2.5.0
+ *
+ * @param int $post_id ID of the post to check for editing
+ * @return bool|int False: not locked or locked by current user. Int: user ID of user with lock.
+ */
+function wp_check_post_lock( $post_id ) {
+ if ( !$post = get_post( $post_id ) )
+ return false;
+
+ if ( !$lock = get_post_meta( $post->ID, '_edit_lock', true ) )
+ return false;
+
+ $lock = explode( ':', $lock );
+ $time = $lock[0];
+ $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
+
+ $time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 );
+
+ if ( $time && $time > time() - $time_window && $user != get_current_user_id() )
+ return $user;
+ return false;
+}
+
+/**
+ * Mark the post as currently being edited by the current user
+ *
+ * @since 2.5.0
+ *
+ * @param int $post_id ID of the post to being edited
+ * @return bool|array Returns false if the post doesn't exist of there is no current user, or
+ * an array of the lock time and the user ID.
+ */
+function wp_set_post_lock( $post_id ) {
+ if ( !$post = get_post( $post_id ) )
+ return false;
+ if ( 0 == ($user_id = get_current_user_id()) )
+ return false;
+
+ $now = time();
+ $lock = "$now:$user_id";
+
+ update_post_meta( $post->ID, '_edit_lock', $lock );
+ return array( $now, $user_id );
+}
+
+/**
+ * Outputs the notice message to say that someone else is editing this post at the moment.
+ *
+ * @since 2.8.5
+ * @return none
+ */
+function _admin_notice_post_locked() {
+ global $post;
+
+ $lock = explode( ':', get_post_meta( $post->ID, '_edit_lock', true ) );
+ $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
+ $last_user = get_userdata( $user );
+ $last_user_name = $last_user ? $last_user->display_name : __('Somebody');
+
+ switch ($post->post_type) {
+ case 'post':
+ $message = __( 'Warning: %s is currently editing this post' );
+ break;
+ case 'page':
+ $message = __( 'Warning: %s is currently editing this page' );
+ break;
+ default:
+ $message = __( 'Warning: %s is currently editing this.' );
+ }
+
+ $message = sprintf( $message, esc_html( $last_user_name ) );
+ echo "";
+}
+
+/**
+ * Creates autosave data for the specified post from $_POST data.
+ *
+ * @package WordPress
+ * @subpackage Post_Revisions
+ * @since 2.6.0
+ *
+ * @uses _wp_translate_postdata()
+ * @uses _wp_post_revision_fields()
+ *
+ * @return unknown
+ */
+function wp_create_post_autosave( $post_id ) {
+ $translated = _wp_translate_postdata( true );
+ if ( is_wp_error( $translated ) )
+ return $translated;
+
+ // Only store one autosave. If there is already an autosave, overwrite it.
+ if ( $old_autosave = wp_get_post_autosave( $post_id ) ) {
+ $new_autosave = _wp_post_revision_fields( $_POST, true );
+ $new_autosave['ID'] = $old_autosave->ID;
+ $new_autosave['post_author'] = get_current_user_id();
+ return wp_update_post( $new_autosave );
+ }
+
+ // _wp_put_post_revision() expects unescaped.
+ $_POST = stripslashes_deep($_POST);
+
+ // Otherwise create the new autosave as a special post revision
+ return _wp_put_post_revision( $_POST, true );
+}
+
+/**
+ * Save draft or manually autosave for showing preview.
+ *
+ * @package WordPress
+ * @since 2.7.0
+ *
+ * @uses get_post_status()
+ * @uses edit_post()
+ * @uses get_post()
+ * @uses current_user_can()
+ * @uses wp_die()
+ * @uses wp_create_post_autosave()
+ * @uses add_query_arg()
+ * @uses wp_create_nonce()
+ *
+ * @return str URL to redirect to show the preview
+ */
+function post_preview() {
+
+ $post_ID = (int) $_POST['post_ID'];
+ $status = get_post_status( $post_ID );
+ if ( 'auto-draft' == $status )
+ wp_die( __('Preview not available. Please save as a draft first.') );
+
+ if ( isset($_POST['catslist']) )
+ $_POST['post_category'] = explode(",", $_POST['catslist']);
+
+ if ( isset($_POST['tags_input']) )
+ $_POST['tags_input'] = explode(",", $_POST['tags_input']);
+
+ if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
+ unset($_POST['post_category']);
+
+ $_POST['ID'] = $post_ID;
+ $post = get_post($post_ID);
+
+ if ( 'page' == $post->post_type ) {
+ if ( !current_user_can('edit_page', $post_ID) )
+ wp_die(__('You are not allowed to edit this page.'));
+ } else {
+ if ( !current_user_can('edit_post', $post_ID) )
+ wp_die(__('You are not allowed to edit this post.'));
+ }
+
+ if ( 'draft' == $post->post_status ) {
+ $id = edit_post();
+ } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
+ $id = wp_create_post_autosave( $post->ID );
+ if ( ! is_wp_error($id) )
+ $id = $post->ID;
+ }
+
+ if ( is_wp_error($id) )
+ wp_die( $id->get_error_message() );
+
+ if ( $_POST['post_status'] == 'draft' ) {
+ $url = add_query_arg( 'preview', 'true', get_permalink($id) );
+ } else {
+ $nonce = wp_create_nonce('post_preview_' . $id);
+ $url = add_query_arg( array( 'preview' => 'true', 'preview_id' => $id, 'preview_nonce' => $nonce ), get_permalink($id) );
+ }
+
+ return $url;
+}