本文整理汇总了PHP中sanitize_post_field函数的典型用法代码示例。如果您正苦于以下问题:PHP sanitize_post_field函数的具体用法?PHP sanitize_post_field怎么用?PHP sanitize_post_field使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sanitize_post_field函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: kia_repeating_save_filter
function kia_repeating_save_filter($meta, $post_id)
{
if (is_array($meta) && !empty($meta)) {
array_walk($meta, function (&$item, $key) {
if (isset($item['textarea'])) {
$item['textarea'] = sanitize_post_field('post_content', $item['textarea'], $post_id, 'db');
}
});
}
return $meta;
}
示例2: acf_create_rel_post
/**
* Create new posts via ajax.
* Requires title and post_type to be present in $_POST
*
* @return array
*/
function acf_create_rel_post()
{
// validate nonce first
if (!wp_verify_nonce($_POST['nonce'], 'acf_nonce')) {
wp_send_json_error();
}
if (!current_user_can('publish_posts')) {
wp_send_json_error();
}
// collect and santize data before insertion
$title = sanitize_post_field('post_title', $_POST['title'], null, 'db');
$post_type = sanitize_post_field('post_type', $_POST['post_type'][0], null, 'db');
if (!empty($title) && !empty($post_type)) {
// allow other developers to filter arguments
$post_id = wp_insert_post(apply_filters('acf_add_rel_post_args', array('post_type' => $post_type, 'post_title' => $title)));
}
wp_send_json_success(apply_filters('acf_add_rel_post_created', $post_id));
}
开发者ID:jancbeck,项目名称:acf-add-posts-via-relationship-interface,代码行数:24,代码来源:acf-add-posts-via-relationship-interface.php
示例3: save_main_header
/**
* Save a possible override of the default sup and sub headers at an individual page level.
*
* @param int $post_id The current post ID.
* @param WP_Post $post Object representing the current post.
*/
function save_main_header($post_id, $post)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!isset($_POST['_spine_header_nonce']) || false === wp_verify_nonce($_POST['_spine_header_nonce'], 'save-spine-main-header')) {
return;
}
if ('auto-draft' === $post->post_status) {
return;
}
if (isset($_POST['spine_sup_header']) && '' != trim($_POST['spine_sup_header'])) {
$sup_header = sanitize_post_field('post_title', $_POST['spine_sup_header'], $post->ID, 'db');
update_post_meta($post_id, 'sup-header', $sup_header);
} else {
delete_post_meta($post_id, 'sup-header');
}
if (isset($_POST['spine_sub_header']) && '' != trim($_POST['spine_sub_header'])) {
$sub_header = sanitize_post_field('post_title', $_POST['spine_sub_header'], $post->ID, 'db');
update_post_meta($post_id, 'sub-header', $sub_header);
} else {
delete_post_meta($post_id, 'sub-header');
}
}
示例4: bbp_get_global_post_field
/**
* Get the unfiltered value of a global $post's key
*
* Used most frequently when editing a forum/topic/reply
*
* @since 2.1.0 bbPress (r3694)
*
* @global WP_Query $post
* @param string $field Name of the key
* @param string $context How to sanitize - raw|edit|db|display|attribute|js
* @return string Field value
*/
function bbp_get_global_post_field($field = 'ID', $context = 'edit')
{
global $post;
$retval = isset($post->{$field}) ? $post->{$field} : '';
$retval = sanitize_post_field($field, $retval, $post->ID, $context);
return apply_filters('bbp_get_global_post_field', $retval, $post);
}
示例5: sanitize_post
/**
* Sanitize every post field.
*
* If the context is 'raw', then the post object or array will just be returned.
*
* @since 2.3.0
* @uses sanitize_post_field() Used to sanitize the fields.
*
* @param object|array $post The Post Object or Array
* @param string $context Optional, default is 'display'. How to sanitize post fields.
* @return object|array The now sanitized Post Object or Array (will be the same type as $post)
*/
function sanitize_post($post, $context = 'display')
{
if ('raw' == $context) {
return $post;
}
if (is_object($post)) {
if (!isset($post->ID)) {
$post->ID = 0;
}
foreach (array_keys(get_object_vars($post)) as $field) {
$post->{$field} = sanitize_post_field($field, $post->{$field}, $post->ID, $context);
}
$post->filter = $context;
} else {
if (!isset($post['ID'])) {
$post['ID'] = 0;
}
foreach (array_keys($post) as $field) {
$post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
}
$post['filter'] = $context;
}
return $post;
}
示例6: post_exists
/**
* Determine if a post exists based on title, content, and date
*
* @since 2.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $title Post title
* @param string $content Optional post content
* @param string $date Optional post date
* @return int Post ID if post exists, 0 otherwise.
*/
function post_exists($title, $content = '', $date = '')
{
global $wpdb;
$post_title = wp_unslash(sanitize_post_field('post_title', $title, 0, 'db'));
$post_content = wp_unslash(sanitize_post_field('post_content', $content, 0, 'db'));
$post_date = wp_unslash(sanitize_post_field('post_date', $date, 0, 'db'));
$query = "SELECT ID FROM {$wpdb->posts} WHERE 1=1";
$args = array();
if (!empty($date)) {
$query .= ' AND post_date = %s';
$args[] = $post_date;
}
if (!empty($title)) {
$query .= ' AND post_title = %s';
$args[] = $post_title;
}
if (!empty($content)) {
$query .= ' AND post_content = %s';
$args[] = $post_content;
}
if (!empty($args)) {
return (int) $wpdb->get_var($wpdb->prepare($query, $args));
}
return 0;
}
示例7: spot_post_exists
function spot_post_exists($title, $content = '', $date = '')
{
global $wpdb;
$post_title = stripslashes(sanitize_post_field('post_title', $title, 0, 'db'));
$post_slug = sanitize_title($title);
$post_content = stripslashes(sanitize_post_field('post_content', $content, 0, 'db'));
$post_date = stripslashes(sanitize_post_field('post_date', $date, 0, 'db'));
$query = "SELECT ID FROM {$wpdb->posts} WHERE 1=1 AND post_type = '" . SPOTS_POST_TYPE . "'";
// force checking of spots only
$args = array();
if (!empty($date)) {
$query .= ' AND post_date = %s';
$args[] = $post_date;
}
if (!empty($title)) {
$query .= ' AND ( post_title = %s OR post_name = %s )';
$args[] = $post_title;
$args[] = $post_slug;
}
if (!empty($content)) {
$query .= ' AND post_content = %s';
$args[] = $post_content;
}
if (!empty($args)) {
return $wpdb->get_var($wpdb->prepare($query, $args));
}
return 0;
}
示例8: process_posts
/**
* process_posts
*
* Imports posts and loads $this->posts
*
* @uses $wpdb
*
* @param none
* @return none
*/
public function process_posts()
{
$headers = array();
$content = array();
// read file contents first
if (($handle = fopen($this->file, 'r')) !== false) {
$row = 0;
while (($data = fgetcsv($handle, 1000, self::DELIMITER)) !== false) {
// read headers
if ($row === 0) {
$headers = $data;
} else {
$content[] = $data;
}
$row++;
}
}
fclose($handle);
$results = array('errors' => 0, 'updated' => 0);
// Check for invalid headers
$matches = preg_grep('/^(' . implode('|', array_keys($this->header_settings)) . ')$/', $headers, PREG_GREP_INVERT);
if ($matches) {
\Timber::render('importer/invalid-headers.twig', array('headers' => $matches));
}
// store the post data
if ($headers && $content) {
// read cols
foreach ($content as $row => &$cols) {
$post_id = $content[$row][array_search('ID', $headers)];
$post = get_post($post_id);
if (!$post) {
\Timber::render('importer/warning.twig', array('message' => sprintf(__("ID `%s` does not match any existing posts.", TEXTDOMAIN), esc_html($post_id))));
$results['errors']++;
continue;
}
$post->post_status = 'publish';
foreach ($cols as $col => $val) {
if (!isset($headers[$col])) {
continue;
}
$key = $headers[$col];
switch ($key) {
case 'ID':
continue 2;
// skip to next col
// skip to next col
case 'post_title':
case 'post_content':
case 'post_date':
case 'post_date_gmt':
case 'comment_status':
case 'ping_status':
case 'post_name':
case 'status':
case 'post_parent':
case 'menu_order':
case 'post_type':
case 'post_password':
case 'is_sticky':
$post->{$key} = sanitize_post_field($key, $val, $post->ID, 'save');
break;
// TODO categories, tags, attachments!
// TODO categories, tags, attachments!
default:
foreach ($this->header_settings as $pattern => $settings) {
if (preg_match("/{$pattern}/", $key)) {
$settings = wp_parse_args($settings, array('filter' => FILTER_SANITIZE_STRING, 'filter_options' => null));
$val = trim(filter_var($val, $settings['filter'], $settings['filter_options']));
$val = apply_filters('importer_meta_field_value', $val, array($settings));
do_action('importer_meta_field_update', $post, $key, $val);
break;
}
}
}
}
if (wp_update_post($post)) {
$results['updated']++;
}
}
\Timber::render('importer/complete.twig', array('results' => $results));
} else {
\Timber::render('importer/error.twig', array('error' => __("File had no recognized content!", TEXTDOMAIN)));
}
}
示例9: siteorigin_panels_save_home_page
/**
* Save home page
*/
function siteorigin_panels_save_home_page()
{
if (!isset($_POST['_sopanels_home_nonce']) || !wp_verify_nonce($_POST['_sopanels_home_nonce'], 'save')) {
return;
}
if (!current_user_can('edit_theme_options')) {
return;
}
if (!isset($_POST['panels_data'])) {
return;
}
// Check that the home page ID is set and the home page exists
$page_id = get_option('page_on_front');
if (empty($page_id)) {
$page_id = get_option('siteorigin_panels_home_page_id');
}
$post_content = wp_unslash($_POST['post_content']);
if (!$page_id || get_post_meta($page_id, 'panels_data', true) == '') {
// Lets create a new page
$page_id = wp_insert_post(array('post_title' => __('Home Page', 'siteorigin-panels'), 'post_status' => !empty($_POST['siteorigin_panels_home_enabled']) ? 'publish' : 'draft', 'post_type' => 'page', 'post_content' => $post_content, 'comment_status' => 'closed'));
update_option('page_on_front', $page_id);
update_option('siteorigin_panels_home_page_id', $page_id);
// Action triggered when creating a new home page through the custom home page interface
do_action('siteorigin_panels_create_home_page', $page_id);
} else {
// `wp_insert_post` does it's own sanitization, but it seems `wp_update_post` doesn't.
$post_content = sanitize_post_field('post_content', $post_content, $page_id, 'db');
// Update the post with changed content to save revision if necessary.
wp_update_post(array('ID' => $page_id, 'post_content' => $post_content));
}
// Save the updated page data
$panels_data = json_decode(wp_unslash($_POST['panels_data']), true);
$panels_data['widgets'] = siteorigin_panels_process_raw_widgets($panels_data['widgets']);
$panels_data = siteorigin_panels_styles_sanitize_all($panels_data);
update_post_meta($page_id, 'panels_data', $panels_data);
$template = get_post_meta($page_id, '_wp_page_template', true);
$home_template = siteorigin_panels_setting('home-template');
if (($template == '' || $template == 'default') && !empty($home_template)) {
// Set the home page template
update_post_meta($page_id, '_wp_page_template', $home_template);
}
if (!empty($_POST['siteorigin_panels_home_enabled'])) {
update_option('show_on_front', 'page');
update_option('page_on_front', $page_id);
update_option('siteorigin_panels_home_page_id', $page_id);
wp_publish_post($page_id);
} else {
// We're disabling this home page
update_option('show_on_front', 'posts');
// Change the post status to draft
$post = get_post($page_id);
if ($post->post_status != 'draft') {
global $wpdb;
$wpdb->update($wpdb->posts, array('post_status' => 'draft'), array('ID' => $post->ID));
clean_post_cache($post->ID);
$old_status = $post->post_status;
$post->post_status = 'draft';
wp_transition_post_status('draft', $old_status, $post);
do_action('edit_post', $post->ID, $post);
do_action("save_post_{$post->post_type}", $post->ID, $post, true);
do_action('save_post', $post->ID, $post, true);
do_action('wp_insert_post', $post->ID, $post, true);
}
}
}
示例10: save
/**
* Saves a doc.
*
* This method handles saving for both new and existing docs. It detects the difference by
* looking for the presence of $this->doc_slug
*
* @since 1.0-beta
*/
function save($args = false)
{
global $bp, $wp_rewrite;
// bbPress plays naughty with revision saving
add_action('pre_post_update', 'wp_save_post_revision');
// Get the required taxonomy items associated with the group. We only run this
// on a save because it requires extra database hits.
$this->setup_terms();
// Set up the default value for the result message
$results = array('message' => __('Unknown error. Please try again.', 'bp-docs'), 'redirect' => 'create');
// Backward compatibility. Had to change to doc_content to work with wp_editor
$doc_content = '';
if (isset($_POST['doc_content'])) {
$doc_content = $_POST['doc_content'];
} else {
if (isset($_POST['doc']['content'])) {
$doc_content = $_POST['doc']['content'];
}
}
// Check group associations
// @todo Move into group integration piece
// This group id is only used to check whether the user can associate the doc with the group.
$associated_group_id = isset($_POST['associated_group_id']) ? intval($_POST['associated_group_id']) : null;
if (bp_is_active('groups')) {
if (!empty($associated_group_id) && !current_user_can('bp_docs_associate_with_group', $associated_group_id)) {
$retval = array('message_type' => 'error', 'message' => __('You are not allowed to associate a Doc with that group.', 'bp-docs'), 'redirect_url' => bp_docs_get_create_link());
return $retval;
}
}
if (empty($_POST['doc']['title'])) {
// The title field is required
$result['message'] = __('The title field is required.', 'bp-docs');
$result['redirect'] = !empty($this->doc_slug) ? 'edit' : 'create';
} else {
$defaults = array('post_type' => $this->post_type_name, 'post_title' => $_POST['doc']['title'], 'post_name' => isset($_POST['doc']['permalink']) ? sanitize_title($_POST['doc']['permalink']) : sanitize_title($_POST['doc']['title']), 'post_content' => sanitize_post_field('post_content', $doc_content, 0, 'db'), 'post_status' => 'publish');
$r = wp_parse_args($args, $defaults);
if (empty($this->doc_slug)) {
$this->is_new_doc = true;
$r['post_author'] = bp_loggedin_user_id();
// If there's a 'doc_id' value in the POST, use
// the autodraft as a starting point
if (isset($_POST['doc_id']) && 0 != $_POST['doc_id']) {
$post_id = (int) $_POST['doc_id'];
$r['ID'] = $post_id;
wp_update_post($r);
} else {
$post_id = wp_insert_post($r);
}
if (!$post_id) {
$result['message'] = __('There was an error when creating the doc.', 'bp-docs');
$result['redirect'] = 'create';
} else {
$this->doc_id = $post_id;
$the_doc = get_post($this->doc_id);
$this->doc_slug = $the_doc->post_name;
// A normal, successful save
$result['message'] = __('Doc successfully created!', 'bp-docs');
$result['redirect'] = 'single';
}
} else {
$this->is_new_doc = false;
$doc = bp_docs_get_current_doc();
$this->doc_id = $doc->ID;
$r['ID'] = $this->doc_id;
// Make sure the post_name is set
if (empty($r['post_name'])) {
$r['post_name'] = sanitize_title($r['post_title']);
}
// Make sure the post_name is unique
$r['post_name'] = wp_unique_post_slug($r['post_name'], $this->doc_id, $r['post_status'], $this->post_type_name, $doc->post_parent);
$this->doc_slug = $r['post_name'];
// Save pre-update post data, for comparison by callbacks.
$this->previous_revision = clone $doc;
if (!wp_update_post($r)) {
$result['message'] = __('There was an error when saving the doc.', 'bp-docs');
$result['redirect'] = 'edit';
} else {
// Remove the edit lock
delete_post_meta($this->doc_id, '_edit_lock');
delete_post_meta($this->doc_id, '_bp_docs_last_pinged');
// When the post has been autosaved, we need to leave a
// special success message
if (!empty($_POST['is_auto']) && $_POST['is_auto']) {
$result['message'] = __('You idled a bit too long while in Edit mode. In order to allow others to edit the doc you were working on, your changes have been autosaved. Click the Edit button to return to Edit mode.', 'bp-docs');
} else {
// A normal, successful save
$result['message'] = __('Doc successfully edited!', 'bp-docs');
}
$result['redirect'] = 'single';
}
$post_id = $this->doc_id;
}
//.........这里部分代码省略.........
示例11: sanitize_post
/**
* Sanitize every post field.
*
* If the context is 'raw', then the post object or array will just be returned.
*
* @since 2.3.0
* @uses sanitize_post_field() Used to sanitize the fields.
*
* @param object|array $post The Post Object or Array
* @param string $context Optional, default is 'display'. How to sanitize post fields.
* @return object|array The now sanitized Post Object or Array (will be the same type as $post)
*/
function sanitize_post($post, $context = 'display') {
if ( 'raw' == $context )
return $post;
if ( is_object($post) ) {
if ( !isset($post->ID) )
$post->ID = 0;
foreach ( array_keys(get_object_vars($post)) as $field )
$post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
} else {
if ( !isset($post['ID']) )
$post['ID'] = 0;
foreach ( array_keys($post) as $field )
$post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
}
return $post;
}
示例12: change_cats
private function change_cats()
{
global $title, $action, $post_ID, $parent_file, $post, $post_referredby, $post_orig_referer, $checked_cats;
if ($this->admin->get_data('originalaction') == 'editpost') {
$title = __('Edit');
$action = 'edit';
$post_ID = intval($this->admin->get_data('post_ID'));
$post = $this->get_post($post_ID);
} else {
// $parent_file = 'post-new.php';
$title = __('Add New Post');
$post_ID = 0;
$post = get_default_post_to_edit();
}
check_admin_referer('change-cats_' . $post_ID);
foreach (array('post_title', 'post_name', 'post_content', 'tags_input') as $f) {
$post->{$f} = $this->admin->get_data($f);
$post->{$f} = sanitize_post_field($f, $post->{$f}, $post_ID, 'edit');
}
$post_referredby = $this->admin->get_data('referredby');
$post_orig_referer = $this->admin->get_data('_wp_original_http_referer');
if (!isset($_POST['cancel'])) {
$checked_cats = array();
if (is_array($_POST['cat']) && count($_POST['cat']) >= 1) {
foreach ($_POST['cat'] as $c) {
$checked_cats[] = intval($c);
}
} else {
$checked_cats[] = get_option('default_category');
}
} else {
$checked_cats = array_map('intval', explode(',', $this->admin->get_data('post_cats')));
}
include dirname(__FILE__) . '/edit-form.php';
}
示例13: product_exists
public function product_exists($title, $sku = '', $post_name = '')
{
global $wpdb;
// Post Title Check
$post_title = stripslashes(sanitize_post_field('post_title', $title, 0, 'db'));
$query = "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'product' AND post_status IN ( 'publish', 'private', 'draft', 'pending', 'future' )";
$args = array();
if (!empty($title)) {
$query .= ' AND post_title = %s';
$args[] = $post_title;
}
if (!empty($post_name)) {
$query .= ' AND post_name = %s';
$args[] = $post_name;
}
if (!empty($args)) {
$posts_that_exist = $wpdb->get_col($wpdb->prepare($query, $args));
if ($posts_that_exist) {
foreach ($posts_that_exist as $post_exists) {
// Check unique SKU
$post_exists_sku = get_post_meta($post_exists, '_sku', true);
if ($sku == $post_exists_sku) {
return true;
}
}
}
}
// Sku Check
if ($sku) {
$post_exists_sku = $wpdb->get_var($wpdb->prepare("\n\t\t\t\tSELECT {$wpdb->posts}.ID\n\t\t\t FROM {$wpdb->posts}\n\t\t\t LEFT JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id )\n\t\t\t WHERE {$wpdb->posts}.post_status IN ( 'publish', 'private', 'draft', 'pending', 'future' )\n\t\t\t AND {$wpdb->postmeta}.meta_key = '_sku' AND {$wpdb->postmeta}.meta_value = '%s'\n\t\t\t ", $sku));
if ($post_exists_sku) {
return true;
}
}
return false;
}
示例14: save_banner
/**
* Clean the data being passed when saving the Banner layout.
*
* @param array $data Array of data inputs being passed.
*
* @return array Clean data.
*/
public function save_banner($data)
{
$clean_data = array();
$clean_data['title'] = $clean_data['label'] = isset($data['title']) ? apply_filters('title_save_pre', $data['title']) : '';
$clean_data['hide-arrows'] = isset($data['hide-arrows']) && 1 === (int) $data['hide-arrows'] ? 1 : 0;
$clean_data['hide-dots'] = isset($data['hide-dots']) && 1 === (int) $data['hide-dots'] ? 1 : 0;
$clean_data['autoplay'] = isset($data['autoplay']) && 1 === (int) $data['autoplay'] ? 1 : 0;
if (isset($data['transition']) && in_array($data['transition'], array('fade', 'scrollHorz', 'none'))) {
$clean_data['transition'] = $data['transition'];
}
if (isset($data['delay'])) {
$clean_data['delay'] = absint($data['delay']);
}
if (isset($data['height'])) {
$clean_data['height'] = absint($data['height']);
}
if (isset($data['responsive']) && in_array($data['responsive'], array('aspect', 'balanced'))) {
$clean_data['responsive'] = $data['responsive'];
}
if (isset($data['banner-slide-order'])) {
$clean_data['banner-slide-order'] = array_map(array('TTFMAKE_Builder_Save', 'clean_section_id'), explode(',', $data['banner-slide-order']));
}
if (isset($data['banner-slides']) && is_array($data['banner-slides'])) {
foreach ($data['banner-slides'] as $id => $slide) {
if (isset($slide['content'])) {
$clean_data['banner-slides'][$id]['content'] = sanitize_post_field('post_content', $slide['content'], get_post() ? get_the_ID() : 0, 'db');
}
if (isset($slide['background-color'])) {
$clean_data['banner-slides'][$id]['background-color'] = maybe_hash_hex_color($slide['background-color']);
}
$clean_data['banner-slides'][$id]['darken'] = isset($slide['darken']) && 1 === (int) $slide['darken'] ? 1 : 0;
if (isset($slide['image-id'])) {
$clean_data['banner-slides'][$id]['image-id'] = ttfmake_sanitize_image_id($slide['image-id']);
}
$clean_data['banner-slides'][$id]['alignment'] = isset($slide['alignment']) && in_array($slide['alignment'], array('none', 'left', 'right')) ? $slide['alignment'] : 'none';
if (isset($slide['state'])) {
$clean_data['banner-slides'][$id]['state'] = in_array($slide['state'], array('open', 'closed')) ? $slide['state'] : 'open';
}
if (isset($slide['spine_slide_url'])) {
$clean_data['banner-slides'][$id]['slide-url'] = esc_url_raw($slide['spine_slide_url']);
}
}
}
if (isset($data['section-classes'])) {
$clean_data['section-classes'] = $this->clean_classes($data['section-classes']);
}
if (isset($data['section-wrapper'])) {
$clean_data['section-wrapper'] = $this->clean_classes($data['section-wrapper']);
}
if (isset($data['column-classes'])) {
$clean_data['column-classes'] = $this->clean_classes($data['column-classes']);
}
if (isset($data['label'])) {
$clean_data['label'] = sanitize_text_field($data['label']);
}
if (isset($data['background-img'])) {
$clean_data['background-img'] = esc_url_raw($data['background-img']);
}
if (isset($data['background-mobile-img'])) {
$clean_data['background-mobile-img'] = esc_url_raw($data['background-mobile-img']);
}
$clean_data = apply_filters('spine_builder_save_banner', $clean_data, $data);
return $clean_data;
}
示例15: save
/**
* Saves a doc.
*
* This method handles saving for both new and existing docs. It detects the difference by
* looking for the presence of $this->doc_slug
*
* @package BuddyPress Docs
* @since 1.0-beta
*/
function save($args = false)
{
global $bp;
// bbPress plays naughty with revision saving
add_action('pre_post_update', 'wp_save_post_revision');
// Get the required taxonomy items associated with the group. We only run this
// on a save because it requires extra database hits.
$this->setup_terms();
// Set up the default value for the result message
$results = array('message' => __('Unknown error. Please try again.', 'bp-docs'), 'redirect' => 'create');
// Backward compatibility. Had to change to doc_content to work with wp_editor
$doc_content = '';
if (isset($_POST['doc_content'])) {
$doc_content = $_POST['doc_content'];
} else {
if (isset($_POST['doc']['content'])) {
$doc_content = $_POST['doc']['content'];
}
}
// Check group associations
// @todo Move into group integration piece
if (bp_is_active('groups')) {
$associated_group_id = isset($_POST['associated_group_id']) ? intval($_POST['associated_group_id']) : 0;
if ($associated_group_id && !BP_Docs_Groups_Integration::user_can_associate_doc_with_group(bp_loggedin_user_id(), $associated_group_id)) {
bp_core_add_message(__('You are not allowed to associate a Doc with that group.', 'bp-docs'), 'error');
bp_core_redirect(bp_docs_get_create_link());
}
}
if (empty($_POST['doc']['title']) || empty($doc_content)) {
// Both the title and the content fields are required
$result['message'] = __('Both the title and the content fields are required.', 'bp-docs');
$result['redirect'] = $this->current_view;
} else {
// If both the title and content fields are filled in, we can proceed
$defaults = array('post_type' => $this->post_type_name, 'post_title' => $_POST['doc']['title'], 'post_name' => isset($_POST['doc']['permalink']) ? sanitize_title($_POST['doc']['permalink']) : sanitize_title($_POST['doc']['title']), 'post_content' => stripslashes(sanitize_post_field('post_content', $doc_content, 0, 'db')), 'post_status' => 'publish');
$r = wp_parse_args($args, $defaults);
if (empty($this->doc_slug)) {
$this->is_new_doc = true;
$r['post_author'] = bp_loggedin_user_id();
// This is a new doc
if (!($post_id = wp_insert_post($r))) {
$result['message'] = __('There was an error when creating the doc.', 'bp-docs');
$result['redirect'] = 'create';
} else {
$this->doc_id = $post_id;
$the_doc = get_post($this->doc_id);
$this->doc_slug = $the_doc->post_name;
// A normal, successful save
$result['message'] = __('Doc successfully created!', 'bp-docs');
$result['redirect'] = 'single';
}
} else {
$this->is_new_doc = false;
$doc = get_queried_object();
$this->doc_id = $doc->ID;
$r['ID'] = $this->doc_id;
// Make sure the post_name is set
if (empty($r['post_name'])) {
$r['post_name'] = sanitize_title($r['post_title']);
}
// Make sure the post_name is unique
$r['post_name'] = wp_unique_post_slug($r['post_name'], $this->doc_id, $r['post_status'], $this->post_type_name, $doc->post_parent);
$this->doc_slug = $r['post_name'];
if (!wp_update_post($r)) {
$result['message'] = __('There was an error when saving the doc.', 'bp-docs');
$result['redirect'] = 'edit';
} else {
// Remove the edit lock
delete_post_meta($this->doc_id, '_edit_lock');
// When the post has been autosaved, we need to leave a
// special success message
if (!empty($_POST['is_auto']) && $_POST['is_auto']) {
$result['message'] = __('You idled a bit too long while in Edit mode. In order to allow others to edit the doc you were working on, your changes have been autosaved. Click the Edit button to return to Edit mode.', 'bp-docs');
} else {
// A normal, successful save
$result['message'] = __('Doc successfully edited!', 'bp-docs');
}
$result['redirect'] = 'single';
}
$post_id = $this->doc_id;
}
}
// Add to a group, if necessary
if (isset($associated_group_id)) {
bp_docs_set_associated_group_id($post_id, $associated_group_id);
}
// Make sure the current user is added as one of the authors
wp_set_post_terms($post_id, $this->user_term_id, $this->associated_item_tax_name, true);
// Save the last editor id. We'll use this to create an activity item
update_post_meta($this->doc_id, 'bp_docs_last_editor', bp_loggedin_user_id());
// Save settings
//.........这里部分代码省略.........