1 | <?php |
---|
2 | |
---|
3 | namespace AC\Settings\Column; |
---|
4 | |
---|
5 | use AC\Settings; |
---|
6 | use AC\View; |
---|
7 | |
---|
8 | class Post extends Settings\Column |
---|
9 | implements Settings\FormatValue { |
---|
10 | |
---|
11 | /** |
---|
12 | * @var string |
---|
13 | */ |
---|
14 | private $post_property; |
---|
15 | |
---|
16 | protected function set_name() { |
---|
17 | $this->name = 'post'; |
---|
18 | } |
---|
19 | |
---|
20 | protected function define_options() { |
---|
21 | return [ |
---|
22 | 'post_property_display' => 'title', |
---|
23 | ]; |
---|
24 | } |
---|
25 | |
---|
26 | public function get_dependent_settings() { |
---|
27 | $setting = []; |
---|
28 | |
---|
29 | switch ( $this->get_post_property_display() ) { |
---|
30 | case 'thumbnail' : |
---|
31 | $setting[] = new Settings\Column\Image( $this->column ); |
---|
32 | break; |
---|
33 | } |
---|
34 | |
---|
35 | $setting[] = new Settings\Column\PostLink( $this->column ); |
---|
36 | |
---|
37 | return $setting; |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * @param int $id |
---|
42 | * @param mixed $original_value |
---|
43 | * |
---|
44 | * @return string|int |
---|
45 | */ |
---|
46 | public function format( $id, $original_value ) { |
---|
47 | |
---|
48 | switch ( $this->get_post_property_display() ) { |
---|
49 | |
---|
50 | case 'author' : |
---|
51 | $value = ac_helper()->user->get_display_name( ac_helper()->post->get_raw_field( 'post_author', $id ) ); |
---|
52 | |
---|
53 | break; |
---|
54 | case 'thumbnail' : |
---|
55 | $value = get_post_thumbnail_id( $id ); |
---|
56 | |
---|
57 | break; |
---|
58 | case 'title' : |
---|
59 | $value = ac_helper()->post->get_title( $id ); |
---|
60 | |
---|
61 | break; |
---|
62 | default : |
---|
63 | $value = $id; |
---|
64 | } |
---|
65 | |
---|
66 | return $value; |
---|
67 | } |
---|
68 | |
---|
69 | public function create_view() { |
---|
70 | $select = $this->create_element( 'select' ) |
---|
71 | ->set_attribute( 'data-refresh', 'column' ) |
---|
72 | ->set_options( $this->get_display_options() ); |
---|
73 | |
---|
74 | $view = new View( [ |
---|
75 | 'label' => __( 'Display', 'codepress-admin-columns' ), |
---|
76 | 'setting' => $select, |
---|
77 | ] ); |
---|
78 | |
---|
79 | return $view; |
---|
80 | } |
---|
81 | |
---|
82 | protected function get_display_options() { |
---|
83 | $options = [ |
---|
84 | 'title' => __( 'Title' ), |
---|
85 | 'id' => __( 'ID' ), |
---|
86 | 'author' => __( 'Author' ), |
---|
87 | 'thumbnail' => _x( 'Featured Image', 'post' ), |
---|
88 | ]; |
---|
89 | |
---|
90 | asort( $options ); |
---|
91 | |
---|
92 | return $options; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * @return string |
---|
97 | */ |
---|
98 | public function get_post_property_display() { |
---|
99 | return $this->post_property; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * @param string $post_property |
---|
104 | * |
---|
105 | * @return bool |
---|
106 | */ |
---|
107 | public function set_post_property_display( $post_property ) { |
---|
108 | $this->post_property = $post_property; |
---|
109 | |
---|
110 | return true; |
---|
111 | } |
---|
112 | |
---|
113 | } |
---|