Example #1
0
/**
 * Insert or update a post.
 *
 * If the $postarr parameter has 'ID' set to a value, then post will be updated.
 *
 * You can set the post date manually, by setting the values for 'post_date'
 * and 'post_date_gmt' keys. You can close the comments or open the comments by
 * setting the value for 'comment_status' key.
 *
 * @since 1.0.0
 * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt.
 * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data.
 *
 * @see sanitize_post()
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param array $postarr {
 *     An array of elements that make up a post to update or insert.
 *
 *     @type int    $ID                    The post ID. If equal to something other than 0,
 *                                         the post with that ID will be updated. Default 0.
 *     @type int    $post_author           The ID of the user who added the post. Default is
 *                                         the current user ID.
 *     @type string $post_date             The date of the post. Default is the current time.
 *     @type string $post_date_gmt         The date of the post in the GMT timezone. Default is
 *                                         the value of `$post_date`.
 *     @type mixed  $post_content          The post content. Default empty.
 *     @type string $post_content_filtered The filtered post content. Default empty.
 *     @type string $post_title            The post title. Default empty.
 *     @type string $post_excerpt          The post excerpt. Default empty.
 *     @type string $post_status           The post status. Default 'draft'.
 *     @type string $post_type             The post type. Default 'post'.
 *     @type string $comment_status        Whether the post can accept comments. Accepts 'open' or 'closed'.
 *                                         Default is the value of 'default_comment_status' option.
 *     @type string $ping_status           Whether the post can accept pings. Accepts 'open' or 'closed'.
 *                                         Default is the value of 'default_ping_status' option.
 *     @type string $post_password         The password to access the post. Default empty.
 *     @type string $post_name             The post name. Default is the sanitized post title.
 *     @type string $to_ping               Space or carriage return-separated list of URLs to ping.
 *                                         Default empty.
 *     @type string $pinged                Space or carriage return-separated list of URLs that have
 *                                         been pinged. Default empty.
 *     @type string $post_modified         The date when the post was last modified. Default is
 *                                         the current time.
 *     @type string $post_modified_gmt     The date when the post was last modified in the GMT
 *                                         timezone. Default is the current time.
 *     @type int    $post_parent           Set this for the post it belongs to, if any. Default 0.
 *     @type int    $menu_order            The order the post should be displayed in. Default 0.
 *     @type string $post_mime_type        The mime type of the post. Default empty.
 *     @type string $guid                  Global Unique ID for referencing the post. Default empty.
 *     @type array  $tax_input             Array of taxonomy terms keyed by their taxonomy name. Default empty.
 *     @type array  $meta_input            Array of post meta values keyed by their post meta key. Default empty.
 * }
 * @param bool  $wp_error Optional. Whether to allow return of WP_Error on failure. Default false.
 * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
 */
