[ XREF Home ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

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      if ( isset( $post_data['content'] ) )
  30          $post_data['post_content'] = $post_data['content'];
  31  
  32      if ( isset( $post_data['excerpt'] ) )
  33          $post_data['post_excerpt'] = $post_data['excerpt'];
  34  
  35      if ( isset( $post_data['parent_id'] ) )
  36          $post_data['post_parent'] = (int) $post_data['parent_id'];
  37  
  38      if ( isset($post_data['trackback_url']) )
  39          $post_data['to_ping'] = $post_data['trackback_url'];
  40  
  41      if ( !isset($post_data['user_ID']) )
  42          $post_data['user_ID'] = $GLOBALS['user_ID'];
  43  
  44      if (!empty ( $post_data['post_author_override'] ) ) {
  45          $post_data['post_author'] = (int) $post_data['post_author_override'];
  46      } else {
  47          if (!empty ( $post_data['post_author'] ) ) {
  48              $post_data['post_author'] = (int) $post_data['post_author'];
  49          } else {
  50              $post_data['post_author'] = (int) $post_data['user_ID'];
  51          }
  52      }
  53  
  54      $ptype = get_post_type_object( $post_data['post_type'] );
  55      if ( isset($post_data['user_ID']) && ($post_data['post_author'] != $post_data['user_ID']) ) {
  56          if ( !current_user_can( $ptype->cap->edit_others_posts ) ) {
  57              if ( 'page' == $post_data['post_type'] ) {
  58                  return new WP_Error( 'edit_others_pages', $update ?
  59                      __( 'You are not allowed to edit pages as this user.' ) :
  60                      __( 'You are not allowed to create pages as this user.' )
  61                  );
  62              } else {
  63                  return new WP_Error( 'edit_others_posts', $update ?
  64                      __( 'You are not allowed to edit posts as this user.' ) :
  65                      __( 'You are not allowed to post as this user.' )
  66                  );
  67              }
  68          }
  69      }
  70  
  71      // What to do based on which button they pressed
  72      if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
  73          $post_data['post_status'] = 'draft';
  74      if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
  75          $post_data['post_status'] = 'private';
  76      if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) )
  77          $post_data['post_status'] = 'publish';
  78      if ( isset($post_data['advanced']) && '' != $post_data['advanced'] )
  79          $post_data['post_status'] = 'draft';
  80      if ( isset($post_data['pending']) && '' != $post_data['pending'] )
  81          $post_data['post_status'] = 'pending';
  82  
  83      if ( isset( $post_data['ID'] ) )
  84          $post_id = $post_data['ID'];
  85      else
  86          $post_id = false;
  87      $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
  88  
  89      // Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
  90      // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
  91      if ( isset($post_data['post_status']) && ('publish' == $post_data['post_status'] && !current_user_can( $ptype->cap->publish_posts )) )
  92          if ( $previous_status != 'publish' || !current_user_can( 'edit_post', $post_id ) )
  93              $post_data['post_status'] = 'pending';
  94  
  95      if ( ! isset($post_data['post_status']) )
  96          $post_data['post_status'] = $previous_status;
  97  
  98      if (!isset( $post_data['comment_status'] ))
  99          $post_data['comment_status'] = 'closed';
 100  
 101      if (!isset( $post_data['ping_status'] ))
 102          $post_data['ping_status'] = 'closed';
 103  
 104      foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
 105          if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) {
 106              $post_data['edit_date'] = '1';
 107              break;
 108          }
 109      }
 110  
 111      if ( !empty( $post_data['edit_date'] ) ) {
 112          $aa = $post_data['aa'];
 113          $mm = $post_data['mm'];
 114          $jj = $post_data['jj'];
 115          $hh = $post_data['hh'];
 116          $mn = $post_data['mn'];
 117          $ss = $post_data['ss'];
 118          $aa = ($aa <= 0 ) ? date('Y') : $aa;
 119          $mm = ($mm <= 0 ) ? date('n') : $mm;
 120          $jj = ($jj > 31 ) ? 31 : $jj;
 121          $jj = ($jj <= 0 ) ? date('j') : $jj;
 122          $hh = ($hh > 23 ) ? $hh -24 : $hh;
 123          $mn = ($mn > 59 ) ? $mn -60 : $mn;
 124          $ss = ($ss > 59 ) ? $ss -60 : $ss;
 125          $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
 126          $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
 127      }
 128  
 129      return $post_data;
 130  }
 131  
 132  /**
 133   * Update an existing post with values provided in $_POST.
 134   *
 135   * @since 1.5.0
 136   *
 137   * @param array $post_data Optional.
 138   * @return int Post ID.
 139   */
 140  function edit_post( $post_data = null ) {
 141  
 142      if ( empty($post_data) )
 143          $post_data = &$_POST;
 144  
 145      $post_ID = (int) $post_data['post_ID'];
 146      $post = get_post( $post_ID );
 147      $post_data['post_type'] = $post->post_type;
 148      $post_data['post_mime_type'] = $post->post_mime_type;
 149  
 150      $ptype = get_post_type_object($post_data['post_type']);
 151      if ( !current_user_can( $ptype->cap->edit_post, $post_ID ) ) {
 152          if ( 'page' == $post_data['post_type'] )
 153              wp_die( __('You are not allowed to edit this page.' ));
 154          else
 155              wp_die( __('You are not allowed to edit this post.' ));
 156      }
 157  
 158      // Autosave shouldn't save too soon after a real save
 159      if ( 'autosave' == $post_data['action'] ) {
 160          $post =& get_post( $post_ID );
 161          $now = time();
 162          $then = strtotime($post->post_date_gmt . ' +0000');
 163          $delta = AUTOSAVE_INTERVAL / 2;
 164          if ( ($now - $then) < $delta )
 165              return $post_ID;
 166      }
 167  
 168      $post_data = _wp_translate_postdata( true, $post_data );
 169      if ( is_wp_error($post_data) )
 170          wp_die( $post_data->get_error_message() );
 171      if ( 'autosave' != $post_data['action']  && 'auto-draft' == $post_data['post_status'] )
 172          $post_data['post_status'] = 'draft';
 173  
 174      if ( isset($post_data['visibility']) ) {
 175          switch ( $post_data['visibility'] ) {
 176              case 'public' :
 177                  $post_data['post_password'] = '';
 178                  break;
 179              case 'password' :
 180                  unset( $post_data['sticky'] );
 181                  break;
 182              case 'private' :
 183                  $post_data['post_status'] = 'private';
 184                  $post_data['post_password'] = '';
 185                  unset( $post_data['sticky'] );
 186                  break;
 187          }
 188      }
 189  
 190      // Post Formats
 191      if ( current_theme_supports( 'post-formats' ) && isset( $post_data['post_format'] ) ) {
 192          $formats = get_theme_support( 'post-formats' );
 193          if ( is_array( $formats ) ) {
 194              $formats = $formats[0];
 195              if ( in_array( $post_data['post_format'], $formats ) ) {
 196                  set_post_format( $post_ID, $post_data['post_format'] );
 197              } elseif ( '0' == $post_data['post_format'] ) {
 198                  set_post_format( $post_ID, false );
 199              }
 200          }
 201      }
 202  
 203      // Meta Stuff
 204      if ( isset($post_data['meta']) && $post_data['meta'] ) {
 205          foreach ( $post_data['meta'] as $key => $value ) {
 206              if ( !$meta = get_post_meta_by_id( $key ) )
 207                  continue;
 208              if ( $meta->post_id != $post_ID )
 209                  continue;
 210              if ( is_protected_meta( $value['key'] ) )
 211                  continue;
 212              update_meta( $key, $value['key'], $value['value'] );
 213          }
 214      }
 215  
 216      if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) {
 217          foreach ( $post_data['deletemeta'] as $key => $value ) {
 218              if ( !$meta = get_post_meta_by_id( $key ) )
 219                  continue;
 220              if ( $meta->post_id != $post_ID )
 221                  continue;
 222              if ( is_protected_meta( $meta->meta_key ) )
 223                  continue;
 224              delete_meta( $key );
 225          }
 226      }
 227  
 228      add_meta( $post_ID );
 229  
 230      update_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
 231  
 232      wp_update_post( $post_data );
 233  
 234      // Reunite any orphaned attachments with their parent
 235      if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
 236          $draft_ids = array();
 237      if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )
 238          _relocate_children( $draft_temp_id, $post_ID );
 239  
 240      // Now that we have an ID we can fix any attachment anchor hrefs
 241      _fix_attachment_links( $post_ID );
 242  
 243      wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );
 244  
 245      if ( current_user_can( $ptype->cap->edit_others_posts ) ) {
 246          if ( ! empty( $post_data['sticky'] ) )
 247              stick_post( $post_ID );
 248          else
 249              unstick_post( $post_ID );
 250      }
 251  
 252      return $post_ID;
 253  }
 254  
 255  /**
 256   * Process the post data for the bulk editing of posts.
 257   *
 258   * Updates all bulk edited posts/pages, adding (but not removing) tags and
 259   * categories. Skips pages when they would be their own parent or child.
 260   *
 261   * @since 2.7.0
 262   *
 263   * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal.
 264   * @return array
 265   */
 266  function bulk_edit_posts( $post_data = null ) {
 267      global $wpdb;
 268  
 269      if ( empty($post_data) )
 270          $post_data = &$_POST;
 271  
 272      if ( isset($post_data['post_type']) )
 273          $ptype = get_post_type_object($post_data['post_type']);
 274      else
 275          $ptype = get_post_type_object('post');
 276  
 277      if ( !current_user_can( $ptype->cap->edit_posts ) ) {
 278          if ( 'page' == $ptype->name )
 279              wp_die( __('You are not allowed to edit pages.'));
 280          else
 281              wp_die( __('You are not allowed to edit posts.'));
 282      }
 283  
 284      if ( -1 == $post_data['_status'] ) {
 285          $post_data['post_status'] = null;
 286          unset($post_data['post_status']);
 287      } else {
 288          $post_data['post_status'] = $post_data['_status'];
 289      }
 290      unset($post_data['_status']);
 291  
 292      $post_IDs = array_map( 'intval', (array) $post_data['post'] );
 293  
 294      $reset = array( 'post_author', 'post_status', 'post_password', 'post_parent', 'page_template', 'comment_status', 'ping_status', 'keep_private', 'tax_input', 'post_category', 'sticky' );
 295      foreach ( $reset as $field ) {
 296          if ( isset($post_data[$field]) && ( '' == $post_data[$field] || -1 == $post_data[$field] ) )
 297              unset($post_data[$field]);
 298      }
 299  
 300      if ( isset($post_data['post_category']) ) {
 301          if ( is_array($post_data['post_category']) && ! empty($post_data['post_category']) )
 302              $new_cats = array_map( 'absint', $post_data['post_category'] );
 303          else
 304              unset($post_data['post_category']);
 305      }
 306  
 307      $tax_input = array();
 308      if ( isset($post_data['tax_input'])) {
 309          foreach ( $post_data['tax_input'] as $tax_name => $terms ) {
 310              if ( empty($terms) )
 311                  continue;
 312              if ( is_taxonomy_hierarchical( $tax_name ) )
 313                  $tax_input[$tax_name] = array_map( 'absint', $terms );
 314              else {
 315                  $tax_input[$tax_name] = preg_replace( '/\s*,\s*/', ',', rtrim( trim($terms), ' ,' ) );
 316                  $tax_input[$tax_name] = explode(',', $tax_input[$tax_name]);
 317              }
 318          }
 319      }
 320  
 321      if ( isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent']) ) {
 322          $pages = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'");
 323          $children = array();
 324  
 325          for ( $i = 0; $i < 50 && $parent > 0; $i++ ) {
 326              $children[] = $parent;
 327  
 328              foreach ( $pages as $page ) {
 329                  if ( $page->ID == $parent ) {
 330                      $parent = $page->post_parent;
 331                      break;
 332                  }
 333              }
 334          }
 335      }
 336  
 337      $updated = $skipped = $locked = array();
 338      foreach ( $post_IDs as $post_ID ) {
 339          $post_type_object = get_post_type_object( get_post_type( $post_ID ) );
 340  
 341          if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( $post_type_object->cap->edit_post, $post_ID ) ) {
 342              $skipped[] = $post_ID;
 343              continue;
 344          }
 345  
 346          if ( wp_check_post_lock( $post_ID ) ) {
 347              $locked[] = $post_ID;
 348              continue;
 349          }
 350  
 351          $tax_names = get_object_taxonomies( get_post($post_ID) );
 352          foreach ( $tax_names as $tax_name ) {
 353              $taxonomy_obj = get_taxonomy($tax_name);
 354              if (  isset( $tax_input[$tax_name]) && current_user_can( $taxonomy_obj->cap->assign_terms ) )
 355                  $new_terms = $tax_input[$tax_name];
 356              else
 357                  $new_terms = array();
 358  
 359              if ( $taxonomy_obj->hierarchical )
 360                  $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'ids') );
 361              else
 362                  $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array('fields' => 'names') );
 363  
 364              $post_data['tax_input'][$tax_name] = array_merge( $current_terms, $new_terms );
 365          }
 366  
 367          if ( isset($new_cats) && in_array( 'category', $tax_names ) ) {
 368              $cats = (array) wp_get_post_categories($post_ID);
 369              $post_data['post_category'] = array_unique( array_merge($cats, $new_cats) );
 370              unset( $post_data['tax_input']['category'] );
 371          }
 372  
 373          $post_data['ID'] = $post_ID;
 374          $updated[] = wp_update_post( $post_data );
 375  
 376          if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
 377              if ( 'sticky' == $post_data['sticky'] )
 378                  stick_post( $post_ID );
 379              else
 380                  unstick_post( $post_ID );
 381          }
 382  
 383      }
 384  
 385      return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );
 386  }
 387  
 388  /**
 389   * Default post information to use when populating the "Write Post" form.
 390   *
 391   * @since 2.0.0
 392   *
 393   * @param string $post_type A post type string, defaults to 'post'.
 394   * @return object stdClass object containing all the default post data as attributes
 395   */
 396  function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
 397      global $wpdb;
 398  
 399      $post_title = '';
 400      if ( !empty( $_REQUEST['post_title'] ) )
 401          $post_title = esc_html( stripslashes( $_REQUEST['post_title'] ));
 402  
 403      $post_content = '';
 404      if ( !empty( $_REQUEST['content'] ) )
 405          $post_content = esc_html( stripslashes( $_REQUEST['content'] ));
 406  
 407      $post_excerpt = '';
 408      if ( !empty( $_REQUEST['excerpt'] ) )
 409          $post_excerpt = esc_html( stripslashes( $_REQUEST['excerpt'] ));
 410  
 411      if ( $create_in_db ) {
 412          // Cleanup old auto-drafts more than 7 days old
 413          $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" );
 414          foreach ( (array) $old_posts as $delete )
 415              wp_delete_post( $delete, true ); // Force delete
 416          $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
 417          $post = get_post( $post_id );
 418          if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
 419              set_post_format( $post, get_option( 'default_post_format' ) );
 420      } else {
 421          $post->ID = 0;
 422          $post->post_author = '';
 423          $post->post_date = '';
 424          $post->post_date_gmt = '';
 425          $post->post_password = '';
 426          $post->post_type = $post_type;
 427          $post->post_status = 'draft';
 428          $post->to_ping = '';
 429          $post->pinged = '';
 430          $post->comment_status = get_option( 'default_comment_status' );
 431          $post->ping_status = get_option( 'default_ping_status' );
 432          $post->post_pingback = get_option( 'default_pingback_flag' );
 433          $post->post_category = get_option( 'default_category' );
 434          $post->page_template = 'default';
 435          $post->post_parent = 0;
 436          $post->menu_order = 0;
 437      }
 438  
 439      $post->post_content = apply_filters( 'default_content', $post_content, $post );
 440      $post->post_title   = apply_filters( 'default_title',   $post_title, $post   );
 441      $post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post );
 442      $post->post_name = '';
 443  
 444      return $post;
 445  }
 446  
 447  /**
 448   * Get the default page information to use.
 449   *
 450   * @since 2.5.0
 451   *
 452   * @return object stdClass object containing all the default post data as attributes
 453   */
 454  function get_default_page_to_edit() {
 455      $page = get_default_post_to_edit();
 456      $page->post_type = 'page';
 457      return $page;
 458  }
 459  
 460  /**
 461   * Get an existing post and format it for editing.
 462   *
 463   * @since 2.0.0
 464   *
 465   * @param unknown_type $id
 466   * @return unknown
 467   */
 468  function get_post_to_edit( $id ) {
 469  
 470      $post = get_post( $id, OBJECT, 'edit' );
 471  
 472      if ( $post->post_type == 'page' )
 473          $post->page_template = get_post_meta( $id, '_wp_page_template', true );
 474  
 475      return $post;
 476  }
 477  
 478  /**
 479   * Determine if a post exists based on title, content, and date
 480   *
 481   * @since 2.0.0
 482   *
 483   * @param string $title Post title
 484   * @param string $content Optional post content
 485   * @param string $date Optional post date
 486   * @return int Post ID if post exists, 0 otherwise.
 487   */
 488  function post_exists($title, $content = '', $date = '') {
 489      global $wpdb;
 490  
 491      $post_title = stripslashes( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
 492      $post_content = stripslashes( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
 493      $post_date = stripslashes( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
 494  
 495      $query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
 496      $args = array();
 497  
 498      if ( !empty ( $date ) ) {
 499          $query .= ' AND post_date = %s';
 500          $args[] = $post_date;
 501      }
 502  
 503      if ( !empty ( $title ) ) {
 504          $query .= ' AND post_title = %s';
 505          $args[] = $post_title;
 506      }
 507  
 508      if ( !empty ( $content ) ) {
 509          $query .= 'AND post_content = %s';
 510          $args[] = $post_content;
 511      }
 512  
 513      if ( !empty ( $args ) )
 514          return $wpdb->get_var( $wpdb->prepare($query, $args) );
 515  
 516      return 0;
 517  }
 518  
 519  /**
 520   * Creates a new post from the "Write Post" form using $_POST information.
 521   *
 522   * @since 2.1.0
 523   *
 524   * @return unknown
 525   */
 526  function wp_write_post() {
 527      global $user_ID;
 528  
 529  
 530      if ( isset($_POST['post_type']) )
 531          $ptype = get_post_type_object($_POST['post_type']);
 532      else
 533          $ptype = get_post_type_object('post');
 534  
 535      if ( !current_user_can( $ptype->cap->edit_posts ) ) {
 536          if ( 'page' == $ptype->name )
 537              return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) );
 538          else
 539              return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) );
 540      }
 541  
 542      $_POST['post_mime_type'] = '';
 543  
 544      // Check for autosave collisions
 545      // Does this need to be updated? ~ Mark
 546      $temp_id = false;
 547      if ( isset($_POST['temp_ID']) ) {
 548          $temp_id = (int) $_POST['temp_ID'];
 549          if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
 550              $draft_ids = array();
 551          foreach ( $draft_ids as $temp => $real )
 552              if ( time() + $temp > 86400 ) // 1 day: $temp is equal to -1 * time( then )
 553                  unset($draft_ids[$temp]);
 554  
 555          if ( isset($draft_ids[$temp_id]) ) { // Edit, don't write
 556              $_POST['post_ID'] = $draft_ids[$temp_id];
 557              unset($_POST['temp_ID']);
 558              update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
 559              return edit_post();
 560          }
 561      }
 562  
 563      $translated = _wp_translate_postdata( false );
 564      if ( is_wp_error($translated) )
 565          return $translated;
 566  
 567      if ( isset($_POST['visibility']) ) {
 568          switch ( $_POST['visibility'] ) {
 569              case 'public' :
 570                  $_POST['post_password'] = '';
 571                  break;
 572              case 'password' :
 573                  unset( $_POST['sticky'] );
 574                  break;
 575              case 'private' :
 576                  $_POST['post_status'] = 'private';
 577                  $_POST['post_password'] = '';
 578                  unset( $_POST['sticky'] );
 579                  break;
 580          }
 581      }
 582  
 583      // Create the post.
 584      $post_ID = wp_insert_post( $_POST );
 585      if ( is_wp_error( $post_ID ) )
 586          return $post_ID;
 587  
 588      if ( empty($post_ID) )
 589          return 0;
 590  
 591      add_meta( $post_ID );
 592  
 593      add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
 594  
 595      // Reunite any orphaned attachments with their parent
 596      // Does this need to be udpated? ~ Mark
 597      if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
 598          $draft_ids = array();
 599      if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )
 600          _relocate_children( $draft_temp_id, $post_ID );
 601      if ( $temp_id && $temp_id != $draft_temp_id )
 602          _relocate_children( $temp_id, $post_ID );
 603  
 604      // Update autosave collision detection
 605      if ( $temp_id ) {
 606          $draft_ids[$temp_id] = $post_ID;
 607          update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
 608      }
 609  
 610      // Now that we have an ID we can fix any attachment anchor hrefs
 611      _fix_attachment_links( $post_ID );
 612  
 613      wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );
 614  
 615      return $post_ID;
 616  }
 617  
 618  /**
 619   * Calls wp_write_post() and handles the errors.
 620   *
 621   * @since 2.0.0
 622   *
 623   * @return unknown
 624   */
 625  function write_post() {
 626      $result = wp_write_post();
 627      if ( is_wp_error( $result ) )
 628          wp_die( $result->get_error_message() );
 629      else
 630          return $result;
 631  }
 632  
 633  //
 634  // Post Meta
 635  //
 636  
 637  /**
 638   * {@internal Missing Short Description}}
 639   *
 640   * @since 1.2.0
 641   *
 642   * @param unknown_type $post_ID
 643   * @return unknown
 644   */
 645  function add_meta( $post_ID ) {
 646      global $wpdb;
 647      $post_ID = (int) $post_ID;
 648  
 649      $metakeyselect = isset($_POST['metakeyselect']) ? stripslashes( trim( $_POST['metakeyselect'] ) ) : '';
 650      $metakeyinput = isset($_POST['metakeyinput']) ? stripslashes( trim( $_POST['metakeyinput'] ) ) : '';
 651      $metavalue = isset($_POST['metavalue']) ? maybe_serialize( stripslashes_deep( $_POST['metavalue'] ) ) : '';
 652      if ( is_string($metavalue) )
 653          $metavalue = trim( $metavalue );
 654  
 655      if ( ('0' === $metavalue || !empty ( $metavalue ) ) && ((('#NONE#' != $metakeyselect) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput) ) ) {
 656          // We have a key/value pair. If both the select and the
 657          // input for the key have data, the input takes precedence:
 658  
 659           if ('#NONE#' != $metakeyselect)
 660              $metakey = $metakeyselect;
 661  
 662          if ( $metakeyinput)
 663              $metakey = $metakeyinput; // default
 664  
 665          if ( is_protected_meta( $metakey ) )
 666              return false;
 667  
 668          wp_cache_delete($post_ID, 'post_meta');
 669          $wpdb->insert( $wpdb->postmeta, array( 'post_id' => $post_ID, 'meta_key' => $metakey, 'meta_value' => $metavalue ) );
 670          $meta_id = $wpdb->insert_id;
 671          do_action( 'added_postmeta', $meta_id, $post_ID, $metakey, $metavalue );
 672  
 673          return $meta_id;
 674      }
 675      return false;
 676  } // add_meta
 677  
 678  /**
 679   * {@internal Missing Short Description}}
 680   *
 681   * @since 1.2.0
 682   *
 683   * @param unknown_type $mid
 684   * @return unknown
 685   */
 686  function delete_meta( $mid ) {
 687      global $wpdb;
 688      $mid = (int) $mid;
 689  
 690      $post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
 691  
 692      do_action( 'delete_postmeta', $mid );
 693      wp_cache_delete($post_id, 'post_meta');
 694      $rval = $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
 695      do_action( 'deleted_postmeta', $mid );
 696  
 697      return $rval;
 698  }
 699  
 700  /**
 701   * Get a list of previously defined keys.
 702   *
 703   * @since 1.2.0
 704   *
 705   * @return unknown
 706   */
 707  function get_meta_keys() {
 708      global $wpdb;
 709  
 710      $keys = $wpdb->get_col( "
 711              SELECT meta_key
 712              FROM $wpdb->postmeta
 713              GROUP BY meta_key
 714              ORDER BY meta_key" );
 715  
 716      return $keys;
 717  }
 718  
 719  /**
 720   * {@internal Missing Short Description}}
 721   *
 722   * @since 2.1.0
 723   *
 724   * @param unknown_type $mid
 725   * @return unknown
 726   */
 727  function get_post_meta_by_id( $mid ) {
 728      global $wpdb;
 729      $mid = (int) $mid;
 730  
 731      $meta = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
 732      if ( empty($meta) )
 733          return false;
 734      if ( is_serialized_string( $meta->meta_value ) )
 735          $meta->meta_value = maybe_unserialize( $meta->meta_value );
 736      return $meta;
 737  }
 738  
 739  /**
 740   * {@internal Missing Short Description}}
 741   *
 742   * Some postmeta stuff.
 743   *
 744   * @since 1.2.0
 745   *
 746   * @param unknown_type $postid
 747   * @return unknown
 748   */
 749  function has_meta( $postid ) {
 750      global $wpdb;
 751  
 752      return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
 753              FROM $wpdb->postmeta WHERE post_id = %d
 754              ORDER BY meta_key,meta_id", $postid), ARRAY_A );
 755  
 756  }
 757  
 758  /**
 759   * {@internal Missing Short Description}}
 760   *
 761   * @since 1.2.0
 762   *
 763   * @param unknown_type $meta_id
 764   * @param unknown_type $meta_key Expect Slashed
 765   * @param unknown_type $meta_value Expect Slashed
 766   * @return unknown
 767   */
 768  function update_meta( $meta_id, $meta_key, $meta_value ) {
 769      global $wpdb;
 770  
 771      $meta_key = stripslashes($meta_key);
 772  
 773      if ( is_protected_meta( $meta_key ) )
 774          return false;
 775  
 776      if ( '' === trim( $meta_value ) )
 777          return false;
 778  
 779      $post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $meta_id) );
 780  
 781      $meta_value = maybe_serialize( stripslashes_deep( $meta_value ) );
 782      $meta_id = (int) $meta_id;
 783  
 784      $data  = compact( 'meta_key', 'meta_value' );
 785      $where = compact( 'meta_id' );
 786  
 787      do_action( 'update_postmeta', $meta_id, $post_id, $meta_key, $meta_value );
 788      $rval = $wpdb->update( $wpdb->postmeta, $data, $where );
 789      wp_cache_delete($post_id, 'post_meta');
 790      do_action( 'updated_postmeta', $meta_id, $post_id, $meta_key, $meta_value );
 791  
 792      return $rval;
 793  }
 794  
 795  //
 796  // Private
 797  //
 798  
 799  /**
 800   * Replace hrefs of attachment anchors with up-to-date permalinks.
 801   *
 802   * @since 2.3.0
 803   * @access private
 804   *
 805   * @param unknown_type $post_ID
 806   * @return unknown
 807   */
 808  function _fix_attachment_links( $post_ID ) {
 809      global $_fix_attachment_link_id;
 810  
 811      $post = & get_post( $post_ID, ARRAY_A );
 812  
 813      $search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie";
 814  
 815      // See if we have any rel="attachment" links
 816      if ( 0 == preg_match_all( $search, $post['post_content'], $anchor_matches, PREG_PATTERN_ORDER ) )
 817          return;
 818  
 819      $i = 0;
 820      $search = "#[\s]+rel=(\"|')(.*?)wp-att-(\d+)\\1#i";
 821      foreach ( $anchor_matches[0] as $anchor ) {
 822          if ( 0 == preg_match( $search, $anchor, $id_matches ) )
 823              continue;
 824  
 825          $id = (int) $id_matches[3];
 826  
 827          // While we have the attachment ID, let's adopt any orphans.
 828          $attachment = & get_post( $id, ARRAY_A );
 829          if ( ! empty( $attachment) && ! is_object( get_post( $attachment['post_parent'] ) ) ) {
 830              $attachment['post_parent'] = $post_ID;
 831              // Escape data pulled from DB.
 832              $attachment = add_magic_quotes( $attachment );
 833              wp_update_post( $attachment );
 834          }
 835  
 836          $post_search[$i] = $anchor;
 837           $_fix_attachment_link_id = $id;
 838          $post_replace[$i] = preg_replace_callback( "#href=(\"|')[^'\"]*\\1#", '_fix_attachment_links_replace_cb', $anchor );
 839          ++$i;
 840      }
 841  
 842      $post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] );
 843  
 844      // Escape data pulled from DB.
 845      $post = add_magic_quotes( $post);
 846  
 847      return wp_update_post( $post);
 848  }
 849  
 850  function _fix_attachment_links_replace_cb($match) {
 851          global $_fix_attachment_link_id;
 852          return stripslashes( 'href='.$match[1] ).get_attachment_link( $_fix_attachment_link_id ).stripslashes( $match[1] );
 853  }
 854  
 855  /**
 856   * Move child posts to a new parent.
 857   *
 858   * @since 2.3.0
 859   * @access private
 860   *
 861   * @param unknown_type $old_ID
 862   * @param unknown_type $new_ID
 863   * @return unknown
 864   */
 865  function _relocate_children( $old_ID, $new_ID ) {
 866      global $wpdb;
 867      $old_ID = (int) $old_ID;
 868      $new_ID = (int) $new_ID;
 869  
 870      $children = $wpdb->get_col( $wpdb->prepare("
 871          SELECT post_id
 872          FROM $wpdb->postmeta
 873          WHERE meta_key = '_wp_attachment_temp_parent'
 874          AND meta_value = %d", $old_ID) );
 875  
 876      foreach ( $children as $child_id ) {
 877          $wpdb->update($wpdb->posts, array('post_parent' => $new_ID), array('ID' => $child_id) );
 878          delete_post_meta($child_id, '_wp_attachment_temp_parent');
 879      }
 880  }
 881  
 882  /**
 883   * Get all the possible statuses for a post_type
 884   *
 885   * @since 2.5.0
 886   *
 887   * @param string $type The post_type you want the statuses for
 888   * @return array As array of all the statuses for the supplied post type
 889   */
 890  function get_available_post_statuses($type = 'post') {
 891      $stati = wp_count_posts($type);
 892  
 893      return array_keys(get_object_vars($stati));
 894  }
 895  
 896  /**
 897   * Run the wp query to fetch the posts for listing on the edit posts page
 898   *
 899   * @since 2.5.0
 900   *
 901   * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
 902   * @return array
 903   */
 904  function wp_edit_posts_query( $q = false ) {
 905      if ( false === $q )
 906          $q = $_GET;
 907      $q['m'] = isset($q['m']) ? (int) $q['m'] : 0;
 908      $q['cat'] = isset($q['cat']) ? (int) $q['cat'] : 0;
 909      $post_stati  = get_post_stati();
 910  
 911      if ( isset($q['post_type']) && in_array( $q['post_type'], get_post_types() ) )
 912          $post_type = $q['post_type'];
 913      else
 914          $post_type = 'post';
 915  
 916      $avail_post_stati = get_available_post_statuses($post_type);
 917  
 918      if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) {
 919          $post_status = $q['post_status'];
 920          $perm = 'readable';
 921      }
 922  
 923      if ( isset($q['orderby']) )
 924          $orderby = $q['orderby'];
 925      elseif ( isset($q['post_status']) && in_array($q['post_status'], array('pending', 'draft')) )
 926          $orderby = 'modified';
 927  
 928      if ( isset($q['order']) )
 929          $order = $q['order'];
 930      elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] )
 931          $order = 'ASC';
 932  
 933      $per_page = 'edit_' . $post_type . '_per_page';
 934      $posts_per_page = (int) get_user_option( $per_page );
 935      if ( empty( $posts_per_page ) || $posts_per_page < 1 )
 936          $posts_per_page = 20;
 937  
 938      $posts_per_page = apply_filters( $per_page, $posts_per_page );
 939      $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
 940  
 941      $query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page');
 942  
 943      // Hierarchical types require special args.
 944      if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) {
 945          $query['orderby'] = 'menu_order title';
 946          $query['order'] = 'asc';
 947          $query['posts_per_page'] = -1;
 948          $query['posts_per_archive_page'] = -1;
 949      }
 950  
 951      if ( ! empty( $q['show_sticky'] ) )
 952          $query['post__in'] = (array) get_option( 'sticky_posts' );
 953  
 954      wp( $query );
 955  
 956      return $avail_post_stati;
 957  }
 958  
 959  /**
 960   * Get default post mime types
 961   *
 962   * @since 2.9.0
 963   *
 964   * @return array
 965   */
 966  function get_post_mime_types() {
 967      $post_mime_types = array(    //    array( adj, noun )
 968          'image' => array(__('Images'), __('Manage Images'), _n_noop('Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>')),
 969          'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>')),
 970          'video' => array(__('Video'), __('Manage Video'), _n_noop('Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>')),
 971      );
 972  
 973      return apply_filters('post_mime_types', $post_mime_types);
 974  }
 975  
 976  /**
 977   * {@internal Missing Short Description}}
 978   *
 979   * @since 2.5.0
 980   *
 981   * @param unknown_type $type
 982   * @return unknown
 983   */
 984  function get_available_post_mime_types($type = 'attachment') {
 985      global $wpdb;
 986  
 987      $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
 988      return $types;
 989  }
 990  
 991  /**
 992   * {@internal Missing Short Description}}
 993   *
 994   * @since 2.5.0
 995   *
 996   * @param unknown_type $q
 997   * @return unknown
 998   */
 999  function wp_edit_attachments_query( $q = false ) {
1000      if ( false === $q )
1001          $q = $_GET;
1002  
1003      $q['m']   = isset( $q['m'] ) ? (int) $q['m'] : 0;
1004      $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
1005      $q['post_type'] = 'attachment';
1006      $post_type = get_post_type_object( 'attachment' );
1007      $states = 'inherit';
1008      if ( current_user_can( $post_type->cap->read_private_posts ) )
1009          $states .= ',private';
1010  
1011      $q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states;
1012      $media_per_page = (int) get_user_option( 'upload_per_page' );
1013      if ( empty( $media_per_page ) || $media_per_page < 1 )
1014          $media_per_page = 20;
1015      $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
1016  
1017      $post_mime_types = get_post_mime_types();
1018      $avail_post_mime_types = get_available_post_mime_types('attachment');
1019  
1020      if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) )
1021          unset($q['post_mime_type']);
1022  
1023      if ( isset($q['detached']) )
1024          add_filter('posts_where', '_edit_attachments_query_helper');
1025  
1026      wp( $q );
1027  
1028      if ( isset($q['detached']) )
1029          remove_filter('posts_where', '_edit_attachments_query_helper');
1030  
1031      return array($post_mime_types, $avail_post_mime_types);
1032  }
1033  
1034  function _edit_attachments_query_helper($where) {
1035      return $where .= ' AND post_parent < 1';
1036  }
1037  
1038  /**
1039   * Returns the list of classes to be used by a metabox
1040   *
1041   * @uses get_user_option()
1042   * @since 2.5.0
1043   *
1044   * @param unknown_type $id
1045   * @param unknown_type $page
1046   * @return unknown
1047   */
1048  function postbox_classes( $id, $page ) {
1049      if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) {
1050          $classes = array( '' );
1051      } elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) {
1052          if ( !is_array( $closed ) ) {
1053              $classes = array( '' );
1054          } else {
1055              $classes = in_array( $id, $closed ) ? array( 'closed' ) : array( '' );
1056          }
1057      } else {
1058          $classes = array( '' );
1059      }
1060  
1061      $classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes );
1062      return implode( ' ', $classes );
1063  }
1064  
1065  /**
1066   * {@internal Missing Short Description}}
1067   *
1068   * @since 2.5.0
1069   *
1070   * @param int|object $id    Post ID or post object.
1071   * @param string $title (optional) Title
1072   * @param string $name (optional) Name
1073   * @return array With two entries of type string
1074   */
1075  function get_sample_permalink($id, $title = null, $name = null) {
1076      $post = &get_post($id);
1077      if ( !$post->ID )
1078          return array('', '');
1079  
1080      $ptype = get_post_type_object($post->post_type);
1081  
1082      $original_status = $post->post_status;
1083      $original_date = $post->post_date;
1084      $original_name = $post->post_name;
1085  
1086      // Hack: get_permalink would return ugly permalink for
1087      // drafts, so we will fake, that our post is published
1088      if ( in_array($post->post_status, array('draft', 'pending')) ) {
1089          $post->post_status = 'publish';
1090          $post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
1091      }
1092  
1093      // If the user wants to set a new name -- override the current one
1094      // Note: if empty name is supplied -- use the title instead, see #6072
1095      if ( !is_null($name) )
1096          $post->post_name = sanitize_title($name ? $name : $title, $post->ID);
1097  
1098      $post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
1099  
1100      $post->filter = 'sample';
1101  
1102      $permalink = get_permalink($post, true);
1103  
1104      // Replace custom post_type Token with generic pagename token for ease of use.
1105      $permalink = str_replace("%$post->post_type%", '%pagename%', $permalink);
1106  
1107      // Handle page hierarchy
1108      if ( $ptype->hierarchical ) {
1109          $uri = get_page_uri($post);
1110          $uri = untrailingslashit($uri);
1111          $uri = strrev( stristr( strrev( $uri ), '/' ) );
1112          $uri = untrailingslashit($uri);
1113          if ( !empty($uri) )
1114              $uri .= '/';
1115          $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
1116      }
1117  
1118      $permalink = array($permalink, apply_filters('editable_slug', $post->post_name));
1119      $post->post_status = $original_status;
1120      $post->post_date = $original_date;
1121      $post->post_name = $original_name;
1122      unset($post->filter);
1123  
1124      return $permalink;
1125  }
1126  
1127  /**
1128   * sample permalink html
1129   *
1130   * intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor.
1131   *
1132   * @since 2.5.0
1133   *
1134   * @param int|object $id Post ID or post object.
1135   * @param string $new_title (optional) New title
1136   * @param string $new_slug (optional) New slug
1137   * @return string intended to be used for the inplace editor of the permalink post slug on in the post (and page?) editor.
1138   */
1139  function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
1140      global $wpdb;
1141      $post = &get_post($id);
1142  
1143      list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
1144  
1145      if ( 'publish' == $post->post_status ) {
1146          $ptype = get_post_type_object($post->post_type);
1147          $view_post = $ptype->labels->view_item;
1148          $title = __('Click to edit this part of the permalink');
1149      } else {
1150          $title = __('Temporary permalink. Click to edit this part.');
1151      }
1152  
1153      if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) {
1154          $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink">' . $permalink . "</span>\n";
1155          if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) )
1156              $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
1157          if ( isset($view_post) )
1158              $return .= "<span id='view-post-btn'><a href='$permalink' class='button' target='_blank'>$view_post</a></span>\n";
1159  
1160          $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
1161  
1162          return $return;
1163      }
1164  
1165      if ( function_exists('mb_strlen') ) {
1166          if ( mb_strlen($post_name) > 30 ) {
1167              $post_name_abridged = mb_substr($post_name, 0, 14). '&hellip;' . mb_substr($post_name, -14);
1168          } else {
1169              $post_name_abridged = $post_name;
1170          }
1171      } else {
1172          if ( strlen($post_name) > 30 ) {
1173              $post_name_abridged = substr($post_name, 0, 14). '&hellip;' . substr($post_name, -14);
1174          } else {
1175              $post_name_abridged = $post_name;
1176          }
1177      }
1178  
1179      $post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>';
1180      $display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink);
1181      $view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
1182      $return =  '<strong>' . __('Permalink:') . "</strong>\n";
1183      $return .= '<span id="sample-permalink">' . $display_link . "</span>\n";
1184      $return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
1185      $return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n";
1186      $return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
1187      if ( isset($view_post) )
1188          $return .= "<span id='view-post-btn'><a href='$view_link' class='button' target='_blank'>$view_post</a></span>\n";
1189  
1190      $return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
1191  
1192      return $return;
1193  }
1194  
1195  /**
1196   * Output HTML for the post thumbnail meta-box.
1197   *
1198   * @since 2.9.0
1199   *
1200   * @param int $thumbnail_id ID of the attachment used for thumbnail
1201   * @return string html
1202   */
1203  function _wp_post_thumbnail_html( $thumbnail_id = NULL ) {
1204      global $content_width, $_wp_additional_image_sizes, $post_ID;
1205      $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="' . esc_url( get_upload_iframe_src('image') ) . '" id="set-post-thumbnail" class="thickbox">%s</a></p>';
1206      $content = sprintf($set_thumbnail_link, esc_html__( 'Set featured image' ));
1207  
1208      if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
1209          $old_content_width = $content_width;
1210          $content_width = 266;
1211          if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
1212              $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
1213          else
1214              $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
1215          if ( !empty( $thumbnail_html ) ) {
1216              $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$post_ID" );
1217              $content = sprintf($set_thumbnail_link, $thumbnail_html);
1218              $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>';
1219          }
1220          $content_width = $old_content_width;
1221      }
1222  
1223      return apply_filters( 'admin_post_thumbnail_html', $content );
1224  }
1225  
1226  /**
1227   * Check to see if the post is currently being edited by another user.
1228   *
1229   * @since 2.5.0
1230   *
1231   * @param int $post_id ID of the post to check for editing
1232   * @return bool|int False: not locked or locked by current user. Int: user ID of user with lock.
1233   */
1234  function wp_check_post_lock( $post_id ) {
1235      if ( !$post = get_post( $post_id ) )
1236          return false;
1237  
1238      if ( !$lock = get_post_meta( $post->ID, '_edit_lock', true ) )
1239          return false;
1240  
1241      $lock = explode( ':', $lock );
1242      $time = $lock[0];
1243      $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
1244  
1245      $time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 );
1246  
1247      if ( $time && $time > time() - $time_window && $user != get_current_user_id() )
1248          return $user;
1249      return false;
1250  }
1251  
1252  /**
1253   * Mark the post as currently being edited by the current user
1254   *
1255   * @since 2.5.0
1256   *
1257   * @param int $post_id ID of the post to being edited
1258   * @return bool Returns false if the post doesn't exist of there is no current user
1259   */
1260  function wp_set_post_lock( $post_id ) {
1261      if ( !$post = get_post( $post_id ) )
1262          return false;
1263      if ( 0 == ($user_id = get_current_user_id()) )
1264          return false;
1265  
1266      $now = time();
1267      $lock = "$now:$user_id";
1268  
1269      update_post_meta( $post->ID, '_edit_lock', $lock );
1270  }
1271  
1272  /**
1273   * Outputs the notice message to say that someone else is editing this post at the moment.
1274   *
1275   * @since 2.8.5
1276   * @return none
1277   */
1278  function _admin_notice_post_locked() {
1279      global $post;
1280  
1281      $lock = explode( ':', get_post_meta( $post->ID, '_edit_lock', true ) );
1282      $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
1283      $last_user = get_userdata( $user );
1284      $last_user_name = $last_user ? $last_user->display_name : __('Somebody');
1285  
1286      switch ($post->post_type) {
1287          case 'post':
1288              $message = __( 'Warning: %s is currently editing this post' );
1289              break;
1290          case 'page':
1291              $message = __( 'Warning: %s is currently editing this page' );
1292              break;
1293          default:
1294              $message = __( 'Warning: %s is currently editing this.' );
1295      }
1296  
1297      $message = sprintf( $message, esc_html( $last_user_name ) );
1298      echo "<div class='error'><p>$message</p></div>";
1299  }
1300  
1301  /**
1302   * Creates autosave data for the specified post from $_POST data.
1303   *
1304   * @package WordPress
1305   * @subpackage Post_Revisions
1306   * @since 2.6.0
1307   *
1308   * @uses _wp_translate_postdata()
1309   * @uses _wp_post_revision_fields()
1310   *
1311   * @return unknown
1312   */
1313  function wp_create_post_autosave( $post_id ) {
1314      $translated = _wp_translate_postdata( true );
1315      if ( is_wp_error( $translated ) )
1316          return $translated;
1317  
1318      // Only store one autosave.  If there is already an autosave, overwrite it.
1319      if ( $old_autosave = wp_get_post_autosave( $post_id ) ) {
1320          $new_autosave = _wp_post_revision_fields( $_POST, true );
1321          $new_autosave['ID'] = $old_autosave->ID;
1322          $new_autosave['post_author'] = get_current_user_id();
1323          return wp_update_post( $new_autosave );
1324      }
1325  
1326      // _wp_put_post_revision() expects unescaped.
1327      $_POST = stripslashes_deep($_POST);
1328  
1329      // Otherwise create the new autosave as a special post revision
1330      return _wp_put_post_revision( $_POST, true );
1331  }
1332  
1333  /**
1334   * Save draft or manually autosave for showing preview.
1335   *
1336   * @package WordPress
1337   * @since 2.7.0
1338   *
1339   * @uses wp_write_post()
1340   * @uses edit_post()
1341   * @uses get_post()
1342   * @uses current_user_can()
1343   * @uses wp_create_post_autosave()
1344   *
1345   * @return str URL to redirect to show the preview
1346   */
1347  function post_preview() {
1348  
1349      $post_ID = (int) $_POST['post_ID'];
1350      $status = get_post_status( $post_ID );
1351      if ( 'auto-draft' == $status )
1352          wp_die( __('Preview not available. Please save as a draft first.') );
1353  
1354      if ( isset($_POST['catslist']) )
1355          $_POST['post_category'] = explode(",", $_POST['catslist']);
1356  
1357      if ( isset($_POST['tags_input']) )
1358          $_POST['tags_input'] = explode(",", $_POST['tags_input']);
1359  
1360      if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
1361          unset($_POST['post_category']);
1362  
1363      $_POST['ID'] = $post_ID;
1364      $post = get_post($post_ID);
1365  
1366      if ( 'page' == $post->post_type ) {
1367          if ( !current_user_can('edit_page', $post_ID) )
1368              wp_die(__('You are not allowed to edit this page.'));
1369      } else {
1370          if ( !current_user_can('edit_post', $post_ID) )
1371              wp_die(__('You are not allowed to edit this post.'));
1372      }
1373  
1374      if ( 'draft' == $post->post_status ) {
1375          $id = edit_post();
1376      } else { // Non drafts are not overwritten.  The autosave is stored in a special post revision.
1377          $id = wp_create_post_autosave( $post->ID );
1378          if ( ! is_wp_error($id) )
1379              $id = $post->ID;
1380      }
1381  
1382      if ( is_wp_error($id) )
1383          wp_die( $id->get_error_message() );
1384  
1385      if ( $_POST['post_status'] == 'draft'  ) {
1386          $url = add_query_arg( 'preview', 'true', get_permalink($id) );
1387      } else {
1388          $nonce = wp_create_nonce('post_preview_' . $id);
1389          $url = add_query_arg( array( 'preview' => 'true', 'preview_id' => $id, 'preview_nonce' => $nonce ), get_permalink($id) );
1390      }
1391  
1392      return $url;
1393  }
1394  
1395  /**
1396   * Adds the TinyMCE editor used on the Write and Edit screens.
1397   *
1398   * @package WordPress
1399   * @since 2.7.0
1400   *
1401   * TinyMCE is loaded separately from other Javascript by using wp-tinymce.php. It outputs concatenated
1402   * and optionaly pre-compressed version of the core and all default plugins. Additional plugins are loaded
1403   * directly by TinyMCE using non-blocking method. Custom plugins can be refreshed by adding a query string
1404   * to the URL when queueing them with the mce_external_plugins filter.
1405   *
1406   * @param bool $teeny optional Output a trimmed down version used in Press This.
1407   * @param mixed $settings optional An array that can add to or overwrite the default TinyMCE settings.
1408   */
1409  function wp_tiny_mce( $teeny = false, $settings = false ) {
1410      global $concatenate_scripts, $compress_scripts, $tinymce_version, $editor_styles;
1411  
1412      if ( ! user_can_richedit() )
1413          return;
1414  
1415      $baseurl = includes_url('js/tinymce');
1416  
1417      $mce_locale = ( '' == get_locale() ) ? 'en' : strtolower( substr(get_locale(), 0, 2) ); // only ISO 639-1
1418  
1419      /*
1420      The following filter allows localization scripts to change the languages displayed in the spellchecker's drop-down menu.
1421      By default it uses Google's spellchecker API, but can be configured to use PSpell/ASpell if installed on the server.
1422      The + sign marks the default language. More information:
1423      http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker
1424      */
1425      $mce_spellchecker_languages = apply_filters('mce_spellchecker_languages', '+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv');
1426  
1427      if ( $teeny ) {
1428          $plugins = apply_filters( 'teeny_mce_plugins', array('inlinepopups', 'fullscreen', 'wordpress', 'wplink', 'wpdialogs') );
1429          $ext_plugins = '';
1430      } else {
1431          $plugins = array( 'inlinepopups', 'spellchecker', 'tabfocus', 'paste', 'media', 'wordpress', 'wpfullscreen', 'wpeditimage', 'wpgallery', 'wplink', 'wpdialogs' );
1432  
1433          /*
1434          The following filter takes an associative array of external plugins for TinyMCE in the form 'plugin_name' => 'url'.
1435          It adds the plugin's name to TinyMCE's plugins init and the call to PluginManager to load the plugin.
1436          The url should be absolute and should include the js file name to be loaded. Example:
1437          array( 'myplugin' => 'http://my-site.com/wp-content/plugins/myfolder/mce_plugin.js' )
1438          If the plugin uses a button, it should be added with one of the "$mce_buttons" filters.
1439          */
1440          $mce_external_plugins = apply_filters('mce_external_plugins', array());
1441  
1442          $ext_plugins = '';
1443          if ( ! empty($mce_external_plugins) ) {
1444  
1445              /*
1446              The following filter loads external language files for TinyMCE plugins.
1447              It takes an associative array 'plugin_name' => 'path', where path is the
1448              include path to the file. The language file should follow the same format as
1449              /tinymce/langs/wp-langs.php and should define a variable $strings that
1450              holds all translated strings.
1451              When this filter is not used, the function will try to load {mce_locale}.js.
1452              If that is not found, en.js will be tried next.
1453              */
1454              $mce_external_languages = apply_filters('mce_external_languages', array());
1455  
1456              $loaded_langs = array();
1457              $strings = '';
1458  
1459              if ( ! empty($mce_external_languages) ) {
1460                  foreach ( $mce_external_languages as $name => $path ) {
1461                      if ( @is_file($path) && @is_readable($path) ) {
1462                          include_once($path);
1463                          $ext_plugins .= $strings . "\n";
1464                          $loaded_langs[] = $name;
1465                      }
1466                  }
1467              }
1468  
1469              foreach ( $mce_external_plugins as $name => $url ) {
1470  
1471                  if ( is_ssl() ) $url = str_replace('http://', 'https://', $url);
1472  
1473                  $plugins[] = '-' . $name;
1474  
1475                  $plugurl = dirname($url);
1476                  $strings = $str1 = $str2 = '';
1477                  if ( ! in_array($name, $loaded_langs) ) {
1478                      $path = str_replace( WP_PLUGIN_URL, '', $plugurl );
1479                      $path = WP_PLUGIN_DIR . $path . '/langs/';
1480  
1481                      if ( function_exists('realpath') )
1482                          $path = trailingslashit( realpath($path) );
1483  
1484                      if ( @is_file($path . $mce_locale . '.js') )
1485                          $strings .= @file_get_contents($path . $mce_locale . '.js') . "\n";
1486  
1487                      if ( @is_file($path . $mce_locale . '_dlg.js') )
1488                          $strings .= @file_get_contents($path . $mce_locale . '_dlg.js') . "\n";
1489  
1490                      if ( 'en' != $mce_locale && empty($strings) ) {
1491                          if ( @is_file($path . 'en.js') ) {
1492                              $str1 = @file_get_contents($path . 'en.js');
1493                              $strings .= preg_replace( '/([\'"])en\./', '$1' . $mce_locale . '.', $str1, 1 ) . "\n";
1494                          }
1495  
1496                          if ( @is_file($path . 'en_dlg.js') ) {
1497                              $str2 = @file_get_contents($path . 'en_dlg.js');
1498                              $strings .= preg_replace( '/([\'"])en\./', '$1' . $mce_locale . '.', $str2, 1 ) . "\n";
1499                          }
1500                      }
1501  
1502                      if ( ! empty($strings) )
1503                          $ext_plugins .= "\n" . $strings . "\n";
1504                  }
1505  
1506                  $ext_plugins .= 'tinyMCEPreInit.load_ext("' . $plugurl . '", "' . $mce_locale . '");' . "\n";
1507                  $ext_plugins .= 'tinymce.PluginManager.load("' . $name . '", "' . $url . '");' . "\n";
1508              }
1509          }
1510      }
1511  
1512      if ( $teeny ) {
1513          $mce_buttons = apply_filters( 'teeny_mce_buttons', array('bold, italic, underline, blockquote, separator, strikethrough, bullist, numlist,justifyleft, justifycenter, justifyright, undo, redo, link, unlink, fullscreen') );
1514          $mce_buttons = implode($mce_buttons, ',');
1515          $mce_buttons_2 = $mce_buttons_3 = $mce_buttons_4 = '';
1516      } else {
1517          $mce_buttons = apply_filters('mce_buttons', array('bold', 'italic', 'strikethrough', '|', 'bullist', 'numlist', 'blockquote', '|', 'justifyleft', 'justifycenter', 'justifyright', '|', 'link', 'unlink', 'wp_more', '|', 'spellchecker', 'fullscreen', 'wp_adv' ));
1518          $mce_buttons = implode($mce_buttons, ',');
1519  
1520          $mce_buttons_2 = array( 'formatselect', 'underline', 'justifyfull', 'forecolor', '|', 'pastetext', 'pasteword', 'removeformat', '|', 'charmap', '|', 'outdent', 'indent', '|', 'undo', 'redo', 'wp_help' );
1521          $mce_buttons_2 = apply_filters('mce_buttons_2', $mce_buttons_2);
1522          $mce_buttons_2 = implode($mce_buttons_2, ',');
1523  
1524          $mce_buttons_3 = apply_filters('mce_buttons_3', array());
1525          $mce_buttons_3 = implode($mce_buttons_3, ',');
1526  
1527          $mce_buttons_4 = apply_filters('mce_buttons_4', array());
1528          $mce_buttons_4 = implode($mce_buttons_4, ',');
1529      }
1530      $no_captions = (bool) apply_filters( 'disable_captions', '' );
1531  
1532      // TinyMCE init settings
1533      $initArray = array (
1534          'mode' => 'specific_textareas',
1535          'editor_selector' => 'theEditor',
1536          'width' => '100%',
1537          'theme' => 'advanced',
1538          'skin' => 'wp_theme',
1539          'theme_advanced_buttons1' => $mce_buttons,
1540          'theme_advanced_buttons2' => $mce_buttons_2,
1541          'theme_advanced_buttons3' => $mce_buttons_3,
1542          'theme_advanced_buttons4' => $mce_buttons_4,
1543          'language' => $mce_locale,
1544          'spellchecker_languages' => $mce_spellchecker_languages,
1545          'theme_advanced_toolbar_location' => 'top',
1546          'theme_advanced_toolbar_align' => 'left',
1547          'theme_advanced_statusbar_location' => 'bottom',
1548          'theme_advanced_resizing' => true,
1549          'theme_advanced_resize_horizontal' => false,
1550          'dialog_type' => 'modal',
1551          'formats' => "{
1552              alignleft : [
1553                  {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'left'}},
1554                  {selector : 'img,table', classes : 'alignleft'}
1555              ],
1556              aligncenter : [
1557                  {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'center'}},
1558                  {selector : 'img,table', classes : 'aligncenter'}
1559              ],
1560              alignright : [
1561                  {selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'right'}},
1562                  {selector : 'img,table', classes : 'alignright'}
1563              ],
1564              strikethrough : {inline : 'del'}
1565          }",
1566          'relative_urls' => false,
1567          'remove_script_host' => false,
1568          'convert_urls' => false,
1569          'apply_source_formatting' => false,
1570          'remove_linebreaks' => true,
1571          'gecko_spellcheck' => true,
1572          'keep_styles' => false,
1573          'entities' => '38,amp,60,lt,62,gt',
1574          'accessibility_focus' => true,
1575          'tabfocus_elements' => 'major-publishing-actions',
1576          'media_strict' => false,
1577          'paste_remove_styles' => true,
1578          'paste_remove_spans' => true,
1579          'paste_strip_class_attributes' => 'all',
1580          'paste_text_use_dialog' => true,
1581          'extended_valid_elements' => 'article[*],aside[*],audio[*],canvas[*],command[*],datalist[*],details[*],embed[*],figcaption[*],figure[*],footer[*],header[*],hgroup[*],keygen[*],mark[*],meter[*],nav[*],output[*],progress[*],section[*],source[*],summary,time[*],video[*],wbr',
1582          'wpeditimage_disable_captions' => $no_captions,
1583          'wp_fullscreen_content_css' => "$baseurl/plugins/wpfullscreen/css/content.css",
1584          'plugins' => implode( ',', $plugins ),
1585      );
1586  
1587      if ( ! empty( $editor_styles ) && is_array( $editor_styles ) ) {
1588          $mce_css = array();
1589          $style_uri = get_stylesheet_directory_uri();
1590          if ( ! is_child_theme() ) {
1591              foreach ( $editor_styles as $file )
1592                  $mce_css[] = "$style_uri/$file";
1593          } else {
1594              $style_dir    = get_stylesheet_directory();
1595              $template_uri = get_template_directory_uri();
1596              $template_dir = get_template_directory();
1597              foreach ( $editor_styles as $file ) {
1598                  if ( file_exists( "$template_dir/$file" ) )
1599                      $mce_css[] = "$template_uri/$file";
1600                  if ( file_exists( "$style_dir/$file" ) )
1601                      $mce_css[] = "$style_uri/$file";
1602              }
1603          }
1604          $mce_css = implode( ',', $mce_css );
1605      } else {
1606          $mce_css = '';
1607      }
1608  
1609      $mce_css = trim( apply_filters( 'mce_css', $mce_css ), ' ,' );
1610  
1611      if ( ! empty($mce_css) )
1612          $initArray['content_css'] = $mce_css;
1613  
1614      if ( is_array($settings) )
1615          $initArray = array_merge($initArray, $settings);
1616  
1617      // For people who really REALLY know what they're doing with TinyMCE
1618      // You can modify initArray to add, remove, change elements of the config before tinyMCE.init
1619      // Setting "valid_elements", "invalid_elements" and "extended_valid_elements" can be done through "tiny_mce_before_init".
1620      // Best is to use the default cleanup by not specifying valid_elements, as TinyMCE contains full set of XHTML 1.0.
1621      if ( $teeny ) {
1622          $initArray = apply_filters('teeny_mce_before_init', $initArray);
1623      } else {
1624          $initArray = apply_filters('tiny_mce_before_init', $initArray);
1625      }
1626  
1627      if ( empty($initArray['theme_advanced_buttons3']) && !empty($initArray['theme_advanced_buttons4']) ) {
1628          $initArray['theme_advanced_buttons3'] = $initArray['theme_advanced_buttons4'];
1629          $initArray['theme_advanced_buttons4'] = '';
1630      }
1631  
1632      if ( ! isset($concatenate_scripts) )
1633          script_concat_settings();
1634  
1635      $language = $initArray['language'];
1636  
1637      $compressed = $compress_scripts && $concatenate_scripts && isset($_SERVER['HTTP_ACCEPT_ENCODING'])
1638          && false !== stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
1639  
1640      /**
1641       * Deprecated
1642       *
1643       * The tiny_mce_version filter is not needed since external plugins are loaded directly by TinyMCE.
1644       * These plugins can be refreshed by appending query string to the URL passed to mce_external_plugins filter.
1645       * If the plugin has a popup dialog, a query string can be added to the button action that opens it (in the plugin's code).
1646       */
1647      $version = apply_filters('tiny_mce_version', '');
1648      $version = 'ver=' . $tinymce_version . $version;
1649  
1650      if ( 'en' != $language )
1651          include_once(ABSPATH . WPINC . '/js/tinymce/langs/wp-langs.php');
1652  
1653      $mce_options = '';
1654      foreach ( $initArray as $k => $v ) {
1655          if ( is_bool($v) ) {
1656              $val = $v ? 'true' : 'false';
1657              $mce_options .= $k . ':' . $val . ', ';
1658              continue;
1659          } elseif ( !empty($v) && is_string($v) && ( ('{' == $v{0} && '}' == $v{strlen($v) - 1}) || ('[' == $v{0} && ']' == $v{strlen($v) - 1}) || preg_match('/^\(?function ?\(/', $v) ) ) {
1660              $mce_options .= $k . ':' . $v . ', ';
1661              continue;
1662          }
1663  
1664          $mce_options .= $k . ':"' . $v . '", ';
1665      }
1666  
1667      $mce_options = rtrim( trim($mce_options), '\n\r,' );
1668  
1669      do_action('before_wp_tiny_mce', $initArray); ?>
1670  
1671  <script type="text/javascript">
1672  /* <![CDATA[ */
1673  tinyMCEPreInit = {
1674      base : "<?php echo $baseurl; ?>",
1675      suffix : "",
1676      query : "<?php echo $version; ?>",
1677      mceInit : {<?php echo $mce_options; ?>},
1678      load_ext : function(url,lang){var sl=tinymce.ScriptLoader;sl.markDone(url+'/langs/'+lang+'.js');sl.markDone(url+'/langs/'+lang+'_dlg.js');}
1679  };
1680  /* ]]> */
1681  </script>
1682  
1683  <?php
1684      if ( $compressed )
1685          echo "<script type='text/javascript' src='$baseurl/wp-tinymce.php?c=1&amp;$version'></script>\n";
1686      else
1687          echo "<script type='text/javascript' src='$baseurl/tiny_mce.js?$version'></script>\n";
1688  
1689      if ( 'en' != $language && isset($lang) )
1690          echo "<script type='text/javascript'>\n$lang\n</script>\n";
1691      else
1692          echo "<script type='text/javascript' src='$baseurl/langs/wp-langs-en.js?$version'></script>\n";
1693  ?>
1694  
1695  <script type="text/javascript">
1696  /* <![CDATA[ */
1697  <?php
1698      if ( $ext_plugins )
1699          echo "$ext_plugins\n";
1700  
1701      if ( ! $compressed ) {
1702  ?>
1703  (function(){var t=tinyMCEPreInit,sl=tinymce.ScriptLoader,ln=t.mceInit.language,th=t.mceInit.theme,pl=t.mceInit.plugins;sl.markDone(t.base+'/langs/'+ln+'.js');sl.markDone(t.base+'/themes/'+th+'/langs/'+ln+'.js');sl.markDone(t.base+'/themes/'+th+'/langs/'+ln+'_dlg.js');tinymce.each(pl.split(','),function(n){if(n&&n.charAt(0)!='-'){sl.markDone(t.base+'/plugins/'+n+'/langs/'+ln+'.js');sl.markDone(t.base+'/plugins/'+n+'/langs/'+ln+'_dlg.js');}});})();
1704  <?php } ?>
1705  tinyMCE.init(tinyMCEPreInit.mceInit);
1706  /* ]]> */
1707  </script>
1708  <?php
1709  
1710  do_action('after_wp_tiny_mce', $initArray);
1711  }
1712  
1713  // Load additional inline scripts based on active plugins.
1714  function wp_preload_dialogs($init) {
1715      $plugins = preg_split('/[ ,-]+/', $init['plugins']);
1716  
1717      if ( in_array( 'wpdialogs', $plugins, true ) ) {
1718          wp_print_scripts('wpdialogs-popup');
1719          wp_print_styles('wp-jquery-ui-dialog');
1720      }
1721  
1722      if ( in_array( 'wplink', $plugins, true ) ) {
1723          require_once  ABSPATH . 'wp-admin/includes/internal-linking.php';
1724          ?><div style="display:none;"><?php wp_link_dialog(); ?></div><?php
1725          wp_print_scripts('wplink');
1726          wp_print_styles('wplink');
1727      }
1728  
1729      // Distraction Free Writing mode
1730      if ( in_array( 'wpfullscreen', $plugins, true ) ) {
1731          wp_fullscreen_html();
1732          wp_print_scripts('wp-fullscreen');
1733      }
1734  
1735      wp_print_scripts('word-count');
1736  }
1737  
1738  function wp_quicktags() {
1739      global $tinymce_version;
1740      
1741      wp_preload_dialogs( array( 'plugins' => 'wpdialogs,wplink,wpfullscreen' ) );
1742      
1743      if ( !user_can_richedit() ) {
1744          wp_enqueue_style( 'tinymce-buttons', includes_url('js/tinymce/themes/advanced/skins/wp_theme/ui.css'), array(), $tinymce_version );
1745          wp_print_styles('tinymce-buttons');
1746      }
1747  }
1748  
1749  function wp_print_editor_js() {
1750      wp_print_scripts('editor');
1751  }
1752  
1753  function wp_fullscreen_html() {
1754      global $content_width, $post;
1755  
1756      $width = isset($content_width) && 800 > $content_width ? $content_width : 800;
1757      $width = $width + 10; // compensate for the padding
1758      $dfw_width = get_user_setting( 'dfw_width', $width );
1759      $save = $post->post_status == 'publish' ? __('Update') : __('Save');
1760  ?>
1761  <div id="wp-fullscreen-body">
1762  <div id="fullscreen-topbar">
1763      <div id="wp-fullscreen-toolbar">
1764          <div id="wp-fullscreen-close"><a href="#" onclick="fullscreen.off();return false;"><?php _e('Exit fullscreen'); ?></a></div>
1765          <div id="wp-fullscreen-central-toolbar" style="width:<?php echo $width; ?>px;">
1766  
1767          <div id="wp-fullscreen-mode-bar"><div id="wp-fullscreen-modes">
1768              <a href="#" onclick="fullscreen.switchmode('tinymce');return false;"><?php _e('Visual'); ?></a>
1769              <a href="#" onclick="fullscreen.switchmode('html');return false;"><?php _e('HTML'); ?></a>
1770          </div></div>
1771  
1772          <div id="wp-fullscreen-button-bar"><div id="wp-fullscreen-buttons" class="wp_themeSkin">
1773  <?php
1774  
1775      $buttons = array(
1776          // format: title, onclick, show in both editors
1777          'bold' => array( 'title' => __('Bold (Ctrl + B)'), 'onclick' => 'fullscreen.b();', 'both' => false ),
1778          'italic' => array( 'title' => __('Italic (Ctrl + I)'), 'onclick' => 'fullscreen.i();', 'both' => false ),
1779          '0' => 'separator',
1780          'bullist' => array( 'title' => __('Unordered list (Alt + Shift + U)'), 'onclick' => 'fullscreen.ul();', 'both' => false ),
1781          'numlist' => array( 'title' => __('Ordered list (Alt + Shift + O)'), 'onclick' => 'fullscreen.ol();', 'both' => false ),
1782          '1' => 'separator',
1783          'blockquote' => array( 'title' => __('Blockquote (Alt+Shift+Q)'), 'onclick' => 'fullscreen.blockquote();', 'both' => false ),
1784          'image' => array( 'title' => __('Insert/edit image (Alt + Shift + M)'), 'onclick' => "jQuery('#add_image').click();", 'both' => true ),
1785          '2' => 'separator',
1786          'link' => array( 'title' => __('Insert/edit link (Alt + Shift + A)'), 'onclick' => 'fullscreen.link();', 'both' => true ),
1787          'unlink' => array( 'title' => __('Unlink (Alt + Shift + S)'), 'onclick' => 'fullscreen.unlink();', 'both' => false ),
1788          '3' => 'separator',
1789          'help' => array( 'title' => __('Help (Alt + Shift + H)'), 'onclick' => 'fullscreen.help();', 'both' => false )
1790      );
1791  
1792      $buttons = apply_filters( 'wp_fullscreen_buttons', $buttons );
1793  
1794      foreach ( $buttons as $button => $args ) {
1795          if ( 'separator' == $args ) { ?> 
1796              <div><span aria-orientation="vertical" role="separator" class="mceSeparator"></span></div>
1797  <?php        continue;
1798          } ?>
1799  
1800          <div<?php if ( $args['both'] ) { ?> class="wp-fullscreen-both"<?php } ?>>
1801          <a title="<?php echo $args['title']; ?>" onclick="<?php echo $args['onclick']; ?>return false;" class="mceButton mceButtonEnabled mce_<?php echo $button; ?>" href="#" id="wp_fs_<?php echo $button; ?>" role="button" aria-pressed="false">
1802          <span class="mceIcon mce_<?php echo $button; ?>"></span>
1803          </a>
1804          </div>
1805  <?php
1806      } ?>
1807  
1808          </div></div>
1809  
1810          <div id="wp-fullscreen-save">
1811              <span><?php if ( $post->post_status == 'publish' ) _e('Updated.'); else _e('Saved.'); ?></span>
1812              <img src="images/wpspin_light.gif" alt="" />
1813              <input type="button" class="button-primary" value="<?php echo $save; ?>" onclick="fullscreen.save();" />
1814          </div>
1815  
1816          </div>
1817      </div>
1818  </div>
1819  
1820  <div id="wp-fullscreen-wrap" style="width:<?php echo $dfw_width; ?>px;">
1821      <label id="wp-fullscreen-title-prompt-text" for="wp-fullscreen-title"><?php echo apply_filters( 'enter_title_here', __( 'Enter title here' ), $post ); ?></label>
1822      <input type="text" id="wp-fullscreen-title" value="" autocomplete="off" />
1823  
1824      <div id="wp-fullscreen-container">
1825          <textarea id="wp_mce_fullscreen"></textarea>
1826      </div>
1827  
1828      <div id="wp-fullscreen-status">
1829          <div id="wp-fullscreen-count"><?php printf( __( 'Word count: %s' ), '<span class="word-count">0</span>' ); ?></div>
1830          <div id="wp-fullscreen-tagline"><?php _e('Just write.'); ?></div>
1831      </div>
1832  </div>
1833  </div>
1834  
1835  <div class="fullscreen-overlay" id="fullscreen-overlay"></div>
1836  <div class="fullscreen-overlay fullscreen-fader fade-600" id="fullscreen-fader"></div>
1837  <?php
1838  }
1839  
1840  


Generated: Wed Jun 1 08:30:02 2011 Cross-referenced by PHPXref 0.7
Provided by Yoast and awesome WordPress Hosting