| 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 | */ |
| 15 | final class WP_Post_Status { |
| 16 | /** |
| 17 | * Name of the post status. |
| 18 | * |
| 19 | * @since 4.6.0 |
| 20 | * @access public |
| 21 | * @var string |
| 22 | */ |
| 23 | public $name; |
| 24 | |
| 25 | /** |
| 26 | * A descriptive name for the post status marked for translation. |
| 27 | * |
| 28 | * Defaults to value of $name. |
| 29 | * |
| 30 | * @since 4.6.0 |
| 31 | * @access public |
| 32 | * @var string |
| 33 | */ |
| 34 | public $label; |
| 35 | |
| 36 | /** |
| 37 | * Descriptive text to use for nooped plurals. |
| 38 | * |
| 39 | * Default array of $label, twice |
| 40 | * |
| 41 | * @since 4.6.0 |
| 42 | * @access public |
| 43 | * @var array |
| 44 | */ |
| 45 | public $label_count; |
| 46 | |
| 47 | /** |
| 48 | * Whether to exclude posts with this post status from front end search |
| 49 | * results. |
| 50 | * |
| 51 | * Default is value of $internal. |
| 52 | * |
| 53 | * @since 4.6.0 |
| 54 | * @access public |
| 55 | * @var bool |
| 56 | */ |
| 57 | public $exclude_from_search = false; |
| 58 | |
| 59 | /** |
| 60 | * Whether the status is built-in. Core-use only. |
| 61 | * |
| 62 | * Default false. |
| 63 | * |
| 64 | * @since 4.6.0 |
| 65 | * @access public |
| 66 | * @var bool |
| 67 | */ |
| 68 | public $_builtin = false; |
| 69 | |
| 70 | /** |
| 71 | * Whether posts of this status should be shown in the front end of the site. |
| 72 | * |
| 73 | * Default false. |
| 74 | * |
| 75 | * @since 4.6.0 |
| 76 | * @access public |
| 77 | * @var bool |
| 78 | */ |
| 79 | public $public = false; |
| 80 | |
| 81 | /** |
| 82 | * Whether the status is for internal use only. |
| 83 | * |
| 84 | * Default false. |
| 85 | * |
| 86 | * @since 4.6.0 |
| 87 | * @access public |
| 88 | * @var bool |
| 89 | */ |
| 90 | public $internal = false; |
| 91 | |
| 92 | /** |
| 93 | * Whether posts with this status should be protected. |
| 94 | * |
| 95 | * Default false. |
| 96 | * |
| 97 | * @since 4.6.0 |
| 98 | * @access public |
| 99 | * @var bool |
| 100 | */ |
| 101 | public $protected = false; |
| 102 | |
| 103 | /** |
| 104 | * Whether posts with this status should be private. |
| 105 | * |
| 106 | * Default false. |
| 107 | * |
| 108 | * @since 4.6.0 |
| 109 | * @access public |
| 110 | * @var bool |
| 111 | */ |
| 112 | public $private = false; |
| 113 | |
| 114 | /** |
| 115 | * Whether posts with this status should be publicly-queryable. |
| 116 | * |
| 117 | * Default is value of $public. |
| 118 | * |
| 119 | * @since 4.6.0 |
| 120 | * @access public |
| 121 | * @var bool |
| 122 | */ |
| 123 | public $publicly_queryable = false; |
| 124 | |
| 125 | /** |
| 126 | * Whether to include posts in the edit listing for their post type. |
| 127 | * |
| 128 | * Default is value of $internal. |
| 129 | * |
| 130 | * @since 4.6.0 |
| 131 | * @access public |
| 132 | * @var bool |
| 133 | */ |
| 134 | public $show_in_admin_status_list = false; |
| 135 | |
| 136 | /** |
| 137 | * Whether to display in the list of statuses with post counts at the top of the edit listings. |
| 138 | * |
| 139 | * e.g. All (12) | Published (9) | My Custom Status (2) |
| 140 | * |
| 141 | * Default is value of `$internal`. |
| 142 | * |
| 143 | * @since 4.6.0 |
| 144 | * @access public |
| 145 | * @var bool |
| 146 | */ |
| 147 | public $show_in_admin_all_list = false; |
| 148 | |
| 149 | /** |
| 150 | * Creates a new WP_Post_Status object with the name of $post_status. |
| 151 | * |
| 152 | * Other object properties will be populated from the provided arguments. |
| 153 | * |
| 154 | * @since 4.6.0 |
| 155 | * @access public |
| 156 | * |
| 157 | * @see register_post_status() |
| 158 | * |
| 159 | * @param string $post_status Name of the post status. |
| 160 | * @param array|string $args Array or string of status arguments. See `WP_Post_Status::set_props()` |
| 161 | * for a list of arguments. |
| 162 | */ |
| 163 | public function __construct( $post_status, $args = array() ) { |
| 164 | $this->set( 'name', sanitize_key( $post_status ) ); |
| 165 | |
| 166 | if ( $args ) { |
| 167 | $this->set_props( $args ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Sets a object property. |
| 173 | * |
| 174 | * @since 4.6.0 |
| 175 | * @access public |
| 176 | * |
| 177 | * @param string $prop Name of the object property. |
| 178 | * @param mixed $value The value to set the property to. |
| 179 | */ |
| 180 | public function set( $prop, $value ) { |
| 181 | $this->$prop = $value; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Sets multiple object properties. |
| 186 | * |
| 187 | * The `$args` array passed will be filled with defaults as needed. |
| 188 | * |
| 189 | * @since 4.6.0 |
| 190 | * @access public |
| 191 | * |
| 192 | * @param array|string $args { |
| 193 | * Array or string of post status arguments. |
| 194 | * |
| 195 | * @type bool|string $label A descriptive name for the post status marked |
| 196 | * for translation. Defaults to value of $post_status. |
| 197 | * @type bool|array $label_count Descriptive text to use for nooped plurals. |
| 198 | * Default array of $label, twice |
| 199 | * @type bool $exclude_from_search Whether to exclude posts with this post status |
| 200 | * from search results. Default is value of $internal. |
| 201 | * @type bool $_builtin Whether the status is built-in. Core-use only. |
| 202 | * Default false. |
| 203 | * @type bool $public Whether posts of this status should be shown |
| 204 | * in the front end of the site. Default false. |
| 205 | * @type bool $internal Whether the status is for internal use only. |
| 206 | * Default false. |
| 207 | * @type bool $protected Whether posts with this status should be protected. |
| 208 | * Default false. |
| 209 | * @type bool $private Whether posts with this status should be private. |
| 210 | * Default false. |
| 211 | * @type bool $publicly_queryable Whether posts with this status should be publicly- |
| 212 | * queryable. Default is value of $public. |
| 213 | * @type bool $show_in_admin_all_list Whether to include posts in the edit listing for |
| 214 | * their post type. Default is value of $internal. |
| 215 | * @type bool $show_in_admin_status_list Show in the list of statuses with post counts at |
| 216 | * the top of the edit listings, |
| 217 | * e.g. All (12) | Published (9) | My Custom Status (2) |
| 218 | * Default is value of $internal. |
| 219 | * } |
| 220 | */ |
| 221 | public function set_props( $args ) { |
| 222 | $args = $this->parse_defaults( $args ); |
| 223 | |
| 224 | foreach ( $args as $key => $value ) { |
| 225 | $this->set( $key, $value ); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Parses an array of object properties and fills it with default values. |
| 231 | * |
| 232 | * @since 4.6.0 |
| 233 | * @access private |
| 234 | * |
| 235 | * @param array|string $args Array or string of status arguments. |
| 236 | * |
| 237 | * @return array The parsed array of status arguments. |
| 238 | */ |
| 239 | private function parse_defaults( $args ) { |
| 240 | $defaults = $this->get_defaults(); |
| 241 | $args = wp_parse_args( $args, $defaults ); |
| 242 | |
| 243 | // Set various defaults. |
| 244 | if ( null === $args['public'] && null === $args['internal'] |
| 245 | && null === $args['protected'] && null === $args['private'] |
| 246 | ) { |
| 247 | $args['internal'] = true; |
| 248 | } |
| 249 | |
| 250 | if ( null === $args['public'] ) { |
| 251 | $args['public'] = false; |
| 252 | } |
| 253 | |
| 254 | if ( null === $args['private'] ) { |
| 255 | $args['private'] = false; |
| 256 | } |
| 257 | |
| 258 | if ( null === $args['protected'] ) { |
| 259 | $args['protected'] = false; |
| 260 | } |
| 261 | |
| 262 | if ( null === $args['internal'] ) { |
| 263 | $args['internal'] = false; |
| 264 | } |
| 265 | |
| 266 | if ( null === $args['publicly_queryable'] ) { |
| 267 | $args['publicly_queryable'] = $args['public']; |
| 268 | } |
| 269 | |
| 270 | if ( null === $args['exclude_from_search'] ) { |
| 271 | $args['exclude_from_search'] = $args['internal']; |
| 272 | } |
| 273 | |
| 274 | if ( null === $args['show_in_admin_all_list'] ) { |
| 275 | $args['show_in_admin_all_list'] = !$args['internal']; |
| 276 | } |
| 277 | |
| 278 | if ( null === $args['show_in_admin_status_list'] ) { |
| 279 | $args['show_in_admin_status_list'] = !$args['internal']; |
| 280 | } |
| 281 | |
| 282 | if ( false === $args['label'] ) { |
| 283 | $args['label'] = $this->name; |
| 284 | } |
| 285 | |
| 286 | if ( false === $args['label_count'] ) { |
| 287 | $args['label_count'] = array( $args['label'], $args['label'] ); |
| 288 | } |
| 289 | |
| 290 | return $args; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Returns defaults for the object properties. |
| 295 | * |
| 296 | * @since 4.6.0 |
| 297 | * @access private |
| 298 | * |
| 299 | * @return array An array of defaults. |
| 300 | */ |
| 301 | private function get_defaults() { |
| 302 | // Args prefixed with an underscore are reserved for internal use. |
| 303 | return array( |
| 304 | 'label' => false, |
| 305 | 'label_count' => false, |
| 306 | 'exclude_from_search' => null, |
| 307 | '_builtin' => false, |
| 308 | 'public' => null, |
| 309 | 'internal' => null, |
| 310 | 'protected' => null, |
| 311 | 'private' => null, |
| 312 | 'publicly_queryable' => null, |
| 313 | 'show_in_admin_status_list' => null, |
| 314 | 'show_in_admin_all_list' => null, |
| 315 | ); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Retrieves a post status object by name. |
| 320 | * |
| 321 | * @since 4.6.0 |
| 322 | * @access public |
| 323 | * @static |
| 324 | * |
| 325 | * @global array $wp_post_statuses List of post statuses. |
| 326 | * |
| 327 | * @param string $post_status The name of the registered post status. |
| 328 | * @return WP_Post_Status|null WP_Post_Status object if it exists, null otherwise. |
| 329 | */ |
| 330 | public static function get_instance( $post_status ) { |
| 331 | global $wp_post_statuses; |
| 332 | |
| 333 | if ( ! is_scalar( $post_status ) || empty( $wp_post_statuses[ $post_status ] ) ) { |
| 334 | return null; |
| 335 | } |
| 336 | |
| 337 | return $wp_post_statuses[ $post_status ]; |
| 338 | } |
| 339 | } |