Make WordPress Core

Changeset 36652


Ignore:
Timestamp:
02/23/2016 10:49:17 PM (9 years ago)
Author:
swissspidy
Message:

Posts: Introduce get_post_types_by_support().

Similar to get_post_types(), this new function returns a list of post type names that support a specific feature.

Props wpsmith, barryceelen, swissspidy.
Fixes #34010.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r36607 r36652  
    16401640
    16411641/**
     1642 * Get a list of post type names that support a specific feature.
     1643 *
     1644 * @since 4.5.0
     1645 *
     1646 * @global array $_wp_post_type_features
     1647 *
     1648 * @param array|string $args     Single feature or an array of features the post types should support.
     1649 * @param string       $operator Optional. The logical operation to perform. 'or' means
     1650 *                               only one element from the array needs to match; 'and'
     1651 *                               means all elements must match; 'not' means no elements may
     1652 *                               match. Default 'and'.
     1653 * @return array A list of post type names.
     1654 */
     1655function get_post_types_by_support( $args, $operator = 'and' ) {
     1656    global $_wp_post_type_features;
     1657
     1658    $features = array_fill_keys( (array) $args, true );
     1659
     1660    return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) );
     1661}
     1662
     1663/**
    16421664 * Update the post type for the post ID.
    16431665 *
  • trunk/tests/phpunit/tests/post/types.php

    r36316 r36652  
    380380        $this->assertFalse( post_type_exists( 'foo' ) );
    381381    }
     382
     383    /**
     384     * @ticket 34010
     385     */
     386    public function test_get_post_types_by_support_single_feature() {
     387        $this->assertContains( 'post', get_post_types_by_support( 'title' ) );
     388        $this->assertContains( 'page', get_post_types_by_support( 'title' ) );
     389        $this->assertContains( 'attachment', get_post_types_by_support( 'title' ) );
     390        $this->assertContains( 'nav_menu_item', get_post_types_by_support( 'title' ) );
     391    }
     392
     393    /**
     394     * @ticket 34010
     395     */
     396    public function test_get_post_types_by_support_multiple_features() {
     397        $this->assertContains( 'post', get_post_types_by_support( array( 'thumbnail', 'author' ) ) );
     398        $this->assertContains( 'page', get_post_types_by_support( array( 'thumbnail', 'author' ) ) );
     399    }
     400
     401    /**
     402     * @ticket 34010
     403     */
     404    public function test_get_post_types_by_support_or_operator() {
     405        $this->assertContains( 'post', get_post_types_by_support( array( 'post-formats', 'page-attributes' ), 'or' ) );
     406        $this->assertContains( 'page', get_post_types_by_support( array( 'post-formats', 'page-attributes' ), 'or' ) );
     407    }
     408
     409    /**
     410     * @ticket 34010
     411     */
     412    public function test_get_post_types_by_support_not_operator() {
     413        $this->assertContains( 'attachment', get_post_types_by_support( array( 'thumbnail' ), 'not' ) );
     414        $this->assertContains( 'revision', get_post_types_by_support( array( 'thumbnail' ), 'not' ) );
     415        $this->assertContains( 'nav_menu_item', get_post_types_by_support( array( 'thumbnail' ), 'not' ) );
     416    }
     417
     418    /**
     419     * @ticket 34010
     420     */
     421    public function test_get_post_types_by_support_excluding_features() {
     422        $this->assertEqualSets( array(), get_post_types_by_support( array( 'post-formats', 'page-attributes' ) ) );
     423    }
     424
     425    /**
     426     * @ticket 34010
     427     */
     428    public function test_get_post_types_by_support_non_existant_feature() {
     429        $this->assertEqualSets( array(), get_post_types_by_support( 'somefeature' ) );
     430    }
    382431}
Note: See TracChangeset for help on using the changeset viewer.