源代碼
File: wp-admin/includes/post.php
function _wp_translate_postdata( $update = false, $post_data = null ) {
if ( empty($post_data) )
$post_data = &$_POST;
if ( $update )
$post_data['ID'] = (int) $post_data['post_ID'];
$ptype = get_post_type_object( $post_data['post_type'] );
if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) {
if ( 'page' == $post_data['post_type'] )
return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
else
return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
} elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) {
if ( 'page' == $post_data['post_type'] )
return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
else
return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
}
if ( isset( $post_data['content'] ) )
$post_data['post_content'] = $post_data['content'];
if ( isset( $post_data['excerpt'] ) )
$post_data['post_excerpt'] = $post_data['excerpt'];
if ( isset( $post_data['parent_id'] ) )
$post_data['post_parent'] = (int) $post_data['parent_id'];
if ( isset($post_data['trackback_url']) )
$post_data['to_ping'] = $post_data['trackback_url'];
$post_data['user_ID'] = get_current_user_id();
if (!empty ( $post_data['post_author_override'] ) ) {
$post_data['post_author'] = (int) $post_data['post_author_override'];
} else {
if (!empty ( $post_data['post_author'] ) ) {
$post_data['post_author'] = (int) $post_data['post_author'];
} else {
$post_data['post_author'] = (int) $post_data['user_ID'];
}
}
if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] )
&& ! current_user_can( $ptype->cap->edit_others_posts ) ) {
if ( $update ) {
if ( 'page' == $post_data['post_type'] )
return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
else
return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
} else {
if ( 'page' == $post_data['post_type'] )
return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
else
return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
}
}
if ( ! empty( $post_data['post_status'] ) ) {
$post_data['post_status'] = sanitize_key( $post_data['post_status'] );
// No longer an auto-draft
if ( 'auto-draft' === $post_data['post_status'] ) {
$post_data['post_status'] = 'draft';
}
if ( ! get_post_status_object( $post_data['post_status'] ) ) {
unset( $post_data['post_status'] );
}
}
// What to do based on which button they pressed
if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
$post_data['post_status'] = 'draft';
if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
$post_data['post_status'] = 'private';
if ( isset($post_data['publish']) && ( '' != $post_data['publish'] ) && ( !isset($post_data['post_status']) || $post_data['post_status'] != 'private' ) )
$post_data['post_status'] = 'publish';
if ( isset($post_data['advanced']) && '' != $post_data['advanced'] )
$post_data['post_status'] = 'draft';
if ( isset($post_data['pending']) && '' != $post_data['pending'] )
$post_data['post_status'] = 'pending';
if ( isset( $post_data['ID'] ) )
$post_id = $post_data['ID'];
else
$post_id = false;
$previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
if ( isset( $post_data['post_status'] ) && 'private' == $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) {
$post_data['post_status'] = $previous_status ? $previous_status : 'pending';
}
$published_statuses = array( 'publish', 'future' );
// Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
// Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
if ( isset($post_data['post_status']) && (in_array( $post_data['post_status'], $published_statuses ) && !current_user_can( $ptype->cap->publish_posts )) )
if ( ! in_array( $previous_status, $published_statuses ) || !current_user_can( 'edit_post', $post_id ) )
$post_data['post_status'] = 'pending';
if ( ! isset( $post_data['post_status'] ) ) {
$post_data['post_status'] = 'auto-draft' === $previous_status ? 'draft' : $previous_status;
}
if ( isset( $post_data['post_password'] ) && ! current_user_can( $ptype->cap->publish_posts ) ) {
unset( $post_data['post_password'] );
}
if (!isset( $post_data['comment_status'] ))
$post_data['comment_status'] = 'closed';
if (!isset( $post_data['ping_status'] ))
$post_data['ping_status'] = 'closed';
foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
if ( !empty( $post_data['hidden_' . $timeunit] ) && $post_data['hidden_' . $timeunit] != $post_data[$timeunit] ) {
$post_data['edit_date'] = '1';
break;
}
}
if ( !empty( $post_data['edit_date'] ) ) {
$aa = $post_data['aa'];
$mm = $post_data['mm'];
$jj = $post_data['jj'];
$hh = $post_data['hh'];
$mn = $post_data['mn'];
$ss = $post_data['ss'];
$aa = ($aa <= 0 ) ? date('Y') : $aa;
$mm = ($mm <= 0 ) ? date('n') : $mm;
$jj = ($jj > 31 ) ? 31 : $jj;
$jj = ($jj <= 0 ) ? date('j') : $jj;
$hh = ($hh > 23 ) ? $hh -24 : $hh;
$mn = ($mn > 59 ) ? $mn -60 : $mn;
$ss = ($ss > 59 ) ? $ss -60 : $ss;
$post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
$valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
if ( !$valid_date ) {
return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
}
$post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
}
if ( isset( $post_data['post_category'] ) ) {
$category_object = get_taxonomy( 'category' );
if ( ! current_user_can( $category_object->cap->assign_terms ) ) {
unset( $post_data['post_category'] );
}
}
return $post_data;
}
更新日志
Version | 描述 |
---|---|
2.6.0 | Introduced. |
在WordPress中,_wp_translate_postdata()
是一個內(nèi)部函數(shù),用于將表單提交的數(shù)據(jù)轉(zhuǎn)換為WordPress帖子(posts)和頁面(pages)的正確格式。這個函數(shù)主要在WordPress的 wp_insert_post()
函數(shù)中使用,以準(zhǔn)備和過濾通過 $_POST
變量提交的數(shù)據(jù)。
這個函數(shù)不是公開文檔化的,通常不應(yīng)該在主題或插件代碼中直接使用,因為它可能會在WordPress的未來版本中發(fā)生變化。然而,如果你需要了解其用法,以下是該函數(shù)的基本結(jié)構(gòu):
_wp_translate_postdata( $post_type = 'post' );
參數(shù)解釋如下:
$post_type
:可選參數(shù),表示帖子類型。默認(rèn)為 ‘post’,但也可以是 ‘page’ 或其他自定義帖子類型。
函數(shù)執(zhí)行以下操作:- 確保提交的數(shù)據(jù)是有效的。
- 將提交的數(shù)據(jù)轉(zhuǎn)換為WordPress帖子或頁面的格式。
- 執(zhí)行必要的數(shù)據(jù)過濾和消毒。
由于_wp_translate_postdata()
是一個內(nèi)部函數(shù),它的使用通常是在WordPress核心處理帖子提交時隱式發(fā)生的。以下是一個示例,展示了在處理帖子提交時可能會調(diào)用此函數(shù)的上下文:
if ( isset( $_POST['post_type'] ) ) {
$post_type = $_POST['post_type'];
} else {
$post_type = 'post';
}
// 在處理帖子提交之前,確保已經(jīng)做了安全檢查
if ( isset( $_POST['post_ID'] ) ) {
$post_id = absint( $_POST['post_ID'] );
}
// 確保當(dāng)前用戶有權(quán)限編輯或創(chuàng)建帖子
if ( current_user_can( 'edit_post', $post_id ) ) {
// 調(diào)用內(nèi)部函數(shù)來轉(zhuǎn)換和過濾提交的數(shù)據(jù)
_wp_translate_postdata( $post_type );
// 這里可以添加更多的代碼來處理轉(zhuǎn)換后的數(shù)據(jù),例如保存帖子
}
請記住,直接使用 _wp_translate_postdata()
函數(shù)是不推薦的,因為它可能會在沒有通知的情況下在未來的WordPress版本中發(fā)生變化。如果你正在編寫插件或主題,并需要處理帖子提交,應(yīng)該使用WordPress提供的高級函數(shù),如 wp_insert_post()
和 wp_update_post()
。
如果你確實需要處理提交的數(shù)據(jù),以下是一個更安全的做法:
// 準(zhǔn)備要插入或更新的帖子數(shù)組
$postarr = array(
'ID' => $post_id, // 如果是更新已有帖子,提供帖子ID
'post_title' => $_POST['post_title'],
'post_content' => $_POST['post_content'],
// ...其他帖子字段
);
// 使用 wp_insert_post() 或 wp_update_post() 來處理帖子
if ( $post_id ) {
// 更新帖子
$post_id = wp_update_post( $postarr );
} else {
// 插入新帖子
$post_id = wp_insert_post( $postarr );
}
在上述代碼中,不需要直接調(diào)用 _wp_translate_postdata()
,因為 wp_insert_post()
和 wp_update_post()
已經(jīng)處理了數(shù)據(jù)轉(zhuǎn)換和過濾。
未經(jīng)允許不得轉(zhuǎn)載:445IT之家 » WordPress函數(shù)_wp_translate_postdata()用法