Make WordPress Core

Ticket #49644: 49644-1.patch

File 49644-1.patch, 8.5 KB (added by MikeSchinkel, 5 years ago)

Updates the patch to include a last_error. Also adds another $this as a 2nd parameter to unregistered_post_type hook

  • www/wp-includes/class-wp-post-type.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    345345         */
    346346        public $rest_controller;
    347347
     348        /**
     349         * Set to a WP_Error when an inspected error occurs within the class.
     350         *
     351         * Currently this is only used by __construct() and unregister().
     352         *
     353         * @since 5.4.0
     354         * @var WP_Error|null $last_error
     355         */
     356        public $last_error = null;
     357
    348358        /**
    349359         * Constructor.
    350360         *
     
    360370         *                                Default empty array.
    361371         */
    362372        public function __construct( $post_type, $args = array() ) {
     373                // Sanitize post type name
     374                $post_type = sanitize_key( $post_type );
     375
     376                if ( empty( $post_type ) || strlen( $post_type ) > 20 ) {
     377                        _doing_it_wrong( __METHOD__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2.0' );
     378                        $this->last_error = new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) );
     379                }
     380
    363381                $this->name = $post_type;
    364382
    365383                $this->set_props( $args );
     
    375393        public function set_props( $args ) {
    376394                $args = wp_parse_args( $args );
    377395
     396                $this->last_error = null;
     397
    378398                /**
    379399                 * Filters the arguments for registering a post type.
    380400                 *
     
    514534
    515535                $this->labels = get_post_type_labels( $this );
    516536                $this->label  = $this->labels->name;
     537
    517538        }
    518539
    519540        /**
     
    522543         * @since 4.6.0
    523544         */
    524545        public function add_supports() {
     546                $this->last_error = null;
    525547                if ( ! empty( $this->supports ) ) {
    526548                        foreach ( $this->supports as $feature => $args ) {
    527549                                if ( is_array( $args ) ) {
     
    548570        public function add_rewrite_rules() {
    549571                global $wp_rewrite, $wp;
    550572
     573                $this->last_error = null;
     574
    551575                if ( false !== $this->query_var && $wp && is_post_type_viewable( $this ) ) {
    552576                        $wp->add_query_var( $this->query_var );
    553577                }
     
    593617                if ( $this->register_meta_box_cb ) {
    594618                        add_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10, 1 );
    595619                }
     620                $this->last_error = null;
    596621        }
    597622
    598623        /**
     
    602627         */
    603628        public function add_hooks() {
    604629                add_action( 'future_' . $this->name, '_future_post_hook', 5, 2 );
     630                $this->last_error = null;
    605631        }
    606632
    607633        /**
     
    613639                foreach ( $this->taxonomies as $taxonomy ) {
    614640                        register_taxonomy_for_object_type( $taxonomy, $this->name );
    615641                }
     642                $this->last_error = null;
    616643        }
    617644
    618645        /**
     
    626653                global $_wp_post_type_features;
    627654
    628655                unset( $_wp_post_type_features[ $this->name ] );
     656
     657                $this->last_error = null;
    629658        }
    630659
    631660        /**
     
    640669        public function remove_rewrite_rules() {
    641670                global $wp, $wp_rewrite, $post_type_meta_caps;
    642671
     672                $this->last_error = null;
     673
    643674                // Remove query var.
    644675                if ( false !== $this->query_var ) {
    645676                        $wp->remove_query_var( $this->query_var );
     
    668699         * @since 4.6.0
    669700         */
    670701        public function unregister_meta_boxes() {
     702                $this->last_error = null;
    671703                if ( $this->register_meta_box_cb ) {
    672704                        remove_action( 'add_meta_boxes_' . $this->name, $this->register_meta_box_cb, 10 );
    673705                }
     
    679711         * @since 4.6.0
    680712         */
    681713        public function unregister_taxonomies() {
     714                $this->last_error = null;
    682715                foreach ( get_object_taxonomies( $this->name ) as $taxonomy ) {
    683716                        unregister_taxonomy_for_object_type( $taxonomy, $this->name );
    684717                }
     
    690723         * @since 4.6.0
    691724         */
    692725        public function remove_hooks() {
     726                $this->last_error = null;
    693727                remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
    694728        }
    695729
     
    704738         *                                 is set not to show in rest.
    705739         */
    706740        public function get_rest_controller() {
     741
     742                $this->last_error = null;
     743
    707744                if ( ! $this->show_in_rest ) {
    708745                        return null;
    709746                }
     
    727764                }
    728765
    729766                return $this->rest_controller;
     767
    730768        }
     769
     770        /**
     771         * Registers this post type.
     772         *
     773         * @since 5.4.0
     774         */
     775        public function register() {
     776                global $wp_post_types;
     777
     778                $this->last_error = null;
     779
     780                if ( ! is_array( $wp_post_types ) ) {
     781                        $wp_post_types = array();
     782                }
     783
     784                $this->add_supports();
     785                $this->add_rewrite_rules();
     786                $this->register_meta_boxes();
     787
     788                $wp_post_types[ $this->name ] = $this;
     789
     790                $this->add_hooks();
     791                $this->register_taxonomies();
     792
     793                /**
     794                 * Fires after a post type is registered.
     795                 *
     796                 * @since 3.3.0
     797                 * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
     798                 *
     799                 * @param string       $post_type        Post type.
     800                 * @param WP_Post_Type $post_type_object Arguments used to register the post type.
     801                 */
     802                do_action( 'registered_post_type', $this->name, $this );
     803        }
     804
     805        /**
     806         * Unregisters this post type.
     807         *
     808         * @since 5.4.0
     809         *
     810         * @return bool Returns true if unregistered, false if a _builtin post type.
     811         */
     812        public function unregister() {
     813                global $wp_post_types;
     814
     815                // Do not allow unregistering internal post types.
     816                if ( $this->_builtin ) {
     817                        $this->last_error = new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) );
     818                        return false;
     819                }
     820
     821                $this->last_error = null;
     822
     823                if ( $removed = post_type_exists( $this->name ) ) {
     824                        $this->remove_supports();
     825                        $this->remove_rewrite_rules();
     826                        $this->unregister_meta_boxes();
     827                        $this->remove_hooks();
     828                        $this->unregister_taxonomies();
     829                        unset( $wp_post_types[ $this->name ] );
     830                }
     831
     832                /**
     833                 * Fires after a post type was unregistered.
     834                 *
     835                 * @since 4.5.0
     836                 *
     837                 * @param string $post_type Post type key.
     838                 * @param WP_Post_Type $post_type_object Arguments used to register the post type.
     839                 * @param bool $removed True if the post type 'existed' and was removed, false otherwise
     840                 */
     841                do_action( 'unregistered_post_type', $this->name, $this, $removed );
     842
     843                return true;
     844
     845        }
     846
    731847}
  • www/wp-includes/post.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    14031403 * @return WP_Post_Type|WP_Error The registered post type object, or an error object.
    14041404 */
    14051405function register_post_type( $post_type, $args = array() ) {
    1406         global $wp_post_types;
    1407 
    1408         if ( ! is_array( $wp_post_types ) ) {
    1409                 $wp_post_types = array();
    1410         }
    1411 
    1412         // Sanitize post type name
    1413         $post_type = sanitize_key( $post_type );
    1414 
    1415         if ( empty( $post_type ) || strlen( $post_type ) > 20 ) {
    1416                 _doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2.0' );
    1417                 return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) );
    1418         }
    1419 
    14201406        $post_type_object = new WP_Post_Type( $post_type, $args );
    1421         $post_type_object->add_supports();
    1422         $post_type_object->add_rewrite_rules();
    1423         $post_type_object->register_meta_boxes();
    1424 
    1425         $wp_post_types[ $post_type ] = $post_type_object;
    1426 
    1427         $post_type_object->add_hooks();
    1428         $post_type_object->register_taxonomies();
    1429 
    1430         /**
    1431          * Fires after a post type is registered.
    1432          *
    1433          * @since 3.3.0
    1434          * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
    1435          *
    1436          * @param string       $post_type        Post type.
    1437          * @param WP_Post_Type $post_type_object Arguments used to register the post type.
    1438          */
    1439         do_action( 'registered_post_type', $post_type, $post_type_object );
    1440 
    1441         return $post_type_object;
     1407        return $post_type_object->register();
    14421408}
    14431409
    14441410/**
     
    14601426                return new WP_Error( 'invalid_post_type', __( 'Invalid post type.' ) );
    14611427        }
    14621428
    1463         $post_type_object = get_post_type_object( $post_type );
    1464 
    1465         // Do not allow unregistering internal post types.
    1466         if ( $post_type_object->_builtin ) {
    1467                 return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) );
    1468         }
    1469 
    1470         $post_type_object->remove_supports();
    1471         $post_type_object->remove_rewrite_rules();
    1472         $post_type_object->unregister_meta_boxes();
    1473         $post_type_object->remove_hooks();
    1474         $post_type_object->unregister_taxonomies();
    1475 
    1476         unset( $wp_post_types[ $post_type ] );
    1477 
    1478         /**
    1479          * Fires after a post type was unregistered.
    1480          *
    1481          * @since 4.5.0
    1482          *
    1483          * @param string $post_type Post type key.
    1484          */
    1485         do_action( 'unregistered_post_type', $post_type );
    1486 
    1487         return true;
     1429        return get_post_type_object( $post_type )->unregister();
    14881430}
    14891431
    14901432/**