WordPress.org

Make WordPress Core

Ticket #14746: 14746.2.diff

File 14746.2.diff, 2.6 KB (added by ryan, 10 years ago)

register_post_mode(), set_post_mode(), on the fly term creation

  • wp-includes/taxonomy.php

     
    7070                'show_ui' => false,
    7171                '_builtin' => true,
    7272        ) ) ;
     73
     74        register_taxonomy( 'post_mode', array('post', 'page', 'attachment'), array(
     75                'hierarchical' => false,
     76                'labels' => array(
     77                        'name' => '',
     78                        'singular_name' => '',
     79                ),
     80                'query_var' => false,
     81                'rewrite' => false,
     82                'show_ui' => false,
     83                '_builtin' => true,
     84                'show_in_nav_menus' => false,
     85        ) ) ;
    7386}
    7487add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority
    7588
  • wp-includes/post.php

     
    471471}
    472472
    473473/**
     474 * Register support for a post mode.
     475 *
     476 * @param string|array $mode A mode or array of modes to register.  Valid modes are 'aside', 'chat', 'gallery', 'image', 'link', 'quote', or 'video'.
     477 * @param string|array
     478 * @return mixed WP_Error on error.
     479 */
     480function register_post_mode( $modes, $post_types = array('post', 'page', 'attachment') ) {
     481        global $wp_post_modes;
     482
     483        if ( !isset($wp_post_modes) )
     484                $wp_post_modes = array();
     485
     486        $the_modes = array('aside', 'chat', 'gallery', 'image', 'link', 'quote', 'video');
     487        // Dummy gettext calls to get translations for modes into the default catalog.
     488        // @todo gettext context.  Move to an admin function?
     489        __('Aside'); __('Chat'); __('Gallery'); __('Image'); __('Link'); __('Quote'); __('Video');
     490
     491        foreach ( (array) $modes as $mode ) {
     492                if ( !in_array($mode, $the_modes) )
     493                        return new WP_Error('invalid_mode', __('Invalid mode'));
     494                $wp_post_modes[$mode] = (array) $post_types;
     495        }
     496}
     497
     498/**
     499 * Assign a mode to a post
     500 *
     501 * @param int|object $post The post for which to assign a mode
     502 * @param string $mode  A mode to assign.  The mode must have already been registered with register_post_mode().
     503 * @return mixed WP_Error on error. Array of affected term IDs on success.
     504 */
     505function set_post_mode( $post, $mode ) {
     506        global $wp_post_modes;
     507
     508        $post = get_post($post);
     509
     510        if ( empty($post) )
     511                return new WP_Error('invalid_post', __('Invalid post'));
     512
     513        if ( !isset($wp_post_nodes) || ! in_array($mode, $wp_post_modes) )
     514                return new WP_Error('invalid_mode', __('Invalid mode'));
     515
     516        return wp_set_post_terms($post_id, array('mode-' . $mode), 'post_mode');
     517}
     518
     519/**
    474520 * Retrieve the post status based on the Post ID.
    475521 *
    476522 * If the post ID is of an attachment, then the parent post status will be given