| | 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 | * Show in the list of statuses with post counts at the top of the edit listings, |
| | 138 | * e.g. All (12) | Published (9) | My Custom Status (2) |
| | 139 | * |
| | 140 | * Default is value of $internal. |
| | 141 | * |
| | 142 | * @since 4.6.0 |
| | 143 | * @access public |
| | 144 | * @var bool |
| | 145 | */ |
| | 146 | public $show_in_admin_all_list = false; |
| | 147 | |
| | 148 | /** |
| | 149 | * Creates a new WP_Post_Status object with the name of $post_status. |
| | 150 | * |
| | 151 | * Other object properties will be populated from the provided arguments. |
| | 152 | * |
| | 153 | * @since 4.6.0 |
| | 154 | * @access public |
| | 155 | * |
| | 156 | * @see register_post_status() |
| | 157 | * |
| | 158 | * @param string $post_status Name of the post status. |
| | 159 | * @param array|string $args Array or string of post status arguments. |
| | 160 | */ |
| | 161 | public function __construct( $post_status, $args = array() ) { |
| | 162 | // Args prefixed with an underscore are reserved for internal use. |
| | 163 | $defaults = array( |
| | 164 | 'label' => false, |
| | 165 | 'label_count' => false, |
| | 166 | 'exclude_from_search' => null, |
| | 167 | '_builtin' => false, |
| | 168 | 'public' => null, |
| | 169 | 'internal' => null, |
| | 170 | 'protected' => null, |
| | 171 | 'private' => null, |
| | 172 | 'publicly_queryable' => null, |
| | 173 | 'show_in_admin_status_list' => null, |
| | 174 | 'show_in_admin_all_list' => null, |
| | 175 | ); |
| | 176 | $args = wp_parse_args($args, $defaults); |
| | 177 | |
| | 178 | $post_status = sanitize_key($post_status); |
| | 179 | |
| | 180 | $args['name'] = $post_status; |
| | 181 | |
| | 182 | // Set various defaults. |
| | 183 | if ( null === $args['public'] && null === $args['internal'] && null === $args['protected'] && null === $args['private'] ) |
| | 184 | $args['internal'] = true; |
| | 185 | |
| | 186 | if ( null === $args['public'] ) |
| | 187 | $args['public'] = false; |
| | 188 | |
| | 189 | if ( null === $args['private'] ) |
| | 190 | $args['private'] = false; |
| | 191 | |
| | 192 | if ( null === $args['protected'] ) |
| | 193 | $args['protected'] = false; |
| | 194 | |
| | 195 | if ( null === $args['internal'] ) |
| | 196 | $args['internal'] = false; |
| | 197 | |
| | 198 | if ( null === $args['publicly_queryable'] ) |
| | 199 | $args['publicly_queryable'] = $args['public']; |
| | 200 | |
| | 201 | if ( null === $args['exclude_from_search'] ) |
| | 202 | $args['exclude_from_search'] = $args['internal']; |
| | 203 | |
| | 204 | if ( null === $args['show_in_admin_all_list'] ) |
| | 205 | $args['show_in_admin_all_list'] = !$args['internal']; |
| | 206 | |
| | 207 | if ( null === $args['show_in_admin_status_list'] ) |
| | 208 | $args['show_in_admin_status_list'] = !$args['internal']; |
| | 209 | |
| | 210 | if ( false === $args['label'] ) |
| | 211 | $args['label'] = $post_status; |
| | 212 | |
| | 213 | if ( false === $args['label_count'] ) |
| | 214 | $args['label_count'] = array( $args['label'], $args['label'] ); |
| | 215 | |
| | 216 | foreach ( $args as $key => $value ) { |
| | 217 | $this->$key = $value; |
| | 218 | } |
| | 219 | } |
| | 220 | |
| | 221 | /** |
| | 222 | * Retrieves a post status object by name. |
| | 223 | * |
| | 224 | * @since 4.6.0 |
| | 225 | * @access public |
| | 226 | * @static |
| | 227 | * |
| | 228 | * @global array $wp_post_statuses List of post statuses. |
| | 229 | * |
| | 230 | * @param string $post_status The name of the registered post status. |
| | 231 | * @return WP_Post_Status|null WP_Post_Status object if it exists, null otherwise. |
| | 232 | */ |
| | 233 | public static function get_instance( $post_status ) { |
| | 234 | global $wp_post_statuses; |
| | 235 | |
| | 236 | if ( ! is_scalar( $post_status ) || empty( $wp_post_statuses[ $post_status ] ) ) { |
| | 237 | return null; |
| | 238 | } |
| | 239 | |
| | 240 | return $wp_post_statuses[ $post_status ]; |
| | 241 | } |
| | 242 | } |