function wp_insert_post($postarr, $wp_error = false)
{
    global $wpdb;
    $user_id = get_current_user_id();
    $defaults = array('post_author' => $user_id, 'post_content' => '', 'post_content_filtered' => '', 'post_title' => '', 'post_excerpt' => '', 'post_status' => 'draft', 'post_type' => 'post', 'comment_status' => '', 'ping_status' => '', 'post_password' => '', 'to_ping' => '', 'pinged' => '', 'post_parent' => 0, 'menu_order' => 0, 'guid' => '', 'import_id' => 0, 'context' => '');
    $postarr = wp_parse_args($postarr, $defaults);
    unset($postarr['filter']);
    $postarr = sanitize_post($postarr, 'db');
    // Are we updating or creating?
    $post_ID = 0;
    $update = false;
    $guid = $postarr['guid'];
    if (!empty($postarr['ID'])) {
        $update = true;
        // Get the post ID and GUID.
        $post_ID = $postarr['ID'];
        $post_before = get_post($post_ID);
        if (is_null($post_before)) {
            if ($wp_error) {
                return new WP_Error('invalid_post', __('Invalid post ID.'));
            }
            return 0;
        }
        $guid = get_post_field('guid', $post_ID);
        $previous_status = get_post_field('post_status', $post_ID);
    } else {
        $previous_status = 'new';
    }
    $post_type = empty($postarr['post_type']) ? 'post' : $postarr['post_type'];
    $post_title = $postarr['post_title'];
    $post_content = $postarr['post_content'];
    $post_excerpt = $postarr['post_excerpt'];
    if (isset($postarr['post_name'])) {
        $post_name = $postarr['post_name'];
    }
    $maybe_empty = 'attachment' !== $post_type && !$post_content && !$post_title && !$post_excerpt && post_type_supports($post_type, 'editor') && post_type_supports($post_type, 'title') && post_type_supports($post_type, 'excerpt');
    /**
     * Filter whether the post should be considered "empty".
     *
     * The post is considered "empty" if both:
     * 1. The post type supports the title, editor, and excerpt fields
     * 2. The title, editor, and excerpt fields are all empty
     *
     * Returning a truthy value to the filter will effectively short-circuit
     * the new post being inserted, returning 0. If $wp_error is true, a WP_Error
     * will be returned instead.
     *
     * @since 3.3.0
     *
     * @param bool  $maybe_empty Whether the post should be considered "empty".
     * @param array $postarr     Array of post data.
     */
    if (apply_filters('wp_insert_post_empty_content', $maybe_empty, $postarr)) {
        if ($wp_error) {
            return new WP_Error('empty_content', __('Content, title, and excerpt are empty.'));
        } else {
            return 0;
        }
    }
    $post_status = empty($postarr['post_status']) ? 'draft' : $postarr['post_status'];
    if ('attachment' === $post_type && !in_array($post_status, array('inherit', 'private', 'trash'))) {
        $post_status = 'inherit';
    }
    if (!empty($postarr['post_category'])) {
        // Filter out empty terms.
        $post_category = array_filter($postarr['post_category']);
    }
    // Make sure we set a valid category.
    if (empty($post_category) || 0 == count($post_category) || !is_array($post_category)) {
        // 'post' requires at least one category.
        if ('post' == $post_type && 'auto-draft' != $post_status) {
            $post_category = array(get_option('default_category'));
        } else {
            $post_category = array();
        }
    }
    // Don't allow contributors to set the post slug for pending review posts.
    if ('pending' == $post_status && !current_user_can('publish_posts')) {
        $post_name = '';
    }
    /*
     * Create a valid post name. Drafts and pending posts are allowed to have
     * an empty post name.
     */
    if (empty($post_name)) {
        if (!in_array($post_status, array('draft', 'pending', 'auto-draft'))) {
            $post_name = sanitize_title($post_title);
        } else {
            $post_name = '';
        }
    } else {
        // On updates, we need to check to see if it's using the old, fixed sanitization context.
        $check_name = sanitize_title($post_name, '', 'old-save');
        if ($update && strtolower(urlencode($post_name)) == $check_name && get_post_field('post_name', $post_ID) == $check_name) {
            $post_name = $check_name;
        } else {
            // new post, or slug has changed.
            $post_name = sanitize_title($post_name);
        }
    }
    /*
     * If the post date is empty (due to having been new or a draft) and status
     * is not 'draft' or 'pending', set date to now.
     */
    if (empty($postarr['post_date']) || '0000-00-00 00:00:00' == $postarr['post_date']) {
        if (empty($postarr['post_date_gmt']) || '0000-00-00 00:00:00' == $postarr['post_date_gmt']) {
            $post_date = current_time('mysql');
        } else {
            $post_date = get_date_from_gmt($postarr['post_date_gmt']);
        }
    } else {
        $post_date = $postarr['post_date'];
    }
    // Validate the date.
    $mm = substr($post_date, 5, 2);
    $jj = substr($post_date, 8, 2);
    $aa = substr($post_date, 0, 4);
    $valid_date = wp_checkdate($mm, $jj, $aa, $post_date);
    if (!$valid_date) {
        if ($wp_error) {
            return new WP_Error('invalid_date', __('Whoops, the provided date is invalid.'));
        } else {
            return 0;
        }
    }
    if (empty($postarr['post_date_gmt']) || '0000-00-00 00:00:00' == $postarr['post_date_gmt']) {
        if (!in_array($post_status, array('draft', 'pending', 'auto-draft'))) {
            $post_date_gmt = get_gmt_from_date($post_date);
        } else {
            $post_date_gmt = '0000-00-00 00:00:00';
        }
    } else {
        $post_date_gmt = $postarr['post_date_gmt'];
    }
    if ($update || '0000-00-00 00:00:00' == $post_date) {
        $post_modified = current_time('mysql');
        $post_modified_gmt = current_time('mysql', 1);
    } else {
        $post_modified = $post_date;
        $post_modified_gmt = $post_date_gmt;
    }
    if ('attachment' !== $post_type) {
        if ('publish' == $post_status) {
            $now = gmdate('Y-m-d H:i:59');
            if (mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false)) {
                $post_status = 'future';
            }
        } elseif ('future' == $post_status) {
            $now = gmdate('Y-m-d H:i:59');
            if (mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false)) {
                $post_status = 'publish';
            }
        }
    }
    // Comment status.
    if (empty($postarr['comment_status'])) {
        if ($update) {
            $comment_status = 'closed';
        } else {
            $comment_status = get_default_comment_status($post_type);
        }
    } else {
        $comment_status = $postarr['comment_status'];
    }
    // These variables are needed by compact() later.
    $post_content_filtered = $postarr['post_content_filtered'];
    $post_author = isset($postarr['post_author']) ? $postarr['post_author'] : $user_id;
    $ping_status = empty($postarr['ping_status']) ? get_default_comment_status($post_type, 'pingback') : $postarr['ping_status'];
    $to_ping = isset($postarr['to_ping']) ? sanitize_trackback_urls($postarr['to_ping']) : '';
    $pinged = isset($postarr['pinged']) ? $postarr['pinged'] : '';
    $import_id = isset($postarr['import_id']) ? $postarr['import_id'] : 0;
    /*
     * The 'wp_insert_post_parent' filter expects all variables to be present.
     * Previously, these variables would have already been extracted
     */
    if (isset($postarr['menu_order'])) {
        $menu_order = (int) $postarr['menu_order'];
    } else {
        $menu_order = 0;
    }
    $post_password = isset($postarr['post_password']) ? $postarr['post_password'] : '';
    if ('private' == $post_status) {
        $post_password = '';
    }
    if (isset($postarr['post_parent'])) {
        $post_parent = (int) $postarr['post_parent'];
    } else {
        $post_parent = 0;
    }
    /**
     * Filter the post parent -- used to check for and prevent hierarchy loops.
     *
     * @since 3.1.0
     *
     * @param int   $post_parent Post parent ID.
     * @param int   $post_ID     Post ID.
     * @param array $new_postarr Array of parsed post data.
     * @param array $postarr     Array of sanitized, but otherwise unmodified post data.
     */
    $post_parent = apply_filters('wp_insert_post_parent', $post_parent, $post_ID, compact(array_keys($postarr)), $postarr);
    $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
    // Don't unslash.
    $post_mime_type = isset($postarr['post_mime_type']) ? $postarr['post_mime_type'] : '';
    // Expected_slashed (everything!).
    $data = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid');
    $emoji_fields = array('post_title', 'post_content', 'post_excerpt');
    foreach ($emoji_fields as $emoji_field) {
        if (isset($data[$emoji_field])) {
            $charset = $wpdb->get_col_charset($wpdb->posts, $emoji_field);
            if ('utf8' === $charset) {
                $data[$emoji_field] = wp_encode_emoji($data[$emoji_field]);
            }
        }
    }
    if ('attachment' === $post_type) {
        /**
         * Filter attachment post data before it is updated in or added to the database.
         *
         * @since 3.9.0
         *
         * @param array $data    An array of sanitized attachment post data.
         * @param array $postarr An array of unsanitized attachment post data.
         */
        $data = apply_filters('wp_insert_attachment_data', $data, $postarr);
    } else {
        /**
         * Filter slashed post data just before it is inserted into the database.
         *
         * @since 2.7.0
         *
         * @param array $data    An array of slashed post data.
         * @param array $postarr An array of sanitized, but otherwise unmodified post data.
         */
        $data = apply_filters('wp_insert_post_data', $data, $postarr);
    }
    $data = wp_unslash($data);
    $where = array('ID' => $post_ID);
    if ($update) {
        /**
         * Fires immediately before an existing post is updated in the database.
         *
         * @since 2.5.0
         *
         * @param int   $post_ID Post ID.
         * @param array $data    Array of unslashed post data.
         */
        do_action('pre_post_update', $post_ID, $data);
        if (false === $wpdb->update($wpdb->posts, $data, $where)) {
            if ($wp_error) {
                return new WP_Error('db_update_error', __('Could not update post in the database'), $wpdb->last_error);
            } else {
                return 0;
            }
        }
    } else {
        // If there is a suggested ID, use it if not already present.
        if (!empty($import_id)) {
            $import_id = (int) $import_id;
            if (!$wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE ID = %d", $import_id))) {
                $data['ID'] = $import_id;
            }
        }
        if (false === $wpdb->insert($wpdb->posts, $data)) {
            if ($wp_error) {
                return new WP_Error('db_insert_error', __('Could not insert post into the database'), $wpdb->last_error);
            } else {
                return 0;
            }
        }
        $post_ID = (int) $wpdb->insert_id;
        // Use the newly generated $post_ID.
        $where = array('ID' => $post_ID);
    }
    if (empty($data['post_name']) && !in_array($data['post_status'], array('draft', 'pending', 'auto-draft'))) {
        $data['post_name'] = wp_unique_post_slug(sanitize_title($data['post_title'], $post_ID), $post_ID, $data['post_status'], $post_type, $post_parent);
        $wpdb->update($wpdb->posts, array('post_name' => $data['post_name']), $where);
        clean_post_cache($post_ID);
    }
    if (is_object_in_taxonomy($post_type, 'category')) {
        wp_set_post_categories($post_ID, $post_category);
    }
    if (isset($postarr['tags_input']) && is_object_in_taxonomy($post_type, 'post_tag')) {
        wp_set_post_tags($post_ID, $postarr['tags_input']);
    }
    // New-style support for all custom taxonomies.
    if (!empty($postarr['tax_input'])) {
        foreach ($postarr['tax_input'] as $taxonomy => $tags) {
            $taxonomy_obj = get_taxonomy($taxonomy);
            if (!$taxonomy_obj) {
                /* translators: %s: taxonomy name */
                _doing_it_wrong(__FUNCTION__, sprintf(__('Invalid taxonomy: %s.'), $taxonomy), '4.4.0');
                continue;
            }
            // array = hierarchical, string = non-hierarchical.
            if (is_array($tags)) {
                $tags = array_filter($tags);
            }
            if (current_user_can($taxonomy_obj->cap->assign_terms)) {
                wp_set_post_terms($post_ID, $tags, $taxonomy);
            }
        }
    }
    if (!empty($postarr['meta_input'])) {
        foreach ($postarr['meta_input'] as $field => $value) {
            update_post_meta($post_ID, $field, $value);
        }
    }
    $current_guid = get_post_field('guid', $post_ID);
    // Set GUID.
    if (!$update && '' == $current_guid) {
        $wpdb->update($wpdb->posts, array('guid' => get_permalink($post_ID)), $where);
    }
    if ('attachment' === $postarr['post_type']) {
        if (!empty($postarr['file'])) {
            update_attached_file($post_ID, $postarr['file']);
        }
        if (!empty($postarr['context'])) {
            add_post_meta($post_ID, '_wp_attachment_context', $postarr['context'], true);
        }
    }
    clean_post_cache($post_ID);
    $post = get_post($post_ID);
    if (!empty($postarr['page_template']) && 'page' == $data['post_type']) {
        $post->page_template = $postarr['page_template'];
        $page_templates = wp_get_theme()->get_page_templates($post);
        if ('default' != $postarr['page_template'] && !isset($page_templates[$postarr['page_template']])) {
            if ($wp_error) {
                return new WP_Error('invalid_page_template', __('The page template is invalid.'));
            }
            update_post_meta($post_ID, '_wp_page_template', 'default');
        } else {
            update_post_meta($post_ID, '_wp_page_template', $postarr['page_template']);
        }
    }
    if ('attachment' !== $postarr['post_type']) {
        wp_transition_post_status($data['post_status'], $previous_status, $post);
    } else {
        if ($update) {
            /**
             * Fires once an existing attachment has been updated.
             *
             * @since 2.0.0
             *
             * @param int $post_ID Attachment ID.
             */
            do_action('edit_attachment', $post_ID);
            $post_after = get_post($post_ID);
            /**
             * Fires once an existing attachment has been updated.
             *
             * @since 4.4.0
             *
             * @param int     $post_ID      Post ID.
             * @param WP_Post $post_after   Post object following the update.
             * @param WP_Post $post_before  Post object before the update.
             */
            do_action('attachment_updated', $post_ID, $post_after, $post_before);
        } else {
            /**
             * Fires once an attachment has been added.
             *
             * @since 2.0.0
             *
             * @param int $post_ID Attachment ID.
             */
            do_action('add_attachment', $post_ID);
        }
        return $post_ID;
    }
    if ($update) {
        /**
         * Fires once an existing post has been updated.
         *
         * @since 1.2.0
         *
         * @param int     $post_ID Post ID.
         * @param WP_Post $post    Post object.
         */
        do_action('edit_post', $post_ID, $post);
        $post_after = get_post($post_ID);
        /**
         * Fires once an existing post has been updated.
         *
         * @since 3.0.0
         *
         * @param int     $post_ID      Post ID.
         * @param WP_Post $post_after   Post object following the update.
         * @param WP_Post $post_before  Post object before the update.
         */
        do_action('post_updated', $post_ID, $post_after, $post_before);
    }
    /**
     * Fires once a post has been saved.
     *
     * The dynamic portion of the hook name, `$post->post_type`, refers to
     * the post type slug.
     *
     * @since 3.7.0
     *
     * @param int     $post_ID Post ID.
     * @param WP_Post $post    Post object.
     * @param bool    $update  Whether this is an existing post being updated or not.
     */
    do_action("save_post_{$post->post_type}", $post_ID, $post, $update);
    /**
     * Fires once a post has been saved.
     *
     * @since 1.5.0
     *
     * @param int     $post_ID Post ID.
     * @param WP_Post $post    Post object.
     * @param bool    $update  Whether this is an existing post being updated or not.
     */
    do_action('save_post', $post_ID, $post, $update);
    /**
     * Fires once a post has been saved.
     *
     * @since 2.0.0
     *
     * @param int     $post_ID Post ID.
     * @param WP_Post $post    Post object.
     * @param bool    $update  Whether this is an existing post being updated or not.
     */
    do_action('wp_insert_post', $post_ID, $post, $update);
    return $post_ID;
}
Example #2
0
/**
 * Default post information to use when populating the "Write Post" form.
 *
 * @since 2.0.0
 *
 * @param string $post_type    Optional. A post type string. Default 'post'.
 * @param bool   $create_in_db Optional. Whether to insert the post into database. Default false.
 * @return WP_Post Post object containing all the default post data as attributes
 */
