raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress Post Administration API.
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Administration
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * Rename $_POST data from form names to DB post columns.
mp-wp_genesis 11 *
mp-wp_genesis 12 * Manipulates $_POST directly.
mp-wp_genesis 13 *
mp-wp_genesis 14 * @package WordPress
mp-wp_genesis 15 * @since 2.6.0
mp-wp_genesis 16 *
mp-wp_genesis 17 * @param bool $update Are we updating a pre-existing post?
mp-wp_genesis 18 * @param post_data array Array of post data. Defaults to the contents of $_POST.
mp-wp_genesis 19 * @return object|bool WP_Error on failure, true on success.
mp-wp_genesis 20 */
mp-wp_genesis 21 function _wp_translate_postdata( $update = false, $post_data = null ) {
mp-wp_genesis 22
mp-wp_genesis 23 if ( empty($post_data) )
mp-wp_genesis 24 $post_data = &$_POST;
mp-wp_genesis 25
mp-wp_genesis 26 if ( $update )
mp-wp_genesis 27 $post_data['ID'] = (int) $post_data['post_ID'];
mp-wp_genesis 28 $post_data['post_content'] = isset($post_data['content']) ? $post_data['content'] : '';
mp-wp_genesis 29 $post_data['post_excerpt'] = isset($post_data['excerpt']) ? $post_data['excerpt'] : '';
mp-wp_genesis 30 $post_data['post_parent'] = isset($post_data['parent_id'])? $post_data['parent_id'] : '';
mp-wp_genesis 31 if ( isset($post_data['trackback_url']) )
mp-wp_genesis 32 $post_data['to_ping'] = $post_data['trackback_url'];
mp-wp_genesis 33
mp-wp_genesis 34 if (!empty ( $post_data['post_author_override'] ) ) {
mp-wp_genesis 35 $post_data['post_author'] = (int) $post_data['post_author_override'];
mp-wp_genesis 36 } else {
mp-wp_genesis 37 if (!empty ( $post_data['post_author'] ) ) {
mp-wp_genesis 38 $post_data['post_author'] = (int) $post_data['post_author'];
mp-wp_genesis 39 } else {
mp-wp_genesis 40 $post_data['post_author'] = (int) $post_data['user_ID'];
mp-wp_genesis 41 }
mp-wp_genesis 42 }
mp-wp_genesis 43
mp-wp_genesis 44 if ( isset($post_data['user_ID']) && ($post_data['post_author'] != $post_data['user_ID']) ) {
mp-wp_genesis 45 if ( 'page' == $post_data['post_type'] ) {
mp-wp_genesis 46 if ( !current_user_can( 'edit_others_pages' ) ) {
mp-wp_genesis 47 return new WP_Error( 'edit_others_pages', $update ?
mp-wp_genesis 48 __( 'You are not allowed to edit pages as this user.' ) :
mp-wp_genesis 49 __( 'You are not allowed to create pages as this user.' )
mp-wp_genesis 50 );
mp-wp_genesis 51 }
mp-wp_genesis 52 } else {
mp-wp_genesis 53 if ( !current_user_can( 'edit_others_posts' ) ) {
mp-wp_genesis 54 return new WP_Error( 'edit_others_posts', $update ?
mp-wp_genesis 55 __( 'You are not allowed to edit posts as this user.' ) :
mp-wp_genesis 56 __( 'You are not allowed to post as this user.' )
mp-wp_genesis 57 );
mp-wp_genesis 58 }
mp-wp_genesis 59 }
mp-wp_genesis 60 }
mp-wp_genesis 61
mp-wp_genesis 62 // What to do based on which button they pressed
mp-wp_genesis 63 if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
mp-wp_genesis 64 $post_data['post_status'] = 'draft';
mp-wp_genesis 65 if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
mp-wp_genesis 66 $post_data['post_status'] = 'private';
mp-wp_genesis 67 if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( $post_data['post_status'] != 'private' ) )
mp-wp_genesis 68 $post_data['post_status'] = 'publish';
mp-wp_genesis 69 if ( isset($post_data['advanced']) && '' != $post_data['advanced'] )
mp-wp_genesis 70 $post_data['post_status'] = 'draft';
mp-wp_genesis 71 if ( isset($post_data['pending']) && '' != $post_data['pending'] )
mp-wp_genesis 72 $post_data['post_status'] = 'pending';
mp-wp_genesis 73
mp-wp_genesis 74 $previous_status = get_post_field('post_status', isset($post_data['ID']) ? $post_data['ID'] : $post_data['temp_ID']);
mp-wp_genesis 75
mp-wp_genesis 76 // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
mp-wp_genesis 77 // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
mp-wp_genesis 78 if ( 'page' == $post_data['post_type'] ) {
mp-wp_genesis 79 $publish_cap = 'publish_pages';
mp-wp_genesis 80 $edit_cap = 'edit_published_pages';
mp-wp_genesis 81 } else {
mp-wp_genesis 82 $publish_cap = 'publish_posts';
mp-wp_genesis 83 $edit_cap = 'edit_published_posts';
mp-wp_genesis 84 }
mp-wp_genesis 85 if ( isset($post_data['post_status']) && ('publish' == $post_data['post_status'] && !current_user_can( $publish_cap )) )
mp-wp_genesis 86 if ( $previous_status != 'publish' || !current_user_can( $edit_cap ) )
mp-wp_genesis 87 $post_data['post_status'] = 'pending';
mp-wp_genesis 88
mp-wp_genesis 89 if ( ! isset($post_data['post_status']) )
mp-wp_genesis 90 $post_data['post_status'] = $previous_status;
mp-wp_genesis 91
mp-wp_genesis 92 if (!isset( $post_data['comment_status'] ))
mp-wp_genesis 93 $post_data['comment_status'] = 'closed';
mp-wp_genesis 94
mp-wp_genesis 95 if (!isset( $post_data['ping_status'] ))
mp-wp_genesis 96 $post_data['ping_status'] = 'closed';
mp-wp_genesis 97
mp-wp_genesis 98 foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
mp-wp_genesis 99 if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) {
mp-wp_genesis 100 $post_data['edit_date'] = '1';
mp-wp_genesis 101 break;
mp-wp_genesis 102 }
mp-wp_genesis 103 }
mp-wp_genesis 104
mp-wp_genesis 105 if ( !empty( $post_data['edit_date'] ) ) {
mp-wp_genesis 106 $aa = $post_data['aa'];
mp-wp_genesis 107 $mm = $post_data['mm'];
mp-wp_genesis 108 $jj = $post_data['jj'];
mp-wp_genesis 109 $hh = $post_data['hh'];
mp-wp_genesis 110 $mn = $post_data['mn'];
mp-wp_genesis 111 $ss = $post_data['ss'];
mp-wp_genesis 112 $aa = ($aa <= 0 ) ? date('Y') : $aa;
mp-wp_genesis 113 $mm = ($mm <= 0 ) ? date('n') : $mm;
mp-wp_genesis 114 $jj = ($jj > 31 ) ? 31 : $jj;
mp-wp_genesis 115 $jj = ($jj <= 0 ) ? date('j') : $jj;
mp-wp_genesis 116 $hh = ($hh > 23 ) ? $hh -24 : $hh;
mp-wp_genesis 117 $mn = ($mn > 59 ) ? $mn -60 : $mn;
mp-wp_genesis 118 $ss = ($ss > 59 ) ? $ss -60 : $ss;
mp-wp_genesis 119 $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
mp-wp_genesis 120 $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
mp-wp_genesis 121 }
mp-wp_genesis 122
mp-wp_genesis 123 return $post_data;
mp-wp_genesis 124 }
mp-wp_genesis 125
mp-wp_genesis 126 /**
mp-wp_genesis 127 * Update an existing post with values provided in $_POST.
mp-wp_genesis 128 *
mp-wp_genesis 129 * @since unknown
mp-wp_genesis 130 *
mp-wp_genesis 131 * @param array $post_data Optional.
mp-wp_genesis 132 * @return int Post ID.
mp-wp_genesis 133 */
mp-wp_genesis 134 function edit_post( $post_data = null ) {
mp-wp_genesis 135
mp-wp_genesis 136 if ( empty($post_data) )
mp-wp_genesis 137 $post_data = &$_POST;
mp-wp_genesis 138
mp-wp_genesis 139 $post_ID = (int) $post_data['post_ID'];
mp-wp_genesis 140
mp-wp_genesis 141 if ( 'page' == $post_data['post_type'] ) {
mp-wp_genesis 142 if ( !current_user_can( 'edit_page', $post_ID ) )
mp-wp_genesis 143 wp_die( __('You are not allowed to edit this page.' ));
mp-wp_genesis 144 } else {
mp-wp_genesis 145 if ( !current_user_can( 'edit_post', $post_ID ) )
mp-wp_genesis 146 wp_die( __('You are not allowed to edit this post.' ));
mp-wp_genesis 147 }
mp-wp_genesis 148
mp-wp_genesis 149 // Autosave shouldn't save too soon after a real save
mp-wp_genesis 150 if ( 'autosave' == $post_data['action'] ) {
mp-wp_genesis 151 $post =& get_post( $post_ID );
mp-wp_genesis 152 $now = time();
mp-wp_genesis 153 $then = strtotime($post->post_date_gmt . ' +0000');
mp-wp_genesis 154 $delta = AUTOSAVE_INTERVAL / 2;
mp-wp_genesis 155 if ( ($now - $then) < $delta )
mp-wp_genesis 156 return $post_ID;
mp-wp_genesis 157 }
mp-wp_genesis 158
mp-wp_genesis 159 $post_data = _wp_translate_postdata( true, $post_data );
mp-wp_genesis 160 if ( is_wp_error($post_data) )
mp-wp_genesis 161 wp_die( $post_data->get_error_message() );
mp-wp_genesis 162
mp-wp_genesis 163 if ( isset($post_data['visibility']) ) {
mp-wp_genesis 164 switch ( $post_data['visibility'] ) {
mp-wp_genesis 165 case 'public' :
mp-wp_genesis 166 $post_data['post_password'] = '';
mp-wp_genesis 167 break;
mp-wp_genesis 168 case 'password' :
mp-wp_genesis 169 unset( $post_data['sticky'] );
mp-wp_genesis 170 break;
mp-wp_genesis 171 case 'private' :
mp-wp_genesis 172 $post_data['post_status'] = 'private';
mp-wp_genesis 173 $post_data['post_password'] = '';
mp-wp_genesis 174 unset( $post_data['sticky'] );
mp-wp_genesis 175 break;
mp-wp_genesis 176 }
mp-wp_genesis 177 }
mp-wp_genesis 178
mp-wp_genesis 179 // Meta Stuff
mp-wp_genesis 180 if ( isset($post_data['meta']) && $post_data['meta'] ) {
mp-wp_genesis 181 foreach ( $post_data['meta'] as $key => $value )
mp-wp_genesis 182 update_meta( $key, $value['key'], $value['value'] );
mp-wp_genesis 183 }
mp-wp_genesis 184
mp-wp_genesis 185 if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) {
mp-wp_genesis 186 foreach ( $post_data['deletemeta'] as $key => $value )
mp-wp_genesis 187 delete_meta( $key );
mp-wp_genesis 188 }
mp-wp_genesis 189
mp-wp_genesis 190 add_meta( $post_ID );
mp-wp_genesis 191
mp-wp_genesis 192 wp_update_post( $post_data );
mp-wp_genesis 193
mp-wp_genesis 194 // Reunite any orphaned attachments with their parent
mp-wp_genesis 195 if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
mp-wp_genesis 196 $draft_ids = array();
mp-wp_genesis 197 if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )
mp-wp_genesis 198 _relocate_children( $draft_temp_id, $post_ID );
mp-wp_genesis 199
mp-wp_genesis 200 // Now that we have an ID we can fix any attachment anchor hrefs
mp-wp_genesis 201 _fix_attachment_links( $post_ID );
mp-wp_genesis 202
mp-wp_genesis 203 wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );
mp-wp_genesis 204
mp-wp_genesis 205 if ( current_user_can( 'edit_others_posts' ) ) {
mp-wp_genesis 206 if ( !empty($post_data['sticky']) )
mp-wp_genesis 207 stick_post($post_ID);
mp-wp_genesis 208 else
mp-wp_genesis 209 unstick_post($post_ID);
mp-wp_genesis 210 }
mp-wp_genesis 211
mp-wp_genesis 212 return $post_ID;
mp-wp_genesis 213 }
mp-wp_genesis 214
mp-wp_genesis 215 /**
mp-wp_genesis 216 * {@internal Missing Short Description}}
mp-wp_genesis 217 *
mp-wp_genesis 218 * Updates all bulk edited posts/pages, adding (but not removing) tags and
mp-wp_genesis 219 * categories. Skips pages when they would be their own parent or child.
mp-wp_genesis 220 *
mp-wp_genesis 221 * @since unknown
mp-wp_genesis 222 *
mp-wp_genesis 223 * @return array
mp-wp_genesis 224 */
mp-wp_genesis 225 function bulk_edit_posts( $post_data = null ) {
mp-wp_genesis 226 global $wpdb;
mp-wp_genesis 227
mp-wp_genesis 228 if ( empty($post_data) )
mp-wp_genesis 229 $post_data = &$_POST;
mp-wp_genesis 230
mp-wp_genesis 231 if ( 'page' == $post_data['post_type'] ) {
mp-wp_genesis 232 if ( ! current_user_can( 'edit_pages' ) )
mp-wp_genesis 233 wp_die( __('You are not allowed to edit pages.') );
mp-wp_genesis 234 } else {
mp-wp_genesis 235 if ( ! current_user_can( 'edit_posts' ) )
mp-wp_genesis 236 wp_die( __('You are not allowed to edit posts.') );
mp-wp_genesis 237 }
mp-wp_genesis 238
mp-wp_genesis 239 $post_IDs = array_map( intval, (array) $post_data['post'] );
mp-wp_genesis 240
mp-wp_genesis 241 $reset = array( 'post_author', 'post_status', 'post_password', 'post_parent', 'page_template', 'comment_status', 'ping_status', 'keep_private', 'tags_input', 'post_category', 'sticky' );
mp-wp_genesis 242 foreach ( $reset as $field ) {
mp-wp_genesis 243 if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) )
mp-wp_genesis 244 unset($post_data[$field]);
mp-wp_genesis 245 }
mp-wp_genesis 246
mp-wp_genesis 247 if ( isset($post_data['post_category']) ) {
mp-wp_genesis 248 if ( is_array($post_data['post_category']) && ! empty($post_data['post_category']) )
mp-wp_genesis 249 $new_cats = array_map( absint, $post_data['post_category'] );
mp-wp_genesis 250 else
mp-wp_genesis 251 unset($post_data['post_category']);
mp-wp_genesis 252 }
mp-wp_genesis 253
mp-wp_genesis 254 if ( isset($post_data['tags_input']) ) {
mp-wp_genesis 255 $new_tags = preg_replace( '/\s*,\s*/', ',', rtrim( trim($post_data['tags_input']), ' ,' ) );
mp-wp_genesis 256 $new_tags = explode(',', $new_tags);
mp-wp_genesis 257 }
mp-wp_genesis 258
mp-wp_genesis 259 if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) {
mp-wp_genesis 260 $pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'");
mp-wp_genesis 261 $children = array();
mp-wp_genesis 262
mp-wp_genesis 263 for ( $i = 0; $i < 50 && $parent > 0; $i++ ) {
mp-wp_genesis 264 $children[] = $parent;
mp-wp_genesis 265
mp-wp_genesis 266 foreach ( $pages as $page ) {
mp-wp_genesis 267 if ( $page->ID == $parent ) {
mp-wp_genesis 268 $parent = $page->post_parent;
mp-wp_genesis 269 break;
mp-wp_genesis 270 }
mp-wp_genesis 271 }
mp-wp_genesis 272 }
mp-wp_genesis 273 }
mp-wp_genesis 274
mp-wp_genesis 275 $updated = $skipped = $locked = array();
mp-wp_genesis 276 foreach ( $post_IDs as $post_ID ) {
mp-wp_genesis 277
mp-wp_genesis 278 if ( isset($children) && in_array($post_ID, $children) ) {
mp-wp_genesis 279 $skipped[] = $post_ID;
mp-wp_genesis 280 continue;
mp-wp_genesis 281 }
mp-wp_genesis 282
mp-wp_genesis 283 if ( wp_check_post_lock( $post_ID ) ) {
mp-wp_genesis 284 $locked[] = $post_ID;
mp-wp_genesis 285 continue;
mp-wp_genesis 286 }
mp-wp_genesis 287
mp-wp_genesis 288 if ( isset($new_cats) ) {
mp-wp_genesis 289 $cats = (array) wp_get_post_categories($post_ID);
mp-wp_genesis 290 $post_data['post_category'] = array_unique( array_merge($cats, $new_cats) );
mp-wp_genesis 291 }
mp-wp_genesis 292
mp-wp_genesis 293 if ( isset($new_tags) ) {
mp-wp_genesis 294 $tags = wp_get_post_tags($post_ID, array('fields' => 'names'));
mp-wp_genesis 295 $post_data['tags_input'] = array_unique( array_merge($tags, $new_tags) );
mp-wp_genesis 296 }
mp-wp_genesis 297
mp-wp_genesis 298 $post_data['ID'] = $post_ID;
mp-wp_genesis 299 $updated[] = wp_update_post( $post_data );
mp-wp_genesis 300
mp-wp_genesis 301 if ( current_user_can( 'edit_others_posts' ) && isset( $post_data['sticky'] ) ) {
mp-wp_genesis 302 if ( 'sticky' == $post_data['sticky'] )
mp-wp_genesis 303 stick_post( $post_ID );
mp-wp_genesis 304 else
mp-wp_genesis 305 unstick_post( $post_ID );
mp-wp_genesis 306 }
mp-wp_genesis 307
mp-wp_genesis 308 }
mp-wp_genesis 309
mp-wp_genesis 310 return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );
mp-wp_genesis 311 }
mp-wp_genesis 312
mp-wp_genesis 313 /**
mp-wp_genesis 314 * Default post information to use when populating the "Write Post" form.
mp-wp_genesis 315 *
mp-wp_genesis 316 * @since unknown
mp-wp_genesis 317 *
mp-wp_genesis 318 * @return unknown
mp-wp_genesis 319 */
mp-wp_genesis 320 function get_default_post_to_edit() {
mp-wp_genesis 321 if ( !empty( $_REQUEST['post_title'] ) )
mp-wp_genesis 322 $post_title = wp_specialchars( stripslashes( $_REQUEST['post_title'] ));
mp-wp_genesis 323 else if ( !empty( $_REQUEST['popuptitle'] ) ) {
mp-wp_genesis 324 $post_title = wp_specialchars( stripslashes( $_REQUEST['popuptitle'] ));
mp-wp_genesis 325 $post_title = funky_javascript_fix( $post_title );
mp-wp_genesis 326 } else {
mp-wp_genesis 327 $post_title = '';
mp-wp_genesis 328 }
mp-wp_genesis 329
mp-wp_genesis 330 $post_content = '';
mp-wp_genesis 331 if ( !empty( $_REQUEST['content'] ) )
mp-wp_genesis 332 $post_content = wp_specialchars( stripslashes( $_REQUEST['content'] ));
mp-wp_genesis 333 else if ( !empty( $post_title ) ) {
mp-wp_genesis 334 $text = wp_specialchars( stripslashes( urldecode( $_REQUEST['text'] ) ) );
mp-wp_genesis 335 $text = funky_javascript_fix( $text);
mp-wp_genesis 336 $popupurl = clean_url($_REQUEST['popupurl']);
mp-wp_genesis 337 $post_content = '<a href="'.$popupurl.'">'.$post_title.'</a>'."\n$text";
mp-wp_genesis 338 }
mp-wp_genesis 339
mp-wp_genesis 340 if ( !empty( $_REQUEST['excerpt'] ) )
mp-wp_genesis 341 $post_excerpt = wp_specialchars( stripslashes( $_REQUEST['excerpt'] ));
mp-wp_genesis 342 else
mp-wp_genesis 343 $post_excerpt = '';
mp-wp_genesis 344
mp-wp_genesis 345 $post->ID = 0;
mp-wp_genesis 346 $post->post_name = '';
mp-wp_genesis 347 $post->post_author = '';
mp-wp_genesis 348 $post->post_date = '';
mp-wp_genesis 349 $post->post_password = '';
mp-wp_genesis 350 $post->post_status = 'draft';
mp-wp_genesis 351 $post->post_type = 'post';
mp-wp_genesis 352 $post->to_ping = '';
mp-wp_genesis 353 $post->pinged = '';
mp-wp_genesis 354 $post->comment_status = get_option( 'default_comment_status' );
mp-wp_genesis 355 $post->ping_status = get_option( 'default_ping_status' );
mp-wp_genesis 356 $post->post_pingback = get_option( 'default_pingback_flag' );
mp-wp_genesis 357 $post->post_category = get_option( 'default_category' );
mp-wp_genesis 358 $post->post_content = apply_filters( 'default_content', $post_content);
mp-wp_genesis 359 $post->post_title = apply_filters( 'default_title', $post_title );
mp-wp_genesis 360 $post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt);
mp-wp_genesis 361 $post->page_template = 'default';
mp-wp_genesis 362 $post->post_parent = 0;
mp-wp_genesis 363 $post->menu_order = 0;
mp-wp_genesis 364
mp-wp_genesis 365 return $post;
mp-wp_genesis 366 }
mp-wp_genesis 367
mp-wp_genesis 368 /**
mp-wp_genesis 369 * {@internal Missing Short Description}}
mp-wp_genesis 370 *
mp-wp_genesis 371 * @since unknown
mp-wp_genesis 372 *
mp-wp_genesis 373 * @return unknown
mp-wp_genesis 374 */
mp-wp_genesis 375 function get_default_page_to_edit() {
mp-wp_genesis 376 $page = get_default_post_to_edit();
mp-wp_genesis 377 $page->post_type = 'page';
mp-wp_genesis 378 return $page;
mp-wp_genesis 379 }
mp-wp_genesis 380
mp-wp_genesis 381 /**
mp-wp_genesis 382 * Get an existing post and format it for editing.
mp-wp_genesis 383 *
mp-wp_genesis 384 * @since unknown
mp-wp_genesis 385 *
mp-wp_genesis 386 * @param unknown_type $id
mp-wp_genesis 387 * @return unknown
mp-wp_genesis 388 */
mp-wp_genesis 389 function get_post_to_edit( $id ) {
mp-wp_genesis 390
mp-wp_genesis 391 $post = get_post( $id, OBJECT, 'edit' );
mp-wp_genesis 392
mp-wp_genesis 393 if ( $post->post_type == 'page' )
mp-wp_genesis 394 $post->page_template = get_post_meta( $id, '_wp_page_template', true );
mp-wp_genesis 395
mp-wp_genesis 396 return $post;
mp-wp_genesis 397 }
mp-wp_genesis 398
mp-wp_genesis 399 /**
mp-wp_genesis 400 * {@internal Missing Short Description}}
mp-wp_genesis 401 *
mp-wp_genesis 402 * @since unknown
mp-wp_genesis 403 *
mp-wp_genesis 404 * @param unknown_type $title
mp-wp_genesis 405 * @param unknown_type $content
mp-wp_genesis 406 * @param unknown_type $post_date
mp-wp_genesis 407 * @return unknown
mp-wp_genesis 408 */
mp-wp_genesis 409 function post_exists($title, $content = '', $post_date = '') {
mp-wp_genesis 410 global $wpdb;
mp-wp_genesis 411
mp-wp_genesis 412 $title = stripslashes($title);
mp-wp_genesis 413 $content = stripslashes($content);
mp-wp_genesis 414 $post_date = stripslashes($post_date);
mp-wp_genesis 415
mp-wp_genesis 416 if (!empty ($post_date))
mp-wp_genesis 417 $post_date = $wpdb->prepare("AND post_date = %s", $post_date);
mp-wp_genesis 418
mp-wp_genesis 419 if (!empty ($title))
mp-wp_genesis 420 return $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s $post_date", $title) );
mp-wp_genesis 421 else
mp-wp_genesis 422 if (!empty ($content))
mp-wp_genesis 423 return $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_content = %s $post_date", $content) );
mp-wp_genesis 424
mp-wp_genesis 425 return 0;
mp-wp_genesis 426 }
mp-wp_genesis 427
mp-wp_genesis 428 /**
mp-wp_genesis 429 * Creates a new post from the "Write Post" form using $_POST information.
mp-wp_genesis 430 *
mp-wp_genesis 431 * @since unknown
mp-wp_genesis 432 *
mp-wp_genesis 433 * @return unknown
mp-wp_genesis 434 */
mp-wp_genesis 435 function wp_write_post() {
mp-wp_genesis 436 global $user_ID;
mp-wp_genesis 437
mp-wp_genesis 438 if ( 'page' == $_POST['post_type'] ) {
mp-wp_genesis 439 if ( !current_user_can( 'edit_pages' ) )
mp-wp_genesis 440 return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this blog.' ) );
mp-wp_genesis 441 } else {
mp-wp_genesis 442 if ( !current_user_can( 'edit_posts' ) )
mp-wp_genesis 443 return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this blog.' ) );
mp-wp_genesis 444 }
mp-wp_genesis 445
mp-wp_genesis 446
mp-wp_genesis 447 // Check for autosave collisions
mp-wp_genesis 448 $temp_id = false;
mp-wp_genesis 449 if ( isset($_POST['temp_ID']) ) {
mp-wp_genesis 450 $temp_id = (int) $_POST['temp_ID'];
mp-wp_genesis 451 if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
mp-wp_genesis 452 $draft_ids = array();
mp-wp_genesis 453 foreach ( $draft_ids as $temp => $real )
mp-wp_genesis 454 if ( time() + $temp > 86400 ) // 1 day: $temp is equal to -1 * time( then )
mp-wp_genesis 455 unset($draft_ids[$temp]);
mp-wp_genesis 456
mp-wp_genesis 457 if ( isset($draft_ids[$temp_id]) ) { // Edit, don't write
mp-wp_genesis 458 $_POST['post_ID'] = $draft_ids[$temp_id];
mp-wp_genesis 459 unset($_POST['temp_ID']);
mp-wp_genesis 460 update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
mp-wp_genesis 461 return edit_post();
mp-wp_genesis 462 }
mp-wp_genesis 463 }
mp-wp_genesis 464
mp-wp_genesis 465 $translated = _wp_translate_postdata( false );
mp-wp_genesis 466 if ( is_wp_error($translated) )
mp-wp_genesis 467 return $translated;
mp-wp_genesis 468
mp-wp_genesis 469 if ( isset($_POST['visibility']) ) {
mp-wp_genesis 470 switch ( $_POST['visibility'] ) {
mp-wp_genesis 471 case 'public' :
mp-wp_genesis 472 $_POST['post_password'] = '';
mp-wp_genesis 473 break;
mp-wp_genesis 474 case 'password' :
mp-wp_genesis 475 unset( $_POST['sticky'] );
mp-wp_genesis 476 break;
mp-wp_genesis 477 case 'private' :
mp-wp_genesis 478 $_POST['post_status'] = 'private';
mp-wp_genesis 479 $_POST['post_password'] = '';
mp-wp_genesis 480 unset( $_POST['sticky'] );
mp-wp_genesis 481 break;
mp-wp_genesis 482 }
mp-wp_genesis 483 }
mp-wp_genesis 484
mp-wp_genesis 485 // Create the post.
mp-wp_genesis 486 $post_ID = wp_insert_post( $_POST );
mp-wp_genesis 487 if ( is_wp_error( $post_ID ) )
mp-wp_genesis 488 return $post_ID;
mp-wp_genesis 489
mp-wp_genesis 490 if ( empty($post_ID) )
mp-wp_genesis 491 return 0;
mp-wp_genesis 492
mp-wp_genesis 493 add_meta( $post_ID );
mp-wp_genesis 494
mp-wp_genesis 495 // Reunite any orphaned attachments with their parent
mp-wp_genesis 496 if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
mp-wp_genesis 497 $draft_ids = array();
mp-wp_genesis 498 if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )
mp-wp_genesis 499 _relocate_children( $draft_temp_id, $post_ID );
mp-wp_genesis 500 if ( $temp_id && $temp_id != $draft_temp_id )
mp-wp_genesis 501 _relocate_children( $temp_id, $post_ID );
mp-wp_genesis 502
mp-wp_genesis 503 // Update autosave collision detection
mp-wp_genesis 504 if ( $temp_id ) {
mp-wp_genesis 505 $draft_ids[$temp_id] = $post_ID;
mp-wp_genesis 506 update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
mp-wp_genesis 507 }
mp-wp_genesis 508
mp-wp_genesis 509 // Now that we have an ID we can fix any attachment anchor hrefs
mp-wp_genesis 510 _fix_attachment_links( $post_ID );
mp-wp_genesis 511
mp-wp_genesis 512 wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );
mp-wp_genesis 513
mp-wp_genesis 514 return $post_ID;
mp-wp_genesis 515 }
mp-wp_genesis 516
mp-wp_genesis 517 /**
mp-wp_genesis 518 * Calls wp_write_post() and handles the errors.
mp-wp_genesis 519 *
mp-wp_genesis 520 * @since unknown
mp-wp_genesis 521 *
mp-wp_genesis 522 * @return unknown
mp-wp_genesis 523 */
mp-wp_genesis 524 function write_post() {
mp-wp_genesis 525 $result = wp_write_post();
mp-wp_genesis 526 if( is_wp_error( $result ) )
mp-wp_genesis 527 wp_die( $result->get_error_message() );
mp-wp_genesis 528 else
mp-wp_genesis 529 return $result;
mp-wp_genesis 530 }
mp-wp_genesis 531
mp-wp_genesis 532 //
mp-wp_genesis 533 // Post Meta
mp-wp_genesis 534 //
mp-wp_genesis 535
mp-wp_genesis 536 /**
mp-wp_genesis 537 * {@internal Missing Short Description}}
mp-wp_genesis 538 *
mp-wp_genesis 539 * @since unknown
mp-wp_genesis 540 *
mp-wp_genesis 541 * @param unknown_type $post_ID
mp-wp_genesis 542 * @return unknown
mp-wp_genesis 543 */
mp-wp_genesis 544 function add_meta( $post_ID ) {
mp-wp_genesis 545 global $wpdb;
mp-wp_genesis 546 $post_ID = (int) $post_ID;
mp-wp_genesis 547
mp-wp_genesis 548 $protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
mp-wp_genesis 549
mp-wp_genesis 550 $metakeyselect = isset($_POST['metakeyselect']) ? stripslashes( trim( $_POST['metakeyselect'] ) ) : '';
mp-wp_genesis 551 $metakeyinput = isset($_POST['metakeyinput']) ? stripslashes( trim( $_POST['metakeyinput'] ) ) : '';
mp-wp_genesis 552 $metavalue = isset($_POST['metavalue']) ? maybe_serialize( stripslashes( trim( $_POST['metavalue'] ) ) ) : '';
mp-wp_genesis 553
mp-wp_genesis 554 if ( ('0' === $metavalue || !empty ( $metavalue ) ) && ((('#NONE#' != $metakeyselect) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput) ) ) {
mp-wp_genesis 555 // We have a key/value pair. If both the select and the
mp-wp_genesis 556 // input for the key have data, the input takes precedence:
mp-wp_genesis 557
mp-wp_genesis 558 if ('#NONE#' != $metakeyselect)
mp-wp_genesis 559 $metakey = $metakeyselect;
mp-wp_genesis 560
mp-wp_genesis 561 if ( $metakeyinput)
mp-wp_genesis 562 $metakey = $metakeyinput; // default
mp-wp_genesis 563
mp-wp_genesis 564 if ( in_array($metakey, $protected) )
mp-wp_genesis 565 return false;
mp-wp_genesis 566
mp-wp_genesis 567 wp_cache_delete($post_ID, 'post_meta');
mp-wp_genesis 568
mp-wp_genesis 569 $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value ) VALUES (%s, %s, %s)", $post_ID, $metakey, $metavalue) );
mp-wp_genesis 570 return $wpdb->insert_id;
mp-wp_genesis 571 }
mp-wp_genesis 572 return false;
mp-wp_genesis 573 } // add_meta
mp-wp_genesis 574
mp-wp_genesis 575 /**
mp-wp_genesis 576 * {@internal Missing Short Description}}
mp-wp_genesis 577 *
mp-wp_genesis 578 * @since unknown
mp-wp_genesis 579 *
mp-wp_genesis 580 * @param unknown_type $mid
mp-wp_genesis 581 * @return unknown
mp-wp_genesis 582 */
mp-wp_genesis 583 function delete_meta( $mid ) {
mp-wp_genesis 584 global $wpdb;
mp-wp_genesis 585 $mid = (int) $mid;
mp-wp_genesis 586
mp-wp_genesis 587 $post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
mp-wp_genesis 588 wp_cache_delete($post_id, 'post_meta');
mp-wp_genesis 589
mp-wp_genesis 590 return $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
mp-wp_genesis 591 }
mp-wp_genesis 592
mp-wp_genesis 593 /**
mp-wp_genesis 594 * Get a list of previously defined keys.
mp-wp_genesis 595 *
mp-wp_genesis 596 * @since unknown
mp-wp_genesis 597 *
mp-wp_genesis 598 * @return unknown
mp-wp_genesis 599 */
mp-wp_genesis 600 function get_meta_keys() {
mp-wp_genesis 601 global $wpdb;
mp-wp_genesis 602
mp-wp_genesis 603 $keys = $wpdb->get_col( "
mp-wp_genesis 604 SELECT meta_key
mp-wp_genesis 605 FROM $wpdb->postmeta
mp-wp_genesis 606 GROUP BY meta_key
mp-wp_genesis 607 ORDER BY meta_key" );
mp-wp_genesis 608
mp-wp_genesis 609 return $keys;
mp-wp_genesis 610 }
mp-wp_genesis 611
mp-wp_genesis 612 /**
mp-wp_genesis 613 * {@internal Missing Short Description}}
mp-wp_genesis 614 *
mp-wp_genesis 615 * @since unknown
mp-wp_genesis 616 *
mp-wp_genesis 617 * @param unknown_type $mid
mp-wp_genesis 618 * @return unknown
mp-wp_genesis 619 */
mp-wp_genesis 620 function get_post_meta_by_id( $mid ) {
mp-wp_genesis 621 global $wpdb;
mp-wp_genesis 622 $mid = (int) $mid;
mp-wp_genesis 623
mp-wp_genesis 624 $meta = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
mp-wp_genesis 625 if ( is_serialized_string( $meta->meta_value ) )
mp-wp_genesis 626 $meta->meta_value = maybe_unserialize( $meta->meta_value );
mp-wp_genesis 627 return $meta;
mp-wp_genesis 628 }
mp-wp_genesis 629
mp-wp_genesis 630 /**
mp-wp_genesis 631 * {@internal Missing Short Description}}
mp-wp_genesis 632 *
mp-wp_genesis 633 * Some postmeta stuff.
mp-wp_genesis 634 *
mp-wp_genesis 635 * @since unknown
mp-wp_genesis 636 *
mp-wp_genesis 637 * @param unknown_type $postid
mp-wp_genesis 638 * @return unknown
mp-wp_genesis 639 */
mp-wp_genesis 640 function has_meta( $postid ) {
mp-wp_genesis 641 global $wpdb;
mp-wp_genesis 642
mp-wp_genesis 643 return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
mp-wp_genesis 644 FROM $wpdb->postmeta WHERE post_id = %d
mp-wp_genesis 645 ORDER BY meta_key,meta_id", $postid), ARRAY_A );
mp-wp_genesis 646
mp-wp_genesis 647 }
mp-wp_genesis 648
mp-wp_genesis 649 /**
mp-wp_genesis 650 * {@internal Missing Short Description}}
mp-wp_genesis 651 *
mp-wp_genesis 652 * @since unknown
mp-wp_genesis 653 *
mp-wp_genesis 654 * @param unknown_type $meta_id
mp-wp_genesis 655 * @param unknown_type $meta_key
mp-wp_genesis 656 * @param unknown_type $meta_value
mp-wp_genesis 657 * @return unknown
mp-wp_genesis 658 */
mp-wp_genesis 659 function update_meta( $meta_id, $meta_key, $meta_value ) {
mp-wp_genesis 660 global $wpdb;
mp-wp_genesis 661
mp-wp_genesis 662 $protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
mp-wp_genesis 663
mp-wp_genesis 664 if ( in_array($meta_key, $protected) )
mp-wp_genesis 665 return false;
mp-wp_genesis 666
mp-wp_genesis 667 $post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $meta_id) );
mp-wp_genesis 668 wp_cache_delete($post_id, 'post_meta');
mp-wp_genesis 669
mp-wp_genesis 670 $meta_value = maybe_serialize( stripslashes( $meta_value ));
mp-wp_genesis 671 $meta_id = (int) $meta_id;
mp-wp_genesis 672
mp-wp_genesis 673 $data = compact( 'meta_key', 'meta_value' );
mp-wp_genesis 674 $where = compact( 'meta_id' );
mp-wp_genesis 675
mp-wp_genesis 676 return $wpdb->update( $wpdb->postmeta, $data, $where );
mp-wp_genesis 677 }
mp-wp_genesis 678
mp-wp_genesis 679 //
mp-wp_genesis 680 // Private
mp-wp_genesis 681 //
mp-wp_genesis 682
mp-wp_genesis 683 /**
mp-wp_genesis 684 * Replace hrefs of attachment anchors with up-to-date permalinks.
mp-wp_genesis 685 *
mp-wp_genesis 686 * @since unknown
mp-wp_genesis 687 * @access private
mp-wp_genesis 688 *
mp-wp_genesis 689 * @param unknown_type $post_ID
mp-wp_genesis 690 * @return unknown
mp-wp_genesis 691 */
mp-wp_genesis 692 function _fix_attachment_links( $post_ID ) {
mp-wp_genesis 693
mp-wp_genesis 694 $post = & get_post( $post_ID, ARRAY_A );
mp-wp_genesis 695
mp-wp_genesis 696 $search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie";
mp-wp_genesis 697
mp-wp_genesis 698 // See if we have any rel="attachment" links
mp-wp_genesis 699 if ( 0 == preg_match_all( $search, $post['post_content'], $anchor_matches, PREG_PATTERN_ORDER ) )
mp-wp_genesis 700 return;
mp-wp_genesis 701
mp-wp_genesis 702 $i = 0;
mp-wp_genesis 703 $search = "#[\s]+rel=(\"|')(.*?)wp-att-(\d+)\\1#i";
mp-wp_genesis 704 foreach ( $anchor_matches[0] as $anchor ) {
mp-wp_genesis 705 if ( 0 == preg_match( $search, $anchor, $id_matches ) )
mp-wp_genesis 706 continue;
mp-wp_genesis 707
mp-wp_genesis 708 $id = (int) $id_matches[3];
mp-wp_genesis 709
mp-wp_genesis 710 // While we have the attachment ID, let's adopt any orphans.
mp-wp_genesis 711 $attachment = & get_post( $id, ARRAY_A );
mp-wp_genesis 712 if ( ! empty( $attachment) && ! is_object( get_post( $attachment['post_parent'] ) ) ) {
mp-wp_genesis 713 $attachment['post_parent'] = $post_ID;
mp-wp_genesis 714 // Escape data pulled from DB.
mp-wp_genesis 715 $attachment = add_magic_quotes( $attachment);
mp-wp_genesis 716 wp_update_post( $attachment);
mp-wp_genesis 717 }
mp-wp_genesis 718
mp-wp_genesis 719 $post_search[$i] = $anchor;
mp-wp_genesis 720 $post_replace[$i] = preg_replace( "#href=(\"|')[^'\"]*\\1#e", "stripslashes( 'href=\\1' ).get_attachment_link( $id ).stripslashes( '\\1' )", $anchor );
mp-wp_genesis 721 ++$i;
mp-wp_genesis 722 }
mp-wp_genesis 723
mp-wp_genesis 724 $post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] );
mp-wp_genesis 725
mp-wp_genesis 726 // Escape data pulled from DB.
mp-wp_genesis 727 $post = add_magic_quotes( $post);
mp-wp_genesis 728
mp-wp_genesis 729 return wp_update_post( $post);
mp-wp_genesis 730 }
mp-wp_genesis 731
mp-wp_genesis 732 /**
mp-wp_genesis 733 * Move child posts to a new parent.
mp-wp_genesis 734 *
mp-wp_genesis 735 * @since unknown
mp-wp_genesis 736 * @access private
mp-wp_genesis 737 *
mp-wp_genesis 738 * @param unknown_type $old_ID
mp-wp_genesis 739 * @param unknown_type $new_ID
mp-wp_genesis 740 * @return unknown
mp-wp_genesis 741 */
mp-wp_genesis 742 function _relocate_children( $old_ID, $new_ID ) {
mp-wp_genesis 743 global $wpdb;
mp-wp_genesis 744 $old_ID = (int) $old_ID;
mp-wp_genesis 745 $new_ID = (int) $new_ID;
mp-wp_genesis 746 return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = %d WHERE post_parent = %d", $new_ID, $old_ID) );
mp-wp_genesis 747 }
mp-wp_genesis 748
mp-wp_genesis 749 /**
mp-wp_genesis 750 * {@internal Missing Short Description}}
mp-wp_genesis 751 *
mp-wp_genesis 752 * @since unknown
mp-wp_genesis 753 *
mp-wp_genesis 754 * @param unknown_type $type
mp-wp_genesis 755 * @return unknown
mp-wp_genesis 756 */
mp-wp_genesis 757 function get_available_post_statuses($type = 'post') {
mp-wp_genesis 758 $stati = wp_count_posts($type);
mp-wp_genesis 759
mp-wp_genesis 760 return array_keys(get_object_vars($stati));
mp-wp_genesis 761 }
mp-wp_genesis 762
mp-wp_genesis 763 /**
mp-wp_genesis 764 * {@internal Missing Short Description}}
mp-wp_genesis 765 *
mp-wp_genesis 766 * @since unknown
mp-wp_genesis 767 *
mp-wp_genesis 768 * @param unknown_type $q
mp-wp_genesis 769 * @return unknown
mp-wp_genesis 770 */
mp-wp_genesis 771 function wp_edit_posts_query( $q = false ) {
mp-wp_genesis 772 if ( false === $q )
mp-wp_genesis 773 $q = $_GET;
mp-wp_genesis 774 $q['m'] = isset($q['m']) ? (int) $q['m'] : 0;
mp-wp_genesis 775 $q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0;
mp-wp_genesis 776 $post_stati = array( // array( adj, noun )
mp-wp_genesis 777 'publish' => array(__('Published'), __('Published posts'), __ngettext_noop('Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>')),
mp-wp_genesis 778 'future' => array(__('Scheduled'), __('Scheduled posts'), __ngettext_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>')),
mp-wp_genesis 779 'pending' => array(__('Pending Review'), __('Pending posts'), __ngettext_noop('Pending Review <span class="count">(%s)</span>', 'Pending Review <span class="count">(%s)</span>')),
mp-wp_genesis 780 'draft' => array(__('Draft'), _c('Drafts|manage posts header'), __ngettext_noop('Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>')),
mp-wp_genesis 781 'private' => array(__('Private'), __('Private posts'), __ngettext_noop('Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>')),
mp-wp_genesis 782 );
mp-wp_genesis 783
mp-wp_genesis 784 $post_stati = apply_filters('post_stati', $post_stati);
mp-wp_genesis 785
mp-wp_genesis 786 $avail_post_stati = get_available_post_statuses('post');
mp-wp_genesis 787
mp-wp_genesis 788 $post_status_q = '';
mp-wp_genesis 789 if ( isset($q['post_status']) && in_array( $q['post_status'], array_keys($post_stati) ) ) {
mp-wp_genesis 790 $post_status_q = '&post_status=' . $q['post_status'];
mp-wp_genesis 791 $post_status_q .= '&perm=readable';
mp-wp_genesis 792 }
mp-wp_genesis 793
mp-wp_genesis 794 if ( isset($q['post_status']) && 'pending' === $q['post_status'] ) {
mp-wp_genesis 795 $order = 'ASC';
mp-wp_genesis 796 $orderby = 'modified';
mp-wp_genesis 797 } elseif ( isset($q['post_status']) && 'draft' === $q['post_status'] ) {
mp-wp_genesis 798 $order = 'DESC';
mp-wp_genesis 799 $orderby = 'modified';
mp-wp_genesis 800 } else {
mp-wp_genesis 801 $order = 'DESC';
mp-wp_genesis 802 $orderby = 'date';
mp-wp_genesis 803 }
mp-wp_genesis 804
mp-wp_genesis 805 wp("post_type=post&what_to_show=posts$post_status_q&posts_per_page=15&order=$order&orderby=$orderby");
mp-wp_genesis 806
mp-wp_genesis 807 return array($post_stati, $avail_post_stati);
mp-wp_genesis 808 }
mp-wp_genesis 809
mp-wp_genesis 810 /**
mp-wp_genesis 811 * {@internal Missing Short Description}}
mp-wp_genesis 812 *
mp-wp_genesis 813 * @since unknown
mp-wp_genesis 814 *
mp-wp_genesis 815 * @param unknown_type $type
mp-wp_genesis 816 * @return unknown
mp-wp_genesis 817 */
mp-wp_genesis 818 function get_available_post_mime_types($type = 'attachment') {
mp-wp_genesis 819 global $wpdb;
mp-wp_genesis 820
mp-wp_genesis 821 $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
mp-wp_genesis 822 return $types;
mp-wp_genesis 823 }
mp-wp_genesis 824
mp-wp_genesis 825 /**
mp-wp_genesis 826 * {@internal Missing Short Description}}
mp-wp_genesis 827 *
mp-wp_genesis 828 * @since unknown
mp-wp_genesis 829 *
mp-wp_genesis 830 * @param unknown_type $q
mp-wp_genesis 831 * @return unknown
mp-wp_genesis 832 */
mp-wp_genesis 833 function wp_edit_attachments_query( $q = false ) {
mp-wp_genesis 834 if ( false === $q )
mp-wp_genesis 835 $q = $_GET;
mp-wp_genesis 836
mp-wp_genesis 837 $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0;
mp-wp_genesis 838 $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
mp-wp_genesis 839 $q['post_type'] = 'attachment';
mp-wp_genesis 840 $q['post_status'] = 'any';
mp-wp_genesis 841 $q['posts_per_page'] = 15;
mp-wp_genesis 842 $post_mime_types = array( // array( adj, noun )
mp-wp_genesis 843 'image' => array(__('Images'), __('Manage Images'), __ngettext_noop('Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>')),
mp-wp_genesis 844 'audio' => array(__('Audio'), __('Manage Audio'), __ngettext_noop('Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>')),
mp-wp_genesis 845 'video' => array(__('Video'), __('Manage Video'), __ngettext_noop('Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>')),
mp-wp_genesis 846 );
mp-wp_genesis 847 $post_mime_types = apply_filters('post_mime_types', $post_mime_types);
mp-wp_genesis 848
mp-wp_genesis 849 $avail_post_mime_types = get_available_post_mime_types('attachment');
mp-wp_genesis 850
mp-wp_genesis 851 if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) )
mp-wp_genesis 852 unset($q['post_mime_type']);
mp-wp_genesis 853
mp-wp_genesis 854 wp($q);
mp-wp_genesis 855
mp-wp_genesis 856 return array($post_mime_types, $avail_post_mime_types);
mp-wp_genesis 857 }
mp-wp_genesis 858
mp-wp_genesis 859 /**
mp-wp_genesis 860 * {@internal Missing Short Description}}
mp-wp_genesis 861 *
mp-wp_genesis 862 * @since unknown
mp-wp_genesis 863 *
mp-wp_genesis 864 * @param unknown_type $id
mp-wp_genesis 865 * @param unknown_type $page
mp-wp_genesis 866 * @return unknown
mp-wp_genesis 867 */
mp-wp_genesis 868 function postbox_classes( $id, $page ) {
mp-wp_genesis 869 if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id )
mp-wp_genesis 870 return '';
mp-wp_genesis 871 $current_user = wp_get_current_user();
mp-wp_genesis 872 if ( $closed = get_user_option('closedpostboxes_'.$page, 0, false ) ) {
mp-wp_genesis 873 if ( !is_array( $closed ) ) return '';
mp-wp_genesis 874 return in_array( $id, $closed )? 'if-js-closed' : '';
mp-wp_genesis 875 } else {
mp-wp_genesis 876 return '';
mp-wp_genesis 877 }
mp-wp_genesis 878 }
mp-wp_genesis 879
mp-wp_genesis 880 /**
mp-wp_genesis 881 * {@internal Missing Short Description}}
mp-wp_genesis 882 *
mp-wp_genesis 883 * @since unknown
mp-wp_genesis 884 *
mp-wp_genesis 885 * @param unknown_type $id
mp-wp_genesis 886 * @param unknown_type $title
mp-wp_genesis 887 * @param unknown_type $name
mp-wp_genesis 888 * @return unknown
mp-wp_genesis 889 */
mp-wp_genesis 890 function get_sample_permalink($id, $title=null, $name = null) {
mp-wp_genesis 891 $post = &get_post($id);
mp-wp_genesis 892 if (!$post->ID) {
mp-wp_genesis 893 return array('', '');
mp-wp_genesis 894 }
mp-wp_genesis 895 $original_status = $post->post_status;
mp-wp_genesis 896 $original_date = $post->post_date;
mp-wp_genesis 897 $original_name = $post->post_name;
mp-wp_genesis 898
mp-wp_genesis 899 // Hack: get_permalink would return ugly permalink for
mp-wp_genesis 900 // drafts, so we will fake, that our post is published
mp-wp_genesis 901 if (in_array($post->post_status, array('draft', 'pending'))) {
mp-wp_genesis 902 $post->post_status = 'publish';
mp-wp_genesis 903 $post->post_name = sanitize_title($post->post_name? $post->post_name : $post->post_title, $post->ID);
mp-wp_genesis 904 }
mp-wp_genesis 905
mp-wp_genesis 906 // If the user wants to set a new name -- override the current one
mp-wp_genesis 907 // Note: if empty name is supplied -- use the title instead, see #6072
mp-wp_genesis 908 if (!is_null($name)) {
mp-wp_genesis 909 $post->post_name = sanitize_title($name? $name : $title, $post->ID);
mp-wp_genesis 910 }
mp-wp_genesis 911
mp-wp_genesis 912 $permalink = get_permalink($post, true);
mp-wp_genesis 913
mp-wp_genesis 914 // Handle page hierarchy
mp-wp_genesis 915 if ( 'page' == $post->post_type ) {
mp-wp_genesis 916 $uri = get_page_uri($post->ID);
mp-wp_genesis 917 $uri = untrailingslashit($uri);
mp-wp_genesis 918 $uri = strrev( stristr( strrev( $uri ), '/' ) );
mp-wp_genesis 919 $uri = untrailingslashit($uri);
mp-wp_genesis 920 if ( !empty($uri) )
mp-wp_genesis 921 $uri .='/';
mp-wp_genesis 922 $permalink = str_replace('%pagename%', "${uri}%pagename%", $permalink);
mp-wp_genesis 923 }
mp-wp_genesis 924
mp-wp_genesis 925 $permalink = array($permalink, apply_filters('editable_slug', $post->post_name));
mp-wp_genesis 926 $post->post_status = $original_status;
mp-wp_genesis 927 $post->post_date = $original_date;
mp-wp_genesis 928 $post->post_name = $original_name;
mp-wp_remove-all-... 929
mp-wp_genesis 930 return $permalink;
mp-wp_genesis 931 }
mp-wp_genesis 932
mp-wp_genesis 933 /**
mp-wp_genesis 934 * {@internal Missing Short Description}}
mp-wp_genesis 935 *
mp-wp_genesis 936 * @since unknown
mp-wp_genesis 937 *
mp-wp_genesis 938 * @param unknown_type $id
mp-wp_genesis 939 * @param unknown_type $new_title
mp-wp_genesis 940 * @param unknown_type $new_slug
mp-wp_genesis 941 * @return unknown
mp-wp_genesis 942 */
mp-wp_genesis 943 function get_sample_permalink_html($id, $new_title=null, $new_slug=null) {
mp-wp_genesis 944 $post = &get_post($id);
mp-wp_genesis 945 list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
mp-wp_genesis 946 if (false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%')) {
mp-wp_genesis 947 return '';
mp-wp_genesis 948 }
mp-wp_remove-all-... 949 $display_link = str_replace(array('%pagename%/','%postname%/'), '', $permalink);
mp-wp_remove-all-... 950 $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink">' . $display_link . "</span>".'<input type="text" id="new-post-slug" name="post_name" value="' . $post_name . '" />';
mp-wp_genesis 951 return $return;
mp-wp_genesis 952 }
mp-wp_genesis 953
mp-wp_genesis 954 /**
mp-wp_genesis 955 * {@internal Missing Short Description}}
mp-wp_genesis 956 *
mp-wp_genesis 957 * @since unknown
mp-wp_genesis 958 *
mp-wp_genesis 959 * @param unknown_type $post_id
mp-wp_genesis 960 * @return bool|int False: not locked or locked by current user. Int: user ID of user with lock.
mp-wp_genesis 961 */
mp-wp_genesis 962 function wp_check_post_lock( $post_id ) {
mp-wp_genesis 963 global $current_user;
mp-wp_genesis 964
mp-wp_genesis 965 if ( !$post = get_post( $post_id ) )
mp-wp_genesis 966 return false;
mp-wp_genesis 967
mp-wp_genesis 968 $lock = get_post_meta( $post->ID, '_edit_lock', true );
mp-wp_genesis 969 $last = get_post_meta( $post->ID, '_edit_last', true );
mp-wp_genesis 970
mp-wp_genesis 971 $time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 );
mp-wp_genesis 972
mp-wp_genesis 973 if ( $lock && $lock > time() - $time_window && $last != $current_user->ID )
mp-wp_genesis 974 return $last;
mp-wp_genesis 975 return false;
mp-wp_genesis 976 }
mp-wp_genesis 977
mp-wp_genesis 978 /**
mp-wp_genesis 979 * {@internal Missing Short Description}}
mp-wp_genesis 980 *
mp-wp_genesis 981 * @since unknown
mp-wp_genesis 982 *
mp-wp_genesis 983 * @param unknown_type $post_id
mp-wp_genesis 984 * @return unknown
mp-wp_genesis 985 */
mp-wp_genesis 986 function wp_set_post_lock( $post_id ) {
mp-wp_genesis 987 global $current_user;
mp-wp_genesis 988 if ( !$post = get_post( $post_id ) )
mp-wp_genesis 989 return false;
mp-wp_genesis 990 if ( !$current_user || !$current_user->ID )
mp-wp_genesis 991 return false;
mp-wp_genesis 992
mp-wp_genesis 993 $now = time();
mp-wp_genesis 994
mp-wp_genesis 995 if ( !add_post_meta( $post->ID, '_edit_lock', $now, true ) )
mp-wp_genesis 996 update_post_meta( $post->ID, '_edit_lock', $now );
mp-wp_genesis 997 if ( !add_post_meta( $post->ID, '_edit_last', $current_user->ID, true ) )
mp-wp_genesis 998 update_post_meta( $post->ID, '_edit_last', $current_user->ID );
mp-wp_genesis 999 }
mp-wp_genesis 1000
mp-wp_genesis 1001 /**
mp-wp_genesis 1002 * Creates autosave data for the specified post from $_POST data.
mp-wp_genesis 1003 *
mp-wp_genesis 1004 * @package WordPress
mp-wp_genesis 1005 * @subpackage Post_Revisions
mp-wp_genesis 1006 * @since 2.6.0
mp-wp_genesis 1007 *
mp-wp_genesis 1008 * @uses _wp_translate_postdata()
mp-wp_genesis 1009 * @uses _wp_post_revision_fields()
mp-wp_genesis 1010 */
mp-wp_genesis 1011 function wp_create_post_autosave( $post_id ) {
mp-wp_genesis 1012 $translated = _wp_translate_postdata( true );
mp-wp_genesis 1013 if ( is_wp_error( $translated ) )
mp-wp_genesis 1014 return $translated;
mp-wp_genesis 1015
mp-wp_genesis 1016 // Only store one autosave. If there is already an autosave, overwrite it.
mp-wp_genesis 1017 if ( $old_autosave = wp_get_post_autosave( $post_id ) ) {
mp-wp_genesis 1018 $new_autosave = _wp_post_revision_fields( $_POST, true );
mp-wp_genesis 1019 $new_autosave['ID'] = $old_autosave->ID;
mp-wp_genesis 1020 return wp_update_post( $new_autosave );
mp-wp_genesis 1021 }
mp-wp_genesis 1022
mp-wp_genesis 1023 // Otherwise create the new autosave as a special post revision
mp-wp_genesis 1024 return _wp_put_post_revision( $_POST, true );
mp-wp_genesis 1025 }
mp-wp_genesis 1026
mp-wp_genesis 1027 /**
mp-wp_genesis 1028 * Save draft or manually autosave for showing preview.
mp-wp_genesis 1029 *
mp-wp_genesis 1030 * @package WordPress
mp-wp_genesis 1031 * @since 2.7
mp-wp_genesis 1032 *
mp-wp_genesis 1033 * @uses wp_write_post()
mp-wp_genesis 1034 * @uses edit_post()
mp-wp_genesis 1035 * @uses get_post()
mp-wp_genesis 1036 * @uses current_user_can()
mp-wp_genesis 1037 * @uses wp_create_post_autosave()
mp-wp_genesis 1038 *
mp-wp_genesis 1039 * @return str URL to redirect to show the preview
mp-wp_genesis 1040 */
mp-wp_genesis 1041 function post_preview() {
mp-wp_genesis 1042
mp-wp_genesis 1043 $post_ID = (int) $_POST['post_ID'];
mp-wp_genesis 1044 if ( $post_ID < 1 )
mp-wp_genesis 1045 wp_die( __('Preview not available. Please save as a draft first.') );
mp-wp_genesis 1046
mp-wp_genesis 1047 if ( isset($_POST['catslist']) )
mp-wp_genesis 1048 $_POST['post_category'] = explode(",", $_POST['catslist']);
mp-wp_genesis 1049
mp-wp_genesis 1050 if ( isset($_POST['tags_input']) )
mp-wp_genesis 1051 $_POST['tags_input'] = explode(",", $_POST['tags_input']);
mp-wp_genesis 1052
mp-wp_genesis 1053 if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
mp-wp_genesis 1054 unset($_POST['post_category']);
mp-wp_genesis 1055
mp-wp_genesis 1056 $_POST['ID'] = $post_ID;
mp-wp_genesis 1057 $post = get_post($post_ID);
mp-wp_genesis 1058
mp-wp_genesis 1059 if ( 'page' == $post->post_type ) {
mp-wp_genesis 1060 if ( !current_user_can('edit_page', $post_ID) )
mp-wp_genesis 1061 wp_die(__('You are not allowed to edit this page.'));
mp-wp_genesis 1062 } else {
mp-wp_genesis 1063 if ( !current_user_can('edit_post', $post_ID) )
mp-wp_genesis 1064 wp_die(__('You are not allowed to edit this post.'));
mp-wp_genesis 1065 }
mp-wp_genesis 1066
mp-wp_genesis 1067 if ( 'draft' == $post->post_status ) {
mp-wp_genesis 1068 $id = edit_post();
mp-wp_genesis 1069 } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
mp-wp_genesis 1070 $id = wp_create_post_autosave( $post->ID );
mp-wp_genesis 1071 if ( ! is_wp_error($id) )
mp-wp_genesis 1072 $id = $post->ID;
mp-wp_genesis 1073 }
mp-wp_genesis 1074
mp-wp_genesis 1075 if ( is_wp_error($id) )
mp-wp_genesis 1076 wp_die( $id->get_error_message() );
mp-wp_genesis 1077
mp-wp_genesis 1078 if ( $_POST['post_status'] == 'draft' ) {
mp-wp_genesis 1079 $url = add_query_arg( 'preview', 'true', get_permalink($id) );
mp-wp_genesis 1080 } else {
mp-wp_genesis 1081 $nonce = wp_create_nonce('post_preview_' . $id);
mp-wp_genesis 1082 $url = add_query_arg( array( 'preview' => 'true', 'preview_id' => $id, 'preview_nonce' => $nonce ), get_permalink($id) );
mp-wp_genesis 1083 }
mp-wp_genesis 1084
mp-wp_genesis 1085 return $url;
mp-wp_genesis 1086 }