Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/post.php

    r6125 r7529  
    33// Update an existing post with values provided in $_POST.
    44function edit_post() {
    5     global $user_ID;
    65
    76    $post_ID = (int) $_POST['post_ID'];
     
    2019        $now = time();
    2120        $then = strtotime($post->post_date_gmt . ' +0000');
    22         // Keep autosave_interval in sync with autosave-js.php.
    23         $delta = apply_filters( 'autosave_interval', 120 ) / 2;
     21        $delta = AUTOSAVE_INTERVAL / 2;
    2422        if ( ($now - $then) < $delta )
    2523            return $post_ID;
     
    3028    $_POST['post_content'] = $_POST['content'];
    3129    $_POST['post_excerpt'] = $_POST['excerpt'];
    32     $_POST['post_parent'] = $_POST['parent_id'];
     30    $_POST['post_parent'] = isset($_POST['parent_id'])? $_POST['parent_id'] : '';
    3331    $_POST['to_ping'] = $_POST['trackback_url'];
    3432
     
    5452
    5553    // What to do based on which button they pressed
    56     if ('' != $_POST['saveasdraft'] )
     54    if ( isset($_POST['saveasdraft']) && '' != $_POST['saveasdraft'] )
    5755        $_POST['post_status'] = 'draft';
    58     if ('' != $_POST['saveasprivate'] )
     56    if ( isset($_POST['saveasprivate']) && '' != $_POST['saveasprivate'] )
    5957        $_POST['post_status'] = 'private';
    60     if ('' != $_POST['publish'] )
     58    if ( isset($_POST['publish']) && ( '' != $_POST['publish'] ) && ( $_POST['post_status'] != 'private' ) )
    6159        $_POST['post_status'] = 'publish';
    62     if ('' != $_POST['advanced'] )
     60    if ( isset($_POST['advanced']) && '' != $_POST['advanced'] )
    6361        $_POST['post_status'] = 'draft';
    6462
    6563    if ( 'page' == $_POST['post_type'] ) {
    66         if ('publish' == $_POST['post_status'] && !current_user_can( 'edit_published_pages' ))
     64        if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_pages' ))
    6765            $_POST['post_status'] = 'pending';
    6866    } else {
    69         if ('publish' == $_POST['post_status'] && !current_user_can( 'edit_published_posts' ))
     67        if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_posts' ))
    7068            $_POST['post_status'] = 'pending';
    7169    }
     
    7674    if (!isset( $_POST['ping_status'] ))
    7775        $_POST['ping_status'] = 'closed';
     76
     77    foreach ( array ('aa', 'mm', 'jj', 'hh', 'mm') as $timeunit ) {
     78        if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
     79            $_POST['edit_date'] = '1';
     80            break;
     81        }
     82    }
    7883
    7984    if (!empty ( $_POST['edit_date'] ) ) {
     
    9398
    9499    // Meta Stuff
    95     if ( $_POST['meta'] ) {
     100    if ( isset($_POST['meta']) && $_POST['meta'] ) {
    96101        foreach ( $_POST['meta'] as $key => $value )
    97102            update_meta( $key, $value['key'], $value['value'] );
    98103    }
    99104
    100     if ( $_POST['deletemeta'] ) {
     105    if ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) {
    101106        foreach ( $_POST['deletemeta'] as $key => $value )
    102107            delete_meta( $key );
     
    115120    // Now that we have an ID we can fix any attachment anchor hrefs
    116121    _fix_attachment_links( $post_ID );
     122
     123    wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );
    117124
    118125    return $post_ID;
     
    130137    }
    131138
     139    $post_content = '';
    132140    if ( !empty( $_REQUEST['content'] ) )
    133141        $post_content = wp_specialchars( stripslashes( $_REQUEST['content'] ));
     
    144152        $post_excerpt = '';
    145153
     154    $post->ID = 0;
     155    $post->post_name = '';
     156    $post->post_author = '';
     157    $post->post_date = '';
    146158    $post->post_status = 'draft';
     159    $post->post_type = 'post';
     160    $post->to_ping = '';
     161    $post->pinged = '';
    147162    $post->comment_status = get_option( 'default_comment_status' );
    148163    $post->ping_status = get_option( 'default_ping_status' );
     
    159174}
    160175
     176function get_default_page_to_edit() {
     177    $page = get_default_post_to_edit();
     178    $page->post_type = 'page';
     179    return $page;
     180}
     181
    161182// Get an existing post and format it for editing.
    162183function get_post_to_edit( $id ) {
     
    219240    $_POST['post_content'] = $_POST['content'];
    220241    $_POST['post_excerpt'] = $_POST['excerpt'];
    221     $_POST['post_parent'] = $_POST['parent_id'];
     242    $_POST['post_parent'] = isset($_POST['parent_id'])? $_POST['parent_id'] : '';
    222243    $_POST['to_ping'] = $_POST['trackback_url'];
    223244
     
    245266
    246267    // What to do based on which button they pressed
    247     if ('' != $_POST['saveasdraft'] )
     268    if ( isset($_POST['saveasdraft']) && '' != $_POST['saveasdraft'] )
    248269        $_POST['post_status'] = 'draft';
    249     if ('' != $_POST['saveasprivate'] )
     270    if ( isset($_POST['saveasprivate']) && '' != $_POST['saveasprivate'] )
    250271        $_POST['post_status'] = 'private';
    251     if ('' != $_POST['publish'] )
     272    if ( isset($_POST['publish']) && ( '' != $_POST['publish'] ) && ( $_POST['post_status'] != 'private' ) )
    252273        $_POST['post_status'] = 'publish';
    253     if ('' != $_POST['advanced'] )
     274    if ( isset($_POST['advanced']) && '' != $_POST['advanced'] )
    254275        $_POST['post_status'] = 'draft';
    255276
     
    267288    if (!isset( $_POST['ping_status'] ))
    268289        $_POST['ping_status'] = 'closed';
     290
     291    foreach ( array ('aa', 'mm', 'jj', 'hh', 'mm') as $timeunit ) {
     292        if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
     293            $_POST['edit_date'] = '1';
     294            break;
     295        }
     296    }
    269297
    270298    if (!empty ( $_POST['edit_date'] ) ) {
     
    310338    _fix_attachment_links( $post_ID );
    311339
     340    wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );
     341
    312342    return $post_ID;
    313343}
     
    349379            return false;
    350380
    351         $result = $wpdb->query( "
    352                         INSERT INTO $wpdb->postmeta
    353                         (post_id,meta_key,meta_value )
    354                         VALUES ('$post_ID','$metakey','$metavalue' )
    355                     " );
     381        wp_cache_delete($post_ID, 'post_meta');
     382
     383        $wpdb->query( "
     384                INSERT INTO $wpdb->postmeta
     385                (post_id,meta_key,meta_value )
     386                VALUES ('$post_ID','$metakey','$metavalue' )
     387            " );
    356388        return $wpdb->insert_id;
    357389    }
     
    362394    global $wpdb;
    363395    $mid = (int) $mid;
     396
     397    $post_id = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = '$mid'");
     398    wp_cache_delete($post_id, 'post_meta');
    364399
    365400    return $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id = '$mid'" );
     
    409444        return false;
    410445
     446    $post_id = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = '$mid'");
     447    wp_cache_delete($post_id, 'post_meta');
     448
    411449    $mvalue = maybe_serialize( stripslashes( $mvalue ));
    412450    $mvalue = $wpdb->escape( $mvalue );
     
    421459// Replace hrefs of attachment anchors with up-to-date permalinks.
    422460function _fix_attachment_links( $post_ID ) {
    423     global $wp_rewrite;
    424461
    425462    $post = & get_post( $post_ID, ARRAY_A );
     
    469506}
    470507
     508function get_available_post_statuses($type = 'post') {
     509    global $wpdb;
     510
     511    $stati = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_status FROM $wpdb->posts WHERE post_type = %s", $type));
     512    return $stati;
     513}
     514
     515function wp_edit_posts_query( $q = false ) {
     516    global $wpdb;
     517    if ( false === $q )
     518        $q = $_GET;
     519    $q['m']   = (int) $q['m'];
     520    $q['cat'] = (int) $q['cat'];
     521    $post_stati  = array(   //  array( adj, noun )
     522                'publish' => array(__('Published'), __('Published posts'), __ngettext_noop('Published (%s)', 'Published (%s)')),
     523                'future' => array(__('Scheduled'), __('Scheduled posts'), __ngettext_noop('Scheduled (%s)', 'Scheduled (%s)')),
     524                'pending' => array(__('Pending Review'), __('Pending posts'), __ngettext_noop('Pending Review (%s)', 'Pending Review (%s)')),
     525                'draft' => array(__('Draft'), _c('Drafts|manage posts header'), __ngettext_noop('Draft (%s)', 'Drafts (%s)')),
     526                'private' => array(__('Private'), __('Private posts'), __ngettext_noop('Private (%s)', 'Private (%s)')),
     527            );
     528
     529    $post_stati = apply_filters('post_stati', $post_stati);
     530
     531    $avail_post_stati = get_available_post_statuses('post');
     532
     533    $post_status_q = '';
     534    if ( isset($q['post_status']) && in_array( $q['post_status'], array_keys($post_stati) ) ) {
     535        $post_status_q = '&post_status=' . $q['post_status'];
     536        $post_status_q .= '&perm=readable';
     537    }
     538
     539    if ( 'pending' === $q['post_status'] ) {
     540        $order = 'ASC';
     541        $orderby = 'modified';
     542    } elseif ( 'draft' === $q['post_status'] ) {
     543        $order = 'DESC';
     544        $orderby = 'modified';
     545    } else {
     546        $order = 'DESC';
     547        $orderby = 'date';
     548    }
     549
     550    wp("post_type=post&what_to_show=posts$post_status_q&posts_per_page=15&order=$order&orderby=$orderby");
     551
     552    return array($post_stati, $avail_post_stati);
     553}
     554
     555function get_available_post_mime_types($type = 'attachment') {
     556    global $wpdb;
     557
     558    $types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
     559    return $types;
     560}
     561
     562function wp_edit_attachments_query( $q = false ) {
     563    global $wpdb;
     564    if ( false === $q )
     565        $q = $_GET;
     566    $q['m']   = (int) $q['m'];
     567    $q['cat'] = (int) $q['cat'];
     568    $q['post_type'] = 'attachment';
     569    $q['post_status'] = 'any';
     570    $q['posts_per_page'] = 15;
     571    $post_mime_types = array(   //  array( adj, noun )
     572                'image' => array(__('Images'), __('Manage Images'), __ngettext_noop('Image (%s)', 'Images (%s)')),
     573                'audio' => array(__('Audio'), __('Manage Audio'), __ngettext_noop('Audio (%s)', 'Audio (%s)')),
     574                'video' => array(__('Video'), __('Manage Video'), __ngettext_noop('Video (%s)', 'Video (%s)')),
     575            );
     576    $post_mime_types = apply_filters('post_mime_types', $post_mime_types);
     577
     578    $avail_post_mime_types = get_available_post_mime_types('attachment');
     579
     580    if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) )
     581        unset($q['post_mime_type']);
     582
     583    wp($q);
     584
     585    return array($post_mime_types, $avail_post_mime_types);
     586}
     587
     588function postbox_classes( $id, $page ) {
     589    $current_user = wp_get_current_user();
     590    if ( $closed = get_usermeta( $current_user->ID, 'closedpostboxes_'.$page ) ) {
     591        if ( !is_array( $closed ) ) return '';
     592        return in_array( $id, $closed )? 'if-js-closed' : '';
     593    } else {
     594        if ( 'tagsdiv' == $id || 'categorydiv' == $id ) return '';
     595        else return 'if-js-closed';
     596    }
     597}
     598
     599function get_sample_permalink($id, $title=null, $name = null) {
     600    $post = &get_post($id);
     601    if (!$post->ID) {
     602        return array('', '');
     603    }
     604    $original_status = $post->post_status;
     605    $original_date = $post->post_date;
     606    $original_name = $post->post_name;
     607
     608    // Hack: get_permalink would return ugly permalink for
     609    // drafts, so we will fake, that our post is published
     610    if (in_array($post->post_status, array('draft', 'pending'))) {
     611        $post->post_status = 'publish';
     612        $post->post_date = date('Y-m-d H:i:s');
     613        $post->post_name = sanitize_title($post->post_name? $post->post_name : $post->post_title, $post->ID);
     614    }
     615
     616    // If the user wants to set a new name -- override the current one
     617    // Note: if empty name is supplied -- use the title instead, see #6072
     618    if (!is_null($name)) {
     619        $post->post_name = sanitize_title($name? $name : $title, $post->ID);
     620    }
     621
     622    $permalink = get_permalink($post, true);
     623
     624    // Handle page hierarchy
     625    if ( 'page' == $post->post_type ) {
     626        $uri = get_page_uri($post->ID);
     627        $uri = untrailingslashit($uri);
     628        $uri = strrev( stristr( strrev( $uri ), '/' ) );
     629        $uri = untrailingslashit($uri);
     630        if ( !empty($uri) )
     631            $uri .='/';
     632        $permalink = str_replace('%pagename%', "${uri}%pagename%", $permalink);
     633    }
     634
     635    $permalink = array($permalink, $post->post_name);
     636    $post->post_status = $original_status;
     637    $post->post_date = $original_date;
     638    $post->post_name = $original_name;
     639    $post->post_title = $original_title;
     640    return $permalink;
     641}
     642
     643function get_sample_permalink_html($id, $new_title=null, $new_slug=null) {
     644    $post = &get_post($id);
     645    list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
     646    if (false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%')) {
     647        return '';
     648    }
     649    $title = __('Click to edit this part of the permalink');
     650    if (strlen($post_name) > 30) {
     651        $post_name_abridged = substr($post_name, 0, 14). '&hellip;' . substr($post_name, -14);
     652    } else {
     653        $post_name_abridged = $post_name;
     654    }
     655    $post_name_html = '<span id="editable-post-name" title="'.$title.'">'.$post_name_abridged.'</span><span id="editable-post-name-full">'.$post_name.'</span>';
     656    $display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink);
     657    $return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink">' . $display_link . "</span>\n";
     658    $return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug" onclick="edit_permalink(' . $id . '); return false;">' . __('Edit') . "</a></span>\n";
     659    return $return;
     660}
     661
     662// false: not locked or locked by current user
     663// int: user ID of user with lock
     664function wp_check_post_lock( $post_id ) {
     665    global $current_user;
     666
     667    if ( !$post = get_post( $post_id ) )
     668        return false;
     669
     670    $lock = get_post_meta( $post->ID, '_edit_lock', true );
     671    $last = get_post_meta( $post->ID, '_edit_last', true );
     672
     673    $time_window = apply_filters( 'wp_check_post_lock_window', AUTOSAVE_INTERVAL * 2 );
     674
     675    if ( $lock && $lock > time() - $time_window && $last != $current_user->ID )
     676        return $last;
     677    return false;
     678}
     679
     680function wp_set_post_lock( $post_id ) {
     681    global $current_user;
     682    if ( !$post = get_post( $post_id ) )
     683        return false;
     684    if ( !$current_user || !$current_user->ID )
     685        return false;
     686
     687    $now = time();
     688
     689    if ( !add_post_meta( $post->ID, '_edit_lock', $now, true ) )
     690        update_post_meta( $post->ID, '_edit_lock', $now );
     691    if ( !add_post_meta( $post->ID, '_edit_last', $current_user->ID, true ) )
     692        update_post_meta( $post->ID, '_edit_last', $current_user->ID );
     693}
     694
    471695?>
Note: See TracChangeset for help on using the changeset viewer.