function get_default_post_to_edit($post_type = 'post', $create_in_db = false)
{
    $post_title = '';
    if (!empty($_REQUEST['post_title'])) {
        $post_title = esc_html(wp_unslash($_REQUEST['post_title']));
    }
    $post_content = '';
    if (!empty($_REQUEST['content'])) {
        $post_content = esc_html(wp_unslash($_REQUEST['content']));
    }
    $post_excerpt = '';
    if (!empty($_REQUEST['excerpt'])) {
        $post_excerpt = esc_html(wp_unslash($_REQUEST['excerpt']));
    }
    if ($create_in_db) {
        $post_id = wp_insert_post(array('post_title' => __('Auto Draft'), 'post_type' => $post_type, 'post_status' => 'auto-draft'));
        $post = get_post($post_id);
        if (current_theme_supports('post-formats') && post_type_supports($post->post_type, 'post-formats') && get_option('default_post_format')) {
            set_post_format($post, get_option('default_post_format'));
        }
    } else {
        $post = new stdClass();
        $post->ID = 0;
        $post->post_author = '';
        $post->post_date = '';
        $post->post_date_gmt = '';
        $post->post_password = '';
        $post->post_name = '';
        $post->post_type = $post_type;
        $post->post_status = 'draft';
        $post->to_ping = '';
        $post->pinged = '';
        $post->comment_status = get_default_comment_status($post_type);
        $post->ping_status = get_default_comment_status($post_type, 'pingback');
        $post->post_pingback = get_option('default_pingback_flag');
        $post->post_category = get_option('default_category');
        $post->page_template = 'default';
        $post->post_parent = 0;
        $post->menu_order = 0;
        $post = new WP_Post($post);
    }
    /**
     * Filters the default post content initially used in the "Write Post" form.
     *
     * @since 1.5.0
     *
     * @param string  $post_content Default post content.
     * @param WP_Post $post         Post object.
     */
    $post->post_content = apply_filters('default_content', $post_content, $post);
    /**
     * Filters the default post title initially used in the "Write Post" form.
     *
     * @since 1.5.0
     *
     * @param string  $post_title Default post title.
     * @param WP_Post $post       Post object.
     */
    $post->post_title = apply_filters('default_title', $post_title, $post);
    /**
     * Filters the default post excerpt initially used in the "Write Post" form.
     *
     * @since 1.5.0
     *
     * @param string  $post_excerpt Default post excerpt.
     * @param WP_Post $post         Post object.
     */
    $post->post_excerpt = apply_filters('default_excerpt', $post_excerpt, $post);
    return $post;
}
 /**
  * Edit a post.
  *
  * @since 1.5.0
  *
  * @param array  $args {
  *     Method arguments. Note: arguments must be ordered as documented.
  *
  *     @type int    $blog_id (unused)
  *     @type string $username
  *     @type string $password
  *     @type array  $content_struct
  *     @type int    $publish
  * }
  * @return bool|IXR_Error True on success.
  */
 public function mw_editPost($args)
 {
     $this->escape($args);
     $post_ID = (int) $args[0];
     $username = $args[1];
     $password = $args[2];
     $content_struct = $args[3];
     $publish = isset($args[4]) ? $args[4] : 0;
     if (!($user = $this->login($username, $password))) {
         return $this->error;
     }
     /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
     do_action('xmlrpc_call', 'metaWeblog.editPost');
     $postdata = get_post($post_ID, ARRAY_A);
     /*
      * If there is no post data for the give post id, stop now and return an error.
      * Otherwise a new post will be created (which was the old behavior).
      */
     if (!$postdata || empty($postdata['ID'])) {
         return new IXR_Error(404, __('Invalid post ID.'));
     }
     if (!current_user_can('edit_post', $post_ID)) {
         return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
     }
     // Use wp.editPost to edit post types other than post and page.
     if (!in_array($postdata['post_type'], array('post', 'page'))) {
         return new IXR_Error(401, __('Invalid post type'));
     }
     // Thwart attempt to change the post type.
     if (!empty($content_struct['post_type']) && $content_struct['post_type'] != $postdata['post_type']) {
         return new IXR_Error(401, __('The post type may not be changed.'));
     }
     // Check for a valid post format if one was given
     if (isset($content_struct['wp_post_format'])) {
         $content_struct['wp_post_format'] = sanitize_key($content_struct['wp_post_format']);
         if (!array_key_exists($content_struct['wp_post_format'], get_post_format_strings())) {
             return new IXR_Error(404, __('Invalid post format'));
         }
     }
     $this->escape($postdata);
     $ID = $postdata['ID'];
     $post_content = $postdata['post_content'];
     $post_title = $postdata['post_title'];
     $post_excerpt = $postdata['post_excerpt'];
     $post_password = $postdata['post_password'];
     $post_parent = $postdata['post_parent'];
     $post_type = $postdata['post_type'];
     $menu_order = $postdata['menu_order'];
     // Let WordPress manage slug if none was provided.
     $post_name = $postdata['post_name'];
     if (isset($content_struct['wp_slug'])) {
         $post_name = $content_struct['wp_slug'];
     }
     // Only use a password if one was given.
     if (isset($content_struct['wp_password'])) {
         $post_password = $content_struct['wp_password'];
     }
     // Only set a post parent if one was given.
     if (isset($content_struct['wp_page_parent_id'])) {
         $post_parent = $content_struct['wp_page_parent_id'];
     }
     // Only set the menu_order if it was given.
     if (isset($content_struct['wp_page_order'])) {
         $menu_order = $content_struct['wp_page_order'];
     }
     $page_template = null;
     if (!empty($content_struct['wp_page_template']) && 'page' == $post_type) {
         $page_template = $content_struct['wp_page_template'];
     }
     $post_author = $postdata['post_author'];
     // Only set the post_author if one is set.
     if (isset($content_struct['wp_author_id'])) {
         // Check permissions if attempting to switch author to or from another user.
         if ($user->ID != $content_struct['wp_author_id'] || $user->ID != $post_author) {
             switch ($post_type) {
                 case 'post':
                     if (!current_user_can('edit_others_posts')) {
                         return new IXR_Error(401, __('You are not allowed to change the post author as this user.'));
                     }
                     break;
                 case 'page':
                     if (!current_user_can('edit_others_pages')) {
                         return new IXR_Error(401, __('You are not allowed to change the page author as this user.'));
                     }
                     break;
                 default:
                     return new IXR_Error(401, __('Invalid post type'));
             }
             $post_author = $content_struct['wp_author_id'];
         }
     }
     if (isset($content_struct['mt_allow_comments'])) {
         if (!is_numeric($content_struct['mt_allow_comments'])) {
             switch ($content_struct['mt_allow_comments']) {
                 case 'closed':
                     $comment_status = 'closed';
                     break;
                 case 'open':
                     $comment_status = 'open';
                     break;
                 default:
                     $comment_status = get_default_comment_status($post_type);
                     break;
             }
         } else {
             switch ((int) $content_struct['mt_allow_comments']) {
                 case 0:
                 case 2:
                     $comment_status = 'closed';
                     break;
                 case 1:
                     $comment_status = 'open';
                     break;
                 default:
                     $comment_status = get_default_comment_status($post_type);
                     break;
             }
         }
     }
     if (isset($content_struct['mt_allow_pings'])) {
         if (!is_numeric($content_struct['mt_allow_pings'])) {
             switch ($content_struct['mt_allow_pings']) {
                 case 'closed':
                     $ping_status = 'closed';
                     break;
                 case 'open':
                     $ping_status = 'open';
                     break;
                 default:
                     $ping_status = get_default_comment_status($post_type, 'pingback');
                     break;
             }
         } else {
             switch ((int) $content_struct["mt_allow_pings"]) {
                 case 0:
                     $ping_status = 'closed';
                     break;
                 case 1:
                     $ping_status = 'open';
                     break;
                 default:
                     $ping_status = get_default_comment_status($post_type, 'pingback');
                     break;
             }
         }
     }
     if (isset($content_struct['title'])) {
         $post_title = $content_struct['title'];
     }
     if (isset($content_struct['description'])) {
         $post_content = $content_struct['description'];
     }
     $post_category = array();
     if (isset($content_struct['categories'])) {
         $catnames = $content_struct['categories'];
         if (is_array($catnames)) {
             foreach ($catnames as $cat) {
                 $post_category[] = get_cat_ID($cat);
             }
         }
     }
     if (isset($content_struct['mt_excerpt'])) {
         $post_excerpt = $content_struct['mt_excerpt'];
     }
     $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
     $post_status = $publish ? 'publish' : 'draft';
     if (isset($content_struct["{$post_type}_status"])) {
         switch ($content_struct["{$post_type}_status"]) {
             case 'draft':
             case 'pending':
             case 'private':
             case 'publish':
                 $post_status = $content_struct["{$post_type}_status"];
                 break;
             default:
                 $post_status = $publish ? 'publish' : 'draft';
                 break;
         }
     }
     $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
     if ('publish' == $post_status || 'private' == $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.'));
         } elseif (!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 = null;
     if (isset($content_struct['mt_tb_ping_urls'])) {
         $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'])) {
         // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
         $dateCreated = rtrim($content_struct['date_created_gmt']->getIso(), 'Z') . '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'])) {
         $data = $newpost;
         $data['sticky'] = $content_struct['sticky'];
         $data['post_type'] = 'post';
         $error = $this->_toggle_sticky($data, true);
         if ($error) {
             return $error;
         }
     }
     if (isset($content_struct['custom_fields'])) {
         $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
     }
     if (isset($content_struct['wp_post_thumbnail'])) {
         // Empty value deletes, non-empty value adds/updates.
         if (empty($content_struct['wp_post_thumbnail'])) {
             delete_post_thumbnail($post_ID);
         } else {
             if (set_post_thumbnail($post_ID, $content_struct['wp_post_thumbnail']) === false) {
                 return new IXR_Error(404, __('Invalid attachment ID.'));
             }
         }
         unset($content_struct['wp_post_thumbnail']);
     }
     // Handle enclosures.
     $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
     $this->add_enclosure_if_new($post_ID, $thisEnclosure);
     $this->attach_uploads($ID, $post_content);
     // Handle post formats if assigned, validation is handled earlier in this function.
     if (isset($content_struct['wp_post_format'])) {
         set_post_format($post_ID, $content_struct['wp_post_format']);
     }
     /**
      * Fires after a post has been successfully updated via the XML-RPC MovableType API.
      *
      * @since 3.4.0
      *
      * @param int   $post_ID ID of the updated post.
      * @param array $args    An array of arguments to update the post.
      */
     do_action('xmlrpc_call_success_mw_editPost', $post_ID, $args);
     return true;
 }
