Make WordPress Core

Ticket #36492: 36492.4.diff

File 36492.4.diff, 14.7 KB (added by flixos90, 7 years ago)

WP_Status as base class for WP_Post_Status

  • src/wp-includes/class-wp-post-status.php

     
     1<?php
     2/**
     3 * Post API: WP_Post_Status class
     4 *
     5 * @package WordPress
     6 * @subpackage Post
     7 * @since 4.6.0
     8 */
     9
     10/**
     11 * Core class used to implement a WP_Post_Status object.
     12 *
     13 * @since 4.6.0
     14 */
     15final class WP_Post_Status extends WP_Status {
     16        /**
     17         * Whether to exclude posts with this post status from front end search
     18         * results.
     19         *
     20         * Default is value of $internal.
     21         *
     22         * @since 4.6.0
     23         * @access public
     24         * @var bool
     25         */
     26        public $exclude_from_search = false;
     27
     28        /**
     29         * Whether posts with this status should be publicly-queryable.
     30         *
     31         * Default is value of $public.
     32         *
     33         * @since 4.6.0
     34         * @access public
     35         * @var bool
     36         */
     37        public $publicly_queryable = false;
     38
     39        /**
     40         * Parses an array of class properties and fills it with default values.
     41         *
     42         * @since 4.6.0
     43         * @access protected
     44         *
     45         * @see WP_Status::parse_defaults()
     46         *
     47         * @param array|string $args Array or string of status arguments.
     48         *
     49         * @return array The parsed array of status arguments.
     50         */
     51        protected function parse_defaults( $args ) {
     52                $args = parent::parse_defaults( $args );
     53
     54                if ( null === $args['exclude_from_search'] ) {
     55                        $args['exclude_from_search'] = $args['internal'];
     56                }
     57
     58                if ( null === $args['publicly_queryable'] ) {
     59                        $args['publicly_queryable'] = $args['public'];
     60                }
     61
     62                return $args;
     63        }
     64
     65        /**
     66         * Returns defaults for the class properties.
     67         *
     68         * @since 4.6.0
     69         * @access protected
     70         *
     71         * @see WP_Status::get_defaults()
     72         *
     73         * @return array An array of defaults.
     74         */
     75        protected function get_defaults() {
     76                return array_merge( parent::get_defaults(), array(
     77                        'exclude_from_search' => null,
     78                        'publicly_queryable'  => null,
     79                ) );
     80        }
     81
     82        /**
     83         * Retrieves a post status object by name.
     84         *
     85         * @since 4.6.0
     86         * @access public
     87         * @static
     88         *
     89         * @global array $wp_post_statuses List of post statuses.
     90         *
     91         * @param string $post_status The name of the registered post status.
     92         * @return WP_Post_Status|null WP_Post_Status object if it exists, null otherwise.
     93         */
     94        public static function get_instance( $post_status ) {
     95                global $wp_post_statuses;
     96
     97                if ( ! is_scalar( $post_status ) || empty( $wp_post_statuses[ $post_status ] ) ) {
     98                        return null;
     99                }
     100
     101                return $wp_post_statuses[ $post_status ];
     102        }
     103}
  • src/wp-includes/class-wp-status.php

    Property changes on: src/wp-includes/class-wp-post-status.php
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
     
     1<?php
     2/**
     3 * WP_Status class
     4 *
     5 * @package WordPress
     6 * @since 4.6.0
     7 */
     8
     9/**
     10 * Abstract class used to implement a WP_Status object.
     11 *
     12 * @since 4.6.0
     13 */
     14abstract class WP_Status {
     15        /**
     16         * Name of the status.
     17         *
     18         * @since 4.6.0
     19         * @access public
     20         * @var string
     21         */
     22        public $name;
     23
     24        /**
     25         * A descriptive name for the status marked for translation.
     26         *
     27         * Defaults to value of $name.
     28         *
     29         * @since 4.6.0
     30         * @access public
     31         * @var string
     32         */
     33        public $label;
     34
     35        /**
     36         * Descriptive text to use for nooped plurals.
     37         *
     38         * Default array of $label, twice
     39         *
     40         * @since 4.6.0
     41         * @access public
     42         * @var array
     43         */
     44        public $label_count;
     45
     46        /**
     47         * Whether the status is built-in. Core-use only.
     48         *
     49         * Default false.
     50         *
     51         * @since 4.6.0
     52         * @access public
     53         * @var bool
     54         */
     55        public $_builtin = false;
     56
     57        /**
     58         * Whether content of this status should be shown in the front end of the site.
     59         *
     60         * Default false.
     61         *
     62         * @since 4.6.0
     63         * @access public
     64         * @var bool
     65         */
     66        public $public = false;
     67
     68        /**
     69         * Whether the status is for internal use only.
     70         *
     71         * Default false.
     72         *
     73         * @since 4.6.0
     74         * @access public
     75         * @var bool
     76         */
     77        public $internal = false;
     78
     79        /**
     80         * Whether content with this status should be protected.
     81         *
     82         * Default false.
     83         *
     84         * @since 4.6.0
     85         * @access public
     86         * @var bool
     87         */
     88        public $protected = false;
     89
     90        /**
     91         * Whether content with this status should be private.
     92         *
     93         * Default false.
     94         *
     95         * @since 4.6.0
     96         * @access public
     97         * @var bool
     98         */
     99        public $private = false;
     100
     101        /**
     102         * Whether to include content with this status in their edit listing.
     103         *
     104         * Default is value of $internal.
     105         *
     106         * @since 4.6.0
     107         * @access public
     108         * @var bool
     109         */
     110        public $show_in_admin_status_list = false;
     111
     112        /**
     113         * Whether to display in the list of statuses with content counts at the top of the edit listings.
     114         *
     115         * Default is value of `$internal`.
     116         *
     117         * @since 4.6.0
     118         * @access public
     119         * @var bool
     120         */
     121        public $show_in_admin_all_list = false;
     122
     123        /**
     124         * Creates a new WP_Status object with the name of $name.
     125         *
     126         * Other object properties will be populated from the provided arguments.
     127         *
     128         * @since 4.6.0
     129         * @access public
     130         *
     131         * @param string       $name Name of the status.
     132         * @param array|string $args Array or string of status arguments. See `WP_Status::set_props()`
     133         *                           for a list of arguments.
     134         */
     135        public function __construct( $name, $args = array() ) {
     136                $this->set( 'name', sanitize_key( $name ) );
     137
     138                if ( $args ) {
     139                        $this->set_props( $args );
     140                }
     141        }
     142
     143        /**
     144         * Sets a class property.
     145         *
     146         * @since 4.6.0
     147         * @access public
     148         *
     149         * @param string $prop  Name of the class property.
     150         * @param mixed  $value The value to set the property to.
     151         */
     152        public function set( $prop, $value ) {
     153                $this->$prop = $value;
     154        }
     155
     156        /**
     157         * Sets multiple class properties.
     158         *
     159         * The `$args` array passed will be filled with defaults as needed.
     160         *
     161         * @since 4.6.0
     162         * @access public
     163         *
     164         * @param array|string $args {
     165         *     Array or string of status arguments.
     166         *
     167         *     @type bool|string $label                     A descriptive name for the status marked
     168         *                                                  for translation. Defaults to value of $name.
     169         *     @type bool|array  $label_count               Descriptive text to use for nooped plurals.
     170         *                                                  Default array of $label, twice
     171         *     @type bool        $_builtin                  Whether the status is built-in. Core-use only.
     172         *                                                  Default false.
     173         *     @type bool        $public                    Whether content of this status should be shown
     174         *                                                  in the front end of the site. Default false.
     175         *     @type bool        $internal                  Whether the status is for internal use only.
     176         *                                                  Default false.
     177         *     @type bool        $protected                 Whether content with this status should be protected.
     178         *                                                  Default false.
     179         *     @type bool        $private                   Whether content with this status should be private.
     180         *                                                  Default false.
     181         *     @type bool        $show_in_admin_all_list    Whether to include content with this status in their
     182         *                                                  edit listing. Default is value of $internal.
     183         *     @type bool        $show_in_admin_status_list Show in the list of statuses with content counts at
     184         *                                                  the top of the edit listings. Default is value of
     185         *                                                  $internal.
     186         * }
     187         */
     188        public function set_props( $args ) {
     189                $args = $this->parse_defaults( $args );
     190
     191                foreach ( $args as $key => $value ) {
     192                        $this->set( $key, $value );
     193                }
     194        }
     195
     196        /**
     197         * Parses an array of class properties and fills it with default values.
     198         *
     199         * @since 4.6.0
     200         * @access protected
     201         *
     202         * @param array|string $args Array or string of status arguments.
     203         *
     204         * @return array The parsed array of status arguments.
     205         */
     206        protected function parse_defaults( $args ) {
     207                // Args prefixed with an underscore are reserved for internal use.
     208                $defaults = $this->get_defaults();
     209                $args = wp_parse_args( $args, $defaults );
     210
     211                // Set various defaults.
     212                if ( null === $args['public'] && null === $args['internal']
     213                        && null === $args['protected'] && null === $args['private']
     214                ) {
     215                        $args['internal'] = true;
     216                }
     217
     218                if ( null === $args['public'] ) {
     219                        $args['public'] = false;
     220                }
     221
     222                if ( null === $args['private'] ) {
     223                        $args['private'] = false;
     224                }
     225
     226                if ( null === $args['protected'] ) {
     227                        $args['protected'] = false;
     228                }
     229
     230                if ( null === $args['internal'] ) {
     231                        $args['internal'] = false;
     232                }
     233
     234                if ( null === $args['show_in_admin_all_list'] ) {
     235                        $args['show_in_admin_all_list'] = !$args['internal'];
     236                }
     237
     238                if ( null === $args['show_in_admin_status_list'] ) {
     239                        $args['show_in_admin_status_list'] = !$args['internal'];
     240                }
     241
     242                if ( false === $args['label'] ) {
     243                        $args['label'] = $this->name;
     244                }
     245
     246                if ( false === $args['label_count'] ) {
     247                        $args['label_count'] = array( $args['label'], $args['label'] );
     248                }
     249
     250                return $args;
     251        }
     252
     253        /**
     254         * Returns defaults for the class properties.
     255         *
     256         * @since 4.6.0
     257         * @access protected
     258         *
     259         * @return array An array of defaults.
     260         */
     261        protected function get_defaults() {
     262                return array(
     263                        'label'                     => false,
     264                        'label_count'               => false,
     265                        '_builtin'                  => false,
     266                        'public'                    => null,
     267                        'internal'                  => null,
     268                        'protected'                 => null,
     269                        'private'                   => null,
     270                        'show_in_admin_status_list' => null,
     271                        'show_in_admin_all_list'    => null,
     272                );
     273        }
     274}
  • src/wp-includes/post.php

    Property changes on: src/wp-includes/class-wp-status.php
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
     
    630630}
    631631
    632632/**
    633  * Register a post status. Do not use before init.
     633 * Registers a post status.
    634634 *
     635 * Do not use before init.
     636 *
    635637 * A simple function for creating or modifying a post status based on the
    636638 * parameters given. The function will accept an array (second optional
    637639 * parameter), along with a string for the post status name.
     
    639641 * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
    640642 *
    641643 * @since 3.0.0
     644 * @since 4.6.0 Converted to use and return a WP_Post_Status instance
     645 *
    642646 * @global array $wp_post_statuses Inserts new post status object into the list
    643647 *
    644648 * @param string $post_status Name of the post status.
     
    670674 *                                                  e.g. All (12) | Published (9) | My Custom Status (2)
    671675 *                                                  Default is value of $internal.
    672676 * }
    673  * @return object
     677 * @return WP_Post_Status The new post status object.
    674678 */
    675679function register_post_status( $post_status, $args = array() ) {
    676680        global $wp_post_statuses;
     
    678682        if (!is_array($wp_post_statuses))
    679683                $wp_post_statuses = array();
    680684
    681         // Args prefixed with an underscore are reserved for internal use.
    682         $defaults = array(
    683                 'label' => false,
    684                 'label_count' => false,
    685                 'exclude_from_search' => null,
    686                 '_builtin' => false,
    687                 'public' => null,
    688                 'internal' => null,
    689                 'protected' => null,
    690                 'private' => null,
    691                 'publicly_queryable' => null,
    692                 'show_in_admin_status_list' => null,
    693                 'show_in_admin_all_list' => null,
    694         );
    695         $args = wp_parse_args($args, $defaults);
    696         $args = (object) $args;
     685        $post_status_object = new WP_Post_Status( $post_status, $args );
    697686
    698         $post_status = sanitize_key($post_status);
    699         $args->name = $post_status;
     687        $wp_post_statuses[ $post_status ] = $post_status_object;
    700688
    701         // Set various defaults.
    702         if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
    703                 $args->internal = true;
    704 
    705         if ( null === $args->public  )
    706                 $args->public = false;
    707 
    708         if ( null === $args->private  )
    709                 $args->private = false;
    710 
    711         if ( null === $args->protected  )
    712                 $args->protected = false;
    713 
    714         if ( null === $args->internal  )
    715                 $args->internal = false;
    716 
    717         if ( null === $args->publicly_queryable )
    718                 $args->publicly_queryable = $args->public;
    719 
    720         if ( null === $args->exclude_from_search )
    721                 $args->exclude_from_search = $args->internal;
    722 
    723         if ( null === $args->show_in_admin_all_list )
    724                 $args->show_in_admin_all_list = !$args->internal;
    725 
    726         if ( null === $args->show_in_admin_status_list )
    727                 $args->show_in_admin_status_list = !$args->internal;
    728 
    729         if ( false === $args->label )
    730                 $args->label = $post_status;
    731 
    732         if ( false === $args->label_count )
    733                 $args->label_count = array( $args->label, $args->label );
    734 
    735         $wp_post_statuses[$post_status] = $args;
    736 
    737         return $args;
     689        return $post_status_object;
    738690}
    739691
    740692/**
    741  * Retrieve a post status object by name.
     693 * Retrieves a post status object by name.
    742694 *
    743695 * @since 3.0.0
     696 * @since 4.6.0 Converted to use WP_Post_Status
    744697 *
    745698 * @global array $wp_post_statuses List of post statuses.
    746699 *
    747  * @see register_post_status()
     700 * @see WP_Post_Status
    748701 *
    749702 * @param string $post_status The name of a registered post status.
    750  * @return object|null A post status object.
     703 * @return WP_Post_Status|null A post status object.
    751704 */
    752705function get_post_status_object( $post_status ) {
    753         global $wp_post_statuses;
    754 
    755         if ( empty($wp_post_statuses[$post_status]) )
    756                 return null;
    757 
    758         return $wp_post_statuses[$post_status];
     706        return WP_Post_Status::get_instance( $post_status );
    759707}
    760708
    761709/**
  • src/wp-settings.php

     
    119119
    120120// Load most of WordPress.
    121121require( ABSPATH . WPINC . '/class-wp-walker.php' );
     122require( ABSPATH . WPINC . '/class-wp-status.php' );
    122123require( ABSPATH . WPINC . '/class-wp-ajax-response.php' );
    123124require( ABSPATH . WPINC . '/formatting.php' );
    124125require( ABSPATH . WPINC . '/capabilities.php' );
     
    142143require( ABSPATH . WPINC . '/post.php' );
    143144require( ABSPATH . WPINC . '/class-walker-page.php' );
    144145require( ABSPATH . WPINC . '/class-walker-page-dropdown.php' );
     146require( ABSPATH . WPINC . '/class-wp-post-status.php' );
    145147require( ABSPATH . WPINC . '/class-wp-post.php' );
    146148require( ABSPATH . WPINC . '/post-template.php' );
    147149require( ABSPATH . WPINC . '/revision.php' );