/**
  * Update an existing post with values provided in $_POST.
  *
  * @since 1.5.0
  *
  * @param array $post_data Optional.
  * @return int Post ID.
  */
 function edit_post($post_data = null)
 {
     if (empty($post_data)) {
         $post_data =& $_POST;
     }
     $post_ID = (int) $post_data['post_ID'];
     $ptype = get_post_type_object($post_data['post_type']);
     if (!current_user_can($ptype->cap->edit_post, $post_ID)) {
         if ('page' == $post_data['post_type']) {
             wp_die(__('You are not allowed to edit this page.'));
         } else {
             wp_die(__('You are not allowed to edit this post.'));
         }
     }
     // Autosave shouldn't save too soon after a real save
     if ('autosave' == $post_data['action']) {
         $post =& get_post($post_ID);
         $now = time();
         $then = strtotime($post->post_date_gmt . ' +0000');
         $delta = AUTOSAVE_INTERVAL / 2;
         if ($now - $then < $delta) {
             return $post_ID;
         }
     }
     $post_data = $this->_translate_postdata(true, $post_data);
     $post_data['post_status'] = 'publish';
     if (is_wp_error($post_data)) {
         wp_die($post_data->get_error_message());
     }
     if ('autosave' != $post_data['action'] && 'auto-draft' == $post_data['post_status']) {
         $post_data['post_status'] = 'draft';
     }
     if (isset($post_data['visibility'])) {
         switch ($post_data['visibility']) {
             case 'public':
                 $post_data['post_password'] = '';
                 break;
             case 'password':
                 unset($post_data['sticky']);
                 break;
             case 'private':
                 $post_data['post_status'] = 'private';
                 $post_data['post_password'] = '';
                 unset($post_data['sticky']);
                 break;
         }
     }
     // Post Formats
     if (current_theme_supports('post-formats') && isset($post_data['post_format'])) {
         $formats = get_theme_support('post-formats');
         if (is_array($formats)) {
             $formats = $formats[0];
             if (in_array($post_data['post_format'], $formats)) {
                 set_post_format($post_ID, $post_data['post_format']);
             } elseif ('0' == $post_data['post_format']) {
                 set_post_format($post_ID, false);
             }
         }
     }
     // print_r($post_data); exit();
     // Meta Stuff
     if (isset($post_data['meta']) && $post_data['meta']) {
         foreach ($post_data['meta'] as $key => $value) {
             if (!($meta = get_post_meta_by_id($key))) {
                 continue;
             }
             if ($meta->post_id != $post_ID) {
                 continue;
             }
             update_meta($key, $value['key'], $value['value']);
         }
     }
     if (isset($post_data['deletemeta']) && $post_data['deletemeta']) {
         foreach ($post_data['deletemeta'] as $key => $value) {
             if (!($meta = get_post_meta_by_id($key))) {
                 continue;
             }
             if ($meta->post_id != $post_ID) {
                 continue;
             }
             delete_meta($key);
         }
     }
     // add_meta( $post_ID );
     update_post_meta($post_ID, '_edit_last', $GLOBALS['current_user']->ID);
     wp_update_post($post_data);
     // Reunite any orphaned attachments with their parent
     if (!($draft_ids = get_user_option('autosave_draft_ids'))) {
         $draft_ids = array();
     }
     if ($draft_temp_id = (int) array_search($post_ID, $draft_ids)) {
         _relocate_children($draft_temp_id, $post_ID);
     }
     $this->set_post_lock($post_ID, $GLOBALS['current_user']->ID);
     if (current_user_can($ptype->cap->edit_others_posts)) {
         if (!empty($post_data['sticky'])) {
             stick_post($post_ID);
         } else {
             unstick_post($post_ID);
         }
     }
     return $post_ID;
 }
예제 #2
0
파일: post.php 프로젝트: schr/wordpress
/**
 * Creates a new post from the "Write Post" form using $_POST information.
 *
 * @since unknown
 *
 * @return unknown
 */
