Beispiel #1
0
/**
 * Insert an attachment.
 *
 * If you set the 'ID' in the $object parameter, it will mean that you are
 * updating and attempt to update the attachment. You can also set the
 * attachment name or title by setting the key 'post_name' or 'post_title'.
 *
 * You can set the dates for the attachment manually by setting the 'post_date'
 * and 'post_date_gmt' keys' values.
 *
 * By default, the comments will use the default settings for whether the
 * comments are allowed. You can close them manually or keep them open by
 * setting the value for the 'comment_status' key.
 *
 * The $object parameter can have the following:
 *     'post_status'   - Default is 'draft'. Can not be overridden, set the same as parent post.
 *     'post_type'     - Default is 'post', will be set to attachment. Can not override.
 *     'post_author'   - Default is current user ID. The ID of the user, who added the attachment.
 *     'ping_status'   - Default is the value in default ping status option. Whether the attachment
 *                       can accept pings.
 *     'post_parent'   - Default is 0. Can use $parent parameter or set this for the post it belongs
 *                       to, if any.
 *     'menu_order'    - Default is 0. The order it is displayed.
 *     'to_ping'       - Whether to ping.
 *     'pinged'        - Default is empty string.
 *     'post_password' - Default is empty string. The password to access the attachment.
 *     'guid'          - Global Unique ID for referencing the attachment.
 *     'post_content_filtered' - Attachment post content filtered.
 *     'post_excerpt'  - Attachment excerpt.
 *
 * @since 2.0.0
 * @uses $nxtdb
 * @uses $user_ID
 * @uses do_action() Calls 'edit_attachment' on $post_ID if this is an update.
 * @uses do_action() Calls 'add_attachment' on $post_ID if this is not an update.
 *
 * @param string|array $object Arguments to override defaults.
 * @param string $file Optional filename.
 * @param int $parent Parent post ID.
 * @return int Attachment ID.
 */
function nxt_insert_attachment($object, $file = false, $parent = 0)
{
    global $nxtdb, $user_ID;
    $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_ID, 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 'context' => '');
    $object = nxt_parse_args($object, $defaults);
    if (!empty($parent)) {
        $object['post_parent'] = $parent;
    }
    unset($object['filter']);
    $object = sanitize_post($object, 'db');
    // export array as variables
    extract($object, EXTR_SKIP);
    if (empty($post_author)) {
        $post_author = $user_ID;
    }
    $post_type = 'attachment';
    if (!in_array($post_status, array('inherit', 'private'))) {
        $post_status = 'inherit';
    }
    // Make sure we set a valid category.
    if (!isset($post_category) || 0 == count($post_category) || !is_array($post_category)) {
        // 'post' requires at least one category.
        if ('post' == $post_type) {
            $post_category = array(get_option('default_category'));
        } else {
            $post_category = array();
        }
    }
    // Are we updating or creating?
    if (!empty($ID)) {
        $update = true;
        $post_ID = (int) $ID;
    } else {
        $update = false;
        $post_ID = 0;
    }
    // Create a valid post name.
    if (empty($post_name)) {
        $post_name = sanitize_title($post_title);
    } else {
        $post_name = sanitize_title($post_name);
    }
    // expected_slashed ($post_name)
    $post_name = nxt_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
    if (empty($post_date)) {
        $post_date = current_time('mysql');
    }
    if (empty($post_date_gmt)) {
        $post_date_gmt = current_time('mysql', 1);
    }
    if (empty($post_modified)) {
        $post_modified = $post_date;
    }
    if (empty($post_modified_gmt)) {
        $post_modified_gmt = $post_date_gmt;
    }
    if (empty($comment_status)) {
        if ($update) {
            $comment_status = 'closed';
        } else {
            $comment_status = get_option('default_comment_status');
        }
    }
    if (empty($ping_status)) {
        $ping_status = get_option('default_ping_status');
    }
    if (isset($to_ping)) {
        $to_ping = preg_replace('|\\s+|', "\n", $to_ping);
    } else {
        $to_ping = '';
    }
    if (isset($post_parent)) {
        $post_parent = (int) $post_parent;
    } else {
        $post_parent = 0;
    }
    if (isset($menu_order)) {
        $menu_order = (int) $menu_order;
    } else {
        $menu_order = 0;
    }
    if (!isset($post_password)) {
        $post_password = '';
    }
    if (!isset($pinged)) {
        $pinged = '';
    }
    // expected_slashed (everything!)
    $data = compact(array('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'));
    $data = stripslashes_deep($data);
    if ($update) {
        $nxtdb->update($nxtdb->posts, $data, array('ID' => $post_ID));
    } else {
        // If there is a suggested ID, use it if not already present
        if (!empty($import_id)) {
            $import_id = (int) $import_id;
            if (!$nxtdb->get_var($nxtdb->prepare("SELECT ID FROM {$nxtdb->posts} WHERE ID = %d", $import_id))) {
                $data['ID'] = $import_id;
            }
        }
        $nxtdb->insert($nxtdb->posts, $data);
        $post_ID = (int) $nxtdb->insert_id;
    }
    if (empty($post_name)) {
        $post_name = sanitize_title($post_title, $post_ID);
        $nxtdb->update($nxtdb->posts, compact("post_name"), array('ID' => $post_ID));
    }
    nxt_set_post_categories($post_ID, $post_category);
    if ($file) {
        update_attached_file($post_ID, $file);
    }
    clean_post_cache($post_ID);
    if (!empty($context)) {
        add_post_meta($post_ID, '_nxt_attachment_context', $context, true);
    }
    if ($update) {
        do_action('edit_attachment', $post_ID);
    } else {
        do_action('add_attachment', $post_ID);
    }
    return $post_ID;
}
Beispiel #2
0
/**
 * {@internal Missing Short Description}}
 *
 * @since 2.5.0
 *
 * @param int|object $id    Post ID or post object.
 * @param string $title (optional) Title
 * @param string $name (optional) Name
 * @return array With two entries of type string
 */
function get_sample_permalink($id, $title = null, $name = null)
{
    $post =& get_post($id);
    if (!$post->ID) {
        return array('', '');
    }
    $ptype = get_post_type_object($post->post_type);
    $original_status = $post->post_status;
    $original_date = $post->post_date;
    $original_name = $post->post_name;
    // Hack: get_permalink would return ugly permalink for
    // drafts, so we will fake, that our post is published
    if (in_array($post->post_status, array('draft', 'pending'))) {
        $post->post_status = 'publish';
        $post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
    }
    // If the user wants to set a new name -- override the current one
    // Note: if empty name is supplied -- use the title instead, see #6072
    if (!is_null($name)) {
        $post->post_name = sanitize_title($name ? $name : $title, $post->ID);
    }
    $post->post_name = nxt_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
    $post->filter = 'sample';
    $permalink = get_permalink($post, true);
    // Replace custom post_type Token with generic pagename token for ease of use.
    $permalink = str_replace("%{$post->post_type}%", '%pagename%', $permalink);
    // Handle page hierarchy
    if ($ptype->hierarchical) {
        $uri = get_page_uri($post);
        $uri = untrailingslashit($uri);
        $uri = strrev(stristr(strrev($uri), '/'));
        $uri = untrailingslashit($uri);
        $uri = apply_filters('editable_slug', $uri);
        if (!empty($uri)) {
            $uri .= '/';
        }
        $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
    }
    $permalink = array($permalink, apply_filters('editable_slug', $post->post_name));
    $post->post_status = $original_status;
    $post->post_date = $original_date;
    $post->post_name = $original_name;
    unset($post->filter);
    return $permalink;
}