1 | <?php |
---|
2 | |
---|
3 | namespace AC\Helper; |
---|
4 | |
---|
5 | use WP_Post; |
---|
6 | |
---|
7 | class Post { |
---|
8 | |
---|
9 | /** |
---|
10 | * @param int $id |
---|
11 | * |
---|
12 | * @return bool |
---|
13 | */ |
---|
14 | public function exists( $id ) { |
---|
15 | return $this->get_raw_field( 'ID', $id ) ? true : false; |
---|
16 | } |
---|
17 | |
---|
18 | /** |
---|
19 | * @param int $id Post ID |
---|
20 | * |
---|
21 | * @return false|string Post Title |
---|
22 | */ |
---|
23 | public function get_raw_post_title( $id ) { |
---|
24 | return ac_helper()->post->get_raw_field( 'post_title', $id ); |
---|
25 | } |
---|
26 | |
---|
27 | /** |
---|
28 | * @param int $post_id Post ID |
---|
29 | * @param int $words |
---|
30 | * |
---|
31 | * @return string Post Excerpt. |
---|
32 | * @since 1.0 |
---|
33 | */ |
---|
34 | public function excerpt( $post_id, $words = 400 ) { |
---|
35 | global $post; |
---|
36 | |
---|
37 | $save_post = $post; |
---|
38 | $post = get_post( $post_id ); |
---|
39 | |
---|
40 | setup_postdata( $post ); |
---|
41 | |
---|
42 | $excerpt = get_the_excerpt(); |
---|
43 | $post = $save_post; |
---|
44 | |
---|
45 | if ( $post ) { |
---|
46 | setup_postdata( $post ); |
---|
47 | } |
---|
48 | |
---|
49 | return ac_helper()->string->trim_words( $excerpt, $words ); |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * @param string $post_type |
---|
54 | * @param bool $plural |
---|
55 | * |
---|
56 | * @return bool |
---|
57 | */ |
---|
58 | public function get_post_type_label( $post_type, $plural = false ) { |
---|
59 | $post_type = get_post_type_object( $post_type ); |
---|
60 | |
---|
61 | if ( ! $post_type ) { |
---|
62 | return false; |
---|
63 | } |
---|
64 | |
---|
65 | if ( $plural ) { |
---|
66 | return $post_type->labels->name; |
---|
67 | } |
---|
68 | |
---|
69 | return $post_type->labels->singular_name; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * @param string $field Field |
---|
74 | * @param int $id Post ID |
---|
75 | * |
---|
76 | * @return string|false |
---|
77 | */ |
---|
78 | public function get_raw_field( $field, $id ) { |
---|
79 | global $wpdb; |
---|
80 | |
---|
81 | if ( ! $id || ! is_numeric( $id ) ) { |
---|
82 | return false; |
---|
83 | } |
---|
84 | |
---|
85 | $sql = " |
---|
86 | SELECT " . $wpdb->_real_escape( $field ) . " |
---|
87 | FROM $wpdb->posts |
---|
88 | WHERE ID = %d |
---|
89 | LIMIT 1 |
---|
90 | "; |
---|
91 | |
---|
92 | return $wpdb->get_var( $wpdb->prepare( $sql, $id ) ); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Get Post Title or Media Filename |
---|
97 | * |
---|
98 | * @param int|WP_Post $post |
---|
99 | * |
---|
100 | * @return bool|string |
---|
101 | */ |
---|
102 | public function get_title( $post ) { |
---|
103 | $post = get_post( $post ); |
---|
104 | |
---|
105 | if ( ! $post ) { |
---|
106 | return false; |
---|
107 | } |
---|
108 | |
---|
109 | $title = $post->post_title; |
---|
110 | |
---|
111 | if ( 'attachment' == $post->post_type ) { |
---|
112 | $title = ac_helper()->image->get_file_name( $post->ID ); |
---|
113 | } |
---|
114 | |
---|
115 | return $title; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * @param WP_Post $post Post |
---|
120 | * |
---|
121 | * @return false|string Dash icon with tooltip |
---|
122 | */ |
---|
123 | public function get_status_icon( $post ) { |
---|
124 | $icon = false; |
---|
125 | |
---|
126 | switch ( $post->post_status ) { |
---|
127 | case 'private' : |
---|
128 | $icon = ac_helper()->html->tooltip( ac_helper()->icon->dashicon( [ 'icon' => 'hidden', 'class' => 'gray' ] ), __( 'Private' ) ); |
---|
129 | break; |
---|
130 | case 'publish' : |
---|
131 | $icon = ac_helper()->html->tooltip( ac_helper()->icon->dashicon( [ 'icon' => 'yes', 'class' => 'blue large' ] ), __( 'Published' ) ); |
---|
132 | break; |
---|
133 | case 'draft' : |
---|
134 | $icon = ac_helper()->html->tooltip( ac_helper()->icon->dashicon( [ 'icon' => 'edit', 'class' => 'green' ] ), __( 'Draft' ) ); |
---|
135 | break; |
---|
136 | case 'pending' : |
---|
137 | $icon = ac_helper()->html->tooltip( ac_helper()->icon->dashicon( [ 'icon' => 'backup', 'class' => 'orange' ] ), __( 'Pending for review' ) ); |
---|
138 | break; |
---|
139 | case 'future' : |
---|
140 | $icon = ac_helper()->html->tooltip( ac_helper()->icon->dashicon( [ 'icon' => 'clock' ] ), __( 'Scheduled' ) . ': <em>' . ac_helper()->date->date( $post->post_date, 'wp_date_time' ) . '</em>' ); |
---|
141 | |
---|
142 | // Missed schedule |
---|
143 | if ( ( time() - mysql2date( 'G', $post->post_date_gmt ) ) > 0 ) { |
---|
144 | $icon .= ac_helper()->html->tooltip( ac_helper()->icon->dashicon( [ 'icon' => 'flag', 'class' => 'gray' ] ), __( 'Missed schedule' ) ); |
---|
145 | } |
---|
146 | break; |
---|
147 | } |
---|
148 | |
---|
149 | return $icon; |
---|
150 | } |
---|
151 | |
---|
152 | } |
---|