| | 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 | */ |
| | 480 | function 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 | */ |
| | 505 | function 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 | /** |