| | 474 | * Retrieve the mode for a post |
| | 475 | * |
| | 476 | * @param int|object $post A post |
| | 477 | * |
| | 478 | * @return mixed The mode if successful. False if no mode is set. WP_Error if errors. |
| | 479 | */ |
| | 480 | function get_post_mode( $post ) { |
| | 481 | $post = get_post($post); |
| | 482 | |
| | 483 | $mode = wp_get_object_terms( $post->ID, 'post_mode', array('order' => 'none', 'fields' => 'names') ); |
| | 484 | |
| | 485 | if ( is_wp_error($mode) ) |
| | 486 | return $mode; |
| | 487 | |
| | 488 | if ( empty($mode) ) |
| | 489 | return false; |
| | 490 | |
| | 491 | return ( str_replace('mode-', '', $mode[0]) ); |
| | 492 | } |
| | 493 | |
| | 494 | /** Retrieve a list of registered post modes. |
| | 495 | * |
| | 496 | * @param string $post_type Limit modes to those registered for a given post type. Optional. Default is any post type. |
| | 497 | * @return array List of registered modes |
| | 498 | */ |
| | 499 | function get_post_modes( $post_type = null ) { |
| | 500 | global $wp_post_modes; |
| | 501 | |
| | 502 | if ( !isset($wp_post_modes) ) |
| | 503 | return array(); |
| | 504 | |
| | 505 | if ( null === $post_type ) |
| | 506 | return array_keys($wp_post_modes); |
| | 507 | |
| | 508 | $modes = array(); |
| | 509 | foreach ( $wp_post_modes as $mode => $types ) { |
| | 510 | if ( in_array($post_type, $types) ) |
| | 511 | $modes[] = $mode; |
| | 512 | } |
| | 513 | |
| | 514 | return $modes; |
| | 515 | } |
| | 516 | |
| | 517 | /** |
| | 518 | * Register support for a post mode. |
| | 519 | * |
| | 520 | * @param string|array $modes A mode or array of modes to register. Valid modes are 'aside', 'chat', 'gallery', 'image', 'link', 'quote', or 'video'. |
| | 521 | * @param string|array $post_types Post types to register $modes for. Default is 'post'. |
| | 522 | * @return mixed WP_Error on error. |
| | 523 | */ |
| | 524 | function register_post_mode( $modes, $post_types = array('post') ) { |
| | 525 | global $wp_post_modes; |
| | 526 | |
| | 527 | if ( !isset($wp_post_modes) ) |
| | 528 | $wp_post_modes = array(); |
| | 529 | |
| | 530 | $the_modes = array('aside', 'chat', 'gallery', 'image', 'link', 'quote', 'video'); |
| | 531 | // Dummy gettext calls to get translations for modes into the default catalog. |
| | 532 | // @todo gettext context. Move to an admin function? |
| | 533 | __('Aside'); __('Chat'); __('Gallery'); __('Image'); __('Link'); __('Quote'); __('Video'); |
| | 534 | |
| | 535 | foreach ( (array) $modes as $mode ) { |
| | 536 | if ( !in_array($mode, $the_modes) ) |
| | 537 | return new WP_Error('invalid_mode', __('Invalid mode')); |
| | 538 | $wp_post_modes[$mode] = (array) $post_types; |
| | 539 | } |
| | 540 | } |
| | 541 | |
| | 542 | /** |
| | 543 | * Assign a mode to a post |
| | 544 | * |
| | 545 | * @param int|object $post The post for which to assign a mode |
| | 546 | * @param string $mode A mode to assign. The mode must have already been registered with register_post_mode(). |
| | 547 | * @return mixed WP_Error on error. Array of affected term IDs on success. |
| | 548 | */ |
| | 549 | function set_post_mode( $post, $mode ) { |
| | 550 | global $wp_post_modes; |
| | 551 | |
| | 552 | $post = get_post($post); |
| | 553 | |
| | 554 | if ( empty($post) ) |
| | 555 | return new WP_Error('invalid_post', __('Invalid post')); |
| | 556 | |
| | 557 | if ( !isset($wp_post_nodes) || ! in_array($mode, $wp_post_modes) ) |
| | 558 | return new WP_Error('invalid_mode', __('Invalid mode')); |
| | 559 | |
| | 560 | return wp_set_post_terms($post_id, array('mode-' . $mode), 'post_mode'); |
| | 561 | } |
| | 562 | |
| | 563 | /** |