function wp_write_post()
{
    global $user_ID;
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_pages')) {
            return new WP_Error('edit_pages', __('You are not allowed to create pages on this blog.'));
        }
    } else {
        if (!current_user_can('edit_posts')) {
            return new WP_Error('edit_posts', __('You are not allowed to create posts or drafts on this blog.'));
        }
    }
    // Check for autosave collisions
    $temp_id = false;
    if (isset($_POST['temp_ID'])) {
        $temp_id = (int) $_POST['temp_ID'];
        if (!($draft_ids = get_user_option('autosave_draft_ids'))) {
            $draft_ids = array();
        }
        foreach ($draft_ids as $temp => $real) {
            if (time() + $temp > 86400) {
                // 1 day: $temp is equal to -1 * time( then )
                unset($draft_ids[$temp]);
            }
        }
        if (isset($draft_ids[$temp_id])) {
            // Edit, don't write
            $_POST['post_ID'] = $draft_ids[$temp_id];
            unset($_POST['temp_ID']);
            update_user_option($user_ID, 'autosave_draft_ids', $draft_ids);
            return edit_post();
        }
    }
    $translated = _wp_translate_postdata(false);
    if (is_wp_error($translated)) {
        return $translated;
    }
    if (isset($_POST['visibility'])) {
        switch ($_POST['visibility']) {
            case 'public':
                $_POST['post_password'] = '';
                break;
            case 'password':
                unset($_POST['sticky']);
                break;
            case 'private':
                $_POST['post_status'] = 'private';
                $_POST['post_password'] = '';
                unset($_POST['sticky']);
                break;
        }
    }
    // Create the post.
    $post_ID = wp_insert_post($_POST);
    if (is_wp_error($post_ID)) {
        return $post_ID;
    }
    if (empty($post_ID)) {
        return 0;
    }
    add_meta($post_ID);
    // Reunite any orphaned attachments with their parent
    if (!($draft_ids = get_user_option('autosave_draft_ids'))) {
        $draft_ids = array();
    }
    if ($draft_temp_id = (int) array_search($post_ID, $draft_ids)) {
        _relocate_children($draft_temp_id, $post_ID);
    }
    if ($temp_id && $temp_id != $draft_temp_id) {
        _relocate_children($temp_id, $post_ID);
    }
    // Update autosave collision detection
    if ($temp_id) {
        $draft_ids[$temp_id] = $post_ID;
        update_user_option($user_ID, 'autosave_draft_ids', $draft_ids);
    }
    // Now that we have an ID we can fix any attachment anchor hrefs
    _fix_attachment_links($post_ID);
    wp_set_post_lock($post_ID, $GLOBALS['current_user']->ID);
    return $post_ID;
}
예제 #3
0
function wp_write_post() {
	global $user_ID;

	if ( 'page' == $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_pages' ) )
			return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this blog.' ) );
	} else {
		if ( !current_user_can( 'edit_posts' ) )
			return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this blog.' ) );
	}


	// Check for autosave collisions
	$temp_id = false;
	if ( isset($_POST['temp_ID']) ) {
		$temp_id = (int) $_POST['temp_ID'];
		if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
			$draft_ids = array();
		foreach ( $draft_ids as $temp => $real )
			if ( time() + $temp > 86400 ) // 1 day: $temp is equal to -1 * time( then )
				unset($draft_ids[$temp]);

		if ( isset($draft_ids[$temp_id]) ) { // Edit, don't write
			$_POST['post_ID'] = $draft_ids[$temp_id];
			unset($_POST['temp_ID']);
			update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
			return edit_post();
		}
	}

	// Rename.
	$_POST['post_content'] = $_POST['content'];
	$_POST['post_excerpt'] = $_POST['excerpt'];
	$_POST['post_parent'] = isset($_POST['parent_id'])? $_POST['parent_id'] : '';
	$_POST['to_ping'] = $_POST['trackback_url'];

	if (!empty ( $_POST['post_author_override'] ) ) {
		$_POST['post_author'] = (int) $_POST['post_author_override'];
	} else {
		if (!empty ( $_POST['post_author'] ) ) {
			$_POST['post_author'] = (int) $_POST['post_author'];
		} else {
			$_POST['post_author'] = (int) $_POST['user_ID'];
		}

	}

	if ( $_POST['post_author'] != $_POST['user_ID'] ) {
		if ( 'page' == $_POST['post_type'] ) {
			if ( !current_user_can( 'edit_others_pages' ) )
				return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
		} else {
			if ( !current_user_can( 'edit_others_posts' ) )
				return new WP_Error( 'edit_others_posts', __( 'You are not allowed to post as this user.' ) );

		}
	}

	// What to do based on which button they pressed
	if ( isset($_POST['saveasdraft']) && '' != $_POST['saveasdraft'] )
		$_POST['post_status'] = 'draft';
	if ( isset($_POST['saveasprivate']) && '' != $_POST['saveasprivate'] )
		$_POST['post_status'] = 'private';
	if ( isset($_POST['publish']) && ( '' != $_POST['publish'] ) && ( $_POST['post_status'] != 'private' ) )
		$_POST['post_status'] = 'publish';
	if ( isset($_POST['advanced']) && '' != $_POST['advanced'] )
		$_POST['post_status'] = 'draft';

	if ( 'page' == $_POST['post_type'] ) {
		if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_pages' ) )
			$_POST['post_status'] = 'pending';
	} else {
		if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_posts' ) )
			$_POST['post_status'] = 'pending';
	}

	if (!isset( $_POST['comment_status'] ))
		$_POST['comment_status'] = 'closed';

	if (!isset( $_POST['ping_status'] ))
		$_POST['ping_status'] = 'closed';

	foreach ( array ('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
		if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
			$_POST['edit_date'] = '1';
			break;
		}
	}

	if (!empty ( $_POST['edit_date'] ) ) {
		$aa = $_POST['aa'];
		$mm = $_POST['mm'];
		$jj = $_POST['jj'];
		$hh = $_POST['hh'];
		$mn = $_POST['mn'];
		$ss = $_POST['ss'];
		$jj = ($jj > 31 ) ? 31 : $jj;
		$hh = ($hh > 23 ) ? $hh -24 : $hh;
		$mn = ($mn > 59 ) ? $mn -60 : $mn;
		$ss = ($ss > 59 ) ? $ss -60 : $ss;
		$_POST['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
		$_POST['post_date_gmt'] = get_gmt_from_date( $_POST['post_date'] );
	}

	// Create the post.
	$post_ID = wp_insert_post( $_POST );
	if ( is_wp_error( $post_ID ) )
		return $post_ID;

	if ( empty($post_ID) )
		return 0;

	add_meta( $post_ID );

	// Reunite any orphaned attachments with their parent
	if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )
		$draft_ids = array();
	if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )
		_relocate_children( $draft_temp_id, $post_ID );
	if ( $temp_id && $temp_id != $draft_temp_id )
		_relocate_children( $temp_id, $post_ID );

	// Update autosave collision detection
	if ( $temp_id ) {
		$draft_ids[$temp_id] = $post_ID;
		update_user_option( $user_ID, 'autosave_draft_ids', $draft_ids );
	}

	// Now that we have an ID we can fix any attachment anchor hrefs
	_fix_attachment_links( $post_ID );

	wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );

	return $post_ID;
}