Make WordPress Core

Ticket #37667: 37667.2.diff

File 37667.2.diff, 18.8 KB (added by flixos90, 10 years ago)
  • src/wp-includes/class-wp-post-type.php

     
    325325        public $rewrite;
    326326
    327327        /**
    328          * The features supported by the post type.
     328         * The features supported by the post type, per post type definition.
    329329         *
    330330         * @since 4.6.0
    331331         * @access public
     
    334334        public $supports;
    335335
    336336        /**
     337         * All features supported by the post type.
     338         *
     339         * @since 4.7.0
     340         * @access private
     341         * @var array $features
     342         */
     343        private $features = array();
     344
     345        /**
    337346         * Constructor.
    338347         *
    339348         * Will populate object properties from the provided arguments and assign other
     
    342351         * @since 4.6.0
    343352         * @access public
    344353         *
     354         * @global array $_wp_post_type_features
     355         *
    345356         * @see register_post_type()
    346357         *
    347358         * @param string       $post_type Post type key.
     
    351362        public function __construct( $post_type, $args = array() ) {
    352363                $this->name = $post_type;
    353364
     365                if ( ! empty( $GLOBALS['_wp_post_type_features'][ $this->name ] ) ) {
     366                        $this->features = $GLOBALS['_wp_post_type_features'][ $this->name ];
     367                        unset( $GLOBALS['_wp_post_type_features'][ $this->name ] );
     368                }
     369
    354370                $this->set_props( $args );
    355371        }
    356372
     
    511527         */
    512528        public function add_supports() {
    513529                if ( ! empty( $this->supports ) ) {
    514                         add_post_type_support( $this->name, $this->supports );
     530                        $this->add_support( $this->supports );
    515531                        unset( $this->supports );
    516532                } elseif ( false !== $this->supports ) {
    517533                        // Add default features.
    518                         add_post_type_support( $this->name, array( 'title', 'editor' ) );
     534                        $this->add_support( array( 'title', 'editor' ) );
    519535                }
    520536        }
    521537
     
    602618        }
    603619
    604620        /**
     621         * Registers support of certain features for the post type.
     622         *
     623         * All core features are directly associated with a functional area of the edit
     624         * screen, such as the editor or a meta box. Features include: 'title', 'editor',
     625         * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes',
     626         * 'thumbnail', 'custom-fields', and 'post-formats'.
     627         *
     628         * Additionally, the 'revisions' feature dictates whether the post type will
     629         * store revisions, and the 'comments' feature dictates whether the comments
     630         * count will show on the edit screen.
     631         *
     632         * @since 4.7.0
     633         * @access public
     634         *
     635         * @param string|array $feature The feature being added, accepts an array of
     636         *                              feature strings or a single string.
     637         * @param bool         $value   Optional. The value to give to the feature. Default
     638         *                              true.
     639         * @param string       $subtype Optional. A subtype to add this feature to. An
     640         *                              empty value will add this feature to the overall
     641         *                              post type. Default empty.
     642         */
     643        public function add_support( $feature, $value = true, $subtype = '' ) {
     644                $features = (array) $feature;
     645                foreach ( $features as $feature ) {
     646                        $this->features[ $subtype ][ $feature ] = $value;
     647                }
     648        }
     649
     650        /**
     651         * Removes support for a feature from the post type.
     652         *
     653         * @since 4.7.0
     654         * @access public
     655         *
     656         * @param string $feature The feature being removed.
     657         * @param string $subtype Optional. A subtype to remove this feature from. An
     658         *                        empty value will remove this feature from the overall
     659         *                        post type. Default empty.
     660         */
     661        public function remove_support( $feature, $subtype = '' ) {
     662                unset( $this->features[ $subtype ][ $feature ] );
     663        }
     664
     665        /**
     666         * Gets all the post type features.
     667         *
     668         * @since 4.7.0
     669         * @access public
     670         *
     671         * @param string $subtype Optional. A subtype to get its features. An
     672         *                        empty value will return the overall post type
     673         *                        features. Default empty.
     674         * @return array Post type supports list.
     675         */
     676        public function get_all_supports( $subtype = '' ) {
     677                return $this->features[ $subtype ];
     678        }
     679
     680        /**
     681         * Checks the post type's support for a given feature.
     682         *
     683         * @since 4.7.0
     684         * @access public
     685         *
     686         * @param string $feature The feature being checked.
     687         * @param string $subtype Optional. A subtype to check in its features. An
     688         *                        empty value will check within the overall post type
     689         *                        features. Default empty.
     690         * @return bool Whether the post type supports the given feature.
     691         */
     692        public function supports( $feature, $subtype = '' ) {
     693                return isset( $this->features[ $subtype ][ $feature ] );
     694        }
     695
     696        /**
     697         * Retrieves a list of post type and subtype names that support a specific feature.
     698         *
     699         * If the features are supported by the overall post type, the post type name is
     700         * included in the list. If the features are supported by a specific subtype, a
     701         * string consisting of post type name and subtype name delimited by a colon is
     702         * included in the list.
     703         *
     704         * @since 4.7.0
     705         * @access public
     706         *
     707         * @param array|string $feature  Single feature or an array of features the post type should support.
     708         * @param string       $operator Optional. The logical operation to perform. 'or' means
     709         *                               only one element from the array needs to match; 'and'
     710         *                               means all elements must match; 'not' means no elements may
     711         *                               match. Default 'and'.
     712         * @return array A list of post type and subtype names.
     713         */
     714        public function get_by_support( $feature, $operator = 'and' ) {
     715                $features = array_fill_keys( (array) $feature, true );
     716                $subtypes = array_keys( wp_filter_object_list( $this->features, $features, $operator ) );
     717
     718                $names = array();
     719                foreach ( $subtypes as $subtype ) {
     720                        if ( empty( $subtype ) ) {
     721                                $names[] = $this->name;
     722                        } else {
     723                                $names[] = $this->name . ':' . $subtype;
     724                        }
     725                }
     726
     727                return $names;
     728        }
     729
     730        /**
    605731         * Removes the features support for the post type.
    606732         *
    607733         * @since 4.6.0
    608734         * @access public
    609          *
    610          * @global array $_wp_post_type_features Post type features.
    611735         */
    612736        public function remove_supports() {
    613                 global $_wp_post_type_features;
    614 
    615                 unset( $_wp_post_type_features[ $this->name ] );
     737                $this->features = array();
    616738        }
    617739
    618740        /**
  • src/wp-includes/post.php

     
    13981398 * count will show on the edit screen.
    13991399 *
    14001400 * @since 3.0.0
     1401 * @since 4.7.0 Uses `WP_Post_Type` to handle features.
    14011402 *
    14021403 * @global array $_wp_post_type_features
    14031404 *
     
    14061407 *                                feature strings or a single string.
    14071408 */
    14081409function add_post_type_support( $post_type, $feature ) {
    1409         global $_wp_post_type_features;
     1410        $subtype = '';
     1411        if ( strpos( $post_type, ':' ) ) {
     1412                list( $post_type, $subtype ) = explode( ':', $post_type );
     1413        }
    14101414
    1411         $features = (array) $feature;
    1412         foreach ($features as $feature) {
    1413                 if ( func_num_args() == 2 )
    1414                         $_wp_post_type_features[$post_type][$feature] = true;
    1415                 else
    1416                         $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 );
     1415        if ( func_num_args() == 2 ) {
     1416                $value = true;
     1417        } else {
     1418                $value = array_slice( func_get_args(), 2 );
    14171419        }
     1420
     1421        $ptype_obj = get_post_type_object( $post_type );
     1422        if ( ! $ptype_obj ) {
     1423                _doing_it_wrong( __FUNCTION__, sprintf( __( 'The post type %s is not registered. Post type features should only be added to existing post types.' ), $post_type ), '4.7.0' );
     1424
     1425                $GLOBALS['_wp_post_type_features'][ $post_type ][ $subtype ][ $feature ] = $value;
     1426                return;
     1427        }
     1428
     1429        $ptype_obj->add_support( $feature, $value, $subtype );
    14181430}
    14191431
    14201432/**
     
    14211433 * Remove support for a feature from a post type.
    14221434 *
    14231435 * @since 3.0.0
     1436 * @since 4.7.0 Uses `WP_Post_Type` to handle features.
    14241437 *
    14251438 * @global array $_wp_post_type_features
    14261439 *
     
    14281441 * @param string $feature   The feature being removed.
    14291442 */
    14301443function remove_post_type_support( $post_type, $feature ) {
    1431         global $_wp_post_type_features;
     1444        $subtype = '';
     1445        if ( strpos( $post_type, ':' ) ) {
     1446                list( $post_type, $subtype ) = explode( ':', $post_type );
     1447        }
    14321448
    1433         unset( $_wp_post_type_features[ $post_type ][ $feature ] );
     1449        $ptype_obj = get_post_type_object( $post_type );
     1450        if ( ! $ptype_obj ) {
     1451                _doing_it_wrong( __FUNCTION__, sprintf( __( 'The post type %s is not registered. Post type features should only be removed from existing post types.' ), $post_type ), '4.7.0' );
     1452
     1453                unset( $GLOBALS['_wp_post_type_features'][ $post_type ][ $subtype ][ $feature ] );
     1454                return;
     1455        }
     1456
     1457        $ptype_obj->remove_support( $feature, $subtype );
    14341458}
    14351459
    14361460/**
     
    14371461 * Get all the post type features
    14381462 *
    14391463 * @since 3.4.0
     1464 * @since 4.7.0 Uses `WP_Post_Type` to handle features.
    14401465 *
    14411466 * @global array $_wp_post_type_features
    14421467 *
     
    14441469 * @return array Post type supports list.
    14451470 */
    14461471function get_all_post_type_supports( $post_type ) {
    1447         global $_wp_post_type_features;
     1472        $subtype = '';
     1473        if ( strpos( $post_type, ':' ) ) {
     1474                list( $post_type, $subtype ) = explode( ':', $post_type );
     1475        }
    14481476
    1449         if ( isset( $_wp_post_type_features[$post_type] ) )
    1450                 return $_wp_post_type_features[$post_type];
     1477        $ptype_obj = get_post_type_object( $post_type );
     1478        if ( ! $ptype_obj ) {
     1479                if ( isset( $GLOBALS['_wp_post_type_features'][ $post_type ][ $subtype ] ) ) {
     1480                        return $GLOBALS['_wp_post_type_features'][ $post_type ][ $subtype ];
     1481                }
     1482                return array();
     1483        }
    14511484
    1452         return array();
     1485        return $ptype_obj->get_all_supports( $subtype );
    14531486}
    14541487
    14551488/**
     
    14561489 * Check a post type's support for a given feature.
    14571490 *
    14581491 * @since 3.0.0
     1492 * @since 4.7.0 Uses `WP_Post_Type` to handle features.
    14591493 *
    14601494 * @global array $_wp_post_type_features
    14611495 *
     
    14641498 * @return bool Whether the post type supports the given feature.
    14651499 */
    14661500function post_type_supports( $post_type, $feature ) {
    1467         global $_wp_post_type_features;
     1501        $subtype = '';
     1502        if ( strpos( $post_type, ':' ) ) {
     1503                list( $post_type, $subtype ) = explode( ':', $post_type );
     1504        }
    14681505
    1469         return ( isset( $_wp_post_type_features[$post_type][$feature] ) );
     1506        $ptype_obj = get_post_type_object( $post_type );
     1507        if ( ! $ptype_obj ) {
     1508                return isset( $GLOBALS['_wp_post_type_features'][ $post_type ][ $subtype ][ $feature ] );
     1509        }
     1510
     1511        return $ptype_obj->supports( $feature, $subtype );
    14701512}
    14711513
    14721514/**
     
    14731515 * Retrieves a list of post type names that support a specific feature.
    14741516 *
    14751517 * @since 4.5.0
     1518 * @since 4.7.0 Uses `WP_Post_Type` to handle features.
    14761519 *
    1477  * @global array $_wp_post_type_features Post type features
     1520 * @global array $_wp_post_type_features
    14781521 *
    14791522 * @param array|string $feature  Single feature or an array of features the post types should support.
    14801523 * @param string       $operator Optional. The logical operation to perform. 'or' means
     
    14841527 * @return array A list of post type names.
    14851528 */
    14861529function get_post_types_by_support( $feature, $operator = 'and' ) {
    1487         global $_wp_post_type_features;
     1530        $names = array();
    14881531
    1489         $features = array_fill_keys( (array) $feature, true );
     1532        $ptype_objects = get_post_types( array(), 'objects' );
     1533        foreach ( $ptype_objects as $ptype_obj ) {
     1534                $names = array_merge( $names, $ptype_obj->get_by_support( $feature, $operator ) );
     1535        }
    14901536
    1491         return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) );
     1537        if ( ! empty( $GLOBALS['_wp_post_type_features'] ) ) {
     1538                $features = array_fill_keys( (array) $feature, true );
     1539
     1540                foreach ( $GLOBALS['_wp_post_type_features'] as $post_type => $subtypes ) {
     1541                        $subtypes = array_keys( wp_filter_object_list( $subtypes, $features, $operator ) );
     1542                        foreach ( $subtypes as $subtype ) {
     1543                                if ( empty( $subtype ) ) {
     1544                                        $names[] = $post_type;
     1545                                } else {
     1546                                        $names[] = $post_type . ':' . $subtype;
     1547                                }
     1548                        }
     1549                }
     1550        }
     1551
     1552        return $names;
    14921553}
    14931554
    14941555/**
  • tests/phpunit/tests/post/typeSupport.php

     
     1<?php
     2
     3/**
     4 * @group post
     5 */
     6class Tests_Post_Type_Support extends WP_UnitTestCase {
     7
     8        /**
     9         * @ticket 37667
     10         */
     11        public function test_add_post_type_support_basic() {
     12                $post_type = rand_str( 20 );
     13                $feature = 'custom_feature_name';
     14
     15                register_post_type( $post_type );
     16                add_post_type_support( $post_type, $feature );
     17                $this->assertTrue( post_type_supports( $post_type, $feature ) );
     18        }
     19
     20        /**
     21         * @ticket 37667
     22         * @expectedIncorrectUsage add_post_type_support
     23         */
     24        public function test_add_post_type_support_before_register_post_type() {
     25                $post_type = rand_str( 20 );
     26                $feature = 'custom_feature_name';
     27
     28                add_post_type_support( $post_type, $feature );
     29                $this->assertTrue( post_type_supports( $post_type, $feature ) );
     30
     31                register_post_type( $post_type );
     32                $this->assertTrue( post_type_supports( $post_type, $feature ) );
     33        }
     34
     35        /**
     36         * @ticket 37667
     37         */
     38        public function test_remove_post_type_support_basic() {
     39                $post_type = rand_str( 20 );
     40                $feature = 'editor';
     41
     42                register_post_type( $post_type );
     43                remove_post_type_support( $post_type, $feature );
     44                $this->assertFalse( post_type_supports( $post_type, $feature ) );
     45        }
     46
     47        /**
     48         * @ticket 37667
     49         * @expectedIncorrectUsage remove_post_type_support
     50         */
     51        public function test_remove_post_type_support_before_register_post_type() {
     52                $post_type = rand_str( 20 );
     53                $feature = 'custom_feature_name';
     54                $default_feature = 'editor';
     55
     56                remove_post_type_support( $post_type, $feature );
     57                $this->assertFalse( post_type_supports( $post_type, $feature ) );
     58
     59                // Features are added on post type registration, so removing them before shouldn't matter.
     60                remove_post_type_support( $post_type, $default_feature );
     61                register_post_type( $post_type );
     62                $this->assertTrue( post_type_supports( $post_type, $default_feature ) );
     63        }
     64
     65        /**
     66         * @ticket 37667
     67         * @expectedIncorrectUsage add_post_type_support
     68         */
     69        public function test_get_post_types_by_support_mixed() {
     70                $registered_post_type = rand_str( 20 );
     71                $unregistered_post_type = rand_str( 20 );
     72                $feature = 'custom_feature_name';
     73
     74                register_post_type( $registered_post_type );
     75
     76                add_post_type_support( $registered_post_type, $feature );
     77                add_post_type_support( $unregistered_post_type, $feature );
     78                $this->assertEqualSets( array( $registered_post_type, $unregistered_post_type ), get_post_types_by_support( $feature ) );
     79        }
     80}
  • tests/phpunit/tests/post/wpPostType.php

    Property changes on: tests/phpunit/tests/post/typeSupport.php
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
     
    148148                $this->assertEqualSets( array( 'post_tag' ), $taxonomies );
    149149                $this->assertEqualSets( array(), $taxonomies_after );
    150150        }
     151
     152        /**
     153         * @ticket 37667
     154         */
     155        public function test_add_support_basic() {
     156                $post_type = rand_str( 20 );
     157                $post_type_object = register_post_type( $post_type );
     158
     159                $feature = 'custom_feature_name';
     160
     161                $post_type_object->add_support( $feature );
     162                $this->assertTrue( post_type_supports( $post_type, $feature ) );
     163        }
     164
     165        /**
     166         * @ticket 37667
     167         */
     168        public function test_add_support_with_subtype() {
     169                $post_type = rand_str( 20 );
     170                $post_type_object = register_post_type( $post_type );
     171
     172                $subtype = rand_str();
     173                $feature = 'custom_feature_name';
     174
     175                $post_type_object->add_support( $feature, true, $subtype );
     176                $this->assertTrue( post_type_supports( $post_type . ':' . $subtype, $feature ) );
     177        }
     178
     179        /**
     180         * @ticket 37667
     181         */
     182        public function test_remove_support_basic() {
     183                $post_type = rand_str( 20 );
     184                $post_type_object = register_post_type( $post_type );
     185
     186                $feature = 'editor';
     187
     188                $post_type_object->remove_support( $feature );
     189                $this->assertFalse( post_type_supports( $post_type, $feature ) );
     190        }
     191
     192        /**
     193         * @ticket 37667
     194         */
     195        public function test_remove_support_with_subtype() {
     196                $post_type = rand_str( 20 );
     197                $post_type_object = register_post_type( $post_type );
     198
     199                $subtype = rand_str();
     200                $feature = 'custom_feature_name';
     201
     202                add_post_type_support( $post_type . ':' . $subtype, $feature );
     203                $post_type_object->remove_support( $feature, $subtype );
     204                $this->assertFalse( post_type_supports( $post_type . ':' . $subtype, $feature ) );
     205        }
     206
     207        /**
     208         * @ticket 37667
     209         */
     210        public function test_get_all_supports_basic() {
     211                $post_type = rand_str( 20 );
     212                $post_type_object = register_post_type( $post_type );
     213
     214                $feature = 'custom_feature_name';
     215
     216                add_post_type_support( $post_type, $feature );
     217                $this->assertEqualSets( array(
     218                        'title' => true,
     219                        'editor' => true,
     220                        $feature => true,
     221                ), $post_type_object->get_all_supports() );
     222        }
     223
     224        /**
     225         * @ticket 37667
     226         */
     227        public function test_get_all_supports_with_subtype() {
     228                $post_type = rand_str( 20 );
     229                $post_type_object = register_post_type( $post_type );
     230
     231                $subtype = rand_str();
     232                $features = array( 'custom_feature_name', 'another_feature_name' );
     233
     234                add_post_type_support( $post_type . ':' . $subtype, $features );
     235                $this->assertEqualSets( array_fill_keys( $features, true ), $post_type_object->get_all_supports( $subtype ) );
     236        }
     237
     238        /**
     239         * @ticket 37667
     240         */
     241        public function test_supports_basic() {
     242                $post_type = rand_str( 20 );
     243                $post_type_object = register_post_type( $post_type );
     244
     245                $feature = 'custom_feature_name';
     246
     247                add_post_type_support( $post_type, $feature );
     248                $this->assertTrue( $post_type_object->supports( $feature ) );
     249        }
     250
     251        /**
     252         * @ticket 37667
     253         */
     254        public function test_supports_with_subtype() {
     255                $post_type = rand_str( 20 );
     256                $post_type_object = register_post_type( $post_type );
     257
     258                $subtype = rand_str();
     259                $feature = 'custom_feature_name';
     260
     261                add_post_type_support( $post_type . ':' . $subtype, $feature );
     262                $this->assertTrue( $post_type_object->supports( $feature, $subtype ) );
     263        }
     264
     265        /**
     266         * @ticket 37667
     267         */
     268        public function test_get_by_support() {
     269                $post_type = rand_str( 20 );
     270                $post_type_object = register_post_type( $post_type );
     271
     272                $subtype1 = rand_str();
     273                $subtype2 = rand_str();
     274                $feature = 'custom_feature_name';
     275
     276                add_post_type_support( $post_type, $feature );
     277                add_post_type_support( $post_type . ':' . $subtype2, $feature );
     278                $this->assertEqualSets( array( $post_type, $post_type . ':' . $subtype2 ), $post_type_object->get_by_support( $feature ) );
     279        }
     280
     281        /**
     282         * @ticket 37667
     283         */
     284        public function test_migrate_support_from_global() {
     285                $post_type = rand_str();
     286
     287                $feature = 'custom_feature_name';
     288
     289                $GLOBALS['_wp_post_type_features'][ $post_type ][''][ $feature ] = true;
     290                $post_type_object = new WP_Post_Type( $post_type );
     291                $this->assertTrue( $post_type_object->supports( $feature ) );
     292        }
    151293}