/** * @param $post_id */ public function lock_post($post_id) { if (!function_exists('wp_set_post_lock')) { require_once ABSPATH . 'wp-admin/includes/post.php'; } wp_set_post_lock($post_id); }
/** * Test autosaving a locked post * @return void */ public function test_autosave_locked_post() { // Lock the post to another user $another_user_id = $this->factory->user->create(array('role' => 'editor')); wp_set_current_user($another_user_id); wp_set_post_lock($this->_post->ID); wp_set_current_user($this->user_id); // Ensure post is locked $this->assertEquals($another_user_id, wp_check_post_lock($this->_post->ID)); // Set up the $_POST request $md5 = md5(uniqid()); $_POST = array('action' => 'heartbeat', '_nonce' => wp_create_nonce('heartbeat-nonce'), 'data' => array('wp_autosave' => array('post_id' => $this->_post->ID, '_wpnonce' => wp_create_nonce('update-post_' . $this->_post->ID), 'post_content' => $this->_post->post_content . PHP_EOL . $md5, 'post_type' => 'post'))); // Make the request try { $this->_handleAjax('heartbeat'); } catch (WPAjaxDieContinueException $e) { unset($e); } $response = json_decode($this->_last_response, true); // Ensure everything is correct $this->assertNotEmpty($response['wp_autosave']); $this->assertTrue($response['wp_autosave']['success']); // Check that the original post was NOT edited $post = get_post($this->_post->ID); $this->assertFalse(strpos($post->post_content, $md5)); // Check if the autosave post was created $autosave = wp_get_post_autosave($this->_post->ID, get_current_user_id()); $this->assertNotEmpty($autosave); $this->assertGreaterThanOrEqual(0, strpos($autosave->post_content, $md5)); }
protected function handle_locking($post_id) { $last_user = wp_check_post_lock($post_id); if ($last_user) { $message = __('Error: %s is currently editing this.', 'front-end-editor'); $message = sprintf($message, esc_html(get_userdata($last_user)->display_name)); throw new Exception($message); } wp_set_post_lock($post_id); }
private function edit() { global $title, $post_ID, $p, $post, $post_referredby; $title = __('Edit Page'); $page_ID = $post_ID = $p = (int) $_GET['post']; $post = $this->get_page($post_ID); if (current_user_can('edit_page', $page_ID)) { if ($last = wp_check_post_lock($post->ID)) { $last_user = get_userdata($last); $last_user_name = $last_user ? $last_user->display_name : __('Somebody'); $message = sprintf(__('Warning: %s is currently editing this page'), esc_html($last_user_name)); $message = '<p><font color="red">' . $message . '</font></p>'; add_action('admin_notices', create_function('', "echo '{$message}';")); } else { wp_set_post_lock($post->ID); } } else { $this->base->ks_die(__('You are not allowed to edit this page.')); // exit; } $post_referredby = $this->sendback; include dirname(__FILE__) . '/edit-page-form.php'; }
if (is_wp_error($revision_id)) { $id = $revision_id; } else { $id = $post->ID; } } $data = $message; } else { if (!empty($_POST['auto_draft'])) { $id = 0; } else { $id = $post->ID; } } if ($do_lock && empty($_POST['auto_draft']) && $id && is_numeric($id)) { $lock_result = wp_set_post_lock($id); $supplemental['active-post-lock'] = implode(':', $lock_result); } if ($nonce_age == 2) { $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave'); $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink'); $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink'); $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes'); $supplemental['replace-_ajax_linking_nonce'] = wp_create_nonce('internal-linking'); if ($id) { if ($_POST['post_type'] == 'post') { $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id); } elseif ($_POST['post_type'] == 'page') { $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id); } }
public function front_end_editor_shortcodes($attr) { global $wp, $current_screen, $wp_meta_boxes, $post; $is_bac = $this->is_bac(); $output = ''; /** * Start Checking the Conditional needed to render editor * Define Variable needed for use in whole function * * */ if (!is_user_logged_in()) { if ($is_bac === true) { wp_safe_redirect(bon_accounts()->my_account_url()); } else { if (is_woocommerce_activated()) { wp_safe_redirect(get_permalink(wc_get_page_id('myaccount'))); } } } else { if (!$this->is_edit()) { return; } $object_id = $this->get_post_to_edit(); if (!$object_id) { bon_error_notice()->add('invalid_post', __('You attempted to edit an item that doesn’t exist. Perhaps it was deleted?'), 'error'); return; } $post_object = get_post($this->get_post_to_edit()); setup_postdata($GLOBALS['post'] =& $post_object); $current_post_type = get_post_type($object_id); if (!$post_object) { bon_error_notice()->add('invalid_post', __('You attempted to edit an item that doesn’t exist. Perhaps it was deleted?'), 'error'); return; } if (!current_user_can('edit_post', $object_id)) { bon_error_notice()->add('permission_denied', __('You are not allowed to edit this item.'), 'error'); return; } if (!post_type_supports($post_object->post_type, 'front-end-editor')) { bon_error_notice()->add('unsupported_posttype', __('The post type assigned is not supporting front end post', 'bon'), 'error'); } $form_extra = ''; $notice = false; if ($post_object->post_status === 'auto-draft') { $post_object->post_title = ''; $post_object->comment_status = get_option('default_comment_status'); $post_object->ping_status = get_option('default_ping_status'); $autosave = false; $form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />"; } else { $autosave = wp_get_post_autosave($object_id); } $form_action = 'editpost'; $nonce_action = 'update-post_' . $object_id; $form_extra .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr($object_id) . "' />"; $content_css = array(trailingslashit(get_stylesheet_directory_uri()) . 'assets/css/editor-styles.css', trailingslashit(includes_url()) . 'css/dashicons.min.css', trailingslashit(includes_url()) . 'js/mediaelement/mediaelementplayer.min.css', trailingslashit(includes_url()) . 'js/mediaelement/wp-mediaelement.css', trailingslashit(includes_url()) . 'js/tinymce/skins/wordpress/wp-content.css', trailingslashit(includes_url()) . 'css/editor.min.css'); $content_css = join(',', array_map('esc_url', array_unique($content_css))); $args = array('post_ID' => $object_id, 'post_type' => $current_post_type, 'user_ID' => get_current_user_id(), 'post' => $post_object, 'post_type_object' => get_post_type_object($current_post_type), 'autosave' => $autosave, 'form_extra' => $form_extra, 'form_action' => $form_action, 'nonce_action' => $nonce_action, 'editor_settings' => array('dfw' => true, 'drag_drop_upload' => true, 'tabfocus_elements' => 'insert-media-button, save-post', 'editor_height' => 360, 'tinymce' => array('resize' => false, 'add_unload_trigger' => false, 'content_css' => $content_css))); ob_start(); bon_get_template('posts/editor.php', $args); $args['editor'] = ob_get_clean(); unset($args['editor_settings']); set_current_screen($current_post_type); $current_screen->set_parentage('edit.php?post_type=' . $current_post_type); if (!wp_check_post_lock($object_id)) { $args['active_post_lock'] = wp_set_post_lock($object_id); } $messages = $this->get_wp_messages($post_object); $message = false; if (isset($_GET['message'])) { $_GET['message'] = absint($_GET['message']); if (isset($messages[$current_post_type][$_GET['message']])) { $message = $messages[$current_post_type][$_GET['message']]; } elseif (!isset($messages[$current_post_type]) && isset($messages['post'][$_GET['message']])) { $message = $messages['post'][$_GET['message']]; } } // Detect if there exists an autosave newer than the post and if that autosave is different than the post if ($autosave && mysql2date('U', $autosave->post_modified_gmt, false) > mysql2date('U', $post_object->post_modified_gmt, false)) { foreach (_wp_post_revision_fields() as $autosave_field => $_autosave_field) { if (normalize_whitespace($autosave->{$autosave_field}) != normalize_whitespace($post_object->{$autosave_field})) { bon_error_notice()->add('autosave_exists', sprintf(__('There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>'), get_edit_post_link($autosave->ID)), 'notice'); break; } } // If this autosave isn't different from the current post, begone. if (!$notice) { wp_delete_post_revision($autosave->ID); } unset($autosave_field, $_autosave_field); } bon_get_template('posts/post.php', $args); unset($GLOBALS['current_screen']); wp_reset_postdata(); } }
/** * Check lock status on the New/Edit Post screen and refresh the lock * * @since 3.6.0 * * @param array $response The Heartbeat response. * @param array $data The $_POST data sent. * @param string $screen_id The screen id. * @return array The Heartbeat response. */ function wp_refresh_post_lock($response, $data, $screen_id) { if (array_key_exists('wp-refresh-post-lock', $data)) { $received = $data['wp-refresh-post-lock']; $send = array(); if (!($post_id = absint($received['post_id']))) { return $response; } if (!current_user_can('edit_post', $post_id)) { return $response; } if (($user_id = wp_check_post_lock($post_id)) && ($user = get_userdata($user_id))) { $error = array('text' => sprintf(__('%s has taken over and is currently editing.'), $user->display_name)); if ($avatar = get_avatar($user->ID, 64)) { if (preg_match("|src='([^']+)'|", $avatar, $matches)) { $error['avatar_src'] = $matches[1]; } } $send['lock_error'] = $error; } else { if ($new_lock = wp_set_post_lock($post_id)) { $send['new_lock'] = implode(':', $new_lock); } } $response['wp-refresh-post-lock'] = $send; } return $response; }
/** * Creates a new post from the "Write Post" form using $_POST information. * * @since 2.1.0 * * @global WP_User $current_user * * @return int|WP_Error */ function wp_write_post() { if (isset($_POST['post_type'])) { $ptype = get_post_type_object($_POST['post_type']); } else { $ptype = get_post_type_object('post'); } if (!current_user_can($ptype->cap->edit_posts)) { if ('page' == $ptype->name) { return new WP_Error('edit_pages', __('Sorry, you are not allowed to create pages on this site.')); } else { return new WP_Error('edit_posts', __('Sorry, you are not allowed to create posts or drafts on this site.')); } } $_POST['post_mime_type'] = ''; // Clear out any data in internal vars. unset($_POST['filter']); // Edit don't write if we have a post id. if (isset($_POST['post_ID'])) { return edit_post(); } 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; } } $translated = _wp_translate_postdata(false); if (is_wp_error($translated)) { return $translated; } // 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); add_post_meta($post_ID, '_edit_last', $GLOBALS['current_user']->ID); // Now that we have an ID we can fix any attachment anchor hrefs _fix_attachment_links($post_ID); wp_set_post_lock($post_ID); return $post_ID; }
$post_new_file = "post-new.php"; } elseif ('attachment' == $post_type) { $parent_file = 'upload.php'; $submenu_file = 'upload.php'; $post_new_file = 'media-new.php'; } else { if (isset($post_type_object) && $post_type_object->show_in_menu && $post_type_object->show_in_menu !== true) { $parent_file = $post_type_object->show_in_menu; } else { $parent_file = "edit.php?post_type={$post_type}"; } $submenu_file = "edit.php?post_type={$post_type}"; $post_new_file = "post-new.php?post_type={$post_type}"; } if (!wp_check_post_lock($post->ID)) { $active_post_lock = wp_set_post_lock($post->ID); if ('attachment' !== $post_type) { wp_enqueue_script('autosave'); } } if (is_multisite()) { add_action('admin_footer', '_admin_notice_post_locked'); } else { $check_users = get_users(array('fields' => 'ID', 'number' => 2)); if (count($check_users) > 1) { add_action('admin_footer', '_admin_notice_post_locked'); } unset($check_users); } $title = $post_type_object->labels->edit_item; $post = get_post($post_id, OBJECT, 'edit');
/** * Loads the display template * * @since 1.0-beta * * @param int $group_id ID of the current group. Available only in BP 2.2+. */ function display($group_id = null) { global $bp; // Docs are stored on the root blog if (!bp_is_root_blog()) { switch_to_blog(BP_ROOT_BLOG); } switch ($bp->bp_docs->current_view) { case 'create': // Todo: Make sure the user has permission to create /** * Load the template tags for the edit screen */ if (!function_exists('wp_tiny_mce')) { bp_docs_define_tiny_mce(); } require_once BP_DOCS_INCLUDES_PATH . 'templatetags-edit.php'; $template = 'edit-doc.php'; break; case 'list': $template = 'docs-loop.php'; break; case 'category': // Check to make sure the category exists // If not, redirect back to list view with error // Otherwise, get args based on category ID // Then load the loop template break; case 'single': case 'edit': case 'delete': case 'history': // If this is the edit screen, we won't really be able to use a // regular have_posts() loop in the template, so we'll stash the // post in the $bp global for the edit-specific template tags if ($bp->bp_docs->current_view == 'edit') { if (bp_docs_has_docs()) { while (bp_docs_has_docs()) { bp_docs_the_doc(); $bp->bp_docs->current_post = $post; // Set an edit lock wp_set_post_lock($post->ID); } } /** * Load the template tags for the edit screen */ require_once BP_DOCS_INCLUDES_PATH . 'templatetags-edit.php'; } switch ($bp->bp_docs->current_view) { case 'single': $template = 'single/index.php'; break; case 'edit': $template = 'single/edit.php'; break; case 'history': $template = 'single/history.php'; break; } // Todo: Maybe some sort of error if there is no edit permission? break; } // Only register on the root blog if (!bp_is_root_blog()) { restore_current_blog(); } $template_path = bp_docs_locate_template($template); if (!empty($template)) { include apply_filters('bp_docs_template', $template_path, $this); } }
public function meta_modal() { global $post, $post_type, $post_type_object, $current_screen, $wp_meta_modal_sections; $post_ID = $post->ID; set_current_screen($post_type); if (!wp_check_post_lock($post->ID)) { $active_post_lock = wp_set_post_lock($post->ID); } $messages = array(); $messages['post'] = array(0 => '', 1 => sprintf(__('Post updated. <a href="%s">View post</a>', 'wplms-front-end'), esc_url(get_permalink($post_ID))), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('Post updated.'), 5 => isset($_GET['revision']) ? sprintf(__('Post restored to revision from %s', 'wplms-front-end'), wp_post_revision_title((int) $_GET['revision'], false)) : false, 6 => sprintf(__('Post published. <a href="%s">View post</a>', 'wplms-front-end'), esc_url(get_permalink($post_ID))), 7 => __('Post saved.'), 8 => sprintf(__('Post submitted. <a target="_blank" href="%s">Preview post</a>', 'wplms-front-end'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID)))), 9 => sprintf(__('Post scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview post</a>', 'wplms-front-end'), date_i18n(__('M j, Y @ G:i'), strtotime($post->post_date)), esc_url(get_permalink($post_ID))), 10 => sprintf(__('Post draft updated. <a target="_blank" href="%s">Preview post</a>', 'wplms-front-end'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID))))); $messages['page'] = array(0 => '', 1 => sprintf(__('Page updated. <a href="%s">View page</a>', 'wplms-front-end'), esc_url(get_permalink($post_ID))), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('Page updated.'), 5 => isset($_GET['revision']) ? sprintf(__('Page restored to revision from %s'), 'wplms-front-end', wp_post_revision_title((int) $_GET['revision'], false)) : false, 6 => sprintf(__('Page published. <a href="%s">View page</a>', 'wplms-front-end'), esc_url(get_permalink($post_ID))), 7 => __('Page saved.'), 8 => sprintf(__('Page submitted. <a target="_blank" href="%s">Preview page</a>', 'wplms-front-end'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID)))), 9 => sprintf(__('Page scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview page</a>', 'wplms-front-end'), date_i18n(__('M j, Y @ G:i'), strtotime($post->post_date)), esc_url(get_permalink($post_ID))), 10 => sprintf(__('Page draft updated. <a target="_blank" href="%s">Preview page</a>', 'wplms-front-end'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID))))); $messages['attachment'] = array_fill(1, 10, __('Media attachment updated.', 'wplms-front-end')); // Hack, for now. $messages = apply_filters('post_updated_messages', $messages); $message = false; if (isset($_GET['message'])) { $_GET['message'] = absint($_GET['message']); if (isset($messages[$post_type][$_GET['message']])) { $message = $messages[$post_type][$_GET['message']]; } elseif (!isset($messages[$post_type]) && isset($messages['post'][$_GET['message']])) { $message = $messages['post'][$_GET['message']]; } } $notice = false; $form_extra = ''; if ('auto-draft' == $post->post_status) { if ('edit' == $action) { $post->post_title = ''; } $autosave = false; $form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />"; } else { $autosave = wp_get_post_autosave($post_ID); } $form_action = 'editpost'; $nonce_action = 'update-post_' . $post_ID; // Detect if there exists an autosave newer than the post and if that autosave is different than the post if ($autosave && mysql2date('U', $autosave->post_modified_gmt, false) > mysql2date('U', $post->post_modified_gmt, false)) { foreach (_wp_post_revision_fields() as $autosave_field => $_autosave_field) { if (normalize_whitespace($autosave->{$autosave_field}) != normalize_whitespace($post->{$autosave_field})) { $notice = sprintf(__('There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>', 'wplms-front-end'), get_edit_post_link($autosave->ID)); break; } } // If this autosave isn't different from the current post, begone. if (!$notice) { wp_delete_post_revision($autosave->ID); } unset($autosave_field, $_autosave_field); } $post_type_object = get_post_type_object($post_type); $this->add_meta_modal_section('submitdiv', __('Publish'), array($this, 'meta_section_publish'), 10, 10); if (post_type_supports($post_type, 'revisions') && 'auto-draft' !== $post->post_status) { $revisions = wp_get_post_revisions($post->ID); $count = count($revisions); if ($count > 1) { $this->add_meta_modal_section('revisionsdiv', __('Revisions', 'wplms-front-end') . ' (' . $count . ')', 'post_revisions_meta_box', 30, 50); } } if (current_theme_supports('post-formats') && post_type_supports($post_type, 'post-formats')) { $this->add_meta_modal_section('formatdiv', _x('Format', 'post format', 'wplms-front-end'), 'post_format_meta_box', 20, 10); } foreach (get_object_taxonomies($post) as $tax_name) { $taxonomy = get_taxonomy($tax_name); if (!$taxonomy->show_ui || false === $taxonomy->meta_box_cb) { continue; } $label = $taxonomy->labels->name; if (!is_taxonomy_hierarchical($tax_name)) { $tax_meta_box_id = 'tagsdiv-' . $tax_name; } else { $tax_meta_box_id = $tax_name . 'div'; } $this->add_meta_modal_section($tax_meta_box_id, $label, $taxonomy->meta_box_cb, 20, 20, array('taxonomy' => $tax_name)); } if (post_type_supports($post_type, 'page-attributes')) { $this->add_meta_modal_section('pageparentdiv', 'page' == $post_type ? __('Page Attributes', 'wplms-front-end') : __('Attributes', 'wplms-front-end'), 'page_attributes_meta_box', 10, 10); } if (post_type_supports($post_type, 'excerpt')) { $this->add_meta_modal_section('postexcerpt', __('Excerpt', 'wplms-front-end'), 'post_excerpt_meta_box', 30, 10); } if (post_type_supports($post_type, 'trackbacks')) { $this->add_meta_modal_section('trackbacksdiv', __('Send Trackbacks', 'wplms-front-end'), 'post_trackback_meta_box', 30, 20); } if (post_type_supports($post_type, 'custom-fields')) { $this->add_meta_modal_section('postcustom', __('Custom Fields', 'wplms-front-end'), 'post_custom_meta_box', 30, 30); } if (post_type_supports($post_type, 'comments')) { $this->add_meta_modal_section('commentstatusdiv', __('Discussion', 'wplms-front-end'), 'post_comment_status_meta_box', 30, 40); } require_once 'meta-modal-template.php'; unset($GLOBALS['current_screen']); }
function etwp_write_post() { global $user_ID; /* if ( isset($_POST['post_type']) ) $ptype = get_post_type_object($_POST['post_type']); else $ptype = get_post_type_object('post'); if ( !current_user_can( $ptype->cap->edit_posts ) ) { if ( 'page' == $ptype->name ) return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) ); else return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) ); } */ $_POST['post_mime_type'] = ''; // Clear out any data in internal vars. unset($_POST['filter']); /* // Edit don't write if we have a post id. if ( isset( $_POST['post_ID'] ) ) return edit_post(); */ if (!isset($_POST['publish'])) { $_POST['publish'] = 'publish'; } if (!isset($_POST['visibility'])) { $_POST['visibility'] = 'public'; } if (empty($_POST['post_status'])) { $_POST['post_status'] = 'publish'; } if (!isset($_POST['comment_status'])) { $_POST['comment_status'] = 'open'; } 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; } } $translated = et_wp_translate_postdata(false); if (is_wp_error($translated)) { return $translated; } // Create the post. $post_ID = etwp_insert_post($_POST); if (is_wp_error($post_ID)) { return $post_ID; } if (empty($post_ID)) { return 0; } //add_meta( $post_ID ); //zzcity add if (isset($_POST['meta']) && $_POST['meta']) { foreach ($_POST['meta'] as $key => $value) { add_post_meta($post_ID, $value['key'], $value['value']); } } add_post_meta($post_ID, '_edit_last', $GLOBALS['current_user']->ID); // Now that we have an ID we can fix any attachment anchor hrefs _fix_attachment_links($post_ID); wp_set_post_lock($post_ID); return $post_ID; }
/** * default method for the 'edit' route for cpt admin pages * * For reference on what to put in here, refer to wp-admin/post.php * * @access protected * @return string template for edit cpt form */ protected function _edit_cpt_item() { global $post, $title, $is_IE; $post_id = isset($this->_req_data['post']) ? $this->_req_data['post'] : NULL; $post = !empty($post_id) ? get_post($post_id, OBJECT, 'edit') : NULL; if (empty($post)) { wp_die(__('You attempted to edit an item that doesn’t exist. Perhaps it was deleted?')); } if (!empty($_GET['get-post-lock'])) { $test = wp_set_post_lock($post_id); wp_redirect(get_edit_post_link($post_id, 'url')); exit; } $this->_template_args['editing'] = TRUE; $this->_template_args['post_ID'] = $post_id; $this->_template_args['post'] = $post; $this->_template_args['post_type'] = $this->_cpt_routes[$this->_req_action]; $this->_template_args['post_type_object'] = $this->_cpt_object; $this->_template_args['is_IE'] = $is_IE; if ($last = wp_check_post_lock($post->ID)) { add_action('admin_notices', '_admin_notice_post_locked'); } else { $this->_template_args['active_post_lock'] = wp_set_post_lock($post->ID); wp_enqueue_script('autosave'); } $title = $this->_cpt_object->labels->edit_item; if (isset($this->_cpt_routes[$this->_req_data['action']]) && !isset($this->_labels['hide_add_button_on_cpt_route']['edit_attendee'])) { $this->_template_args['post_new_file'] = EE_Admin_Page::add_query_args_and_nonce(array('action' => 'create_new', 'page' => $this->page_slug), 'admin.php'); } if (post_type_supports($this->_cpt_routes[$this->_req_action], 'comments')) { wp_enqueue_script('admin-comments'); enqueue_comment_hotkeys_js(); } add_action('admin_print_styles', array($this, 'add_new_admin_page_global')); $template = WP_ADMIN_PATH . 'edit-form-advanced.php'; EEH_Template::display_template($template, $this->_template_args); }
/** * Test with a locked post * @return void */ public function test_locked_post() { // Become an administrator $this->_setRole('administrator'); // Lock the post wp_set_post_lock($this->_post->ID); // Become a different administrator $this->_setRole('administrator'); // Set up the $_POST request $_POST = array('post_ID' => $this->_post->ID, 'autosavenonce' => wp_create_nonce('autosave'), 'autosave' => 1); // Make the request try { $this->_handleAjax('autosave'); } catch (WPAjaxDieContinueException $e) { unset($e); } // Get the response $xml = simplexml_load_string($this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA); // Ensure everything is correct $this->assertEquals($this->_post->ID, (int) $xml->response[0]->autosave['id']); $this->assertEquals('autosave_' . $this->_post->ID, (string) $xml->response['action']); $this->assertEquals('disable', (string) $xml->response[0]->autosave[0]->supplemental[0]->disable_autosave); }
} else { // Non drafts are not overwritten. The autosave is stored in a special post revision. $revision_id = wp_create_post_autosave($post->ID); if (is_wp_error($revision_id)) { $id = $revision_id; } else { $id = $post->ID; } } $data = $message; } else { $id = $post->ID; } } if ($do_lock && $id && is_numeric($id)) { wp_set_post_lock($id); } if ($nonce_age == 2) { $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave'); $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink'); $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink'); $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes'); if ($id) { if ($_POST['post_type'] == 'post') { $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id); } elseif ($_POST['post_type'] == 'page') { $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id); } } } $x = new WP_Ajax_Response(array('what' => 'autosave', 'id' => $id, 'data' => $id ? $data : '', 'supplemental' => $supplemental));
/** * Checks if another user is editing an idea, if not * locks the idea for the current user. * * @package WP Idea Stream * @subpackage ideas/functions * * @since 2.0.0 * * @param int $idea_id The ID of the idea to edit * @uses wp_check_post_lock() to check if the idea is locked to another user * @uses wp_set_post_lock() to lock the idea to current user * @return int the user id editing the idea */ function wp_idea_stream_ideas_lock_idea($idea_id = 0) { $user_id = false; // Bail if no ID to check if (empty($idea_id)) { return $user_id; } // Include needed file require_once ABSPATH . '/wp-admin/includes/post.php'; $user_id = wp_check_post_lock($idea_id); // If not locked, then lock it as current user is editing it. if (empty($user_id)) { wp_set_post_lock($idea_id); } return $user_id; }
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; }
/** * 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; }
/** * Enable the builder editor for the main post in the query. * * @since 1.0 * @return void */ public static function enable_editing() { global $wp_the_query; if (self::is_post_editable()) { $post = $wp_the_query->post; $published = self::get_layout_data('published'); $draft = self::get_layout_data('draft'); // Migrate existing post content to the builder? if (empty($published) && empty($draft) && !empty($post->post_content)) { $row = self::add_row(); $cols = self::get_nodes('column'); $col = array_shift($cols); $settings = self::get_module_defaults('rich-text'); $settings->text = wpautop($post->post_content); self::add_module('rich-text', $settings, $col->node); } else { if (empty($draft)) { self::update_layout_data($published, 'draft', $post->ID); self::update_layout_settings(self::get_layout_settings('published'), 'draft', $post->ID); } } // Delete old draft asset cache. self::delete_asset_cache(); // Lock the post. require_once ABSPATH . 'wp-admin/includes/post.php'; wp_set_post_lock($post->ID); } }
/** * Fires the WP query and loads the appropriate template * * @package BuddyPress Docs * @since 1.0-beta */ function load_template() { global $bp, $post; // Docs are stored on the root blog if (!bp_is_root_blog()) { switch_to_blog(BP_ROOT_BLOG); } switch ($this->current_view) { case 'create': // Todo: Make sure the user has permission to create /** * Load the template tags for the edit screen */ require BP_DOCS_INCLUDES_PATH . 'templatetags-edit.php'; $template = 'edit-doc.php'; break; case 'list': $args = $this->build_query(); /* Todo: Get this into its own 'tree' view */ /* $the_docs = get_posts( $args ); $f = walk_page_tree($the_docs, 0, 0, array( 'walker' => new Walker_Page ) ); print_r( $f ); */ query_posts($args); $template = 'docs-loop.php'; break; case 'category': // Check to make sure the category exists // If not, redirect back to list view with error // Otherwise, get args based on category ID // Then load the loop template break; case 'single': case 'edit': case 'delete': case 'history': $args = $this->build_query(); // Add a 'name' argument so that we only get the specific post $args['name'] = $this->doc_slug; query_posts($args); // If this is the edit screen, we won't really be able to use a // regular have_posts() loop in the template, so we'll stash the // post in the $bp global for the edit-specific template tags if ($this->current_view == 'edit') { if (have_posts()) { while (have_posts()) { the_post(); $bp->bp_docs->current_post = $post; // Set an edit lock wp_set_post_lock($post->ID); } } /** * Load the template tags for the edit screen */ require BP_DOCS_INCLUDES_PATH . 'templatetags-edit.php'; } switch ($this->current_view) { case 'single': $template = 'single-doc.php'; break; case 'edit': $template = 'edit-doc.php'; break; case 'history': $template = 'history-doc.php'; break; } // Todo: Maybe some sort of error if there is no edit permission? break; } // Only register on the root blog if (!bp_is_root_blog()) { restore_current_blog(); } $template_path = bp_docs_locate_template($template); if (!empty($template)) { include apply_filters('bp_docs_template', $template_path, $this); } }
/** * default method for the 'edit' route for cpt admin pages * * For reference on what to put in here, refer to wp-admin/post.php * * @access protected * @return string template for edit cpt form */ protected function _edit_cpt_item() { global $post, $title, $is_IE, $post_type, $post_type_object; $post_id = isset($this->_req_data['post']) ? $this->_req_data['post'] : NULL; $post = !empty($post_id) ? get_post($post_id, OBJECT, 'edit') : NULL; if (empty($post)) { wp_die(__('You attempted to edit an item that doesn’t exist. Perhaps it was deleted?')); } if (!empty($_GET['get-post-lock'])) { wp_set_post_lock($post_id); wp_redirect(get_edit_post_link($post_id, 'url')); exit; } $editing = TRUE; $post_ID = $post_id; $post = $post; $post_type = $this->_cpt_routes[$this->_req_action]; $post_type_object = $this->_cpt_object; $is_IE = $is_IE; if (!wp_check_post_lock($post->ID)) { $active_post_lock = wp_set_post_lock($post->ID); //wp_enqueue_script('autosave'); } $title = $this->_cpt_object->labels->edit_item; add_action('admin_footer', '_admin_notice_post_locked'); if (isset($this->_cpt_routes[$this->_req_data['action']]) && !isset($this->_labels['hide_add_button_on_cpt_route'][$this->_req_data['action']])) { $create_new_action = apply_filters('FHEE__EE_Admin_Page_CPT___edit_cpt_item__create_new_action', 'create_new', $this); $post_new_file = EE_Admin_Page::add_query_args_and_nonce(array('action' => $create_new_action, 'page' => $this->page_slug), 'admin.php'); } if (post_type_supports($this->_cpt_routes[$this->_req_action], 'comments')) { wp_enqueue_script('admin-comments'); enqueue_comment_hotkeys_js(); } add_action('admin_print_styles', array($this, 'add_new_admin_page_global')); //modify the default editor title field with default title. add_filter('enter_title_here', array($this, 'add_custom_editor_default_title'), 10); include_once WP_ADMIN_PATH . 'edit-form-advanced.php'; }
wp_enqueue_script('page'); if (user_can_richedit()) { wp_enqueue_script('editor'); } add_thickbox(); wp_enqueue_script('media-upload'); wp_enqueue_script('word-count'); if (current_user_can('edit_page', $page_ID)) { if ($last = wp_check_post_lock($post->ID)) { $last_user = get_userdata($last); $last_user_name = $last_user ? $last_user->display_name : __('Somebody'); $message = sprintf(__('Warning: %s is currently editing this page'), wp_specialchars($last_user_name)); $message = str_replace("'", "\\'", "<div class='error'><p>{$message}</p></div>"); add_action('admin_notices', create_function('', "echo '{$message}';")); } else { wp_set_post_lock($post->ID); wp_enqueue_script('autosave'); } } require_once 'admin-header.php'; if (!current_user_can('edit_page', $page_ID)) { die(__('You are not allowed to edit this page.')); } include 'edit-page-form.php'; break; case 'editattachment': $page_id = $post_ID = (int) $_POST['post_ID']; check_admin_referer('update-attachment_' . $page_id); // Don't let these be changed unset($_POST['guid']); $_POST['post_type'] = 'attachment';
function wp() { global $post; if (!empty($_GET['get-post-lock'])) { require_once ABSPATH . '/wp-admin/includes/post.php'; wp_set_post_lock($post->ID); wp_redirect($this->edit_link($post->ID)); die; } if (!$this->has_fee()) { return; } if (force_ssl_admin() && !is_ssl()) { wp_redirect(set_url_scheme($this->edit_link($post->ID), 'https')); die; } if ($post->post_status === 'auto-draft') { $post->post_title = ''; $post->comment_status = get_option('default_comment_status'); $post->ping_status = get_option('default_ping_status'); } require_once ABSPATH . '/wp-admin/includes/admin.php'; add_filter('body_class', array($this, 'body_class')); add_filter('post_class', array($this, 'post_class')); add_filter('the_title', array($this, 'the_title'), 10, 2); add_filter('the_content', array($this, 'the_content'), 20); add_filter('wp_link_pages', array($this, 'wp_link_pages')); add_filter('post_thumbnail_html', array($this, 'post_thumbnail_html'), 10, 5); add_filter('get_post_metadata', array($this, 'get_post_metadata'), 10, 4); add_filter('the_category', array($this, 'the_category'), 10, 3); add_filter('private_title_format', array($this, 'private_title_format'), 10, 2); add_filter('protected_title_format', array($this, 'private_title_format'), 10, 2); add_action('wp_before_admin_bar_render', array($this, 'wp_before_admin_bar_render')); add_action('wp_print_footer_scripts', 'wp_auth_check_html'); add_action('wp_print_footer_scripts', array($this, 'footer')); add_action('wp_print_footer_scripts', array($this, 'link_modal')); if (count(get_users(array('fields' => 'ID', 'number' => 2))) > 1) { add_action('wp_print_footer_scripts', '_admin_notice_post_locked'); } add_filter('fee_content', 'wptexturize'); add_filter('fee_content', 'convert_chars'); add_filter('fee_content', 'wpautop'); }
function wp_ajax_autosave() { global $login_grace_period; define('DOING_AUTOSAVE', true); $nonce_age = check_ajax_referer('autosave', 'autosavenonce'); $_POST['post_category'] = explode(",", $_POST['catslist']); if ($_POST['post_type'] == 'page' || empty($_POST['post_category'])) { unset($_POST['post_category']); } $do_autosave = (bool) $_POST['autosave']; $do_lock = true; $data = $alert = ''; /* translators: draft saved date format, see http://php.net/date */ $draft_saved_date_format = __('g:i:s a'); /* translators: %s: date and time */ $message = sprintf(__('Draft saved at %s.'), date_i18n($draft_saved_date_format)); $supplemental = array(); if (isset($login_grace_period)) { $alert .= sprintf(__('Your login has expired. Please open a new browser window and <a href="%s" target="_blank">log in again</a>. '), add_query_arg('interim-login', 1, wp_login_url())); } $id = $revision_id = 0; $post_ID = (int) $_POST['post_ID']; $_POST['ID'] = $post_ID; $post = get_post($post_ID); if ('auto-draft' == $post->post_status) { $_POST['post_status'] = 'draft'; } if ($last = wp_check_post_lock($post->ID)) { $do_autosave = $do_lock = false; $last_user = get_userdata($last); $last_user_name = $last_user ? $last_user->display_name : __('Someone'); $data = __('Autosave disabled.'); $supplemental['disable_autosave'] = 'disable'; $alert .= sprintf(__('%s is currently editing this article. If you update it, you will overwrite the changes.'), esc_html($last_user_name)); } if ('page' == $post->post_type) { if (!current_user_can('edit_page', $post_ID)) { wp_die(__('You are not allowed to edit this page.')); } } else { if (!current_user_can('edit_post', $post_ID)) { wp_die(__('You are not allowed to edit this post.')); } } if ($do_autosave) { // Drafts and auto-drafts are just overwritten by autosave if ('auto-draft' == $post->post_status || 'draft' == $post->post_status) { $id = edit_post(); } else { // Non drafts are not overwritten. The autosave is stored in a special post revision. $revision_id = wp_create_post_autosave($post->ID); if (is_wp_error($revision_id)) { $id = $revision_id; } else { $id = $post->ID; } } $data = $message; } else { if (!empty($_POST['auto_draft'])) { $id = 0; } else { $id = $post->ID; } } if ($do_lock && empty($_POST['auto_draft']) && $id && is_numeric($id)) { $lock_result = wp_set_post_lock($id); $supplemental['active-post-lock'] = implode(':', $lock_result); } if ($nonce_age == 2) { $supplemental['replace-autosavenonce'] = wp_create_nonce('autosave'); $supplemental['replace-getpermalinknonce'] = wp_create_nonce('getpermalink'); $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink'); $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes'); $supplemental['replace-_ajax_linking_nonce'] = wp_create_nonce('internal-linking'); if ($id) { if ($_POST['post_type'] == 'post') { $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id); } elseif ($_POST['post_type'] == 'page') { $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id); } } } if (!empty($alert)) { $supplemental['alert'] = $alert; } $x = new WP_Ajax_Response(array('what' => 'autosave', 'id' => $id, 'data' => $id ? $data : '', 'supplemental' => $supplemental)); $x->send(); }
/** * Ajax Callback to change filelock on lock override * * @since 0.5 * @param bool $send_notice (optional) whether or not to send an e-mail to the former lock owner */ function override_lock($send_notice = true) { //verify current user can edit //consider a specific permission check here if (!$_POST['post_id'] || !current_user_can('edit_post', $_POST['post_id']) || !current_user_can('override_document_lock')) { wp_die(__('Not authorized', 'wp-document-revisions')); } //verify that there is a lock if (!($current_owner = wp_check_post_lock($_POST['post_id']))) { die('-1'); } //update the lock wp_set_post_lock($_POST['post_id']); //get the current user ID $current_user = wp_get_current_user(); if (apply_filters('send_document_override_notice', $send_notice)) { $this->send_override_notice($_POST['post_id'], $current_owner, $current_user->ID); } do_action('document_lock_override', $_POST['post_id'], $current_user->ID, $current_owner); die('1'); }