BeforeAfter
5 years ago
Pro
5 years ago
ActionIcons.php
5 years ago
AttachmentDisplay.php
5 years ago
BeforeAfter.php
5 years ago
CharacterLimit.php
5 years ago
Comment.php
5 years ago
CommentCount.php
5 years ago
CommentLink.php
5 years ago
CustomField.php
5 years ago
CustomFieldType.php
5 years ago
Date.php
5 years ago
DateTimeFormat.php
5 years ago
ExifData.php
5 years ago
Image.php
5 years ago
Images.php
5 years ago
Label.php
5 years ago
LinkLabel.php
5 years ago
LinkToMenu.php
5 years ago
MediaLink.php
5 years ago
Message.php
5 years ago
Meta.php
5 years ago
MissingImageSize.php
5 years ago
NumberFormat.php
5 years ago
NumberOfItems.php
5 years ago
Password.php
5 years ago
PathScope.php
5 years ago
Post.php
5 years ago
PostFormatIcon.php
5 years ago
PostLink.php
5 years ago
PostStatus.php
5 years ago
PostType.php
5 years ago
Pro.php
5 years ago
Separator.php
5 years ago
StatusIcon.php
5 years ago
StringLimit.php
5 years ago
Taxonomy.php
5 years ago
Term.php
5 years ago
TermLink.php
5 years ago
Time.php
5 years ago
Toggle.php
5 years ago
Type.php
5 years ago
User.php
5 years ago
UserLink.php
5 years ago
Width.php
5 years ago
WordLimit.php
5 years ago
WordsPerMinute.php
5 years ago
PostType.php
102 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Settings\Column; |
| 4 | |
| 5 | use AC; |
| 6 | use AC\Settings; |
| 7 | use AC\View; |
| 8 | |
| 9 | class PostType extends Settings\Column { |
| 10 | |
| 11 | /** |
| 12 | * @var string |
| 13 | */ |
| 14 | private $post_type; |
| 15 | |
| 16 | /** |
| 17 | * @var bool |
| 18 | */ |
| 19 | private $show_any; |
| 20 | |
| 21 | public function __construct( AC\Column $column, $show_any = false ) { |
| 22 | parent::__construct( $column ); |
| 23 | |
| 24 | $this->show_any = $show_any; |
| 25 | } |
| 26 | |
| 27 | protected function define_options() { |
| 28 | return [ 'post_type' ]; |
| 29 | } |
| 30 | |
| 31 | public function create_view() { |
| 32 | $options = $this->get_post_type_labels(); |
| 33 | |
| 34 | if ( $this->show_any ) { |
| 35 | $options = [ 'any' => __( 'All Post Types', 'codepress-admin-columns' ) ] + $options; |
| 36 | } |
| 37 | |
| 38 | $setting = $this->create_element( 'select' ) |
| 39 | ->set_attribute( 'data-label', 'update' ) |
| 40 | ->set_options( $options ); |
| 41 | |
| 42 | $view = new View( [ |
| 43 | 'label' => __( 'Post Type', 'codepress-admin-columns' ), |
| 44 | 'setting' => $setting, |
| 45 | ] ); |
| 46 | |
| 47 | return $view; |
| 48 | } |
| 49 | |
| 50 | private function add_slug_to_duplicate_post_type_label( $options ) { |
| 51 | $values = array_values( $options ); |
| 52 | |
| 53 | // Add slug to duplicate post type labels |
| 54 | foreach ( $options as $k => $label ) { |
| 55 | if ( count( array_keys( $values, $label ) ) > 1 ) { |
| 56 | $options[ $k ] .= sprintf( ' (%s)', $k ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return $options; |
| 61 | } |
| 62 | |
| 63 | private function get_post_type_labels() { |
| 64 | $options = []; |
| 65 | |
| 66 | $post_types = get_post_types(); |
| 67 | |
| 68 | if ( ! is_array( $post_types ) ) { |
| 69 | return $options; |
| 70 | } |
| 71 | |
| 72 | foreach ( $post_types as $post_type ) { |
| 73 | $post_type_object = get_post_type_object( $post_type ); |
| 74 | $options[ $post_type ] = $post_type_object->labels->name; |
| 75 | } |
| 76 | |
| 77 | $options = $this->add_slug_to_duplicate_post_type_label( $options ); |
| 78 | |
| 79 | natcasesort( $options ); |
| 80 | |
| 81 | return $options; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return string |
| 86 | */ |
| 87 | public function get_post_type() { |
| 88 | return $this->post_type; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param string $post_type |
| 93 | * |
| 94 | * @return true |
| 95 | */ |
| 96 | public function set_post_type( $post_type ) { |
| 97 | $this->post_type = $post_type; |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | } |