| | 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 { |
| | 161 | * Array or string of post status arguments. |
| | 162 | * |
| | 163 | * @type bool|string $label A descriptive name for the post status marked |
| | 164 | * for translation. Defaults to value of $post_status. |
| | 165 | * @type bool|array $label_count Descriptive text to use for nooped plurals. |
| | 166 | * Default array of $label, twice |
| | 167 | * @type bool $exclude_from_search Whether to exclude posts with this post status |
| | 168 | * from search results. Default is value of $internal. |
| | 169 | * @type bool $_builtin Whether the status is built-in. Core-use only. |
| | 170 | * Default false. |
| | 171 | * @type bool $public Whether posts of this status should be shown |
| | 172 | * in the front end of the site. Default false. |
| | 173 | * @type bool $internal Whether the status is for internal use only. |
| | 174 | * Default false. |
| | 175 | * @type bool $protected Whether posts with this status should be protected. |
| | 176 | * Default false. |
| | 177 | * @type bool $private Whether posts with this status should be private. |
| | 178 | * Default false. |
| | 179 | * @type bool $publicly_queryable Whether posts with this status should be publicly- |
| | 180 | * queryable. Default is value of $public. |
| | 181 | * @type bool $show_in_admin_all_list Whether to include posts in the edit listing for |
| | 182 | * their post type. Default is value of $internal. |
| | 183 | * @type bool $show_in_admin_status_list Show in the list of statuses with post counts at |
| | 184 | * the top of the edit listings, |
| | 185 | * e.g. All (12) | Published (9) | My Custom Status (2) |
| | 186 | * Default is value of $internal. |
| | 187 | * } |
| | 188 | */ |
| | 189 | public function __construct( $post_status, $args = array() ) { |
| | 190 | // Args prefixed with an underscore are reserved for internal use. |
| | 191 | $defaults = array( |
| | 192 | 'label' => false, |
| | 193 | 'label_count' => false, |
| | 194 | 'exclude_from_search' => null, |
| | 195 | '_builtin' => false, |
| | 196 | 'public' => null, |
| | 197 | 'internal' => null, |
| | 198 | 'protected' => null, |
| | 199 | 'private' => null, |
| | 200 | 'publicly_queryable' => null, |
| | 201 | 'show_in_admin_status_list' => null, |
| | 202 | 'show_in_admin_all_list' => null, |
| | 203 | ); |
| | 204 | $args = wp_parse_args( $args, $defaults ); |
| | 205 | |
| | 206 | $post_status = sanitize_key( $post_status ); |
| | 207 | |
| | 208 | $args['name'] = $post_status; |
| | 209 | |
| | 210 | // Set various defaults. |
| | 211 | if ( null === $args['public'] && null === $args['internal'] |
| | 212 | && null === $args['protected'] && null === $args['private'] |
| | 213 | ) { |
| | 214 | $args['internal'] = true; |
| | 215 | } |
| | 216 | |
| | 217 | if ( null === $args['public'] ) { |
| | 218 | $args['public'] = false; |
| | 219 | } |
| | 220 | |
| | 221 | if ( null === $args['private'] ) { |
| | 222 | $args['private'] = false; |
| | 223 | } |
| | 224 | |
| | 225 | if ( null === $args['protected'] ) { |
| | 226 | $args['protected'] = false; |
| | 227 | } |
| | 228 | |
| | 229 | if ( null === $args['internal'] ) { |
| | 230 | $args['internal'] = false; |
| | 231 | } |
| | 232 | |
| | 233 | if ( null === $args['publicly_queryable'] ) { |
| | 234 | $args['publicly_queryable'] = $args['public']; |
| | 235 | } |
| | 236 | |
| | 237 | if ( null === $args['exclude_from_search'] ) { |
| | 238 | $args['exclude_from_search'] = $args['internal']; |
| | 239 | } |
| | 240 | |
| | 241 | if ( null === $args['show_in_admin_all_list'] ) { |
| | 242 | $args['show_in_admin_all_list'] = !$args['internal']; |
| | 243 | } |
| | 244 | |
| | 245 | if ( null === $args['show_in_admin_status_list'] ) { |
| | 246 | $args['show_in_admin_status_list'] = !$args['internal']; |
| | 247 | } |
| | 248 | |
| | 249 | if ( false === $args['label'] ) { |
| | 250 | $args['label'] = $post_status; |
| | 251 | } |
| | 252 | |
| | 253 | if ( false === $args['label_count'] ) { |
| | 254 | $args['label_count'] = array( $args['label'], $args['label'] ); |
| | 255 | } |
| | 256 | |
| | 257 | foreach ( $args as $key => $value ) { |
| | 258 | $this->$key = $value; |
| | 259 | } |
| | 260 | } |
| | 261 | |
| | 262 | /** |
| | 263 | * Retrieves a post status object by name. |
| | 264 | * |
| | 265 | * @since 4.6.0 |
| | 266 | * @access public |
| | 267 | * @static |
| | 268 | * |
| | 269 | * @global array $wp_post_statuses List of post statuses. |
| | 270 | * |
| | 271 | * @param string $post_status The name of the registered post status. |
| | 272 | * @return WP_Post_Status|null WP_Post_Status object if it exists, null otherwise. |
| | 273 | */ |
| | 274 | public static function get_instance( $post_status ) { |
| | 275 | global $wp_post_statuses; |
| | 276 | |
| | 277 | if ( ! is_scalar( $post_status ) || empty( $wp_post_statuses[ $post_status ] ) ) { |
| | 278 | return null; |
| | 279 | } |
| | 280 | |
| | 281 | return $wp_post_statuses[ $post_status ]; |
| | 282 | } |
| | 283 | } |