web/wp-admin/includes/post.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    13  *
    13  *
    14  * @package WordPress
    14  * @package WordPress
    15  * @since 2.6.0
    15  * @since 2.6.0
    16  *
    16  *
    17  * @param bool $update Are we updating a pre-existing post?
    17  * @param bool $update Are we updating a pre-existing post?
    18  * @param post_data array Array of post data. Defaults to the contents of $_POST.
    18  * @param array $post_data Array of post data. Defaults to the contents of $_POST.
    19  * @return object|bool WP_Error on failure, true on success.
    19  * @return object|bool WP_Error on failure, true on success.
    20  */
    20  */
    21 function _wp_translate_postdata( $update = false, $post_data = null ) {
    21 function _wp_translate_postdata( $update = false, $post_data = null ) {
    22 
    22 
    23 	if ( empty($post_data) )
    23 	if ( empty($post_data) )
    24 		$post_data = &$_POST;
    24 		$post_data = &$_POST;
    25 
    25 
    26 	if ( $update )
    26 	if ( $update )
    27 		$post_data['ID'] = (int) $post_data['post_ID'];
    27 		$post_data['ID'] = (int) $post_data['post_ID'];
    28 	$post_data['post_content'] = isset($post_data['content']) ? $post_data['content'] : '';
    28 
    29 	$post_data['post_excerpt'] = isset($post_data['excerpt']) ? $post_data['excerpt'] : '';
    29 	if ( isset( $post_data['content'] ) )
    30 	$post_data['post_parent'] = isset($post_data['parent_id'])? $post_data['parent_id'] : '';
    30 		$post_data['post_content'] = $post_data['content'];
       
    31 
       
    32 	if ( isset( $post_data['excerpt'] ) )
       
    33 		$post_data['post_excerpt'] = $post_data['excerpt'];
       
    34 
       
    35 	if ( isset( $post_data['parent_id'] ) )
       
    36 		$post_data['post_parent'] = (int) $post_data['parent_id'];
       
    37 
    31 	if ( isset($post_data['trackback_url']) )
    38 	if ( isset($post_data['trackback_url']) )
    32 		$post_data['to_ping'] = $post_data['trackback_url'];
    39 		$post_data['to_ping'] = $post_data['trackback_url'];
       
    40 
       
    41 	if ( !isset($post_data['user_ID']) )
       
    42 		$post_data['user_ID'] = $GLOBALS['user_ID'];
    33 
    43 
    34 	if (!empty ( $post_data['post_author_override'] ) ) {
    44 	if (!empty ( $post_data['post_author_override'] ) ) {
    35 		$post_data['post_author'] = (int) $post_data['post_author_override'];
    45 		$post_data['post_author'] = (int) $post_data['post_author_override'];
    36 	} else {
    46 	} else {
    37 		if (!empty ( $post_data['post_author'] ) ) {
    47 		if (!empty ( $post_data['post_author'] ) ) {
    39 		} else {
    49 		} else {
    40 			$post_data['post_author'] = (int) $post_data['user_ID'];
    50 			$post_data['post_author'] = (int) $post_data['user_ID'];
    41 		}
    51 		}
    42 	}
    52 	}
    43 
    53 
       
    54 	$ptype = get_post_type_object( $post_data['post_type'] );
    44 	if ( isset($post_data['user_ID']) && ($post_data['post_author'] != $post_data['user_ID']) ) {
    55 	if ( isset($post_data['user_ID']) && ($post_data['post_author'] != $post_data['user_ID']) ) {
    45 		if ( 'page' == $post_data['post_type'] ) {
    56 		if ( !current_user_can( $ptype->cap->edit_others_posts ) ) {
    46 			if ( !current_user_can( 'edit_others_pages' ) ) {
    57 			if ( 'page' == $post_data['post_type'] ) {
    47 				return new WP_Error( 'edit_others_pages', $update ?
    58 				return new WP_Error( 'edit_others_pages', $update ?
    48 					__( 'You are not allowed to edit pages as this user.' ) :
    59 					__( 'You are not allowed to edit pages as this user.' ) :
    49 					__( 'You are not allowed to create pages as this user.' )
    60 					__( 'You are not allowed to create pages as this user.' )
    50 				);
    61 				);
    51 			}
    62 			} else {
    52 		} else {
       
    53 			if ( !current_user_can( 'edit_others_posts' ) ) {
       
    54 				return new WP_Error( 'edit_others_posts', $update ?
    63 				return new WP_Error( 'edit_others_posts', $update ?
    55 					__( 'You are not allowed to edit posts as this user.' ) :
    64 					__( 'You are not allowed to edit posts as this user.' ) :
    56 					__( 'You are not allowed to post as this user.' )
    65 					__( 'You are not allowed to post as this user.' )
    57 				);
    66 				);
    58 			}
    67 			}
    62 	// What to do based on which button they pressed
    71 	// What to do based on which button they pressed
    63 	if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
    72 	if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
    64 		$post_data['post_status'] = 'draft';
    73 		$post_data['post_status'] = 'draft';
    65 	if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
    74 	if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
    66 		$post_data['post_status'] = 'private';
    75 		$post_data['post_status'] = 'private';
    67 	if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( $post_data['post_status'] != 'private' ) )
    76 	if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) )
    68 		$post_data['post_status'] = 'publish';
    77 		$post_data['post_status'] = 'publish';
    69 	if ( isset($post_data['advanced']) && '' != $post_data['advanced'] )
    78 	if ( isset($post_data['advanced']) && '' != $post_data['advanced'] )
    70 		$post_data['post_status'] = 'draft';
    79 		$post_data['post_status'] = 'draft';
    71 	if ( isset($post_data['pending']) && '' != $post_data['pending'] )
    80 	if ( isset($post_data['pending']) && '' != $post_data['pending'] )
    72 		$post_data['post_status'] = 'pending';
    81 		$post_data['post_status'] = 'pending';
    73 
    82 
    74 	$previous_status = get_post_field('post_status',  isset($post_data['ID']) ? $post_data['ID'] : $post_data['temp_ID']);
    83 	if ( isset( $post_data['ID'] ) )
       
    84 		$post_id = $post_data['ID'];
       
    85 	else
       
    86 		$post_id = false;
       
    87 	$previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
    75 
    88 
    76 	// Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
    89 	// Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
    77 	// Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
    90 	// Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
    78 	if ( 'page' == $post_data['post_type'] ) {
    91 	if ( isset($post_data['post_status']) && ('publish' == $post_data['post_status'] && !current_user_can( $ptype->cap->publish_posts )) )
    79 		$publish_cap = 'publish_pages';
    92 		if ( $previous_status != 'publish' || !current_user_can( 'edit_post', $post_id ) )
    80 		$edit_cap = 'edit_published_pages';
       
    81 	} else {
       
    82 		$publish_cap = 'publish_posts';
       
    83 		$edit_cap = 'edit_published_posts';
       
    84 	}
       
    85 	if ( isset($post_data['post_status']) && ('publish' == $post_data['post_status'] && !current_user_can( $publish_cap )) )
       
    86 		if ( $previous_status != 'publish' || !current_user_can( $edit_cap ) )
       
    87 			$post_data['post_status'] = 'pending';
    93 			$post_data['post_status'] = 'pending';
    88 
    94 
    89 	if ( ! isset($post_data['post_status']) )
    95 	if ( ! isset($post_data['post_status']) )
    90 		$post_data['post_status'] = $previous_status;
    96 		$post_data['post_status'] = $previous_status;
    91 
    97 
   124 }
   130 }
   125 
   131 
   126 /**
   132 /**
   127  * Update an existing post with values provided in $_POST.
   133  * Update an existing post with values provided in $_POST.
   128  *
   134  *
   129  * @since unknown
   135  * @since 1.5.0
   130  *
   136  *
   131  * @param array $post_data Optional.
   137  * @param array $post_data Optional.
   132  * @return int Post ID.
   138  * @return int Post ID.
   133  */
   139  */
   134 function edit_post( $post_data = null ) {
   140 function edit_post( $post_data = null ) {
   135 
   141 
   136 	if ( empty($post_data) )
   142 	if ( empty($post_data) )
   137 		$post_data = &$_POST;
   143 		$post_data = &$_POST;
   138 
   144 
       
   145 	// Clear out any data in internal vars.
       
   146 	unset( $post_data['filter'] );
       
   147 
   139 	$post_ID = (int) $post_data['post_ID'];
   148 	$post_ID = (int) $post_data['post_ID'];
   140 
   149 	$post = get_post( $post_ID );
   141 	if ( 'page' == $post_data['post_type'] ) {
   150 	$post_data['post_type'] = $post->post_type;
   142 		if ( !current_user_can( 'edit_page', $post_ID ) )
   151 	$post_data['post_mime_type'] = $post->post_mime_type;
       
   152 
       
   153 	$ptype = get_post_type_object($post_data['post_type']);
       
   154 	if ( !current_user_can( $ptype->cap->edit_post, $post_ID ) ) {
       
   155 		if ( 'page' == $post_data['post_type'] )
   143 			wp_die( __('You are not allowed to edit this page.' ));
   156 			wp_die( __('You are not allowed to edit this page.' ));
   144 	} else {
   157 		else
   145 		if ( !current_user_can( 'edit_post', $post_ID ) )
       
   146 			wp_die( __('You are not allowed to edit this post.' ));
   158 			wp_die( __('You are not allowed to edit this post.' ));
   147 	}
   159 	}
   148 
   160 
   149 	// Autosave shouldn't save too soon after a real save
   161 	// Autosave shouldn't save too soon after a real save
   150 	if ( 'autosave' == $post_data['action'] ) {
   162 	if ( 'autosave' == $post_data['action'] ) {
   157 	}
   169 	}
   158 
   170 
   159 	$post_data = _wp_translate_postdata( true, $post_data );
   171 	$post_data = _wp_translate_postdata( true, $post_data );
   160 	if ( is_wp_error($post_data) )
   172 	if ( is_wp_error($post_data) )
   161 		wp_die( $post_data->get_error_message() );
   173 		wp_die( $post_data->get_error_message() );
       
   174 	if ( 'autosave' != $post_data['action'] && 'auto-draft' == $post_data['post_status'] )
       
   175 		$post_data['post_status'] = 'draft';
   162 
   176 
   163 	if ( isset($post_data['visibility']) ) {
   177 	if ( isset($post_data['visibility']) ) {
   164 		switch ( $post_data['visibility'] ) {
   178 		switch ( $post_data['visibility'] ) {
   165 			case 'public' :
   179 			case 'public' :
   166 				$post_data['post_password'] = '';
   180 				$post_data['post_password'] = '';
   174 				unset( $post_data['sticky'] );
   188 				unset( $post_data['sticky'] );
   175 				break;
   189 				break;
   176 		}
   190 		}
   177 	}
   191 	}
   178 
   192 
       
   193 	// Post Formats
       
   194 	if ( isset( $post_data['post_format'] ) ) {
       
   195 		if ( current_theme_supports( 'post-formats', $post_data['post_format'] ) )
       
   196 			set_post_format( $post_ID, $post_data['post_format'] );
       
   197 		elseif ( '0' == $post_data['post_format'] )
       
   198 			set_post_format( $post_ID, false );
       
   199 	}
       
   200 
   179 	// Meta Stuff
   201 	// Meta Stuff
   180 	if ( isset($post_data['meta']) && $post_data['meta'] ) {
   202 	if ( isset($post_data['meta']) && $post_data['meta'] ) {
   181 		foreach ( $post_data['meta'] as $key => $value )
   203 		foreach ( $post_data['meta'] as $key => $value ) {
       
   204 			if ( !$meta = get_post_meta_by_id( $key ) )
       
   205 				continue;
       
   206 			if ( $meta->post_id != $post_ID )
       
   207 				continue;
       
   208 			if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) )
       
   209 				continue;
   182 			update_meta( $key, $value['key'], $value['value'] );
   210 			update_meta( $key, $value['key'], $value['value'] );
       
   211 		}
   183 	}
   212 	}
   184 
   213 
   185 	if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) {
   214 	if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) {
   186 		foreach ( $post_data['deletemeta'] as $key => $value )
   215 		foreach ( $post_data['deletemeta'] as $key => $value ) {
       
   216 			if ( !$meta = get_post_meta_by_id( $key ) )
       
   217 				continue;
       
   218 			if ( $meta->post_id != $post_ID )
       
   219 				continue;
       
   220 			if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) )
       
   221 				continue;
   187 			delete_meta( $key );
   222 			delete_meta( $key );
       
   223 		}
   188 	}
   224 	}
   189 
   225 
   190 	add_meta( $post_ID );
   226 	add_meta( $post_ID );
   191 
   227 
       
   228 	update_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
       
   229 
   192 	wp_update_post( $post_data );
   230 	wp_update_post( $post_data );
   193 
       
   194 	// Reunite any orphaned attachments with their parent
       
   195 	if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
       
   196 		$draft_ids = array();
       
   197 	if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )
       
   198 		_relocate_children( $draft_temp_id, $post_ID );
       
   199 
   231 
   200 	// Now that we have an ID we can fix any attachment anchor hrefs
   232 	// Now that we have an ID we can fix any attachment anchor hrefs
   201 	_fix_attachment_links( $post_ID );
   233 	_fix_attachment_links( $post_ID );
   202 
   234 
   203 	wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );
   235 	wp_set_post_lock( $post_ID );
   204 
   236 
   205 	if ( current_user_can( 'edit_others_posts' ) ) {
   237 	if ( current_user_can( $ptype->cap->edit_others_posts ) ) {
   206 		if ( !empty($post_data['sticky']) )
   238 		if ( ! empty( $post_data['sticky'] ) )
   207 			stick_post($post_ID);
   239 			stick_post( $post_ID );
   208 		else
   240 		else
   209 			unstick_post($post_ID);
   241 			unstick_post( $post_ID );
   210 	}
   242 	}
   211 
   243 
   212 	return $post_ID;
   244 	return $post_ID;
   213 }
   245 }
   214 
   246 
   215 /**
   247 /**
   216  * {@internal Missing Short Description}}
   248  * Process the post data for the bulk editing of posts.
   217  *
   249  *
   218  * Updates all bulk edited posts/pages, adding (but not removing) tags and
   250  * Updates all bulk edited posts/pages, adding (but not removing) tags and
   219  * categories. Skips pages when they would be their own parent or child.
   251  * categories. Skips pages when they would be their own parent or child.
   220  *
   252  *
   221  * @since unknown
   253  * @since 2.7.0
   222  *
   254  *
       
   255  * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal.
   223  * @return array
   256  * @return array
   224  */
   257  */
   225 function bulk_edit_posts( $post_data = null ) {
   258 function bulk_edit_posts( $post_data = null ) {
   226 	global $wpdb;
   259 	global $wpdb;
   227 
   260 
   228 	if ( empty($post_data) )
   261 	if ( empty($post_data) )
   229 		$post_data = &$_POST;
   262 		$post_data = &$_POST;
   230 
   263 
   231 	if ( isset($post_data['post_type']) && 'page' == $post_data['post_type'] ) {
   264 	if ( isset($post_data['post_type']) )
   232 		if ( ! current_user_can( 'edit_pages' ) )
   265 		$ptype = get_post_type_object($post_data['post_type']);
   233 			wp_die( __('You are not allowed to edit pages.') );
   266 	else
   234 	} else {
   267 		$ptype = get_post_type_object('post');
   235 		if ( ! current_user_can( 'edit_posts' ) )
   268 
   236 			wp_die( __('You are not allowed to edit posts.') );
   269 	if ( !current_user_can( $ptype->cap->edit_posts ) ) {
       
   270 		if ( 'page' == $ptype->name )
       
   271 			wp_die( __('You are not allowed to edit pages.'));
       
   272 		else
       
   273 			wp_die( __('You are not allowed to edit posts.'));
   237 	}
   274 	}
   238 
   275 
   239 	if ( -1 == $post_data['_status'] ) {
   276 	if ( -1 == $post_data['_status'] ) {
   240 		$post_data['post_status'] = null;
   277 		$post_data['post_status'] = null;
   241 		unset($post_data['post_status']);
   278 		unset($post_data['post_status']);
   244 	}
   281 	}
   245 	unset($post_data['_status']);
   282 	unset($post_data['_status']);
   246 
   283 
   247 	$post_IDs = array_map( 'intval', (array) $post_data['post'] );
   284 	$post_IDs = array_map( 'intval', (array) $post_data['post'] );
   248 
   285 
   249 	$reset = array( 'post_author', 'post_status', 'post_password', 'post_parent', 'page_template', 'comment_status', 'ping_status', 'keep_private', 'tags_input', 'post_category', 'sticky' );
   286 	$reset = array( 'post_author', 'post_status', 'post_password', 'post_parent', 'page_template', 'comment_status', 'ping_status', 'keep_private', 'tax_input', 'post_category', 'sticky' );
   250 	foreach ( $reset as $field ) {
   287 	foreach ( $reset as $field ) {
   251 		if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) )
   288 		if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) )
   252 			unset($post_data[$field]);
   289 			unset($post_data[$field]);
   253 	}
   290 	}
   254 
   291 
   257 			$new_cats = array_map( 'absint', $post_data['post_category'] );
   294 			$new_cats = array_map( 'absint', $post_data['post_category'] );
   258 		else
   295 		else
   259 			unset($post_data['post_category']);
   296 			unset($post_data['post_category']);
   260 	}
   297 	}
   261 
   298 
   262 	if ( isset($post_data['tags_input']) ) {
   299 	$tax_input = array();
   263 		$new_tags = preg_replace( '/\s*,\s*/', ',', rtrim( trim($post_data['tags_input']), ' ,' ) );
   300 	if ( isset($post_data['tax_input'])) {
   264 		$new_tags = explode(',', $new_tags);
   301 		foreach ( $post_data['tax_input'] as $tax_name => $terms ) {
       
   302 			if ( empty($terms) )
       
   303 				continue;
       
   304 			if ( is_taxonomy_hierarchical( $tax_name ) ) {
       
   305 				$tax_input[ $tax_name ] = array_map( 'absint', $terms );
       
   306 			} else {
       
   307 				$comma = _x( ',', 'tag delimiter' );
       
   308 				if ( ',' !== $comma )
       
   309 					$terms = str_replace( $comma, ',', $terms );
       
   310 				$tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
       
   311 			}
       
   312 		}
   265 	}
   313 	}
   266 
   314 
   267 	if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) {
   315 	if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) {
   268 		$pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'");
   316 		$pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'");
   269 		$children = array();
   317 		$children = array();
   278 				}
   326 				}
   279 			}
   327 			}
   280 		}
   328 		}
   281 	}
   329 	}
   282 
   330 
       
   331 	if ( isset( $post_data['post_format'] ) ) {
       
   332 		if ( '0' == $post_data['post_format'] )
       
   333 			$post_data['post_format'] = false;
       
   334 		// don't change the post format if it's not supported or not '0' (standard)
       
   335 		elseif ( ! current_theme_supports( 'post-formats', $post_data['post_format'] ) )
       
   336 			unset( $post_data['post_format'] );
       
   337 	}
       
   338 
   283 	$updated = $skipped = $locked = array();
   339 	$updated = $skipped = $locked = array();
   284 	foreach ( $post_IDs as $post_ID ) {
   340 	foreach ( $post_IDs as $post_ID ) {
   285 
   341 		$post_type_object = get_post_type_object( get_post_type( $post_ID ) );
   286 		if ( isset($children) && in_array($post_ID, $children) ) {
   342 
       
   343 		if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( $post_type_object->cap->edit_post, $post_ID ) ) {
   287 			$skipped[] = $post_ID;
   344 			$skipped[] = $post_ID;
   288 			continue;
   345 			continue;
   289 		}
   346 		}
   290 
   347 
   291 		if ( wp_check_post_lock( $post_ID ) ) {
   348 		if ( wp_check_post_lock( $post_ID ) ) {
   292 			$locked[] = $post_ID;
   349 			$locked[] = $post_ID;
   293 			continue;
   350 			continue;
   294 		}
   351 		}
   295 
   352 
   296 		if ( isset($new_cats) ) {
   353 		$post = get_post( $post_ID );
       
   354 		$tax_names = get_object_taxonomies( $post );
       
   355 		foreach ( $tax_names as $tax_name ) {
       
   356 			$taxonomy_obj = get_taxonomy($tax_name);
       
   357 			if ( isset( $tax_input[$tax_name]) && current_user_can( $taxonomy_obj->cap->assign_terms ) )
       
   358 				$new_terms = $tax_input[$tax_name];
       
   359 			else
       
   360 				$new_terms = array();
       
   361 
       
   362 			if ( $taxonomy_obj->hierarchical )
       
   363 				$current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'ids') );
       
   364 			else
       
   365 				$current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'names') );
       
   366 
       
   367 			$post_data['tax_input'][$tax_name] = array_merge( $current_terms, $new_terms );
       
   368 		}
       
   369 
       
   370 		if ( isset($new_cats) && in_array( 'category', $tax_names ) ) {
   297 			$cats = (array) wp_get_post_categories($post_ID);
   371 			$cats = (array) wp_get_post_categories($post_ID);
   298 			$post_data['post_category'] = array_unique( array_merge($cats, $new_cats) );
   372 			$post_data['post_category'] = array_unique( array_merge($cats, $new_cats) );
   299 		}
   373 			unset( $post_data['tax_input']['category'] );
   300 
   374 		}
   301 		if ( isset($new_tags) ) {
   375 
   302 			$tags = wp_get_post_tags($post_ID, array('fields' => 'names'));
   376 		$post_data['post_mime_type'] = $post->post_mime_type;
   303 			$post_data['tags_input'] = array_unique( array_merge($tags, $new_tags) );
   377 		$post_data['guid'] = $post->guid;
   304 		}
       
   305 
   378 
   306 		$post_data['ID'] = $post_ID;
   379 		$post_data['ID'] = $post_ID;
   307 		$updated[] = wp_update_post( $post_data );
   380 		$updated[] = wp_update_post( $post_data );
   308 
   381 
   309 		if ( isset( $post_data['sticky'] ) && current_user_can( 'edit_others_posts' ) ) {
   382 		if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
   310 			if ( 'sticky' == $post_data['sticky'] )
   383 			if ( 'sticky' == $post_data['sticky'] )
   311 				stick_post( $post_ID );
   384 				stick_post( $post_ID );
   312 			else
   385 			else
   313 				unstick_post( $post_ID );
   386 				unstick_post( $post_ID );
   314 		}
   387 		}
   315 
   388 
       
   389 		if ( isset( $post_data['post_format'] ) )
       
   390 			set_post_format( $post_ID, $post_data['post_format'] );
   316 	}
   391 	}
   317 
   392 
   318 	return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );
   393 	return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );
   319 }
   394 }
   320 
   395 
   321 /**
   396 /**
   322  * Default post information to use when populating the "Write Post" form.
   397  * Default post information to use when populating the "Write Post" form.
   323  *
   398  *
   324  * @since unknown
   399  * @since 2.0.0
   325  *
   400  *
   326  * @return unknown
   401  * @param string $post_type A post type string, defaults to 'post'.
   327  */
   402  * @return object stdClass object containing all the default post data as attributes
   328 function get_default_post_to_edit() {
   403  */
       
   404 function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
       
   405 	global $wpdb;
   329 
   406 
   330 	$post_title = '';
   407 	$post_title = '';
   331 	if ( !empty( $_REQUEST['post_title'] ) )
   408 	if ( !empty( $_REQUEST['post_title'] ) )
   332 		$post_title = esc_html( stripslashes( $_REQUEST['post_title'] ));
   409 		$post_title = esc_html( stripslashes( $_REQUEST['post_title'] ));
   333 
   410 
   337 
   414 
   338 	$post_excerpt = '';
   415 	$post_excerpt = '';
   339 	if ( !empty( $_REQUEST['excerpt'] ) )
   416 	if ( !empty( $_REQUEST['excerpt'] ) )
   340 		$post_excerpt = esc_html( stripslashes( $_REQUEST['excerpt'] ));
   417 		$post_excerpt = esc_html( stripslashes( $_REQUEST['excerpt'] ));
   341 
   418 
   342 	$post->ID = 0;
   419 	if ( $create_in_db ) {
       
   420 		$post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
       
   421 		$post = get_post( $post_id );
       
   422 		if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
       
   423 			set_post_format( $post, get_option( 'default_post_format' ) );
       
   424 	} else {
       
   425 		$post = new stdClass;
       
   426 		$post->ID = 0;
       
   427 		$post->post_author = '';
       
   428 		$post->post_date = '';
       
   429 		$post->post_date_gmt = '';
       
   430 		$post->post_password = '';
       
   431 		$post->post_type = $post_type;
       
   432 		$post->post_status = 'draft';
       
   433 		$post->to_ping = '';
       
   434 		$post->pinged = '';
       
   435 		$post->comment_status = get_option( 'default_comment_status' );
       
   436 		$post->ping_status = get_option( 'default_ping_status' );
       
   437 		$post->post_pingback = get_option( 'default_pingback_flag' );
       
   438 		$post->post_category = get_option( 'default_category' );
       
   439 		$post->page_template = 'default';
       
   440 		$post->post_parent = 0;
       
   441 		$post->menu_order = 0;
       
   442 	}
       
   443 
       
   444 	$post->post_content = apply_filters( 'default_content', $post_content, $post );
       
   445 	$post->post_title   = apply_filters( 'default_title',   $post_title, $post   );
       
   446 	$post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post );
   343 	$post->post_name = '';
   447 	$post->post_name = '';
   344 	$post->post_author = '';
       
   345 	$post->post_date = '';
       
   346 	$post->post_date_gmt = '';
       
   347 	$post->post_password = '';
       
   348 	$post->post_status = 'draft';
       
   349 	$post->post_type = 'post';
       
   350 	$post->to_ping = '';
       
   351 	$post->pinged = '';
       
   352 	$post->comment_status = get_option( 'default_comment_status' );
       
   353 	$post->ping_status = get_option( 'default_ping_status' );
       
   354 	$post->post_pingback = get_option( 'default_pingback_flag' );
       
   355 	$post->post_category = get_option( 'default_category' );
       
   356 	$post->post_content = apply_filters( 'default_content', $post_content);
       
   357 	$post->post_title = apply_filters( 'default_title', $post_title );
       
   358 	$post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt);
       
   359 	$post->page_template = 'default';
       
   360 	$post->post_parent = 0;
       
   361 	$post->menu_order = 0;
       
   362 
   448 
   363 	return $post;
   449 	return $post;
   364 }
   450 }
   365 
   451 
   366 /**
   452 /**
   367  * {@internal Missing Short Description}}
   453  * Get the default page information to use.
   368  *
   454  *
   369  * @since unknown
   455  * @since 2.5.0
   370  *
   456  *
   371  * @return unknown
   457  * @return object stdClass object containing all the default post data as attributes
   372  */
   458  */
   373 function get_default_page_to_edit() {
   459 function get_default_page_to_edit() {
   374 	$page = get_default_post_to_edit();
   460 	$page = get_default_post_to_edit();
   375 	$page->post_type = 'page';
   461 	$page->post_type = 'page';
   376 	return $page;
   462 	return $page;
   377 }
   463 }
   378 
   464 
   379 /**
   465 /**
   380  * Get an existing post and format it for editing.
   466  * Get an existing post and format it for editing.
   381  *
   467  *
   382  * @since unknown
   468  * @since 2.0.0
   383  *
   469  *
   384  * @param unknown_type $id
   470  * @param unknown_type $id
   385  * @return unknown
   471  * @return unknown
   386  */
   472  */
   387 function get_post_to_edit( $id ) {
   473 function get_post_to_edit( $id ) {
   395 }
   481 }
   396 
   482 
   397 /**
   483 /**
   398  * Determine if a post exists based on title, content, and date
   484  * Determine if a post exists based on title, content, and date
   399  *
   485  *
   400  * @since unknown
   486  * @since 2.0.0
   401  *
   487  *
   402  * @param string $title Post title
   488  * @param string $title Post title
   403  * @param string $content Optional post content
   489  * @param string $content Optional post content
   404  * @param string $date Optional post date
   490  * @param string $date Optional post date
   405  * @return int Post ID if post exists, 0 otherwise.
   491  * @return int Post ID if post exists, 0 otherwise.
   436 }
   522 }
   437 
   523 
   438 /**
   524 /**
   439  * Creates a new post from the "Write Post" form using $_POST information.
   525  * Creates a new post from the "Write Post" form using $_POST information.
   440  *
   526  *
   441  * @since unknown
   527  * @since 2.1.0
   442  *
   528  *
   443  * @return unknown
   529  * @return unknown
   444  */
   530  */
   445 function wp_write_post() {
   531 function wp_write_post() {
   446 	global $user_ID;
   532 	global $user_ID;
   447 
   533 
   448 	if ( 'page' == $_POST['post_type'] ) {
   534 	if ( isset($_POST['post_type']) )
   449 		if ( !current_user_can( 'edit_pages' ) )
   535 		$ptype = get_post_type_object($_POST['post_type']);
   450 			return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this blog.' ) );
   536 	else
   451 	} else {
   537 		$ptype = get_post_type_object('post');
   452 		if ( !current_user_can( 'edit_posts' ) )
   538 
   453 			return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this blog.' ) );
   539 	if ( !current_user_can( $ptype->cap->edit_posts ) ) {
   454 	}
   540 		if ( 'page' == $ptype->name )
   455 
   541 			return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) );
   456 
   542 		else
   457 	// Check for autosave collisions
   543 			return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) );
   458 	$temp_id = false;
   544 	}
   459 	if ( isset($_POST['temp_ID']) ) {
   545 
   460 		$temp_id = (int) $_POST['temp_ID'];
   546 	$_POST['post_mime_type'] = '';
   461 		if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
   547 
   462 			$draft_ids = array();
   548 	// Clear out any data in internal vars.
   463 		foreach ( $draft_ids as $temp => $real )
   549 	unset( $_POST['filter'] );
   464 			if ( time() + $temp > 86400 ) // 1 day: $temp is equal to -1 * time( then )
   550 
   465 				unset($draft_ids[$temp]);
   551 	// Edit don't write if we have a post id.
   466 
   552 	if ( isset( $_POST['post_ID'] ) )
   467 		if ( isset($draft_ids[$temp_id]) ) { // Edit, don't write
   553 		return edit_post();
   468 			$_POST['post_ID'] = $draft_ids[$temp_id];
       
   469 			unset($_POST['temp_ID']);
       
   470 			update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
       
   471 			return edit_post();
       
   472 		}
       
   473 	}
       
   474 
   554 
   475 	$translated = _wp_translate_postdata( false );
   555 	$translated = _wp_translate_postdata( false );
   476 	if ( is_wp_error($translated) )
   556 	if ( is_wp_error($translated) )
   477 		return $translated;
   557 		return $translated;
   478 
   558 
   500 	if ( empty($post_ID) )
   580 	if ( empty($post_ID) )
   501 		return 0;
   581 		return 0;
   502 
   582 
   503 	add_meta( $post_ID );
   583 	add_meta( $post_ID );
   504 
   584 
   505 	// Reunite any orphaned attachments with their parent
   585 	add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
   506 	if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
       
   507 		$draft_ids = array();
       
   508 	if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )
       
   509 		_relocate_children( $draft_temp_id, $post_ID );
       
   510 	if ( $temp_id && $temp_id != $draft_temp_id )
       
   511 		_relocate_children( $temp_id, $post_ID );
       
   512 
       
   513 	// Update autosave collision detection
       
   514 	if ( $temp_id ) {
       
   515 		$draft_ids[$temp_id] = $post_ID;
       
   516 		update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
       
   517 	}
       
   518 
   586 
   519 	// Now that we have an ID we can fix any attachment anchor hrefs
   587 	// Now that we have an ID we can fix any attachment anchor hrefs
   520 	_fix_attachment_links( $post_ID );
   588 	_fix_attachment_links( $post_ID );
   521 
   589 
   522 	wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );
   590 	wp_set_post_lock( $post_ID );
   523 
   591 
   524 	return $post_ID;
   592 	return $post_ID;
   525 }
   593 }
   526 
   594 
   527 /**
   595 /**
   528  * Calls wp_write_post() and handles the errors.
   596  * Calls wp_write_post() and handles the errors.
   529  *
   597  *
   530  * @since unknown
   598  * @since 2.0.0
   531  *
   599 
       
   600  * @uses wp_write_post()
       
   601  * @uses is_wp_error()
       
   602  * @uses wp_die()
   532  * @return unknown
   603  * @return unknown
   533  */
   604  */
   534 function write_post() {
   605 function write_post() {
   535 	$result = wp_write_post();
   606 	$result = wp_write_post();
   536 	if( is_wp_error( $result ) )
   607 	if ( is_wp_error( $result ) )
   537 		wp_die( $result->get_error_message() );
   608 		wp_die( $result->get_error_message() );
   538 	else
   609 	else
   539 		return $result;
   610 		return $result;
   540 }
   611 }
   541 
   612 
   544 //
   615 //
   545 
   616 
   546 /**
   617 /**
   547  * {@internal Missing Short Description}}
   618  * {@internal Missing Short Description}}
   548  *
   619  *
   549  * @since unknown
   620  * @since 1.2.0
   550  *
   621  *
   551  * @param unknown_type $post_ID
   622  * @param unknown_type $post_ID
   552  * @return unknown
   623  * @return unknown
   553  */
   624  */
   554 function add_meta( $post_ID ) {
   625 function add_meta( $post_ID ) {
   555 	global $wpdb;
   626 	global $wpdb;
   556 	$post_ID = (int) $post_ID;
   627 	$post_ID = (int) $post_ID;
   557 
   628 
   558 	$protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
       
   559 
       
   560 	$metakeyselect = isset($_POST['metakeyselect']) ? stripslashes( trim( $_POST['metakeyselect'] ) ) : '';
   629 	$metakeyselect = isset($_POST['metakeyselect']) ? stripslashes( trim( $_POST['metakeyselect'] ) ) : '';
   561 	$metakeyinput = isset($_POST['metakeyinput']) ? stripslashes( trim( $_POST['metakeyinput'] ) ) : '';
   630 	$metakeyinput = isset($_POST['metakeyinput']) ? stripslashes( trim( $_POST['metakeyinput'] ) ) : '';
   562 	$metavalue = isset($_POST['metavalue']) ? maybe_serialize( stripslashes_deep( $_POST['metavalue'] ) ) : '';
   631 	$metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : '';
   563 	if ( is_string($metavalue) )
   632 	if ( is_string( $metavalue ) )
   564 		$metavalue = trim( $metavalue );
   633 		$metavalue = trim( $metavalue );
   565 
   634 
   566 	if ( ('0' === $metavalue || !empty ( $metavalue ) ) && ((('#NONE#' != $metakeyselect) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput) ) ) {
   635 	if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) {
   567 		// We have a key/value pair. If both the select and the
   636 		// We have a key/value pair. If both the select and the
   568 		// input for the key have data, the input takes precedence:
   637 		// input for the key have data, the input takes precedence:
   569 
   638 
   570  		if ('#NONE#' != $metakeyselect)
   639  		if ( '#NONE#' != $metakeyselect )
   571 			$metakey = $metakeyselect;
   640 			$metakey = $metakeyselect;
   572 
   641 
   573 		if ( $metakeyinput)
   642 		if ( $metakeyinput )
   574 			$metakey = $metakeyinput; // default
   643 			$metakey = $metakeyinput; // default
   575 
   644 
   576 		if ( in_array($metakey, $protected) )
   645 		if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) )
   577 			return false;
   646 			return false;
   578 
   647 
   579 		wp_cache_delete($post_ID, 'post_meta');
   648 		$metakey = esc_sql( $metakey );
   580 
   649 
   581 		$wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value ) VALUES (%s, %s, %s)", $post_ID, $metakey, $metavalue) );
   650 		return add_post_meta( $post_ID, $metakey, $metavalue );
   582 		do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, $metakey, $metavalue );
   651 	}
   583 
   652 
   584 		return $wpdb->insert_id;
       
   585 	}
       
   586 	return false;
   653 	return false;
   587 } // add_meta
   654 } // add_meta
   588 
   655 
   589 /**
   656 /**
   590  * {@internal Missing Short Description}}
   657  * {@internal Missing Short Description}}
   591  *
   658  *
   592  * @since unknown
   659  * @since 1.2.0
   593  *
   660  *
   594  * @param unknown_type $mid
   661  * @param unknown_type $mid
   595  * @return unknown
   662  * @return unknown
   596  */
   663  */
   597 function delete_meta( $mid ) {
   664 function delete_meta( $mid ) {
   598 	global $wpdb;
   665 	return delete_metadata_by_mid( 'post' , $mid );
   599 	$mid = (int) $mid;
       
   600 
       
   601 	$post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
       
   602 
       
   603 	do_action( 'delete_postmeta', $mid );
       
   604 	wp_cache_delete($post_id, 'post_meta');
       
   605 	$rval = $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
       
   606 	do_action( 'deleted_postmeta', $mid );
       
   607 
       
   608 	return $rval;
       
   609 }
   666 }
   610 
   667 
   611 /**
   668 /**
   612  * Get a list of previously defined keys.
   669  * Get a list of previously defined keys.
   613  *
   670  *
   614  * @since unknown
   671  * @since 1.2.0
   615  *
   672  *
   616  * @return unknown
   673  * @return unknown
   617  */
   674  */
   618 function get_meta_keys() {
   675 function get_meta_keys() {
   619 	global $wpdb;
   676 	global $wpdb;
   628 }
   685 }
   629 
   686 
   630 /**
   687 /**
   631  * {@internal Missing Short Description}}
   688  * {@internal Missing Short Description}}
   632  *
   689  *
   633  * @since unknown
   690  * @since 2.1.0
   634  *
   691  *
   635  * @param unknown_type $mid
   692  * @param unknown_type $mid
   636  * @return unknown
   693  * @return unknown
   637  */
   694  */
   638 function get_post_meta_by_id( $mid ) {
   695 function get_post_meta_by_id( $mid ) {
   639 	global $wpdb;
   696 	return get_metadata_by_mid( 'post', $mid );
   640 	$mid = (int) $mid;
       
   641 
       
   642 	$meta = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
       
   643 	if ( is_serialized_string( $meta->meta_value ) )
       
   644 		$meta->meta_value = maybe_unserialize( $meta->meta_value );
       
   645 	return $meta;
       
   646 }
   697 }
   647 
   698 
   648 /**
   699 /**
   649  * {@internal Missing Short Description}}
   700  * {@internal Missing Short Description}}
   650  *
   701  *
   651  * Some postmeta stuff.
   702  * Some postmeta stuff.
   652  *
   703  *
   653  * @since unknown
   704  * @since 1.2.0
   654  *
   705  *
   655  * @param unknown_type $postid
   706  * @param unknown_type $postid
   656  * @return unknown
   707  * @return unknown
   657  */
   708  */
   658 function has_meta( $postid ) {
   709 function has_meta( $postid ) {
   659 	global $wpdb;
   710 	global $wpdb;
   660 
   711 
   661 	return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
   712 	return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
   662 			FROM $wpdb->postmeta WHERE post_id = %d
   713 			FROM $wpdb->postmeta WHERE post_id = %d
   663 			ORDER BY meta_key,meta_id", $postid), ARRAY_A );
   714 			ORDER BY meta_key,meta_id", $postid), ARRAY_A );
   664 
       
   665 }
   715 }
   666 
   716 
   667 /**
   717 /**
   668  * {@internal Missing Short Description}}
   718  * {@internal Missing Short Description}}
   669  *
   719  *
   670  * @since unknown
   720  * @since 1.2.0
   671  *
   721  *
   672  * @param unknown_type $meta_id
   722  * @param unknown_type $meta_id
   673  * @param unknown_type $meta_key
   723  * @param unknown_type $meta_key Expect Slashed
   674  * @param unknown_type $meta_value
   724  * @param unknown_type $meta_value Expect Slashed
   675  * @return unknown
   725  * @return unknown
   676  */
   726  */
   677 function update_meta( $meta_id, $meta_key, $meta_value ) {
   727 function update_meta( $meta_id, $meta_key, $meta_value ) {
   678 	global $wpdb;
   728 	$meta_key = stripslashes( $meta_key );
   679 
   729 	$meta_value = stripslashes_deep( $meta_value );
   680 	$protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
   730 
   681 
   731 	return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key );
   682 	if ( in_array($meta_key, $protected) )
       
   683 		return false;
       
   684 
       
   685 	if ( '' === trim( $meta_value ) )
       
   686 		return false;
       
   687 
       
   688 	$post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $meta_id) );
       
   689 
       
   690 	$meta_value = maybe_serialize( stripslashes_deep( $meta_value ) );
       
   691 	$meta_id = (int) $meta_id;
       
   692 
       
   693 	$data  = compact( 'meta_key', 'meta_value' );
       
   694 	$where = compact( 'meta_id' );
       
   695 
       
   696 	do_action( 'update_postmeta', $meta_id, $post_id, $meta_key, $meta_value );
       
   697 	$rval = $wpdb->update( $wpdb->postmeta, $data, $where );
       
   698 	wp_cache_delete($post_id, 'post_meta');
       
   699 	do_action( 'updated_postmeta', $meta_id, $post_id, $meta_key, $meta_value );
       
   700 
       
   701 	return $rval;
       
   702 }
   732 }
   703 
   733 
   704 //
   734 //
   705 // Private
   735 // Private
   706 //
   736 //
   707 
   737 
   708 /**
   738 /**
   709  * Replace hrefs of attachment anchors with up-to-date permalinks.
   739  * Replace hrefs of attachment anchors with up-to-date permalinks.
   710  *
   740  *
   711  * @since unknown
   741  * @since 2.3.0
   712  * @access private
   742  * @access private
   713  *
   743  *
   714  * @param unknown_type $post_ID
   744  * @param unknown_type $post_ID
   715  * @return unknown
   745  * @return unknown
   716  */
   746  */
   717 function _fix_attachment_links( $post_ID ) {
   747 function _fix_attachment_links( $post_ID ) {
   718 	global $_fix_attachment_link_id;
       
   719 
       
   720 	$post = & get_post( $post_ID, ARRAY_A );
   748 	$post = & get_post( $post_ID, ARRAY_A );
   721 
   749 	$content = $post['post_content'];
   722 	$search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie";
   750 
   723 
   751 	// quick sanity check, don't run if no pretty permalinks or post is not published
   724 	// See if we have any rel="attachment" links
   752 	if ( !get_option('permalink_structure') || $post['post_status'] != 'publish' )
   725 	if ( 0 == preg_match_all( $search, $post['post_content'], $anchor_matches, PREG_PATTERN_ORDER ) )
       
   726 		return;
   753 		return;
   727 
   754 
   728 	$i = 0;
   755 	// Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero)
   729 	$search = "#[\s]+rel=(\"|')(.*?)wp-att-(\d+)\\1#i";
   756 	if ( !strpos($content, '?attachment_id=') || !preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) )
   730 	foreach ( $anchor_matches[0] as $anchor ) {
   757 		return;
   731 		if ( 0 == preg_match( $search, $anchor, $id_matches ) )
   758 
       
   759 	$site_url = get_bloginfo('url');
       
   760 	$site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s)
       
   761 	$replace = '';
       
   762 
       
   763 	foreach ( $link_matches[1] as $key => $value ) {
       
   764 		if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-')
       
   765 			|| !preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
       
   766 			|| !preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) )
       
   767 				continue;
       
   768 
       
   769 		$quote = $url_match[1]; // the quote (single or double)
       
   770 		$url_id = (int) $url_match[2];
       
   771 		$rel_id = (int) $rel_match[1];
       
   772 
       
   773 		if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false )
   732 			continue;
   774 			continue;
   733 
   775 
   734 		$id = (int) $id_matches[3];
   776 		$link = $link_matches[0][$key];
   735 
   777 		$replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
   736 		// While we have the attachment ID, let's adopt any orphans.
   778 
   737 		$attachment = & get_post( $id, ARRAY_A );
   779 		$content = str_replace( $link, $replace, $content );
   738 		if ( ! empty( $attachment) && ! is_object( get_post( $attachment['post_parent'] ) ) ) {
   780 	}
   739 			$attachment['post_parent'] = $post_ID;
   781 
   740 			// Escape data pulled from DB.
   782 	if ( $replace ) {
   741 			$attachment = add_magic_quotes( $attachment);
   783 		$post['post_content'] = $content;
   742 			wp_update_post( $attachment);
   784 		// Escape data pulled from DB.
   743 		}
   785 		$post = add_magic_quotes($post);
   744 
   786 
   745 		$post_search[$i] = $anchor;
   787 		return wp_update_post($post);
   746 		 $_fix_attachment_link_id = $id;
   788 	}
   747 		$post_replace[$i] = preg_replace_callback( "#href=(\"|')[^'\"]*\\1#", '_fix_attachment_links_replace_cb', $anchor );
       
   748 		++$i;
       
   749 	}
       
   750 
       
   751 	$post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] );
       
   752 
       
   753 	// Escape data pulled from DB.
       
   754 	$post = add_magic_quotes( $post);
       
   755 
       
   756 	return wp_update_post( $post);
       
   757 }
       
   758 
       
   759 function _fix_attachment_links_replace_cb($match) {
       
   760         global $_fix_attachment_link_id;
       
   761         return stripslashes( 'href='.$match[1] ).get_attachment_link( $_fix_attachment_link_id ).stripslashes( $match[1] );
       
   762 }
   789 }
   763 
   790 
   764 /**
   791 /**
   765  * Move child posts to a new parent.
   792  * Move child posts to a new parent.
   766  *
   793  *
   767  * @since unknown
   794  * @since 2.3.0
   768  * @access private
   795  * @access private
   769  *
   796  *
   770  * @param unknown_type $old_ID
   797  * @param unknown_type $old_ID
   771  * @param unknown_type $new_ID
   798  * @param unknown_type $new_ID
   772  * @return unknown
   799  * @return unknown
   787 		delete_post_meta($child_id, '_wp_attachment_temp_parent');
   814 		delete_post_meta($child_id, '_wp_attachment_temp_parent');
   788 	}
   815 	}
   789 }
   816 }
   790 
   817 
   791 /**
   818 /**
   792  * {@internal Missing Short Description}}
   819  * Get all the possible statuses for a post_type
   793  *
   820  *
   794  * @since unknown
   821  * @since 2.5.0
   795  *
   822  *
   796  * @param unknown_type $type
   823  * @param string $type The post_type you want the statuses for
   797  * @return unknown
   824  * @return array As array of all the statuses for the supplied post type
   798  */
   825  */
   799 function get_available_post_statuses($type = 'post') {
   826 function get_available_post_statuses($type = 'post') {
   800 	$stati = wp_count_posts($type);
   827 	$stati = wp_count_posts($type);
   801 
   828 
   802 	return array_keys(get_object_vars($stati));
   829 	return array_keys(get_object_vars($stati));
   803 }
   830 }
   804 
   831 
   805 /**
   832 /**
   806  * {@internal Missing Short Description}}
   833  * Run the wp query to fetch the posts for listing on the edit posts page
   807  *
   834  *
   808  * @since unknown
   835  * @since 2.5.0
   809  *
   836  *
   810  * @param unknown_type $q
   837  * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
   811  * @return unknown
   838  * @return array
   812  */
   839  */
   813 function wp_edit_posts_query( $q = false ) {
   840 function wp_edit_posts_query( $q = false ) {
   814 	if ( false === $q )
   841 	if ( false === $q )
   815 		$q = $_GET;
   842 		$q = $_GET;
   816 	$q['m']   = isset($q['m']) ? (int) $q['m'] : 0;
   843 	$q['m'] = isset($q['m']) ? (int) $q['m'] : 0;
   817 	$q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0;
   844 	$q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0;
   818 	$post_stati  = array(	//	array( adj, noun )
   845 	$post_stati  = get_post_stati();
   819 				'publish' => array(_x('Published', 'post'), __('Published posts'), _n_noop('Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>')),
   846 
   820 				'future' => array(_x('Scheduled', 'post'), __('Scheduled posts'), _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>')),
   847 	if ( isset($q['post_type']) && in_array( $q['post_type'], get_post_types() ) )
   821 				'pending' => array(_x('Pending Review', 'post'), __('Pending posts'), _n_noop('Pending Review <span class="count">(%s)</span>', 'Pending Review <span class="count">(%s)</span>')),
   848 		$post_type = $q['post_type'];
   822 				'draft' => array(_x('Draft', 'post'), _x('Drafts', 'manage posts header'), _n_noop('Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>')),
   849 	else
   823 				'private' => array(_x('Private', 'post'), __('Private posts'), _n_noop('Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>')),
   850 		$post_type = 'post';
   824 				'trash' => array(_x('Trash', 'post'), __('Trash posts'), _n_noop('Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>')),
   851 
   825 			);
   852 	$avail_post_stati = get_available_post_statuses($post_type);
   826 
   853 
   827 	$post_stati = apply_filters('post_stati', $post_stati);
   854 	if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) {
   828 
   855 		$post_status = $q['post_status'];
   829 	$avail_post_stati = get_available_post_statuses('post');
   856 		$perm = 'readable';
   830 
   857 	}
   831 	$post_status_q = '';
   858 
   832 	if ( isset($q['post_status']) && in_array( $q['post_status'], array_keys($post_stati) ) ) {
   859 	if ( isset($q['orderby']) )
   833 		$post_status_q = '&post_status=' . $q['post_status'];
   860 		$orderby = $q['orderby'];
   834 		$post_status_q .= '&perm=readable';
   861 	elseif ( isset($q['post_status']) && in_array($q['post_status'], array('pending', 'draft')) )
   835 	}
   862 		$orderby = 'modified';
   836 
   863 
   837 	if ( isset($q['post_status']) && 'pending' === $q['post_status'] ) {
   864 	if ( isset($q['order']) )
       
   865 		$order = $q['order'];
       
   866 	elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] )
   838 		$order = 'ASC';
   867 		$order = 'ASC';
   839 		$orderby = 'modified';
   868 
   840 	} elseif ( isset($q['post_status']) && 'draft' === $q['post_status'] ) {
   869 	$per_page = 'edit_' . $post_type . '_per_page';
   841 		$order = 'DESC';
   870 	$posts_per_page = (int) get_user_option( $per_page );
   842 		$orderby = 'modified';
       
   843 	} else {
       
   844 		$order = 'DESC';
       
   845 		$orderby = 'date';
       
   846 	}
       
   847 
       
   848 	$posts_per_page = (int) get_user_option( 'edit_per_page', 0, false );
       
   849 	if ( empty( $posts_per_page ) || $posts_per_page < 1 )
   871 	if ( empty( $posts_per_page ) || $posts_per_page < 1 )
   850 		$posts_per_page = 15;
   872 		$posts_per_page = 20;
   851 	$posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page );
   873 
   852 
   874 	$posts_per_page = apply_filters( $per_page, $posts_per_page );
   853 	wp("post_type=post&$post_status_q&posts_per_page=$posts_per_page&order=$order&orderby=$orderby");
   875 	$posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
   854 
   876 
   855 	return array($post_stati, $avail_post_stati);
   877 	$query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page');
       
   878 
       
   879 	// Hierarchical types require special args.
       
   880 	if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) {
       
   881 		$query['orderby'] = 'menu_order title';
       
   882 		$query['order'] = 'asc';
       
   883 		$query['posts_per_page'] = -1;
       
   884 		$query['posts_per_archive_page'] = -1;
       
   885 	}
       
   886 
       
   887 	if ( ! empty( $q['show_sticky'] ) )
       
   888 		$query['post__in'] = (array) get_option( 'sticky_posts' );
       
   889 
       
   890 	wp( $query );
       
   891 
       
   892 	return $avail_post_stati;
   856 }
   893 }
   857 
   894 
   858 /**
   895 /**
   859  * Get default post mime types
   896  * Get default post mime types
   860  *
   897  *
   873 }
   910 }
   874 
   911 
   875 /**
   912 /**
   876  * {@internal Missing Short Description}}
   913  * {@internal Missing Short Description}}
   877  *
   914  *
   878  * @since unknown
   915  * @since 2.5.0
   879  *
   916  *
   880  * @param unknown_type $type
   917  * @param unknown_type $type
   881  * @return unknown
   918  * @return unknown
   882  */
   919  */
   883 function get_available_post_mime_types($type = 'attachment') {
   920 function get_available_post_mime_types($type = 'attachment') {
   888 }
   925 }
   889 
   926 
   890 /**
   927 /**
   891  * {@internal Missing Short Description}}
   928  * {@internal Missing Short Description}}
   892  *
   929  *
   893  * @since unknown
   930  * @since 2.5.0
   894  *
   931  *
   895  * @param unknown_type $q
   932  * @param unknown_type $q
   896  * @return unknown
   933  * @return unknown
   897  */
   934  */
   898 function wp_edit_attachments_query( $q = false ) {
   935 function wp_edit_attachments_query( $q = false ) {
   900 		$q = $_GET;
   937 		$q = $_GET;
   901 
   938 
   902 	$q['m']   = isset( $q['m'] ) ? (int) $q['m'] : 0;
   939 	$q['m']   = isset( $q['m'] ) ? (int) $q['m'] : 0;
   903 	$q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
   940 	$q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
   904 	$q['post_type'] = 'attachment';
   941 	$q['post_type'] = 'attachment';
   905 	$q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : 'inherit';
   942 	$post_type = get_post_type_object( 'attachment' );
   906 	$media_per_page = (int) get_user_option( 'upload_per_page', 0, false );
   943 	$states = 'inherit';
       
   944 	if ( current_user_can( $post_type->cap->read_private_posts ) )
       
   945 		$states .= ',private';
       
   946 
       
   947 	$q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states;
       
   948 	$media_per_page = (int) get_user_option( 'upload_per_page' );
   907 	if ( empty( $media_per_page ) || $media_per_page < 1 )
   949 	if ( empty( $media_per_page ) || $media_per_page < 1 )
   908 		$media_per_page = 20;
   950 		$media_per_page = 20;
   909 	$q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
   951 	$q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
   910 
   952 
   911 	$post_mime_types = get_post_mime_types();
   953 	$post_mime_types = get_post_mime_types();
   912 	$avail_post_mime_types = get_available_post_mime_types('attachment');
   954 	$avail_post_mime_types = get_available_post_mime_types('attachment');
   913 
   955 
   914 	if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) )
   956 	if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) )
   915 		unset($q['post_mime_type']);
   957 		unset($q['post_mime_type']);
   916 
   958 
   917 	wp($q);
   959 	if ( isset($q['detached']) )
       
   960 		add_filter('posts_where', '_edit_attachments_query_helper');
       
   961 
       
   962 	wp( $q );
       
   963 
       
   964 	if ( isset($q['detached']) )
       
   965 		remove_filter('posts_where', '_edit_attachments_query_helper');
   918 
   966 
   919 	return array($post_mime_types, $avail_post_mime_types);
   967 	return array($post_mime_types, $avail_post_mime_types);
   920 }
   968 }
   921 
   969 
   922 /**
   970 function _edit_attachments_query_helper($where) {
   923  * {@internal Missing Short Description}}
   971 	global $wpdb;
   924  *
   972 	return $where .= " AND {$wpdb->posts}.post_parent < 1";
   925  * @since unknown
   973 }
       
   974 
       
   975 /**
       
   976  * Returns the list of classes to be used by a metabox
       
   977  *
       
   978  * @uses get_user_option()
       
   979  * @since 2.5.0
   926  *
   980  *
   927  * @param unknown_type $id
   981  * @param unknown_type $id
   928  * @param unknown_type $page
   982  * @param unknown_type $page
   929  * @return unknown
   983  * @return unknown
   930  */
   984  */
   931 function postbox_classes( $id, $page ) {
   985 function postbox_classes( $id, $page ) {
   932 	if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id )
   986 	if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) {
   933 		return '';
   987 		$classes = array( '' );
   934 	$current_user = wp_get_current_user();
   988 	} elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) {
   935 	if ( $closed = get_user_option('closedpostboxes_'.$page, 0, false ) ) {
   989 		if ( !is_array( $closed ) ) {
   936 		if ( !is_array( $closed ) ) return '';
   990 			$classes = array( '' );
   937 		return in_array( $id, $closed )? 'closed' : '';
   991 		} else {
       
   992 			$classes = in_array( $id, $closed ) ? array( 'closed' ) : array( '' );
       
   993 		}
   938 	} else {
   994 	} else {
   939 		return '';
   995 		$classes = array( '' );
   940 	}
   996 	}
       
   997 
       
   998 	$classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes );
       
   999 	return implode( ' ', $classes );
   941 }
  1000 }
   942 
  1001 
   943 /**
  1002 /**
   944  * {@internal Missing Short Description}}
  1003  * {@internal Missing Short Description}}
   945  *
  1004  *
   946  * @since unknown
  1005  * @since 2.5.0
   947  *
  1006  *
   948  * @param int|object $id    Post ID or post object. 
  1007  * @param int|object $id    Post ID or post object.
   949  * @param string $title (optional) Title 
  1008  * @param string $title (optional) Title
   950  * @param string $name (optional) Name 
  1009  * @param string $name (optional) Name
   951  * @return array With two entries of type string 
  1010  * @return array With two entries of type string
   952  */
  1011  */
   953 function get_sample_permalink($id, $title = null, $name = null) {
  1012 function get_sample_permalink($id, $title = null, $name = null) {
   954 	$post = &get_post($id);
  1013 	$post = &get_post($id);
   955 	if (!$post->ID) {
  1014 	if ( !$post->ID )
   956 		return array('', '');
  1015 		return array('', '');
   957 	}
  1016 
       
  1017 	$ptype = get_post_type_object($post->post_type);
       
  1018 
   958 	$original_status = $post->post_status;
  1019 	$original_status = $post->post_status;
   959 	$original_date = $post->post_date;
  1020 	$original_date = $post->post_date;
   960 	$original_name = $post->post_name;
  1021 	$original_name = $post->post_name;
   961 
  1022 
   962 	// Hack: get_permalink would return ugly permalink for
  1023 	// Hack: get_permalink would return ugly permalink for
   963 	// drafts, so we will fake, that our post is published
  1024 	// drafts, so we will fake, that our post is published
   964 	if (in_array($post->post_status, array('draft', 'pending'))) {
  1025 	if ( in_array($post->post_status, array('draft', 'pending')) ) {
   965 		$post->post_status = 'publish';
  1026 		$post->post_status = 'publish';
   966 		$post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
  1027 		$post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
   967 	}
  1028 	}
   968 
  1029 
   969 	$post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
       
   970 
       
   971 	// If the user wants to set a new name -- override the current one
  1030 	// If the user wants to set a new name -- override the current one
   972 	// Note: if empty name is supplied -- use the title instead, see #6072
  1031 	// Note: if empty name is supplied -- use the title instead, see #6072
   973 	if (!is_null($name)) {
  1032 	if ( !is_null($name) )
   974 		$post->post_name = sanitize_title($name ? $name : $title, $post->ID);
  1033 		$post->post_name = sanitize_title($name ? $name : $title, $post->ID);
   975 	}
  1034 
       
  1035 	$post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
   976 
  1036 
   977 	$post->filter = 'sample';
  1037 	$post->filter = 'sample';
   978 
  1038 
   979 	$permalink = get_permalink($post, true);
  1039 	$permalink = get_permalink($post, true);
   980 
  1040 
       
  1041 	// Replace custom post_type Token with generic pagename token for ease of use.
       
  1042 	$permalink = str_replace("%$post->post_type%", '%pagename%', $permalink);
       
  1043 
   981 	// Handle page hierarchy
  1044 	// Handle page hierarchy
   982 	if ( 'page' == $post->post_type ) {
  1045 	if ( $ptype->hierarchical ) {
   983 		$uri = get_page_uri($post->ID);
  1046 		$uri = get_page_uri($post);
   984 		$uri = untrailingslashit($uri);
  1047 		$uri = untrailingslashit($uri);
   985 		$uri = strrev( stristr( strrev( $uri ), '/' ) );
  1048 		$uri = strrev( stristr( strrev( $uri ), '/' ) );
   986 		$uri = untrailingslashit($uri);
  1049 		$uri = untrailingslashit($uri);
       
  1050 		$uri = apply_filters( 'editable_slug', $uri );
   987 		if ( !empty($uri) )
  1051 		if ( !empty($uri) )
   988 			$uri .='/';
  1052 			$uri .= '/';
   989 		$permalink = str_replace('%pagename%', "${uri}%pagename%", $permalink);
  1053 		$permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
   990 	}
  1054 	}
   991 
  1055 
   992 	$permalink = array($permalink, apply_filters('editable_slug', $post->post_name));
  1056 	$permalink = array($permalink, apply_filters('editable_slug', $post->post_name));
   993 	$post->post_status = $original_status;
  1057 	$post->post_status = $original_status;
   994 	$post->post_date = $original_date;
  1058 	$post->post_date = $original_date;
  1000 
  1064 
  1001 /**
  1065 /**
  1002  * sample permalink html
  1066  * sample permalink html
  1003  *
  1067  *
  1004  * intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor.
  1068  * intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor.
  1005  * 
  1069  *
  1006  * @since unknown
  1070  * @since 2.5.0
  1007  *
  1071  *
  1008  * @param int|object $id Post ID or post object. 
  1072  * @param int|object $id Post ID or post object.
  1009  * @param string $new_title (optional) New title  
  1073  * @param string $new_title (optional) New title
  1010  * @param string $new_slug (optional) New slug 
  1074  * @param string $new_slug (optional) New slug
  1011  * @return string intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor. 
  1075  * @return string intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor.
  1012  */
  1076  */
  1013 function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
  1077 function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
       
  1078 	global $wpdb;
  1014 	$post = &get_post($id);
  1079 	$post = &get_post($id);
       
  1080 
  1015 	list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
  1081 	list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
  1016 
  1082 
  1017 	if ( 'publish' == $post->post_status ) {
  1083 	if ( 'publish' == $post->post_status ) {
  1018 		$view_post = 'post' == $post->post_type ? __('View Post') : __('View Page');
  1084 		$ptype = get_post_type_object($post->post_type);
       
  1085 		$view_post = $ptype->labels->view_item;
  1019 		$title = __('Click to edit this part of the permalink');
  1086 		$title = __('Click to edit this part of the permalink');
  1020 	} else {
  1087 	} else {
  1021 		$title = __('Temporary permalink. Click to edit this part.');
  1088 		$title = __('Temporary permalink. Click to edit this part.');
  1022 	}
  1089 	}
  1023 
  1090 
  1024 	if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) {
  1091 	if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) {
  1025 		$return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink">' . $permalink . "</span>\n";
  1092 		$return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink">' . $permalink . "</span>\n";
  1026 		if ( current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) )
  1093 		if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) )
  1027 			$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
  1094 			$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
  1028 		if ( isset($view_post) )
  1095 		if ( isset($view_post) )
  1029 			$return .= "<span id='view-post-btn'><a href='$permalink' class='button' target='_blank'>$view_post</a></span>\n";
  1096 			$return .= "<span id='view-post-btn'><a href='$permalink' class='button' target='_blank'>$view_post</a></span>\n";
  1030 
  1097 
  1031 		$return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
  1098 		$return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
  1048 	}
  1115 	}
  1049 
  1116 
  1050 	$post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>';
  1117 	$post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>';
  1051 	$display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink);
  1118 	$display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink);
  1052 	$view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
  1119 	$view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
  1053 	$return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink">' . $display_link . "</span>\n";
  1120 	$return =  '<strong>' . __('Permalink:') . "</strong>\n";
       
  1121 	$return .= '<span id="sample-permalink">' . $display_link . "</span>\n";
       
  1122 	$return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
  1054 	$return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n";
  1123 	$return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n";
  1055 	$return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
  1124 	$return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
  1056 	if ( isset($view_post) )
  1125 	if ( isset($view_post) )
  1057 		$return .= "<span id='view-post-btn'><a href='$view_link' class='button' target='_blank'>$view_post</a></span>\n";
  1126 		$return .= "<span id='view-post-btn'><a href='$view_link' class='button' target='_blank'>$view_post</a></span>\n";
  1058 
  1127 
  1065  * Output HTML for the post thumbnail meta-box.
  1134  * Output HTML for the post thumbnail meta-box.
  1066  *
  1135  *
  1067  * @since 2.9.0
  1136  * @since 2.9.0
  1068  *
  1137  *
  1069  * @param int $thumbnail_id ID of the attachment used for thumbnail
  1138  * @param int $thumbnail_id ID of the attachment used for thumbnail
       
  1139  * @param int $post_id ID of the post associated with the thumbnail, defaults to global $post_ID
  1070  * @return string html
  1140  * @return string html
  1071  */
  1141  */
  1072 function _wp_post_thumbnail_html( $thumbnail_id = NULL ) {
  1142 function _wp_post_thumbnail_html( $thumbnail_id = null, $post_id = null ) {
  1073 	global $content_width, $_wp_additional_image_sizes;
  1143 	global $content_width, $_wp_additional_image_sizes, $post_ID;
  1074 	$content = '<p class="hide-if-no-js"><a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . esc_html__( 'Set thumbnail' ) . '</a></p>';
  1144 
       
  1145 	if ( empty( $post_id ) )
       
  1146 		$post_id = $post_ID;
       
  1147 
       
  1148 	$upload_iframe_src = esc_url( get_upload_iframe_src('image', $post_id) );
       
  1149 	$set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>';
       
  1150 	$content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) );
  1075 
  1151 
  1076 	if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
  1152 	if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
  1077 		$old_content_width = $content_width;
  1153 		$old_content_width = $content_width;
  1078 		$content_width = 266;
  1154 		$content_width = 266;
  1079 		if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
  1155 		if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
  1080 			$thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
  1156 			$thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
  1081 		else
  1157 		else
  1082 			$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
  1158 			$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
  1083 		if ( !empty( $thumbnail_html ) ) {
  1159 		if ( !empty( $thumbnail_html ) ) {
  1084 			$content = '<a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . $thumbnail_html . '</a>';
  1160 			$ajax_nonce = wp_create_nonce( "set_post_thumbnail-$post_id" );
  1085 			$content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail();return false;">' . esc_html__( 'Remove thumbnail' ) . '</a></p>';
  1161 			$content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
       
  1162 			$content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>';
  1086 		}
  1163 		}
  1087 		$content_width = $old_content_width;
  1164 		$content_width = $old_content_width;
  1088 	}
  1165 	}
  1089 
  1166 
  1090 	return apply_filters( 'admin_post_thumbnail_html', $content );
  1167 	return apply_filters( 'admin_post_thumbnail_html', $content );
  1097  *
  1174  *
  1098  * @param int $post_id ID of the post to check for editing
  1175  * @param int $post_id ID of the post to check for editing
  1099  * @return bool|int False: not locked or locked by current user. Int: user ID of user with lock.
  1176  * @return bool|int False: not locked or locked by current user. Int: user ID of user with lock.
  1100  */
  1177  */
  1101 function wp_check_post_lock( $post_id ) {
  1178 function wp_check_post_lock( $post_id ) {
  1102 	global $current_user;
       
  1103 
       
  1104 	if ( !$post = get_post( $post_id ) )
  1179 	if ( !$post = get_post( $post_id ) )
  1105 		return false;
  1180 		return false;
  1106 
  1181 
  1107 	$lock = get_post_meta( $post->ID, '_edit_lock', true );
  1182 	if ( !$lock = get_post_meta( $post->ID, '_edit_lock', true ) )
  1108 	$last = get_post_meta( $post->ID, '_edit_last', true );
  1183 		return false;
       
  1184 
       
  1185 	$lock = explode( ':', $lock );
       
  1186 	$time = $lock[0];
       
  1187 	$user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
  1109 
  1188 
  1110 	$time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 );
  1189 	$time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 );
  1111 
  1190 
  1112 	if ( $lock && $lock > time() - $time_window && $last != $current_user->ID )
  1191 	if ( $time && $time > time() - $time_window && $user != get_current_user_id() )
  1113 		return $last;
  1192 		return $user;
  1114 	return false;
  1193 	return false;
  1115 }
  1194 }
  1116 
  1195 
  1117 /**
  1196 /**
  1118  * Mark the post as currently being edited by the current user
  1197  * Mark the post as currently being edited by the current user
  1119  *
  1198  *
  1120  * @since 2.5.0
  1199  * @since 2.5.0
  1121  *
  1200  *
  1122  * @param int $post_id ID of the post to being edited
  1201  * @param int $post_id ID of the post to being edited
  1123  * @return bool Returns false if the post doesn't exist of there is no current user
  1202  * @return bool|array Returns false if the post doesn't exist of there is no current user, or
       
  1203  * 	an array of the lock time and the user ID.
  1124  */
  1204  */
  1125 function wp_set_post_lock( $post_id ) {
  1205 function wp_set_post_lock( $post_id ) {
  1126 	global $current_user;
       
  1127 	if ( !$post = get_post( $post_id ) )
  1206 	if ( !$post = get_post( $post_id ) )
  1128 		return false;
  1207 		return false;
  1129 	if ( !$current_user || !$current_user->ID )
  1208 	if ( 0 == ($user_id = get_current_user_id()) )
  1130 		return false;
  1209 		return false;
  1131 
  1210 
  1132 	$now = time();
  1211 	$now = time();
  1133 
  1212 	$lock = "$now:$user_id";
  1134 	if ( !add_post_meta( $post->ID, '_edit_lock', $now, true ) )
  1213 
  1135 		update_post_meta( $post->ID, '_edit_lock', $now );
  1214 	update_post_meta( $post->ID, '_edit_lock', $lock );
  1136 	if ( !add_post_meta( $post->ID, '_edit_last', $current_user->ID, true ) )
  1215 	return array( $now, $user_id );
  1137 		update_post_meta( $post->ID, '_edit_last', $current_user->ID );
       
  1138 }
  1216 }
  1139 
  1217 
  1140 /**
  1218 /**
  1141  * Outputs the notice message to say that someone else is editing this post at the moment.
  1219  * Outputs the notice message to say that someone else is editing this post at the moment.
  1142  *
  1220  *
  1143  * @since 2.8.5
  1221  * @since 2.8.5
  1144  * @return none
  1222  * @return none
  1145  */
  1223  */
  1146 function _admin_notice_post_locked() {
  1224 function _admin_notice_post_locked() {
  1147 	global $post;
  1225 	global $post;
  1148 	$last_user = get_userdata( get_post_meta( $post->ID, '_edit_last', true ) );
  1226 
       
  1227 	$lock = explode( ':', get_post_meta( $post->ID, '_edit_lock', true ) );
       
  1228 	$user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
       
  1229 	$last_user = get_userdata( $user );
  1149 	$last_user_name = $last_user ? $last_user->display_name : __('Somebody');
  1230 	$last_user_name = $last_user ? $last_user->display_name : __('Somebody');
  1150 
  1231 
  1151 	switch ($post->post_type) {
  1232 	switch ($post->post_type) {
  1152 		case 'post':
  1233 		case 'post':
  1153 			$message = __( 'Warning: %s is currently editing this post' );
  1234 			$message = __( 'Warning: %s is currently editing this post' );
  1170  * @subpackage Post_Revisions
  1251  * @subpackage Post_Revisions
  1171  * @since 2.6.0
  1252  * @since 2.6.0
  1172  *
  1253  *
  1173  * @uses _wp_translate_postdata()
  1254  * @uses _wp_translate_postdata()
  1174  * @uses _wp_post_revision_fields()
  1255  * @uses _wp_post_revision_fields()
       
  1256  *
       
  1257  * @return unknown
  1175  */
  1258  */
  1176 function wp_create_post_autosave( $post_id ) {
  1259 function wp_create_post_autosave( $post_id ) {
  1177 	$translated = _wp_translate_postdata( true );
  1260 	$translated = _wp_translate_postdata( true );
  1178 	if ( is_wp_error( $translated ) )
  1261 	if ( is_wp_error( $translated ) )
  1179 		return $translated;
  1262 		return $translated;
  1180 
  1263 
  1181 	// Only store one autosave.  If there is already an autosave, overwrite it.
  1264 	// Only store one autosave. If there is already an autosave, overwrite it.
  1182 	if ( $old_autosave = wp_get_post_autosave( $post_id ) ) {
  1265 	if ( $old_autosave = wp_get_post_autosave( $post_id ) ) {
  1183 		$new_autosave = _wp_post_revision_fields( $_POST, true );
  1266 		$new_autosave = _wp_post_revision_fields( $_POST, true );
  1184 		$new_autosave['ID'] = $old_autosave->ID;
  1267 		$new_autosave['ID'] = $old_autosave->ID;
  1185 		$current_user = wp_get_current_user();
  1268 		$new_autosave['post_author'] = get_current_user_id();
  1186 		$new_autosave['post_author'] = $current_user->ID;
       
  1187 		return wp_update_post( $new_autosave );
  1269 		return wp_update_post( $new_autosave );
  1188 	}
  1270 	}
  1189 
  1271 
  1190 	// _wp_put_post_revision() expects unescaped.
  1272 	// _wp_put_post_revision() expects unescaped.
  1191 	$_POST = stripslashes_deep($_POST);
  1273 	$_POST = stripslashes_deep($_POST);
  1196 
  1278 
  1197 /**
  1279 /**
  1198  * Save draft or manually autosave for showing preview.
  1280  * Save draft or manually autosave for showing preview.
  1199  *
  1281  *
  1200  * @package WordPress
  1282  * @package WordPress
  1201  * @since 2.7
  1283  * @since 2.7.0
  1202  *
  1284  *
  1203  * @uses wp_write_post()
  1285  * @uses get_post_status()
  1204  * @uses edit_post()
  1286  * @uses edit_post()
  1205  * @uses get_post()
  1287  * @uses get_post()
  1206  * @uses current_user_can()
  1288  * @uses current_user_can()
       
  1289  * @uses wp_die()
  1207  * @uses wp_create_post_autosave()
  1290  * @uses wp_create_post_autosave()
       
  1291  * @uses add_query_arg()
       
  1292  * @uses wp_create_nonce()
  1208  *
  1293  *
  1209  * @return str URL to redirect to show the preview
  1294  * @return str URL to redirect to show the preview
  1210  */
  1295  */
  1211 function post_preview() {
  1296 function post_preview() {
  1212 
  1297 
  1213 	$post_ID = (int) $_POST['post_ID'];
  1298 	$post_ID = (int) $_POST['post_ID'];
  1214 	if ( $post_ID < 1 )
  1299 	$status = get_post_status( $post_ID );
       
  1300 	if ( 'auto-draft' == $status )
  1215 		wp_die( __('Preview not available. Please save as a draft first.') );
  1301 		wp_die( __('Preview not available. Please save as a draft first.') );
  1216 
  1302 
  1217 	if ( isset($_POST['catslist']) )
  1303 	if ( isset($_POST['catslist']) )
  1218 		$_POST['post_category'] = explode(",", $_POST['catslist']);
  1304 		$_POST['post_category'] = explode(",", $_POST['catslist']);
  1219 
  1305 
  1234 			wp_die(__('You are not allowed to edit this post.'));
  1320 			wp_die(__('You are not allowed to edit this post.'));
  1235 	}
  1321 	}
  1236 
  1322 
  1237 	if ( 'draft' == $post->post_status ) {
  1323 	if ( 'draft' == $post->post_status ) {
  1238 		$id = edit_post();
  1324 		$id = edit_post();
  1239 	} else { // Non drafts are not overwritten.  The autosave is stored in a special post revision.
  1325 	} else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
  1240 		$id = wp_create_post_autosave( $post->ID );
  1326 		$id = wp_create_post_autosave( $post->ID );
  1241 		if ( ! is_wp_error($id) )
  1327 		if ( ! is_wp_error($id) )
  1242 			$id = $post->ID;
  1328 			$id = $post->ID;
  1243 	}
  1329 	}
  1244 
  1330 
  1252 		$url = add_query_arg( array( 'preview' => 'true', 'preview_id' => $id, 'preview_nonce' => $nonce ), get_permalink($id) );
  1338 		$url = add_query_arg( array( 'preview' => 'true', 'preview_id' => $id, 'preview_nonce' => $nonce ), get_permalink($id) );
  1253 	}
  1339 	}
  1254 
  1340 
  1255 	return $url;
  1341 	return $url;
  1256 }
  1342 }
  1257 
       
  1258 /**
       
  1259  * Adds the TinyMCE editor used on the Write and Edit screens.
       
  1260  *
       
  1261  * @package WordPress
       
  1262  * @since 2.7
       
  1263  *
       
  1264  * TinyMCE is loaded separately from other Javascript by using wp-tinymce.php. It outputs concatenated
       
  1265  * and optionaly pre-compressed version of the core and all default plugins. Additional plugins are loaded
       
  1266  * directly by TinyMCE using non-blocking method. Custom plugins can be refreshed by adding a query string
       
  1267  * to the URL when queueing them with the mce_external_plugins filter.
       
  1268  *
       
  1269  * @param bool $teeny optional Output a trimmed down version used in Press This.
       
  1270  * @param mixed $settings optional An array that can add to or overwrite the default TinyMCE settings.
       
  1271  */
       
  1272 function wp_tiny_mce( $teeny = false, $settings = false ) {
       
  1273 	global $concatenate_scripts, $compress_scripts, $tinymce_version;
       
  1274 
       
  1275 	if ( ! user_can_richedit() )
       
  1276 		return;
       
  1277 
       
  1278 	$baseurl = includes_url('js/tinymce');
       
  1279 
       
  1280 	$mce_locale = ( '' == get_locale() ) ? 'en' : strtolower( substr(get_locale(), 0, 2) ); // only ISO 639-1
       
  1281 
       
  1282 	/*
       
  1283 	The following filter allows localization scripts to change the languages displayed in the spellchecker's drop-down menu.
       
  1284 	By default it uses Google's spellchecker API, but can be configured to use PSpell/ASpell if installed on the server.
       
  1285 	The + sign marks the default language. More information:
       
  1286 	http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker
       
  1287 	*/
       
  1288 	$mce_spellchecker_languages = apply_filters('mce_spellchecker_languages', '+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv');
       
  1289 
       
  1290 	if ( $teeny ) {
       
  1291 		$plugins = apply_filters( 'teeny_mce_plugins', array('safari', 'inlinepopups', 'media', 'fullscreen', 'wordpress') );
       
  1292 		$ext_plugins = '';
       
  1293 	} else {
       
  1294 		$plugins = array( 'safari', 'inlinepopups', 'spellchecker', 'paste', 'wordpress', 'media', 'fullscreen', 'wpeditimage', 'wpgallery', 'tabfocus' );
       
  1295 
       
  1296 		/*
       
  1297 		The following filter takes an associative array of external plugins for TinyMCE in the form 'plugin_name' => 'url'.
       
  1298 		It adds the plugin's name to TinyMCE's plugins init and the call to PluginManager to load the plugin.
       
  1299 		The url should be absolute and should include the js file name to be loaded. Example:
       
  1300 		array( 'myplugin' => 'http://my-site.com/wp-content/plugins/myfolder/mce_plugin.js' )
       
  1301 		If the plugin uses a button, it should be added with one of the "$mce_buttons" filters.
       
  1302 		*/
       
  1303 		$mce_external_plugins = apply_filters('mce_external_plugins', array());
       
  1304 
       
  1305 		$ext_plugins = '';
       
  1306 		if ( ! empty($mce_external_plugins) ) {
       
  1307 
       
  1308 			/*
       
  1309 			The following filter loads external language files for TinyMCE plugins.
       
  1310 			It takes an associative array 'plugin_name' => 'path', where path is the
       
  1311 			include path to the file. The language file should follow the same format as
       
  1312 			/tinymce/langs/wp-langs.php and should define a variable $strings that
       
  1313 			holds all translated strings.
       
  1314 			When this filter is not used, the function will try to load {mce_locale}.js.
       
  1315 			If that is not found, en.js will be tried next.
       
  1316 			*/
       
  1317 			$mce_external_languages = apply_filters('mce_external_languages', array());
       
  1318 
       
  1319 			$loaded_langs = array();
       
  1320 			$strings = '';
       
  1321 
       
  1322 			if ( ! empty($mce_external_languages) ) {
       
  1323 				foreach ( $mce_external_languages as $name => $path ) {
       
  1324 					if ( @is_file($path) && @is_readable($path) ) {
       
  1325 						include_once($path);
       
  1326 						$ext_plugins .= $strings . "\n";
       
  1327 						$loaded_langs[] = $name;
       
  1328 					}
       
  1329 				}
       
  1330 			}
       
  1331 
       
  1332 			foreach ( $mce_external_plugins as $name => $url ) {
       
  1333 
       
  1334 				if ( is_ssl() ) $url = str_replace('http://', 'https://', $url);
       
  1335 
       
  1336 				$plugins[] = '-' . $name;
       
  1337 
       
  1338 				$plugurl = dirname($url);
       
  1339 				$strings = $str1 = $str2 = '';
       
  1340 				if ( ! in_array($name, $loaded_langs) ) {
       
  1341 					$path = str_replace( WP_PLUGIN_URL, '', $plugurl );
       
  1342 					$path = WP_PLUGIN_DIR . $path . '/langs/';
       
  1343 
       
  1344 					if ( function_exists('realpath') )
       
  1345 						$path = trailingslashit( realpath($path) );
       
  1346 
       
  1347 					if ( @is_file($path . $mce_locale . '.js') )
       
  1348 						$strings .= @file_get_contents($path . $mce_locale . '.js') . "\n";
       
  1349 
       
  1350 					if ( @is_file($path . $mce_locale . '_dlg.js') )
       
  1351 						$strings .= @file_get_contents($path . $mce_locale . '_dlg.js') . "\n";
       
  1352 
       
  1353 					if ( 'en' != $mce_locale && empty($strings) ) {
       
  1354 						if ( @is_file($path . 'en.js') ) {
       
  1355 							$str1 = @file_get_contents($path . 'en.js');
       
  1356 							$strings .= preg_replace( '/([\'"])en\./', '$1' . $mce_locale . '.', $str1, 1 ) . "\n";
       
  1357 						}
       
  1358 
       
  1359 						if ( @is_file($path . 'en_dlg.js') ) {
       
  1360 							$str2 = @file_get_contents($path . 'en_dlg.js');
       
  1361 							$strings .= preg_replace( '/([\'"])en\./', '$1' . $mce_locale . '.', $str2, 1 ) . "\n";
       
  1362 						}
       
  1363 					}
       
  1364 
       
  1365 					if ( ! empty($strings) )
       
  1366 						$ext_plugins .= "\n" . $strings . "\n";
       
  1367 				}
       
  1368 
       
  1369 				$ext_plugins .= 'tinyMCEPreInit.load_ext("' . $plugurl . '", "' . $mce_locale . '");' . "\n";
       
  1370 				$ext_plugins .= 'tinymce.PluginManager.load("' . $name . '", "' . $url . '");' . "\n";
       
  1371 			}
       
  1372 		}
       
  1373 	}
       
  1374 
       
  1375 	$plugins = implode($plugins, ',');
       
  1376 
       
  1377 	if ( $teeny ) {
       
  1378 		$mce_buttons = apply_filters( 'teeny_mce_buttons', array('bold, italic, underline, blockquote, separator, strikethrough, bullist, numlist,justifyleft, justifycenter, justifyright, undo, redo, link, unlink, fullscreen') );
       
  1379 		$mce_buttons = implode($mce_buttons, ',');
       
  1380 		$mce_buttons_2 = $mce_buttons_3 = $mce_buttons_4 = '';
       
  1381 	} else {
       
  1382 		$mce_buttons = apply_filters('mce_buttons', array('bold', 'italic', 'strikethrough', '|', 'bullist', 'numlist', 'blockquote', '|', 'justifyleft', 'justifycenter', 'justifyright', '|', 'link', 'unlink', 'wp_more', '|', 'spellchecker', 'fullscreen', 'wp_adv' ));
       
  1383 		$mce_buttons = implode($mce_buttons, ',');
       
  1384 
       
  1385 		$mce_buttons_2 = apply_filters('mce_buttons_2', array('formatselect', 'underline', 'justifyfull', 'forecolor', '|', 'pastetext', 'pasteword', 'removeformat', '|', 'media', 'charmap', '|', 'outdent', 'indent', '|', 'undo', 'redo', 'wp_help' ));
       
  1386 		$mce_buttons_2 = implode($mce_buttons_2, ',');
       
  1387 
       
  1388 		$mce_buttons_3 = apply_filters('mce_buttons_3', array());
       
  1389 		$mce_buttons_3 = implode($mce_buttons_3, ',');
       
  1390 
       
  1391 		$mce_buttons_4 = apply_filters('mce_buttons_4', array());
       
  1392 		$mce_buttons_4 = implode($mce_buttons_4, ',');
       
  1393 	}
       
  1394 	$no_captions = ( apply_filters( 'disable_captions', '' ) ) ? true : false;
       
  1395 
       
  1396 	// TinyMCE init settings
       
  1397 	$initArray = array (
       
  1398 		'mode' => 'specific_textareas',
       
  1399 		'editor_selector' => 'theEditor',
       
  1400 		'width' => '100%',
       
  1401 		'theme' => 'advanced',
       
  1402 		'skin' => 'wp_theme',
       
  1403 		'theme_advanced_buttons1' => "$mce_buttons",
       
  1404 		'theme_advanced_buttons2' => "$mce_buttons_2",
       
  1405 		'theme_advanced_buttons3' => "$mce_buttons_3",
       
  1406 		'theme_advanced_buttons4' => "$mce_buttons_4",
       
  1407 		'language' => "$mce_locale",
       
  1408 		'spellchecker_languages' => "$mce_spellchecker_languages",
       
  1409 		'theme_advanced_toolbar_location' => 'top',
       
  1410 		'theme_advanced_toolbar_align' => 'left',
       
  1411 		'theme_advanced_statusbar_location' => 'bottom',
       
  1412 		'theme_advanced_resizing' => true,
       
  1413 		'theme_advanced_resize_horizontal' => false,
       
  1414 		'dialog_type' => 'modal',
       
  1415 		'relative_urls' => false,
       
  1416 		'remove_script_host' => false,
       
  1417 		'convert_urls' => false,
       
  1418 		'apply_source_formatting' => false,
       
  1419 		'remove_linebreaks' => true,
       
  1420 		'gecko_spellcheck' => true,
       
  1421 		'entities' => '38,amp,60,lt,62,gt',
       
  1422 		'accessibility_focus' => true,
       
  1423 		'tabfocus_elements' => 'major-publishing-actions',
       
  1424 		'media_strict' => false,
       
  1425 		'paste_remove_styles' => true,
       
  1426 		'paste_remove_spans' => true,
       
  1427 		'paste_strip_class_attributes' => 'all',
       
  1428 		'wpeditimage_disable_captions' => $no_captions,
       
  1429 		'plugins' => "$plugins"
       
  1430 	);
       
  1431 
       
  1432 	$mce_css = trim(apply_filters('mce_css', ''), ' ,');
       
  1433 
       
  1434 	if ( ! empty($mce_css) )
       
  1435 		$initArray['content_css'] = "$mce_css";
       
  1436 
       
  1437 	if ( is_array($settings) )
       
  1438 		$initArray = array_merge($initArray, $settings);
       
  1439 
       
  1440 	// For people who really REALLY know what they're doing with TinyMCE
       
  1441 	// You can modify initArray to add, remove, change elements of the config before tinyMCE.init
       
  1442 	// Setting "valid_elements", "invalid_elements" and "extended_valid_elements" can be done through "tiny_mce_before_init".
       
  1443 	// Best is to use the default cleanup by not specifying valid_elements, as TinyMCE contains full set of XHTML 1.0.
       
  1444 	if ( $teeny ) {
       
  1445 		$initArray = apply_filters('teeny_mce_before_init', $initArray);
       
  1446 	} else {
       
  1447 		$initArray = apply_filters('tiny_mce_before_init', $initArray);
       
  1448 	}
       
  1449 
       
  1450 	if ( empty($initArray['theme_advanced_buttons3']) && !empty($initArray['theme_advanced_buttons4']) ) {
       
  1451 		$initArray['theme_advanced_buttons3'] = $initArray['theme_advanced_buttons4'];
       
  1452 		$initArray['theme_advanced_buttons4'] = '';
       
  1453 	}
       
  1454 
       
  1455 	if ( ! isset($concatenate_scripts) )
       
  1456 		script_concat_settings();
       
  1457 
       
  1458 	$language = $initArray['language'];
       
  1459 	$zip = $compress_scripts ? 1 : 0;
       
  1460 
       
  1461 	/**
       
  1462 	 * Deprecated
       
  1463 	 *
       
  1464 	 * The tiny_mce_version filter is not needed since external plugins are loaded directly by TinyMCE.
       
  1465 	 * These plugins can be refreshed by appending query string to the URL passed to mce_external_plugins filter.
       
  1466 	 * If the plugin has a popup dialog, a query string can be added to the button action that opens it (in the plugin's code).
       
  1467 	 */
       
  1468 	$version = apply_filters('tiny_mce_version', '');
       
  1469 	$version = 'ver=' . $tinymce_version . $version;
       
  1470 
       
  1471 	if ( 'en' != $language )
       
  1472 		include_once(ABSPATH . WPINC . '/js/tinymce/langs/wp-langs.php');
       
  1473 
       
  1474 	$mce_options = '';
       
  1475 	foreach ( $initArray as $k => $v )
       
  1476 	    $mce_options .= $k . ':"' . $v . '", ';
       
  1477 
       
  1478 	$mce_options = rtrim( trim($mce_options), '\n\r,' ); ?>
       
  1479 
       
  1480 <script type="text/javascript">
       
  1481 /* <![CDATA[ */
       
  1482 tinyMCEPreInit = {
       
  1483 	base : "<?php echo $baseurl; ?>",
       
  1484 	suffix : "",
       
  1485 	query : "<?php echo $version; ?>",
       
  1486 	mceInit : {<?php echo $mce_options; ?>},
       
  1487 	load_ext : function(url,lang){var sl=tinymce.ScriptLoader;sl.markDone(url+'/langs/'+lang+'.js');sl.markDone(url+'/langs/'+lang+'_dlg.js');}
       
  1488 };
       
  1489 /* ]]> */
       
  1490 </script>
       
  1491 
       
  1492 <?php
       
  1493 	if ( $concatenate_scripts )
       
  1494 		echo "<script type='text/javascript' src='$baseurl/wp-tinymce.php?c=$zip&amp;$version'></script>\n";
       
  1495 	else
       
  1496 		echo "<script type='text/javascript' src='$baseurl/tiny_mce.js?$version'></script>\n";
       
  1497 
       
  1498 	if ( 'en' != $language && isset($lang) )
       
  1499 		echo "<script type='text/javascript'>\n$lang\n</script>\n";
       
  1500 	else
       
  1501 		echo "<script type='text/javascript' src='$baseurl/langs/wp-langs-en.js?$version'></script>\n";
       
  1502 ?>
       
  1503 
       
  1504 <script type="text/javascript">
       
  1505 /* <![CDATA[ */
       
  1506 <?php if ( $ext_plugins ) echo "$ext_plugins\n"; ?>
       
  1507 <?php if ( $concatenate_scripts ) { ?>
       
  1508 tinyMCEPreInit.go();
       
  1509 <?php } else { ?>
       
  1510 (function(){var t=tinyMCEPreInit,sl=tinymce.ScriptLoader,ln=t.mceInit.language,th=t.mceInit.theme,pl=t.mceInit.plugins;sl.markDone(t.base+'/langs/'+ln+'.js');sl.markDone(t.base+'/themes/'+th+'/langs/'+ln+'.js');sl.markDone(t.base+'/themes/'+th+'/langs/'+ln+'_dlg.js');tinymce.each(pl.split(','),function(n){if(n&&n.charAt(0)!='-'){sl.markDone(t.base+'/plugins/'+n+'/langs/'+ln+'.js');sl.markDone(t.base+'/plugins/'+n+'/langs/'+ln+'_dlg.js');}});})();
       
  1511 <?php } ?>
       
  1512 tinyMCE.init(tinyMCEPreInit.mceInit);
       
  1513 /* ]]> */
       
  1514 </script>
       
  1515 <?php
       
  1516 }