[ Index ]

WordPress Cross Reference

title

Body

[close]

/wp-admin/includes/ -> post.php (source)

   1  <?php
   2  /**
   3   * WordPress Post Administration API.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Rename $_POST data from form names to DB post columns.
  11   *
  12   * Manipulates $_POST directly.
  13   *
  14   * @package WordPress
  15   * @since 2.6.0
  16   *
  17   * @param bool $update Are we updating a pre-existing 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.
  20   */
  21  function _wp_translate_postdata( $update = false, $post_data = null ) {
  22  
  23      if ( empty($post_data) )
  24          $post_data = &$_POST;
  25  
  26      if ( $update )
  27          $post_data['ID'] = (int) $post_data['post_ID'];
  28  
  29      $ptype = get_post_type_object( $post_data['post_type'] );
  30  
  31      if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) {
  32          if ( 'page' == $post_data['post_type'] )
  33              return new WP_Error( 'edit_others_pages', __( 'You are not allowed to edit pages as this user.' ) );
  34          else
  35              return new WP_Error( 'edit_others_posts', __( 'You are not allowed to edit posts as this user.' ) );
  36      } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) {
  37          if ( 'page' == $post_data['post_type'] )
  38              return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
  39          else
  40              return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) );
  41      }
  42  
  43      if ( isset( $post_data['content'] ) )
  44          $post_data['post_content'] = $post_data['content'];
  45  
  46      if ( isset( $post_data['excerpt'] ) )
  47          $post_data['post_excerpt'] = $post_data['excerpt'];
  48  
  49      if ( isset( $post_data['parent_id'] ) )
  50          $post_data['post_parent'] = (int) $post_data['parent_id'];
  51  
  52      if ( isset($post_data['trackback_url']) )
  53          $post_data['to_ping'] = $post_data['trackback_url'];
  54  
  55      $post_data['user_ID'] = get_current_user_id();
  56  
  57      if (!empty ( $post_data['post_author_override'] ) ) {
  58          $post_data['post_author'] = (int) $post_data['post_author_override'];
  59      } else {
  60          if (!empty ( $post_data['post_author'] ) ) {
  61              $post_data['post_author'] = (int) $post_data['post_author'];
  62          } else {
  63              $post_data['post_author'] = (int) $post_data['user_ID'];
  64          }
  65      }
  66  
  67      if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] )
  68           && ! current_user_can( $ptype->cap->edit_others_posts ) ) {
  69          if ( $update ) {
  70              if ( 'page' == $post_data['post_type'] )
  71                  return new WP_Error( 'edit_others_pages', __( 'You are not allowed to edit pages as this user.' ) );
  72              else
  73                  return new WP_Error( 'edit_others_posts', __( 'You are not allowed to edit posts as this user.' ) );
  74          } else {
  75              if ( 'page' == $post_data['post_type'] )
  76                  return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
  77              else
  78                  return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) );
  79          }
  80      }
  81  
  82      if ( ! empty( $post_data['post_status'] ) )
  83          $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
  84  
  85      // What to do based on which button they pressed
  86      if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
  87          $post_data['post_status'] = 'draft';
  88      if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
  89          $post_data['post_status'] = 'private';
  90      if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) )
  91          $post_data['post_status'] = 'publish';
  92      if ( isset($post_data['advanced']) && '' != $post_data['advanced'] )
  93          $post_data['post_status'] = 'draft';
  94      if ( isset($post_data['pending']) && '' != $post_data['pending'] )
  95          $post_data['post_status'] = 'pending';
  96  
  97      if ( isset( $post_data['ID'] ) )
  98          $post_id = $post_data['ID'];
  99      else
 100          $post_id = false;
 101      $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
 102  
 103      $published_statuses = array( 'publish', 'future' );
 104  
 105      // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
 106      // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
 107      if ( isset($post_data['post_status']) && (in_array( $post_data['post_status'], $published_statuses ) && !current_user_can( $ptype->cap->publish_posts )) )
 108          if ( ! in_array( $previous_status, $published_statuses ) || !current_user_can( 'edit_post', $post_id ) )
 109              $post_data['post_status'] = 'pending';
 110  
 111      if ( ! isset($post_data['post_status']) )
 112          $post_data['post_status'] = $previous_status;
 113  
 114      if (!isset( $post_data['comment_status'] ))
 115          $post_data['comment_status'] = 'closed';
 116  
 117      if (!isset( $post_data['ping_status'] ))
 118          $post_data['ping_status'] = 'closed';
 119  
 120      foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
 121          if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) {
 122              $post_data['edit_date'] = '1';
 123              break;
 124          }
 125      }
 126  
 127      if ( !empty( $post_data['edit_date'] ) ) {
 128          $aa = $post_data['aa'];
 129          $mm = $post_data['mm'];
 130          $jj = $post_data['jj'];
 131          $hh = $post_data['hh'];
 132          $mn = $post_data['mn'];
 133          $ss = $post_data['ss'];
 134          $aa = ($aa <= 0 ) ? date('Y') : $aa;
 135          $mm = ($mm <= 0 ) ? date('n') : $mm;
 136          $jj = ($jj > 31 ) ? 31 : $jj;
 137          $jj = ($jj <= 0 ) ? date('j') : $jj;
 138          $hh = ($hh > 23 ) ? $hh -24 : $hh;
 139          $mn = ($mn > 59 ) ? $mn -60 : $mn;
 140          $ss = ($ss > 59 ) ? $ss -60 : $ss;
 141          $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
 142          $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
 143          if ( !$valid_date ) {
 144              return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
 145          }
 146          $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
 147      }
 148  
 149      return $post_data;
 150  }
 151  
 152  /**
 153   * Update an existing post with values provided in $_POST.
 154   *
 155   * @since 1.5.0
 156   *
 157   * @param array $post_data Optional.
 158   * @return int Post ID.
 159   */
 160  function edit_post( $post_data = null ) {
 161  
 162      if ( empty($post_data) )
 163          $post_data = &$_POST;
 164  
 165      // Clear out any data in internal vars.
 166      unset( $post_data['filter'] );
 167  
 168      $post_ID = (int) $post_data['post_ID'];
 169      $post = get_post( $post_ID );
 170      $post_data['post_type'] = $post->post_type;
 171      $post_data['post_mime_type'] = $post->post_mime_type;
 172  
 173      $ptype = get_post_type_object($post_data['post_type']);
 174      if ( !current_user_can( 'edit_post', $post_ID ) ) {
 175          if ( 'page' == $post_data['post_type'] )
 176              wp_die( __('You are not allowed to edit this page.' ));
 177          else
 178              wp_die( __('You are not allowed to edit this post.' ));
 179      }
 180  
 181      if ( post_type_supports( $ptype->name, 'revisions' ) ) {
 182          $revisions = wp_get_post_revisions( $post_ID, array( 'order' => 'ASC', 'posts_per_page' => 1 ) );
 183          $revision = current( $revisions );
 184  
 185          // Check if the revisions have been upgraded
 186          if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 )
 187              _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) );
 188      }
 189  
 190      $post_data = _wp_translate_postdata( true, $post_data );
 191      if ( is_wp_error($post_data) )
 192          wp_die( $post_data->get_error_message() );
 193      if ( ( empty( $post_data['action'] ) || 'autosave' != $post_data['action'] ) && 'auto-draft' == $post_data['post_status'] ) {
 194          $post_data['post_status'] = 'draft';
 195      }
 196  
 197      if ( isset($post_data['visibility']) ) {
 198          switch ( $post_data['visibility'] ) {
 199              case 'public' :
 200                  $post_data['post_password'] = '';
 201                  break;
 202              case 'password' :
 203                  unset( $post_data['sticky'] );
 204                  break;
 205              case 'private' :
 206                  $post_data['post_status'] = 'private';
 207                  $post_data['post_password'] = '';
 208                  unset( $post_data['sticky'] );
 209                  break;
 210          }
 211      }
 212  
 213      // Post Formats
 214      if ( isset( $post_data['post_format'] ) )
 215          set_post_format( $post_ID, $post_data['post_format'] );
 216  
 217      $format_meta_urls = array( 'url', 'link_url', 'quote_source_url' );
 218      foreach ( $format_meta_urls as $format_meta_url ) {
 219          $keyed = '_format_' . $format_meta_url;
 220          if ( isset( $post_data[ $keyed ] ) )
 221              update_post_meta( $post_ID, $keyed, wp_slash( esc_url_raw( wp_unslash( $post_data[ $keyed ] ) ) ) );
 222      }
 223  
 224      $format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' );
 225  
 226      foreach ( $format_keys as $key ) {
 227          $keyed = '_format_' . $key;
 228          if ( isset( $post_data[ $keyed ] ) ) {
 229              if ( current_user_can( 'unfiltered_html' ) )
 230                  update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] );
 231              else
 232                  update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
 233          }
 234      }
 235  
 236      // Meta Stuff
 237      if ( isset($post_data['meta']) && $post_data['meta'] ) {
 238          foreach ( $post_data['meta'] as $key => $value ) {
 239              if ( !$meta = get_post_meta_by_id( $key ) )
 240                  continue;
 241              if ( $meta->post_id != $post_ID )
 242                  continue;
 243              if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) )
 244                  continue;
 245              update_meta( $key, $value['key'], $value['value'] );
 246          }
 247      }
 248  
 249      if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) {
 250          foreach ( $post_data['deletemeta'] as $key => $value ) {
 251              if ( !$meta = get_post_meta_by_id( $key ) )
 252                  continue;
 253              if ( $meta->post_id != $post_ID )
 254                  continue;
 255              if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) )
 256                  continue;
 257              delete_meta( $key );
 258          }
 259      }
 260  
 261      // Attachment stuff
 262      if ( 'attachment' == $post_data['post_type'] ) {
 263          if ( isset( $post_data[ '_wp_attachment_image_alt' ] ) ) {
 264              $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] );
 265              if ( $image_alt != get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) ) {
 266                  $image_alt = wp_strip_all_tags( $image_alt, true );
 267                  // update_meta expects slashed
 268                  update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
 269              }
 270          }
 271  
 272          $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array();
 273          /** This filter is documented in wp-admin/includes/media.php */
 274          $post_data = apply_filters( 'attachment_fields_to_save', $post_data, $attachment_data );
 275      }
 276  
 277      add_meta( $post_ID );
 278  
 279      update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
 280  
 281      wp_update_post( $post_data );
 282  
 283      // Now that we have an ID we can fix any attachment anchor hrefs
 284      _fix_attachment_links( $post_ID );
 285  
 286      wp_set_post_lock( $post_ID );
 287  
 288      if ( current_user_can( $ptype->cap->edit_others_posts ) ) {
 289          if ( ! empty( $post_data['sticky'] ) )
 290              stick_post( $post_ID );
 291          else
 292              unstick_post( $post_ID );
 293      }
 294  
 295      return $post_ID;
 296  }
 297  
 298  /**
 299   * Process the post data for the bulk editing of posts.
 300   *
 301   * Updates all bulk edited posts/pages, adding (but not removing) tags and
 302   * categories. Skips pages when they would be their own parent or child.
 303   *
 304   * @since 2.7.0
 305   *
 306   * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal.
 307   * @return array
 308   */
 309  function bulk_edit_posts( $post_data = null ) {
 310      global $wpdb;
 311  
 312      if ( empty($post_data) )
 313          $post_data = &$_POST;
 314  
 315      if ( isset($post_data['post_type']) )
 316          $ptype = get_post_type_object($post_data['post_type']);
 317      else
 318          $ptype = get_post_type_object('post');
 319  
 320      if ( !current_user_can( $ptype->cap->edit_posts ) ) {
 321          if ( 'page' == $ptype->name )
 322              wp_die( __('You are not allowed to edit pages.'));
 323          else
 324              wp_die( __('You are not allowed to edit posts.'));
 325      }
 326  
 327      if ( -1 == $post_data['_status'] ) {
 328          $post_data['post_status'] = null;
 329          unset($post_data['post_status']);
 330      } else {
 331          $post_data['post_status'] = $post_data['_status'];
 332      }
 333      unset($post_data['_status']);
 334  
 335      $post_IDs = array_map( 'intval', (array) $post_data['post'] );
 336  
 337      $reset = array(
 338          'post_author', 'post_status', 'post_password',
 339          'post_parent', 'page_template', 'comment_status',
 340          'ping_status', 'keep_private', 'tax_input',
 341          'post_category', 'sticky', 'post_format',
 342      );
 343  
 344      foreach ( $reset as $field ) {
 345          if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) )
 346              unset($post_data[$field]);
 347      }
 348  
 349      if ( isset($post_data['post_category']) ) {
 350          if ( is_array($post_data['post_category']) && ! empty($post_data['post_category']) )
 351              $new_cats = array_map( 'absint', $post_data['post_category'] );
 352          else
 353              unset($post_data['post_category']);
 354      }
 355  
 356      $tax_input = array();
 357      if ( isset($post_data['tax_input'])) {
 358          foreach ( $post_data['tax_input'] as $tax_name => $terms ) {
 359              if ( empty($terms) )
 360                  continue;
 361              if ( is_taxonomy_hierarchical( $tax_name ) ) {
 362                  $tax_input[ $tax_name ] = array_map( 'absint', $terms );
 363              } else {
 364                  $comma = _x( ',', 'tag delimiter' );
 365                  if ( ',' !== $comma )
 366                      $terms = str_replace( $comma, ',', $terms );
 367                  $tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
 368              }
 369          }
 370      }
 371  
 372      if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) {
 373          $pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'");
 374          $children = array();
 375  
 376          for ( $i = 0; $i < 50 && $parent > 0; $i++ ) {
 377              $children[] = $parent;
 378  
 379              foreach ( $pages as $page ) {
 380                  if ( $page->ID == $parent ) {
 381                      $parent = $page->post_parent;
 382                      break;
 383                  }
 384              }
 385          }
 386      }
 387  
 388      $updated = $skipped = $locked = array();
 389      foreach ( $post_IDs as $post_ID ) {
 390          $post_type_object = get_post_type_object( get_post_type( $post_ID ) );
 391  
 392          if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( 'edit_post', $post_ID ) ) {
 393              $skipped[] = $post_ID;
 394              continue;
 395          }
 396  
 397          if ( wp_check_post_lock( $post_ID ) ) {
 398              $locked[] = $post_ID;
 399              continue;
 400          }
 401  
 402          $post = get_post( $post_ID );
 403          $tax_names = get_object_taxonomies( $post );
 404          foreach ( $tax_names as $tax_name ) {
 405              $taxonomy_obj = get_taxonomy($tax_name);
 406              if ( isset( $tax_input[$tax_name]) && current_user_can( $taxonomy_obj->cap->assign_terms ) )
 407                  $new_terms = $tax_input[$tax_name];
 408              else
 409                  $new_terms = array();
 410  
 411              if ( $taxonomy_obj->hierarchical )
 412                  $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'ids') );
 413              else
 414                  $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'names') );
 415  
 416              $post_data['tax_input'][$tax_name] = array_merge( $current_terms, $new_terms );
 417          }
 418  
 419          if ( isset($new_cats) && in_array( 'category', $tax_names ) ) {
 420              $cats = (array) wp_get_post_categories($post_ID);
 421              $post_data['post_category'] = array_unique( array_merge($cats, $new_cats) );
 422              unset( $post_data['tax_input']['category'] );
 423          }
 424  
 425          $post_data['post_mime_type'] = $post->post_mime_type;
 426          $post_data['guid'] = $post->guid;
 427  
 428          $post_data['ID'] = $post_ID;
 429          $updated[] = wp_update_post( $post_data );
 430  
 431          if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
 432              if ( 'sticky' == $post_data['sticky'] )
 433                  stick_post( $post_ID );
 434              else
 435                  unstick_post( $post_ID );
 436          }
 437  
 438          if ( isset( $post_data['post_format'] ) )
 439              set_post_format( $post_ID, $post_data['post_format'] );
 440      }
 441  
 442      return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );
 443  }
 444  
 445  /**
 446   * Default post information to use when populating the "Write Post" form.
 447   *
 448   * @since 2.0.0
 449   *
 450   * @param string $post_type A post type string, defaults to 'post'.
 451   * @return WP_Post Post object containing all the default post data as attributes
 452   */
 453  function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
 454      global $wpdb;
 455  
 456      $post_title = '';
 457      if ( !empty( $_REQUEST['post_title'] ) )
 458          $post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ));
 459  
 460      $post_content = '';
 461      if ( !empty( $_REQUEST['content'] ) )
 462          $post_content = esc_html( wp_unslash( $_REQUEST['content'] ));
 463  
 464      $post_excerpt = '';
 465      if ( !empty( $_REQUEST['excerpt'] ) )
 466          $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ));
 467  
 468      if ( $create_in_db ) {
 469          $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
 470          $post = get_post( $post_id );
 471          if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
 472              set_post_format( $post, get_option( 'default_post_format' ) );
 473      } else {
 474          $post = new stdClass;
 475          $post->ID = 0;
 476          $post->post_author = '';
 477          $post->post_date = '';
 478          $post->post_date_gmt = '';
 479          $post->post_password = '';
 480          $post->post_type = $post_type;
 481          $post->post_status = 'draft';
 482          $post->to_ping = '';
 483          $post->pinged = '';
 484          $post->comment_status = get_option( 'default_comment_status' );
 485          $post->ping_status = get_option( 'default_ping_status' );
 486          $post->post_pingback = get_option( 'default_pingback_flag' );
 487          $post->post_category = get_option( 'default_category' );
 488          $post->page_template = 'default';
 489          $post->post_parent = 0;
 490          $post->menu_order = 0;
 491          $post = new WP_Post( $post );
 492      }
 493  
 494      $post->post_content = apply_filters( 'default_content', $post_content, $post );
 495      $post->post_title   = apply_filters( 'default_title',   $post_title, $post   );
 496      $post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post );
 497      $post->post_name = '';
 498  
 499      return $post;
 500  }
 501  
 502  /**
 503   * Determine if a post exists based on title, content, and date
 504   *
 505   * @since 2.0.0
 506   *
 507   * @param string $title Post title
 508   * @param string $content Optional post content
 509   * @param string $date Optional post date
 510   * @return int Post ID if post exists, 0 otherwise.
 511   */
 512  function post_exists($title, $content = '', $date = '') {
 513      global $wpdb;
 514  
 515      $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
 516      $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
 517      $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
 518  
 519      $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
 520      $args = array();
 521  
 522      if ( !empty ( $date ) ) {
 523          $query .= ' AND post_date = %s';
 524          $args[] = $post_date;
 525      }
 526  
 527      if ( !empty ( $title ) ) {
 528          $query .= ' AND post_title = %s';
 529          $args[] = $post_title;
 530      }
 531  
 532      if ( !empty ( $content ) ) {
 533          $query .= 'AND post_content = %s';
 534          $args[] = $post_content;
 535      }
 536  
 537      if ( !empty ( $args ) )
 538          return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
 539  
 540      return 0;
 541  }
 542  
 543  /**
 544   * Creates a new post from the "Write Post" form using $_POST information.
 545   *
 546   * @since 2.1.0
 547   *
 548   * @return unknown
 549   */
 550  function wp_write_post() {
 551      if ( isset($_POST['post_type']) )
 552          $ptype = get_post_type_object($_POST['post_type']);
 553      else
 554          $ptype = get_post_type_object('post');
 555  
 556      if ( !current_user_can( $ptype->cap->edit_posts ) ) {
 557          if ( 'page' == $ptype->name )
 558              return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) );
 559          else
 560              return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) );
 561      }
 562  
 563      $_POST['post_mime_type'] = '';
 564  
 565      // Clear out any data in internal vars.
 566      unset( $_POST['filter'] );
 567  
 568      // Edit don't write if we have a post id.
 569      if ( isset( $_POST['post_ID'] ) )
 570          return edit_post();
 571  
 572      $translated = _wp_translate_postdata( false );
 573      if ( is_wp_error($translated) )
 574          return $translated;
 575  
 576      if ( isset($_POST['visibility']) ) {
 577          switch ( $_POST['visibility'] ) {
 578              case 'public' :
 579                  $_POST['post_password'] = '';
 580                  break;
 581              case 'password' :
 582                  unset( $_POST['sticky'] );
 583                  break;
 584              case 'private' :
 585                  $_POST['post_status'] = 'private';
 586                  $_POST['post_password'] = '';
 587                  unset( $_POST['sticky'] );
 588                  break;
 589          }
 590      }
 591  
 592      // Create the post.
 593      $post_ID = wp_insert_post( $_POST );
 594      if ( is_wp_error( $post_ID ) )
 595          return $post_ID;
 596  
 597      if ( empty($post_ID) )
 598          return 0;
 599  
 600      add_meta( $post_ID );
 601  
 602      add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
 603  
 604      // Now that we have an ID we can fix any attachment anchor hrefs
 605      _fix_attachment_links( $post_ID );
 606  
 607      wp_set_post_lock( $post_ID );
 608  
 609      return $post_ID;
 610  }
 611  
 612  /**
 613   * Calls wp_write_post() and handles the errors.
 614   *
 615   * @since 2.0.0
 616  
 617   * @uses wp_write_post()
 618   * @uses is_wp_error()
 619   * @uses wp_die()
 620   * @return unknown
 621   */
 622  function write_post() {
 623      $result = wp_write_post();
 624      if ( is_wp_error( $result ) )
 625          wp_die( $result->get_error_message() );
 626      else
 627          return $result;
 628  }
 629  
 630  //
 631  // Post Meta
 632  //
 633  
 634  /**
 635   * {@internal Missing Short Description}}
 636   *
 637   * @since 1.2.0
 638   *
 639   * @param unknown_type $post_ID
 640   * @return unknown
 641   */
 642  function add_meta( $post_ID ) {
 643      global $wpdb;
 644      $post_ID = (int) $post_ID;
 645  
 646      $metakeyselect = isset($_POST['metakeyselect']) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
 647      $metakeyinput = isset($_POST['metakeyinput']) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
 648      $metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : '';
 649      if ( is_string( $metavalue ) )
 650          $metavalue = trim( $metavalue );
 651  
 652      if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) {
 653          // We have a key/value pair. If both the select and the
 654          // input for the key have data, the input takes precedence:
 655  
 656           if ( '#NONE#' != $metakeyselect )
 657              $metakey = $metakeyselect;
 658  
 659          if ( $metakeyinput )
 660              $metakey = $metakeyinput; // default
 661  
 662          if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) )
 663              return false;
 664  
 665          $metakey = wp_slash( $metakey );
 666  
 667          return add_post_meta( $post_ID, $metakey, $metavalue );
 668      }
 669  
 670      return false;
 671  } // add_meta
 672  
 673  /**
 674   * {@internal Missing Short Description}}
 675   *
 676   * @since 1.2.0
 677   *
 678   * @param unknown_type $mid
 679   * @return unknown
 680   */
 681  function delete_meta( $mid ) {
 682      return delete_metadata_by_mid( 'post' , $mid );
 683  }
 684  
 685  /**
 686   * Get a list of previously defined keys.
 687   *
 688   * @since 1.2.0
 689   *
 690   * @return unknown
 691   */
 692  function get_meta_keys() {
 693      global $wpdb;
 694  
 695      $keys = $wpdb->get_col( "
 696              SELECT meta_key
 697              FROM $wpdb->postmeta
 698              GROUP BY meta_key
 699              ORDER BY meta_key" );
 700  
 701      return $keys;
 702  }
 703  
 704  /**
 705   * {@internal Missing Short Description}}
 706   *
 707   * @since 2.1.0
 708   *
 709   * @param unknown_type $mid
 710   * @return unknown
 711   */
 712  function get_post_meta_by_id( $mid ) {
 713      return get_metadata_by_mid( 'post', $mid );
 714  }
 715  
 716  /**
 717   * {@internal Missing Short Description}}
 718   *
 719   * Some postmeta stuff.
 720   *
 721   * @since 1.2.0
 722   *
 723   * @param unknown_type $postid
 724   * @return unknown
 725   */
 726  function has_meta( $postid ) {
 727      global $wpdb;
 728  
 729      return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
 730              FROM $wpdb->postmeta WHERE post_id = %d
 731              ORDER BY meta_key,meta_id", $postid), ARRAY_A );
 732  }
 733  
 734  /**
 735   * {@internal Missing Short Description}}
 736   *
 737   * @since 1.2.0
 738   *
 739   * @param unknown_type $meta_id
 740   * @param unknown_type $meta_key Expect Slashed
 741   * @param unknown_type $meta_value Expect Slashed
 742   * @return unknown
 743   */
 744  function update_meta( $meta_id, $meta_key, $meta_value ) {
 745      $meta_key = wp_unslash( $meta_key );
 746      $meta_value = wp_unslash( $meta_value );
 747  
 748      return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key );
 749  }
 750  
 751  //
 752  // Private
 753  //
 754  
 755  /**
 756   * Replace hrefs of attachment anchors with up-to-date permalinks.
 757   *
 758   * @since 2.3.0
 759   * @access private
 760   *
 761   * @param int|object $post Post ID or post object.
 762   * @return void|int|WP_Error Void if nothing fixed. 0 or WP_Error on update failure. The post ID on update success.
 763   */
 764  function _fix_attachment_links( $post ) {
 765      $post = get_post( $post, ARRAY_A );
 766      $content = $post['post_content'];
 767  
 768      // Don't run if no pretty permalinks or post is not published, scheduled, or privately published.
 769      if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ) ) )
 770          return;
 771  
 772      // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero)
 773      if ( !strpos($content, '?attachment_id=') || !preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) )
 774          return;
 775  
 776      $site_url = get_bloginfo('url');
 777      $site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s)
 778      $replace = '';
 779  
 780      foreach ( $link_matches[1] as $key => $value ) {
 781          if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-')
 782              || !preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
 783              || !preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) )
 784                  continue;
 785  
 786          $quote = $url_match[1]; // the quote (single or double)
 787          $url_id = (int) $url_match[2];
 788          $rel_id = (int) $rel_match[1];
 789  
 790          if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false )
 791              continue;
 792  
 793          $link = $link_matches[0][$key];
 794          $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
 795  
 796          $content = str_replace( $link, $replace, $content );
 797      }
 798  
 799      if ( $replace ) {
 800          $post['post_content'] = $content;
 801          // Escape data pulled from DB.
 802          $post = add_magic_quotes($post);
 803  
 804          return wp_update_post($post);
 805      }
 806  }
 807  
 808  /**
 809   * Move child posts to a new parent.
 810   *
 811   * @since 2.3.0
 812   * @access private
 813   *
 814   * @param unknown_type $old_ID
 815   * @param unknown_type $new_ID
 816   * @return unknown
 817   */
 818  function _relocate_children( $old_ID, $new_ID ) {
 819      global $wpdb;
 820      $old_ID = (int) $old_ID;
 821      $new_ID = (int) $new_ID;
 822  
 823      $children = $wpdb->get_col( $wpdb->prepare("
 824          SELECT post_id
 825          FROM $wpdb->postmeta
 826          WHERE meta_key = '_wp_attachment_temp_parent'
 827          AND meta_value = %d", $old_ID) );
 828  
 829      foreach ( $children as $child_id ) {
 830          $wpdb->update($wpdb->posts, array('post_parent' => $new_ID), array('ID' => $child_id) );
 831          delete_post_meta($child_id, '_wp_attachment_temp_parent');
 832      }
 833  }
 834  
 835  /**
 836   * Get all the possible statuses for a post_type
 837   *
 838   * @since 2.5.0
 839   *
 840   * @param string $type The post_type you want the statuses for
 841   * @return array As array of all the statuses for the supplied post type
 842   */
 843  function get_available_post_statuses($type = 'post') {
 844      $stati = wp_count_posts($type);
 845  
 846      return array_keys(get_object_vars($stati));
 847  }
 848  
 849  /**
 850   * Run the wp query to fetch the posts for listing on the edit posts page
 851   *
 852   * @since 2.5.0
 853   *
 854   * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
 855   * @return array
 856   */
 857  function wp_edit_posts_query( $q = false ) {
 858      if ( false === $q )
 859          $q = $_GET;
 860      $q['m'] = isset($q['m']) ? (int) $q['m'] : 0;
 861      $q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0;
 862      $post_stati  = get_post_stati();
 863  
 864      if ( isset($q['post_type']) && in_array( $q['post_type'], get_post_types() ) )
 865          $post_type = $q['post_type'];
 866      else
 867          $post_type = 'post';
 868  
 869      $avail_post_stati = get_available_post_statuses($post_type);
 870  
 871      if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) {
 872          $post_status = $q['post_status'];
 873          $perm = 'readable';
 874      }
 875  
 876      if ( isset($q['orderby']) )
 877          $orderby = $q['orderby'];
 878      elseif ( isset($q['post_status']) && in_array($q['post_status'], array('pending', 'draft')) )
 879          $orderby = 'modified';
 880  
 881      if ( isset($q['order']) )
 882          $order = $q['order'];
 883      elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] )
 884          $order = 'ASC';
 885  
 886      $per_page = 'edit_' . $post_type . '_per_page';
 887      $posts_per_page = (int) get_user_option( $per_page );
 888      if ( empty( $posts_per_page ) || $posts_per_page < 1 )
 889          $posts_per_page = 20;
 890  
 891      $posts_per_page = apply_filters( $per_page, $posts_per_page );
 892      $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
 893  
 894      $query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page');
 895  
 896      // Hierarchical types require special args.
 897      if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) {
 898          $query['orderby'] = 'menu_order title';
 899          $query['order'] = 'asc';
 900          $query['posts_per_page'] = -1;
 901          $query['posts_per_archive_page'] = -1;
 902      }
 903  
 904      if ( ! empty( $q['show_sticky'] ) )
 905          $query['post__in'] = (array) get_option( 'sticky_posts' );
 906  
 907      wp( $query );
 908  
 909      return $avail_post_stati;
 910  }
 911  
 912  /**
 913   * {@internal Missing Short Description}}
 914   *
 915   * @since 2.5.0
 916   *
 917   * @param unknown_type $type
 918   * @return unknown
 919   */
 920  function get_available_post_mime_types($type = 'attachment') {
 921      global $wpdb;
 922  
 923      $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
 924      return $types;
 925  }
 926  
 927  /**
 928   * Executes a query for attachments. An array of WP_Query arguments
 929   * can be passed in, which will override the arguments set by this function.
 930   *
 931   * @since 2.5.0
 932   * @uses apply_filters() Calls 'upload_per_page' on posts_per_page argument
 933   *
 934   * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
 935   * @return array
 936   */
 937  function wp_edit_attachments_query( $q = false ) {
 938      if ( false === $q )
 939          $q = $_GET;
 940  
 941      $q['m']   = isset( $q['m'] ) ? (int) $q['m'] : 0;
 942      $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
 943      $q['post_type'] = 'attachment';
 944      $post_type = get_post_type_object( 'attachment' );
 945      $states = 'inherit';
 946      if ( current_user_can( $post_type->cap->read_private_posts ) )
 947          $states .= ',private';
 948  
 949      $q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states;
 950      $media_per_page = (int) get_user_option( 'upload_per_page' );
 951      if ( empty( $media_per_page ) || $media_per_page < 1 )
 952          $media_per_page = 20;
 953      $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
 954  
 955      $post_mime_types = get_post_mime_types();
 956      $avail_post_mime_types = get_available_post_mime_types('attachment');
 957  
 958      if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) )
 959          unset($q['post_mime_type']);
 960  
 961      if ( isset($q['detached']) )
 962          add_filter('posts_where', '_edit_attachments_query_helper');
 963  
 964      wp( $q );
 965  
 966      if ( isset($q['detached']) )
 967          remove_filter('posts_where', '_edit_attachments_query_helper');
 968  
 969      return array($post_mime_types, $avail_post_mime_types);
 970  }
 971  
 972  function _edit_attachments_query_helper($where) {
 973      global $wpdb;
 974      return $where .= " AND {$wpdb->posts}.post_parent < 1";
 975  }
 976  
 977  /**
 978   * Returns the list of classes to be used by a metabox
 979   *
 980   * @uses get_user_option()
 981   * @since 2.5.0
 982   *
 983   * @param unknown_type $id
 984   * @param unknown_type $page
 985   * @return unknown
 986   */
 987  function postbox_classes( $id, $page ) {
 988      if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) {
 989          $classes = array( '' );
 990      } elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) {
 991          if ( !is_array( $closed ) ) {
 992              $classes = array( '' );
 993          } else {
 994              $classes = in_array( $id, $closed ) ? array( 'closed' ) : array( '' );
 995          }
 996      } else {
 997          $classes = array( '' );
 998      }
 999  
