本文整理汇总了PHP中stick_post函数的典型用法代码示例。如果您正苦于以下问题:PHP stick_post函数的具体用法?PHP stick_post怎么用?PHP stick_post使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了stick_post函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: toggle_sticky_state
public function toggle_sticky_state($sticky)
{
if (!$this->validate_request()) {
exit;
}
$success = false;
$res = array();
if (isset($_GET['note_id'])) {
$note_id = (int) $_GET['note_id'];
$note_post = get_post($note_id);
if ($note_post && $note_post->post_type == $this->note_post_type) {
if ($sticky) {
stick_post($note_id);
} else {
unstick_post($note_id);
}
$success = true;
$note_obj = new USIN_Note($note_id);
$user_id = $note_obj->get_note_user();
$all_notes = USIN_Note::get_all($user_id);
$res['notes'] = $all_notes;
}
}
$res['success'] = $success;
echo json_encode($res);
exit;
}
示例2: wpSetUpBeforeClass
public static function wpSetUpBeforeClass($factory)
{
// Set post times to get a reliable order.
$now = time();
for ($i = 0; $i <= 22; $i++) {
$post_date = date('Y-m-d H:i:s', $now - 10 * $i);
self::$posts[$i] = $factory->post->create(array('post_date' => $post_date));
}
stick_post(self::$posts[2]);
stick_post(self::$posts[14]);
stick_post(self::$posts[8]);
}
示例3: testBlogPosts
function testBlogPosts()
{
$pids = $this->factory->post->create_many(7);
$pid = $this->factory->post->create();
stick_post($pid);
$pid = $this->factory->post->create();
stick_post($pid);
$pid = $this->factory->post->create();
stick_post($pid);
$posts = Blades_Home::get_blog_posts();
$this->assertEquals(6, count($posts));
}
示例4: setUpBeforeClass
public static function setUpBeforeClass()
{
$f = new WP_UnitTest_Factory();
// Set post times to get a reliable order.
$now = time();
for ($i = 0; $i <= 22; $i++) {
$post_date = date('Y-m-d H:i:s', $now - 10 * $i);
self::$posts[$i] = $f->post->create(array('post_date' => $post_date));
}
stick_post(self::$posts[2]);
stick_post(self::$posts[14]);
stick_post(self::$posts[8]);
self::commit_transaction();
}
示例5: process_posts
/**
* Create new posts based on import information
*
* Posts marked as having a parent which doesn't exist will become top level items.
* Doesn't create a new post if: the post type doesn't exist, the given post ID
* is already noted as imported or a post with the same title and date already exists.
* Note that new/updated terms, comments and meta are imported for the last of the above.
*/
function process_posts()
{
foreach ($this->posts as $post) {
if (!post_type_exists($post['post_type'])) {
printf(__('Failed to import “%s”: Invalid post type %s', 'radium'), esc_html($post['post_title']), esc_html($post['post_type']));
echo '<br />';
continue;
}
if (isset($this->processed_posts[$post['post_id']]) && !empty($post['post_id'])) {
continue;
}
if ($post['status'] == 'auto-draft') {
continue;
}
if ('nav_menu_item' == $post['post_type']) {
$this->process_menu_item($post);
continue;
}
$post_type_object = get_post_type_object($post['post_type']);
$post_exists = post_exists($post['post_title'], '', $post['post_date']);
if ($post_exists && get_post_type($post_exists) == $post['post_type']) {
printf(__('%s “%s” already exists.', 'radium'), $post_type_object->labels->singular_name, esc_html($post['post_title']));
echo '<br />';
$comment_post_ID = $post_id = $post_exists;
} else {
$post_parent = (int) $post['post_parent'];
if ($post_parent) {
// if we already know the parent, map it to the new local ID
if (isset($this->processed_posts[$post_parent])) {
$post_parent = $this->processed_posts[$post_parent];
// otherwise record the parent for later
} else {
$this->post_orphans[intval($post['post_id'])] = $post_parent;
$post_parent = 0;
}
}
// map the post author
$author = sanitize_user($post['post_author'], true);
if (isset($this->author_mapping[$author])) {
$author = $this->author_mapping[$author];
} else {
$author = (int) get_current_user_id();
}
$postdata = array('import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'], 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'], 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'], 'post_status' => $post['status'], 'post_name' => $post['post_name'], 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'], 'post_type' => $post['post_type'], 'post_password' => $post['post_password']);
if ('attachment' == $postdata['post_type']) {
$remote_url = !empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
// try to use _wp_attached file for upload folder placement to ensure the same location as the export site
// e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
$postdata['upload_date'] = $post['post_date'];
if (isset($post['postmeta'])) {
foreach ($post['postmeta'] as $meta) {
if ($meta['key'] == '_wp_attached_file') {
if (preg_match('%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches)) {
$postdata['upload_date'] = $matches[0];
}
break;
}
}
}
$comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url);
} else {
$comment_post_ID = $post_id = wp_insert_post($postdata, true);
}
if (is_wp_error($post_id)) {
printf(__('Failed to import %s “%s”', 'radium'), $post_type_object->labels->singular_name, esc_html($post['post_title']));
if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) {
echo ': ' . $post_id->get_error_message();
}
echo '<br />';
continue;
}
if ($post['is_sticky'] == 1) {
stick_post($post_id);
}
}
// map pre-import ID to local ID
$this->processed_posts[intval($post['post_id'])] = (int) $post_id;
// add categories, tags and other terms
if (!empty($post['terms'])) {
$terms_to_set = array();
foreach ($post['terms'] as $term) {
// back compat with WXR 1.0 map 'tag' to 'post_tag'
$taxonomy = 'tag' == $term['domain'] ? 'post_tag' : $term['domain'];
$term_exists = term_exists($term['slug'], $taxonomy);
$term_id = is_array($term_exists) ? $term_exists['term_id'] : $term_exists;
if (!$term_id) {
$t = wp_insert_term($term['name'], $taxonomy, array('slug' => $term['slug']));
if (!is_wp_error($t)) {
$term_id = $t['term_id'];
} else {
printf(__('Failed to import %s %s', 'radium'), esc_html($taxonomy), esc_html($term['name']));
if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) {
//.........这里部分代码省略.........
示例6: callback
/**
* Main AJAX callback
*
* @param object The post fragment object
*/
function callback($post_data)
{
if (!property_exists($post_data, 'postID') || !property_exists($post_data, 'isSticky')) {
self::die_failure('invalid_message', __('Insufficient information provided.', 'o2'));
}
$post = get_post(absint($post_data->postID));
if (!$post) {
self::die_failure('post_not_found', __('Post not found.', 'o2'));
}
if (!current_user_can('edit_post', $post->ID)) {
self::die_failure('cannot_edit_post_sticky', __('You are not allowed to edit this post sticky.', 'o2'));
}
if ($post_data->isSticky) {
stick_post($post->ID);
} else {
unstick_post($post->ID);
}
// Bump the post to make it update in polling
o2_Fragment::bump_post($post->ID);
$retval = array('isSticky' => is_sticky($post->ID));
self::die_success($retval);
}
示例7: bulk_edit_posts
//.........这里部分代码省略.........
if (is_array($post_data['post_category']) && !empty($post_data['post_category'])) {
$new_cats = array_map('absint', $post_data['post_category']);
} else {
unset($post_data['post_category']);
}
}
$tax_input = array();
if (isset($post_data['tax_input'])) {
foreach ($post_data['tax_input'] as $tax_name => $terms) {
if (empty($terms)) {
continue;
}
if (is_taxonomy_hierarchical($tax_name)) {
$tax_input[$tax_name] = array_map('absint', $terms);
} else {
$comma = _x(',', 'tag delimiter');
if (',' !== $comma) {
$terms = str_replace($comma, ',', $terms);
}
$tax_input[$tax_name] = explode(',', trim($terms, " \n\t\r\v,"));
}
}
}
if (isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent'])) {
$pages = $wpdb->get_results("SELECT ID, post_parent FROM {$wpdb->posts} WHERE post_type = 'page'");
$children = array();
for ($i = 0; $i < 50 && $parent > 0; $i++) {
$children[] = $parent;
foreach ($pages as $page) {
if ($page->ID == $parent) {
$parent = $page->post_parent;
break;
}
}
}
}
$updated = $skipped = $locked = array();
$shared_post_data = $post_data;
foreach ($post_IDs as $post_ID) {
// Start with fresh post data with each iteration.
$post_data = $shared_post_data;
$post_type_object = get_post_type_object(get_post_type($post_ID));
if (!isset($post_type_object) || isset($children) && in_array($post_ID, $children) || !current_user_can('edit_post', $post_ID)) {
$skipped[] = $post_ID;
continue;
}
if (wp_check_post_lock($post_ID)) {
$locked[] = $post_ID;
continue;
}
$post = get_post($post_ID);
$tax_names = get_object_taxonomies($post);
foreach ($tax_names as $tax_name) {
$taxonomy_obj = get_taxonomy($tax_name);
if (isset($tax_input[$tax_name]) && current_user_can($taxonomy_obj->cap->assign_terms)) {
$new_terms = $tax_input[$tax_name];
} else {
$new_terms = array();
}
if ($taxonomy_obj->hierarchical) {
$current_terms = (array) wp_get_object_terms($post_ID, $tax_name, array('fields' => 'ids'));
} else {
$current_terms = (array) wp_get_object_terms($post_ID, $tax_name, array('fields' => 'names'));
}
$post_data['tax_input'][$tax_name] = array_merge($current_terms, $new_terms);
}
if (isset($new_cats) && in_array('category', $tax_names)) {
$cats = (array) wp_get_post_categories($post_ID);
$post_data['post_category'] = array_unique(array_merge($cats, $new_cats));
unset($post_data['tax_input']['category']);
}
$post_data['post_type'] = $post->post_type;
$post_data['post_mime_type'] = $post->post_mime_type;
$post_data['guid'] = $post->guid;
foreach (array('comment_status', 'ping_status', 'post_author') as $field) {
if (!isset($post_data[$field])) {
$post_data[$field] = $post->{$field};
}
}
$post_data['ID'] = $post_ID;
$post_data['post_ID'] = $post_ID;
$post_data = _wp_translate_postdata(true, $post_data);
if (is_wp_error($post_data)) {
$skipped[] = $post_ID;
continue;
}
$updated[] = wp_update_post($post_data);
if (isset($post_data['sticky']) && current_user_can($ptype->cap->edit_others_posts)) {
if ('sticky' == $post_data['sticky']) {
stick_post($post_ID);
} else {
unstick_post($post_ID);
}
}
if (isset($post_data['post_format'])) {
set_post_format($post_ID, $post_data['post_format']);
}
}
return array('updated' => $updated, 'skipped' => $skipped, 'locked' => $locked);
}
示例8: bulk_edit_posts
/**
* {@internal Missing Short Description}}
*
* Updates all bulk edited posts/pages, adding (but not removing) tags and
* categories. Skips pages when they would be their own parent or child.
*
* @since unknown
*
* @return array
*/
function bulk_edit_posts($post_data = null)
{
global $wpdb;
if (empty($post_data)) {
$post_data =& $_POST;
}
if (isset($post_data['post_type']) && 'page' == $post_data['post_type']) {
if (!current_user_can('edit_pages')) {
wp_die(__('You are not allowed to edit pages.'));
}
} else {
if (!current_user_can('edit_posts')) {
wp_die(__('You are not allowed to edit posts.'));
}
}
$post_IDs = array_map('intval', (array) $post_data['post']);
$reset = array('post_author', 'post_status', 'post_password', 'post_parent', 'page_template', 'comment_status', 'ping_status', 'keep_private', 'tags_input', 'post_category', 'sticky');
foreach ($reset as $field) {
if (isset($post_data[$field]) && ('' == $post_data[$field] || -1 == $post_data[$field])) {
unset($post_data[$field]);
}
}
if (isset($post_data['post_category'])) {
if (is_array($post_data['post_category']) && !empty($post_data['post_category'])) {
$new_cats = array_map(absint, $post_data['post_category']);
} else {
unset($post_data['post_category']);
}
}
if (isset($post_data['tags_input'])) {
$new_tags = preg_replace('/\\s*,\\s*/', ',', rtrim(trim($post_data['tags_input']), ' ,'));
$new_tags = explode(',', $new_tags);
}
if (isset($post_data['post_parent']) && ($parent = (int) $post_data['post_parent'])) {
$pages = $wpdb->get_results("SELECT ID, post_parent FROM {$wpdb->posts} WHERE post_type = 'page'");
$children = array();
for ($i = 0; $i < 50 && $parent > 0; $i++) {
$children[] = $parent;
foreach ($pages as $page) {
if ($page->ID == $parent) {
$parent = $page->post_parent;
break;
}
}
}
}
$updated = $skipped = $locked = array();
foreach ($post_IDs as $post_ID) {
if (isset($children) && in_array($post_ID, $children)) {
$skipped[] = $post_ID;
continue;
}
if (wp_check_post_lock($post_ID)) {
$locked[] = $post_ID;
continue;
}
if (isset($new_cats)) {
$cats = (array) wp_get_post_categories($post_ID);
$post_data['post_category'] = array_unique(array_merge($cats, $new_cats));
}
if (isset($new_tags)) {
$tags = wp_get_post_tags($post_ID, array('fields' => 'names'));
$post_data['tags_input'] = array_unique(array_merge($tags, $new_tags));
}
$post_data['ID'] = $post_ID;
$updated[] = wp_update_post($post_data);
if (current_user_can('edit_others_posts') && isset($post_data['sticky'])) {
if ('sticky' == $post_data['sticky']) {
stick_post($post_ID);
} else {
unstick_post($post_ID);
}
}
}
return array('updated' => $updated, 'skipped' => $skipped, 'locked' => $locked);
}
示例9: processDataInWP
//.........这里部分代码省略.........
}
}
unset($postauthor);
if ($post_id) {
$uploaded_file_name = $session_arr['uploadedFile'];
$real_file_name = $session_arr['uploaded_csv_name'];
// $version = $session_arr['currentfileversion'];
$action = $data_array['post_type'];
/* $version_arr=array();
$version_arr=explode("(",$uploaded_file_name);
$version_arr=explode(")",$version_arr[1]);
$version=$version_arr[0]; */
$get_imported_feature_image = array();
$get_imported_feature_image = get_option('IMPORTED_FEATURE_IMAGES');
if (is_array($get_imported_feature_image)) {
$imported_feature_img = array_merge($get_imported_feature_image, $imported_feature_img);
} else {
$imported_feature_img = $imported_feature_img;
}
update_option('IMPORTED_FEATURE_IMAGES', $imported_feature_img);
$created_records[$action][] = $post_id;
if ($action == 'post') {
$imported_as = 'Post';
}
if ($action == 'page') {
$imported_as = 'Page';
}
if ($action != 'post' && $action != 'page') {
$imported_as = 'Custom Post';
}
$keyword = $action;
$this->insPostCount++;
if (isset($sticky) && $sticky) {
stick_post($post_id);
}
if (!empty($custom_array)) {
foreach ($custom_array as $custom_key => $custom_value) {
update_post_meta($post_id, $custom_key, $custom_value);
}
}
// Import post formats added by fredrick marks
if (isset($post_format)) {
wp_set_object_terms($post_id, $post_format, 'post_format');
}
//Import SEO Values
if (!empty($seo_custom_array)) {
$this->importSEOfields($seo_custom_array, $post_id);
}
// Create custom taxonomy to post
if (!empty($smack_taxo)) {
foreach ($smack_taxo as $taxo_key => $taxo_value) {
if (!empty($taxo_value)) {
$split_line = explode('|', $taxo_value);
wp_set_object_terms($post_id, $split_line, $taxo_key);
}
}
}
// Create/Add tags to post
if (!empty($tags)) {
$this->detailedLog[$currentLimit]['tags'] = "";
foreach ($tags as $tag_key => $tag_value) {
$this->detailedLog[$currentLimit]['tags'] .= $tag_value . "|";
wp_set_post_tags($post_id, $tag_value);
}
$this->detailedLog[$currentLimit]['tags'] = "<b>" . __('Tags', 'wp-ultimate-csv-importer') . " - </b>" . substr($this->detailedLog[$currentLimit]['tags'], 0, -1);
}
示例10: test_password_transition_unsticky
function test_password_transition_unsticky() {
// when transitioning to private status or adding a post password, post should be un-stuck
$editor_id = $this->make_user_by_role( 'editor' );
$post_id = $this->factory->post->create( array( 'post_author' => $editor_id ) );
stick_post( $post_id );
$post2 = array( 'post_password' => 'foobar', 'sticky' => false );
$result = $this->myxmlrpcserver->wp_editPost( array( 1, 'editor', 'editor', $post_id, $post2 ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertFalse( is_sticky( $post_id ) );
}
示例11: mw_editPost
//.........这里部分代码省略.........
} else {
switch ((int) $content_struct["mt_allow_pings"]) {
case 0:
$ping_status = "closed";
break;
case 1:
$ping_status = "open";
break;
default:
$ping_status = get_option("default_ping_status");
break;
}
}
}
$post_title = $content_struct['title'];
$post_content = $content_struct['description'];
$catnames = $content_struct['categories'];
$post_category = array();
if (is_array($catnames)) {
foreach ($catnames as $cat) {
$post_category[] = get_cat_ID($cat);
}
}
$post_excerpt = $content_struct['mt_excerpt'];
$post_more = $content_struct['mt_text_more'];
$post_status = $publish ? 'publish' : 'draft';
if (isset($content_struct["{$post_type}_status"])) {
switch ($content_struct["{$post_type}_status"]) {
case 'draft':
case 'private':
case 'publish':
$post_status = $content_struct["{$post_type}_status"];
break;
case 'pending':
// Pending is only valid for posts, not pages.
if ($post_type === 'post') {
$post_status = $content_struct["{$post_type}_status"];
}
break;
default:
$post_status = $publish ? 'publish' : 'draft';
break;
}
}
$tags_input = $content_struct['mt_keywords'];
if ('publish' == $post_status) {
if ('page' == $post_type && !current_user_can('publish_pages')) {
return new IXR_Error(401, __('Sorry, you do not have the right to publish this page.'));
} else {
if (!current_user_can('publish_posts')) {
return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
}
}
}
if ($post_more) {
$post_content = $post_content . "<!--more-->" . $post_more;
}
$to_ping = $content_struct['mt_tb_ping_urls'];
if (is_array($to_ping)) {
$to_ping = implode(' ', $to_ping);
}
// Do some timestamp voodoo
if (!empty($content_struct['date_created_gmt'])) {
$dateCreated = str_replace('Z', '', $content_struct['date_created_gmt']->getIso()) . 'Z';
} elseif (!empty($content_struct['dateCreated'])) {
$dateCreated = $content_struct['dateCreated']->getIso();
}
if (!empty($dateCreated)) {
$post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
$post_date_gmt = iso8601_to_datetime($dateCreated, GMT);
} else {
$post_date = $postdata['post_date'];
$post_date_gmt = $postdata['post_date_gmt'];
}
// We've got all the data -- post it:
$newpost = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'post_date', 'post_date_gmt', 'to_ping', 'post_name', 'post_password', 'post_parent', 'menu_order', 'post_author', 'tags_input', 'page_template');
$result = wp_update_post($newpost, true);
if (is_wp_error($result)) {
return new IXR_Error(500, $result->get_error_message());
}
if (!$result) {
return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
}
// Only posts can be sticky
if ($post_type == 'post' && isset($content_struct['sticky'])) {
if ($content_struct['sticky'] == true) {
stick_post($post_ID);
} elseif ($content_struct['sticky'] == false) {
unstick_post($post_ID);
}
}
if (isset($content_struct['custom_fields'])) {
$this->set_custom_fields($post_ID, $content_struct['custom_fields']);
}
// Handle enclosures
$this->add_enclosure_if_new($post_ID, $content_struct['enclosure']);
$this->attach_uploads($ID, $post_content);
logIO('O', "(MW) Edited ! ID: {$post_ID}");
return true;
}
示例12: test_user_without_publish_cannot_affect_sticky_with_edit_post
/**
* If the `edit_post()` method is invoked by a user without publish_posts permission, the sticky status of the post
* should not be changed.
*
* @ticket 24153
*/
function test_user_without_publish_cannot_affect_sticky_with_edit_post() {
// Create a sticky post.
$post = $this->factory->post->create_and_get( array(
'post_title' => 'Will be changed',
'post_content' => 'Will be changed',
) );
stick_post( $post->ID );
// Sanity Check.
$this->assertTrue( is_sticky( $post->ID ) );
// Create a role with edit_others_posts.
add_role( 'grammarian', 'Grammarian', array(
'read' => true,
'edit_posts' => true,
'edit_others_posts' => true,
'edit_published_posts' => true,
) );
$editor_user = $this->factory->user->create( array( 'role' => 'grammarian' ) );
$old_uid = get_current_user_id();
wp_set_current_user( $editor_user );
// Sanity Check.
$this->assertFalse( current_user_can( 'publish_posts' ) );
$this->assertTrue( current_user_can( 'edit_others_posts' ) );
$this->assertTrue( current_user_can( 'edit_published_posts' ) );
// Edit the post - The key 'sticky' is intentionally unset.
$data = array(
'post_ID' => $post->ID,
'post_title' => 'Updated',
'post_content' => 'Updated',
);
edit_post( $data );
// Make sure it's still sticky
$saved_post = get_post( $post->ID );
$this->assertTrue( is_sticky( $saved_post->ID ) );
$this->assertEquals( 'Updated', $saved_post->post_title );
$this->assertEquals( 'Updated', $saved_post->post_content );
// Teardown
wp_set_current_user( $old_uid );
}
示例13: pll_save_post
public function pll_save_post($post_id, $post, $translations)
{
global $wpdb;
// prepare properties to synchronize
foreach (array('comment_status', 'ping_status', 'menu_order', 'post_date') as $property) {
if (in_array($property, $this->options['sync'])) {
$postarr[$property] = $post->{$property};
}
}
if (in_array('post_date', $this->options['sync'])) {
$post_arr['post_date_gmt'] = $post->post_date_gmt;
}
// synchronise terms and metas in translations
foreach ($translations as $lang => $tr_id) {
if (!$tr_id) {
continue;
}
// synchronize terms and metas
$this->copy_post_metas($post_id, $tr_id, $lang, true);
// sticky posts
if (in_array('sticky_posts', $this->options['sync'])) {
isset($_REQUEST['sticky']) ? stick_post($tr_id) : unstick_post($tr_id);
}
// synchronize comment status, ping status, menu order...
if (!empty($postarr)) {
$wpdb->update($wpdb->posts, $postarr, array('ID' => $tr_id));
}
// FIXME: optimize the 2 db update in 1
// post parent
// do not udpate the translation parent if the user set a parent with no translation
if (in_array('post_parent', $this->options['sync'])) {
$post_parent = ($parent_id = wp_get_post_parent_id($post_id)) ? $this->model->get_translation('post', $parent_id, $lang) : 0;
if (!($parent_id && !$post_parent)) {
$wpdb->update($wpdb->posts, array('post_parent' => $post_parent), array('ID' => $tr_id));
}
}
}
}
示例14: update_item
/**
* Updates a single post.
*
* @since 4.7.0
* @access public
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function update_item($request)
{
$id = (int) $request['id'];
$post = get_post($id);
if (empty($id) || empty($post->ID) || $this->post_type !== $post->post_type) {
return new WP_Error('rest_post_invalid_id', __('Invalid post ID.'), array('status' => 404));
}
$post = $this->prepare_item_for_database($request);
if (is_wp_error($post)) {
return $post;
}
// convert the post object to an array, otherwise wp_update_post will expect non-escaped input.
$post_id = wp_update_post(wp_slash((array) $post), true);
if (is_wp_error($post_id)) {
if ('db_update_error' === $post_id->get_error_code()) {
$post_id->add_data(array('status' => 500));
} else {
$post_id->add_data(array('status' => 400));
}
return $post_id;
}
$post = get_post($post_id);
/* This action is documented in lib/endpoints/class-wp-rest-controller.php */
do_action("rest_insert_{$this->post_type}", $post, $request, false);
$schema = $this->get_item_schema();
if (!empty($schema['properties']['format']) && !empty($request['format'])) {
set_post_format($post, $request['format']);
}
if (!empty($schema['properties']['featured_media']) && isset($request['featured_media'])) {
$this->handle_featured_media($request['featured_media'], $post_id);
}
if (!empty($schema['properties']['sticky']) && isset($request['sticky'])) {
if (!empty($request['sticky'])) {
stick_post($post_id);
} else {
unstick_post($post_id);
}
}
if (!empty($schema['properties']['template']) && isset($request['template'])) {
$this->handle_template($request['template'], $post->ID);
}
$terms_update = $this->handle_terms($post->ID, $request);
if (is_wp_error($terms_update)) {
return $terms_update;
}
if (!empty($schema['properties']['meta']) && isset($request['meta'])) {
$meta_update = $this->meta->update_value($request['meta'], $post->ID);
if (is_wp_error($meta_update)) {
return $meta_update;
}
}
$post = get_post($post_id);
$fields_update = $this->update_additional_fields_for_object($post, $request);
if (is_wp_error($fields_update)) {
return $fields_update;
}
$request->set_param('context', 'edit');
$response = $this->prepare_item_for_response($post, $request);
return rest_ensure_response($response);
}
示例15: edit_post
//.........这里部分代码省略.........
$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;
}
开发者ID:Esleelkartea,项目名称:asociacion-tecnicos-artes-escenicas-ATAE-,代码行数:101,代码来源:wordpress-wiki-plugin.php