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