1000      $classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes );
1001      return implode( ' ', $classes );
1002  }
1003  
1004  /**
1005   * {@internal Missing Short Description}}
1006   *
1007   * @since 2.5.0
1008   *
1009   * @param int|object $id    Post ID or post object.
1010   * @param string $title (optional) Title
1011   * @param string $name (optional) Name
1012   * @return array With two entries of type string
1013   */
1014  function get_sample_permalink($id, $title = null, $name = null) {
1015      $post = get_post( $id );
1016      if ( ! $post )
1017          return array( '', '' );
1018  
1019      $ptype = get_post_type_object($post->post_type);
1020  
1021      $original_status = $post->post_status;
1022      $original_date = $post->post_date;
1023      $original_name = $post->post_name;
1024  
1025      // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
1026      if ( in_array( $post->post_status, array( 'draft', 'pending' ) ) ) {
1027          $post->post_status = 'publish';
1028          $post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
1029      }
1030  
1031      // If the user wants to set a new name -- override the current one
1032      // Note: if empty name is supplied -- use the title instead, see #6072
1033      if ( !is_null($name) )
1034          $post->post_name = sanitize_title($name ? $name : $title, $post->ID);
1035  
1036      $post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
1037  
1038      $post->filter = 'sample';
1039  
1040      $permalink = get_permalink($post, true);
1041  
1042      // Replace custom post_type Token with generic pagename token for ease of use.
1043      $permalink = str_replace("%$post->post_type%", '%pagename%', $permalink);
1044  
1045      // Handle page hierarchy
1046      if ( $ptype->hierarchical ) {
1047          $uri = get_page_uri($post);
1048          $uri = untrailingslashit($uri);
1049          $uri = strrev( stristr( strrev( $uri ), '/' ) );
1050          $uri = untrailingslashit($uri);
1051          $uri = apply_filters( 'editable_slug', $uri );
1052          if ( !empty($uri) )
1053              $uri .= '/';
1054          $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
1055      }
1056  
1057      $permalink = array($permalink, apply_filters('editable_slug', $post->post_name));
1058      $post->post_status = $original_status;
1059      $post->post_date = $original_date;
1060      $post->post_name = $original_name;
1061      unset($post->filter);
1062  
1063      return $permalink;
1064  }
1065  
1066  /**
1067   * Returns the HTML of the sample permalink slug editor.
1068   *
1069   * @since 2.5.0
1070   *
1071   * @param int|object $id Post ID or post object.
1072   * @param string $new_title Optional. New title.
1073   * @param string $new_slug Optional. New slug.
1074   * @return string The HTML of the sample permalink slug editor.
1075   */
1076  function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
1077      $post = get_post( $id );
1078      if ( ! $post )
1079          return '';
1080  
1081      list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
1082  
1083      if ( 'publish' == get_post_status( $post ) ) {
1084          $ptype = get_post_type_object($post->post_type);
1085          $view_post = $ptype->labels->view_item;
1086          $title = __('Click to edit this part of the permalink');
1087      } else {
1088          $title = __('Temporary permalink. Click to edit this part.');
1089      }
1090  
1091      if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) {
1092          $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n";
1093          if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) )
1094              $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
1095          if ( isset( $view_post ) )
1096              $return .= "<span id='view-post-btn'><a href='$permalink' class='button button-small'>$view_post</a></span>\n";
1097  
1098          $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
1099  
1100          return $return;
1101      }
1102  
1103      if ( function_exists('mb_strlen') ) {
1104          if ( mb_strlen($post_name) > 30 ) {
1105              $post_name_abridged = mb_substr($post_name, 0, 14). '&hellip;' . mb_substr($post_name, -14);
1106          } else {
1107              $post_name_abridged = $post_name;
1108          }
1109      } else {
1110          if ( strlen($post_name) > 30 ) {
1111              $post_name_abridged = substr($post_name, 0, 14). '&hellip;' . substr($post_name, -14);
1112          } else {
1113              $post_name_abridged = $post_name;
1114          }
1115      }
1116  
1117      $post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>';
1118      $display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink);
1119      $view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
1120      $return =  '<strong>' . __('Permalink:') . "</strong>\n";
1121      $return .= '<span id="sample-permalink" tabindex="-1">' . $display_link . "</span>\n";
1122      $return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
1123      $return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button button-small hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n";
1124      $return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
1125      if ( isset($view_post) )
1126          $return .= "<span id='view-post-btn'><a href='$view_link' class='button button-small'>$view_post</a></span>\n";
1127  
1128      $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
1129  
1130      return $return;
1131  }
1132  
1133  /**
1134   * Output HTML for the post thumbnail meta-box.
1135   *
1136   * @since 2.9.0
1137   *
1138   * @param int $thumbnail_id ID of the attachment used for thumbnail
1139   * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post.
1140   * @return string html
1141   */
1142  function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
1143      global $content_width, $_wp_additional_image_sizes;
1144  
1145      $post = get_post( $post );
1146  
1147      $upload_iframe_src = esc_url( get_upload_iframe_src('image', $post->ID ) );
1148      $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>';
1149      $content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) );
1150  
1151      if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
1152          $old_content_width = $content_width;
1153          $content_width = 266;
1154          if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
1155              $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
1156          else
1157              $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
1158          if ( !empty( $thumbnail_html ) ) {
1159              $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID );
1160              $content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
1161              $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>';
1162          }
1163          $content_width = $old_content_width;
1164      }
1165  
1166      return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID );
1167  }
1168  
1169  /**
1170   * Check to see if the post is currently being edited by another user.
1171   *
1172   * @since 2.5.0
1173   *
1174   * @param int $post_id ID of the post to check for editing
1175   * @return bool|int False: not locked or locked by current user. Int: user ID of user with lock.
1176   */
1177  function wp_check_post_lock( $post_id ) {
1178      if ( !$post = get_post( $post_id ) )
1179          return false;
1180  
1181      if ( !$lock = get_post_meta( $post->ID, '_edit_lock', true ) )
1182          return false;
1183  
1184      $lock = explode( ':', $lock );
1185      $time = $lock[0];
1186      $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
1187  
1188      $time_window = apply_filters( 'wp_check_post_lock_window', 150 );
1189  
1190      if ( $time && $time > time() - $time_window && $user != get_current_user_id() )
1191          return $user;
1192      return false;
1193  }
1194  
1195  /**
1196   * Mark the post as currently being edited by the current user
1197   *
1198   * @since 2.5.0
1199   *
1200   * @param int $post_id ID of the post to being edited
1201   * @return bool|array Returns false if the post doesn't exist of there is no current user, or
1202   *     an array of the lock time and the user ID.
1203   */
1204  function wp_set_post_lock( $post_id ) {
1205      if ( !$post = get_post( $post_id ) )
1206          return false;
1207      if ( 0 == ($user_id = get_current_user_id()) )
1208          return false;
1209  
1210      $now = time();
1211      $lock = "$now:$user_id";
1212  
1213      update_post_meta( $post->ID, '_edit_lock', $lock );
1214      return array( $now, $user_id );
1215  }
1216  
1217  /**
1218   * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
1219   *
1220   * @since 2.8.5
1221   * @return none
1222   */
1223  function _admin_notice_post_locked() {
1224      if ( ! $post = get_post() )
1225          return;
1226  
1227      $user = null;
1228      if (  $user_id = wp_check_post_lock( $post->ID ) )
1229          $user = get_userdata( $user_id );
1230  
1231      if ( $user ) {
1232          if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) )
1233              return;
1234  
1235          $locked = true;
1236      } else {
1237          $locked = false;
1238      }
1239  
1240      if ( $locked && ( $sendback = wp_get_referer() ) &&
1241          false === strpos( $sendback, 'post.php' ) && false === strpos( $sendback, 'post-new.php' ) ) {
1242  
1243          $sendback_text = __('Go back');
1244      } else {
1245          $sendback = admin_url( 'edit.php' );
1246  
1247          if ( 'post' != $post->post_type )
1248              $sendback = add_query_arg( 'post_type', $post->post_type, $sendback );
1249  
1250          $sendback_text = get_post_type_object( $post->post_type )->labels->all_items;
1251      }
1252  
1253      $hidden = $locked ? '' : ' hidden';
1254  
1255      ?>
1256      <div id="post-lock-dialog" class="notification-dialog-wrap<?php echo $hidden; ?>">
1257      <div class="notification-dialog-background"></div>
1258      <div class="notification-dialog">
1259      <?php
1260  
1261      if ( $locked ) {
1262          if ( get_post_type_object( $post->post_type )->public ) {
1263              $preview_link = set_url_scheme( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) );
1264  
1265              if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) {
1266                  // Latest content is in autosave
1267                  $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
1268                  $preview_link = add_query_arg( array( 'preview_id' => $post->ID, 'preview_nonce' => $nonce ), $preview_link );
1269              }
1270          } else {
1271              $preview_link = '';
1272          }
1273  
1274          $preview_link = apply_filters( 'preview_post_link', $preview_link );
1275          $override = apply_filters( 'override_post_lock', true, $post, $user );
1276          $tab_last = $override ? '' : ' wp-tab-last';
1277  
1278          ?>
1279          <div class="post-locked-message">
1280          <div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div>
1281          <p class="currently-editing wp-tab-first" tabindex="0">
1282          <?php
1283              _e( 'This content is currently locked.' );
1284              if ( $override )
1285                  printf( ' ' . __( 'If you take over, %s will be blocked from continuing to edit.' ), esc_html( $user->display_name ) );
1286          ?>
1287          </p>
1288          <?php do_action( 'post_locked_dialog', $post ); ?>
1289          <p>
1290          <a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a>
1291          <?php if ( $preview_link ) { ?>
1292          <a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e('Preview'); ?></a>
1293          <?php
1294          }
1295  
1296          // Allow plugins to prevent some users overriding the post lock
1297          if ( $override ) {
1298              ?>
1299              <a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'get-post-lock', '1', get_edit_post_link( $post->ID, 'url' ) ) ); ?>"><?php _e('Take over'); ?></a>
1300              <?php
1301          }
1302  
1303          ?>
1304          </p>
1305          </div>
1306          <?php
1307      } else {
1308          ?>
1309          <div class="post-taken-over">
1310              <div class="post-locked-avatar"></div>
1311              <p class="wp-tab-first" tabindex="0">
1312              <span class="currently-editing"></span><br>
1313              <span class="locked-saving hidden"><img src="images/wpspin_light-2x.gif" width="16" height="16" /> <?php _e('Saving revision...'); ?></span>
1314              <span class="locked-saved hidden"><?php _e('Your latest changes were saved as a revision.'); ?></span>
1315              </p>
1316              <?php do_action( 'post_lock_lost_dialog', $post ); ?>
1317              <p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a></p>
1318          </div>
1319          <?php
1320      }
1321  
1322      ?>
1323      </div>
1324      </div>
1325      <?php
1326  }
1327  
1328  /**
1329   * Creates autosave data for the specified post from $_POST data.
1330   *
1331   * @package WordPress
1332   * @subpackage Post_Revisions
1333   * @since 2.6.0
1334   *
1335   * @uses _wp_translate_postdata()
1336   * @uses _wp_post_revision_fields()
1337   *
1338   * @return unknown
1339   */
1340  function wp_create_post_autosave( $post_id ) {
1341      $translated = _wp_translate_postdata( true );
1342      if ( is_wp_error( $translated ) )
1343          return $translated;
1344  
1345      $post_author = get_current_user_id();
1346  
1347      // Store one autosave per author. If there is already an autosave, overwrite it.
1348      if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
1349          $new_autosave = _wp_post_revision_fields( $_POST, true );
1350          $new_autosave['ID'] = $old_autosave->ID;
1351          $new_autosave['post_author'] = $post_author;
1352  
1353          // If the new autosave is the same content as the post, delete the old autosave.
1354          $post = get_post( $post_id );
1355          $autosave_is_different = false;
1356          foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
1357              if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
1358                  $autosave_is_different = true;
1359                  break;
1360              }
1361          }
1362  
1363          if ( ! $autosave_is_different ) {
1364              wp_delete_post_revision( $old_autosave->ID );
1365              return;
1366          }
1367  
1368          return wp_update_post( $new_autosave );
1369      }
1370  
1371      // _wp_put_post_revision() expects unescaped.
1372      $post_data = wp_unslash( $_POST );
1373  
1374      // Otherwise create the new autosave as a special post revision
1375      return _wp_put_post_revision( $post_data, true );
1376  }
1377  
1378  /**
1379   * Save draft or manually autosave for showing preview.
1380   *
1381   * @package WordPress
1382   * @since 2.7.0
1383   *
1384   * @uses get_post_status()
1385   * @uses edit_post()
1386   * @uses get_post()
1387   * @uses current_user_can()
1388   * @uses wp_die()
1389   * @uses wp_create_post_autosave()
1390   * @uses add_query_arg()
1391   * @uses wp_create_nonce()
1392   *
1393   * @return str URL to redirect to show the preview
1394   */
1395  function post_preview() {
1396  
1397      $post_ID = (int) $_POST['post_ID'];
1398      $status = get_post_status( $post_ID );
1399      if ( 'auto-draft' == $status )
1400          wp_die( __('Preview not available. Please save as a draft first.') );
1401  
1402      if ( isset($_POST['catslist']) )
1403          $_POST['post_category'] = explode(",", $_POST['catslist']);
1404  
1405      if ( isset($_POST['tags_input']) )
1406          $_POST['tags_input'] = explode(",", $_POST['tags_input']);
1407  
1408      if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
1409          unset($_POST['post_category']);
1410  
1411      $_POST['ID'] = $post_ID;
1412      $post = get_post($post_ID);
1413  
1414      if ( 'page' == $post->post_type ) {
1415          if ( ! current_user_can('edit_page', $post_ID) )
1416              wp_die( __('You are not allowed to edit this page.') );
1417      } else {
1418          if ( ! current_user_can('edit_post', $post_ID) )
1419              wp_die( __('You are not allowed to edit this post.') );
1420      }
1421  
1422      $user_id = get_current_user_id();
1423      $locked = wp_check_post_lock( $post->ID );
1424      if ( ! $locked && 'draft' == $post->post_status && $user_id == $post->post_author ) {
1425          $id = edit_post();
1426      } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
1427          $id = wp_create_post_autosave( $post->ID );
1428          if ( ! is_wp_error($id) )
1429              $id = $post->ID;
1430      }
1431  
1432      if ( is_wp_error($id) )
1433          wp_die( $id->get_error_message() );
1434  
1435      if ( ! $locked && $_POST['post_status'] == 'draft' && $user_id == $post->post_author ) {
1436          $url = add_query_arg( 'preview', 'true', get_permalink($id) );
1437      } else {
1438          $nonce = wp_create_nonce('post_preview_' . $id);
1439          $args = array(
1440              'preview' => 'true',
1441              'preview_id' => $id,
1442              'preview_nonce' => $nonce,
1443          );
1444  
1445          if ( isset( $_POST['post_format'] ) )
1446              $args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
1447  
1448          $url = add_query_arg( $args, get_permalink($id) );
1449      }
1450  
1451      return apply_filters( 'preview_post_link', $url );
1452  }


Generated: Tue Mar 25 01:41:18 2014 WordPress honlapkészítés: online1.hu