Example #4
0
     $error_msg = false;
     // For output of the quickdraft dashboard widget
     require_once ABSPATH . 'wp-admin/includes/dashboard.php';
     if (!wp_verify_nonce($nonce, 'add-post')) {
         $error_msg = __('Unable to submit this form, please refresh and try again.');
     }
     if (!current_user_can(get_post_type_object('post')->cap->create_posts)) {
         exit;
     }
     if ($error_msg) {
         return wp_dashboard_quick_press($error_msg);
     }
     $post = get_post($_REQUEST['post_ID']);
     check_admin_referer('add-' . $post->post_type);
     $_POST['comment_status'] = get_default_comment_status($post->post_type);
     $_POST['ping_status'] = get_default_comment_status($post->post_type, 'pingback');
     edit_post();
     wp_dashboard_quick_press();
     exit;
 case 'postajaxpost':
 case 'post':
     check_admin_referer('add-' . $post_type);
     $post_id = 'postajaxpost' == $action ? edit_post() : write_post();
     redirect_post($post_id);
     exit;
 case 'edit':
     $editing = true;
     if (empty($post_id)) {
         wp_redirect(admin_url('post.php'));
         exit;
     }
Example #5
0
    }
}
// Comment status.
if (empty($postarr['comment_status'])) {
    if ($update) {
        $comment_status = 'closed';
    } else {
        $comment_status = get_default_comment_status($post_type);
    }
} else {
    $comment_status = $postarr['comment_status'];
}
// These variables are needed by compact() later.
$post_content_filtered = $postarr['post_content_filtered'];
$post_author = isset($postarr['post_author']) ? $postarr['post_author'] : $user_id;
$ping_status = empty($postarr['ping_status']) ? get_default_comment_status($post_type, 'pingback') : $postarr['ping_status'];
$to_ping = isset($postarr['to_ping']) ? sanitize_trackback_urls($postarr['to_ping']) : '';
$pinged = isset($postarr['pinged']) ? $postarr['pinged'] : '';
$import_id = isset($postarr['import_id']) ? $postarr['import_id'] : 0;
/*
 * The 'wp_insert_post_parent' filter expects all variables to be present.
 * Previously, these variables would have already been extracted
 */
if (isset($postarr['menu_order'])) {
    $menu_order = (int) $postarr['menu_order'];
} else {
    $menu_order = 0;
}
$post_password = isset($postarr['post_password']) ? $postarr['post_password'] : '';
if ('private' == $post_status) {
    $post_password = '';